Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ source = diffsims
include = */diffsims/*
omit =
*/diffsims/__init__.py
*/diffsims/utils/atomic_scattering_params.py
*/diffsims/utils/scattering_params.py
*/diffsims/version.py
*/diffsims/utils/__init__.py
6 changes: 0 additions & 6 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
name: Pull request
about: A pull request that fixes a bug or adds a feature

- [ ] ready for review and merge?
---

**Release Notes**
> major or minor (all but urgent bugfixes go in the former)
> new feature / improvement / bugfix / developer change
Summary: 1 line per end-user relevant change

**What does this PR do? Please describe and/or link to an open issue.**
A clear and concise description of what the code in this PR does. Does it fix a bug or add a new feature?
If it is related to an open issue, reference it here.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

Expand Down
22 changes: 10 additions & 12 deletions diffsims/generators/diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from diffsims.sims.diffraction_simulation import DiffractionSimulation
from diffsims.sims.diffraction_simulation import ProfileSimulation

from diffsims.utils.atomic_scattering_params import ATOMIC_SCATTERING_PARAMS
from diffsims.utils import get_method_from_string
from diffsims.utils.xtables_scattering_params import ATOMIC_SCATTERING_PARAMS
from diffsims.utils.lobato_scattering_params import ATOMIC_SCATTERING_PARAMS_LOBATO
from diffsims.utils.sim_utils import get_electron_wavelength,\
get_kinematical_intensities, get_unique_families, get_points_in_sphere, \
get_vectorized_list_for_atomic_scattering_factors, is_lattice_hexagonal
Expand Down Expand Up @@ -59,6 +61,7 @@ class DiffractionGenerator(object):
"""
# TODO: Refactor the excitation error to a structure property.


def __init__(self,
accelerating_voltage,
max_excitation_error,
Expand All @@ -67,17 +70,12 @@ def __init__(self,
self.wavelength = get_electron_wavelength(accelerating_voltage)
self.max_excitation_error = max_excitation_error
self.debye_waller_factors = debye_waller_factors or {}

scattering_params_dict = {'xtables':ATOMIC_SCATTERING_PARAMS,
'lobato':ATOMIC_SCATTERING_PARAMS_LOBATO}
self.scattering_params = get_method_from_string(scattering_params,
scattering_params_dict)

scattering_params_dict = {
'lobato': 'lobato',
'xtables': 'xtables'
}
if scattering_params in scattering_params_dict:
self.scattering_params = scattering_params_dict[scattering_params]
else:
raise NotImplementedError("The scattering parameters `{}` is not implemented. "
"See documentation for available "
"implementations.".format(scattering_params))

def calculate_ed_data(self,
structure,
Expand All @@ -96,7 +94,7 @@ def calculate_ed_data(self,
reciprocal angstroms.
with_direct_beam : bool
If True, the direct beam is included in the simulated diffraction
pattern. If False, it is not.
pattern.

Returns
-------
Expand Down
46 changes: 0 additions & 46 deletions diffsims/libraries/diffraction_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,8 @@
# You should have received a copy of the GNU General Public License
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.

import pickle
import numpy as np


def load_DiffractionLibrary(filename, safety=False):
"""Loads a previously saved diffraction library.

Parameters
----------
filename : str
The location of the file to be loaded
safety : bool (defaults to False)
Unpickling is risky, this variable requires you to acknowledge this.

Returns
-------
DiffractionLibrary
Previously saved Library

See Also
--------
DiffractionLibrary.pickle_library()

"""
if safety:
with open(filename, 'rb') as handle:
return pickle.load(handle)
else:
raise RuntimeError('Unpickling is risky, turn safety to True if \
trust the author of this content')


def _get_library_entry_from_angles(library, phase, angles):
"""Finds an element that is orientation within 1e-5 of that specified.

Expand Down Expand Up @@ -152,19 +122,3 @@ def get_library_entry(self, phase=None, angle=None):
'pixel_coords': phase_entry['pixel_coords'][orientation_index],
'pattern_norm': np.linalg.norm(phase_entry['intensities'][orientation_index])
}

def pickle_library(self, filename):
"""Saves a diffraction library in the pickle format.

Parameters
----------
filename : str
The location in which to save the file

See Also
--------
load_DiffractionLibrary()

"""
with open(filename, 'wb') as handle:
pickle.dump(self, handle, protocol=pickle.HIGHEST_PROTOCOL)
22 changes: 0 additions & 22 deletions diffsims/tests/test_library/test_diffraction_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from diffsims.sims.diffraction_simulation import DiffractionSimulation

from diffsims.libraries.diffraction_library import load_DiffractionLibrary
from diffsims.libraries.structure_library import StructureLibrary


Expand Down Expand Up @@ -58,21 +57,6 @@ def test_get_library_small_offset(get_library):
assert np.allclose(alpha, beta)


def test_library_io(get_library):
get_library.pickle_library('file_01.pickle')
loaded_library = load_DiffractionLibrary('file_01.pickle', safety=True)
os.remove('file_01.pickle')
# We can't check that the entire libraries are the same as the memory
# location of the 'Sim' changes
for i in range(len(get_library['Phase']['orientations'])):
np.testing.assert_allclose(get_library['Phase']['orientations'][i],
loaded_library['Phase']['orientations'][i])
np.testing.assert_allclose(get_library['Phase']['intensities'][i],
loaded_library['Phase']['intensities'][i])
np.testing.assert_allclose(get_library['Phase']['pixel_coords'][i],
loaded_library['Phase']['pixel_coords'][i])


@pytest.mark.xfail(raises=ValueError)
def test_angle_but_no_phase(get_library):
# we have given an angle but no phase
Expand All @@ -86,9 +70,3 @@ def test_unknown_library_entry(get_library):
assert isinstance(get_library.get_library_entry(phase='Phase',
angle=(1e-3, 0, 0))['Sim'],
DiffractionSimulation)


@pytest.mark.xfail(raises=RuntimeError)
def test_unsafe_loading(get_library):
get_library.pickle_library('file_01.pickle')
loaded_library = load_DiffractionLibrary('file_01.pickle')
13 changes: 13 additions & 0 deletions diffsims/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@
#
# You should have received a copy of the GNU General Public License
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.


def get_method_from_string(string,method_dict):
"""

"""

if string in method_dict.keys():
return method_dict[string]
else:
raise NotImplementedError("The option `{}` is not implemented. "
"See documentation for available "
"implementations.".format(method_dict.keys()))
35 changes: 7 additions & 28 deletions diffsims/utils/sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
from transforms3d.euler import mat2euler
from transforms3d.euler import euler2mat

from .atomic_scattering_params import ATOMIC_SCATTERING_PARAMS
from . import get_method_from_string
from .xtables_scattering_params import ATOMIC_SCATTERING_PARAMS
from .lobato_scattering_params import ATOMIC_SCATTERING_PARAMS_LOBATO

#from diffsims.sims.diffraction_simulation import DiffractionSimulation
from diffsims.utils.vector_utils import get_angle_cartesian

scattering_params_dict = {'xtables':ATOMIC_SCATTERING_PARAMS,
'lobato':ATOMIC_SCATTERING_PARAMS_LOBATO}


def get_electron_wavelength(accelerating_voltage):
"""Calculates the (relativistic) electron wavelength in Angstroms for a
Expand Down Expand Up @@ -113,31 +117,6 @@ def is_perm(hkl1, hkl2):

return pretty_unique


def get_scattering_params_dict(scattering_params):
"""Get scattering parameter dictionary from name.

Parameters
----------
scattering_params : string
Name of scattering factors. One of 'lobato', 'xtables'.

Returns
-------
scattering_params_dict : dict
Dictionary of scattering parameters mapping from element name.
"""
if scattering_params == 'lobato':
scattering_params_dict = ATOMIC_SCATTERING_PARAMS_LOBATO
elif scattering_params == 'xtables':
scattering_params_dict = ATOMIC_SCATTERING_PARAMS
else:
raise NotImplementedError("The scattering parameters `{}` are not implemented. "
"See documentation for available "
"implementations.".format(scattering_params))
return scattering_params_dict


def get_vectorized_list_for_atomic_scattering_factors(structure,
debye_waller_factors,
scattering_params):
Expand Down Expand Up @@ -168,7 +147,7 @@ def get_vectorized_list_for_atomic_scattering_factors(structure,
Debye-Waller factors for each atom in the structure.
"""

scattering_params_dict = get_scattering_params_dict(scattering_params)
scattering_params_dict = get_method_from_string(scattering_params,scattering_params_dict)

n_structures = len(structure)
coeffs = np.empty((n_structures, 5, 2))
Expand Down Expand Up @@ -306,7 +285,7 @@ def simulate_kinematic_scattering(atomic_coordinates,
ElectronDiffraction simulation.
"""
# Get atomic scattering parameters for specified element.
coeffs = np.array(get_scattering_params_dict(scattering_params)[element])
coeffs = np.array(get_method_from_string(scattering_params,scattering_params_dict)[element])

# Calculate electron wavelength for given keV.
wavelength = get_electron_wavelength(accelerating_voltage)
Expand Down
2 changes: 1 addition & 1 deletion diffsims/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__copyright__ = "Copyright 2017-2019, Diffraction Simulations in Python"
__credits__ = ["Duncan Johnstone", "Phillip Crout", "Ben Martineau", "Simon Hogas"]
__license__ = "GPL"
__version__ = "0.1.4"
__version__ = "0.2.0-rc.1"
__maintainer__ = "Duncan Johnstone"
__email__ = "dnj23@cam.ac.uk"
__status__ = "Development"