Skip to content

Commit d4f89bd

Browse files
Jammy2211claude
authored andcommitted
rename OperateDeflections to LensCalc, move fermat_potential_from into LensCalc
- Rename operate/deflections.py -> operate/lens_calc.py - Rename class OperateDeflections -> LensCalc throughout - LensCalc.__init__ now accepts optional potential_2d_from callable - from_mass_obj and from_tracer capture potential_2d_from automatically - fermat_potential_from moved into LensCalc (removed from MassProfile, Galaxy, Galaxies) - Update all imports, call sites, tests, and docs in autogalaxy Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent eabbc61 commit d4f89bd

11 files changed

Lines changed: 109 additions & 142 deletions

File tree

autogalaxy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
from .operate.image import OperateImage
7070
from .operate.image import OperateImageList
7171
from .operate.image import OperateImageGalaxies
72-
from .operate.deflections import OperateDeflections
72+
from .operate.lens_calc import LensCalc
7373
from .gui.scribbler import Scribbler
7474
from .imaging.fit_imaging import FitImaging
7575
from .imaging.model.analysis import AnalysisImaging

autogalaxy/galaxy/galaxies.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -243,34 +243,6 @@ def potential_2d_from(
243243
"""
244244
return sum(map(lambda g: g.potential_2d_from(grid=grid, xp=xp), self))
245245

246-
def fermat_potential_from(self, grid, xp=np) -> aa.Array2D:
247-
"""
248-
Returns the Fermat potential of the galaxies for a given grid of image-plane positions.
249-
250-
This is the sum of the geometric time delay term and the lensing potential, computed
251-
using the combined deflections and potential of all galaxies:
252-
253-
.. math::
254-
\\phi(\\boldsymbol{\\theta}) = \\frac{1}{2} |\\boldsymbol{\\alpha}|^2
255-
- \\psi(\\boldsymbol{\\theta})
256-
257-
Parameters
258-
----------
259-
grid
260-
The 2D grid of (y,x) arc-second coordinates the Fermat potential is computed on.
261-
xp
262-
The array module (``numpy`` or ``jax.numpy``).
263-
"""
264-
from autogalaxy.operate.deflections import OperateDeflections
265-
266-
od = OperateDeflections.from_mass_obj(self)
267-
time_delay = od.time_delay_geometry_term_from(grid=grid, xp=xp)
268-
potential = self.potential_2d_from(grid=grid, xp=xp)
269-
fermat_potential = time_delay - potential
270-
if isinstance(grid, aa.Grid2DIrregular):
271-
return aa.ArrayIrregular(values=fermat_potential)
272-
return aa.Array2D(values=fermat_potential, mask=grid.mask)
273-
274246
def has(self, cls: Union[Type, Tuple[Type]]) -> bool:
275247
"""
276248
Returns a bool specifying whether any of the galaxies has a certain class type.

autogalaxy/galaxy/galaxy.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,6 @@ def potential_2d_from(
343343
)
344344
return xp.zeros((grid.shape[0],))
345345

346-
def fermat_potential_from(self, grid, xp=np) -> aa.Array2D:
347-
"""
348-
Returns the Fermat potential of the galaxy for a given grid of image-plane positions.
349-
350-
This is the sum of the geometric time delay term and the lensing potential, computed
351-
using the galaxy's total deflections and total potential across all mass profiles:
352-
353-
.. math::
354-
\\phi(\\boldsymbol{\\theta}) = \\frac{1}{2} |\\boldsymbol{\\alpha}|^2
355-
- \\psi(\\boldsymbol{\\theta})
356-
357-
Parameters
358-
----------
359-
grid
360-
The 2D grid of (y,x) arc-second coordinates the Fermat potential is computed on.
361-
xp
362-
The array module (``numpy`` or ``jax.numpy``).
363-
"""
364-
from autogalaxy.operate.deflections import OperateDeflections
365-
366-
od = OperateDeflections.from_mass_obj(self)
367-
time_delay = od.time_delay_geometry_term_from(grid=grid, xp=xp)
368-
potential = self.potential_2d_from(grid=grid, xp=xp)
369-
fermat_potential = time_delay - potential
370-
if isinstance(grid, aa.Grid2DIrregular):
371-
return aa.ArrayIrregular(values=fermat_potential)
372-
return aa.Array2D(values=fermat_potential, mask=grid.mask)
373-
374346
@property
375347
def half_light_radius(self):
376348
return None
Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,40 @@ def wrapper(
7878
return wrapper
7979

8080

81-
class OperateDeflections:
81+
class LensCalc:
8282
"""
83-
Packages methods which manipulate the 2D deflection angle map returned from the `deflections_yx_2d_from` function
84-
of a mass object (e.g. a `MassProfile`, `Galaxy`).
83+
Computes lensing quantities from a deflection-angle callable and an optional potential callable.
8584
86-
The majority of methods are those which from the 2D deflection angle map compute lensing quantities like a 2D
87-
shear field, magnification map or the Einstein Radius.
85+
The deflection callable is used to compute the Hessian, Jacobian, convergence, shear,
86+
magnification, critical curves, caustics, and Einstein radius/mass. If a potential
87+
callable is also supplied, ``fermat_potential_from`` is available as well.
8888
8989
Parameters
9090
----------
9191
deflections_yx_2d_from
92-
A callable with signature ``(grid, xp=np, **kwargs)`` that returns the 2D deflection angles on the given
93-
grid. Typically a bound method of a ``MassProfile``, ``Galaxy``, or ``Galaxies`` instance.
92+
A callable with signature ``(grid, xp=np, **kwargs)`` that returns the 2D deflection
93+
angles on the given grid. Typically a bound method of a ``MassProfile``, ``Galaxy``,
94+
or ``Galaxies`` instance.
95+
potential_2d_from
96+
Optional callable with signature ``(grid, xp=np, **kwargs)`` that returns the 2D
97+
lensing potential on the given grid. Required only for ``fermat_potential_from``.
9498
"""
9599

96-
def __init__(self, deflections_yx_2d_from):
100+
def __init__(self, deflections_yx_2d_from, potential_2d_from=None):
97101
self.deflections_yx_2d_from = deflections_yx_2d_from
102+
self.potential_2d_from = potential_2d_from
98103

99104
@classmethod
100105
def from_mass_obj(cls, mass_obj):
101-
"""Construct from any object that has a ``deflections_yx_2d_from`` method."""
102-
return cls(deflections_yx_2d_from=mass_obj.deflections_yx_2d_from)
106+
"""Construct from any object that has a ``deflections_yx_2d_from`` method.
107+
108+
If the object also exposes ``potential_2d_from``, it is captured so that
109+
``fermat_potential_from`` is available on the returned instance.
110+
"""
111+
return cls(
112+
deflections_yx_2d_from=mass_obj.deflections_yx_2d_from,
113+
potential_2d_from=getattr(mass_obj, "potential_2d_from", None),
114+
)
103115

104116
@classmethod
105117
def from_tracer(
@@ -130,6 +142,8 @@ def from_tracer(
130142
Index of the second plane used by ``deflections_between_planes_from``.
131143
Ignored when ``use_multi_plane=False``. Defaults to ``-1`` (source plane).
132144
"""
145+
potential_2d_from = getattr(tracer, "potential_2d_from", None)
146+
133147
if use_multi_plane:
134148
from functools import partial
135149

@@ -138,9 +152,13 @@ def from_tracer(
138152
tracer.deflections_between_planes_from,
139153
plane_i=plane_i,
140154
plane_j=plane_j,
141-
)
155+
),
156+
potential_2d_from=potential_2d_from,
142157
)
143-
return cls(deflections_yx_2d_from=tracer.deflections_yx_2d_from)
158+
return cls(
159+
deflections_yx_2d_from=tracer.deflections_yx_2d_from,
160+
potential_2d_from=potential_2d_from,
161+
)
144162

145163
def time_delay_geometry_term_from(self, grid, xp=np) -> aa.Array2D:
146164
"""
@@ -177,6 +195,39 @@ def time_delay_geometry_term_from(self, grid, xp=np) -> aa.Array2D:
177195
return aa.ArrayIrregular(values=delay)
178196
return aa.Array2D(values=delay, mask=grid.mask)
179197

198+
def fermat_potential_from(self, grid, xp=np) -> aa.Array2D:
199+
"""
200+
Returns the Fermat potential for a given grid of image-plane positions.
201+
202+
This is the sum of the geometric time delay term and the gravitational (Shapiro) delay
203+
term (i.e. the lensing potential), and is given by:
204+
205+
.. math::
206+
\\phi(\\boldsymbol{\\theta}) = \\frac{1}{2} |\\boldsymbol{\\theta} - \\boldsymbol{\\beta}|^2
207+
- \\psi(\\boldsymbol{\\theta})
208+
209+
Requires that ``potential_2d_from`` was supplied at construction (e.g. via
210+
``LensCalc.from_mass_obj`` or ``LensCalc.from_tracer``).
211+
212+
Parameters
213+
----------
214+
grid
215+
The 2D grid of (y,x) arc-second coordinates the Fermat potential is computed on.
216+
xp
217+
The array module (``numpy`` or ``jax.numpy``).
218+
"""
219+
if self.potential_2d_from is None:
220+
raise ValueError(
221+
"fermat_potential_from requires a potential_2d_from callable. "
222+
"Construct LensCalc with potential_2d_from, or use from_mass_obj / from_tracer."
223+
)
224+
time_delay = self.time_delay_geometry_term_from(grid=grid, xp=xp)
225+
potential = self.potential_2d_from(grid=grid, xp=xp)
226+
fermat_potential = time_delay - potential
227+
if isinstance(grid, aa.Grid2DIrregular):
228+
return aa.ArrayIrregular(values=fermat_potential)
229+
return aa.Array2D(values=fermat_potential, mask=grid.mask)
230+
180231
def tangential_eigen_value_from(self, grid, xp=np) -> aa.Array2D:
181232
"""
182233
Returns the tangential eigen values of lensing jacobian, which are given by the expression:

autogalaxy/plot/mass_plotter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ def figures_2d(
118118
)
119119

120120
if magnification:
121-
from autogalaxy.operate.deflections import OperateDeflections
121+
from autogalaxy.operate.lens_calc import LensCalc
122122

123123
self.mat_plot_2d.plot_array(
124-
array=OperateDeflections.from_mass_obj(
124+
array=LensCalc.from_mass_obj(
125125
self.mass_obj
126126
).magnification_2d_from(grid=self.grid),
127127
visuals_2d=self.visuals_2d_with_critical_curves,

autogalaxy/plot/visuals/one_d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ def add_einstein_radius(
170170
The collection of attributes that can be plotted by a `Plotter` object.
171171
"""
172172

173-
from autogalaxy.operate.deflections import OperateDeflections
173+
from autogalaxy.operate.lens_calc import LensCalc
174174

175175
einstein_radius = None
176176

177177
try:
178-
od = OperateDeflections.from_mass_obj(mass_obj)
178+
od = LensCalc.from_mass_obj(mass_obj)
179179
einstein_radius = od.einstein_radius_from(grid=grid)
180180
except (TypeError, AttributeError):
181181
pass
@@ -217,13 +217,13 @@ def add_einstein_radius_errors(
217217
The mean value and errors of each attribute that are plotted in 1D by a `Plotter` object.
218218
"""
219219

220-
from autogalaxy.operate.deflections import OperateDeflections
220+
from autogalaxy.operate.lens_calc import LensCalc
221221

222222
einstein_radius_list = []
223223

224224
for mass_obj in mass_obj_list:
225225
try:
226-
od = OperateDeflections.from_mass_obj(mass_obj)
226+
od = LensCalc.from_mass_obj(mass_obj)
227227
einstein_radius_list.append(od.einstein_radius_from(grid=grid))
228228
except TypeError:
229229
einstein_radius_list.append(None)

autogalaxy/plot/visuals/two_d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ def add_critical_curves(self, mass_obj, grid: aa.type.Grid2DLike):
184184
vis.Visuals2D
185185
A collection of attributes that can be plotted by a `Plotter` object.
186186
"""
187-
from autogalaxy.operate.deflections import OperateDeflections
187+
from autogalaxy.operate.lens_calc import LensCalc
188188

189-
od = OperateDeflections.from_mass_obj(mass_obj)
189+
od = LensCalc.from_mass_obj(mass_obj)
190190

191191
tangential_critical_curves = od.tangential_critical_curve_list_from(grid=grid)
192192

@@ -227,9 +227,9 @@ def add_caustics(self, mass_obj, grid: aa.type.Grid2DLike):
227227
vis.Visuals2D
228228
A collection of attributes that can be plotted by a `Plotter` object.
229229
"""
230-
from autogalaxy.operate.deflections import OperateDeflections
230+
from autogalaxy.operate.lens_calc import LensCalc
231231

232-
od = OperateDeflections.from_mass_obj(mass_obj)
232+
od = LensCalc.from_mass_obj(mass_obj)
233233

234234
tangential_caustics = od.tangential_caustic_list_from(grid=grid)
235235
radial_caustics = od.radial_caustic_list_from(grid=grid)

autogalaxy/profiles/mass/abstract/abstract.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,6 @@ def __init__(
2727
def deflections_yx_2d_from(self, grid):
2828
raise NotImplementedError
2929

30-
def fermat_potential_from(self, grid, xp=np) -> aa.Array2D:
31-
"""
32-
Returns the Fermat potential for a given grid of image-plane positions.
33-
34-
This is the sum of the geometric time delay term and the gravitational (Shapiro) delay term (i.e. the lensing
35-
potential), and is given by:
36-
37-
.. math::
38-
\\phi(\\boldsymbol{\\theta}) = \\frac{1}{2} |\\boldsymbol{\\theta} - \\boldsymbol{\\beta}|^2
39-
- \\psi(\\boldsymbol{\\theta})
40-
41-
Parameters
42-
----------
43-
grid
44-
The 2D grid of (y,x) arc-second coordinates the Fermat potential is computed on.
45-
xp
46-
The array module (``numpy`` or ``jax.numpy``).
47-
"""
48-
from autogalaxy.operate.deflections import OperateDeflections
49-
50-
od = OperateDeflections.from_mass_obj(self)
51-
time_delay = od.time_delay_geometry_term_from(grid=grid, xp=xp)
52-
potential = self.potential_2d_from(grid=grid, xp=xp)
53-
fermat_potential = time_delay - potential
54-
if isinstance(grid, aa.Grid2DIrregular):
55-
return aa.ArrayIrregular(values=fermat_potential)
56-
return aa.Array2D(values=fermat_potential, mask=grid.mask)
57-
5830
def deflections_2d_via_potential_2d_from(self, grid):
5931
potential = self.potential_2d_from(grid=grid)
6032

docs/api/source.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Operators
3131
:recursive:
3232

3333
OperateImage
34-
OperateDeflections
34+
LensCalc
3535

3636
Total [ag.mp]
3737
-------------

0 commit comments

Comments
 (0)