Skip to content

Commit 0ea7c60

Browse files
Jammy2211Jammy2211
authored andcommitted
xp refactor complete
1 parent 1a6e388 commit 0ea7c60

File tree

29 files changed

+122
-227
lines changed

29 files changed

+122
-227
lines changed

autolens/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from autoarray.operators.transformer import TransformerDFT
3030
from autoarray.operators.transformer import TransformerNUFFT
3131
from autoarray.preloads import Preloads
32+
from autoarray.preloads import mapper_indices_from
3233
from autoarray.structures.arrays.uniform_1d import Array1D
3334
from autoarray.structures.arrays.uniform_2d import Array2D
3435
from autoarray.structures.arrays.rgb import Array2DRGB
@@ -115,8 +116,6 @@
115116
from .quantity.fit_quantity import FitQuantity
116117
from .quantity.model.analysis import AnalysisQuantity
117118

118-
from .analysis.preloads import mapper_indices_from
119-
120119
from . import exc
121120
from . import mock as m
122121
from . import util

autolens/analysis/analysis/lens.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import jax.numpy as jnp
21
import logging
32
import numpy as np
43
from typing import List, Optional

autolens/analysis/model_util.py

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,7 @@
1-
import numpy as np
2-
from typing import Optional, Tuple
3-
41
import autofit as af
52
import autolens as al
63

7-
8-
def mge_model_from(
9-
mask_radius: float,
10-
total_gaussians: int = 30,
11-
gaussian_per_basis: int = 1,
12-
centre_prior_is_uniform: bool = True,
13-
centre: Tuple[float, float] = (0.0, 0.0),
14-
centre_fixed: Optional[Tuple[float, float]] = None,
15-
use_spherical: bool = False,
16-
) -> af.Collection:
17-
"""
18-
Construct a Multi-Gaussian Expansion (MGE) for the lens or source galaxy light
19-
20-
This model is designed as a "start here" configuration for lens modeling:
21-
22-
- The lens and source light are represented by a Basis object composed of many
23-
Gaussian light profiles with fixed logarithmically spaced widths (`sigma`).
24-
- All Gaussians within each basis share common centres and ellipticity
25-
components, reducing degeneracy while retaining flexibility.
26-
27-
- Users can combine with a lens mass model of their choiuce.
28-
29-
The resulting model provides a good balance of speed, flexibility, and accuracy
30-
for fitting most galaxy-scale strong lenses.
31-
32-
This code is mostly to make the API simple for new users, hiding the technical
33-
details of setting up an MGE. More advanced users may wish to customize the
34-
model further.
35-
36-
Parameters
37-
----------
38-
mask_radius
39-
The outer radius (in arcseconds) of the circular mask applied to the data.
40-
This determines the maximum Gaussian width (`sigma`) used in the lens MGE.
41-
lens_total_gaussians
42-
Total number of Gaussian light profiles used in the lens MGE basis.
43-
source_total_gaussians
44-
Total number of Gaussian light profiles used in the source MGE basis.
45-
lens_gaussian_per_basis
46-
Number of separate Gaussian bases to include for the lens light profile.
47-
Each basis has `lens_total_gaussians` components.
48-
source_gaussian_per_basis
49-
Number of separate Gaussian bases to include for the source light profile.
50-
Each basis has `source_total_gaussians` components.
51-
52-
Returns
53-
-------
54-
model : af.Collection
55-
An `autofit.Collection` containing:
56-
- A lens galaxy at redshift 0.5, with:
57-
* bulge light profile: MGE basis of Gaussians
58-
* mass profile: Isothermal ellipsoid
59-
* external shear
60-
- A source galaxy at redshift 1.0, with:
61-
* bulge light profile: MGE basis of Gaussians
62-
63-
Notes
64-
-----
65-
- Lens light Gaussians have widths (sigma) logarithmically spaced between 0.01"
66-
and the mask radius.
67-
- Source light Gaussians have widths logarithmically spaced between 0.01" and 1.0".
68-
- Gaussian centres are free parameters but tied across all components in each
69-
basis to reduce dimensionality.
70-
- This function is a convenience utility: it hides the technical setup of MGE
71-
composition and provides a ready-to-use lens model for quick experimentation.
72-
"""
73-
74-
# The sigma values of the Gaussians will be fixed to values spanning 0.01 to the mask radius, 3.0".
75-
log10_sigma_list = np.linspace(-4, np.log10(mask_radius), total_gaussians)
76-
77-
# By defining the centre here, it creates two free parameters that are assigned below to all Gaussians.
78-
79-
if centre_fixed is not None:
80-
centre_0 = centre[0]
81-
centre_1 = centre[1]
82-
elif centre_prior_is_uniform:
83-
centre_0 = af.UniformPrior(
84-
lower_limit=centre[0] - 0.1, upper_limit=centre[0] + 0.1
85-
)
86-
centre_1 = af.UniformPrior(
87-
lower_limit=centre[1] - 0.1, upper_limit=centre[1] + 0.1
88-
)
89-
else:
90-
centre_0 = af.GaussianPrior(mean=centre[0], sigma=0.3)
91-
centre_1 = af.GaussianPrior(mean=centre[1], sigma=0.3)
92-
93-
if use_spherical:
94-
model_cls = al.lp_linear.GaussianSph
95-
else:
96-
model_cls = al.lp_linear.Gaussian
97-
98-
bulge_gaussian_list = []
99-
100-
for j in range(gaussian_per_basis):
101-
# A list of Gaussian model components whose parameters are customized belows.
102-
103-
gaussian_list = af.Collection(
104-
af.Model(model_cls) for _ in range(total_gaussians)
105-
)
106-
107-
# Iterate over every Gaussian and customize its parameters.
108-
109-
for i, gaussian in enumerate(gaussian_list):
110-
gaussian.centre.centre_0 = centre_0 # All Gaussians have same y centre.
111-
gaussian.centre.centre_1 = centre_1 # All Gaussians have same x centre.
112-
if not use_spherical:
113-
gaussian.ell_comps = gaussian_list[
114-
0
115-
].ell_comps # All Gaussians have same elliptical components.
116-
gaussian.sigma = (
117-
10 ** log10_sigma_list[i]
118-
) # All Gaussian sigmas are fixed to values above.
119-
120-
bulge_gaussian_list += gaussian_list
121-
122-
# The Basis object groups many light profiles together into a single model component.
123-
124-
return af.Model(
125-
al.lp_basis.Basis,
126-
profile_list=bulge_gaussian_list,
127-
)
4+
from autogalaxy.analysis.model_util import mge_model_from
1285

1296

1307
def simulator_start_here_model_from(

autolens/analysis/positions.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import jax
2-
import jax.numpy as jnp
32
import numpy as np
4-
from typing import Optional, Union
3+
from typing import Optional
54
from os import path
6-
import os
7-
from typing import Dict
85

96
import autoarray as aa
107
import autofit as af

autolens/analysis/preloads.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

autolens/config/visualize/plots.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,3 @@ fit_quantity: # Settings for plots of fit quantitie
6666
galaxies: # Settings for plots of galaxies (e.g. GalaxiesPlotter).
6767
subplot_galaxies: true # Plot subplot of all quantities in each galaxies group (e.g. images, convergence)?
6868
subplot_galaxy_images: false # Plot subplot of the image of each galaxy in the model?
69-
subplot_galaxies_1d: false # Plot subplot of all quantities in 1D of each galaxies group (e.g. images, convergence)?
70-
subplot_galaxies_1d_decomposed: false # Plot subplot of all quantities in 1D decomposed of each galaxies group (e.g. images, convergence)?

autolens/imaging/fit_imaging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ def blurred_image(self) -> aa.Array2D:
8787
):
8888
return self.tracer.image_2d_from(
8989
grid=self.grids.lp,
90+
xp=self._xp,
9091
)
9192

9293
return self.tracer.blurred_image_2d_from(
9394
grid=self.grids.lp,
9495
psf=self.dataset.psf,
9596
blurring_grid=self.grids.blurring,
97+
xp=self._xp,
9698
)
9799

98100
@property
@@ -119,7 +121,7 @@ def tracer_to_inversion(self) -> TracerToInversion:
119121
adapt_images=self.adapt_images,
120122
settings_inversion=self.settings_inversion,
121123
preloads=self.preloads,
122-
xp=self.xp,
124+
xp=self._xp,
123125
)
124126

125127
@cached_property

autolens/interferometer/fit_interferometer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(
2121
adapt_images: Optional[ag.AdaptImages] = None,
2222
settings_inversion: aa.SettingsInversion = aa.SettingsInversion(),
2323
preloads: aa.Preloads = None,
24+
xp=np,
2425
):
2526
"""
2627
Fits an interferometer dataset using a `Tracer` object.
@@ -76,12 +77,14 @@ def __init__(
7677
super().__init__(
7778
dataset=dataset,
7879
dataset_model=dataset_model,
80+
xp=xp,
7981
)
8082
AbstractFitInversion.__init__(
8183
self=self, model_obj=tracer, settings_inversion=settings_inversion
8284
)
8385

8486
self.preloads = preloads
87+
self._xp = xp
8588

8689
@property
8790
def profile_visibilities(self) -> aa.Visibilities:
@@ -90,7 +93,7 @@ def profile_visibilities(self) -> aa.Visibilities:
9093
transform to the sum of light profile images.
9194
"""
9295
return self.tracer.visibilities_from(
93-
grid=self.grids.lp, transformer=self.dataset.transformer
96+
grid=self.grids.lp, transformer=self.dataset.transformer, xp=self._xp
9497
)
9598

9699
@property
@@ -117,6 +120,7 @@ def tracer_to_inversion(self) -> TracerToInversion:
117120
adapt_images=self.adapt_images,
118121
settings_inversion=self.settings_inversion,
119122
preloads=self.preloads,
123+
xp=self._xp,
120124
)
121125

122126
@cached_property

autolens/interferometer/model/analysis.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
114114

115115
return self
116116

117-
def log_likelihood_function(self, instance):
117+
def log_likelihood_function(self, instance, xp=np):
118118
"""
119119
Given an instance of the model, where the model parameters are set via a non-linear search, fit the model
120120
instance to the interferometer dataset.
@@ -153,14 +153,16 @@ def log_likelihood_function(self, instance):
153153
The log likelihood indicating how well this model instance fitted the interferometer data.
154154
"""
155155

156-
log_likelihood_penalty = self.log_likelihood_penalty_from(instance=instance)
156+
log_likelihood_penalty = self.log_likelihood_penalty_from(
157+
instance=instance, xp=xp
158+
)
157159

158-
return self.fit_from(instance=instance).figure_of_merit - log_likelihood_penalty
160+
return (
161+
self.fit_from(instance=instance, xp=xp).figure_of_merit
162+
- log_likelihood_penalty
163+
)
159164

160-
def fit_from(
161-
self,
162-
instance: af.ModelInstance,
163-
) -> FitInterferometer:
165+
def fit_from(self, instance: af.ModelInstance, xp=np) -> FitInterferometer:
164166
"""
165167
Given a model instance create a `FitInterferometer` object.
166168
@@ -196,6 +198,7 @@ def fit_from(
196198
adapt_images=adapt_images,
197199
settings_inversion=self.settings_inversion,
198200
preloads=self.preloads,
201+
xp=xp,
199202
)
200203

201204
def save_attributes(self, paths: af.DirectoryPaths):

autolens/lens/mock/mock_tracer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import numpy as np
2+
3+
14
class MockTracer:
25
def __init__(
36
self, traced_grid_2d_list_from=None, image_plane_mesh_grid_pg_list=None
@@ -61,5 +64,5 @@ def einstein_radius_from(self, grid):
6164
def einstein_mass_angular_from(self, grid):
6265
return self.einstein_mass
6366

64-
def time_delays_from(self, grid):
67+
def time_delays_from(self, grid, xp=np):
6568
return self.time_delays

0 commit comments

Comments
 (0)