Skip to content

Commit 2bb02f2

Browse files
committed
Merge branch 'main' into feature/ellipse_utils
2 parents 8a6de24 + 6d6d2a2 commit 2bb02f2

61 files changed

Lines changed: 868 additions & 385 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

autogalaxy/__init__.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@
1010
from autoarray.dataset.imaging.dataset import Imaging # noqa
1111
from autoarray.dataset.interferometer.dataset import Interferometer # noqa
1212
from autoarray.dataset.dataset_model import DatasetModel
13-
from autoarray.inversion.pixelization import mesh # noqa
13+
from autoarray.inversion.mesh import mesh # noqa
1414
from autoarray.inversion import regularization as reg # noqa
15-
from autoarray.inversion.pixelization import image_mesh
16-
from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper # noqa
17-
from autoarray.inversion.inversion.settings import SettingsInversion # noqa
15+
from autoarray.inversion.mesh import image_mesh
16+
from autoarray.inversion.mappers.abstract import Mapper # noqa
17+
from autoarray.settings import Settings # noqa
1818
from autoarray.inversion.inversion.factory import inversion_from as Inversion # noqa
19-
from autoarray.inversion.pixelization.image_mesh.abstract import AbstractImageMesh
20-
from autoarray.inversion.pixelization.mesh.abstract import AbstractMesh
19+
from autoarray.inversion.mesh.image_mesh.abstract import AbstractImageMesh
20+
from autoarray.inversion.mesh.mesh.abstract import AbstractMesh
2121
from autoarray.inversion.regularization.abstract import AbstractRegularization
22-
from autoarray.inversion.pixelization.pixelization import Pixelization # noqa
23-
from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper
24-
from autoarray.inversion.pixelization.mappers.mapper_grids import MapperGrids # noqa
25-
from autoarray.inversion.pixelization.mappers.factory import (
26-
mapper_from as Mapper,
27-
) # noqa
28-
from autoarray.inversion.pixelization.border_relocator import BorderRelocator
22+
from autoarray.inversion.pixelization import Pixelization # noqa
23+
from autoarray.inversion.mesh.border_relocator import BorderRelocator
2924
from autoarray.preloads import Preloads
3025
from autoarray.preloads import mapper_indices_from
3126
from autoarray.mask.mask_1d import Mask1D # noqa
@@ -43,18 +38,22 @@
4338
from autoarray.structures.grids.uniform_2d import Grid2D # noqa
4439
from autoarray.structures.grids.irregular_2d import Grid2DIrregular # noqa
4540
from autoarray.operators.over_sampling.over_sampler import OverSampler # noqa
46-
from autoarray.structures.mesh.rectangular_2d import Mesh2DRectangular # noqa
47-
from autoarray.structures.mesh.rectangular_2d_uniform import (
48-
Mesh2DRectangularUniform,
41+
from autoarray.inversion.mesh.interpolator.rectangular import (
42+
InterpolatorRectangular,
43+
) # noqa
44+
from autoarray.inversion.mesh.interpolator.delaunay import (
45+
InterpolatorDelaunay,
4946
) # noqa
50-
from autoarray.structures.mesh.delaunay_2d import Mesh2DDelaunay # noqa
5147
from autoarray.structures.vectors.uniform import VectorYX2D # noqa
5248
from autoarray.structures.vectors.irregular import VectorYX2DIrregular # noqa
5349
from autoarray.layout.region import Region1D # noqa
5450
from autoarray.layout.region import Region2D # noqa
5551
from autoarray.structures.arrays.kernel_2d import Kernel2D # noqa
5652
from autoarray.structures.visibilities import Visibilities # noqa
5753
from autoarray.structures.visibilities import VisibilitiesNoiseMap # noqa
54+
from autoarray.inversion.mesh.mesh_geometry.rectangular import (
55+
rectangular_edge_pixel_list_from,
56+
)
5857

5958
from .analysis import model_util
6059
from .analysis.adapt_images.adapt_images import AdaptImages

autogalaxy/abstract_fit.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING, Dict, Optional
33

4+
import numpy as np
5+
46
from autofit import ModelInstance
57

68
if TYPE_CHECKING:
@@ -13,11 +15,7 @@
1315

1416

1517
class AbstractFitInversion:
16-
def __init__(
17-
self,
18-
model_obj,
19-
settings_inversion: aa.SettingsInversion,
20-
):
18+
def __init__(self, model_obj, settings: aa.Settings, xp=np):
2119
"""
2220
An abstract fit object which fits to datasets (e.g. imaging, interferometer) inherit from.
2321
@@ -30,11 +28,20 @@ def __init__(
3028
The object which contains the model components (e.g. light profiles, galaxies, etc) which are used to
3129
create the model-data that fits the data. In PyAutoGalaxy this is a list of galaxies and PyAutoLens
3230
it is a `Tracer`.
33-
settings_inversion
31+
settings
3432
Settings controlling how an inversion is fitted for example which linear algebra formalism is used.
3533
"""
3634
self.model_obj = model_obj
37-
self.settings_inversion = settings_inversion
35+
self.settings = settings or aa.Settings()
36+
self.use_jax = xp is not np
37+
38+
@property
39+
def _xp(self):
40+
if self.use_jax:
41+
import jax.numpy as jnp
42+
43+
return jnp
44+
return np
3845

3946
@property
4047
def total_mappers(self) -> int:
@@ -68,7 +75,7 @@ def perform_inversion(self) -> bool:
6875
def sparse_operator(self) -> Optional[aa.ImagingSparseOperator]:
6976
"""
7077
Only call the `sparse_operator` property of a dataset used to perform efficient linear algebra calculations if
71-
the SettingsInversion()` object has `use_sparse_operator=True`, to avoid unnecessary computation.
78+
the Settings()` object has `use_sparse_operator=True`, to avoid unnecessary computation.
7279
7380
Returns
7481
-------
@@ -95,6 +102,14 @@ def linear_light_profile_intensity_dict(
95102
96103
This function returns a dictionary which maps every linear light profile instance to its solved for
97104
`intensity` value in the inversion, so that the intensity value of every light profile can be accessed.
105+
106+
Type casting is complicated by JAX. When this function is used in a JAX.jit (e.g. computed latent varialbes)
107+
it requires the reconstruction values to be JAX arrays, but when it is used outside of JAX certain taks
108+
requires the reconstruction values to be floats.
109+
110+
An example of the latter is using a tracer inferred in one search to pass the solved for intensity values of
111+
linear light profiles to a subsequent search, for example setting up the intensities of the mass components
112+
of a light dark model.
98113
"""
99114

100115
if self.inversion is None:
@@ -110,12 +125,20 @@ def linear_light_profile_intensity_dict(
110125
reconstruction = self.inversion.reconstruction_dict[linear_obj_func]
111126

112127
for i, light_profile in enumerate(linear_obj_func.light_profile_list):
113-
linear_light_profile_intensity_dict[light_profile] = reconstruction[i]
128+
if self.use_jax:
129+
linear_light_profile_intensity_dict[light_profile] = reconstruction[
130+
i
131+
]
132+
else:
133+
linear_light_profile_intensity_dict[light_profile] = float(
134+
reconstruction[i]
135+
)
114136

115137
return linear_light_profile_intensity_dict
116138

117139
def galaxy_linear_obj_data_dict_from(
118-
self, use_image: bool = False
140+
self,
141+
use_operated: bool = True,
119142
) -> Dict[Galaxy, aa.Array2D]:
120143
"""
121144
Returns a dictionary mapping every galaxy containing a linear
@@ -127,16 +150,17 @@ def galaxy_linear_obj_data_dict_from(
127150
This is used to create the overall `galaxy_model_image_dict`, which maps every galaxy to its
128151
overall `model_data` (e.g. including the `model_data` of orindary light profiles too).
129152
130-
If `use_image=False`, the `reconstructed_data` of the inversion (e.g. an image for dataset data,
153+
If `use_operated=False`, the `reconstructed_data` of the inversion (e.g. an image for dataset data,
131154
visibilities for interferometer data) is input in the dictionary.
132155
133-
if `use_image=True`, the `reconstructed_image` of the inversion (e.g. the image for dataset data, the
156+
if `use_operated=True`, the `reconstructed_operated_data` of the inversion (e.g. the image for dataset data, the
134157
real-space image for interferometer data) is input in the dictionary.
135158
136159
Parameters
137160
----------
138-
use_image
139-
Whether to put the reconstructed data or images in the dictionary.
161+
use_operated
162+
Whether to use the operated (e.g PSF convolved) images of the linear objects in the dictionary, or
163+
the unoperated images.
140164
141165
Returns
142166
-------
@@ -154,13 +178,12 @@ def galaxy_linear_obj_data_dict_from(
154178
except KeyError:
155179
continue
156180

157-
if not use_image:
158-
mapped_reconstructed = self.inversion.mapped_reconstructed_data_dict[
159-
linear_obj
160-
]
161-
181+
if use_operated:
182+
mapped_reconstructed = (
183+
self.inversion.mapped_reconstructed_operated_data_dict[linear_obj]
184+
)
162185
else:
163-
mapped_reconstructed = self.inversion.mapped_reconstructed_image_dict[
186+
mapped_reconstructed = self.inversion.mapped_reconstructed_data_dict[
164187
linear_obj
165188
]
166189

autogalaxy/aggregator/ellipse/fit_ellipse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _fit_ellipse_from(
3333
is instead used to load lists of the data, noise-map, PSF and mask and combine them into a list of
3434
`FitEllipse` objects.
3535
36-
The settings of an inversion can be overwritten by inputting a `settings_inversion` object, for example
36+
The settings of an inversion can be overwritten by inputting a `settings` object, for example
3737
if you want to use a grid with a different inversion solver.
3838
3939
Parameters

autogalaxy/aggregator/imaging/fit_imaging.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def _fit_imaging_from(
1717
fit: af.Fit,
1818
instance: Optional[af.ModelInstance] = None,
19-
settings_inversion: aa.SettingsInversion = None,
19+
settings: aa.Settings = None,
2020
) -> List[FitImaging]:
2121
"""
2222
Returns a list of `FitImaging` objects from a `PyAutoFit` loaded directory `Fit` or sqlite database `Fit` object.
@@ -26,7 +26,7 @@ def _fit_imaging_from(
2626
2727
- The imaging data, noise-map, PSF and settings as .fits files (e.g. `dataset/data.fits`).
2828
- The mask used to mask the `Imaging` data structure in the fit (`dataset.fits[hdu=0]`).
29-
- The settings of inversions used by the fit (`dataset/settings_inversion.json`).
29+
- The settings of inversions used by the fit (`dataset/settings.json`).
3030
3131
Each individual attribute can be loaded from the database via the `fit.value()` method.
3232
@@ -37,7 +37,7 @@ def _fit_imaging_from(
3737
is instead used to load lists of the data, noise-map, PSF and mask and combine them into a list of
3838
`FitImaging` objects.
3939
40-
The settings of an inversion can be overwritten by inputting a `settings_inversion` object, for example
40+
The settings of an inversion can be overwritten by inputting a `settings` object, for example
4141
if you want to use a grid with a different inversion solver.
4242
4343
Parameters
@@ -48,8 +48,8 @@ def _fit_imaging_from(
4848
instance
4949
A manual instance that overwrites the max log likelihood instance in fit (e.g. for drawing the instance
5050
randomly from the PDF).
51-
settings_inversion
52-
Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit.
51+
settings
52+
Optionally overwrite the `Settings` of the `Inversion` object that is created from the fit.
5353
"""
5454

5555
from autogalaxy.imaging.fit_imaging import FitImaging
@@ -62,7 +62,7 @@ def _fit_imaging_from(
6262

6363
adapt_images_list = agg_util.adapt_images_from(fit=fit)
6464

65-
settings_inversion = settings_inversion or fit.value(name="settings_inversion")
65+
settings = settings or fit.value(name="settings")
6666

6767
fit_dataset_list = []
6868

@@ -79,7 +79,7 @@ def _fit_imaging_from(
7979
galaxies=galaxies,
8080
dataset_model=dataset_model,
8181
adapt_images=adapt_images,
82-
settings_inversion=settings_inversion,
82+
settings=settings,
8383
)
8484
)
8585

@@ -90,7 +90,7 @@ class FitImagingAgg(af.AggBase):
9090
def __init__(
9191
self,
9292
aggregator: af.Aggregator,
93-
settings_inversion: Optional[aa.SettingsInversion] = None,
93+
settings: Optional[aa.Settings] = None,
9494
):
9595
"""
9696
Interfaces with an `PyAutoFit` aggregator object to create instances of `FitImaging` objects from the results
@@ -101,7 +101,7 @@ def __init__(
101101
102102
- The imaging data, noise-map, PSF and settings as .fits files (e.g. `dataset/data.fits`).
103103
- The mask used to mask the `Imaging` data structure in the fit (`dataset.fits[hdu=0]`).
104-
- The settings of inversions used by the fit (`dataset/settings_inversion.json`).
104+
- The settings of inversions used by the fit (`dataset/settings.json`).
105105
106106
The `aggregator` contains the path to each of these files, and they can be loaded individually. This class
107107
can load them all at once and create an `FitImaging` object via the `_fit_imaging_from` method.
@@ -123,19 +123,19 @@ def __init__(
123123
----------
124124
aggregator
125125
A `PyAutoFit` aggregator object which can load the results of model-fits.
126-
settings_inversion
127-
Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit.
126+
settings
127+
Optionally overwrite the `Settings` of the `Inversion` object that is created from the fit.
128128
129129
Parameters
130130
----------
131131
aggregator
132132
A `PyAutoFit` aggregator object which can load the results of model-fits.
133-
settings_inversion
134-
Optionally overwrite the `SettingsInversion` of the `Inversion` object that is created from the fit.
133+
settings
134+
Optionally overwrite the `Settings` of the `Inversion` object that is created from the fit.
135135
"""
136136
super().__init__(aggregator=aggregator)
137137

138-
self.settings_inversion = settings_inversion
138+
self.settings = settings
139139

140140
def object_via_gen_from(
141141
self, fit, instance: Optional[af.ModelInstance] = None
@@ -157,5 +157,5 @@ def object_via_gen_from(
157157
return _fit_imaging_from(
158158
fit=fit,
159159
instance=instance,
160-
settings_inversion=self.settings_inversion,
160+
settings=self.settings,
161161
)

0 commit comments

Comments
 (0)