From cf4dc41a6981b9237ef38fd5fe8a0504210a46ee Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sun, 19 Oct 2025 16:59:04 +0100 Subject: [PATCH] fix bug in opver ampling --- autogalaxy/analysis/analysis/analysis.py | 30 +++++++++++++++++++ autogalaxy/analysis/plotter_interface.py | 3 +- autogalaxy/imaging/model/plotter_interface.py | 6 +++- autogalaxy/imaging/model/visualizer.py | 8 +++-- .../interferometer/model/plotter_interface.py | 8 +++-- autogalaxy/interferometer/model/visualizer.py | 25 +++++++++------- autogalaxy/plot/mat_plot/two_d.py | 2 ++ test_autogalaxy/config/general.yaml | 2 +- test_autogalaxy/config/non_linear.yaml | 20 ++++--------- 9 files changed, 71 insertions(+), 33 deletions(-) diff --git a/autogalaxy/analysis/analysis/analysis.py b/autogalaxy/analysis/analysis/analysis.py index 345b218db..d927ba195 100644 --- a/autogalaxy/analysis/analysis/analysis.py +++ b/autogalaxy/analysis/analysis/analysis.py @@ -127,3 +127,33 @@ def make_result( search_internal=search_internal, analysis=self, ) + + def perform_quick_update(self, paths, instance): + """ + Perform a quick visualization update during non-linear search fitting. + + This method is called intermittently while the sampler is running to produce + the `subplot+fit` plots of the current maximum-likelihood model fit. The intent + is to provide fast feedback (without waiting for the full run to complete) so that + users can monitor whether the fit is behaving sensibly. + + The plot appears both in a matplotlib window (if running locally) and is also saved to the + `output` folder of the output path. + + Parameters + ---------- + paths : af.DirectoryPaths + Object describing the output folder structure where visualization files + should be written. + instance : model instance + The current maximum-likelihood instance of the model, used to generate + the visualization plots. + """ + + self.Visualizer().visualize( + analysis=self, + paths=paths, + instance=instance, + during_analysis=True, + quick_update=True, + ) diff --git a/autogalaxy/analysis/plotter_interface.py b/autogalaxy/analysis/plotter_interface.py index b49e492d8..346df6586 100644 --- a/autogalaxy/analysis/plotter_interface.py +++ b/autogalaxy/analysis/plotter_interface.py @@ -90,7 +90,7 @@ def mat_plot_1d_from(self) -> MatPlot1D: output=aplt.Output(path=self.image_path, format=self.fmt), ) - def mat_plot_2d_from(self) -> MatPlot2D: + def mat_plot_2d_from(self, quick_update : bool = False) -> MatPlot2D: """ Returns a 2D matplotlib plotting object whose `Output` class uses the `image_path`, such that it outputs images to the `image` folder of the non-linear search. @@ -103,6 +103,7 @@ def mat_plot_2d_from(self) -> MatPlot2D: return MatPlot2D( title=aplt.Title(prefix=self.title_prefix), output=aplt.Output(path=self.image_path, format=self.fmt), + quick_update=quick_update, ) def galaxies( diff --git a/autogalaxy/imaging/model/plotter_interface.py b/autogalaxy/imaging/model/plotter_interface.py index 00fece1d9..8ad4143cf 100644 --- a/autogalaxy/imaging/model/plotter_interface.py +++ b/autogalaxy/imaging/model/plotter_interface.py @@ -139,6 +139,7 @@ def should_plot(name): def fit_imaging( self, fit: FitImaging, + quick_update: bool = False ): """ Visualizes a `FitImaging` object, which fits an imaging dataset. @@ -169,9 +170,12 @@ def should_plot(name): mat_plot_2d=mat_plot_2d, ) - if should_plot("subplot_fit"): + if should_plot("subplot_fit") or quick_update: fit_plotter.subplot_fit() + if quick_update: + return + if should_plot("subplot_of_galaxies"): fit_plotter.subplot_of_galaxies() diff --git a/autogalaxy/imaging/model/visualizer.py b/autogalaxy/imaging/model/visualizer.py index fc2cc0d93..c0a0d82cd 100644 --- a/autogalaxy/imaging/model/visualizer.py +++ b/autogalaxy/imaging/model/visualizer.py @@ -46,6 +46,7 @@ def visualize( paths: af.DirectoryPaths, instance: af.ModelInstance, during_analysis: bool, + quick_update: bool = False, ): """ Output images of the maximum log likelihood model inferred by the model-fit. This function is called throughout @@ -78,13 +79,16 @@ def visualize( plotter = PlotterInterfaceImaging( image_path=paths.image_path, title_prefix=analysis.title_prefix ) - plotter.imaging(dataset=analysis.dataset) + # Quick Update only, skips everything after try: - plotter.fit_imaging(fit=fit) + plotter.fit_imaging(fit=fit, quick_update=quick_update) except exc.InversionException: pass + if quick_update: + return + galaxies = fit.galaxies_linear_light_profiles_to_light_profiles plotter.galaxies(galaxies=galaxies, grid=fit.grids.lp) diff --git a/autogalaxy/interferometer/model/plotter_interface.py b/autogalaxy/interferometer/model/plotter_interface.py index f27237b31..d67e8c8b4 100644 --- a/autogalaxy/interferometer/model/plotter_interface.py +++ b/autogalaxy/interferometer/model/plotter_interface.py @@ -131,6 +131,7 @@ def should_plot(name): def fit_interferometer( self, fit: FitInterferometer, + quick_update : bool = False, ): """ Visualizes a `FitInterferometer` object, which fits an interferometer dataset. @@ -163,12 +164,15 @@ def should_plot(name): mat_plot_2d=mat_plot_2d, ) - if should_plot("subplot_fit"): + if should_plot("subplot_fit") or quick_update: fit_plotter.subplot_fit() - if should_plot("subplot_fit_dirty_images"): + if should_plot("subplot_fit_dirty_images") or quick_update: fit_plotter.subplot_fit_dirty_images() + if quick_update: + return + if should_plot("subplot_fit_real_space"): fit_plotter.subplot_fit_real_space() diff --git a/autogalaxy/interferometer/model/visualizer.py b/autogalaxy/interferometer/model/visualizer.py index 87a9103f7..305758707 100644 --- a/autogalaxy/interferometer/model/visualizer.py +++ b/autogalaxy/interferometer/model/visualizer.py @@ -43,6 +43,7 @@ def visualize( paths: af.DirectoryPaths, instance: af.ModelInstance, during_analysis: bool, + quick_update: bool = False, ): """ Outputs images of the maximum log likelihood model inferred by the model-fit. This function is called @@ -75,28 +76,30 @@ def visualize( """ fit = analysis.fit_from(instance=instance) - PlotterInterface = PlotterInterfaceInterferometer( + plotter_interface = PlotterInterfaceInterferometer( image_path=paths.image_path, title_prefix=analysis.title_prefix ) - PlotterInterface.interferometer(dataset=analysis.interferometer) + + try: + plotter_interface.fit_interferometer( + fit=fit, quick_update=quick_update, + ) + except exc.InversionException: + pass + + if quick_update: + return galaxies = fit.galaxies_linear_light_profiles_to_light_profiles - PlotterInterface.galaxies( + plotter_interface.galaxies( galaxies=galaxies, grid=fit.grids.lp, ) - try: - PlotterInterface.fit_interferometer( - fit=fit, - ) - except exc.InversionException: - pass - if fit.inversion is not None: try: - PlotterInterface.inversion( + plotter_interface.inversion( inversion=fit.inversion, ) except IndexError: diff --git a/autogalaxy/plot/mat_plot/two_d.py b/autogalaxy/plot/mat_plot/two_d.py index d5ec95995..48126017d 100644 --- a/autogalaxy/plot/mat_plot/two_d.py +++ b/autogalaxy/plot/mat_plot/two_d.py @@ -50,6 +50,7 @@ def __init__( tangential_caustics_plot: Optional[w.TangentialCausticsPlot] = None, radial_caustics_plot: Optional[w.RadialCausticsPlot] = None, use_log10: bool = False, + quick_update : bool = False ): """ Visualizes data structures (e.g an `Array2D`, `Grid2D`, `VectorField`, etc.) using Matplotlib. @@ -215,4 +216,5 @@ def __init__( delaunay_drawer=delaunay_drawer, voronoi_drawer=voronoi_drawer, use_log10=use_log10, + quick_update=quick_update, ) diff --git a/test_autogalaxy/config/general.yaml b/test_autogalaxy/config/general.yaml index 2d8648e15..c3eef84d7 100644 --- a/test_autogalaxy/config/general.yaml +++ b/test_autogalaxy/config/general.yaml @@ -14,7 +14,7 @@ inversion: use_border_relocator: false # If True, by default a pixelization's border is used to relocate all pixels outside its border to the border. hpc: hpc_mode: false - iterations_per_update: 5000 + iterations_per_full_update: 5000 adapt: adapt_minimum_percent: 0.01 adapt_noise_limit: 100000000.0 diff --git a/test_autogalaxy/config/non_linear.yaml b/test_autogalaxy/config/non_linear.yaml index a805c821e..aa36965c4 100644 --- a/test_autogalaxy/config/non_linear.yaml +++ b/test_autogalaxy/config/non_linear.yaml @@ -4,18 +4,14 @@ mock: method: prior printing: silence: false - updates: - iterations_per_update: 2500 - remove_state_files_at_end: true + MockSearch: initialize: method: prior printing: silence: false search: {} - updates: - iterations_per_update: 2500 - remove_state_files_at_end: true + GridSearch: general: number_of_cores: 2 @@ -39,9 +35,7 @@ mcmc: nsteps: 2000 search: nwalkers: 50 - updates: - iterations_per_update: 2500 - remove_state_files_at_end: true + nest: DynestyDynamic: initialize: @@ -72,9 +66,7 @@ nest: walks: 25 settings: stagger_resampling_likelihood: false - updates: - iterations_per_update: 2500 - remove_state_files_at_end: true + DynestyStatic: initialize: method: prior @@ -102,6 +94,4 @@ nest: walks: 5 settings: stagger_resampling_likelihood: false - updates: - iterations_per_update: 2500 - remove_state_files_at_end: true +