Skip to content

Commit

Permalink
Fix Orientations IO (#293)
Browse files Browse the repository at this point in the history
* Fix Orientations IO

* Allow different primitive matrices

* Remove duplicate methods

* Update src/gemdat/rotations.py

Co-authored-by: Stef Smeets <stefsmeets@users.noreply.github.com>

* Fix plot test

* Remove .set_transform

* Fix docstrings

* Use functions instead of classes for transforms

* Avoid expensive np sqrt

* Code cleanup

* Return Orientation from transforms

* Refactor Orientations

* Update tests

* Fix line endings

* Update hooks

---------

Co-authored-by: Stef Smeets <stefsmeets@users.noreply.github.com>
Co-authored-by: Stef Smeets <s.smeets@esciencecenter.nl>
  • Loading branch information
3 people authored Apr 8, 2024
1 parent a5ff972 commit e5e4250
Show file tree
Hide file tree
Showing 7 changed files with 457 additions and 391 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exclude: |
\.vol
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
Expand All @@ -23,7 +23,7 @@ repos:
hooks:
- id: nbcheckorder
- repo: https://github.com/google/yapf
rev: v0.40.1
rev: v0.40.2
hooks:
- id: yapf
additional_dependencies:
Expand All @@ -34,17 +34,17 @@ repos:
hooks:
- id: docformatter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.3.5
hooks:
- id: ruff
args: [--fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
rev: v1.9.0
hooks:
- id: mypy
additional_dependencies: [matplotlib, MDAnalysis, numpy, pymatgen, rich, scikit-image, scipy]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.7.0
rev: 1.8.5
hooks:
- id: nbqa-yapf
additional_dependencies: [toml, yapf]
Expand Down
36 changes: 13 additions & 23 deletions src/gemdat/plots/matplotlib/_rotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,44 @@
from scipy.optimize import curve_fit
from scipy.stats import skewnorm

from gemdat.rotations import Orientations, calculate_spherical_areas, mean_squared_angular_displacement
from gemdat.rotations import (
Orientations,
calculate_spherical_areas,
mean_squared_angular_displacement,
)
from gemdat.utils import cartesian_to_spherical


def rectilinear_plot(*,
orientations: Orientations,
shape: tuple[int, int] = (90, 360),
symmetrize: bool = True,
normalize: bool = True) -> plt.Figure:
"""Plot a rectilinear projection of a spherical function.
normalize_histo: bool = True) -> plt.Figure:
"""Plot a rectilinear projection of a spherical function. This function
uses the transformed trajectory.
Parameters
----------
orientations : Orientations
The unit vector trajectories
symmetrize : bool, optional
If True, use the symmetrized trajectory
shape : tuple
The shape of the spherical sector in which the trajectory is plotted
normalize : bool, optional
normalize_histo : bool, optional
If True, normalize the histogram by the area of the bins, by default True
Returns
-------
fig : matplotlib.figure.Figure
Output figure
"""

if symmetrize:
trajectory = orientations.get_symmetric_trajectory()
else:
trajectory = orientations.get_conventional_coordinates()
# Convert the trajectory to spherical coordinates
trajectory = cartesian_to_spherical(trajectory, degrees=True)
trajectory = cartesian_to_spherical(orientations.vectors, degrees=True)

az = trajectory[:, :, 0].flatten()
el = trajectory[:, :, 1].flatten()

hist, xedges, yedges = np.histogram2d(el, az, shape)

if normalize:
if normalize_histo:
# Normalize by the area of the bins
areas = calculate_spherical_areas(shape)
hist = np.divide(hist, areas)
Expand Down Expand Up @@ -80,16 +77,13 @@ def rectilinear_plot(*,

def bond_length_distribution(*,
orientations: Orientations,
symmetrize: bool = True,
bins: int = 1000) -> plt.Figure:
"""Plot the bond length probability distribution.
Parameters
----------
orientations : Orientations
The unit vector trajectories
symmetrize : bool, optional
If True, use the symmetrized trajectory
bins : int, optional
The number of bins, by default 1000
Expand All @@ -99,12 +93,8 @@ def bond_length_distribution(*,
Output figure
"""

if symmetrize:
trajectory = orientations.get_symmetric_trajectory()
else:
trajectory = orientations.get_conventional_coordinates()
# Convert the trajectory to spherical coordinates
trajectory = cartesian_to_spherical(trajectory, degrees=True)
trajectory = cartesian_to_spherical(orientations.vectors, degrees=True)

fig, ax = plt.subplots()

Expand Down Expand Up @@ -163,7 +153,7 @@ def unit_vector_autocorrelation(
"""

# The trajectory is expected to have shape (n_times, n_particles, n_coordinates)
trajectory = orientations.get_unit_vectors_trajectory()
trajectory = orientations.vectors

ac = mean_squared_angular_displacement(trajectory)
ac_std = ac.std(axis=0)
Expand Down
Loading

0 comments on commit e5e4250

Please sign in to comment.