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
35 changes: 34 additions & 1 deletion autolens/lens/plot/critical_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def _compute_critical_curve_lines(tracer, grid):


def critical_curve_grid_from(
dataset, shape_native=(100, 100), scale: float = 2.0, buffer: float = 0.1
dataset,
shape_native=(100, 100),
scale: float = 2.0,
buffer: float = 0.1,
solver=None,
) -> "ag.Grid2D":
"""
Returns the grid on which the critical curves and caustics of a dataset's lens model are computed.
Expand All @@ -82,6 +86,11 @@ def critical_curve_grid_from(
``dataset.extent_from`` returns ``[y_min, y_max, x_min, x_max]`` whereas ``Grid2D.from_extent`` takes the
extent x first, which this function handles.

If a ``solver`` (e.g. a ``PointSolver``) with rectangular bounds is passed, the square is enlarged where needed
so it also covers the solver grid. The point-source positions panels are drawn over the solver grid's extent,
so a critical curve computed on a smaller grid would otherwise stop inside the panel as disconnected arcs; on
the enlarged grid it is only ever cut by the panel frame itself.

Parameters
----------
dataset
Expand All @@ -93,12 +102,36 @@ def critical_curve_grid_from(
The factor by which the grid's half-width exceeds the half-span of the positions.
buffer
A buffer (arc-seconds) added to the half-span of the positions.
solver
An optional solver whose ``y_min`` / ``y_max`` / ``x_min`` / ``x_max`` bounds the grid is enlarged to cover.
"""
y_min, y_max, x_min, x_max = dataset.extent_from(buffer=0.0)

y_centre, x_centre = (y_min + y_max) / 2.0, (x_min + x_max) / 2.0
half_width = scale * (max(y_max - y_min, x_max - x_min) / 2.0 + buffer)

if solver is not None:
try:
solver_bounds = [
float(solver.y_min),
float(solver.y_max),
float(solver.x_min),
float(solver.x_max),
]
except (AttributeError, TypeError, ValueError):
solver_bounds = None
if solver_bounds is not None and all(
abs(bound) < float("inf") for bound in solver_bounds
):
s_y_min, s_y_max, s_x_min, s_x_max = solver_bounds
half_width = max(
half_width,
abs(s_y_min - y_centre),
abs(s_y_max - y_centre),
abs(s_x_min - x_centre),
abs(s_x_max - x_centre),
)

# `Grid2D.from_extent` takes the extent x first: (x0, x1, y0, y1).
return ag.Grid2D.from_extent(
extent=(
Expand Down
23 changes: 19 additions & 4 deletions autolens/point/model/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@


class PlotterPoint(Plotter):
def dataset_point(self, dataset: PointDataset):
def dataset_point(self, dataset: PointDataset, solver=None):
"""
Output visualization of a `PointDataset` dataset.

Parameters
----------
dataset
The point dataset which is visualized.
solver
The `PointSolver` of the model-fit, whose grid sets the extent of the positions panel. If `None`, the
panel extent falls back to the critical-curve region around the positions.
"""

def should_plot(name):
Expand All @@ -30,8 +33,13 @@ def should_plot(name):
fmt = self.fmt

if should_plot("subplot_dataset"):
subplot_dataset(dataset, output_path=output_path, output_format=fmt,
title_prefix=self.title_prefix)
subplot_dataset(
dataset,
output_path=output_path,
output_format=fmt,
title_prefix=self.title_prefix,
solver=solver,
)

def image_with_points(
self,
Expand Down Expand Up @@ -115,7 +123,9 @@ def should_plot(name):

# Use pre-computed critical curves if provided, otherwise compute once here.
if image_plane_lines is None and source_plane_lines is None:
grid = critical_curve_grid_from(dataset=fit.dataset)
grid = critical_curve_grid_from(
dataset=fit.dataset, solver=getattr(fit, "solver", None)
)
ip_lines, ip_colors, sp_lines, sp_colors = _compute_critical_curve_lines(
fit.tracer, grid
)
Expand All @@ -125,6 +135,11 @@ def should_plot(name):
source_plane_lines, source_plane_line_colors,
)

# `subplot_fit` computes the curves itself when it is given none, so pass an explicit empty list when the
# model has no critical curves, rather than computing them twice.
if ip_lines is None:
ip_lines, ip_colors = [], []

if quick_update:
subplot_fit_point(
fit, output_path=output_path, output_format=fmt,
Expand Down
12 changes: 9 additions & 3 deletions autolens/point/model/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def visualize_before_fit(
image_path=paths.image_path, title_prefix=analysis.title_prefix
)

plotter.dataset_point(dataset=analysis.dataset)
plotter.dataset_point(
dataset=analysis.dataset, solver=getattr(analysis, "solver", None)
)

if getattr(analysis, "image", None) is not None:
# Dataset-only figure (observed positions, no model): `image_with_points.png`.
Expand Down Expand Up @@ -78,9 +80,13 @@ def visualize(

grid = critical_curve_grid_from(dataset=fit.dataset)

# Compute critical curves and caustics once for all plot functions.
# Compute critical curves and caustics once for all plot functions, on a grid which also covers the
# solver grid, so the curves on the solver-extent positions panel are only ever cut by its frame.
ip_lines, ip_colors, sp_lines, sp_colors = _compute_critical_curve_lines(
tracer, grid
tracer,
critical_curve_grid_from(
dataset=fit.dataset, solver=getattr(fit, "solver", None)
),
)

plotter.fit_point(
Expand Down
99 changes: 75 additions & 24 deletions autolens/point/plot/fit_point_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@ def subplot_fit(
source_plane_lines=None,
source_plane_line_colors=None,
title_prefix: str = None,
solver=None,
):
"""
Produce a subplot summarising a `FitPointDataset`.

The subplot contains one or two panels depending on whether flux
measurements are present in the dataset:

* **Positions panel** (always shown): observed point-source positions
plotted as a grid, with the model-predicted positions overlaid as
red scatter points.
* **Positions panel** (always shown), titled ``Fit Positions (<name>)``.
Drawn on the same frame as the dataset positions panel (the
``PointSolver`` grid extent, no axis labels), it shows:

- the observed positions as large filled markers with their noise-map
circles and the sigma in the legend;
- the model positions on top as red crosses;
- a thin ``Residual`` line from each observed position to the model
position the fit's own pairing matches it to (Hungarian for
``FitPositionsImagePair``, nearest for ``FitPositionsImagePairRepeat``
and the dominant nearest term of ``FitPositionsImagePairAll``);
- model images no observed position is paired with as hollow grey
crosses (``Unmatched model image``);
- the tangential / radial critical curves, thin and light, clipped at the
panel edge where they extend beyond the solver grid;
- the centre of every mass profile in the tracer (``Mass centre``).

For a source-plane fit (e.g. ``FitPositionsSource``) the model data are
the observed positions ray-traced to the source plane: they are drawn as
``Model (source plane)`` crosses and no residual lines or unmatched images
are drawn, as there is no image-plane pairing.
* **Fluxes panel** (shown only when ``fit.dataset.fluxes`` is not
``None``): a bar/line plot of the observed flux values.

Expand All @@ -35,38 +54,70 @@ def subplot_fit(
not saved to disk.
output_format : str, optional
Image format passed to :func:`~autoarray.plot.utils.save_figure`.
image_plane_lines : list, optional
Critical-curve lines drawn on the positions panel. If this and
``source_plane_lines`` are both ``None`` they are computed from the
fit's tracer on ``critical_curve_grid_from`` (enlarged to cover the
solver grid); pass ``[]`` to draw none.
image_plane_line_colors : list, optional
Colours of each image-plane line (``"white"`` tangential, ``"yellow"``
radial, as returned by the critical-curve helper).
source_plane_lines, source_plane_line_colors
Accepted for a uniform plotter interface; not drawn.
title_prefix : str, optional
Text prepended to every panel title.
solver : PointSolver, optional
The solver whose grid sets the positions panel extent. Defaults to
``fit.solver``.
"""
from autogalaxy.util.plot_utils import plot_grid
from autoarray.plot.yx import plot_yx
from autolens.point.plot.positions_panel import (
finish_positions_panel,
plot_critical_curves,
plot_fit_model_positions,
plot_mass_centres,
plot_observed_positions,
positions_extent_from,
)

has_fluxes = fit.dataset.fluxes is not None
n = 2 if has_fluxes else 1

if solver is None:
solver = getattr(fit, "solver", None)

if image_plane_lines is None and source_plane_lines is None:
from autolens.lens.plot.critical_curves import (
_compute_critical_curve_lines,
critical_curve_grid_from,
)

image_plane_lines, image_plane_line_colors, _, _ = (
_compute_critical_curve_lines(
fit.tracer, critical_curve_grid_from(dataset=fit.dataset, solver=solver)
)
)

fig, axes = subplots(1, n, figsize=conf_subplot_figsize(1, n))
axes_flat = [axes] if n == 1 else list(np.array(axes).flatten())

_prefix = f"{title_prefix.rstrip()} " if title_prefix else ""

# Positions panel
obs_grid = np.array(
fit.dataset.positions.array
if hasattr(fit.dataset.positions, "array")
else fit.dataset.positions
)
model_grid = np.array(
fit.positions.model_data.array
if hasattr(fit.positions.model_data, "array")
else fit.positions.model_data
ax = axes_flat[0]
plot_critical_curves(ax, image_plane_lines, image_plane_line_colors)
plot_mass_centres(ax, fit.tracer)
plot_observed_positions(
ax=ax,
positions=fit.dataset.positions,
noise_map=fit.dataset.positions_noise_map,
)

_prefix = f"{title_prefix.rstrip()} " if title_prefix else ""
plot_grid(
grid=obs_grid,
ax=axes_flat[0],
title=f"{_prefix}{fit.dataset.name} Fit Positions",
output_path=None,
output_filename=None,
output_format=output_format,
plot_fit_model_positions(ax, fit.positions)
finish_positions_panel(
ax=ax,
title=f"{_prefix}Fit Positions ({fit.dataset.name})",
extent=positions_extent_from(positions=fit.dataset.positions, solver=solver),
)
axes_flat[0].scatter(model_grid[:, 1], model_grid[:, 0], c="r", s=20, zorder=5)

# Fluxes panel
if has_fluxes and n > 1:
Expand All @@ -76,7 +127,7 @@ def subplot_fit(
y=y,
x=x,
ax=axes_flat[1],
title=f"{_prefix}{fit.dataset.name} Fit Fluxes",
title=f"{_prefix}Fit Fluxes ({fit.dataset.name})",
output_path=None,
output_filename="fit_point_fluxes",
output_format=output_format,
Expand Down
44 changes: 28 additions & 16 deletions autolens/point/plot/point_dataset_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ def subplot_dataset(
output_path: Optional[str] = None,
output_format: str = None,
title_prefix: str = None,
solver=None,
):
"""
Produce a subplot visualising a `PointDataset`.

The subplot contains one or two panels depending on whether flux
measurements are present:

* **Positions panel** (always shown): the observed point-source
positions rendered as a grid scatter plot.
* **Positions panel** (always shown), titled ``Dataset Positions (<name>)``:
the observed point-source positions as large filled markers, each with a
circle at its noise-map radius (never inflated, so it may be sub-pixel)
and the sigma stated in the legend, e.g. ``Observed (σ = 0.005″)``. The
panel extent is the ``PointSolver`` grid extent when ``solver`` is given
(where the model can find images), otherwise the critical-curve region of
``critical_curve_grid_from``, so no position is clipped. There are no axis
labels, only arc-second tick labels.
* **Fluxes panel** (shown only when ``dataset.fluxes`` is not
``None``): a line/bar plot of the observed flux values indexed by
image position.
Expand All @@ -31,30 +38,35 @@ def subplot_dataset(
not saved to disk.
output_format : str, optional
Image format passed to :func:`~autoarray.plot.utils.save_figure`.
title_prefix : str, optional
Text prepended to every panel title.
solver : PointSolver, optional
The solver of the model-fit, whose grid sets the positions panel extent.
"""
from autogalaxy.util.plot_utils import plot_grid
from autoarray.plot.yx import plot_yx
from autolens.point.plot.positions_panel import (
finish_positions_panel,
plot_observed_positions,
positions_extent_from,
)

has_fluxes = dataset.fluxes is not None
n = 2 if has_fluxes else 1

fig, axes = subplots(1, n, figsize=conf_subplot_figsize(1, n))
axes_flat = [axes] if n == 1 else list(np.array(axes).flatten())

grid = np.array(
dataset.positions.array
if hasattr(dataset.positions, "array")
else dataset.positions
)

_prefix = f"{title_prefix.rstrip()} " if title_prefix else ""
plot_grid(
grid=grid,

plot_observed_positions(
ax=axes_flat[0],
positions=dataset.positions,
noise_map=dataset.positions_noise_map,
)
finish_positions_panel(
ax=axes_flat[0],
title=f"{_prefix}{dataset.name} Positions",
output_path=None,
output_filename=None,
output_format=output_format,
title=f"{_prefix}Dataset Positions ({dataset.name})",
extent=positions_extent_from(positions=dataset.positions, solver=solver),
)

if has_fluxes and n > 1:
Expand All @@ -64,7 +76,7 @@ def subplot_dataset(
y=y,
x=x,
ax=axes_flat[1],
title=f"{_prefix}{dataset.name} Fluxes",
title=f"{_prefix}Dataset Fluxes ({dataset.name})",
output_path=None,
output_filename="point_dataset_fluxes",
output_format=output_format,
Expand Down
Loading
Loading