Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove settings pixelization #244

Merged
merged 13 commits into from
Dec 23, 2023
2 changes: 0 additions & 2 deletions autolens/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -83,7 +82,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
Expand Down
14 changes: 0 additions & 14 deletions autolens/aggregator/fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -92,7 +85,6 @@ def _fit_imaging_from(
FitImaging(
dataset=dataset,
tracer=tracer,
settings_pixelization=settings_pixelization,
settings_inversion=settings_inversion,
preloads=preloads,
)
Expand All @@ -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,
):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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,
)
14 changes: 0 additions & 14 deletions autolens/aggregator/fit_interferometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -95,7 +88,6 @@ def _fit_interferometer_from(
FitInterferometer(
dataset=dataset,
tracer=tracer,
settings_pixelization=settings_pixelization,
settings_inversion=settings_inversion,
preloads=preloads,
)
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
)
2 changes: 0 additions & 2 deletions autolens/aggregator/subhalo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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

Expand Down
123 changes: 0 additions & 123 deletions autolens/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -40,7 +39,6 @@ def __init__(
positions_likelihood: Optional[
Union[PositionsLHResample, PositionsLHPenalty]
] = None,
settings_lens: SettingsLens = SettingsLens(),
cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(),
):
"""
Expand All @@ -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(
Expand Down Expand Up @@ -162,9 +156,7 @@ def __init__(
] = None,
adapt_result=None,
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,
):
"""
Expand All @@ -186,15 +178,9 @@ 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.
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
Expand All @@ -206,19 +192,15 @@ def __init__(
dataset=dataset,
adapt_result=adapt_result,
cosmology=cosmology,
settings_pixelization=settings_pixelization,
settings_inversion=settings_inversion,
)

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 = (
Expand Down Expand Up @@ -297,9 +279,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
Expand Down Expand Up @@ -332,105 +311,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,
)
Loading
Loading