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
5 changes: 3 additions & 2 deletions autolens/point/plot/point_solver_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ def display_pixel_scale_from(extent, points) -> float:
def display_solver_from(solver, extent, pixel_scale: float):
"""
Returns a solver of the same class as ``solver`` tiling ``extent`` (plus a pixel of margin) with triangles of
``pixel_scale``, with the solver's own ``pixel_scale_precision``, ``magnification_threshold`` and
``neighbor_degree``. It is used for the figure only.
``pixel_scale``, with the solver's own ``pixel_scale_precision``, ``magnification_threshold``,
``neighbor_degree`` and ``residual_threshold``. It is used for the figure only.
"""
x_min, x_max, y_min, y_max = extent
return type(solver).for_limits_and_scale(
Expand All @@ -366,6 +366,7 @@ def display_solver_from(solver, extent, pixel_scale: float):
pixel_scale_precision=solver.pixel_scale_precision,
magnification_threshold=solver.magnification_threshold,
neighbor_degree=getattr(solver, "neighbor_degree", 1),
residual_threshold=getattr(solver, "residual_threshold", 10.0),
)


Expand Down
31 changes: 26 additions & 5 deletions autolens/point/solver/point_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ def solve(
# `_solve_array`, because there are two distinct routes to an empty answer:
#
# 1. no triangle contained the coordinate -> `filtered_means` is already length 0
# 2. every candidate failed the magnification threshold -> `_filter_low_magnification`
# preserves the length and writes NaN rows, which become `inf` and are then stripped
# by `remove_infinities`, landing here at length 0
# 2. every candidate failed the magnification threshold (or the residual
# threshold) -> the filters preserve the length and write NaN rows, which become
# `inf` and are then stripped by `remove_infinities`, landing here at length 0
#
# Only reachable on the NumPy path: the JAX path keeps its padded static shape, so
# `len(solution)` is non-zero there and this reads no traced value.
Expand All @@ -212,7 +212,13 @@ def solve(
f"{tuple(source_plane_coordinate)}, so an empty grid is returned. This means "
f"either that the coordinate lies outside the region traced by the image-plane "
f"grid, or that every candidate image was rejected by "
f"`magnification_threshold` (currently {self.magnification_threshold})."
f"`magnification_threshold` (currently {self.magnification_threshold})"
+ (
""
if self.residual_threshold is None
else f" or `residual_threshold` (currently {self.residual_threshold})"
)
+ "."
)

return aa.Grid2DIrregular(solution)
Expand All @@ -226,7 +232,8 @@ def _solve_array(
remove_infinities: bool = False,
):
"""
The raw solve: triangle refinement, magnification filtering and sentinel padding,
The raw solve: triangle refinement, magnification (and, unless ``residual_threshold``
is ``None``, source-residual) filtering and sentinel padding,
returning the bare ``xp`` positions array (no ``Grid2DIrregular`` wrap, no logging).

On the JAX path this is the primal of the ``custom_jvp`` wrapper built by
Expand All @@ -248,6 +255,20 @@ def _solve_array(
plane_redshift=plane_redshift,
)

# On by default (``None`` disables): drop candidates that do not satisfy the lens
# equation, e.g. on a zero-core mass profile's singular centre (PyAutoLens#799). A
# mask, like the magnification filter, so the shape stays static; the length-0 NumPy
# case has nothing to trace.
if self.residual_threshold is not None and len(filtered_means) > 0:

filtered_means = self._filter_large_residual(
tracer=tracer,
points=filtered_means,
source_plane_coordinate=source_plane_coordinate,
xp=xp,
plane_redshift=plane_redshift,
)

# When no triangle traces to the source-plane coordinate -- e.g. the coordinate lies
# outside the region the image-plane grid tiles -- `filtered_means` comes back with shape
# (0, 2). The `[pair for pair in ...]` comprehension below then yields `[]`, and the
Expand Down
87 changes: 87 additions & 0 deletions autolens/point/solver/shape_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
magnification_threshold=0.1,
neighbor_degree: int = 1,
use_jax: bool = False,
residual_threshold: Optional[float] = 10.0,
):
"""
Determine the image plane coordinates that are traced to be a source plane coordinate.
Expand Down Expand Up @@ -75,6 +76,32 @@ def __init__(
galaxy / profile classes it carries as JAX pytrees on the first call. The
user wraps the call in their own ``@jax.jit`` — see the ``lens_calc.py``
workspace guide for the canonical pattern.
residual_threshold
``PointSolver`` rejects every candidate image whose source-plane residual
``|beta(theta) - beta|`` exceeds ``residual_threshold * pixel_scale_precision``.
The default is 10.0; ``None`` disables the filter.

This removes candidates on the centre of a zero-core mass profile (e.g. a
``dPIEMassSph`` cluster member with ``r_core=0``, PyAutoLens#799). The deflection
is discontinuous there, so a triangle around the centre traces to a triangle
inscribed in a circle of the member's Einstein radius. That triangle can contain
the source at every refinement step with no root inside it. The candidate then
lies within one final triangle of the centre, with a residual of order the
Einstein radius (~0.04" on cluster members). The magnification filter can pass
it, because in a near-critical host ``|mu|`` there is ~0.1-0.3.

A true image traces to within its final triangle, so its residual is of order
the final triangle edge times the largest singular value of the lens-equation
Jacobian, which near a cusp is small. On the cluster and near-cusp fixtures of
PyAutoLens#799, true images reach 3.6 times ``pixel_scale_precision`` (1.1 times
near a cusp) and the core candidates at least 38 times, so 10 is the default: it
keeps every true image with margin and removes every core candidate. The
linearised correction ``|A^-1 r|`` is deliberately not used: ``A`` is huge near a
singular centre, so it would accept the core candidate.

The cut scales with ``pixel_scale_precision``. At a coarse precision (e.g. 0.01,
a cut of 0.1") it is looser than the ~0.04" core residuals, so core candidates
can survive; lower ``residual_threshold`` or refine the precision if they matter.
"""
self.y_min = y_min
self.y_max = y_max
Expand All @@ -85,6 +112,7 @@ def __init__(
self.magnification_threshold = magnification_threshold
self.neighbor_degree = neighbor_degree
self.use_jax = use_jax
self.residual_threshold = residual_threshold

# The grid ``for_grid`` was built from, when there was one. It is the image-plane
# pixelization ``image_regions_from`` reports its regions on; a solver built via
Expand Down Expand Up @@ -159,6 +187,7 @@ def for_grid(
magnification_threshold=0.1,
neighbor_degree: int = 1,
use_jax: bool = False,
residual_threshold: Optional[float] = 10.0,
):
"""
Create a solver for a given grid.
Expand All @@ -177,6 +206,8 @@ def for_grid(
The number of times recursively add neighbors for the triangles that contain
use_jax
Forwarded to the constructor; see ``__init__``.
residual_threshold
Forwarded to the constructor; see ``__init__``.

Returns
-------
Expand All @@ -197,6 +228,7 @@ def for_grid(
magnification_threshold=magnification_threshold,
neighbor_degree=neighbor_degree,
use_jax=use_jax,
residual_threshold=residual_threshold,
)

# Remembered so ``ShapeSolver.image_regions_from`` can report image-plane regions
Expand All @@ -218,6 +250,7 @@ def for_limits_and_scale(
magnification_threshold=0.1,
neighbor_degree: int = 1,
use_jax: bool = False,
residual_threshold: Optional[float] = 10.0,
):
"""
Create a solver for an explicit image-plane extent.
Expand All @@ -236,6 +269,8 @@ def for_limits_and_scale(
The number of times recursively add neighbors for the triangles that contain
use_jax
Forwarded to the constructor; see ``__init__``.
residual_threshold
Forwarded to the constructor; see ``__init__``.

Returns
-------
Expand All @@ -251,6 +286,7 @@ def for_limits_and_scale(
magnification_threshold=magnification_threshold,
neighbor_degree=neighbor_degree,
use_jax=use_jax,
residual_threshold=residual_threshold,
)

@property
Expand Down Expand Up @@ -447,6 +483,54 @@ def _filter_low_magnification(
mask = xp.abs(magnifications) > self.magnification_threshold
return xp.where(mask[:, None], points, xp.nan)

def _filter_large_residual(
self,
tracer: Tracer,
points,
source_plane_coordinate: Tuple[float, float],
xp,
plane_redshift: Optional[float] = None,
):
"""
Reject the points whose source-plane residual ``|beta(theta) - beta|`` exceeds
``residual_threshold * pixel_scale_precision`` (see ``__init__``).

The points are traced with ``_plane_grid``, the callable the triangle search uses,
so the residual is measured at the plane being solved for. Rejected points become
``nan`` rows, as in ``_filter_low_magnification``, so the array keeps its static
shape; rows that are already ``nan`` stay ``nan``.

Parameters
----------
points
The ``(n, 2)`` candidate image-plane positions.
source_plane_coordinate
The plane coordinate being solved for.
xp
The array module used for the trace.
plane_redshift
The redshift of the plane being solved for. ``None`` is the last plane.

Returns
-------
The points with a source-plane residual no larger than the threshold.
"""
traced = self._plane_grid(
tracer=tracer,
grid=aa.Grid2DIrregular(points),
xp=xp,
plane_redshift=plane_redshift,
).array
beta = xp.stack(
[
xp.asarray(source_plane_coordinate[0]),
xp.asarray(source_plane_coordinate[1]),
]
)
residuals = xp.sqrt(xp.sum((traced - beta[None, :]) ** 2, axis=1))
reject = residuals > self.residual_threshold * self.pixel_scale_precision
return xp.where(reject[:, None], xp.nan, points)

def _plane_triangles(
self,
tracer: Tracer,
Expand Down Expand Up @@ -547,6 +631,7 @@ def tree_flatten(self):
self.magnification_threshold,
self.neighbor_degree,
self.use_jax,
self.residual_threshold,
)

@classmethod
Expand All @@ -561,6 +646,7 @@ def tree_unflatten(cls, aux_data, children):
magnification_threshold,
neighbor_degree,
use_jax,
residual_threshold,
) = aux_data
return cls(
y_min=y_min,
Expand All @@ -572,6 +658,7 @@ def tree_unflatten(cls, aux_data, children):
magnification_threshold=magnification_threshold,
neighbor_degree=neighbor_degree,
use_jax=use_jax,
residual_threshold=residual_threshold,
)


Expand Down
Loading
Loading