-
Notifications
You must be signed in to change notification settings - Fork 28
Use phase class2 #205
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
Merged
Merged
Use phase class2 #205
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1fc6a87
New Feature: Add Simulation classes using Phase Class
CSSFrancis 8282721
New Feature: Add intensity to reciprocal lattice vector
CSSFrancis bfe0505
Testing: Add tests for adding intensity of reciprocal_lattive_vector.py
CSSFrancis ac97ee6
New Feature: Added zerobeam to reciprocal lattice vector
CSSFrancis b0b67fa
New Feature: Moved Shape factor calculation to shape_factor_models.py
CSSFrancis 6ec15b8
Testing: Added testing for simulation_generator.
CSSFrancis f3bd483
Testing: Testing the zero Beam is most intense.
CSSFrancis d7c86ec
Min Version: Bump orix min version
CSSFrancis ef03b39
Packaging: Include function and classes in init file
CSSFrancis d1839a5
Testing: Clean up tests due to changed function name
CSSFrancis 3567308
Testing: Add coverage for simulation classes
CSSFrancis 66f0133
Documentation: Fixed Broken documentation
CSSFrancis 3d9c662
Refactor: Fixes per @viljarjf
CSSFrancis 99327ad
Refactor: Clean up intensity in rlv class
CSSFrancis 2ad4567
Refactor: Remove calculate structure factor
CSSFrancis 273ffcc
Refactor: Changes from @hakonanes
CSSFrancis f32bfda
Refactor: Improve Documentation
CSSFrancis a8c68a3
Testing: Test simulation continuity
CSSFrancis 3a27a7b
Documentation: Add intensity parameter
CSSFrancis 9bf9404
Manifest: Add numpy old file
CSSFrancis 9c216f8
Refactor: Refactor DiffractingVector code
CSSFrancis 2ada567
Refactor: Refactor Rotation implementation
CSSFrancis b598a5a
BugFix: Fix HKL computation
CSSFrancis e0815d5
Refactor: Fix MANIFEST.in
CSSFrancis dff72bb
Plotting: Add interactive plotting
CSSFrancis 8b92dce
Testing: no cover interactive plotting
CSSFrancis f3d9b95
Documentation: Add docstrings for interactive plotting
CSSFrancis 82b3333
Documentation: Remove lattice rotation
CSSFrancis c39e357
Bugfix: Fix polar conversion
CSSFrancis 05b3736
Bugfix: Fix Rotation with RLV
CSSFrancis bfbd299
Revert "Bugfix: Fix Rotation with RLV"
CSSFrancis 96a48ec
Testing: Improve testing
CSSFrancis 1f2150d
Refactor: Improve based on @hakonanes and @viljarjf's suggestions
CSSFrancis efde31d
Refactor: Tests including zero-vector
CSSFrancis da45a32
Refactor: Remove changes from RLV
CSSFrancis 0e97f22
Refactor: suggestions for @hakonanes
CSSFrancis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017-2024 The diffsims developers | ||
# | ||
# This file is part of diffsims. | ||
# | ||
# diffsims is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# diffsims is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with diffsims. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from diffsims.crystallography import ReciprocalLatticeVector | ||
import numpy as np | ||
from orix.vector.miller import _transform_space | ||
from orix.quaternion import Rotation | ||
|
||
|
||
class DiffractingVector(ReciprocalLatticeVector): | ||
r"""Reciprocal lattice vectors :math:`(hkl)` for use in electron | ||
diffraction analysis and simulation. | ||
|
||
All lengths are assumed to be given in Å or inverse Å. | ||
|
||
This extends the :class:`ReciprocalLatticeVector` class. `DiffractingVector` | ||
focus on the subset of reciprocal lattice vectors that are relevant for | ||
electron diffraction based on the intersection of the Ewald sphere with the | ||
reciprocal lattice. | ||
|
||
This class is only used internally to store the DiffractionVectors generated from the | ||
:class:`~diffsims.simulations.DiffractionSimulation` class. It is not (currently) | ||
intended to be used directly by the user. | ||
|
||
Parameters | ||
---------- | ||
phase : orix.crystal_map.Phase | ||
A phase with a crystal lattice and symmetry. | ||
xyz : numpy.ndarray, list, or tuple, optional | ||
Cartesian coordinates of indices of reciprocal lattice vector(s) | ||
``hkl``. Default is ``None``. This, ``hkl``, or ``hkil`` is | ||
required. | ||
hkl : numpy.ndarray, list, or tuple, optional | ||
Indices of reciprocal lattice vector(s). Default is ``None``. | ||
This, ``xyz``, or ``hkil`` is required. | ||
hkil : numpy.ndarray, list, or tuple, optional | ||
Indices of reciprocal lattice vector(s), often preferred over | ||
``hkl`` in trigonal and hexagonal lattices. Default is ``None``. | ||
This, ``xyz``, or ``hkl`` is required. | ||
intensity : numpy.ndarray, list, or tuple, optional | ||
Intensity of the diffraction vector(s). Default is ``None``. | ||
rotation : orix.quaternion.Rotation, optional | ||
Rotation matrix previously applied to the reciprocal lattice vector(s) and the | ||
lattice of the phase. Default is ``None`` which corresponds to the | ||
identity matrix. | ||
|
||
|
||
Examples | ||
-------- | ||
>>> from diffpy.structure import Atom, Lattice, Structure | ||
>>> from orix.crystal_map import Phase | ||
>>> from diffsims.crystallography import DiffractingVector | ||
>>> phase = Phase( | ||
... "al", | ||
... space_group=225, | ||
... structure=Structure( | ||
... lattice=Lattice(4.04, 4.04, 4.04, 90, 90, 90), | ||
... atoms=[Atom("Al", [0, 0, 1])], | ||
... ), | ||
... ) | ||
>>> rlv = DiffractingVector(phase, hkl=[[1, 1, 1], [2, 0, 0]]) | ||
>>> rlv | ||
ReciprocalLatticeVector (2,), al (m-3m) | ||
[[1. 1. 1.] | ||
[2. 0. 0.]] | ||
|
||
""" | ||
|
||
def __init__(self, phase, xyz=None, hkl=None, hkil=None, intensity=None): | ||
super().__init__(phase, xyz=xyz, hkl=hkl, hkil=hkil) | ||
if intensity is None: | ||
self._intensity = np.full(self.shape, np.nan) | ||
elif len(intensity) != self.size: | ||
raise ValueError("Length of intensity array must match number of vectors") | ||
else: | ||
self._intensity = np.array(intensity) | ||
|
||
def __getitem__(self, key): | ||
new_data = self.data[key] | ||
dv_new = self.__class__(self.phase, xyz=new_data) | ||
|
||
if np.isnan(self.structure_factor).all(): | ||
dv_new._structure_factor = np.full(dv_new.shape, np.nan, dtype="complex128") | ||
|
||
else: | ||
dv_new._structure_factor = self.structure_factor[key] | ||
if np.isnan(self.theta).all(): | ||
dv_new._theta = np.full(dv_new.shape, np.nan) | ||
else: | ||
dv_new._theta = self.theta[key] | ||
if np.isnan(self.intensity).all(): | ||
dv_new._intensity = np.full(dv_new.shape, np.nan) | ||
else: | ||
slic = self.intensity[key] | ||
if not hasattr(slic, "__len__"): | ||
slic = np.array( | ||
[ | ||
slic, | ||
] | ||
) | ||
dv_new._intensity = slic | ||
|
||
return dv_new | ||
|
||
@property | ||
def basis_rotation(self): | ||
""" | ||
Returns the lattice basis rotation. | ||
""" | ||
return Rotation.from_matrix(self.phase.structure.lattice.baserot) | ||
|
||
def rotate_with_basis(self, rotation): | ||
"""Rotate both vectors and the basis with a given `Rotation`. | ||
This differs from simply multiplying with a `Rotation`, | ||
as that would NOT update the basis. | ||
|
||
Parameters | ||
---------- | ||
rot : orix.quaternion.Rotation | ||
A rotation to apply to vectors and the basis. | ||
|
||
Returns | ||
------- | ||
DiffractingVector | ||
A new DiffractingVector with the rotated vectors and basis. This maintains | ||
the hkl indices of the vectors, but the underlying vector xyz coordinates | ||
are rotated by the given rotation. | ||
|
||
Notes | ||
----- | ||
Rotating the lattice basis may lead to undefined behavior in orix as it violates | ||
the assumption that the basis is aligned with the crystal axes. Particularly, | ||
applying symmetry operations to the phase may lead to unexpected results. | ||
""" | ||
hakonanes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if rotation.size != 1: | ||
raise ValueError("Rotation must be a single rotation") | ||
# rotate basis | ||
new_phase = self.phase.deepcopy() | ||
br = new_phase.structure.lattice.baserot | ||
# In case the base rotation is set already | ||
new_br = br @ rotation.to_matrix().squeeze() | ||
new_phase.structure.lattice.setLatPar(baserot=new_br) | ||
# rotate vectors | ||
vecs = ~rotation * self.to_miller() | ||
return ReciprocalLatticeVector(new_phase, xyz=vecs.data) | ||
|
||
@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 calculate_structure_factor(self): | ||
raise NotImplementedError( | ||
"Structure factor calculation not implemented for DiffractionVector. " | ||
"Use ReciprocalLatticeVector instead." | ||
) | ||
|
||
def to_flat_polar(self): | ||
"""Return the vectors in polar coordinates as projected onto the x,y plane""" | ||
flat_self = self.flatten() | ||
r = np.linalg.norm(flat_self.data[:, :2], axis=1) | ||
theta = np.arctan2( | ||
flat_self.data[:, 1], | ||
flat_self.data[:, 0], | ||
) | ||
return r, theta |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.