Skip to content

Commit f050d20

Browse files
authored
Merge pull request #742 from serinlee1065/diffraction
From diffraction (fork) to dev for clustering module
2 parents 990d563 + e030260 commit f050d20

19 files changed

Lines changed: 569 additions & 521 deletions

File tree

py4DSTEM/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from py4DSTEM import io
2626
from py4DSTEM.io import import_file, read, save
2727

28-
2928
### basic data classes
3029

3130
# data
@@ -40,7 +39,6 @@
4039
# datacube
4140
from py4DSTEM.datacube import DataCube, VirtualImage, VirtualDiffraction
4241

43-
4442
### visualization
4543

4644
from py4DSTEM import visualize
@@ -64,15 +62,12 @@
6462
# diffraction
6563
from py4DSTEM.process.diffraction import Crystal, Orientation
6664

67-
6865
# ptycho
6966
from py4DSTEM.process import phase
7067

71-
7268
# polar
7369
from py4DSTEM.process.polar import PolarDatacube
7470

75-
7671
# strain
7772
from py4DSTEM.process.strain.strain import StrainMap
7873

@@ -89,7 +84,6 @@
8984
from py4DSTEM import preprocess
9085
from py4DSTEM import process
9186

92-
9387
### utilities
9488

9589
# config

py4DSTEM/braggvectors/diskdetection_parallel_new.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from emdfile import PointListArray, PointList
2222
from py4DSTEM.braggvectors.diskdetection import _find_Bragg_disks_single_DP_FK
2323

24-
2524
#### SERIALISERS ####
2625
# Define Serialiser
2726
# these are functions which allow the hdf5 objects to be passed. May not be required anymore

py4DSTEM/data/propagating_calibration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import warnings
55

6-
76
# This is the abstract pattern:
87

98

py4DSTEM/datacube/virtualimage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from py4DSTEM.preprocess import get_shifted_ar
1414
from py4DSTEM.visualize import show
1515

16-
1716
# Virtual image container class
1817

1918

py4DSTEM/io/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
from py4DSTEM.io.read import read
44
from py4DSTEM.io.save import save
55

6-
76
# google downloader
87
from py4DSTEM.io.google_drive_downloader import gdrive_download, get_sample_file_ids

py4DSTEM/io/google_drive_downloader.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import warnings
44

5-
65
### File IDs
76

87
# single files

py4DSTEM/io/legacy/legacy13/v13_emd_classes/io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from numbers import Number
77
from emdfile import tqdmnd
88

9-
109
# Define the EMD group types
1110

1211
EMD_group_types = {

py4DSTEM/io/legacy/legacy13/v13_py4dstem_classes/io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
_read_metadata,
1919
)
2020

21-
2221
# Calibration
2322

2423

py4DSTEM/io/legacy/legacy13/v13_to_14.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import numpy as np
44
from emdfile import tqdmnd
55

6-
76
# v13 imports
87

98
from py4DSTEM.io.legacy.legacy13.v13_emd_classes import (
@@ -25,7 +24,6 @@
2524
BraggVectors as BraggVectors13,
2625
)
2726

28-
2927
# v14 imports
3028

3129
from emdfile import Root, Metadata, Array, PointList, PointListArray

py4DSTEM/process/diffraction/crystal_ACOM.py

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ def orientation_plan(
335335
)
336336
self.orientation_zone_axis_steps = (
337337
np.round(step / self.orientation_refine_ratio) * self.orientation_refine_ratio
338-
).astype(np.integer)
338+
).astype(np.int32)
339+
# ).astype(np.integer)
339340

340341
if self.orientation_fiber and self.orientation_fiber_angles[0] == 0:
341342
self.orientation_num_zones = int(1)
@@ -370,7 +371,8 @@ def orientation_plan(
370371
(self.orientation_zone_axis_steps + 1)
371372
* (self.orientation_zone_axis_steps + 2)
372373
/ 2
373-
).astype(np.integer)
374+
).astype(np.int32)
375+
# ).astype(np.integer)
374376
self.orientation_vecs = np.zeros((self.orientation_num_zones, 3))
375377
self.orientation_vecs[0, :] = self.orientation_zone_axis_range[0, :]
376378
self.orientation_inds = np.zeros((self.orientation_num_zones, 3), dtype="int")
@@ -379,7 +381,8 @@ def orientation_plan(
379381
# or circular arc SLERP for fiber texture
380382
for a0 in np.arange(1, self.orientation_zone_axis_steps + 1):
381383
inds = np.arange(a0 * (a0 + 1) / 2, a0 * (a0 + 1) / 2 + a0 + 1).astype(
382-
np.integer
384+
np.int32
385+
# np.integer
383386
)
384387

385388
p0 = pv[a0, :]
@@ -617,7 +620,8 @@ def orientation_plan(
617620

618621
# Solve for number of angular steps along in-plane rotation direction
619622
self.orientation_in_plane_steps = np.round(360 / angle_step_in_plane).astype(
620-
np.integer
623+
np.int32
624+
# np.integer
621625
)
622626

623627
# Calculate -z angles (Euler angle 3)
@@ -2208,8 +2212,6 @@ def calculate_strain(
22082212
deformation tensor which transforms the simulated diffraction pattern
22092213
into the experimental pattern, for all probe positons.
22102214
2211-
TODO: add robust fitting?
2212-
22132215
Parameters
22142216
----------
22152217
bragg_peaks_array (PointListArray):
@@ -2330,71 +2332,75 @@ def calculate_strain(
23302332
inds_match[a0] = ind_min
23312333
keep[a0] = True
23322334

2333-
# Get all paired peaks
2334-
qxy = np.vstack((p.data["qx"][keep], p.data["qy"][keep])).T
2335-
qxy_ref = np.vstack(
2336-
(p_ref.data["qx"][inds_match[keep]], p_ref.data["qy"][inds_match[keep]])
2337-
).T
2335+
if np.sum(keep) >= min_num_peaks:
2336+
# Get all paired peaks
2337+
qxy = np.vstack((p.data["qx"][keep], p.data["qy"][keep])).T
2338+
qxy_ref = np.vstack(
2339+
(
2340+
p_ref.data["qx"][inds_match[keep]],
2341+
p_ref.data["qy"][inds_match[keep]],
2342+
)
2343+
).T
23382344

2339-
# Fit transformation matrix
2340-
# Note - not sure about transpose here
2341-
# (though it might not matter if rotation isn't included)
2342-
if intensity_weighting:
2343-
weights = np.sqrt(p.data["intensity"][keep, None]) * 0 + 1
2344-
m = lstsq(
2345-
qxy_ref * weights,
2346-
qxy * weights,
2347-
rcond=None,
2348-
)[0].T
2349-
else:
2350-
m = lstsq(
2351-
qxy_ref,
2352-
qxy,
2353-
rcond=None,
2354-
)[0].T
2355-
2356-
# Robust fitting
2357-
if robust:
2358-
for a0 in range(5):
2359-
# calculate new weights
2360-
qxy_fit = qxy_ref @ m
2361-
diff2 = np.sum((qxy_fit - qxy) ** 2, axis=1)
2362-
2363-
weights = np.exp(
2364-
diff2 / ((-2 * robust_thresh**2) * np.median(diff2))
2365-
)[:, None]
2366-
if intensity_weighting:
2367-
weights *= np.sqrt(p.data["intensity"][keep, None])
2368-
2369-
# calculate new fits
2345+
# Fit transformation matrix
2346+
# Note - not sure about transpose here
2347+
# (though it might not matter if rotation isn't included)
2348+
if intensity_weighting:
2349+
weights = np.sqrt(p.data["intensity"][keep, None]) * 0 + 1
23702350
m = lstsq(
23712351
qxy_ref * weights,
23722352
qxy * weights,
23732353
rcond=None,
23742354
)[0].T
2355+
else:
2356+
m = lstsq(
2357+
qxy_ref,
2358+
qxy,
2359+
rcond=None,
2360+
)[0].T
23752361

2376-
# Set values into the infinitesimal strain matrix
2377-
strain_map.get_slice("e_xx").data[rx, ry] = 1 - m[0, 0]
2378-
strain_map.get_slice("e_yy").data[rx, ry] = 1 - m[1, 1]
2379-
strain_map.get_slice("e_xy").data[rx, ry] = -(m[0, 1] + m[1, 0]) / 2.0
2380-
strain_map.get_slice("theta").data[rx, ry] = (m[0, 1] - m[1, 0]) / 2.0
2381-
2382-
# Add finite rotation from ACOM orientation map.
2383-
# I am not sure about the relative signs here.
2384-
# Also, maybe I need to add in the mirror operator?
2385-
if orientation_map.mirror[rx, ry, 0]:
2386-
strain_map.get_slice("theta").data[rx, ry] += (
2387-
orientation_map.angles[rx, ry, 0, 0]
2388-
+ orientation_map.angles[rx, ry, 0, 2]
2389-
)
2390-
else:
2391-
strain_map.get_slice("theta").data[rx, ry] -= (
2392-
orientation_map.angles[rx, ry, 0, 0]
2393-
+ orientation_map.angles[rx, ry, 0, 2]
2394-
)
2362+
# Robust fitting
2363+
if robust:
2364+
for a0 in range(5):
2365+
# calculate new weights
2366+
qxy_fit = qxy_ref @ m
2367+
diff2 = np.sum((qxy_fit - qxy) ** 2, axis=1)
2368+
2369+
weights = np.exp(
2370+
diff2 / ((-2 * robust_thresh**2) * np.median(diff2))
2371+
)[:, None]
2372+
if intensity_weighting:
2373+
weights *= np.sqrt(p.data["intensity"][keep, None])
2374+
2375+
# calculate new fits
2376+
m = lstsq(
2377+
qxy_ref * weights,
2378+
qxy * weights,
2379+
rcond=None,
2380+
)[0].T
2381+
2382+
# Set values into the infinitesimal strain matrix
2383+
strain_map.get_slice("e_xx").data[rx, ry] = 1 - m[0, 0]
2384+
strain_map.get_slice("e_yy").data[rx, ry] = 1 - m[1, 1]
2385+
strain_map.get_slice("e_xy").data[rx, ry] = -(m[0, 1] + m[1, 0]) / 2.0
2386+
strain_map.get_slice("theta").data[rx, ry] = (m[0, 1] - m[1, 0]) / 2.0
2387+
2388+
# Add finite rotation from ACOM orientation map.
2389+
# I am not sure about the relative signs here.
2390+
# Also, maybe I need to add in the mirror operator?
2391+
if orientation_map.mirror[rx, ry, 0]:
2392+
strain_map.get_slice("theta").data[rx, ry] += (
2393+
orientation_map.angles[rx, ry, 0, 0]
2394+
+ orientation_map.angles[rx, ry, 0, 2]
2395+
)
2396+
else:
2397+
strain_map.get_slice("theta").data[rx, ry] -= (
2398+
orientation_map.angles[rx, ry, 0, 0]
2399+
+ orientation_map.angles[rx, ry, 0, 2]
2400+
)
23952401

2396-
else:
2397-
strain_map.get_slice("mask").data[rx, ry] = 0.0
2402+
else:
2403+
strain_map.get_slice("mask").data[rx, ry] = 0.0
23982404

23992405
if rotation_range is not None:
24002406
strain_map.get_slice("theta").data[:] = np.mod(

0 commit comments

Comments
 (0)