Skip to content

Commit 00a5301

Browse files
Jammy2211Jammy2211
authored andcommitted
moved jnps remove
1 parent cf86118 commit 00a5301

18 files changed

Lines changed: 342 additions & 174 deletions

File tree

autoarray/fit/fit_dataset.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def chi_squared(self) -> float:
8585
"""
8686
Returns the chi-squared terms of the model data's fit to an dataset, by summing the chi-squared-map.
8787
"""
88-
return fit_util.chi_squared_from(chi_squared_map=self.chi_squared_map.array)
88+
return fit_util.chi_squared_from(chi_squared_map=self.chi_squared_map.array, xp=self.xp)
8989

9090
@property
9191
def noise_normalization(self) -> float:
@@ -94,7 +94,7 @@ def noise_normalization(self) -> float:
9494
9595
[Noise_Term] = sum(log(2*pi*[Noise]**2.0))
9696
"""
97-
return fit_util.noise_normalization_from(noise_map=self.noise_map.array)
97+
return fit_util.noise_normalization_from(noise_map=self.noise_map.array, xp=self.xp)
9898

9999
@property
100100
def log_likelihood(self) -> float:
@@ -115,6 +115,7 @@ def __init__(
115115
dataset,
116116
use_mask_in_fit: bool = False,
117117
dataset_model: DatasetModel = None,
118+
xp=np
118119
):
119120
"""Class to fit a masked dataset where the dataset's data structures are any dimension.
120121
@@ -147,6 +148,7 @@ def __init__(
147148
self.dataset = dataset
148149
self.use_mask_in_fit = use_mask_in_fit
149150
self.dataset_model = dataset_model or DatasetModel()
151+
self.xp = xp
150152

151153
@property
152154
def mask(self) -> Mask2D:
@@ -196,7 +198,7 @@ def residual_map(self) -> ty.DataLike:
196198

197199
if self.use_mask_in_fit:
198200
return fit_util.residual_map_with_mask_from(
199-
data=self.data, model_data=self.model_data, mask=self.mask
201+
data=self.data, model_data=self.model_data, mask=self.mask, xp=self.xp
200202
)
201203
return super().residual_map
202204

@@ -209,7 +211,7 @@ def normalized_residual_map(self) -> ty.DataLike:
209211
"""
210212
if self.use_mask_in_fit:
211213
return fit_util.normalized_residual_map_with_mask_from(
212-
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask
214+
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self.xp
213215
)
214216
return super().normalized_residual_map
215217

@@ -222,7 +224,7 @@ def chi_squared_map(self) -> ty.DataLike:
222224
"""
223225
if self.use_mask_in_fit:
224226
return fit_util.chi_squared_map_with_mask_from(
225-
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask
227+
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self.xp
226228
)
227229
return super().chi_squared_map
228230

@@ -243,7 +245,7 @@ def chi_squared(self) -> float:
243245

244246
if self.use_mask_in_fit:
245247
return fit_util.chi_squared_with_mask_from(
246-
chi_squared_map=self.chi_squared_map, mask=self.mask
248+
chi_squared_map=self.chi_squared_map, mask=self.mask, xp=self.xp
247249
)
248250
return super().chi_squared
249251

@@ -256,7 +258,7 @@ def noise_normalization(self) -> float:
256258
"""
257259
if self.use_mask_in_fit:
258260
return fit_util.noise_normalization_with_mask_from(
259-
noise_map=self.noise_map, mask=self.mask
261+
noise_map=self.noise_map, mask=self.mask, xp=self.xp
260262
)
261263
return super().noise_normalization
262264

autoarray/fit/fit_imaging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional
1+
import numpy as np
22

33
from autoarray.dataset.imaging.dataset import Imaging
44
from autoarray.dataset.dataset_model import DatasetModel
@@ -14,6 +14,7 @@ def __init__(
1414
dataset: Imaging,
1515
use_mask_in_fit: bool = False,
1616
dataset_model: DatasetModel = None,
17+
xp=np
1718
):
1819
"""
1920
Class to fit a masked imaging dataset.
@@ -49,6 +50,7 @@ def __init__(
4950
dataset=dataset,
5051
use_mask_in_fit=use_mask_in_fit,
5152
dataset_model=dataset_model,
53+
xp=xp
5254
)
5355

5456
@property

autoarray/fit/fit_util.py

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from functools import wraps
2-
import jax.numpy as jnp
32
import numpy as np
43

54
from autoarray.mask.abstract_mask import Mask
@@ -75,7 +74,7 @@ def chi_squared_map_from(
7574
return (residual_map / noise_map) ** 2.0
7675

7776

78-
def chi_squared_from(*, chi_squared_map: ty.DataLike) -> float:
77+
def chi_squared_from(*, chi_squared_map: ty.DataLike, xp=np) -> float:
7978
"""
8079
Returns the chi-squared terms of a model data's fit to an dataset, by summing the chi-squared-map.
8180
@@ -84,10 +83,10 @@ def chi_squared_from(*, chi_squared_map: ty.DataLike) -> float:
8483
chi_squared_map
8584
The chi-squared-map of values of the model-data fit to the dataset.
8685
"""
87-
return jnp.sum(chi_squared_map)
86+
return xp.sum(chi_squared_map)
8887

8988

90-
def noise_normalization_from(*, noise_map: ty.DataLike) -> float:
89+
def noise_normalization_from(*, noise_map: ty.DataLike, xp=np) -> float:
9190
"""
9291
Returns the noise-map normalization term of the noise-map, summing the noise_map value in every pixel as:
9392
@@ -98,12 +97,12 @@ def noise_normalization_from(*, noise_map: ty.DataLike) -> float:
9897
noise_map
9998
The masked noise-map of the dataset.
10099
"""
101-
return jnp.sum(jnp.log(2 * jnp.pi * noise_map**2.0))
100+
return xp.sum(xp.log(2 * xp.pi * noise_map**2.0))
102101

103102

104103
def normalized_residual_map_complex_from(
105-
*, residual_map: jnp.ndarray, noise_map: jnp.ndarray
106-
) -> jnp.ndarray:
104+
*, residual_map: np.ndarray, noise_map: np.ndarray
105+
) -> np.ndarray:
107106
"""
108107
Returns the normalized residual-map of the fit of complex model-data to a dataset, where:
109108
@@ -127,8 +126,8 @@ def normalized_residual_map_complex_from(
127126

128127

129128
def chi_squared_map_complex_from(
130-
*, residual_map: jnp.ndarray, noise_map: jnp.ndarray
131-
) -> jnp.ndarray:
129+
*, residual_map: np.ndarray, noise_map: np.ndarray
130+
) -> np.ndarray:
132131
"""
133132
Returnss the chi-squared-map of the fit of complex model-data to a dataset, where:
134133
@@ -146,7 +145,7 @@ def chi_squared_map_complex_from(
146145
return chi_squared_map_real + 1j * chi_squared_map_imag
147146

148147

149-
def chi_squared_complex_from(*, chi_squared_map: jnp.ndarray) -> float:
148+
def chi_squared_complex_from(*, chi_squared_map: np.ndarray, xp=np) -> float:
150149
"""
151150
Returns the chi-squared terms of each complex model data's fit to a masked dataset, by summing the masked
152151
chi-squared-map of the fit.
@@ -158,12 +157,12 @@ def chi_squared_complex_from(*, chi_squared_map: jnp.ndarray) -> float:
158157
chi_squared_map
159158
The chi-squared-map of values of the model-data fit to the dataset.
160159
"""
161-
chi_squared_real = jnp.sum(chi_squared_map.real)
162-
chi_squared_imag = jnp.sum(chi_squared_map.imag)
160+
chi_squared_real = xp.sum(chi_squared_map.real)
161+
chi_squared_imag = xp.sum(chi_squared_map.imag)
163162
return chi_squared_real + chi_squared_imag
164163

165164

166-
def noise_normalization_complex_from(*, noise_map: jnp.ndarray) -> float:
165+
def noise_normalization_complex_from(*, noise_map: np.ndarray, xp=np) -> float:
167166
"""
168167
Returns the noise-map normalization terms of a complex noise-map, summing the noise_map value in every pixel as:
169168
@@ -174,14 +173,14 @@ def noise_normalization_complex_from(*, noise_map: jnp.ndarray) -> float:
174173
noise_map
175174
The masked noise-map of the dataset.
176175
"""
177-
noise_normalization_real = jnp.sum(jnp.log(2 * jnp.pi * noise_map.real**2.0))
178-
noise_normalization_imag = jnp.sum(jnp.log(2 * jnp.pi * noise_map.imag**2.0))
176+
noise_normalization_real = xp.sum(xp.log(2 * xp.pi * noise_map.real**2.0))
177+
noise_normalization_imag = xp.sum(xp.log(2 * xp.pi * noise_map.imag**2.0))
179178
return noise_normalization_real + noise_normalization_imag
180179

181180

182181
@to_new_array
183182
def residual_map_with_mask_from(
184-
*, data: ty.DataLike, mask: Mask, model_data: ty.DataLike
183+
*, data: ty.DataLike, mask: Mask, model_data: ty.DataLike, xp=np
185184
) -> ty.DataLike:
186185
"""
187186
Returns the residual-map of the fit of model-data to a masked dataset, where:
@@ -199,12 +198,12 @@ def residual_map_with_mask_from(
199198
model_data
200199
The model data used to fit the data.
201200
"""
202-
return jnp.where(jnp.asarray(mask) == 0, jnp.subtract(data, model_data), 0)
201+
return xp.where(xp.asarray(mask) == 0, xp.subtract(data, model_data), 0)
203202

204203

205204
@to_new_array
206205
def normalized_residual_map_with_mask_from(
207-
*, residual_map: ty.DataLike, noise_map: ty.DataLike, mask: Mask
206+
*, residual_map: ty.DataLike, noise_map: ty.DataLike, mask: Mask, xp=np
208207
) -> ty.DataLike:
209208
"""
210209
Returns the normalized residual-map of the fit of model-data to a masked dataset, where:
@@ -222,12 +221,12 @@ def normalized_residual_map_with_mask_from(
222221
mask
223222
The mask applied to the residual-map, where `False` entries are included in the calculation.
224223
"""
225-
return jnp.where(jnp.asarray(mask) == 0, jnp.divide(residual_map, noise_map), 0)
224+
return xp.where(xp.asarray(mask) == 0, xp.divide(residual_map, noise_map), 0)
226225

227226

228227
@to_new_array
229228
def chi_squared_map_with_mask_from(
230-
*, residual_map: ty.DataLike, noise_map: ty.DataLike, mask: Mask
229+
*, residual_map: ty.DataLike, noise_map: ty.DataLike, mask: Mask, xp=np
231230
) -> ty.DataLike:
232231
"""
233232
Returnss the chi-squared-map of the fit of model-data to a masked dataset, where:
@@ -245,10 +244,10 @@ def chi_squared_map_with_mask_from(
245244
mask
246245
The mask applied to the residual-map, where `False` entries are included in the calculation.
247246
"""
248-
return jnp.where(jnp.asarray(mask) == 0, jnp.square(residual_map / noise_map), 0)
247+
return xp.where(xp.asarray(mask) == 0, xp.square(residual_map / noise_map), 0)
249248

250249

251-
def chi_squared_with_mask_from(*, chi_squared_map: ty.DataLike, mask: Mask) -> float:
250+
def chi_squared_with_mask_from(*, chi_squared_map: ty.DataLike, mask: Mask, xp=np) -> float:
252251
"""
253252
Returns the chi-squared terms of each model data's fit to a masked dataset, by summing the masked
254253
chi-squared-map of the fit.
@@ -262,11 +261,11 @@ def chi_squared_with_mask_from(*, chi_squared_map: ty.DataLike, mask: Mask) -> f
262261
mask
263262
The mask applied to the chi-squared-map, where `False` entries are included in the calculation.
264263
"""
265-
return float(jnp.sum(chi_squared_map[jnp.asarray(mask) == 0]))
264+
return float(xp.sum(chi_squared_map[xp.asarray(mask) == 0]))
266265

267266

268267
def chi_squared_with_mask_fast_from(
269-
*, data: ty.DataLike, mask: Mask, model_data: ty.DataLike, noise_map: ty.DataLike
268+
*, data: ty.DataLike, mask: Mask, model_data: ty.DataLike, noise_map: ty.DataLike, xp=np
270269
) -> float:
271270
"""
272271
Returns the chi-squared terms of each model data's fit to a masked dataset, by summing the masked
@@ -289,21 +288,21 @@ def chi_squared_with_mask_fast_from(
289288
The mask applied to the chi-squared-map, where `False` entries are included in the calculation.
290289
"""
291290
return float(
292-
jnp.sum(
293-
jnp.square(
294-
jnp.divide(
295-
jnp.subtract(
291+
xp.sum(
292+
xp.square(
293+
xp.divide(
294+
xp.subtract(
296295
data,
297296
model_data,
298-
)[jnp.asarray(mask) == 0],
299-
noise_map[jnp.asarray(mask) == 0],
297+
)[xp.asarray(mask) == 0],
298+
noise_map[xp.asarray(mask) == 0],
300299
)
301300
)
302301
)
303302
)
304303

305304

306-
def noise_normalization_with_mask_from(*, noise_map: ty.DataLike, mask: Mask) -> float:
305+
def noise_normalization_with_mask_from(*, noise_map: ty.DataLike, mask: Mask, xp=np) -> float:
307306
"""
308307
Returns the noise-map normalization terms of masked noise-map, summing the noise_map value in every pixel as:
309308
@@ -319,12 +318,12 @@ def noise_normalization_with_mask_from(*, noise_map: ty.DataLike, mask: Mask) ->
319318
The mask applied to the noise-map, where `False` entries are included in the calculation.
320319
"""
321320
return float(
322-
jnp.sum(jnp.log(2 * jnp.pi * noise_map[jnp.asarray(mask) == 0] ** 2.0))
321+
xp.sum(xp.log(2 * xp.pi * noise_map[xp.asarray(mask) == 0] ** 2.0))
323322
)
324323

325324

326325
def chi_squared_with_noise_covariance_from(
327-
*, residual_map: ty.DataLike, noise_covariance_matrix_inv: jnp.ndarray
326+
*, residual_map: ty.DataLike, noise_covariance_matrix_inv: np.ndarray
328327
) -> float:
329328
"""
330329
Returns the chi-squared value of the fit of model-data to a masked dataset, where
@@ -420,8 +419,8 @@ def log_evidence_from(
420419

421420

422421
def residual_flux_fraction_map_from(
423-
*, residual_map: jnp.ndarray, data: jnp.ndarray
424-
) -> jnp.ndarray:
422+
*, residual_map: np.ndarray, data: np.ndarray, xp=np
423+
) -> np.ndarray:
425424
"""
426425
Returns the residual flux fraction map of the fit of model-data to a masked dataset, where:
427426
@@ -434,12 +433,12 @@ def residual_flux_fraction_map_from(
434433
data
435434
The data of the dataset.
436435
"""
437-
return jnp.where(data != 0, residual_map / data, 0)
436+
return xp.where(data != 0, residual_map / data, 0)
438437

439438

440439
def residual_flux_fraction_map_with_mask_from(
441-
*, residual_map: jnp.ndarray, data: jnp.ndarray, mask: Mask
442-
) -> jnp.ndarray:
440+
*, residual_map: np.ndarray, data: np.ndarray, mask: Mask, xp=np
441+
) -> np.ndarray:
443442
"""
444443
Returnss the residual flux fraction map of the fit of model-data to a masked dataset, where:
445444
@@ -456,4 +455,4 @@ def residual_flux_fraction_map_with_mask_from(
456455
mask
457456
The mask applied to the residual-map, where `False` entries are included in the calculation.
458457
"""
459-
return jnp.where(mask == 0, residual_map / data, 0)
458+
return xp.where(mask == 0, residual_map / data, 0)

autoarray/geometry/geometry_util.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 numpy as np
32
from typing import Tuple, Union
43

autoarray/inversion/inversion/imaging/abstract.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def operated_mapping_matrix_list(self) -> List[np.ndarray]:
9595
return [
9696
(
9797
self.psf.convolved_mapping_matrix_from(
98-
mapping_matrix=linear_obj.mapping_matrix, mask=self.mask
98+
mapping_matrix=linear_obj.mapping_matrix, mask=self.mask, xp=self.xp
9999
)
100100
if linear_obj.operated_mapping_matrix_override is None
101101
else self.linear_func_operated_mapping_matrix_dict[linear_obj]
@@ -139,6 +139,7 @@ def linear_func_operated_mapping_matrix_dict(self) -> Dict:
139139
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
140140
mapping_matrix=linear_func.mapping_matrix,
141141
mask=self.mask,
142+
xp=self.xp
142143
)
143144

144145
linear_func_operated_mapping_matrix_dict[linear_func] = (
@@ -220,6 +221,7 @@ def mapper_operated_mapping_matrix_dict(self) -> Dict:
220221
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
221222
mapping_matrix=mapper.mapping_matrix,
222223
mask=self.mask,
224+
xp=self.xp
223225
)
224226

225227
mapper_operated_mapping_matrix_dict[mapper] = operated_mapping_matrix

autoarray/inversion/inversion/imaging/mapping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _data_vector_mapper(self) -> np.ndarray:
7676
param_range = mapper_param_range_list[i]
7777

7878
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
79-
mapping_matrix=mapper.mapping_matrix, mask=self.mask
79+
mapping_matrix=mapper.mapping_matrix, mask=self.mask, xp=self.xp
8080
)
8181

8282
data_vector_mapper = (
@@ -135,7 +135,7 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]:
135135
mapper_param_range_i = mapper_param_range_list[i]
136136

137137
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
138-
mapping_matrix=mapper_i.mapping_matrix, mask=self.mask
138+
mapping_matrix=mapper_i.mapping_matrix, mask=self.mask, xp=self.xp
139139
)
140140

141141
diag = inversion_util.curvature_matrix_via_mapping_matrix_from(

autoarray/inversion/inversion/imaging/w_tilde.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ def mapped_reconstructed_data_dict(self) -> Dict[LinearObj, Array2D]:
524524
mapped_reconstructed_image = self.psf.convolved_image_from(
525525
image=mapped_reconstructed_image,
526526
blurring_image=None,
527+
xp=self.xp
527528
).array
528529

529530
mapped_reconstructed_image = Array2D(

autoarray/inversion/inversion/interferometer/mapping.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import jax.numpy as jnp
21
import numpy as np
3-
from typing import Dict, List, Optional, Union
4-
5-
from autoconf import cached_property
2+
from typing import Dict, List, Union
63

74
from autoarray.dataset.interferometer.dataset import Interferometer
85
from autoarray.inversion.inversion.dataset_interface import DatasetInterface

0 commit comments

Comments
 (0)