Skip to content

Commit

Permalink
Merge 9517760 into be3d7e1
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjohnstone committed Jul 15, 2019
2 parents be3d7e1 + 9517760 commit 5ec1000
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 363 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: python

env:
global:
- CONDA_DEPS="scipy matplotlib numpy tqdm transforms3d diffpy.structure"
- DEPS="scipy matplotlib numpy tqdm transforms3d diffpy.structure"
- TEST_DEPS="pytest pytest-cov coveralls"

matrix:
Expand Down Expand Up @@ -34,7 +34,7 @@ before_install:
- df -h

install:
- conda install -y $CONDA_DEPS $TEST_DEPS;
- conda install -y $DEPS $TEST_DEPS;
- pip install .

script:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
environment:

global:
DEPS: "transforms3d diffpy.structure"
DEPS: "scipy matplotlib numpy tqdm transforms3d diffpy.structure"
TEST_DEPS: "pytest"
MPLBACKEND: "agg"

Expand Down
21 changes: 0 additions & 21 deletions diffsims/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,3 @@ def default_structure():
atom = diffpy.structure.atom.Atom(atype='Ni',xyz=[0,0,0],lattice=latt)
hexagonal_structure = diffpy.structure.Structure(atoms=[atom],lattice=latt)
return hexagonal_structure

@pytest.fixture
def vector_library():
library = DiffractionVectorLibrary()
library['A'] = {
'indices': np.array([
[[0, 2, 0], [1, 0, 0]],
[[1, 2, 3], [0, 2, 0]],
[[1, 2, 3], [1, 0, 0]],
]),
'measurements': np.array([
[2, 1, np.pi / 2],
[np.sqrt(14), 2, 1.006853685],
[np.sqrt(14), 1, 1.300246564],
])
}
lattice = diffpy.structure.Lattice(1, 1, 1, 90, 90, 90)
library.structures = [
diffpy.structure.Structure(lattice=lattice)
]
return library
18 changes: 17 additions & 1 deletion diffsims/tests/test_generators/test_diffraction_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import numpy as np
import pytest

from diffsims.sims.diffraction_simulation import DiffractionSimulation
from diffsims.sims.diffraction_simulation import ProfileSimulation
from diffsims.generators.diffraction_generator import DiffractionGenerator
import diffpy.structure

Expand Down Expand Up @@ -96,6 +99,19 @@ def test_appropriate_intensities(self, diffraction_calculator, local_structure):
smaller = np.greater_equal(diffraction.intensities[central_beam], diffraction.intensities)
assert np.all(smaller)

def test_calculate_profile_class(self, local_structure, diffraction_calculator):
# tests the non-hexagonal (cubic) case
profile = diffraction_calculator.calculate_profile_data(local_structure,
reciprocal_radius=1.)
assert isinstance(profile, ProfileSimulation)

latt = diffpy.structure.lattice.Lattice(3, 3, 5, 90, 90, 120)
atom = diffpy.structure.atom.Atom(atype='Ni', xyz=[0, 0, 0], lattice=latt)
hexagonal_structure = diffpy.structure.Structure(atoms=[atom], lattice=latt)
hexagonal_profile = diffraction_calculator.calculate_profile_data(structure=hexagonal_structure,
reciprocal_radius=1.)
assert isinstance(hexagonal_profile, ProfileSimulation)


scattering_params = ['lobato', 'xtables']

Expand All @@ -110,4 +126,4 @@ def test_param_check(scattering_param):
def test_invalid_scattering_params():
scattering_param = '_empty'
generator = DiffractionGenerator(300, 0.2, None,
scattering_params=scattering_param)
scattering_params=scattering_param)
6 changes: 0 additions & 6 deletions diffsims/tests/test_sims/test_diffraction_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
from diffsims.generators.diffraction_generator import DiffractionGenerator


@pytest.fixture
def coords_intensity_simulation():
return DiffractionSimulation(coordinates=np.asarray([[0.3, 1.2, 0]]),
intensities=np.ones(1))


@pytest.mark.xfail(raises=ValueError)
def test_wrong_calibration_setting():
DiffractionSimulation(coordinates=np.asarray([[0.3, 1.2, 0]]),
Expand Down
3 changes: 1 addition & 2 deletions diffsims/tests/test_utils/test_sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from diffsims.utils.sim_utils import get_electron_wavelength, \
get_interaction_constant, get_unique_families, get_kinematical_intensities,\
get_vectorized_list_for_atomic_scattering_factors, get_points_in_sphere, \
simulate_kinematic_scattering, peaks_from_best_template, \
is_lattice_hexagonal, transfer_navigation_axes, uvtw_to_uvw, \
simulate_kinematic_scattering, is_lattice_hexagonal, uvtw_to_uvw, \
rotation_list_stereographic


Expand Down
45 changes: 45 additions & 0 deletions diffsims/tests/test_utils/test_vector_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2019 The pyXem developers
#
# This file is part of pyXem.
#
# pyXem 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.
#
# pyXem 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 pyXem. If not, see <http://www.gnu.org/licenses/>.

import numpy as np
import pytest

from diffsims.utils.vector_utils import get_angle_cartesian
from diffsims.utils.vector_utils import get_angle_cartesian_vec


@pytest.mark.parametrize('vec_a, vec_b, expected_angle', [
([0, 0, 1], [0, 1, 0], np.deg2rad(90)),
([0, 0, 0], [0, 0, 1], 0)
])
def test_get_angle_cartesian(vec_a, vec_b, expected_angle):
angle = get_angle_cartesian(vec_a, vec_b)
np.testing.assert_allclose(angle, expected_angle)


@pytest.mark.parametrize('a, b, expected_angles', [
(np.array([[0, 0, 1], [0, 0, 0]]), np.array([[0, 1, 0], [0, 0, 1]]), [np.deg2rad(90), 0])
])
def test_get_angle_cartesian_vec(a, b, expected_angles):
angles = get_angle_cartesian_vec(a, b)
np.testing.assert_allclose(angles, expected_angles)


@pytest.mark.xfail(raises=ValueError)
def test_get_angle_cartesian_vec_input_validation():
get_angle_cartesian_vec(np.empty((2, 3)), np.empty((5, 3)))
133 changes: 0 additions & 133 deletions diffsims/utils/sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,77 +377,6 @@ def simulate_rotated_structure(diffraction_generator, structure, rotation_matrix
with_direct_beam)


def peaks_from_best_template(single_match_result, library):
""" Takes a TemplateMatchingResults object and return the associated peaks,
to be used in combination with map().
Parameters
----------
single_match_result : ndarray
An entry in a TemplateMatchingResults.
library : DiffractionLibrary
Diffraction library containing the phases and rotations.
Returns
-------
peaks : array
Coordinates of peaks in the matching results object in calibrated units.
"""
best_fit = single_match_result[np.argmax(single_match_result[:, 2])]
phase_names = list(library.keys())
best_index = int(best_fit[0])
phase = phase_names[best_index]
try:
simulation = library.get_library_entry(
phase=phase,
angle=tuple(best_fit[1]))['Sim']
except ValueError:
structure = library.structures[best_index]
rotation_matrix = euler2mat(*np.deg2rad(best_fit[1]), 'rzxz')
simulation = simulate_rotated_structure(
library.diffraction_generator,
structure,
rotation_matrix,
library.reciprocal_radius,
library.with_direct_beam)

peaks = simulation.coordinates[:, :2] # cut z
return peaks


def peaks_from_best_vector_match(single_match_result, library):
""" Takes a VectorMatchingResults object and return the associated peaks,
to be used in combination with map().
Parameters
----------
single_match_result : ndarray
An entry in a VectorMatchingResults
library : DiffractionLibrary
Diffraction library containing the phases and rotations
Returns
-------
peaks : ndarray
Coordinates of peaks in the matching results object in calibrated units.
"""
best_fit = single_match_result[np.argmax(single_match_result[:, 2])]
best_index = best_fit[0]

rotation_matrix = best_fit[1]
# Don't change the original
structure = library.structures[best_index]
sim = simulate_rotated_structure(
library.diffraction_generator,
structure,
rotation_matrix,
library.reciprocal_radius,
with_direct_beam=False)

# Cut z
return sim.coordinates[:, :2]


def get_points_in_sphere(reciprocal_lattice, reciprocal_radius):
"""Finds all reciprocal lattice points inside a given reciprocal sphere.
Utilised within the DiffractionGenerator.
Expand Down Expand Up @@ -489,12 +418,10 @@ def get_points_in_sphere(reciprocal_lattice, reciprocal_radius):

def is_lattice_hexagonal(latt):
"""Determines if a diffpy lattice is hexagonal or trigonal.
Parameters
----------
latt : diffpy.Structure.lattice
The diffpy lattice object to be determined as hexagonal or not.
Returns
-------
is_true : bool
Expand All @@ -508,66 +435,6 @@ def is_lattice_hexagonal(latt):
return len(truth_list) == np.sum(truth_list)


def transfer_navigation_axes(new_signal, old_signal):
""" Transfers navigation axis calibrations from an old signal to a new
signal produced from it by a method or a generator.
Parameters
----------
new_signal : Signal
The product signal with undefined navigation axes.
old_signal : Signal
The parent signal with calibrated navigation axes.
Returns
-------
new_signal : Signal
The new signal with calibrated navigation axes.
"""
new_signal.axes_manager.set_signal_dimension(
len(new_signal.data.shape) - old_signal.axes_manager.navigation_dimension)

for i in range(min(new_signal.axes_manager.navigation_dimension,
old_signal.axes_manager.navigation_dimension)):
ax_new = new_signal.axes_manager.navigation_axes[i]
ax_old = old_signal.axes_manager.navigation_axes[i]
ax_new.name = ax_old.name
ax_new.scale = ax_old.scale
ax_new.units = ax_old.units

return new_signal


def transfer_navigation_axes_to_signal_axes(new_signal, old_signal):
""" Transfers navigation axis calibrations from an old signal to the signal
axes of a new signal produced from it by a method or a generator.
Used from methods that generate a signal with a single value at each
navigation position.
Parameters
----------
new_signal : Signal
The product signal with undefined navigation axes.
old_signal : Signal
The parent signal with calibrated navigation axes.
Returns
-------
new_signal : Signal
The new signal with calibrated signal axes.
"""
for i in range(min(new_signal.axes_manager.signal_dimension,
old_signal.axes_manager.navigation_dimension)):
ax_new = new_signal.axes_manager.signal_axes[i]
ax_old = old_signal.axes_manager.navigation_axes[i]
ax_new.name = ax_old.name
ax_new.scale = ax_old.scale
ax_new.units = ax_old.units

return new_signal


def uvtw_to_uvw(uvtw):
"""Convert 4-index direction to a 3-index direction.
Expand Down

0 comments on commit 5ec1000

Please sign in to comment.