From 31fc2e5fabf9f9961f09fb34d3600c54984bab26 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:22:17 +0100 Subject: [PATCH 1/9] soft refactor of scattering-selection --- diffsims/generators/diffraction_generator.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/diffsims/generators/diffraction_generator.py b/diffsims/generators/diffraction_generator.py index a4e41226..02f34ac8 100644 --- a/diffsims/generators/diffraction_generator.py +++ b/diffsims/generators/diffraction_generator.py @@ -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. @@ -68,11 +68,7 @@ def __init__(self, self.max_excitation_error = max_excitation_error self.debye_waller_factors = debye_waller_factors or {} - scattering_params_dict = { - 'lobato': 'lobato', - 'xtables': 'xtables' - } - if scattering_params in scattering_params_dict: + if scattering_params in ['lobato','xtables']: self.scattering_params = scattering_params_dict[scattering_params] else: raise NotImplementedError("The scattering parameters `{}` is not implemented. " From 1645c45996819185923eb0399efc77d267ecbb2d Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:22:54 +0100 Subject: [PATCH 2/9] bump version to 0.2.0-rc.1 --- diffsims/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffsims/version.py b/diffsims/version.py index 70a5fdda..164e3ac8 100644 --- a/diffsims/version.py +++ b/diffsims/version.py @@ -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" From e75f7a405be547a64c98c0248a8591b810dc7263 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:23:43 +0100 Subject: [PATCH 3/9] scattering_params ---> kirkland-scattering_params --- .../utils/{scattering_params.py => kirkland-scattering_params.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename diffsims/utils/{scattering_params.py => kirkland-scattering_params.py} (100%) diff --git a/diffsims/utils/scattering_params.py b/diffsims/utils/kirkland-scattering_params.py similarity index 100% rename from diffsims/utils/scattering_params.py rename to diffsims/utils/kirkland-scattering_params.py From 794e8c0e1d2a14dd1a19c692dc1645cfebf43140 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:24:33 +0100 Subject: [PATCH 4/9] Remove load/save of DiffractionLibrary --- diffsims/libraries/diffraction_library.py | 46 ----------------------- 1 file changed, 46 deletions(-) diff --git a/diffsims/libraries/diffraction_library.py b/diffsims/libraries/diffraction_library.py index 2824bdb3..fae9e8f1 100644 --- a/diffsims/libraries/diffraction_library.py +++ b/diffsims/libraries/diffraction_library.py @@ -16,38 +16,8 @@ # You should have received a copy of the GNU General Public License # along with diffsims. If not, see . -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. @@ -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) From 4888c5332f6ea3c169a31c26a1a023ea923cfef9 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:27:04 +0100 Subject: [PATCH 5/9] Remove save/load from VectorLibrary --- diffsims/libraries/vector_library.py | 45 ---------------------------- 1 file changed, 45 deletions(-) diff --git a/diffsims/libraries/vector_library.py b/diffsims/libraries/vector_library.py index 1e823212..e2acd356 100644 --- a/diffsims/libraries/vector_library.py +++ b/diffsims/libraries/vector_library.py @@ -16,37 +16,8 @@ # You should have received a copy of the GNU General Public License # along with diffsims. If not, see . -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. @@ -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) From 1621a16979e495cb540a7ad781a70fb09a5a80a7 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:33:13 +0100 Subject: [PATCH 6/9] Delete tests for removed functionality --- .../test_library/test_diffraction_library.py | 23 ------------------- .../tests/test_library/test_vector_library.py | 17 -------------- 2 files changed, 40 deletions(-) diff --git a/diffsims/tests/test_library/test_diffraction_library.py b/diffsims/tests/test_library/test_diffraction_library.py index d6dcebf4..3fd932a0 100644 --- a/diffsims/tests/test_library/test_diffraction_library.py +++ b/diffsims/tests/test_library/test_diffraction_library.py @@ -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 @@ -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 @@ -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') diff --git a/diffsims/tests/test_library/test_vector_library.py b/diffsims/tests/test_library/test_vector_library.py index e78ed899..e244cb48 100644 --- a/diffsims/tests/test_library/test_vector_library.py +++ b/diffsims/tests/test_library/test_vector_library.py @@ -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 @@ -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') From 950f3c502a6883540a8ca663386616c53a1195e3 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:35:18 +0100 Subject: [PATCH 7/9] Making the refactor even smaller --- diffsims/generators/diffraction_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffsims/generators/diffraction_generator.py b/diffsims/generators/diffraction_generator.py index 02f34ac8..a26e3f47 100644 --- a/diffsims/generators/diffraction_generator.py +++ b/diffsims/generators/diffraction_generator.py @@ -69,7 +69,7 @@ def __init__(self, self.debye_waller_factors = debye_waller_factors or {} if scattering_params in ['lobato','xtables']: - self.scattering_params = scattering_params_dict[scattering_params] + self.scattering_params = scattering_params else: raise NotImplementedError("The scattering parameters `{}` is not implemented. " "See documentation for available " From 307acc05b4c977525763c5e112ada7b42fad2f74 Mon Sep 17 00:00:00 2001 From: pc494 Date: Thu, 17 Oct 2019 19:37:57 +0100 Subject: [PATCH 8/9] Correct refactor for scattering params in diffgen --- diffsims/generators/diffraction_generator.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/diffsims/generators/diffraction_generator.py b/diffsims/generators/diffraction_generator.py index a26e3f47..5125e755 100644 --- a/diffsims/generators/diffraction_generator.py +++ b/diffsims/generators/diffraction_generator.py @@ -67,13 +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 - if scattering_params in ['lobato','xtables']: - self.scattering_params = 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, @@ -105,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. @@ -175,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) From dc4c28aac4d6875dcacc971970f075e7e158fabc Mon Sep 17 00:00:00 2001 From: Phillip Crout <32455330+pc494@users.noreply.github.com> Date: Fri, 18 Oct 2019 12:00:04 +0100 Subject: [PATCH 9/9] [skip ci] include dev in the versioning --- diffsims/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffsims/version.py b/diffsims/version.py index 164e3ac8..81ac0c62 100644 --- a/diffsims/version.py +++ b/diffsims/version.py @@ -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.2.0-rc.1" +__version__ = "0.2.0-rc.1dev" __maintainer__ = "Duncan Johnstone" __email__ = "dnj23@cam.ac.uk" __status__ = "Development"