Skip to content

Commit

Permalink
Alignment/centering: changed API
Browse files Browse the repository at this point in the history
Signed-off-by: Nicola VIGANO <nicola.vigano@esrf.fr>
  • Loading branch information
Obi-Wan committed Mar 26, 2024
1 parent 849e165 commit 8606a69
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
10 changes: 4 additions & 6 deletions corrct/alignment/centering.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class RecenterVolume:
"""Volume recentering class."""
"""Volume re-centering class."""

def __init__(
self, proj_geom: models.ProjectionGeometry, angles_rad: Union[NDArray, ArrayLike], precision: int = 2
Expand All @@ -40,9 +40,7 @@ def _apply_displacement_vu(self, shifts_vu: NDArray, displacemenet_zyx: NDArray)
shifts_vu_corrs = self.prj_geom.project_displacement_to_detector(displacemenet_zyx)
return np.around(shifts_vu + shifts_vu_corrs, decimals=self.precision)

def recenter_to(
self, shifts_vu: Union[ArrayLike, NDArray], volume: NDArray, com_ref_zyx: Union[ArrayLike, NDArray]
) -> NDArray:
def to_com(self, shifts_vu: Union[ArrayLike, NDArray], volume: NDArray, com_ref_zyx: Union[ArrayLike, NDArray]) -> NDArray:
"""Recenter to a given center-of-mass (CoM).
Parameters
Expand All @@ -63,7 +61,7 @@ def recenter_to(
displacemenet_zyx = com_ref_zyx - com_rec_zyx
return self._apply_displacement_vu(shifts_vu, displacemenet_zyx)

def recenter_as(self, shifts_vu: NDArray, volume: NDArray, reference: NDArray, method: str = "com") -> NDArray:
def as_reference(self, shifts_vu: NDArray, volume: NDArray, reference: NDArray, method: str = "com") -> NDArray:
"""Recenter with respect to a given volume.
Parameters
Expand All @@ -89,7 +87,7 @@ def recenter_as(self, shifts_vu: NDArray, volume: NDArray, reference: NDArray, m
"""
if method.lower() == "com":
com_ref_zyx = post_proc.com(reference)
return self.recenter_to(shifts_vu, volume, com_ref_zyx)
return self.to_com(shifts_vu, volume, com_ref_zyx)
elif method.lower() == "xc":
displacemenet_zyx = fitting.fit_shifts_zyx_xc(reference, volume, decimals=self.precision)
return self._apply_displacement_vu(shifts_vu, displacemenet_zyx)
Expand Down
6 changes: 3 additions & 3 deletions examples/alignment/example_01_pre-alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def cm2inch(x: Union[float, Sequence[float], NDArray]) -> Sequence[float]:
rec_noise_pre, _ = solver(A, data_test, iterations=ITERATIONS, **solver_opts)

# Recentering the reconstructions on the phantom's center-of-mass -> moving the shifts accordingly
theo_rot_axis = recenter.recenter_to(THEO_ROT_AXIS, rec_noise_theocor, com_ph_yx)
cor = recenter.recenter_to(cor, rec_noise_precor, com_ph_yx)
shifts_u_pre = recenter.recenter_to(shifts_u_pre, rec_noise_pre, com_ph_yx)
theo_rot_axis = recenter.to_com(THEO_ROT_AXIS, rec_noise_theocor, com_ph_yx)
cor = recenter.to_com(cor, rec_noise_precor, com_ph_yx)
shifts_u_pre = recenter.to_com(shifts_u_pre, rec_noise_pre, com_ph_yx)

# Reconstructing again, with centered shifts
with cct.projectors.ProjectorUncorrected(ph.shape, angles_rad, theo_rot_axis) as A:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ def test_pre_alignment(add_noise: bool, theo_rot_axis: float = -1.25):
recenter = cct.alignment.RecenterVolume(prj_geom, angles_rad)

# Re-centering the reconstructions on the phantom's center-of-mass -> moving the shifts accordingly
shifts_u_pre = recenter.recenter_to(shifts_u_pre, rec_noise_pre, com_ph_yx)
shifts_u_pre = recenter.to_com(shifts_u_pre, rec_noise_pre, com_ph_yx)

if debug:
theo_rot_axis_per_angle = np.ones_like(angles_rad) * theo_rot_axis
theo_rot_axis_per_angle = recenter.recenter_to(theo_rot_axis_per_angle, rec_noise_theocor, com_ph_yx)
cor = recenter.recenter_to(cor, rec_noise_precor, com_ph_yx)
theo_rot_axis_per_angle = recenter.to_com(theo_rot_axis_per_angle, rec_noise_theocor, com_ph_yx)
cor = recenter.to_com(cor, rec_noise_precor, com_ph_yx)

print(f"{theo_shifts = }")
print(f"{shifts_u_pre = }")
Expand Down Expand Up @@ -246,14 +246,14 @@ def test_pre_alignment_3d(add_noise: bool, theo_rot_axis: float = -1.25):
recenter = cct.alignment.RecenterVolume(prj_geom, angles_rad)

# Re-centering the reconstructions on the phantom's center-of-mass -> moving the shifts accordingly
shifts_vu_pre = recenter.recenter_to(shifts_vu_pre, rec_noise_xc, com_ph_yx)
shifts_vu_pre = recenter.to_com(shifts_vu_pre, rec_noise_xc, com_ph_yx)
if debug:
theo_rot_axis_per_angle = cct.models.combine_shifts_vu(
np.zeros_like(angles_rad), np.ones_like(angles_rad) * theo_rot_axis
)
theo_rot_axis_per_angle = recenter.recenter_to(theo_rot_axis_per_angle, rec_noise_theocor, com_ph_yx)
theo_rot_axis_per_angle = recenter.to_com(theo_rot_axis_per_angle, rec_noise_theocor, com_ph_yx)
cor_pre = cct.models.combine_shifts_vu(np.zeros_like(angles_rad), np.ones_like(angles_rad) * cor_pre)
cor_pre = recenter.recenter_to(cor_pre, rec_noise_precor, com_ph_yx)
cor_pre = recenter.to_com(cor_pre, rec_noise_precor, com_ph_yx)

print(f"{theo_shifts_vu = }")
print(f"{shifts_vu_pre = }")
Expand Down

0 comments on commit 8606a69

Please sign in to comment.