Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions autoarray/inversion/mesh/border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ def ellipse_params_via_border_pca_from(border_grid, xp=np, eps=1e-12):

phi = xp.arctan2(v_major[1], v_major[0])

# PCA eigenvectors are undefined for an isotropic covariance. NumPy and
# JAX may therefore choose different, equally valid orientations whose
# downstream max-extent ellipses are not equivalent. Use a deterministic
# axis-aligned frame when the eigenvalue gap is at floating-point scale.
eigenvalue_scale = xp.maximum(xp.max(xp.abs(evals)), eps)
relative_gap = (evals[-1] - evals[0]) / eigenvalue_scale
isotropy_tolerance = xp.sqrt(xp.finfo(C.dtype).eps)
phi = xp.where(relative_gap <= isotropy_tolerance, 0.0, phi)

# Rotate border points into ellipse-aligned frame
c = xp.cos(phi)
s = xp.sin(phi)
Expand Down
38 changes: 38 additions & 0 deletions test_autoarray/inversion/pixelization/test_border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import autoarray as aa

from autoarray.inversion.mesh.border_relocator import (
ellipse_params_via_border_pca_from,
sub_border_pixel_slim_indexes_from,
)

Expand Down Expand Up @@ -376,3 +377,40 @@ def test__relocated_grid_from__positive_origin_included_in_relocate():
relocated_grid = border_relocator.relocated_grid_from(grid=grid)

assert relocated_grid.over_sampled[1] == pytest.approx([1.95, 1.0], 1e-4)

def test__ellipse_params__near_isotropic_border_uses_deterministic_axis():
border_grid = np.array(
[
[-0.0960155108, 0.0960155108],
[-0.55, 0.0],
[-0.0960155108, -0.0960155108],
[0.0, 0.55],
[0.0, -0.55],
[0.0960155108, 0.0960155108],
[0.55, 0.0],
[0.0960155108, -0.0960155108],
]
)

_, a, b, phi = ellipse_params_via_border_pca_from(border_grid=border_grid)

assert float(phi) == 0.0
assert float(a) == pytest.approx(0.55 + 1.0e-12)
assert float(b) == pytest.approx(0.55 + 1.0e-12)


def test__ellipse_params__anisotropic_border_retains_pca_major_axis():
border_grid = np.array(
[
[-2.0, 0.0],
[0.0, 1.0],
[2.0, 0.0],
[0.0, -1.0],
]
)

_, a, b, phi = ellipse_params_via_border_pca_from(border_grid=border_grid)

assert abs(float(phi)) == pytest.approx(np.pi / 2.0)
assert float(a) > float(b)

Loading