Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use phase class #201

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1f3f4b1
New Feature: Add Deprecations
CSSFrancis Oct 20, 2023
1a9a439
New Feature: Add deprecation wrappers
CSSFrancis Oct 20, 2023
151a927
Documentation: Added documentation about deprecating code.
CSSFrancis Oct 20, 2023
1ecb824
Testing: Added test for uncovered line.
CSSFrancis Oct 20, 2023
825691a
Refactor: Added new class for simulating multiple rotations
CSSFrancis Oct 24, 2023
9ff5fe0
Refactor: Add tests for new simulations
CSSFrancis Oct 24, 2023
022716d
Documentation: Add example for simulating one diffraction pattern
CSSFrancis Oct 24, 2023
4630e69
Deprecation: Deprecate old code
CSSFrancis Oct 24, 2023
0833ed4
Rollback: Rollback code for finding orientations.
CSSFrancis Oct 25, 2023
5cc74f9
Formatting: Applied `black` formatting
CSSFrancis Oct 25, 2023
02933f8
NewFeature: Added ``get_polar_coordinates`` function to ``Diffraction…
CSSFrancis Oct 25, 2023
c47bbe5
Refactor: Sped up library Simulation
CSSFrancis Oct 26, 2023
9305fe4
Refactor: Moved SimulationLibraries
CSSFrancis Oct 26, 2023
9436ef1
New Feature: Added Simulation class
CSSFrancis Oct 28, 2023
f040ded
Refactor: Cleaned SimulationGenerator tests with changed API
CSSFrancis Oct 28, 2023
b646d33
Refactor: Cleaned up examples for simulating diffraction
CSSFrancis Oct 28, 2023
130f741
Refactor: Added new plotting feature for plotting rotations
CSSFrancis Oct 28, 2023
eea91c6
New Feature: Added polar flatten to simulation to get an optimized re…
CSSFrancis Nov 6, 2023
aa96b37
Testing: Removed extra tests
CSSFrancis Nov 6, 2023
6141b9f
Manifest: Fixed failing manifest
CSSFrancis Nov 15, 2023
8060b7a
New Feature: Added direct beam to plotting
CSSFrancis Nov 15, 2023
759cbfc
Deprecation: Deprecate old simulation methods
CSSFrancis Nov 15, 2023
68e78ad
Upped min Orix version 0.9.0 -->0.11.0
CSSFrancis Nov 15, 2023
fdde53c
Fix test failures
CSSFrancis Nov 15, 2023
dda0693
Refactor: Clean up tests and improve coverage for Simualtions
CSSFrancis Nov 21, 2023
1701287
Refactor: Fix iter for simulations
CSSFrancis Nov 26, 2023
11eb813
Refactor: Fixed test coverage and removed some extra code
CSSFrancis Nov 27, 2023
a82b067
Testing: Testing to 100%
CSSFrancis Nov 28, 2023
d8de653
Documentation: Added Copyright statements
CSSFrancis Dec 14, 2023
96f0b28
Documentation: Improve documentation for simulations
CSSFrancis Dec 14, 2023
69a2efd
Documentation: Add to CHANGELOG.rst
CSSFrancis Dec 15, 2023
94085d0
NewFeature: Add simulate1d
CSSFrancis Dec 19, 2023
0916cd3
Refactor: Adjust documentation per @pc494 and Simulation --> Simulati…
CSSFrancis Dec 19, 2023
c47baff
Testing: Fix hexagonal Simulation1D
CSSFrancis Dec 19, 2023
107d27a
Bugfix: Fix intensity scaling with zero beam
CSSFrancis Jan 14, 2024
d928173
Testing: Increase coverage of simulation1d
CSSFrancis Jan 14, 2024
e4de186
Documentation: Add documentation to ``__init__`` function
CSSFrancis Jan 15, 2024
543d3f2
BugFix: Fix labels for miller indices.
CSSFrancis Feb 1, 2024
f234a85
BugFix: Fix labels for miller indices, with rounding
CSSFrancis Feb 1, 2024
32fdbe2
BugFix: Fix euler rotation
CSSFrancis Feb 2, 2024
b7fa8f5
Documentation: Add migration guide
CSSFrancis Feb 2, 2024
8427342
Coverage: Add tests for get_current_rotation
CSSFrancis Feb 3, 2024
d23285a
BugFix: Fix hexagonal issues.
CSSFrancis Feb 7, 2024
118ccf8
Refactor: Add methods for easier indexing related to Orientation Mapping
CSSFrancis Feb 14, 2024
28e34eb
Coverage: Increased coverage for simulation2d
CSSFrancis Feb 14, 2024
38ca9dd
Bugfix: Fix simulation for multiple phases with different lengths.
CSSFrancis Feb 14, 2024
64e4971
Documentation: Clean up Deprecations and circular imports
CSSFrancis Mar 27, 2024
6840707
BugFix: Fix simulation intensities.
CSSFrancis Apr 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- os: ubuntu-latest
python-version: 3.7
OLDEST_SUPPORTED_VERSION: true
DEPENDENCIES: diffpy.structure==3.0.0 matplotlib==3.3 numpy==1.17 orix==0.9.0 scipy==1.1 tqdm==4.9
DEPENDENCIES: diffpy.structure==3.0.2 matplotlib==3.3 numpy==1.17 orix==0.11.0 scipy==1.2 tqdm==4.9
LABEL: -oldest
steps:
- uses: actions/checkout@v3
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ Unreleased
Added
-----
- Explicit support for Python 3.11.
- Added deprecation tools for deprecating functions and arguments.
- Added new class :class:`diffsims.generators.simulation_generator.SimulationGenerator` for
generating kinemetic diffraction from a phase and a set of reflections.
- Added new class :class:`diffsims.generators.simulations.Simulation` for storing results
from a :class:`diffsims.generators.simulation_generator.SimulationGenerator`.
- Added Pre-Commit for code formatting.
- Added new class :class:`diffsims.simulations.Simulation1D` for storing the results of a
1D simulation.

Changed
-------
- Documentation theme from Furo to the PyData-Sphinx-Theme.

Deprecated
----------
- Deprecated :class:`diffsims.generators.diffraction_generator.DiffractionGenerator` in
favor of :class:`diffsims.generators.simulation_generator.SimulationGenerator`.
- Deprecated :class:`diffsims.generators.library_generator.DiffractionLibraryGenerator` in
favor of :class:`diffsims.generators.simulation_generator.SimulationGenerator`.
- Deprecated :class:`diffsims.diffraction_library.DiffractionLibrary` in favor of
:class:`diffsims.generators.simulations.Simulation`.

Removed
-------
Expand Down
21 changes: 21 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ Useful hints on testing:
error-prone. See `pytest documentation for more details
<https://doc.pytest.org/en/latest/how-to/parametrize.html>`_.


Deprecations
------------
'This project attempts to adhere to (pre 1.0.0) semantic versioning and ideally no functionality
should be broken between minor releases. Deprecation warnings are raised to give user a full minor
release cycle to adjust their code before a breaking change is implemented.'

The decorator should be placed right above the object signature to be deprecated::

.. code-block:: python
>>> from diffsims.utils._deprecated import deprecate
>>> @deprecate(since=0.8, removal=0.9, alternative="bar")
>>> def foo(self, n):
>>> return n + 1

>>> @property
>>> @deprecate(since=0.9, removal=0.10, alternative="another", is_function=True)
>>> def this_property(self):
>>> return 2


Build and write documentation
-----------------------------

Expand Down
1 change: 0 additions & 1 deletion diffsims/crystallography/reciprocal_lattice_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
get_refraction_corrected_wavelength,
)


_FLOAT_EPS = np.finfo(float).eps # Used to round values below 1e-16 to zero


Expand Down
94 changes: 89 additions & 5 deletions diffsims/crystallography/reciprocal_lattice_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
# You should have received a copy of the GNU General Public License
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.

from typing import Tuple
from collections import defaultdict
from copy import deepcopy

Expand Down Expand Up @@ -122,10 +122,11 @@ def __init__(self, phase, xyz=None, hkl=None, hkil=None):

self._theta = np.full(self.shape, np.nan)
self._structure_factor = np.full(self.shape, np.nan, dtype="complex128")
self._intensity = np.full(self.shape, np.nan)

def __getitem__(self, key):
miller_new = self.to_miller().__getitem__(key)
rlv_new = self.from_miller(miller_new)
new_data = self.data[key]
rlv_new = self.__class__(self.phase, xyz=new_data)

if np.isnan(self.structure_factor).all():
rlv_new._structure_factor = np.full(
Expand All @@ -139,6 +140,18 @@ def __getitem__(self, key):
else:
rlv_new._theta = self.theta[key]

if np.isnan(self.intensity).all():
rlv_new._intensity = np.full(rlv_new.shape, np.nan)
else:
slic = self.intensity[key]
if not hasattr(slic, "__len__"):
slic = np.array(
[
slic,
]
)
rlv_new._intensity = slic

return rlv_new

def __repr__(self):
Expand Down Expand Up @@ -169,7 +182,6 @@ def hkl(self):
>>> rlv.hkl
array([[1., 1., 1.],
[2., 0., 0.]])

"""

return _transform_space(self.data, "c", "r", self.phase.structure.lattice)
Expand Down Expand Up @@ -502,6 +514,28 @@ def scattering_parameter(self):

return 0.5 * self.gspacing

@property
def intensity(self):
return self._intensity

@intensity.setter
def intensity(self, value):
if not hasattr(value, "__len__"):
value = np.array(
[
value,
]
* self.size
)
if len(value) != self.size:
raise ValueError("Length of intensity array must match number of vectors")
self._intensity = np.array(value)

def rotate_from_matrix(self, rotation_matrix):
return ReciprocalLatticeVector(
phase=self.phase, xyz=np.matmul(rotation_matrix.T, self.data.T).T
)

@property
def structure_factor(self):
r"""Kinematical structure factors :math:`F`.
Expand Down Expand Up @@ -1070,7 +1104,7 @@ def from_highest_hkl(cls, phase, hkl):
return cls(phase, hkl=idx).unique()

@classmethod
def from_min_dspacing(cls, phase, min_dspacing=0.7):
def from_min_dspacing(cls, phase, min_dspacing=0.7, include_zero_beam=False):
"""Create a set of unique reciprocal lattice vectors with a
a direct space interplanar spacing greater than a lower
threshold.
Expand Down Expand Up @@ -1128,6 +1162,8 @@ def from_min_dspacing(cls, phase, min_dspacing=0.7):
dspacing = 1 / phase.structure.lattice.rnorm(hkl)
idx = dspacing >= min_dspacing
hkl = hkl[idx]
if include_zero_beam:
hkl = np.vstack((hkl, np.zeros(3, dtype=int)))
return cls(phase, hkl=hkl).unique()

@classmethod
Expand Down Expand Up @@ -1180,6 +1216,54 @@ def from_miller(cls, miller):
)
return cls(miller.phase, **{miller.coordinate_format: miller.coordinates})

def to_polar(
self, degrees: bool = False
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Convert the vectors to polar coordinates.

Parameters
----------
degrees : bool, optional
Whether to return angles in degrees (default is False).

Returns
-------
r : numpy.ndarray
Length of the vectors.
theta : numpy.ndarray
Polar angle of the vectors.
phi : numpy.ndarray
Azimuthal angle of the vectors.

Examples
--------
See :class:`ReciprocalLatticeVector` for the creation of ``rlv``

>>> rlv
ReciprocalLatticeVector (2,), al (m-3m)
[[1. 1. 1.]
[2. 0. 0.]]
>>> r, theta, phi = rlv.to_polar()
>>> r
array([1.73205081, 2. ])
>>> theta
array([0.95531662, 0. ])
>>> phi
array([0., 0.])

"""

x = self.data[:, 0]
y = self.data[:, 1]
z = self.data[:, 2]

r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y, x)

if degrees:
theta = np.rad2deg(theta)
return r, theta, z

def to_miller(self):
"""Return the vectors as a ``Miller`` instance.

Expand Down
2 changes: 2 additions & 0 deletions diffsims/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
rotation_list_generators,
sphere_mesh_generators,
zap_map_generator,
simulation_generator,
)

__all__ = [
"diffraction_generator",
"library_generator",
"rotation_list_generators",
"sphere_mesh_generators",
"simulation_generator",
"zap_map_generator",
]
4 changes: 4 additions & 0 deletions diffsims/generators/diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
lorentzian_precession,
)

from diffsims.utils._deprecated import deprecated

__all__ = [
"AtomicDiffractionGenerator",
Expand Down Expand Up @@ -153,6 +154,9 @@ class DiffractionGenerator(object):
`custom shape_factor_model` is used.
"""

@deprecated(since="0.6.0",
removal="0.7.0",
alternative="diffsims.generators.simulation_generator.SimulationGenerator")
def __init__(
self,
accelerating_voltage,
Expand Down
4 changes: 2 additions & 2 deletions diffsims/generators/library_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from diffsims.utils.sim_utils import get_points_in_sphere
from diffsims.utils.vector_utils import get_angle_cartesian_vec

from diffsims.utils._deprecated import deprecated

__all__ = [
"DiffractionLibraryGenerator",
Expand All @@ -39,7 +39,7 @@ class DiffractionLibraryGenerator:
"""Computes a library of electron diffraction patterns for specified atomic
structures and orientations.
"""

@deprecated(since="0.6.0", alternative="Diffsims.generators.SimulationGenerator")
def __init__(self, electron_diffraction_calculator):
"""Initialises the generator with a diffraction calculator.

Expand Down
Loading
Loading