Skip to content

Commit

Permalink
Merge 4e7058a into f312256
Browse files Browse the repository at this point in the history
  • Loading branch information
pc494 committed Feb 28, 2018
2 parents f312256 + 4e7058a commit de30ed9
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 61 deletions.
1 change: 1 addition & 0 deletions pyxem/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def correlate(image, pattern_dictionary):
pixel_coordinates = pattern_dictionary['pixel_coords']
pattern_normalization = pattern_dictionary['pattern_norm']

# The x,y choice here is correct. Basically the numpy/hyperspy conversion is a danger
image_intensities = image[pixel_coordinates[:, 1], pixel_coordinates[:, 0]]

return np.dot(image_intensities,pattern_intensities)/pattern_normalization
191 changes: 130 additions & 61 deletions tests/Orientation_mapping/test_orientation_mapping_phys.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,90 +20,159 @@
import pytest
import pyxem as pxm
import hyperspy.api as hs
import pymatgen as pmg

from pyxem.signals.diffraction_simulation import DiffractionSimulation
from pyxem.generators.indexation_generator import IndexationGenerator
from OM_fixtures import create_GaAs, build_linear_grid_in_euler,half_side_length,create_sample
from pyxem.utils.sim_utils import peaks_from_best_template
from pyxem.utils.plot import generate_marker_inputs_from_peaks
from transforms3d.euler import euler2axangle
from pymatgen.transformations.standard_transformations import RotationTransformation

half_side_length = 72

def create_GaAs():
Ga = pmg.Element("Ga")
As = pmg.Element("As")
lattice = pmg.Lattice.cubic(5.6535)
return pmg.Structure.from_spacegroup("F23",lattice, [Ga,As], [[0, 0, 0],[0.25,0.25,0.25]])

def create_pair(angle_start,angle_change):
""" Lists for angles """
angle_2 = np.add(angle_start,angle_change)
return [angle_start,angle_start,angle_2,angle_2]

def build_linear_grid_in_euler(alpha_max,beta_max,gamma_max,resolution):
a = np.arange(0,alpha_max,step=resolution)
b = np.arange(0,beta_max,step=resolution)
c = np.arange(0,gamma_max,step=resolution)
from itertools import product
return list(product(a,b,c))

def create_sample(edc,structure,angle_start,angle_change):
dps = []
for orientation in create_pair(angle_start,angle_change):
axis, angle = euler2axangle(orientation[0], orientation[1],orientation[2], 'rzxz')
rotation = RotationTransformation(axis, angle,angle_in_radians=True)
rotated_structure = rotation.apply_transformation(structure)
data = edc.calculate_ed_data(rotated_structure,
reciprocal_radius=0.9, #avoiding a reflection issue
with_direct_beam=False)
dps.append(data.as_signal(2*half_side_length,0.025,1).data)
dp = pxm.ElectronDiffraction([dps[0:2],dps[2:]])
dp.set_calibration(1/half_side_length)
return dp

def build_structure_lib(structure,rot_list):
struc_lib = dict()
struc_lib["A"] = (structure,rot_list)
return struc_lib

edc = pxm.DiffractionGenerator(300, 5e-2)
diff_gen = pxm.DiffractionLibraryGenerator(edc)
rot_list = build_linear_grid_in_euler(12,10,5,1)
structure = create_GaAs()
rot_list = build_linear_grid_in_euler(12,10,5,1)
struc_lib = build_structure_lib(structure,rot_list)
library = diff_gen.get_diffraction_library(struc_lib,
edc = pxm.DiffractionGenerator(300, 5e-2)

def get_template_library(structure,rot_list,edc):
diff_gen = pxm.DiffractionLibraryGenerator(edc)
struc_lib = build_structure_lib(structure,rot_list)
library = diff_gen.get_diffraction_library(struc_lib,
calibration=1/half_side_length,
reciprocal_radius=0.6,
half_shape=(half_side_length,half_side_length),
representation='euler',
with_direct_beam=False)
return library

def get_match_results_case_a(structure,rot_list,edc): #get params
"""
Case A -
We rotate about 4 on the first, z axis, as we don't rotate around x at all we can
then rotate again around the second z axis in a similar way
"""
dp = create_sample(edc,structure,[0,0,0],[4,0,0])
library = get_template_library(structure,rot_list,edc)
indexer = IndexationGenerator(dp,library)
match_results = indexer.correlate()
return match_results

def get_match_results_case_b(structure,rot_list,edc):
"""
Case B -
Now an arbitary rotation
"""
dp = create_sample(edc,structure,[0,0,0],[3,7,1])
library = get_template_library(structure,rot_list,edc)
indexer = IndexationGenerator(dp,library)
match_results = indexer.correlate()
return match_results


def get_match_results_case_c(structure,rot_list,edc):
"""
Case C -
Use non-ints for the rotation in B
"""
dp = create_sample(edc,structure,[0,0,0],[3,7.01,0.99])
library = get_template_library(structure,rot_list,edc)
indexer = IndexationGenerator(dp,library)
match_results = indexer.correlate()
return match_results

### Test two rotation direction on axis and an arbitary rotation direction

"""
Case A -
We rotate about 4 on the first, z axis, as we don't rotate around x at all we can
then rotate again around the second z axis in a similar way
"""
#This runs the test twice, but only the test directly below -
dp = create_sample(edc,structure,[0,0,0],[4,0,0])
indexer = IndexationGenerator(dp,library)
match_results_A = indexer.correlate()

def test_match_results_essential():
assert np.all(match_results.inav[0,0] == match_results.inav[1,0])
assert np.all(match_results.inav[0,1] == match_results.inav[1,1])

def test_peak_from_best_template():
# Will fail if top line of test_match_results failed
peaks = match_results.map(peaks_from_best_template,phase=["A"],library=library,inplace=False)
assert peaks.inav[0,0] == library["A"][(0,0,0)]['Sim'].coordinates[:,:2]

def test_match_results_caseA():
assert np.all(match_results_A.inav[0,0].data[0,1:4] == np.array([0,0,0]))
assert match_results_A.inav[1,1].data[0,2] == 0 #no rotation in z for the twinning
#rotation totals must equal 4, and each must give the same coefficient
assert np.all(np.sum(match_results_A.inav[1,1].data[:,1:4],axis=1) == 4)
assert np.all(match_results_A.inav[1,1].data[:,4] == match_results_A.inav[1,1].data[0,4])

#test_match_results_essential()
#test_peak_from_best_template()
test_match_results_caseA()

rot_list_1 = build_linear_grid_in_euler(12,10,5,1)
rot_list_2 = build_linear_grid_in_euler(12,10,7,5)
@pytest.mark.parametrize("structure",[create_GaAs()])
@pytest.mark.parametrize("rot_list",[rot_list_1,rot_list_2])
@pytest.mark.parametrize("edc",[edc])
"""
Case B -
We rotate all 3 and test that we get good answers
"""

dp = create_sample(edc,structure,[0,0,0],[3,7,1])
indexer = IndexationGenerator(dp,library)
match_results = indexer.correlate()

test_match_results_essential()

def test_match_results_caseB():
assert np.all(match_results.inav[1,1].data[0,1:4] == np.array([3,7,1]))

test_match_results_caseB()

""" Case C - Use non-integers """

dp = create_sample(edc,structure,[0,0,0],[3,7.01,0.99])
indexer = IndexationGenerator(dp,library)
match_results = indexer.correlate()
@pytest.mark.parametrize("structure",[create_GaAs()])
@pytest.mark.parametrize("rot_list",[rot_list])
@pytest.mark.parametrize("edc",[edc])

def test_match_results_essential(structure,rot_list,edc):
M = get_match_results_case_a(structure,rot_list,edc) #for concision
assert np.all(M.inav[0,0] == M.inav[1,0])
assert np.all(M.inav[0,1] == M.inav[1,1])

# also test peaks from best template
library = get_template_library(structure,rot_list,edc)
peaks = M.map(peaks_from_best_template,phase=["A"],library=library,inplace=False)
assert peaks.inav[0,0] == library["A"][(0,0,0)]['Sim'].coordinates[:,:2]

test_match_results_essential()
@pytest.mark.parametrize("structure",[create_GaAs()])
@pytest.mark.parametrize("rot_list",[rot_list])
@pytest.mark.parametrize("edc",[edc])

def test_match_results_caseC():
assert np.all(match_results.inav[1,1].data[0,1:4] == np.array([3,7,1]))
def test_match_results_caseA(structure,rot_list,edc):
M = get_match_results_case_a(structure,rot_list,edc)
assert np.all(M.inav[0,0].data[0,1:4] == np.array([0,0,0]))
assert M.inav[1,1].data[0,2] == 0 #no rotation in z

test_match_results_caseC()

#for the twinning the rotation totals must equal 4
assert np.all(np.sum(M.inav[1,1].data[:,1:4],axis=1) == 4)
#and each must give the same coefficient
assert np.all(M.inav[1,1].data[:,4] == M.inav[1,1].data[0,4])

@pytest.mark.parametrize("structure",[create_GaAs()])
@pytest.mark.parametrize("rot_list",[rot_list])
@pytest.mark.parametrize("edc",[edc])

def test_match_results_caseB(structure,rot_list,edc):
M = get_match_results_case_b(structure,rot_list,edc)
assert np.all(M.inav[1,1].data[0,1:4] == np.array([3,7,1]))

@pytest.mark.parametrize("structure",[create_GaAs()])
@pytest.mark.parametrize("rot_list",[rot_list])
@pytest.mark.parametrize("edc",[edc])

def test_match_results_caseC(structure,rot_list,edc):
M = get_match_results_case_c(structure,rot_list,edc)
assert np.all(M.inav[1,1].data[0,1:4] == np.array([3,7,1]))

"""
# Visualization Code
peaks = match_results.map(peaks_from_best_template,phase=["A"],library=library,inplace=False)
mmx,mmy = generate_marker_inputs_from_peaks(peaks)
Expand All @@ -112,4 +181,4 @@ def test_match_results_caseC():
## THERE IS A GOTCHA HERE DUE TO WEAK REFLECTION
m = hs.markers.point(x=mx,y=my,color='red',marker='x') #see visual test
dp.add_marker(m,plot_marker=True,permanent=True)

"""
1 change: 1 addition & 0 deletions tests/test_diffraction_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def check_pattern_equivilance(p1,p2,coords_only=False):
fake_pattern = get_pattern(Mscope,fake_cubic_I)
larger_pattern = get_pattern(Mscope,larger_cubic_I)


def test_casual_formal():
# Checks that Pymatgen understands that these are the same structure
assert formal_cubic_I == casual_cubic_I
Expand Down

0 comments on commit de30ed9

Please sign in to comment.