Skip to content

Commit

Permalink
black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pc494 committed Sep 2, 2020
1 parent 070f167 commit 445391f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 43 deletions.
47 changes: 30 additions & 17 deletions diffsims/generators/diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ def __init__(
self,
accelerating_voltage,
debye_waller_factors={},
scattering_params="lobato", *args
scattering_params="lobato",
*args
):
if args:
print("This class changed in v0.3 and no longer takes a maximum_excitation_error")
print(
"This class changed in v0.3 and no longer takes a maximum_excitation_error"
)
self.wavelength = get_electron_wavelength(accelerating_voltage)
self.debye_waller_factors = debye_waller_factors

Expand All @@ -86,8 +89,15 @@ def __init__(
)

def calculate_ed_data(
self, structure, reciprocal_radius, rotation=(0, 0, 0), excitation_function="linear", max_excitation_error=1e-2, with_direct_beam=True,
**kwargs):
self,
structure,
reciprocal_radius,
rotation=(0, 0, 0),
excitation_function="linear",
max_excitation_error=1e-2,
with_direct_beam=True,
**kwargs
):
"""Calculates the Electron Diffraction data for a structure.
Parameters
Expand Down Expand Up @@ -156,17 +166,20 @@ def calculate_ed_data(
elif excitation_function == "binary":
shape_factor = 1
else:
shape_factor = excitation_function(excitation_error,max_excitation_error,**kwargs)
shape_factor = excitation_function(
excitation_error, max_excitation_error, **kwargs
)

# Calculate diffracted intensities based on a kinematical model.
intensities = get_kinematical_intensities(
structure,
g_indices,
g_hkls,
prefactor=shape_factor,
scattering_params=self.scattering_params ,
scattering_params=self.scattering_params,
debye_waller_factors=self.debye_waller_factors,
**kwargs)
**kwargs
)

# Threshold peaks included in simulation based on minimum intensity.
peak_mask = intensities > 1e-20
Expand Down Expand Up @@ -230,13 +243,18 @@ def calculate_profile_data(
g_indices,
np.asarray(g_hkls),
prefactor=multiplicities,
scattering_params=self.scattering_params ,
scattering_params=self.scattering_params,
debye_waller_factors=self.debye_waller_factors,
)

if is_lattice_hexagonal(latt):
# Use Miller-Bravais indices for hexagonal lattices.
g_indices = (g_indices[0], g_indices[1], -g_indices[0] - g_indices[1], g_indices[2])
g_indices = (
g_indices[0],
g_indices[1],
-g_indices[0] - g_indices[1],
g_indices[2],
)

hkls_labels = ["".join([str(int(x)) for x in xs]) for xs in g_indices]

Expand Down Expand Up @@ -278,12 +296,7 @@ class AtomicDiffractionGenerator:
"""

def __init__(
self,
accelerating_voltage,
detector,
reciprocal_mesh=False
):
def __init__(self, accelerating_voltage, detector, reciprocal_mesh=False):
self.wavelength = get_electron_wavelength(accelerating_voltage)
# Always store a 'real' mesh
self.detector = detector if not reciprocal_mesh else from_recip(detector)
Expand Down Expand Up @@ -352,14 +365,14 @@ def calculate_ed_data(

species = structure.element
coordinates = structure.xyz_cartn.reshape(species.size, -1)
dim = coordinates.shape[1] #guarenteed to be 3
dim = coordinates.shape[1] # guarenteed to be 3

if not ZERO > 0:
raise ValueError("The value of the ZERO argument must be greater than 0")

if probe_centre is None:
probe_centre = np.zeros(dim)
elif len(probe_centre) == (dim-1):
elif len(probe_centre) == (dim - 1):
probe_centre = np.array(list(probe_centre) + [0])

coordinates = coordinates - probe_centre[None]
Expand Down
1 change: 0 additions & 1 deletion diffsims/sims/diffraction_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ def __init__(self, magnitudes, intensities, hkls):
self.intensities = intensities
self.hkls = hkls


def get_plot(self, g_max, annotate_peaks=True, with_labels=True, fontsize=12):

"""Plots the diffraction profile simulation for the
Expand Down
20 changes: 10 additions & 10 deletions diffsims/tests/test_generators/test_diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import diffpy.structure


@pytest.fixture(
params=[(300)]
)
@pytest.fixture(params=[(300)])
def diffraction_calculator(request):
return DiffractionGenerator(request.param)

Expand Down Expand Up @@ -167,20 +165,22 @@ def test_mode(self, diffraction_calculator_atomic, local_structure):
local_structure, probe, 1, mode="other"
)

@pytest.mark.xfail(raises=ValueError,strict=True)
def test_bad_ZERO(self,diffraction_calculator_atomic,local_structure):
_ = diffraction_calculator_atomic.calculate_ed_data(local_structure,probe,1,ZERO=-1)

@pytest.mark.xfail(raises=ValueError, strict=True)
def test_bad_ZERO(self, diffraction_calculator_atomic, local_structure):
_ = diffraction_calculator_atomic.calculate_ed_data(
local_structure, probe, 1, ZERO=-1
)


@pytest.mark.parametrize("scattering_param",["lobato", "xtables"])
@pytest.mark.parametrize("scattering_param", ["lobato", "xtables"])
def test_param_check(scattering_param):
generator = DiffractionGenerator(300,scattering_params=scattering_param)
generator = DiffractionGenerator(300, scattering_params=scattering_param)


@pytest.mark.xfail(raises=NotImplementedError)
def test_invalid_scattering_params():
scattering_param = "_empty"
generator = DiffractionGenerator(300,scattering_params=scattering_param)
generator = DiffractionGenerator(300, scattering_params=scattering_param)


@pytest.mark.parametrize("shape", [(10, 20), (20, 10)])
Expand Down
1 change: 1 addition & 0 deletions diffsims/tests/test_sims/test_diffraction_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def profile_simulation():
],
)


def test_plot_profile_simulation(profile_simulation):
profile_simulation.get_plot(g_max=1)

Expand Down
32 changes: 18 additions & 14 deletions diffsims/tests/test_utils/test_sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
diffraction_scattering_angle,
get_intensities_params,
)
from diffsims.tests.test_generators.test_diffraction_generator import (
make_structure
)
from diffsims.tests.test_generators.test_diffraction_generator import make_structure


@pytest.mark.parametrize(
Expand Down Expand Up @@ -308,27 +306,33 @@ def test_array_like(self):
sa_known = np.array([9.84e-3, 6.56e-3])
assert np.allclose(sa_known, scattering_angle, rtol=0.001)


def test_get_intensities_params(default_structure):
latt = default_structure.lattice
reciprocal_lattice = latt.reciprocal()
reciprocal_radius = 0.2
unique_hkls, multiplicites, g_hkls = get_intensities_params(reciprocal_lattice, reciprocal_radius)
np.testing.assert_equal(multiplicites, ([1.]))
unique_hkls, multiplicites, g_hkls = get_intensities_params(
reciprocal_lattice, reciprocal_radius
)
np.testing.assert_equal(multiplicites, ([1.0]))
np.testing.assert_equal(g_hkls, [0.0])
np.testing.assert_array_equal(unique_hkls, [[-0., -0., 0.]])
np.testing.assert_array_equal(unique_hkls, [[-0.0, -0.0, 0.0]])


def test_get_kinematical_intensities(default_structure):
latt = default_structure.lattice
reciprocal_lattice = latt.reciprocal()
reciprocal_radius = 0.2
unique_hkls, multiplicites, g_hkls = get_intensities_params(reciprocal_lattice, reciprocal_radius)
unique_hkls, multiplicites, g_hkls = get_intensities_params(
reciprocal_lattice, reciprocal_radius
)
g_hkls_array = np.asarray(g_hkls)
i_hkls = get_kinematical_intensities(
default_structure,
g_indices=unique_hkls,
g_hkls_array=g_hkls_array,
debye_waller_factors={"Al":1},
prefactor=multiplicites,
scattering_params="lobato"
)
default_structure,
g_indices=unique_hkls,
g_hkls_array=g_hkls_array,
debye_waller_factors={"Al": 1},
prefactor=multiplicites,
scattering_params="lobato",
)
np.testing.assert_array_almost_equal(i_hkls, ([43.0979]), decimal=4)
4 changes: 3 additions & 1 deletion diffsims/utils/sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_kinematical_intensities(
g_hkls_array,
debye_waller_factors={},
scattering_params="lobato",
prefactor=1
prefactor=1,
):

"""Calculates peak intensities.
Expand Down Expand Up @@ -293,6 +293,7 @@ def get_kinematical_intensities(
peak_intensities = prefactor * (f_hkls * f_hkls.conjugate()).real
return peak_intensities


def simulate_kinematic_scattering(
atomic_coordinates,
element,
Expand Down Expand Up @@ -495,6 +496,7 @@ def get_intensities_params(reciprocal_lattice, reciprocal_radius):

return unique_hkls, multiplicites, g_hkls


def get_holz_angle(electron_wavelength, lattice_parameter):
""" Converts electron wavelength and lattice paramater to holz angle
Parameters
Expand Down

0 comments on commit 445391f

Please sign in to comment.