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
19 changes: 5 additions & 14 deletions diffsims/generators/diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class DiffractionGenerator(object):
max_excitation_error : float
The maximum extent of the relrods in reciprocal angstroms. Typically
equal to 1/{specimen thickness}.
debye_waller_factors : dict of str : float
debye_waller_factors : dict of str or float
Maps element names to their temperature-dependent Debye-Waller factors.

scattering_params : 'lobato' or 'xtables'
"""
# TODO: Refactor the excitation error to a structure property.

Expand All @@ -67,17 +67,8 @@ 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 {}
self.scattering_params_string = scattering_params

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 Down Expand Up @@ -109,7 +100,7 @@ def calculate_ed_data(self,
max_excitation_error = self.max_excitation_error
debye_waller_factors = self.debye_waller_factors
latt = structure.lattice
scattering_params = self.scattering_params
scattering_params = self.scattering_params_string

# Obtain crystallographic reciprocal lattice points within `max_r` and
# g-vector magnitudes for intensity calculations.
Expand Down Expand Up @@ -179,7 +170,7 @@ def calculate_profile_data(self, structure,
"""
max_r = reciprocal_radius
wavelength = self.wavelength
scattering_params = self.scattering_params
scattering_params = self.scattering_params_string

latt = structure.lattice
is_hex = is_lattice_hexagonal(latt)
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)
45 changes: 0 additions & 45 deletions diffsims/libraries/vector_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +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_VectorLibrary(filename, safety=False):
"""Loads a previously saved vectorlibrary.

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
-------
VectorLibrary
Previously saved Library

See Also
--------
VectorLibrary.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')


class DiffractionVectorLibrary(dict):
"""Maps crystal structure (phase) to diffraction vectors.

Expand Down Expand Up @@ -74,19 +45,3 @@ def __init__(self, *args, **kwargs):
self.identifiers = None
self.structures = None
self.reciprocal_radius = None

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

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

See Also
--------
load_VectorLibrary()

"""
with open(filename, 'wb') as handle:
pickle.dump(self, handle, protocol=pickle.HIGHEST_PROTOCOL)
23 changes: 0 additions & 23 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 @@ -57,22 +56,6 @@ def test_get_library_small_offset(get_library):
angle=(1e-8, 0, 0))['intensities']
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 +69,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')
17 changes: 0 additions & 17 deletions diffsims/tests/test_library/test_vector_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from diffsims.generators.library_generator import VectorLibraryGenerator

from diffsims.libraries.vector_library import load_VectorLibrary
from diffsims.libraries.structure_library import StructureLibrary


Expand All @@ -32,19 +31,3 @@ def get_library(default_structure):
structure_library = StructureLibrary(['Phase'], [default_structure], [[(0, 0, 0), (0, 0.2, 0)]])
vlg = VectorLibraryGenerator(structure_library)
return vlg.get_vector_library(0.5)


def test_library_io(get_library):
get_library.pickle_library('file_01.pickle')
loaded_library = load_VectorLibrary('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
np.testing.assert_allclose(get_library['Phase']['measurements'], loaded_library['Phase']['measurements'])
np.testing.assert_allclose(get_library['Phase']['indices'], loaded_library['Phase']['indices'])


@pytest.mark.xfail(raises=RuntimeError)
def test_unsafe_loading(get_library):
get_library.pickle_library('file_01.pickle')
loaded_library = load_VectorLibrary('file_01.pickle')
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.1dev"
__maintainer__ = "Duncan Johnstone"
__email__ = "dnj23@cam.ac.uk"
__status__ = "Development"