Skip to content

Commit

Permalink
added force_transpose option for other two aberration fit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gvarnavi committed Oct 22, 2023
1 parent ada4d4d commit 9529945
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions py4DSTEM/process/phase/iterative_parallax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,18 @@ def aberration_fit(
### First pass

# Convert real space shifts to Angstroms
self._xy_shifts_Ang = self._xy_shifts * xp.array(self._scan_sampling)

if force_transpose is None:
self.transpose_detected = False
else:
self.transpose_detected = force_transpose

if force_transpose is True:
self._xy_shifts_Ang = xp.flip(self._xy_shifts, axis=1) * xp.array(
self._scan_sampling
)
else:
self._xy_shifts_Ang = self._xy_shifts * xp.array(self._scan_sampling)

# Solve affine transformation
m = asnumpy(
Expand All @@ -1362,11 +1373,6 @@ def aberration_fit(
self.aberration_A1x = (m_aberration[0, 0] - m_aberration[1, 1]) / 2.0
self.aberration_A1y = (m_aberration[1, 0] + m_aberration[0, 1]) / 2.0

if force_transpose is None:
self.transpose_detected = False
else:
self.transpose_detected = force_transpose

### Second pass

# Aberration coefs
Expand Down Expand Up @@ -1590,8 +1596,15 @@ def score_CTF(coefs):
)
)

if force_transpose is None or force_transpose is True:
# Transposed fit
# (Relative) untransposed fit
tf = AffineTransform(angle=self.rotation_Q_to_R_rads)
rotated_shifts = tf(self._xy_shifts_Ang, xp=xp).T.ravel()
aberrations_coefs, res = xp.linalg.lstsq(
gradients, rotated_shifts, rcond=None
)[:2]

if force_transpose is None:
# (Relative) transposed fit
transposed_shifts = xp.flip(self._xy_shifts_Ang, axis=1)
m_T = asnumpy(
xp.linalg.lstsq(self._probe_angles, transposed_shifts, rcond=None)[
Expand All @@ -1615,19 +1628,10 @@ def score_CTF(coefs):
gradients, rotated_shifts_T, rcond=None
)[:2]

if force_transpose is None or force_transpose is False:
# Untransposed fit
tf = AffineTransform(angle=self.rotation_Q_to_R_rads)
rotated_shifts = tf(self._xy_shifts_Ang, xp=xp).T.ravel()
aberrations_coefs, res = xp.linalg.lstsq(
gradients, rotated_shifts, rcond=None
)[:2]

if force_transpose is None:
# Compare fits
if res_T.sum() < res.sum():
self.rotation_Q_to_R_rads = rotation_Q_to_R_rads_T
self.transpose_detected = True
self.transpose_detected = not self.transpose_detected
self._aberrations_coefs = asnumpy(aberrations_coefs_T)
self._rotated_shifts = rotated_shifts_T

Expand All @@ -1638,15 +1642,6 @@ def score_CTF(coefs):
),
UserWarning,
)
else:
self._aberrations_coefs = asnumpy(aberrations_coefs)
self._rotated_shifts = rotated_shifts

elif force_transpose is True:
self.rotation_Q_to_R_rads = rotation_Q_to_R_rads_T
self._aberrations_coefs = asnumpy(aberrations_coefs_T)
self._rotated_shifts = rotated_shifts_T

else:
self._aberrations_coefs = asnumpy(aberrations_coefs)
self._rotated_shifts = rotated_shifts
Expand Down

0 comments on commit 9529945

Please sign in to comment.