From ab466049a454fd3899c2864a6645bda61abc4334 Mon Sep 17 00:00:00 2001 From: maclariz Date: Thu, 19 Sep 2024 15:22:20 +0100 Subject: [PATCH 1/5] Update digital_dark_field.py Radius filtering separated as function, and radius-azimuth imaging added as separate function --- .../process/diffraction/digital_dark_field.py | 116 ++++++++++++++++-- 1 file changed, 106 insertions(+), 10 deletions(-) diff --git a/py4DSTEM/process/diffraction/digital_dark_field.py b/py4DSTEM/process/diffraction/digital_dark_field.py index 6051eccce..8e9956e7b 100644 --- a/py4DSTEM/process/diffraction/digital_dark_field.py +++ b/py4DSTEM/process/diffraction/digital_dark_field.py @@ -334,7 +334,7 @@ def pointlist_to_array( if True, applies rotational calibration to bragg_peaks rphi: bool if True, generates two extra columns of Qr and Qphi for addressing in polar - coordinates, Qphi is the angle anticlockwise from horizontal to the right + coordinates, Qphi is the angle in degrees anticlockwise from horizontal to the right Returns ---------- @@ -494,24 +494,58 @@ def DDFimage(points_array, aperture_positions, Rshape=None, tol=1): return image -def DDF_radial_image(points_array, radius, Rshape, tol=1): +def radial_filtered_array(points_array_w_rphi, radius, tol=1): """ Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance Parameters ---------- - points_array: numpy array - as produced by pointlist_to_array and defined in docstring for that function, must be the version with r and phi included + points_array_w_rphi: numpy array + as produced by pointlist_to_array with rphi=True and defined in docstring for that function + radius: float + the radius of diffraction spot you wish to filter by in pixels or calibrated units + tol: float + the tolerance in pixels or calibrated units for a point of qr in the points_array to be considered to match to the radius + + Returns + ---------- + radial_filtered_points_array: numpy array + This will be an 2D numpy array of n points x 7 columns: + qx + qy + I + Rx + Ry + qr + qphi + + """ + radial_filtered_points_array = np.delete( + points_array_w_rphi, + np.where(np.abs(points_array_w_rphi[:, 5] - radius) > tol), + axis=0, + ) + return radial_filtered_points_array + + +def DDF_radial_image(points_array_w_rphi, radius, Rshape, tol=1): + """ + Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance + + Parameters + ---------- + points_array_w_rphi: numpy array + as produced by pointlist_to_array with rphi=True and defined in docstring for that function radius: float the radius of diffraction spot you wish to image in pixels or calibrated units Rshape: tuple, list, array a 2 element vector giving the real space dimensions. If not specified, this is determined from the max along points_array tol: float - the tolerance in pixels or calibrated units for a point in the points_array to be considered to match to an aperture position in the aperture_positions array + the tolerance in pixels or calibrated units for a point of qr in the points_array to be considered to match to the radius Returns ---------- - image: numpy array + radialimage: numpy array 2D numpy array with dimensions determined by Rshape """ @@ -522,19 +556,81 @@ def DDF_radial_image(points_array, radius, Rshape, tol=1): np.max(np.max(points_array[:, 4])).astype("int") + 1, ) - points_array_edit = np.delete( - points_array, np.where(np.abs(points_array[:, 5] - radius) > tol), axis=0 + radial_filtered_points_array = radial_filtered_array( + points_array_w_rphi, radius, tol ) + radialimage = np.zeros(shape=Rshape) for i in range(Rshape[0]): for j in range(Rshape[1]): radialimage[i, j] = np.where( np.logical_and( - points_array_edit[:, 3] == i, points_array_edit[:, 4] == j + radial_filtered_points_array[:, 3] == i, + radial_filtered_points_array[:, 4] == j, ), - points_array_edit[:, 2], + radial_filtered_points_array[:, 2], 0, ).sum() return radialimage + + +def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1): + """ + Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance + + Parameters + ---------- + points_array_w_rphi: numpy array + as produced by pointlist_to_array with rphi=True and defined in docstring for that function + radius: float + the radius of diffraction spot you wish to image in pixels or calibrated units + phi0: float + Angle in degrees anticlockwise from horizontal-right for setting minimum qphi for inclusion in the image calculation + phi1: float + Angle in degrees anticlockwise from horizontal-right for setting maximum qphi for inclusion in the image calculation + Rshape: tuple, list, array + a 2 element vector giving the real space dimensions. If not specified, this is determined from the max along points_array + tol: float + the tolerance in pixels or calibrated units for a point of qr in the points_array to be considered to match to the radius + + Returns + ---------- + image: numpy array + 2D numpy array with dimensions determined by Rshape + + """ + if Rshape is None: + Rshape = ( + np.max(np.max(points_array[:, 3])).astype("int") + 1, + np.max(np.max(points_array[:, 4])).astype("int") + 1, + ) + + radial_filtered_points_array = radial_filtered_array( + points_array_w_rphi, radius, tol + ) + + rphi_filtered_points_array = np.delete( + radial_filtered_points_array, + np.where( + np.logical_or( + radial_filtered_points_array[:, 6] < phi0, + radial_filtered_points_array[:, 6] > phi1, + ) + ), + axis=0, + ) + radiusazimuthimage = np.zeros(shape=Rshape) + + for i in range(h): + for j in range(w): + radiusazimuthimage[i, j] = np.where( + np.logical_and( + rphi_filtered_points_array[:, 3] == i, + rphi_filtered_points_array[:, 4] == j, + ), + rphi_filtered_points_array[:, 2], + 0, + ).sum() + return radiusazimuthimage From a005f1cf4d452996cd72292548b515c2ad554dc9 Mon Sep 17 00:00:00 2001 From: maclariz Date: Thu, 19 Sep 2024 15:28:41 +0100 Subject: [PATCH 2/5] Update digital_dark_field.py fixed minor errors --- py4DSTEM/process/diffraction/digital_dark_field.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/py4DSTEM/process/diffraction/digital_dark_field.py b/py4DSTEM/process/diffraction/digital_dark_field.py index 8e9956e7b..4f3ae5690 100644 --- a/py4DSTEM/process/diffraction/digital_dark_field.py +++ b/py4DSTEM/process/diffraction/digital_dark_field.py @@ -552,8 +552,8 @@ def DDF_radial_image(points_array_w_rphi, radius, Rshape, tol=1): if Rshape is None: Rshape = ( - np.max(np.max(points_array[:, 3])).astype("int") + 1, - np.max(np.max(points_array[:, 4])).astype("int") + 1, + np.max(np.max(points_array_w_rphi[:, 3])).astype("int") + 1, + np.max(np.max(points_array_w_rphi[:, 4])).astype("int") + 1, ) radial_filtered_points_array = radial_filtered_array( @@ -603,8 +603,8 @@ def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1 """ if Rshape is None: Rshape = ( - np.max(np.max(points_array[:, 3])).astype("int") + 1, - np.max(np.max(points_array[:, 4])).astype("int") + 1, + np.max(np.max(points_array_w_rphi[:, 3])).astype("int") + 1, + np.max(np.max(points_array_w_rphi[:, 4])).astype("int") + 1, ) radial_filtered_points_array = radial_filtered_array( @@ -623,8 +623,8 @@ def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1 ) radiusazimuthimage = np.zeros(shape=Rshape) - for i in range(h): - for j in range(w): + for i in range(Rshape[0]): + for j in range(Rshape[1]): radiusazimuthimage[i, j] = np.where( np.logical_and( rphi_filtered_points_array[:, 3] == i, From 13357b1de2192848b8b4f2b6e70ff4636f704438 Mon Sep 17 00:00:00 2001 From: maclariz Date: Fri, 15 Nov 2024 08:51:08 +0000 Subject: [PATCH 3/5] Update digital_dark_field.py --- py4DSTEM/process/diffraction/digital_dark_field.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/py4DSTEM/process/diffraction/digital_dark_field.py b/py4DSTEM/process/diffraction/digital_dark_field.py index 4f3ae5690..44a3ad2e7 100644 --- a/py4DSTEM/process/diffraction/digital_dark_field.py +++ b/py4DSTEM/process/diffraction/digital_dark_field.py @@ -496,7 +496,8 @@ def DDFimage(points_array, aperture_positions, Rshape=None, tol=1): def radial_filtered_array(points_array_w_rphi, radius, tol=1): """ - Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance + Calculates a Filtered points array from a list of detected diffraction peak positions in a points_array + matching a specific qr radius, within a defined matching tolerance Parameters ---------- @@ -578,7 +579,8 @@ def DDF_radial_image(points_array_w_rphi, radius, Rshape, tol=1): def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1): """ - Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance + Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array + matching a specific qr radius, within a defined matching tolerance, and only within a defined azimuthal range Parameters ---------- @@ -616,7 +618,7 @@ def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1 np.where( np.logical_or( radial_filtered_points_array[:, 6] < phi0, - radial_filtered_points_array[:, 6] > phi1, + radial_filtered_points_array[:, 6] >= phi1, ) ), axis=0, From d3e227b8ac0c2e8d088af9ff54e68aff6ccad501 Mon Sep 17 00:00:00 2001 From: bsavitzky Date: Mon, 18 Nov 2024 16:14:47 -0500 Subject: [PATCH 4/5] black formatting --- py4DSTEM/io/filereaders/read_mib.py | 1 - .../diffraction/WK_scattering_factors.py | 4 ++- py4DSTEM/process/diffraction/crystal_viz.py | 3 +- .../process/diffraction/digital_dark_field.py | 4 +-- py4DSTEM/process/fit/fit.py | 3 +- .../magnetic_ptychographic_tomography.py | 28 +++++++-------- .../process/phase/magnetic_ptychography.py | 28 +++++++-------- py4DSTEM/process/phase/parallax.py | 22 ++++++------ .../process/phase/ptychographic_methods.py | 4 ++- .../process/phase/ptychographic_tomography.py | 28 +++++++-------- py4DSTEM/process/phase/utils.py | 4 ++- .../phase/xray_magnetic_ptychography.py | 34 ++++++++----------- py4DSTEM/process/utils/utils.py | 18 ++++++++-- 13 files changed, 97 insertions(+), 84 deletions(-) diff --git a/py4DSTEM/io/filereaders/read_mib.py b/py4DSTEM/io/filereaders/read_mib.py index 178a239ad..e0de41813 100644 --- a/py4DSTEM/io/filereaders/read_mib.py +++ b/py4DSTEM/io/filereaders/read_mib.py @@ -163,7 +163,6 @@ def scan_size(path, scan): header_path = path[:-3] + "hdr" result = {} if os.path.exists(header_path): - with open(header_path, encoding="UTF-8") as f: for line in f: k, v = line.split("\t", 1) diff --git a/py4DSTEM/process/diffraction/WK_scattering_factors.py b/py4DSTEM/process/diffraction/WK_scattering_factors.py index eb964de96..70110a977 100644 --- a/py4DSTEM/process/diffraction/WK_scattering_factors.py +++ b/py4DSTEM/process/diffraction/WK_scattering_factors.py @@ -221,7 +221,9 @@ def RI1(BI, BJ, G): ri1[sub] = np.pi * (BI * np.log((BI + BJ) / BI) + BJ * np.log((BI + BJ) / BJ)) sub = np.logical_and(eps <= 0.1, G > 0.0) - temp = 0.5 * BI**2 * np.log(BI / (BI + BJ)) + 0.5 * BJ**2 * np.log(BJ / (BI + BJ)) + temp = 0.5 * BI**2 * np.log(BI / (BI + BJ)) + 0.5 * BJ**2 * np.log( + BJ / (BI + BJ) + ) temp += 0.75 * (BI**2 + BJ**2) - 0.25 * (BI + BJ) ** 2 temp -= 0.5 * (BI - BJ) ** 2 ri1[sub] += np.pi * G[sub] ** 2 * temp diff --git a/py4DSTEM/process/diffraction/crystal_viz.py b/py4DSTEM/process/diffraction/crystal_viz.py index 5bc16c1bc..5c3c8303b 100644 --- a/py4DSTEM/process/diffraction/crystal_viz.py +++ b/py4DSTEM/process/diffraction/crystal_viz.py @@ -453,7 +453,8 @@ def plot_scattering_intensity( int_sf_plot = calc_1D_profile( k, self.g_vec_leng, - (self.struct_factors_int**int_power_scale) * (self.g_vec_leng**k_power_scale), + (self.struct_factors_int**int_power_scale) + * (self.g_vec_leng**k_power_scale), remove_origin=True, k_broadening=k_broadening, int_scale=int_scale, diff --git a/py4DSTEM/process/diffraction/digital_dark_field.py b/py4DSTEM/process/diffraction/digital_dark_field.py index 44a3ad2e7..7488dde64 100644 --- a/py4DSTEM/process/diffraction/digital_dark_field.py +++ b/py4DSTEM/process/diffraction/digital_dark_field.py @@ -496,7 +496,7 @@ def DDFimage(points_array, aperture_positions, Rshape=None, tol=1): def radial_filtered_array(points_array_w_rphi, radius, tol=1): """ - Calculates a Filtered points array from a list of detected diffraction peak positions in a points_array + Calculates a Filtered points array from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance Parameters @@ -579,7 +579,7 @@ def DDF_radial_image(points_array_w_rphi, radius, Rshape, tol=1): def DDFradialazimuthimage(points_array_w_rphi, radius, phi0, phi1, Rshape, tol=1): """ - Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array + Calculates a Digital Dark Field image from a list of detected diffraction peak positions in a points_array matching a specific qr radius, within a defined matching tolerance, and only within a defined azimuthal range Parameters diff --git a/py4DSTEM/process/fit/fit.py b/py4DSTEM/process/fit/fit.py index 5c2d56a3c..9973ff79f 100644 --- a/py4DSTEM/process/fit/fit.py +++ b/py4DSTEM/process/fit/fit.py @@ -169,7 +169,8 @@ def polar_gaussian_2D( # t2 = np.min(np.vstack([t,1-t])) t2 = np.square(t - mu_t) return ( - I0 * np.exp(-(t2 / (2 * sigma_t**2) + (q - mu_q) ** 2 / (2 * sigma_q**2))) + C + I0 * np.exp(-(t2 / (2 * sigma_t**2) + (q - mu_q) ** 2 / (2 * sigma_q**2))) + + C ) diff --git a/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py b/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py index 8265c1325..3e13f09f6 100644 --- a/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py +++ b/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py @@ -1202,20 +1202,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[batch_indices] = ( - self._position_correction( - object_sliced, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, - ) + self._positions_px_all[ + batch_indices + ] = self._position_correction( + object_sliced, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/magnetic_ptychography.py b/py4DSTEM/process/phase/magnetic_ptychography.py index 975f6ac84..ce1c4795b 100644 --- a/py4DSTEM/process/phase/magnetic_ptychography.py +++ b/py4DSTEM/process/phase/magnetic_ptychography.py @@ -1510,20 +1510,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[batch_indices] = ( - self._position_correction( - self._object, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, - ) + self._positions_px_all[ + batch_indices + ] = self._position_correction( + self._object, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/parallax.py b/py4DSTEM/process/phase/parallax.py index e5768f3cc..a7673e76b 100644 --- a/py4DSTEM/process/phase/parallax.py +++ b/py4DSTEM/process/phase/parallax.py @@ -884,15 +884,17 @@ def guess_common_aberrations( sampling = 1 / ( np.array(self._reciprocal_sampling) * self._region_of_interest_shape ) - aberrations_basis, aberrations_basis_du, aberrations_basis_dv = ( - calculate_aberration_gradient_basis( - aberrations_mn, - sampling, - self._region_of_interest_shape, - self._wavelength, - rotation_angle=np.deg2rad(rotation_angle_deg), - xp=xp, - ) + ( + aberrations_basis, + aberrations_basis_du, + aberrations_basis_dv, + ) = calculate_aberration_gradient_basis( + aberrations_mn, + sampling, + self._region_of_interest_shape, + self._wavelength, + rotation_angle=np.deg2rad(rotation_angle_deg), + xp=xp, ) # shifts @@ -2432,7 +2434,6 @@ def score_CTF(coefs): # Plot the measured/fitted shifts comparison if plot_BF_shifts_comparison: - fitted_shifts = ( xp.tensordot(gradients, xp.array(self._aberrations_coefs), axes=1) .reshape((2, -1)) @@ -3055,7 +3056,6 @@ def show_shifts( shifts = shifts_px * scale_arrows * xp.array(self._reciprocal_sampling) if plot_rotated_shifts and hasattr(self, "rotation_Q_to_R_rads"): - if figax is None: figsize = kwargs.pop("figsize", (8, 4)) fig, ax = plt.subplots(1, 2, figsize=figsize) diff --git a/py4DSTEM/process/phase/ptychographic_methods.py b/py4DSTEM/process/phase/ptychographic_methods.py index 2e47a5e23..e7a332787 100644 --- a/py4DSTEM/process/phase/ptychographic_methods.py +++ b/py4DSTEM/process/phase/ptychographic_methods.py @@ -366,7 +366,9 @@ def _precompute_propagator_arrays( propagators[i] = xp.exp( 1.0j * (-(kx**2)[:, None] * np.pi * wavelength * dz) ) - propagators[i] *= xp.exp(1.0j * (-(ky**2)[None] * np.pi * wavelength * dz)) + propagators[i] *= xp.exp( + 1.0j * (-(ky**2)[None] * np.pi * wavelength * dz) + ) if theta_x is not None: propagators[i] *= xp.exp( diff --git a/py4DSTEM/process/phase/ptychographic_tomography.py b/py4DSTEM/process/phase/ptychographic_tomography.py index 037ef4849..e672e712e 100644 --- a/py4DSTEM/process/phase/ptychographic_tomography.py +++ b/py4DSTEM/process/phase/ptychographic_tomography.py @@ -1093,20 +1093,20 @@ def reconstruct( # position correction if not fix_positions: - self._positions_px_all[batch_indices] = ( - self._position_correction( - object_sliced, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, - ) + self._positions_px_all[ + batch_indices + ] = self._position_correction( + object_sliced, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/utils.py b/py4DSTEM/process/phase/utils.py index bb960da62..95b7fca4e 100644 --- a/py4DSTEM/process/phase/utils.py +++ b/py4DSTEM/process/phase/utils.py @@ -202,7 +202,9 @@ def evaluate_gaussian_envelope( self, alpha: Union[float, np.ndarray] ) -> Union[float, np.ndarray]: xp = self._xp - return xp.exp(-0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2) + return xp.exp( + -0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2 + ) def evaluate_spatial_envelope( self, alpha: Union[float, np.ndarray], phi: Union[float, np.ndarray] diff --git a/py4DSTEM/process/phase/xray_magnetic_ptychography.py b/py4DSTEM/process/phase/xray_magnetic_ptychography.py index b1b8a5862..62a4aeb4c 100644 --- a/py4DSTEM/process/phase/xray_magnetic_ptychography.py +++ b/py4DSTEM/process/phase/xray_magnetic_ptychography.py @@ -892,7 +892,6 @@ def _gradient_descent_adjoint( match (self._recon_mode, self._active_measurement_index): case (0, 0) | (1, 0): # reverse - magnetic_conj = xp.exp(1.0j * xp.conj(object_patches[1])) probe_magnetic_abs = xp.abs(shifted_probes * magnetic_conj) @@ -930,7 +929,6 @@ def _gradient_descent_adjoint( ) if not fix_probe: - electrostatic_magnetic_abs = xp.abs( electrostatic_conj * magnetic_conj ) @@ -962,7 +960,6 @@ def _gradient_descent_adjoint( ) case (0, 1) | (1, 2) | (2, 1): # forward - magnetic_conj = xp.exp(-1.0j * xp.conj(object_patches[1])) probe_magnetic_abs = xp.abs(shifted_probes * magnetic_conj) @@ -992,7 +989,6 @@ def _gradient_descent_adjoint( ) if not fix_probe: - electrostatic_magnetic_abs = xp.abs( electrostatic_conj * magnetic_conj ) @@ -1024,7 +1020,6 @@ def _gradient_descent_adjoint( ) case (1, 1) | (2, 0): # neutral - probe_abs = xp.abs(shifted_probes) probe_normalization = self._sum_overlapping_patches_bincounts( probe_abs**2, @@ -1047,7 +1042,6 @@ def _gradient_descent_adjoint( ) if not fix_probe: - electrostatic_abs = xp.abs(electrostatic_conj) electrostatic_normalization = xp.sum( electrostatic_abs**2, @@ -1482,20 +1476,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[batch_indices] = ( - self._position_correction( - self._object, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, - ) + self._positions_px_all[ + batch_indices + ] = self._position_correction( + self._object, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, ) measurement_error += batch_error diff --git a/py4DSTEM/process/utils/utils.py b/py4DSTEM/process/utils/utils.py index ddeeb2c36..60da616d1 100644 --- a/py4DSTEM/process/utils/utils.py +++ b/py4DSTEM/process/utils/utils.py @@ -93,7 +93,12 @@ def electron_wavelength_angstrom(E_eV): c = 299792458 h = 6.62607 * 10**-34 - lam = h / ma.sqrt(2 * m * e * E_eV) / ma.sqrt(1 + e * E_eV / 2 / m / c**2) * 10**10 + lam = ( + h + / ma.sqrt(2 * m * e * E_eV) + / ma.sqrt(1 + e * E_eV / 2 / m / c**2) + * 10**10 + ) return lam @@ -102,8 +107,15 @@ def electron_interaction_parameter(E_eV): e = 1.602177 * 10**-19 c = 299792458 h = 6.62607 * 10**-34 - lam = h / ma.sqrt(2 * m * e * E_eV) / ma.sqrt(1 + e * E_eV / 2 / m / c**2) * 10**10 - sigma = (2 * np.pi / lam / E_eV) * (m * c**2 + e * E_eV) / (2 * m * c**2 + e * E_eV) + lam = ( + h + / ma.sqrt(2 * m * e * E_eV) + / ma.sqrt(1 + e * E_eV / 2 / m / c**2) + * 10**10 + ) + sigma = ( + (2 * np.pi / lam / E_eV) * (m * c**2 + e * E_eV) / (2 * m * c**2 + e * E_eV) + ) return sigma From 20917d07675de1de06ce6c9f124d280dd4effe0b Mon Sep 17 00:00:00 2001 From: bsavitzky Date: Mon, 18 Nov 2024 16:19:09 -0500 Subject: [PATCH 5/5] black formatting --- .../diffraction/WK_scattering_factors.py | 4 +-- py4DSTEM/process/diffraction/crystal_viz.py | 3 +- py4DSTEM/process/fit/fit.py | 3 +- .../magnetic_ptychographic_tomography.py | 28 +++++++++---------- .../process/phase/magnetic_ptychography.py | 28 +++++++++---------- .../process/phase/ptychographic_methods.py | 4 +-- .../process/phase/ptychographic_tomography.py | 28 +++++++++---------- py4DSTEM/process/phase/utils.py | 4 +-- .../phase/xray_magnetic_ptychography.py | 28 +++++++++---------- py4DSTEM/process/utils/utils.py | 18 ++---------- 10 files changed, 64 insertions(+), 84 deletions(-) diff --git a/py4DSTEM/process/diffraction/WK_scattering_factors.py b/py4DSTEM/process/diffraction/WK_scattering_factors.py index 70110a977..eb964de96 100644 --- a/py4DSTEM/process/diffraction/WK_scattering_factors.py +++ b/py4DSTEM/process/diffraction/WK_scattering_factors.py @@ -221,9 +221,7 @@ def RI1(BI, BJ, G): ri1[sub] = np.pi * (BI * np.log((BI + BJ) / BI) + BJ * np.log((BI + BJ) / BJ)) sub = np.logical_and(eps <= 0.1, G > 0.0) - temp = 0.5 * BI**2 * np.log(BI / (BI + BJ)) + 0.5 * BJ**2 * np.log( - BJ / (BI + BJ) - ) + temp = 0.5 * BI**2 * np.log(BI / (BI + BJ)) + 0.5 * BJ**2 * np.log(BJ / (BI + BJ)) temp += 0.75 * (BI**2 + BJ**2) - 0.25 * (BI + BJ) ** 2 temp -= 0.5 * (BI - BJ) ** 2 ri1[sub] += np.pi * G[sub] ** 2 * temp diff --git a/py4DSTEM/process/diffraction/crystal_viz.py b/py4DSTEM/process/diffraction/crystal_viz.py index 5c3c8303b..5bc16c1bc 100644 --- a/py4DSTEM/process/diffraction/crystal_viz.py +++ b/py4DSTEM/process/diffraction/crystal_viz.py @@ -453,8 +453,7 @@ def plot_scattering_intensity( int_sf_plot = calc_1D_profile( k, self.g_vec_leng, - (self.struct_factors_int**int_power_scale) - * (self.g_vec_leng**k_power_scale), + (self.struct_factors_int**int_power_scale) * (self.g_vec_leng**k_power_scale), remove_origin=True, k_broadening=k_broadening, int_scale=int_scale, diff --git a/py4DSTEM/process/fit/fit.py b/py4DSTEM/process/fit/fit.py index 9973ff79f..5c2d56a3c 100644 --- a/py4DSTEM/process/fit/fit.py +++ b/py4DSTEM/process/fit/fit.py @@ -169,8 +169,7 @@ def polar_gaussian_2D( # t2 = np.min(np.vstack([t,1-t])) t2 = np.square(t - mu_t) return ( - I0 * np.exp(-(t2 / (2 * sigma_t**2) + (q - mu_q) ** 2 / (2 * sigma_q**2))) - + C + I0 * np.exp(-(t2 / (2 * sigma_t**2) + (q - mu_q) ** 2 / (2 * sigma_q**2))) + C ) diff --git a/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py b/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py index 3e13f09f6..8265c1325 100644 --- a/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py +++ b/py4DSTEM/process/phase/magnetic_ptychographic_tomography.py @@ -1202,20 +1202,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[ - batch_indices - ] = self._position_correction( - object_sliced, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, + self._positions_px_all[batch_indices] = ( + self._position_correction( + object_sliced, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, + ) ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/magnetic_ptychography.py b/py4DSTEM/process/phase/magnetic_ptychography.py index ce1c4795b..975f6ac84 100644 --- a/py4DSTEM/process/phase/magnetic_ptychography.py +++ b/py4DSTEM/process/phase/magnetic_ptychography.py @@ -1510,20 +1510,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[ - batch_indices - ] = self._position_correction( - self._object, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, + self._positions_px_all[batch_indices] = ( + self._position_correction( + self._object, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, + ) ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/ptychographic_methods.py b/py4DSTEM/process/phase/ptychographic_methods.py index e7a332787..2e47a5e23 100644 --- a/py4DSTEM/process/phase/ptychographic_methods.py +++ b/py4DSTEM/process/phase/ptychographic_methods.py @@ -366,9 +366,7 @@ def _precompute_propagator_arrays( propagators[i] = xp.exp( 1.0j * (-(kx**2)[:, None] * np.pi * wavelength * dz) ) - propagators[i] *= xp.exp( - 1.0j * (-(ky**2)[None] * np.pi * wavelength * dz) - ) + propagators[i] *= xp.exp(1.0j * (-(ky**2)[None] * np.pi * wavelength * dz)) if theta_x is not None: propagators[i] *= xp.exp( diff --git a/py4DSTEM/process/phase/ptychographic_tomography.py b/py4DSTEM/process/phase/ptychographic_tomography.py index e672e712e..037ef4849 100644 --- a/py4DSTEM/process/phase/ptychographic_tomography.py +++ b/py4DSTEM/process/phase/ptychographic_tomography.py @@ -1093,20 +1093,20 @@ def reconstruct( # position correction if not fix_positions: - self._positions_px_all[ - batch_indices - ] = self._position_correction( - object_sliced, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, + self._positions_px_all[batch_indices] = ( + self._position_correction( + object_sliced, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, + ) ) measurement_error += batch_error diff --git a/py4DSTEM/process/phase/utils.py b/py4DSTEM/process/phase/utils.py index 95b7fca4e..bb960da62 100644 --- a/py4DSTEM/process/phase/utils.py +++ b/py4DSTEM/process/phase/utils.py @@ -202,9 +202,7 @@ def evaluate_gaussian_envelope( self, alpha: Union[float, np.ndarray] ) -> Union[float, np.ndarray]: xp = self._xp - return xp.exp( - -0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2 - ) + return xp.exp(-0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2) def evaluate_spatial_envelope( self, alpha: Union[float, np.ndarray], phi: Union[float, np.ndarray] diff --git a/py4DSTEM/process/phase/xray_magnetic_ptychography.py b/py4DSTEM/process/phase/xray_magnetic_ptychography.py index 62a4aeb4c..a89905a6c 100644 --- a/py4DSTEM/process/phase/xray_magnetic_ptychography.py +++ b/py4DSTEM/process/phase/xray_magnetic_ptychography.py @@ -1476,20 +1476,20 @@ def reconstruct( # position correction if not fix_positions and a0 > 0: - self._positions_px_all[ - batch_indices - ] = self._position_correction( - self._object, - vectorized_patch_indices_row, - vectorized_patch_indices_col, - shifted_probes, - overlap, - amplitudes_device, - positions_px, - positions_px_initial, - positions_step_size, - max_position_update_distance, - max_position_total_distance, + self._positions_px_all[batch_indices] = ( + self._position_correction( + self._object, + vectorized_patch_indices_row, + vectorized_patch_indices_col, + shifted_probes, + overlap, + amplitudes_device, + positions_px, + positions_px_initial, + positions_step_size, + max_position_update_distance, + max_position_total_distance, + ) ) measurement_error += batch_error diff --git a/py4DSTEM/process/utils/utils.py b/py4DSTEM/process/utils/utils.py index 60da616d1..ddeeb2c36 100644 --- a/py4DSTEM/process/utils/utils.py +++ b/py4DSTEM/process/utils/utils.py @@ -93,12 +93,7 @@ def electron_wavelength_angstrom(E_eV): c = 299792458 h = 6.62607 * 10**-34 - lam = ( - h - / ma.sqrt(2 * m * e * E_eV) - / ma.sqrt(1 + e * E_eV / 2 / m / c**2) - * 10**10 - ) + lam = h / ma.sqrt(2 * m * e * E_eV) / ma.sqrt(1 + e * E_eV / 2 / m / c**2) * 10**10 return lam @@ -107,15 +102,8 @@ def electron_interaction_parameter(E_eV): e = 1.602177 * 10**-19 c = 299792458 h = 6.62607 * 10**-34 - lam = ( - h - / ma.sqrt(2 * m * e * E_eV) - / ma.sqrt(1 + e * E_eV / 2 / m / c**2) - * 10**10 - ) - sigma = ( - (2 * np.pi / lam / E_eV) * (m * c**2 + e * E_eV) / (2 * m * c**2 + e * E_eV) - ) + lam = h / ma.sqrt(2 * m * e * E_eV) / ma.sqrt(1 + e * E_eV / 2 / m / c**2) * 10**10 + sigma = (2 * np.pi / lam / E_eV) * (m * c**2 + e * E_eV) / (2 * m * c**2 + e * E_eV) return sigma