From 8038d1c3ca2efd5738f54156c7262e6a1080ad25 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 6 Aug 2026 15:39:38 -0400 Subject: [PATCH] fix: inverse reference-frame transform Sph name check (startswith -> endswith) --- autogalaxy/profiles/geometry_profiles.py | 2 +- .../profiles/test_geometry_profiles.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/autogalaxy/profiles/geometry_profiles.py b/autogalaxy/profiles/geometry_profiles.py index 90ce10ed..5ab82979 100644 --- a/autogalaxy/profiles/geometry_profiles.py +++ b/autogalaxy/profiles/geometry_profiles.py @@ -389,7 +389,7 @@ def transformed_from_reference_frame_grid_from( grid The (y, x) coordinates in the reference frame of the profile. """ - if self.__class__.__name__.startswith("Sph"): + if self.__class__.__name__.endswith("Sph"): return super().transformed_from_reference_frame_grid_from(grid=grid, xp=xp) return aa.util.geometry.transform_grid_2d_from_reference_frame( diff --git a/test_autogalaxy/profiles/test_geometry_profiles.py b/test_autogalaxy/profiles/test_geometry_profiles.py index 9f4da5c5..a008cb13 100644 --- a/test_autogalaxy/profiles/test_geometry_profiles.py +++ b/test_autogalaxy/profiles/test_geometry_profiles.py @@ -1,6 +1,7 @@ from __future__ import division, print_function from pathlib import Path +from unittest import mock import numpy as np import pytest @@ -135,6 +136,36 @@ def test__spherical__transform_to_and_from_reference_frame__roundtrip_recovers_o assert transformed_grid == pytest.approx(grid_original, 1e-5) +def test__sph_named_profile__both_transforms_use_translation_only_path(): + # Regression test: transformed_from_reference_frame_grid_from checked + # startswith("Sph") but spherical profiles are suffix-named (IsothermalSph), + # so the inverse transform silently took the elliptical rotation path. + profile = ag.mp.IsothermalSph(centre=(1.0, 2.0), einstein_radius=1.0) + + grid = ag.Grid2DIrregular([[3.0, 5.0]]) + + transformed_grid = profile.transformed_to_reference_frame_grid_from(grid) + + assert transformed_grid == pytest.approx(np.array([[2.0, 3.0]]), 1e-8) + + recovered_grid = profile.transformed_from_reference_frame_grid_from( + transformed_grid + ) + + assert recovered_grid == pytest.approx(grid.array, 1e-8) + + with mock.patch.object( + geometry_profiles.aa.util.geometry, "transform_grid_2d_to_reference_frame" + ) as rotation_to, mock.patch.object( + geometry_profiles.aa.util.geometry, "transform_grid_2d_from_reference_frame" + ) as rotation_from: + profile.transformed_to_reference_frame_grid_from(grid) + profile.transformed_from_reference_frame_grid_from(transformed_grid) + + rotation_to.assert_not_called() + rotation_from.assert_not_called() + + def test__elliptical__transform_grid_to_reference_frame__zero_ell_comps__grid_unchanged(): elliptical_profile = geometry_profiles.EllProfile(ell_comps=(0.0, 0.0))