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
409 changes: 239 additions & 170 deletions autolens/cluster/plot/cluster_plots.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion autolens/config/visualize/plots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fit_point_dataset: # Settings for plots of fits to point
fit_image_with_points: true # Plot the observed image (if passed to AnalysisPoint) with observed and model positions and critical curves over it (fit_image_with_points.png)?
subplot_point_solver: false # Plot how the PointSolver refines its triangles onto the best-fit model's images (point_solver.png)? Re-solves with NumPy at the best fit only, so it is off by default.
subplot_fit_combined: true # Multi-dataset (e.g. cluster) fits: plot every source's fit in one panel, each source's critical curves at its own redshift, in the top-level image folder (fit.png)?
fit_per_image_combined: true # Multi-dataset (e.g. cluster) fits: plot fit_per_image.png for every source, the ranking range shared across sources (one file per source in fit_per_image/ beyond 8 sources)?
fit_per_image_combined: true # Write <family>/fit.png with per-image contributions and fitted-plane zooms (six zooms per page).
family_fit_single_panel: false # Show all fitted-plane positions on shared angular axes instead of individual zooms.
fit_image_with_points_combined: true # Multi-dataset (e.g. cluster) fits: plot the observed image with every source's observed and model positions and critical curves over it (fit_image_with_points.png)?

weak_dataset: # Settings for plots of weak lensing shear catalogues (e.g. PlotterWeak).
Expand Down
5 changes: 5 additions & 0 deletions autolens/point/fit/positions/image/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,16 @@ def square_distance(
"""
return (coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2

# Only a visualizer's private copy sets this. A live fit always calls its solver.
_plot_model_data = None

@property
def model_data(self) -> aa.Grid2DIrregular:
"""
Returns the model positions, which are computed via the point solver.
"""
if self._plot_model_data is not None:
return self._plot_model_data
return self.solver.solve(
tracer=self.tracer,
source_plane_coordinate=self.source_plane_coordinate,
Expand Down
55 changes: 44 additions & 11 deletions autolens/point/model/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _setting(section, name: str, default: bool = True) -> bool:


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

Expand All @@ -49,6 +49,7 @@ def should_plot(name):
output_format=fmt,
title_prefix=self.title_prefix,
solver=solver,
family_index=family_index,
)

def image_with_points(
Expand All @@ -59,6 +60,7 @@ def image_with_points(
image_plane_lines=None,
image_plane_line_colors=None,
curves=None,
family_index=None,
):
"""
Output an observed image of the lensed point source with the point-source positions overlaid.
Expand Down Expand Up @@ -102,6 +104,7 @@ def image_with_points(
image_plane_line_colors=image_plane_line_colors,
title_prefix=self.title_prefix,
curves=curves,
family_index=family_index,
)

def fit_point(
Expand All @@ -113,6 +116,7 @@ def fit_point(
source_plane_lines=None,
source_plane_line_colors=None,
curves=None,
family_index=None,
):
"""
Visualizes a `FitPointDataset` object: ``fit.png`` (controlled by ``fit: subplot_fit``, also output on a
Expand Down Expand Up @@ -147,30 +151,49 @@ def fit_point(
source_plane_line_colors=source_plane_line_colors,
title_prefix=self.title_prefix,
curves=curves,
family_index=family_index,
)

def fit_per_image(self, fit_list, curves=None, solver=None, combined: bool = False):
def fit_per_image(
self,
fit_list,
curves=None,
solver=None,
combined: bool = False,
image=None,
family_index=None,
):
"""
Output ``fit_per_image.png``: one row per source of its residual ranking, image-plane zoom and source-plane
panel (beyond 8 sources, one file per source in ``fit_per_image/``).

Controlled by ``fit_point_dataset: fit_per_image`` for a single dataset and
``fit_point_dataset: fit_per_image_combined`` for the all-sources figure of a multi-dataset (e.g. cluster)
fit, whose ranking range is shared by every source.
Output a cluster diagnostic per family for a combined fit, or the single dataset's
residual ranking, image-plane zoom and source-plane panel in ``fit_per_image.png``.
"""
name = "fit_per_image_combined" if combined else "fit_per_image"
if not _setting(["fit", "fit_point_dataset"], name):
return
if combined:
from autolens.point.plot.family_fit_plots import plot_family_fits

return plot_family_fits(
fit_list,
image=image,
single_panel=_setting(
["fit", "fit_point_dataset"], "family_fit_single_panel", default=False
),
output_path=str(self.image_path),
output_format=self.fmt,
title_prefix=self.title_prefix,
)
multi_source_plots.subplot_fit_per_image(
fit_list,
curves=curves,
solver=solver,
output_path=str(self.image_path),
output_format=self.fmt,
title_prefix=self.title_prefix,
family_index=family_index,
)

def dataset_point_combined(self, dataset_list, solver=None):
def dataset_point_combined(self, dataset_list, solver=None, image_extent=None):
"""
Output ``dataset_point.png`` of every point dataset of a multi-dataset (e.g. cluster) fit in one panel,
controlled by ``point_dataset: subplot_dataset_combined``.
Expand All @@ -180,12 +203,20 @@ def dataset_point_combined(self, dataset_list, solver=None):
multi_source_plots.plot_dataset_positions(
dataset_list,
solver=solver,
image_extent=image_extent,
output_path=str(self.image_path),
output_format=self.fmt,
title_prefix=self.title_prefix,
)

def fit_point_combined(self, fit_list, curves=None, solver=None, quick_update: bool = False):
def fit_point_combined(
self,
fit_list,
curves=None,
solver=None,
quick_update: bool = False,
image_extent=None,
):
"""
Output ``fit.png`` of every point dataset of a multi-dataset (e.g. cluster) fit in one panel ("Fit
Positions", each source's curves at its own redshift), controlled by
Expand All @@ -197,6 +228,7 @@ def fit_point_combined(self, fit_list, curves=None, solver=None, quick_update: b
return
multi_source_plots.plot_fit_positions(
fit_list,
image_extent=image_extent,
solver=solver,
curves=curves,
output_path=str(self.image_path),
Expand Down Expand Up @@ -227,7 +259,7 @@ def image_with_points_combined(self, image, dataset_list, fit_list=None, curves=
title_prefix=self.title_prefix,
)

def point_solver(self, fit: FitPointDataset, image_plane_lines=None):
def point_solver(self, fit: FitPointDataset, image_plane_lines=None, family_index=None):
"""
Output `point_solver.png`: how the `PointSolver` refines its triangles onto the images of the fit's model.

Expand Down Expand Up @@ -275,4 +307,5 @@ def point_solver(self, fit: FitPointDataset, image_plane_lines=None):
output_path=str(self.image_path),
output_format=self.fmt,
title_prefix=self.title_prefix,
family_index=family_index,
)
64 changes: 52 additions & 12 deletions autolens/point/model/visualizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from copy import copy

import autofit as af

from autolens.point.model.plotter import PlotterPoint
from autolens.point.fit.positions.image.abstract import AbstractFitPositionsImagePair
from autolens.lens.plot.critical_curves import critical_curve_grid_from
from autolens.point.plot import point_sources as ps

Expand All @@ -11,6 +14,7 @@ def visualize_before_fit(
analysis,
paths: af.AbstractPaths,
model: af.AbstractPriorModel,
family_index=None,
):
"""
PyAutoFit calls this function immediately before the non-linear search begins.
Expand All @@ -25,19 +29,27 @@ def visualize_before_fit(
model
The model object, which includes model components representing the galaxies that are fitted to
the imaging data.
family_index
The zero-based family index when this analysis is being visualized as part of a cluster.
"""

plotter = PlotterPoint(
image_path=paths.image_path, title_prefix=analysis.title_prefix
)

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

if getattr(analysis, "image", None) is not None:
# Dataset-only figure (observed positions, no model): `image_with_points.png`.
plotter.image_with_points(image=analysis.image, dataset=analysis.dataset)
plotter.image_with_points(
image=analysis.image,
dataset=analysis.dataset,
family_index=family_index,
)

@staticmethod
def visualize(
Expand All @@ -46,6 +58,7 @@ def visualize(
instance: af.ModelInstance,
during_analysis: bool,
quick_update: bool = False,
family_index=None,
):
"""
Output images of the maximum log likelihood model inferred by the model-fit. This function is called throughout
Expand All @@ -69,6 +82,8 @@ def visualize(
instance
An instance of the model that is being fitted to the data by this analysis (whose parameters have been set
via a non-linear search).
family_index
The zero-based family index when this analysis is being visualized as part of a cluster.
"""
fit = analysis.fit_for_visualization(instance=instance)

Expand All @@ -87,7 +102,9 @@ def visualize(
curves = curves_for_point_fits(fits=[fit], solver=solver)
ip_lines, ip_colors, sp_lines, sp_colors = last_plane_lines_from(tracer, curves)

plotter.fit_point(fit=fit, quick_update=quick_update, curves=curves)
plotter.fit_point(
fit=fit, quick_update=quick_update, curves=curves, family_index=family_index
)

# Output even on `quick_update`, so the observed image with model positions updates on-the-fly. Saved as
# `fit_image_with_points.png`, so the dataset-only `image_with_points.png` from before the fit is kept.
Expand All @@ -99,12 +116,15 @@ def visualize(
image_plane_lines=ip_lines,
image_plane_line_colors=ip_colors,
curves=curves,
family_index=family_index,
)

if quick_update:
return

plotter.fit_per_image(fit_list=[fit], curves=curves, solver=solver)
plotter.fit_per_image(
fit_list=[fit], curves=curves, solver=solver, family_index=family_index
)

plotter.tracer(
tracer=tracer,
Expand All @@ -122,7 +142,7 @@ def visualize(
# Off by default (`fit_point_dataset: subplot_point_solver`): re-solves the best-fit model with NumPy to show
# the solver's refinement steps, so it is output only for the full (not quick-update) visualization. The
# figure computes the critical curves of the source's own plane on its own (per-source) extent.
plotter.point_solver(fit=fit)
plotter.point_solver(fit=fit, family_index=family_index)

@staticmethod
def visualize_before_fit_combined(
Expand Down Expand Up @@ -160,8 +180,8 @@ def visualize_combined(

- ``fit.png``: every source's observed and model positions and its critical curves at its own redshift
(also output on a quick update);
- ``fit_per_image.png``: one row per source, with the residual ranking range shared by every source
(``fit_per_image/<name>.png`` beyond 8 sources);
- ``<family>/fit.png``: the cluster overview, per-image contributions, fitted-plane details and the
complete source-plane view. Additional groups of six zooms use ``fit_page_02.png``, etc.;
- ``fit_image_with_points.png`` if an observed image was passed to an ``AnalysisPoint``;
- the dataset figures of ``visualize_before_fit_combined`` (``dataset_point.png``,
``image_with_points.png``), as a factor-graph fit does not call that function.
Expand All @@ -182,19 +202,23 @@ def visualize_combined(
)

fit_list = [
analysis.fit_for_visualization(instance=single_instance)
_combined_plot_fit(analysis.fit_for_visualization(instance=single_instance))
for analysis, single_instance in pairs
]
solver = getattr(analyses[0], "solver", None) or getattr(
fit_list[0], "solver", None
)
curves = curves_for_point_fits(fits=fit_list, solver=solver)

image = _image_from(analyses)
plotter.fit_point_combined(
fit_list=fit_list, curves=curves, solver=solver, quick_update=quick_update
fit_list=fit_list,
curves=curves,
solver=solver,
quick_update=quick_update,
image_extent=image.geometry.extent if image is not None else None,
)

image = _image_from(analyses)
if image is not None:
plotter.image_with_points_combined(
image=image,
Expand All @@ -208,11 +232,24 @@ def visualize_combined(
return

plotter.fit_per_image(
fit_list=fit_list, curves=curves, solver=solver, combined=True
fit_list=fit_list, curves=curves, solver=solver, combined=True, image=image
)
_visualize_datasets_combined(plotter, analyses)


def _combined_plot_fit(fit):
"""Share a fixed image-plane solution across all panels of this visualization call."""
positions = fit.positions
if not isinstance(positions, AbstractFitPositionsImagePair):
return fit
if type(positions).model_data is not AbstractFitPositionsImagePair.model_data:
return fit
result = copy(fit)
result.positions = copy(positions)
result.positions._plot_model_data = positions.model_data
return result


def _is_point_analysis(analysis) -> bool:
from autolens.point.model.analysis import AnalysisPoint

Expand All @@ -236,8 +273,11 @@ def _image_from(analyses):
def _visualize_datasets_combined(plotter, analyses):
solver = getattr(analyses[0], "solver", None)
dataset_list = [analysis.dataset for analysis in analyses]
plotter.dataset_point_combined(dataset_list=dataset_list, solver=solver)
image = _image_from(analyses)
plotter.dataset_point_combined(
dataset_list=dataset_list, solver=solver,
image_extent=image.geometry.extent if image is not None else None,
)
if image is not None:
plotter.image_with_points_combined(
image=image, dataset_list=dataset_list, solver=solver
Expand Down
Loading
Loading