From b887a7f4fe7d7e61547cc2e336ca0334f8775868 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:28:33 +0000 Subject: [PATCH 01/13] remove stochastic config oiptions --- test_autolens/config/general.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/test_autolens/config/general.yaml b/test_autolens/config/general.yaml index b4e59f75d..7374d5a40 100644 --- a/test_autolens/config/general.yaml +++ b/test_autolens/config/general.yaml @@ -13,7 +13,6 @@ hpc: adapt: adapt_minimum_percent: 0.01 adapt_noise_limit: 100000000.0 - stochastic_outputs: false inversion: check_reconstruction: false # If True, the inversion's reconstruction is checked to ensure the solution of a meshs's mapper is not an invalid solution where the values are all the same. use_positive_only_solver: false # If True, inversion's use a positive-only linear algebra solver by default, which is slower but prevents unphysical negative values in the reconstructed solutuion. From 50a78f7631350b018bb9ae0735210866c3c152f8 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:32:04 +0000 Subject: [PATCH 02/13] remove reference to SettingsLens and settings_lens --- autolens/__init__.py | 1 - autolens/analysis/analysis.py | 13 ------------- autolens/analysis/settings.py | 13 ------------- autolens/interferometer/model/analysis.py | 6 ------ autolens/point/model/analysis.py | 6 +----- autolens/quantity/model/analysis.py | 5 ----- docs/api/galaxy.rst | 1 - 7 files changed, 1 insertion(+), 44 deletions(-) delete mode 100644 autolens/analysis/settings.py diff --git a/autolens/__init__.py b/autolens/__init__.py index 3e250e6f2..154024dfe 100644 --- a/autolens/__init__.py +++ b/autolens/__init__.py @@ -83,7 +83,6 @@ from . import plot from . import aggregator as agg from .lens import subhalo -from .analysis.settings import SettingsLens from .lens.ray_tracing import Tracer from .lens.to_inversion import TracerToInversion from .analysis.positions import PositionsLHResample diff --git a/autolens/analysis/analysis.py b/autolens/analysis/analysis.py index 44f878077..51e25f867 100644 --- a/autolens/analysis/analysis.py +++ b/autolens/analysis/analysis.py @@ -23,7 +23,6 @@ from autolens.analysis.positions import PositionsLHPenalty from autolens.analysis.visualizer import Visualizer from autolens.lens.ray_tracing import Tracer -from autolens.analysis.settings import SettingsLens from autolens.lens import ray_tracing_util @@ -40,7 +39,6 @@ def __init__( positions_likelihood: Optional[ Union[PositionsLHResample, PositionsLHPenalty] ] = None, - settings_lens: SettingsLens = SettingsLens(), cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), ): """ @@ -55,14 +53,10 @@ def __init__( Parameters ---------- - settings_lens - Settings controlling the lens calculation, for example how close the lensed source's multiple images have - to trace within one another in the source plane for the model to not be discarded. cosmology The Cosmology assumed for this analysis. """ self.cosmology = cosmology - self.settings_lens = settings_lens or SettingsLens() self.positions_likelihood = positions_likelihood def tracer_via_instance_from( @@ -164,7 +158,6 @@ def __init__( cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, - settings_lens: SettingsLens = None, raise_inversion_positions_likelihood_exception: bool = True, ): """ @@ -192,9 +185,6 @@ def __init__( settings_inversion Settings controlling how an inversion is fitted during the model-fit, for example which linear algebra formalism is used. - settings_lens - Settings controlling the lens calculation, for example how close the lensed source's multiple images have - to trace within one another in the source plane for the model to not be discarded. raise_inversion_positions_likelihood_exception If an inversion is used without the `positions_likelihood` it is likely a systematic solution will be inferred, in which case an Exception is raised before the model-fit begins to inform the user @@ -213,12 +203,9 @@ def __init__( AnalysisLensing.__init__( self=self, positions_likelihood=positions_likelihood, - settings_lens=settings_lens, cosmology=cosmology, ) - self.settings_lens = settings_lens or SettingsLens() - self.preloads = self.preloads_cls() self.raise_inversion_positions_likelihood_exception = ( diff --git a/autolens/analysis/settings.py b/autolens/analysis/settings.py deleted file mode 100644 index 74620ae1a..000000000 --- a/autolens/analysis/settings.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import Optional - - -class SettingsLens: - def __init__( - self, - stochastic_likelihood_resamples: Optional[int] = None, - stochastic_samples: int = 250, - stochastic_histogram_bins: int = 10, - ): - self.stochastic_likelihood_resamples = stochastic_likelihood_resamples - self.stochastic_samples = stochastic_samples - self.stochastic_histogram_bins = stochastic_histogram_bins diff --git a/autolens/interferometer/model/analysis.py b/autolens/interferometer/model/analysis.py index 2b8650bcd..423019893 100644 --- a/autolens/interferometer/model/analysis.py +++ b/autolens/interferometer/model/analysis.py @@ -18,7 +18,6 @@ from autolens.interferometer.model.result import ResultInterferometer from autolens.interferometer.model.visualizer import VisualizerInterferometer from autolens.interferometer.fit_interferometer import FitInterferometer -from autolens.analysis.settings import SettingsLens from autolens import exc @@ -38,7 +37,6 @@ def __init__( cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, - settings_lens: SettingsLens = None, raise_inversion_positions_likelihood_exception: bool = True, ): """ @@ -75,9 +73,6 @@ def __init__( pixelization. settings_inversion Settings controlling how an inversion is fitted, for example which linear algebra formalism is used. - settings_lens - Settings controlling the lens calculation, for example how close the lensed source's multiple images have - to trace within one another in the source plane for the model to not be discarded. raise_inversion_positions_likelihood_exception If an inversion is used without the `positions_likelihood` it is likely a systematic solution will be inferred, in which case an Exception is raised before the model-fit begins to inform the user @@ -91,7 +86,6 @@ def __init__( cosmology=cosmology, settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, - settings_lens=settings_lens, raise_inversion_positions_likelihood_exception=raise_inversion_positions_likelihood_exception, ) diff --git a/autolens/point/model/analysis.py b/autolens/point/model/analysis.py index 9aac44ede..fb34ad4b5 100644 --- a/autolens/point/model/analysis.py +++ b/autolens/point/model/analysis.py @@ -12,7 +12,6 @@ from autolens.point.model.result import ResultPoint from autolens.point.point_solver import PointSolver -from autolens.analysis.settings import SettingsLens from autolens import exc @@ -31,7 +30,6 @@ def __init__( solver: PointSolver, dataset=None, cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), - settings_lens=SettingsLens(), ): """ The analysis performed for model-fitting a point-source dataset, for example fitting the point-sources of a @@ -52,14 +50,12 @@ def __init__( visualization. cosmology The cosmology of the ray-tracing calculation. - settings_lens - Settings which control how the model-fit is performed. """ super().__init__(cosmology=cosmology) AnalysisLensing.__init__( - self=self, settings_lens=settings_lens, cosmology=cosmology + self=self, cosmology=cosmology ) self.point_dict = point_dict diff --git a/autolens/quantity/model/analysis.py b/autolens/quantity/model/analysis.py index f09a2b03d..a51435e05 100644 --- a/autolens/quantity/model/analysis.py +++ b/autolens/quantity/model/analysis.py @@ -7,7 +7,6 @@ from autolens.analysis.visualizer import Visualizer from autolens.analysis.analysis import AnalysisLensing -from autolens.analysis.settings import SettingsLens from autogalaxy.quantity.plot.fit_quantity_plotters import FitQuantityPlotter from autolens.quantity.model.result import ResultQuantity from autolens.quantity.fit_quantity import FitQuantity @@ -19,7 +18,6 @@ def __init__( dataset: ag.DatasetQuantity, func_str: str, cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), - settings_lens=SettingsLens(), ): """ Analysis classes are used by PyAutoFit to fit a model to a dataset via a non-linear search. @@ -50,9 +48,6 @@ def __init__( the dataset. cosmology The Cosmology assumed for this analysis. - settings_lens - Settings controlling the lens calculation, for example how close the lensed source's multiple images have - to trace within one another in the source plane for the model to not be discarded. """ super().__init__(dataset=dataset, func_str=func_str, cosmology=cosmology) diff --git a/docs/api/galaxy.rst b/docs/api/galaxy.rst index 20d097f10..5897d8c46 100644 --- a/docs/api/galaxy.rst +++ b/docs/api/galaxy.rst @@ -15,7 +15,6 @@ Galaxy / Plane / Tracer Galaxy Plane Tracer - SettingsLens To treat the redshift of a galaxy as a free parameter in a model, the ``Redshift`` object must be used. From 7867e41ab52cf9cc3695f7ff3a4b5be53cf8abdd Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:35:25 +0000 Subject: [PATCH 03/13] remove stochastic from analysis.py --- autolens/analysis/analysis.py | 102 ---------------------------------- 1 file changed, 102 deletions(-) diff --git a/autolens/analysis/analysis.py b/autolens/analysis/analysis.py index 51e25f867..d3f86b205 100644 --- a/autolens/analysis/analysis.py +++ b/autolens/analysis/analysis.py @@ -319,105 +319,3 @@ def save_results(self, paths: af.DirectoryPaths, result: ResultDataset): result.max_log_likelihood_fit.tracer_to_inversion.image_plane_mesh_grid_pg_list ), ) - - if conf.instance["general"]["adapt"]["stochastic_outputs"]: - if len(image_mesh_list) > 0: - for image_mesh in image_mesh_list: - if image_mesh.is_stochastic: - self.save_stochastic_outputs( - paths=paths, samples=result.samples - ) - - def log_likelihood_cap_from( - self, stochastic_log_likelihoods_json_file: str - ) -> float: - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate (e.g. due to how different KMeans - seeds change the pixelization constructed by a `VoronoiBrightnessImage` pixelization). - - A log likelihood cap can be applied to model-fits performed using these `Inversion`'s to improve error and - posterior estimates. This log likelihood cap is estimated from a list of stochastic log likelihoods, where - these log likelihoods are computed using the same model but with different KMeans seeds. - - This function computes the log likelihood cap of a model-fit by loading a set of stochastic log likelihoods - from a .json file and fitting them with a 1D Gaussian. The cap is the mean value of this Gaussian. - - Parameters - ---------- - stochastic_log_likelihoods_json_file - A .json file which loads an ndarray of stochastic log likelihoods, which are likelihoods computed using the - same model but with different KMeans seeds. - - Returns - ------- - float - A log likelihood cap which is applied in a stochastic model-fit to give improved error and posterior - estimates. - """ - try: - with open(stochastic_log_likelihoods_json_file, "r") as f: - stochastic_log_likelihoods = np.asarray(json.load(f)) - except FileNotFoundError: - raise exc.AnalysisException( - "The file 'stochastic_log_likelihoods.json' could not be found in the output of the model-fitting results" - "in the analysis before the stochastic analysis. Rerun PyAutoLens with `stochastic_outputs=True` in the" - "`general.ini` configuration file." - ) - - mean, sigma = norm.fit(stochastic_log_likelihoods) - - return mean - - def stochastic_log_likelihoods_via_instance_from(self, instance) -> List[float]: - raise NotImplementedError() - - def save_stochastic_outputs(self, paths: af.DirectoryPaths, samples: af.Samples): - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate (e.g. due to how different KMeans - seeds change the pixelization constructed by a `VoronoiBrightnessImage` pixelization). - - This function computes the stochastic log likelihoods of such a model, which are the log likelihoods computed - using the same model but with different KMeans seeds. - - It outputs these stochastic likelihoods to a format which can be loaded via PyAutoFit's database tools, and - may also be loaded if this analysis is extended with a stochastic model-fit that applies a log likelihood cap. - - This function also outputs visualization showing a histogram of the stochastic likelihood distribution. - - Parameters - ---------- - paths - The PyAutoFit paths object which manages all paths, e.g. where the non-linear search outputs are stored, - visualization and the pickled objects used by the aggregator output by this function. - samples - A PyAutoFit object which contains the samples of the non-linear search, for example the chains of an MCMC - run of samples of the nested sampler. - """ - stochastic_log_likelihoods_json_file = path.join( - paths._files_path, "stochastic_log_likelihoods.json" - ) - - try: - with open(stochastic_log_likelihoods_json_file, "r") as f: - stochastic_log_likelihoods = np.asarray(json.load(f)) - except FileNotFoundError: - instance = samples.max_log_likelihood() - stochastic_log_likelihoods = ( - self.stochastic_log_likelihoods_via_instance_from(instance=instance) - ) - - if stochastic_log_likelihoods is None: - return - - with open(stochastic_log_likelihoods_json_file, "w") as outfile: - json.dump( - [float(evidence) for evidence in stochastic_log_likelihoods], outfile - ) - - visualizer = Visualizer(visualize_path=paths.image_path) - - visualizer.visualize_stochastic_histogram( - stochastic_log_likelihoods=stochastic_log_likelihoods, - max_log_evidence=np.max(samples.log_likelihood_list), - histogram_bins=self.settings_lens.stochastic_histogram_bins, - ) From f7e3a0657c018918e83781eb7e8c9c84d9fc2e87 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:35:43 +0000 Subject: [PATCH 04/13] remove stochastic from result.py --- autolens/analysis/result.py | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/autolens/analysis/result.py b/autolens/analysis/result.py index fb0e9093c..e7819a51b 100644 --- a/autolens/analysis/result.py +++ b/autolens/analysis/result.py @@ -242,37 +242,4 @@ def source_plane_inversion_centre(self) -> aa.Grid2DIrregular: if self.max_log_likelihood_fit.inversion.has(cls=aa.AbstractMapper): return self.max_log_likelihood_fit.inversion.brightest_reconstruction_pixel_centre_list[ 0 - ] - - def stochastic_log_likelihoods_from(self, paths: AbstractPaths) -> np.ndarray: - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate. - - For example, the `VoronoiBrightnessImage` pixelization, which changes the likelihood depending on how different - KMeans seeds change the pixel-grid. - - A log likelihood cap can be applied to model-fits performed using these `Inversion`'s to improve error and - posterior estimates. This log likelihood cap is estimated from a list of stochastic log likelihoods, where - these log likelihoods are computed using the same model but with different KMeans seeds. - - This function loads existing stochastic log likelihoods from the hard disk via a .json file. If the .json - file is not presented, then the log likelihoods are computed via the `stochastic_log_likelihoods_via_instance_from` - function of the associated Analysis class. - """ - stochastic_log_likelihoods_json_file = path.join( - paths.output_path, "stochastic_log_likelihoods.json" - ) - - paths.restore() - - try: - with open(stochastic_log_likelihoods_json_file, "r") as f: - stochastic_log_likelihoods = np.asarray(json.load(f)) - except FileNotFoundError: - self.analysis.save_stochastic_outputs(paths=paths, samples=self.samples) - with open(stochastic_log_likelihoods_json_file, "r") as f: - stochastic_log_likelihoods = np.asarray(json.load(f)) - - paths.zip_remove() - - return stochastic_log_likelihoods + ] \ No newline at end of file From fc52707627c3ffd4fb0ef3d196554a5e641f7e49 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:36:26 +0000 Subject: [PATCH 05/13] remove stochastic from visualizer.py --- autolens/analysis/visualizer.py | 69 +-------------------------------- 1 file changed, 1 insertion(+), 68 deletions(-) diff --git a/autolens/analysis/visualizer.py b/autolens/analysis/visualizer.py index 880675506..9192fff60 100644 --- a/autolens/analysis/visualizer.py +++ b/autolens/analysis/visualizer.py @@ -247,71 +247,4 @@ def should_plot(name): if should_plot("contribution_map_list"): adapt_plotter.subplot_contribution_map_list( contribution_map_list_list=tracer.contribution_map_list - ) - - def visualize_stochastic_histogram( - self, - stochastic_log_likelihoods: np.ndarray, - max_log_evidence: float, - histogram_bins: int = 10, - ): - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate. - - For example, the `VoronoiBrightnessImage` pixelization, which changes the likelihood depending on how different - KMeans seeds change the pixel-grid. - - A log likelihood cap can be applied to model-fits performed using these `Inversion`'s to improve error and - posterior estimates. This log likelihood cap is estimated from a list of stochastic log likelihoods, where - these log likelihoods are computed using the same model but with different KMeans seeds. - - This function plots a histogram representing the distribution of these stochastic log likelihoods with a 1D - Gaussian fit to the likelihoods overlaid. This figure can be used to determine how subject the fit to this - dataset is to the stochastic likelihood effect. - - Parameters - ---------- - stochastic_log_likelihoods - The stochastic log likelihood which are used to plot the histogram and Gaussian. - max_log_evidence - The maximum log likelihood value of the non-linear search, which will likely be much larger than any of the - stochastic log likelihoods as it will be boosted high relative to most samples. - histogram_bins - The number of bins in the histogram used to visualize the distribution of stochastic log likelihoods. - - Returns - ------- - float - A log likelihood cap which is applied in a stochastic model-fit to give improved error and posterior - estimates. - """ - if stochastic_log_likelihoods is None: - return - - if plot_setting("other", "stochastic_histogram"): - file_path = path.join(self.visualize_path, "other") - - try: - os.makedirs(file_path) - except FileExistsError or IsADirectoryError: - pass - - filename = path.join(file_path, "stochastic_histogram.png") - - if path.exists(filename): - try: - os.rmdir(filename) - except Exception: - pass - - (mu, sigma) = norm.fit(stochastic_log_likelihoods) - n, bins, patches = plt.hist( - x=stochastic_log_likelihoods, bins=histogram_bins, density=1 - ) - y = norm.pdf(bins, mu, sigma) - plt.plot(bins, y, "--") - plt.xlabel("log evidence") - plt.title("Stochastic Log Evidence Histogram") - plt.axvline(max_log_evidence, color="r") - plt.savefig(filename, bbox_inches="tight") - plt.close() + ) \ No newline at end of file From 86b16f9c4c0caf9c8831d2bca21ee085a84da841 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:38:07 +0000 Subject: [PATCH 06/13] remove stochastic from imaging / interferometer analysis.py --- autolens/config/visualize/plots.yaml | 2 - autolens/imaging/model/analysis.py | 65 ---------------------- autolens/interferometer/model/analysis.py | 66 ----------------------- 3 files changed, 133 deletions(-) diff --git a/autolens/config/visualize/plots.yaml b/autolens/config/visualize/plots.yaml index deda74da8..cb942970d 100644 --- a/autolens/config/visualize/plots.yaml +++ b/autolens/config/visualize/plots.yaml @@ -73,8 +73,6 @@ dirty_residual_map: false dirty_normalized_residual_map: false dirty_chi_squared_map: false - other: # Settings for other plotting quantities. - stochastic_histogram: false fit_quantity: # Settings for plots of fit quantities (e.g. FitQuantityPlotter). all_at_end_png: true # Plot all individual plots listed below as .png (even if False)? all_at_end_fits: true # Plot all individual plots listed below as .fits (even if False)? diff --git a/autolens/imaging/model/analysis.py b/autolens/imaging/model/analysis.py index bc58d3894..438c0cf9f 100644 --- a/autolens/imaging/model/analysis.py +++ b/autolens/imaging/model/analysis.py @@ -204,71 +204,6 @@ def fit_imaging_via_tracer_from( def fit_func(self): return self.fit_imaging_via_instance_from - def stochastic_log_likelihoods_via_instance_from(self, instance: af.ModelInstance): - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate. - - For example, the `VoronoiBrightnessImage` pixelization, which changes the likelihood depending on how different - KMeans seeds change the pixel-grid. - - A log likelihood cap can be applied to model-fits performed using these `Inversion`'s to improve error and - posterior estimates. This log likelihood cap is estimated from a list of stochastic log likelihoods, where - these log likelihoods are computed using the same model but with different KMeans seeds. - - This function computes these stochastic log likelihoods by iterating over many model-fits using different - KMeans seeds. - - Parameters - ---------- - instance - The maximum log likelihood instance of a model that is has finished being fitted to the dataset. - - Returns - ------- - float - A log likelihood cap which is applied in a stochastic model-fit to give improved error and posterior - estimates. - """ - instance = self.instance_with_associated_adapt_images_from(instance=instance) - tracer = self.tracer_via_instance_from(instance=instance) - - if not tracer.has(cls=ag.Pixelization): - return - - if not any( - pix.image_mesh.is_stochastic for pix in tracer.cls_list_from(cls=ag.Pixelization) - ): - return - - log_evidences = [] - - for i in range(self.settings_lens.stochastic_samples): - - try: - - tracer.galaxies[-1].pixelization.image_mesh.seed = i - - log_evidence = FitImaging( - dataset=self.dataset, - tracer=tracer, - settings_pixelization=self.settings_pixelization, - settings_inversion=self.settings_inversion, - preloads=self.preloads, - ).log_evidence - except ( - PixelizationException, - exc.PixelizationException, - exc.InversionException, - exc.GridException, - OverflowError, - ) as e: - log_evidence = None - - if log_evidence is not None: - log_evidences.append(log_evidence) - - return log_evidences - def visualize_before_fit(self, paths: af.DirectoryPaths, model: af.Collection): """ PyAutoFit calls this function immediately before the non-linear search begins. diff --git a/autolens/interferometer/model/analysis.py b/autolens/interferometer/model/analysis.py index 423019893..8af7d2ffd 100644 --- a/autolens/interferometer/model/analysis.py +++ b/autolens/interferometer/model/analysis.py @@ -266,72 +266,6 @@ def fit_interferometer_via_tracer_from( def fit_func(self): return self.fit_interferometer_via_instance_from - def stochastic_log_likelihoods_via_instance_from(self, instance): - """ - Certain `Inversion`'s have stochasticity in their log likelihood estimate. - - For example, the `VoronoiBrightnessImage` pixelization, which changes the likelihood depending on how different - KMeans seeds change the pixel-grid. - - A log likelihood cap can be applied to model-fits performed using these `Inversion`'s to improve error and - posterior estimates. This log likelihood cap is estimated from a list of stochastic log likelihoods, where - these log likelihoods are computed using the same model but with different KMeans seeds. - - This function computes these stochastic log likelihoods by iterating over many model-fits using different - KMeans seeds. - - Parameters - ---------- - instance - The maximum log likelihood instance of a model that is has finished being fitted to the dataset. - - Returns - ------- - float - A log likelihood cap which is applied in a stochastic model-fit to give improved error and posterior - estimates. - """ - instance = self.instance_with_associated_adapt_images_from(instance=instance) - tracer = self.tracer_via_instance_from(instance=instance) - - if not tracer.has(cls=aa.Pixelization): - return None - - if not any( - [ - pix.image_mesh.is_stochastic - for pix in tracer.cls_list_from(cls=ag.Pixelization) - ] - ): - return - - log_evidences = [] - - for i in range(self.settings_lens.stochastic_samples): - try: - tracer.galaxies[-1].pixelization.image_mesh.seed = i - - log_evidence = FitInterferometer( - dataset=self.dataset, - tracer=tracer, - settings_pixelization=self.settings_pixelization, - settings_inversion=self.settings_inversion, - preloads=self.preloads, - ).log_evidence - except ( - PixelizationException, - exc.PixelizationException, - exc.InversionException, - exc.GridException, - OverflowError, - ) as e: - log_evidence = None - - if log_evidence is not None: - log_evidences.append(log_evidence) - - return log_evidences - def visualize_before_fit(self, paths: af.DirectoryPaths, model: af.Collection): """ PyAutoFit calls this function immediately before the non-linear search begins. From 49177081f57844ea6c460f0e2226a386fb698d00 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:40:33 +0000 Subject: [PATCH 07/13] remove unit tests --- test_autolens/analysis/test_visualizer.py | 8 --- test_autolens/config/general.yaml | 2 - test_autolens/config/visualize.yaml | 2 - .../imaging/model/test_analysis_imaging.py | 65 ------------------- test_autolens/imaging/test_fit_imaging.py | 50 -------------- .../model/test_analysis_interferometer.py | 45 ------------- 6 files changed, 172 deletions(-) diff --git a/test_autolens/analysis/test_visualizer.py b/test_autolens/analysis/test_visualizer.py index 62fe8feb1..cbc796c3d 100644 --- a/test_autolens/analysis/test_visualizer.py +++ b/test_autolens/analysis/test_visualizer.py @@ -59,11 +59,3 @@ def test__visualizes_image_with_positions__uses_configs( assert path.join(plot_path, "image_with_positions.png") in plot_patch.paths - -def test__visualize_stochastic_histogram(masked_imaging_7x7, plot_path, plot_patch): - visualizer = vis.Visualizer(visualize_path=plot_path) - - visualizer.visualize_stochastic_histogram( - stochastic_log_likelihoods=[1.0, 2.0, 1.0, 2.0, 3.0, 2.5], max_log_evidence=3.0 - ) - assert path.join(plot_path, "other", "stochastic_histogram.png") in plot_patch.paths diff --git a/test_autolens/config/general.yaml b/test_autolens/config/general.yaml index 7374d5a40..824254fa0 100644 --- a/test_autolens/config/general.yaml +++ b/test_autolens/config/general.yaml @@ -5,8 +5,6 @@ fits: flip_for_ds9: true grid: remove_projected_centre: false - stochastic_histogram_bins: 2 - stochastic_histogram_samples: 2 hpc: hpc_mode: false iterations_per_update: 5000 diff --git a/test_autolens/config/visualize.yaml b/test_autolens/config/visualize.yaml index a9fd1fbd5..d2ac393a4 100644 --- a/test_autolens/config/visualize.yaml +++ b/test_autolens/config/visualize.yaml @@ -140,8 +140,6 @@ plots: regularization_weights: false residual_map: false subplot_inversion: true - other: - stochastic_histogram: true positions: image_with_positions: true ray_tracing: diff --git a/test_autolens/imaging/model/test_analysis_imaging.py b/test_autolens/imaging/model/test_analysis_imaging.py index 0f1f9d30e..bfe5059db 100644 --- a/test_autolens/imaging/model/test_analysis_imaging.py +++ b/test_autolens/imaging/model/test_analysis_imaging.py @@ -156,71 +156,6 @@ def test__sets_up_adapt_galaxy_images__froms(masked_imaging_7x7): assert (analysis.adapt_model_image.native == 3.0 * np.ones((3, 3))).all() - -def test__stochastic_log_likelihoods_for_instance(masked_imaging_7x7): - - lens_adapt_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) - lens_adapt_image[4] = 10.0 - source_adapt_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) - source_adapt_image[4] = 10.0 - adapt_model_image = al.Array2D.full( - fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1 - ) - - adapt_galaxy_image_path_dict = { - ("galaxies", "lens"): lens_adapt_image, - ("galaxies", "source"): source_adapt_image, - } - - result = al.m.MockResult( - adapt_galaxy_image_path_dict=adapt_galaxy_image_path_dict, - adapt_model_image=adapt_model_image, - ) - - pixelization = al.Pixelization( - image_mesh=al.image_mesh.Overlay(shape=(3, 3)), - mesh=al.mesh.Voronoi() - ) - - galaxies = af.ModelInstance() - galaxies.lens = al.Galaxy( - redshift=0.5, mass=al.mp.IsothermalSph(einstein_radius=1.0) - ) - galaxies.source = al.Galaxy(redshift=1.0, pixelization=pixelization) - - instance = af.ModelInstance() - instance.galaxies = galaxies - - analysis = al.AnalysisImaging( - dataset=masked_imaging_7x7, - adapt_result=result, - settings_lens=al.SettingsLens(stochastic_samples=2), - ) - - stochastic_log_likelihoods = analysis.stochastic_log_likelihoods_via_instance_from( - instance=instance - ) - - assert stochastic_log_likelihoods is None - - pixelization = al.Pixelization( - image_mesh=al.image_mesh.KMeans(pixels=7), - mesh=al.mesh.Voronoi() - ) - - galaxies.source = al.Galaxy(redshift=1.0, pixelization=pixelization) - - instance = af.ModelInstance() - instance.galaxies = galaxies - - stochastic_log_likelihoods = analysis.stochastic_log_likelihoods_via_instance_from( - instance=instance - ) - - assert len(stochastic_log_likelihoods) == 2 - assert stochastic_log_likelihoods[0] != stochastic_log_likelihoods[1] - - def test__profile_log_likelihood_function(masked_imaging_7x7): pixelization = al.Pixelization( diff --git a/test_autolens/imaging/test_fit_imaging.py b/test_autolens/imaging/test_fit_imaging.py index 51b6d9755..13ab46d3c 100644 --- a/test_autolens/imaging/test_fit_imaging.py +++ b/test_autolens/imaging/test_fit_imaging.py @@ -452,56 +452,6 @@ def test__tracer_linear_light_profiles_to_light_profiles(masked_imaging_7x7): assert tracer.galaxies[1].bulge.intensity == pytest.approx(-371.061130, 1.0e-4) assert tracer.galaxies[2].bulge.intensity == pytest.approx(0.08393533428, 1.0e-4) - -def _test___stochastic_mode__gives_different_log_likelihoods(masked_imaging_7x7): - - pixelization = al.Pixelization( - mesh=al.mesh.VoronoiBrightnessImage(pixels=7), - regularization=al.reg.Constant(coefficient=1.0), - ) - - g0 = al.Galaxy( - redshift=0.5, - pixelization=pixelization, - adapt_model_image=al.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0), - adapt_galaxy_image=al.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0), - ) - - tracer = al.Tracer.from_galaxies(galaxies=[al.Galaxy(redshift=0.5), g0]) - - fit_0 = al.FitImaging( - dataset=masked_imaging_7x7, - tracer=tracer, - settings_pixelization=al.SettingsPixelization(is_stochastic=False), - ) - fit_1 = al.FitImaging( - dataset=masked_imaging_7x7, - tracer=tracer, - settings_pixelization=al.SettingsPixelization(is_stochastic=False), - ) - - assert fit_0.log_evidence == fit_1.log_evidence - - fit_0 = al.FitImaging( - dataset=masked_imaging_7x7, - tracer=tracer, - settings_pixelization=al.SettingsPixelization(is_stochastic=True), - ) - fit_1 = al.FitImaging( - dataset=masked_imaging_7x7, - tracer=tracer, - settings_pixelization=al.SettingsPixelization(is_stochastic=True), - ) - - # Sum 5 stochastic likelihoods to avoid random chance of identical - # pixelizations and therefore likelihoods. - - log_evidence_x5_0 = sum([fit_0.log_evidence for i in range(5)]) - log_evidence_x5_1 = sum([fit_1.log_evidence for i in range(5)]) - - assert log_evidence_x5_0 != log_evidence_x5_1 - - def test__preloads__refit_with_new_preloads(masked_imaging_7x7): g0 = al.Galaxy( diff --git a/test_autolens/interferometer/model/test_analysis_interferometer.py b/test_autolens/interferometer/model/test_analysis_interferometer.py index 4a12700c3..d711c7345 100644 --- a/test_autolens/interferometer/model/test_analysis_interferometer.py +++ b/test_autolens/interferometer/model/test_analysis_interferometer.py @@ -146,51 +146,6 @@ def test__sets_up_adapt_galaxy_images(interferometer_7): assert (analysis.adapt_model_image.native == 3.0 * np.ones((3, 3))).all() - -def test__stochastic_log_likelihoods_for_instance(interferometer_7): - lens_adapt_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) - lens_adapt_image[4] = 10.0 - source_adapt_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) - source_adapt_image[4] = 10.0 - adapt_model_image = al.Array2D.full( - fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1 - ) - - adapt_galaxy_image_path_dict = { - ("galaxies", "lens"): lens_adapt_image, - ("galaxies", "source"): source_adapt_image, - } - - result = al.m.MockResult( - adapt_galaxy_image_path_dict=adapt_galaxy_image_path_dict, - adapt_model_image=adapt_model_image, - ) - - analysis = al.AnalysisInterferometer( - dataset=interferometer_7, - settings_lens=al.SettingsLens(stochastic_samples=2), - adapt_result=result, - settings_inversion=al.SettingsInversion(use_w_tilde=False), - ) - - pixelization = al.Pixelization( - image_mesh=al.image_mesh.KMeans(pixels=5), mesh=al.mesh.Voronoi() - ) - - galaxies = af.ModelInstance() - galaxies.source = al.Galaxy(redshift=1.0, pixelization=pixelization) - - instance = af.ModelInstance() - instance.galaxies = galaxies - - log_evidences = analysis.stochastic_log_likelihoods_via_instance_from( - instance=instance - ) - - assert len(log_evidences) == 2 - assert log_evidences[0] != log_evidences[1] - - def test__profile_log_likelihood_function(interferometer_7): pixelization = al.Pixelization( mesh=al.mesh.Rectangular(shape=(3, 3)), From 76912a6c0c04fe173ed961fbd07ccf25f0b6ddae Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:41:01 +0000 Subject: [PATCH 08/13] black --- autolens/analysis/result.py | 2 +- autolens/analysis/visualizer.py | 2 +- autolens/point/model/analysis.py | 4 +--- test_autolens/analysis/test_visualizer.py | 1 - .../interferometer/model/test_analysis_interferometer.py | 1 + 5 files changed, 4 insertions(+), 6 deletions(-) diff --git a/autolens/analysis/result.py b/autolens/analysis/result.py index e7819a51b..438540b84 100644 --- a/autolens/analysis/result.py +++ b/autolens/analysis/result.py @@ -242,4 +242,4 @@ def source_plane_inversion_centre(self) -> aa.Grid2DIrregular: if self.max_log_likelihood_fit.inversion.has(cls=aa.AbstractMapper): return self.max_log_likelihood_fit.inversion.brightest_reconstruction_pixel_centre_list[ 0 - ] \ No newline at end of file + ] diff --git a/autolens/analysis/visualizer.py b/autolens/analysis/visualizer.py index 9192fff60..fabeb9ba3 100644 --- a/autolens/analysis/visualizer.py +++ b/autolens/analysis/visualizer.py @@ -247,4 +247,4 @@ def should_plot(name): if should_plot("contribution_map_list"): adapt_plotter.subplot_contribution_map_list( contribution_map_list_list=tracer.contribution_map_list - ) \ No newline at end of file + ) diff --git a/autolens/point/model/analysis.py b/autolens/point/model/analysis.py index fb34ad4b5..1bdeeb14d 100644 --- a/autolens/point/model/analysis.py +++ b/autolens/point/model/analysis.py @@ -54,9 +54,7 @@ def __init__( super().__init__(cosmology=cosmology) - AnalysisLensing.__init__( - self=self, cosmology=cosmology - ) + AnalysisLensing.__init__(self=self, cosmology=cosmology) self.point_dict = point_dict diff --git a/test_autolens/analysis/test_visualizer.py b/test_autolens/analysis/test_visualizer.py index cbc796c3d..0c977d2f7 100644 --- a/test_autolens/analysis/test_visualizer.py +++ b/test_autolens/analysis/test_visualizer.py @@ -58,4 +58,3 @@ def test__visualizes_image_with_positions__uses_configs( plot_path = path.join(plot_path, "positions") assert path.join(plot_path, "image_with_positions.png") in plot_patch.paths - diff --git a/test_autolens/interferometer/model/test_analysis_interferometer.py b/test_autolens/interferometer/model/test_analysis_interferometer.py index d711c7345..e26ccb17e 100644 --- a/test_autolens/interferometer/model/test_analysis_interferometer.py +++ b/test_autolens/interferometer/model/test_analysis_interferometer.py @@ -146,6 +146,7 @@ def test__sets_up_adapt_galaxy_images(interferometer_7): assert (analysis.adapt_model_image.native == 3.0 * np.ones((3, 3))).all() + def test__profile_log_likelihood_function(interferometer_7): pixelization = al.Pixelization( mesh=al.mesh.Rectangular(shape=(3, 3)), From fce7124e2d5e994e8424e4b52b4ee64897b6f290 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:43:14 +0000 Subject: [PATCH 09/13] final unit tests fixed --- autolens/quantity/model/analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autolens/quantity/model/analysis.py b/autolens/quantity/model/analysis.py index a51435e05..bcf11396f 100644 --- a/autolens/quantity/model/analysis.py +++ b/autolens/quantity/model/analysis.py @@ -52,7 +52,7 @@ def __init__( super().__init__(dataset=dataset, func_str=func_str, cosmology=cosmology) AnalysisLensing.__init__( - self=self, settings_lens=settings_lens, cosmology=cosmology + self=self, cosmology=cosmology ) def fit_quantity_for_instance(self, instance: af.ModelInstance) -> FitQuantity: From e499bf1b149a31991d4dbbf153dcfd85b95e2294 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 10:51:26 +0000 Subject: [PATCH 10/13] minor --- autolens/analysis/analysis.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/autolens/analysis/analysis.py b/autolens/analysis/analysis.py index d3f86b205..f1e1ab3f4 100644 --- a/autolens/analysis/analysis.py +++ b/autolens/analysis/analysis.py @@ -284,9 +284,6 @@ def save_results(self, paths: af.DirectoryPaths, result: ResultDataset): - The maximum log likelihood tracer of the fit. - - The stochastic log likelihoods of a pixelization, provided the pixelization has functionality that can - compute likelihoods for different KMeans seeds and grids (e.g. `VoronoiBrightnessImage). - Parameters ---------- paths From 298b86201aafed9ab80c9c7f6ec073f9c687d3ea Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 12:54:06 +0000 Subject: [PATCH 11/13] remove all reference to settings_pixelization --- autolens/__init__.py | 1 - autolens/aggregator/fit_imaging.py | 14 -------------- autolens/aggregator/fit_interferometer.py | 14 -------------- autolens/aggregator/subhalo.py | 2 -- autolens/analysis/analysis.py | 5 ----- autolens/imaging/fit_imaging.py | 7 ------- autolens/imaging/model/analysis.py | 1 - autolens/interferometer/fit_interferometer.py | 7 ------- autolens/interferometer/model/analysis.py | 6 ------ autolens/lens/to_inversion.py | 4 ---- 10 files changed, 61 deletions(-) diff --git a/autolens/__init__.py b/autolens/__init__.py index 154024dfe..2a96976bf 100644 --- a/autolens/__init__.py +++ b/autolens/__init__.py @@ -16,7 +16,6 @@ from autoarray.inversion.pixelization.mesh.abstract import AbstractMesh from autoarray.inversion.regularization.abstract import AbstractRegularization from autoarray.inversion.pixelization.pixelization import Pixelization -from autoarray.inversion.pixelization.settings import SettingsPixelization from autoarray.inversion.inversion.settings import SettingsInversion from autoarray.inversion.inversion.factory import inversion_from as Inversion from autoarray.inversion.inversion.factory import ( diff --git a/autolens/aggregator/fit_imaging.py b/autolens/aggregator/fit_imaging.py index 2df1d918c..b936c5ded 100644 --- a/autolens/aggregator/fit_imaging.py +++ b/autolens/aggregator/fit_imaging.py @@ -19,7 +19,6 @@ def _fit_imaging_from( fit: af.Fit, galaxies: List[ag.Galaxy], settings_dataset: aa.SettingsImaging = None, - settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, use_preloaded_grid: bool = True, ) -> List[FitImaging]: @@ -30,7 +29,6 @@ def _fit_imaging_from( - The imaging data, noise-map, PSF and settings as .fits files (e.g. `dataset/data.fits`). - The mask used to mask the `Imaging` data structure in the fit (`dataset/mask.fits`). - - The settings of pixelization used by the fit (`dataset/settings_pixelization.json`). - The settings of inversions used by the fit (`dataset/settings_inversion.json`). Each individual attribute can be loaded from the database via the `fit.value()` method. @@ -53,8 +51,6 @@ def _fit_imaging_from( A list of galaxies corresponding to a sample of a non-linear search and model-fit. settings_dataset Optionally overwrite the `SettingsImaging` of the `Imaging` object that is created from the fit. - settings_pixelization - Optionally overwrite the `SettingsPixelization` of the `Pixelization` object that is created from the fit. settings_inversion Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit. use_preloaded_grid @@ -67,9 +63,6 @@ def _fit_imaging_from( tracer_list = _tracer_from(fit=fit, galaxies=galaxies) - settings_pixelization = settings_pixelization or fit.value( - name="settings_pixelization" - ) settings_inversion = settings_inversion or fit.value(name="settings_inversion") mesh_grids_of_planes_list = agg_util.mesh_grids_of_planes_list_from( @@ -92,7 +85,6 @@ def _fit_imaging_from( FitImaging( dataset=dataset, tracer=tracer, - settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, ) @@ -106,7 +98,6 @@ def __init__( self, aggregator: af.Aggregator, settings_dataset: Optional[aa.SettingsImaging] = None, - settings_pixelization: Optional[aa.SettingsPixelization] = None, settings_inversion: Optional[aa.SettingsInversion] = None, use_preloaded_grid: bool = True, ): @@ -118,7 +109,6 @@ def __init__( - The imaging data, noise-map, PSF and settings as .fits files (e.g. `dataset/data.fits`). - The mask used to mask the `Imaging` data structure in the fit (`dataset/mask.fits`). - - The settings of pixelization used by the fit (`dataset/settings_pixelization.json`). - The settings of inversions used by the fit (`dataset/settings_inversion.json`). The `aggregator` contains the path to each of these files, and they can be loaded individually. This class @@ -143,8 +133,6 @@ def __init__( A `PyAutoFit` aggregator object which can load the results of model-fits. settings_dataset Optionally overwrite the `SettingsImaging` of the `Imaging` object that is created from the fit. - settings_pixelization - Optionally overwrite the `SettingsPixelization` of the `Pixelization` object that is created from the fit. settings_inversion Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit. use_preloaded_grid @@ -155,7 +143,6 @@ def __init__( super().__init__(aggregator=aggregator) self.settings_dataset = settings_dataset - self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.use_preloaded_grid = use_preloaded_grid @@ -176,7 +163,6 @@ def object_via_gen_from(self, fit, galaxies) -> List[FitImaging]: fit=fit, galaxies=galaxies, settings_dataset=self.settings_dataset, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, use_preloaded_grid=self.use_preloaded_grid, ) diff --git a/autolens/aggregator/fit_interferometer.py b/autolens/aggregator/fit_interferometer.py index 1b9999191..5fcea7837 100644 --- a/autolens/aggregator/fit_interferometer.py +++ b/autolens/aggregator/fit_interferometer.py @@ -19,7 +19,6 @@ def _fit_interferometer_from( galaxies: List[ag.Galaxy], real_space_mask: Optional[aa.Mask2D] = None, settings_dataset: aa.SettingsInterferometer = None, - settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, use_preloaded_grid: bool = True, ) -> List[FitInterferometer]: @@ -30,7 +29,6 @@ def _fit_interferometer_from( - The interferometer data, noise-map, uv-wavelengths and settings as .fits files (e.g. `dataset/data.fits`). - The real space mask defining the grid of the interferometer for the FFT (`dataset/real_space_mask.fits`). - - The settings of pixelization used by the fit (`dataset/settings_pixelization.json`). - The settings of inversions used by the fit (`dataset/settings_inversion.json`). Each individual attribute can be loaded from the database via the `fit.value()` method. @@ -54,8 +52,6 @@ def _fit_interferometer_from( A list of galaxies corresponding to a sample of a non-linear search and model-fit. settings_dataset Optionally overwrite the `SettingsInterferometer` of the `Interferometer` object that is created from the fit. - settings_pixelization - Optionally overwrite the `SettingsPixelization` of the `Pixelization` object that is created from the fit. settings_inversion Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit. use_preloaded_grid @@ -70,9 +66,6 @@ def _fit_interferometer_from( ) tracer_list = _tracer_from(fit=fit, galaxies=galaxies) - settings_pixelization = settings_pixelization or fit.value( - name="settings_pixelization" - ) settings_inversion = settings_inversion or fit.value(name="settings_inversion") mesh_grids_of_planes_list = agg_util.mesh_grids_of_planes_list_from( @@ -95,7 +88,6 @@ def _fit_interferometer_from( FitInterferometer( dataset=dataset, tracer=tracer, - settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, ) @@ -109,7 +101,6 @@ def __init__( self, aggregator: af.Aggregator, settings_dataset: Optional[aa.SettingsInterferometer] = None, - settings_pixelization: Optional[aa.SettingsPixelization] = None, settings_inversion: Optional[aa.SettingsInversion] = None, use_preloaded_grid: bool = True, real_space_mask: Optional[aa.Mask2D] = None, @@ -122,7 +113,6 @@ def __init__( - The interferometer data, noise-map, uv-wavelengths and settings as .fits files (e.g. `dataset/data.fits`). - The real space mask defining the grid of the interferometer for the FFT (`dataset/real_space_mask.fits`). - - The settings of pixelization used by the fit (`dataset/settings_pixelization.json`). - The settings of inversions used by the fit (`dataset/settings_inversion.json`). The `aggregator` contains the path to each of these files, and they can be loaded individually. This class @@ -143,8 +133,6 @@ def __init__( A `PyAutoFit` aggregator object which can load the results of model-fits. settings_dataset Optionally overwrite the `SettingsInterferometer` of the `Interferometer` object that is created from the fit. - settings_pixelization - Optionally overwrite the `SettingsPixelization` of the `Pixelization` object that is created from the fit. settings_inversion Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit. use_preloaded_grid @@ -155,7 +143,6 @@ def __init__( super().__init__(aggregator=aggregator) self.settings_dataset = settings_dataset - self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.use_preloaded_grid = use_preloaded_grid self.real_space_mask = real_space_mask @@ -177,7 +164,6 @@ def object_via_gen_from(self, fit, galaxies) -> FitInterferometer: fit=fit, galaxies=galaxies, settings_dataset=self.settings_dataset, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, use_preloaded_grid=self.use_preloaded_grid, ) diff --git a/autolens/aggregator/subhalo.py b/autolens/aggregator/subhalo.py index 1b0420dac..f13874b39 100644 --- a/autolens/aggregator/subhalo.py +++ b/autolens/aggregator/subhalo.py @@ -11,7 +11,6 @@ def __init__( self, aggregator_grid_search: af.GridSearchAggregator, settings_dataset: Optional[aa.SettingsImaging] = None, - settings_pixelization: Optional[aa.SettingsPixelization] = None, settings_inversion: Optional[aa.SettingsInversion] = None, use_preloaded_grid: bool = True, ): @@ -22,7 +21,6 @@ def __init__( self.aggregator_grid_search = aggregator_grid_search self.settings_dataset = settings_dataset - self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.use_preloaded_grid = use_preloaded_grid diff --git a/autolens/analysis/analysis.py b/autolens/analysis/analysis.py index f1e1ab3f4..5b533ab04 100644 --- a/autolens/analysis/analysis.py +++ b/autolens/analysis/analysis.py @@ -156,7 +156,6 @@ def __init__( ] = None, adapt_result=None, cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), - settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, raise_inversion_positions_likelihood_exception: bool = True, ): @@ -179,9 +178,6 @@ def __init__( trace close to one another in the source-plane. cosmology The AstroPy Cosmology assumed for this analysis. - settings_pixelization - settings controlling how a pixelization is fitted during the model-fit, for example if a border is used - when creating the pixelization. settings_inversion Settings controlling how an inversion is fitted during the model-fit, for example which linear algebra formalism is used. @@ -196,7 +192,6 @@ def __init__( dataset=dataset, adapt_result=adapt_result, cosmology=cosmology, - settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, ) diff --git a/autolens/imaging/fit_imaging.py b/autolens/imaging/fit_imaging.py index e784cfc29..86133b97b 100644 --- a/autolens/imaging/fit_imaging.py +++ b/autolens/imaging/fit_imaging.py @@ -20,7 +20,6 @@ def __init__( self, dataset: aa.Imaging, tracer: Tracer, - settings_pixelization: aa.SettingsPixelization = aa.SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, @@ -55,9 +54,6 @@ def __init__( The imaging dataset which is fitted by the galaxies in the tracer. tracer The tracer of galaxies whose light profile images are used to fit the imaging data. - settings_pixelization - Settings controlling how a pixelization is fitted for example if a border is used when creating the - pixelization. settings_inversion Settings controlling how an inversion is fitted for example which linear algebra formalism is used. preloads @@ -75,7 +71,6 @@ def __init__( self.tracer = tracer - self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.preloads = preloads @@ -114,7 +109,6 @@ def tracer_to_inversion(self) -> TracerToInversion: data=self.profile_subtracted_image, noise_map=self.noise_map, w_tilde=self.w_tilde, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, preloads=self.preloads, ) @@ -319,7 +313,6 @@ def refit_with_new_preloads( return FitImaging( dataset=self.dataset, tracer=self.tracer, - settings_pixelization=self.settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, run_time_dict=run_time_dict, diff --git a/autolens/imaging/model/analysis.py b/autolens/imaging/model/analysis.py index 438c0cf9f..afd4e3dc2 100644 --- a/autolens/imaging/model/analysis.py +++ b/autolens/imaging/model/analysis.py @@ -194,7 +194,6 @@ def fit_imaging_via_tracer_from( return FitImaging( dataset=self.dataset, tracer=tracer, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, preloads=preloads, run_time_dict=run_time_dict, diff --git a/autolens/interferometer/fit_interferometer.py b/autolens/interferometer/fit_interferometer.py index fc53c87ae..f89da34af 100644 --- a/autolens/interferometer/fit_interferometer.py +++ b/autolens/interferometer/fit_interferometer.py @@ -18,7 +18,6 @@ def __init__( self, dataset: aa.Interferometer, tracer: Tracer, - settings_pixelization: aa.SettingsPixelization = aa.SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, @@ -54,9 +53,6 @@ def __init__( The interforometer dataset which is fitted by the galaxies in the tracer. tracer The tracer of galaxies whose light profile images are used to fit the interferometer data. - settings_pixelization - Settings controlling how a pixelization is fitted for example if a border is used when creating the - pixelization. settings_inversion Settings controlling how an inversion is fitted for example which linear algebra formalism is used. preloads @@ -74,7 +70,6 @@ def __init__( self.tracer = tracer - self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.preloads = preloads @@ -112,7 +107,6 @@ def tracer_to_inversion(self) -> TracerToInversion: data=self.profile_subtracted_visibilities, noise_map=self.noise_map, w_tilde=self.w_tilde, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, preloads=self.preloads, ) @@ -268,7 +262,6 @@ def refit_with_new_preloads( return FitInterferometer( dataset=self.interferometer, tracer=self.tracer, - settings_pixelization=self.settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, run_time_dict=run_time_dict, diff --git a/autolens/interferometer/model/analysis.py b/autolens/interferometer/model/analysis.py index 8af7d2ffd..fa625a51c 100644 --- a/autolens/interferometer/model/analysis.py +++ b/autolens/interferometer/model/analysis.py @@ -35,7 +35,6 @@ def __init__( ] = None, adapt_result=None, cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(), - settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, raise_inversion_positions_likelihood_exception: bool = True, ): @@ -68,9 +67,6 @@ def __init__( used by certain classes for adapting the analysis to the properties of the dataset. cosmology The Cosmology assumed for this analysis. - settings_pixelization - settings controlling how a pixelization is fitted for example if a border is used when creating the - pixelization. settings_inversion Settings controlling how an inversion is fitted, for example which linear algebra formalism is used. raise_inversion_positions_likelihood_exception @@ -84,7 +80,6 @@ def __init__( positions_likelihood=positions_likelihood, adapt_result=adapt_result, cosmology=cosmology, - settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, raise_inversion_positions_likelihood_exception=raise_inversion_positions_likelihood_exception, ) @@ -256,7 +251,6 @@ def fit_interferometer_via_tracer_from( return FitInterferometer( dataset=self.dataset, tracer=tracer, - settings_pixelization=self.settings_pixelization, settings_inversion=self.settings_inversion, preloads=preloads, run_time_dict=run_time_dict, diff --git a/autolens/lens/to_inversion.py b/autolens/lens/to_inversion.py index cfc103d31..a1f2aa595 100644 --- a/autolens/lens/to_inversion.py +++ b/autolens/lens/to_inversion.py @@ -18,7 +18,6 @@ def __init__( data: Optional[Union[aa.Array2D, aa.Visibilities]] = None, noise_map: Optional[Union[aa.Array2D, aa.VisibilitiesNoiseMap]] = None, w_tilde: Optional[Union[aa.WTildeImaging, aa.WTildeInterferometer]] = None, - settings_pixelization=aa.SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads=Preloads(), run_time_dict: Optional[Dict] = None, @@ -30,7 +29,6 @@ def __init__( data=data, noise_map=noise_map, w_tilde=w_tilde, - settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, run_time_dict=run_time_dict, @@ -111,7 +109,6 @@ def image_plane_mesh_grid_pg_list(self) -> List[List]: plane_to_inversion = ag.PlaneToInversion( plane=plane, grid_pixelization=self.dataset.grid, - settings_pixelization=self.settings_pixelization, noise_map=self.noise_map, ) @@ -183,7 +180,6 @@ def mapper_galaxy_dict(self) -> Dict[aa.AbstractMapper, ag.Galaxy]: plane_to_inversion = ag.PlaneToInversion( plane=plane, grid_pixelization=traced_grids_of_planes_list[plane_index], - settings_pixelization=self.settings_pixelization, preloads=self.preloads, noise_map=self.noise_map, ) From 6fd8e7a1c7e8c9703a360b4bd8a5fa7591d4b72d Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 12:55:47 +0000 Subject: [PATCH 12/13] test config disables relocate_pix_border by default --- test_autolens/analysis/test_analysis.py | 5 ++--- test_autolens/config/general.yaml | 1 + .../interferometer/test_simulate_and_fit_interferometer.py | 5 ----- test_autolens/lens/test_to_inversion.py | 4 ---- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/test_autolens/analysis/test_analysis.py b/test_autolens/analysis/test_analysis.py index 3d632df5a..4e863a4a2 100644 --- a/test_autolens/analysis/test_analysis.py +++ b/test_autolens/analysis/test_analysis.py @@ -78,7 +78,7 @@ def test__tracer_for_instance__subhalo_redshift_rescale_used(analysis_imaging_7x assert tracer.galaxies[1].mass.centre == pytest.approx((-0.19959, -0.39919), 1.0e-4) -def test__use_border__determines_if_border_pixel_relocation_is_used(masked_imaging_7x7): +def test__relocate_pix_border__determines_if_border_pixel_relocation_is_used(masked_imaging_7x7): pixelization = al.Pixelization( mesh=al.mesh.Rectangular(shape=(3, 3)), regularization=al.reg.Constant(coefficient=1.0), @@ -99,7 +99,7 @@ def test__use_border__determines_if_border_pixel_relocation_is_used(masked_imagi analysis = al.AnalysisImaging( dataset=masked_imaging_7x7, - settings_pixelization=al.SettingsPixelization(use_border=True), + settings_inversion=al.SettingsInversion(relocate_pix_border=True), ) analysis.dataset.grid_pixelization[4] = np.array([[500.0, 0.0]]) @@ -119,7 +119,6 @@ def test__use_border__determines_if_border_pixel_relocation_is_used(masked_imagi analysis = al.AnalysisImaging( dataset=masked_imaging_7x7, - settings_pixelization=al.SettingsPixelization(use_border=False), ) analysis.dataset.grid_pixelization[4] = np.array([300.0, 0.0]) diff --git a/test_autolens/config/general.yaml b/test_autolens/config/general.yaml index 824254fa0..3a7754f0c 100644 --- a/test_autolens/config/general.yaml +++ b/test_autolens/config/general.yaml @@ -16,6 +16,7 @@ inversion: use_positive_only_solver: false # If True, inversion's use a positive-only linear algebra solver by default, which is slower but prevents unphysical negative values in the reconstructed solutuion. no_regularization_add_to_curvature_diag_value: 1.0e-8 # The default value added to the curvature matrix's diagonal when regularization is not applied to a linear object, which prevents inversion's failing due to the matrix being singular. positive_only_uses_p_initial: false # If True, the positive-only solver of an inversion's uses an initial guess of the reconstructed data's values as which values should be positive, speeding up the solver. + relocate_pix_border: false # If True, by default a pixelization's border is used to relocate all pixels outside its border to the border. model: ignore_prior_limits: true numba: diff --git a/test_autolens/interferometer/test_simulate_and_fit_interferometer.py b/test_autolens/interferometer/test_simulate_and_fit_interferometer.py index 47bdce97b..15af413e7 100644 --- a/test_autolens/interferometer/test_simulate_and_fit_interferometer.py +++ b/test_autolens/interferometer/test_simulate_and_fit_interferometer.py @@ -71,7 +71,6 @@ def test__perfect_fit__chi_squared_0(): fit = al.FitInterferometer( dataset=dataset, tracer=tracer, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -95,7 +94,6 @@ def test__perfect_fit__chi_squared_0(): fit = al.FitInterferometer( dataset=dataset, tracer=tracer, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) assert abs(fit.chi_squared) < 1.0e-4 @@ -194,7 +192,6 @@ def test__simulate_interferometer_data_and_fit__linear_light_profiles_agree_with fit = al.FitInterferometer( dataset=dataset, tracer=tracer, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -217,7 +214,6 @@ def test__simulate_interferometer_data_and_fit__linear_light_profiles_agree_with fit_linear = al.FitInterferometer( dataset=dataset, tracer=tracer_linear, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -324,7 +320,6 @@ def test__simulate_interferometer_data_and_fit__linear_light_profiles_and_pixeli fit_linear = al.FitInterferometer( dataset=dataset, tracer=tracer_linear, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) diff --git a/test_autolens/lens/test_to_inversion.py b/test_autolens/lens/test_to_inversion.py index 10cf38612..b76009dea 100644 --- a/test_autolens/lens/test_to_inversion.py +++ b/test_autolens/lens/test_to_inversion.py @@ -448,7 +448,6 @@ def test__inversion_imaging_from(sub_grid_2d_7x7, masked_imaging_7x7): data=masked_imaging_7x7.data, noise_map=masked_imaging_7x7.noise_map, w_tilde=masked_imaging_7x7.w_tilde, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -471,7 +470,6 @@ def test__inversion_imaging_from(sub_grid_2d_7x7, masked_imaging_7x7): data=masked_imaging_7x7.data, noise_map=masked_imaging_7x7.noise_map, w_tilde=masked_imaging_7x7.w_tilde, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -495,7 +493,6 @@ def test__inversion_interferometer_from(sub_grid_2d_7x7, interferometer_7): data=interferometer_7.visibilities, noise_map=interferometer_7.noise_map, w_tilde=None, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) @@ -518,7 +515,6 @@ def test__inversion_interferometer_from(sub_grid_2d_7x7, interferometer_7): data=interferometer_7.visibilities, noise_map=interferometer_7.noise_map, w_tilde=None, - settings_pixelization=al.SettingsPixelization(use_border=False), settings_inversion=al.SettingsInversion(use_w_tilde=False), ) From cdd83f1aff93c468a222e2149c455ad6bce52976 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Wed, 20 Dec 2023 13:07:34 +0000 Subject: [PATCH 13/13] settings inversio passed to PLaneToInversion --- autolens/lens/to_inversion.py | 3 +++ test_autolens/analysis/test_analysis.py | 1 + test_autolens/config/general.yaml | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/autolens/lens/to_inversion.py b/autolens/lens/to_inversion.py index a1f2aa595..ba0be98c4 100644 --- a/autolens/lens/to_inversion.py +++ b/autolens/lens/to_inversion.py @@ -72,6 +72,7 @@ def lp_linear_func_list_galaxy_dict( grid=traced_grids_of_planes_list[plane_index], blurring_grid=traced_blurring_grids_of_planes_list[plane_index], noise_map=self.noise_map, + settings_inversion=self.settings_inversion, ) lp_linear_galaxy_dict_of_plane = ( @@ -110,6 +111,7 @@ def image_plane_mesh_grid_pg_list(self) -> List[List]: plane=plane, grid_pixelization=self.dataset.grid, noise_map=self.noise_map, + settings_inversion=self.settings_inversion, ) image_plane_mesh_grid_list = plane_to_inversion.image_plane_mesh_grid_list @@ -182,6 +184,7 @@ def mapper_galaxy_dict(self) -> Dict[aa.AbstractMapper, ag.Galaxy]: grid_pixelization=traced_grids_of_planes_list[plane_index], preloads=self.preloads, noise_map=self.noise_map, + settings_inversion=self.settings_inversion, ) galaxies_with_pixelization_list = plane.galaxies_with_cls_list_from( diff --git a/test_autolens/analysis/test_analysis.py b/test_autolens/analysis/test_analysis.py index 4e863a4a2..20be17c29 100644 --- a/test_autolens/analysis/test_analysis.py +++ b/test_autolens/analysis/test_analysis.py @@ -119,6 +119,7 @@ def test__relocate_pix_border__determines_if_border_pixel_relocation_is_used(mas analysis = al.AnalysisImaging( dataset=masked_imaging_7x7, + settings_inversion=al.SettingsInversion(relocate_pix_border=False), ) analysis.dataset.grid_pixelization[4] = np.array([300.0, 0.0]) diff --git a/test_autolens/config/general.yaml b/test_autolens/config/general.yaml index 3a7754f0c..824254fa0 100644 --- a/test_autolens/config/general.yaml +++ b/test_autolens/config/general.yaml @@ -16,7 +16,6 @@ inversion: use_positive_only_solver: false # If True, inversion's use a positive-only linear algebra solver by default, which is slower but prevents unphysical negative values in the reconstructed solutuion. no_regularization_add_to_curvature_diag_value: 1.0e-8 # The default value added to the curvature matrix's diagonal when regularization is not applied to a linear object, which prevents inversion's failing due to the matrix being singular. positive_only_uses_p_initial: false # If True, the positive-only solver of an inversion's uses an initial guess of the reconstructed data's values as which values should be positive, speeding up the solver. - relocate_pix_border: false # If True, by default a pixelization's border is used to relocate all pixels outside its border to the border. model: ignore_prior_limits: true numba: