Skip to content

Commit 68ca1d7

Browse files
committed
Remove _plot_with_* helpers; simplify plot_array/plot_grid via _prepare_array
- Delete _plot_with_vmax, _plot_with_vmin, _plot_with_vmin_vmax, _plot_symmetric from fit_imaging_plots.py — callers now pass vmin/vmax directly to plot_array - Add _symmetric_vmax() helper in fit_imaging_plots.py to compute abs-max for symmetric colormap scaling - Add _prepare_array() to plot_utils.py: handles zoom, Array2D→numpy extraction, and mask derivation in one place - Rewrite plot_array and plot_grid in plot_utils.py as thin adapters that use _prepare_array then delegate entirely to autoarray's low-level functions https://claude.ai/code/session_01CzJBy8KvFXiNchoNdk5i9k
1 parent c2f7cfb commit 68ca1d7

2 files changed

Lines changed: 132 additions & 151 deletions

File tree

autolens/imaging/plot/fit_imaging_plots.py

Lines changed: 43 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
_save_subplot,
1212
_critical_curves_from,
1313
_caustics_from,
14+
_zoom_array,
1415
)
1516

1617

@@ -74,14 +75,8 @@ def subplot_fit(
7475
plot_array(array=fit.data, ax=axes_flat[0], title="Data", colormap=colormap)
7576

7677
# Data at source scale
77-
if source_vmax is not None:
78-
from autoarray.structures.plot.structure_plotters import _zoom_array
79-
import matplotlib as mpl
80-
_ax = axes_flat[1]
81-
_plot_with_vmax(fit.data, _ax, "Data (Source Scale)", colormap, vmax=source_vmax)
82-
else:
83-
plot_array(array=fit.data, ax=axes_flat[1], title="Data (Source Scale)",
84-
colormap=colormap)
78+
plot_array(array=fit.data, ax=axes_flat[1], title="Data (Source Scale)",
79+
colormap=colormap, vmax=source_vmax)
8580

8681
plot_array(array=fit.signal_to_noise_map, ax=axes_flat[2],
8782
title="Signal-To-Noise Map", colormap=colormap)
@@ -105,12 +100,9 @@ def subplot_fit(
105100
except (IndexError, AttributeError):
106101
subtracted_img = None
107102
if subtracted_img is not None:
108-
if source_vmax is not None:
109-
_plot_with_vmin_vmax(subtracted_img, axes_flat[5],
110-
"Lens Light Subtracted", colormap, vmin=0.0, vmax=source_vmax)
111-
else:
112-
plot_array(array=subtracted_img, ax=axes_flat[5],
113-
title="Lens Light Subtracted", colormap=colormap)
103+
plot_array(array=subtracted_img, ax=axes_flat[5], title="Lens Light Subtracted",
104+
colormap=colormap, vmin=0.0 if source_vmax is not None else None,
105+
vmax=source_vmax)
114106
else:
115107
axes_flat[5].axis("off")
116108

@@ -120,12 +112,8 @@ def subplot_fit(
120112
except (IndexError, AttributeError):
121113
source_model_img = None
122114
if source_model_img is not None:
123-
if source_vmax is not None:
124-
_plot_with_vmax(source_model_img, axes_flat[6], "Source Model Image",
125-
colormap, vmax=source_vmax)
126-
else:
127-
plot_array(array=source_model_img, ax=axes_flat[6],
128-
title="Source Model Image", colormap=colormap)
115+
plot_array(array=source_model_img, ax=axes_flat[6], title="Source Model Image",
116+
colormap=colormap, vmax=source_vmax)
129117
else:
130118
axes_flat[6].axis("off")
131119

@@ -135,12 +123,14 @@ def subplot_fit(
135123

136124
# Normalized residual map (symmetric)
137125
norm_resid = fit.normalized_residual_map
138-
_plot_symmetric(norm_resid, axes_flat[8], "Normalized Residual Map", colormap)
126+
_abs_max = _symmetric_vmax(norm_resid)
127+
plot_array(array=norm_resid, ax=axes_flat[8], title="Normalized Residual Map",
128+
colormap=colormap, vmin=-_abs_max, vmax=_abs_max)
139129

140130
# Normalized residual map clipped to [-1, 1]
141-
_plot_with_vmin_vmax(norm_resid, axes_flat[9],
142-
r"Normalized Residual Map $1\sigma$", colormap,
143-
vmin=-1.0, vmax=1.0)
131+
plot_array(array=norm_resid, ax=axes_flat[9],
132+
title=r"Normalized Residual Map $1\sigma$",
133+
colormap=colormap, vmin=-1.0, vmax=1.0)
144134

145135
plot_array(array=fit.chi_squared_map, ax=axes_flat[10],
146136
title="Chi-Squared Map", colormap=colormap)
@@ -168,28 +158,24 @@ def subplot_fit_x1_plane(
168158
except (IndexError, AttributeError, ValueError):
169159
vmax = None
170160

171-
if vmax is not None:
172-
_plot_with_vmax(fit.data, axes_flat[0], "Data", colormap, vmax=vmax)
173-
else:
174-
plot_array(array=fit.data, ax=axes_flat[0], title="Data", colormap=colormap)
161+
plot_array(array=fit.data, ax=axes_flat[0], title="Data", colormap=colormap, vmax=vmax)
175162

176163
plot_array(array=fit.signal_to_noise_map, ax=axes_flat[1],
177164
title="Signal-To-Noise Map", colormap=colormap)
178165

179-
if vmax is not None:
180-
_plot_with_vmax(fit.model_data, axes_flat[2], "Model Image", colormap, vmax=vmax)
181-
else:
182-
plot_array(array=fit.model_data, ax=axes_flat[2], title="Model Image",
183-
colormap=colormap)
166+
plot_array(array=fit.model_data, ax=axes_flat[2], title="Model Image",
167+
colormap=colormap, vmax=vmax)
184168

185169
norm_resid = fit.normalized_residual_map
186170
plot_array(array=norm_resid, ax=axes_flat[3], title="Lens Light Subtracted",
187171
colormap=colormap)
188172

189-
_plot_with_vmin(norm_resid, axes_flat[4], "Subtracted Image Zero Minimum",
190-
colormap, vmin=0.0)
173+
plot_array(array=norm_resid, ax=axes_flat[4], title="Subtracted Image Zero Minimum",
174+
colormap=colormap, vmin=0.0)
191175

192-
_plot_symmetric(norm_resid, axes_flat[5], "Normalized Residual Map", colormap)
176+
_abs_max = _symmetric_vmax(norm_resid)
177+
plot_array(array=norm_resid, ax=axes_flat[5], title="Normalized Residual Map",
178+
colormap=colormap, vmin=-_abs_max, vmax=_abs_max)
193179

194180
plt.tight_layout()
195181
_save_subplot(fig, output_path, "subplot_fit_x1_plane", output_format)
@@ -260,11 +246,13 @@ def subplot_fit_log10(
260246
colormap=colormap, use_log10=True)
261247

262248
norm_resid = fit.normalized_residual_map
263-
_plot_symmetric(norm_resid, axes_flat[8], "Normalized Residual Map", colormap)
249+
_abs_max = _symmetric_vmax(norm_resid)
250+
plot_array(array=norm_resid, ax=axes_flat[8], title="Normalized Residual Map",
251+
colormap=colormap, vmin=-_abs_max, vmax=_abs_max)
264252

265-
_plot_with_vmin_vmax(norm_resid, axes_flat[9],
266-
r"Normalized Residual Map $1\sigma$", colormap,
267-
vmin=-1.0, vmax=1.0)
253+
plot_array(array=norm_resid, ax=axes_flat[9],
254+
title=r"Normalized Residual Map $1\sigma$",
255+
colormap=colormap, vmin=-1.0, vmax=1.0)
268256

269257
plot_array(array=fit.chi_squared_map, ax=axes_flat[10], title="Chi-Squared Map",
270258
colormap=colormap, use_log10=True)
@@ -291,30 +279,24 @@ def subplot_fit_log10_x1_plane(
291279
except (IndexError, AttributeError, ValueError):
292280
vmax = None
293281

294-
if vmax is not None:
295-
_plot_with_vmax(fit.data, axes_flat[0], "Data", colormap, vmax=vmax,
296-
use_log10=True)
297-
else:
298-
plot_array(array=fit.data, ax=axes_flat[0], title="Data", colormap=colormap,
299-
use_log10=True)
282+
plot_array(array=fit.data, ax=axes_flat[0], title="Data", colormap=colormap,
283+
vmax=vmax, use_log10=True)
300284

301285
try:
302286
plot_array(array=fit.signal_to_noise_map, ax=axes_flat[1],
303287
title="Signal-To-Noise Map", colormap=colormap, use_log10=True)
304288
except ValueError:
305289
axes_flat[1].axis("off")
306290

307-
if vmax is not None:
308-
_plot_with_vmax(fit.model_data, axes_flat[2], "Model Image", colormap,
309-
vmax=vmax, use_log10=True)
310-
else:
311-
plot_array(array=fit.model_data, ax=axes_flat[2], title="Model Image",
312-
colormap=colormap, use_log10=True)
291+
plot_array(array=fit.model_data, ax=axes_flat[2], title="Model Image",
292+
colormap=colormap, vmax=vmax, use_log10=True)
313293

314294
norm_resid = fit.normalized_residual_map
315295
plot_array(array=norm_resid, ax=axes_flat[3], title="Lens Light Subtracted",
316296
colormap=colormap)
317-
_plot_symmetric(norm_resid, axes_flat[4], "Normalized Residual Map", colormap)
297+
_abs_max = _symmetric_vmax(norm_resid)
298+
plot_array(array=norm_resid, ax=axes_flat[4], title="Normalized Residual Map",
299+
colormap=colormap, vmin=-_abs_max, vmax=_abs_max)
318300
plot_array(array=fit.chi_squared_map, ax=axes_flat[5], title="Chi-Squared Map",
319301
colormap=colormap, use_log10=True)
320302

@@ -381,8 +363,8 @@ def subplot_tracer_from_fit(
381363
try:
382364
source_model_img = fit.model_images_of_planes_list[final_plane_index]
383365
source_vmax = float(np.max(source_model_img.array))
384-
_plot_with_vmax(source_model_img, axes_flat[1], "Source Model Image",
385-
colormap, vmax=source_vmax)
366+
plot_array(array=source_model_img, ax=axes_flat[1], title="Source Model Image",
367+
colormap=colormap, vmax=source_vmax)
386368
except (IndexError, AttributeError, ValueError):
387369
axes_flat[1].axis("off")
388370

@@ -523,71 +505,11 @@ def subplot_fit_combined_log10(
523505
_save_subplot(fig, output_path, "fit_combined_log10", output_format)
524506

525507

526-
# ---------------------------------------------------------------------------
527-
# Private helpers for vmin/vmax manipulation without Cmap objects
528-
# ---------------------------------------------------------------------------
529-
530-
def _plot_with_vmax(array, ax, title, colormap, vmax, use_log10=False):
531-
from autoarray.plot.plots.array import plot_array as _aa_plot_array
532-
from autoarray.structures.plot.structure_plotters import (
533-
_auto_mask_edge, _zoom_array,
534-
)
535-
array = _zoom_array(array)
536-
try:
537-
arr = array.native.array
538-
extent = array.geometry.extent
539-
except AttributeError:
540-
arr = np.asarray(array)
541-
extent = None
542-
mask = _auto_mask_edge(array) if hasattr(array, "mask") else None
543-
_aa_plot_array(array=arr, ax=ax, extent=extent, mask=mask,
544-
title=title, colormap=colormap, use_log10=use_log10,
545-
vmax=vmax, structure=array)
546-
547-
548-
def _plot_with_vmin(array, ax, title, colormap, vmin, use_log10=False):
549-
from autoarray.plot.plots.array import plot_array as _aa_plot_array
550-
from autoarray.structures.plot.structure_plotters import (
551-
_auto_mask_edge, _zoom_array,
552-
)
553-
array = _zoom_array(array)
554-
try:
555-
arr = array.native.array
556-
extent = array.geometry.extent
557-
except AttributeError:
558-
arr = np.asarray(array)
559-
extent = None
560-
mask = _auto_mask_edge(array) if hasattr(array, "mask") else None
561-
_aa_plot_array(array=arr, ax=ax, extent=extent, mask=mask,
562-
title=title, colormap=colormap, use_log10=use_log10,
563-
vmin=vmin, structure=array)
564-
565-
566-
def _plot_with_vmin_vmax(array, ax, title, colormap, vmin, vmax, use_log10=False):
567-
from autoarray.plot.plots.array import plot_array as _aa_plot_array
568-
from autoarray.structures.plot.structure_plotters import (
569-
_auto_mask_edge, _zoom_array,
570-
)
571-
array = _zoom_array(array)
572-
try:
573-
arr = array.native.array
574-
extent = array.geometry.extent
575-
except AttributeError:
576-
arr = np.asarray(array)
577-
extent = None
578-
mask = _auto_mask_edge(array) if hasattr(array, "mask") else None
579-
_aa_plot_array(array=arr, ax=ax, extent=extent, mask=mask,
580-
title=title, colormap=colormap, use_log10=use_log10,
581-
vmin=vmin, vmax=vmax, structure=array)
582-
583-
584-
def _plot_symmetric(array, ax, title, colormap):
585-
"""Plot with symmetric colormap (vmin = -vmax)."""
586-
from autoarray.structures.plot.structure_plotters import _zoom_array
587-
_arr = _zoom_array(array)
508+
def _symmetric_vmax(array) -> float:
509+
"""Return abs-max finite value for symmetric colormap scaling."""
588510
try:
589-
vals = _arr.native.array
511+
vals = _zoom_array(array).native.array
590512
except AttributeError:
591-
vals = np.asarray(_arr)
592-
abs_max = float(np.max(np.abs(vals[np.isfinite(vals)]))) if np.any(np.isfinite(vals)) else 1.0
593-
_plot_with_vmin_vmax(array, ax, title, colormap, vmin=-abs_max, vmax=abs_max)
513+
vals = np.asarray(array)
514+
finite = vals[np.isfinite(vals)]
515+
return float(np.max(np.abs(finite))) if finite.size else 1.0

0 commit comments

Comments
 (0)