Skip to content

Commit f624774

Browse files
Jammy2211Jammy2211
authored andcommitted
fix numpy interp methods
1 parent d55304f commit f624774

28 files changed

Lines changed: 112 additions & 98 deletions

File tree

autoarray/abstract_ndarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, array, xp=np):
8484
except ValueError:
8585
pass
8686

87-
self.xp = xp
87+
self._xp = xp
8888

8989
def invert(self):
9090
new = self.copy()

autoarray/fit/fit_dataset.py

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

8888
@property
8989
def noise_normalization(self) -> float:
@@ -92,7 +92,7 @@ def noise_normalization(self) -> float:
9292
9393
[Noise_Term] = sum(log(2*pi*[Noise]**2.0))
9494
"""
95-
return fit_util.noise_normalization_from(noise_map=self.noise_map.array, xp=self.xp)
95+
return fit_util.noise_normalization_from(noise_map=self.noise_map.array, xp=self._xp)
9696

9797
@property
9898
def log_likelihood(self) -> float:
@@ -146,7 +146,7 @@ def __init__(
146146
self.dataset = dataset
147147
self.use_mask_in_fit = use_mask_in_fit
148148
self.dataset_model = dataset_model or DatasetModel()
149-
self.xp = xp
149+
self._xp = xp
150150

151151
@property
152152
def mask(self) -> Mask2D:
@@ -196,7 +196,7 @@ def residual_map(self) -> ty.DataLike:
196196

197197
if self.use_mask_in_fit:
198198
return fit_util.residual_map_with_mask_from(
199-
data=self.data, model_data=self.model_data, mask=self.mask, xp=self.xp
199+
data=self.data, model_data=self.model_data, mask=self.mask, xp=self._xp
200200
)
201201
return super().residual_map
202202

@@ -209,7 +209,7 @@ def normalized_residual_map(self) -> ty.DataLike:
209209
"""
210210
if self.use_mask_in_fit:
211211
return fit_util.normalized_residual_map_with_mask_from(
212-
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self.xp
212+
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self._xp
213213
)
214214
return super().normalized_residual_map
215215

@@ -222,7 +222,7 @@ def chi_squared_map(self) -> ty.DataLike:
222222
"""
223223
if self.use_mask_in_fit:
224224
return fit_util.chi_squared_map_with_mask_from(
225-
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self.xp
225+
residual_map=self.residual_map, noise_map=self.noise_map, mask=self.mask, xp=self._xp
226226
)
227227
return super().chi_squared_map
228228

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

244244
if self.use_mask_in_fit:
245245
return fit_util.chi_squared_with_mask_from(
246-
chi_squared_map=self.chi_squared_map, mask=self.mask, xp=self.xp
246+
chi_squared_map=self.chi_squared_map, mask=self.mask, xp=self._xp
247247
)
248248
return super().chi_squared
249249

@@ -256,7 +256,7 @@ def noise_normalization(self) -> float:
256256
"""
257257
if self.use_mask_in_fit:
258258
return fit_util.noise_normalization_with_mask_from(
259-
noise_map=self.noise_map, mask=self.mask, xp=self.xp
259+
noise_map=self.noise_map, mask=self.mask, xp=self._xp
260260
)
261261
return super().noise_normalization
262262

autoarray/fit/fit_interferometer.py

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

43
from autoarray.dataset.interferometer.dataset import Interferometer
54

@@ -18,6 +17,7 @@ def __init__(
1817
dataset: Interferometer,
1918
dataset_model: DatasetModel = None,
2019
use_mask_in_fit: bool = False,
20+
xp=np
2121
):
2222
"""
2323
Class to fit a masked interferometer dataset.
@@ -58,6 +58,7 @@ def __init__(
5858
dataset=dataset,
5959
dataset_model=dataset_model,
6060
use_mask_in_fit=use_mask_in_fit,
61+
xp=xp
6162
)
6263

6364
@property

autoarray/inversion/inversion/abstract.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(
7373

7474
self.preloads = preloads or Preloads()
7575

76-
self.xp = xp
76+
self._xp = xp
7777

7878

7979

@@ -285,7 +285,7 @@ def mapping_matrix(self) -> np.ndarray:
285285
If there are multiple linear objects, the mapping matrices are stacked such that their simultaneous linear
286286
equations are solved simultaneously. This property returns the stacked mapping matrix.
287287
"""
288-
return self.xp.hstack(
288+
return self._xp.hstack(
289289
[linear_obj.mapping_matrix for linear_obj in self.linear_obj_list]
290290
)
291291

@@ -304,7 +304,7 @@ def operated_mapping_matrix(self) -> np.ndarray:
304304
If there are multiple linear objects, the blurred mapping matrices are stacked such that their simultaneous
305305
linear equations are solved simultaneously.
306306
"""
307-
return self.xp.hstack(self.operated_mapping_matrix_list)
307+
return self._xp.hstack(self.operated_mapping_matrix_list)
308308

309309
@property
310310
def data_vector(self) -> np.ndarray:
@@ -331,7 +331,7 @@ def regularization_matrix(self) -> Optional[np.ndarray]:
331331
If the `settings.force_edge_pixels_to_zeros` is `True`, the edge pixels of each mapper in the inversion
332332
are regularized so high their value is forced to zero.
333333
"""
334-
if self.xp.__name__.startswith("jax"):
334+
if self._xp.__name__.startswith("jax"):
335335
from jax.scipy.linalg import block_diag
336336
return block_diag(
337337
*[linear_obj.regularization_matrix for linear_obj in self.linear_obj_list]
@@ -379,7 +379,7 @@ def curvature_reg_matrix(self) -> np.ndarray:
379379
if not self.has(cls=AbstractRegularization):
380380
return self.curvature_matrix
381381

382-
return self.xp.add(self.curvature_matrix, self.regularization_matrix)
382+
return self._xp.add(self.curvature_matrix, self.regularization_matrix)
383383

384384
@property
385385
def curvature_reg_matrix_reduced(self) -> Optional[np.ndarray]:
@@ -448,15 +448,15 @@ def reconstruction(self) -> np.ndarray:
448448
data_vector=data_vector,
449449
curvature_reg_matrix=curvature_reg_matrix,
450450
settings=self.settings,
451-
xp=self.xp
451+
xp=self._xp
452452
)
453453
)
454454

455455
# Allocate full solution array
456-
reconstruction = self.xp.zeros(self.data_vector.shape[0])
456+
reconstruction = self._xp.zeros(self.data_vector.shape[0])
457457

458458
# Scatter the partial solution back to the full shape
459-
if self.xp.__name__.startswith("jax"):
459+
if self._xp.__name__.startswith("jax"):
460460
reconstruction = reconstruction.at[ids_to_keep].set(
461461
reconstruction_partial
462462
)
@@ -471,13 +471,13 @@ def reconstruction(self) -> np.ndarray:
471471
data_vector=self.data_vector,
472472
curvature_reg_matrix=self.curvature_reg_matrix,
473473
settings=self.settings,
474-
xp=self.xp
474+
xp=self._xp
475475
)
476476

477477
return inversion_util.reconstruction_positive_negative_from(
478478
data_vector=self.data_vector,
479479
curvature_reg_matrix=self.curvature_reg_matrix,
480-
xp=self.xp
480+
xp=self._xp
481481
)
482482

483483
@property
@@ -638,9 +638,9 @@ def regularization_term(self) -> float:
638638
if not self.has(cls=AbstractRegularization):
639639
return 0.0
640640

641-
return self.xp.matmul(
641+
return self._xp.matmul(
642642
self.reconstruction_reduced.T,
643-
self.xp.matmul(self.regularization_matrix_reduced, self.reconstruction_reduced),
643+
self._xp.matmul(self.regularization_matrix_reduced, self.reconstruction_reduced),
644644
)
645645

646646
@property
@@ -653,8 +653,8 @@ def log_det_curvature_reg_matrix_term(self) -> float:
653653
if not self.has(cls=AbstractRegularization):
654654
return 0.0
655655

656-
return 2.0 * self.xp.sum(
657-
self.xp.log(self.xp.diag(self.xp.linalg.cholesky(self.curvature_reg_matrix_reduced)))
656+
return 2.0 * self._xp.sum(
657+
self._xp.log(self._xp.diag(self._xp.linalg.cholesky(self.curvature_reg_matrix_reduced)))
658658
)
659659

660660
@property
@@ -674,8 +674,8 @@ def log_det_regularization_matrix_term(self) -> float:
674674
if not self.has(cls=AbstractRegularization):
675675
return 0.0
676676

677-
return 2.0 * self.xp.sum(
678-
self.xp.log(self.xp.diag(self.xp.linalg.cholesky(self.regularization_matrix_reduced)))
677+
return 2.0 * self._xp.sum(
678+
self._xp.log(self._xp.diag(self._xp.linalg.cholesky(self.regularization_matrix_reduced)))
679679
)
680680

681681
@property
@@ -738,7 +738,7 @@ def regularization_weights_from(self, index: int) -> np.ndarray:
738738

739739
return np.zeros((pixels,))
740740

741-
return regularization.regularization_weights_from(linear_obj=linear_obj, xp=self.xp)
741+
return regularization.regularization_weights_from(linear_obj=linear_obj, xp=self._xp)
742742

743743
@property
744744
def regularization_weights_mapper_dict(self) -> Dict[LinearObj, np.ndarray]:

autoarray/inversion/inversion/imaging/abstract.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def operated_mapping_matrix_list(self) -> List[np.ndarray]:
9393
return [
9494
(
9595
self.psf.convolved_mapping_matrix_from(
96-
mapping_matrix=linear_obj.mapping_matrix, mask=self.mask, xp=self.xp
96+
mapping_matrix=linear_obj.mapping_matrix, mask=self.mask, xp=self._xp
9797
)
9898
if linear_obj.operated_mapping_matrix_override is None
9999
else self.linear_func_operated_mapping_matrix_dict[linear_obj]
@@ -137,7 +137,7 @@ def linear_func_operated_mapping_matrix_dict(self) -> Dict:
137137
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
138138
mapping_matrix=linear_func.mapping_matrix,
139139
mask=self.mask,
140-
xp=self.xp
140+
xp=self._xp
141141
)
142142

143143
linear_func_operated_mapping_matrix_dict[linear_func] = (
@@ -219,7 +219,7 @@ def mapper_operated_mapping_matrix_dict(self) -> Dict:
219219
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
220220
mapping_matrix=mapper.mapping_matrix,
221221
mask=self.mask,
222-
xp=self.xp
222+
xp=self._xp
223223
)
224224

225225
mapper_operated_mapping_matrix_dict[mapper] = operated_mapping_matrix

autoarray/inversion/inversion/imaging/mapping.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _data_vector_mapper(self) -> np.ndarray:
7474
param_range = mapper_param_range_list[i]
7575

7676
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
77-
mapping_matrix=mapper.mapping_matrix, mask=self.mask, xp=self.xp
77+
mapping_matrix=mapper.mapping_matrix, mask=self.mask, xp=self._xp
7878
)
7979

8080
data_vector_mapper = (
@@ -133,7 +133,7 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]:
133133
mapper_param_range_i = mapper_param_range_list[i]
134134

135135
operated_mapping_matrix = self.psf.convolved_mapping_matrix_from(
136-
mapping_matrix=mapper_i.mapping_matrix, mask=self.mask, xp=self.xp
136+
mapping_matrix=mapper_i.mapping_matrix, mask=self.mask, xp=self._xp
137137
)
138138

139139
diag = inversion_util.curvature_matrix_via_mapping_matrix_from(
@@ -142,7 +142,7 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]:
142142
settings=self.settings,
143143
add_to_curvature_diag=True,
144144
no_regularization_index_list=self.no_regularization_index_list,
145-
xp=self.xp
145+
xp=self._xp
146146
)
147147

148148
curvature_matrix[
@@ -151,7 +151,7 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]:
151151
] = diag
152152

153153
curvature_matrix = inversion_util.curvature_matrix_mirrored_from(
154-
curvature_matrix=curvature_matrix, xp=self.xp
154+
curvature_matrix=curvature_matrix, xp=self._xp
155155
)
156156

157157
return curvature_matrix
@@ -181,7 +181,7 @@ def curvature_matrix(self):
181181
settings=self.settings,
182182
add_to_curvature_diag=True,
183183
no_regularization_index_list=self.no_regularization_index_list,
184-
xp=self.xp
184+
xp=self._xp
185185
)
186186

187187
@property
@@ -224,7 +224,7 @@ def mapped_reconstructed_data_dict(self) -> Dict[LinearObj, Array2D]:
224224
inversion_util.mapped_reconstructed_data_via_mapping_matrix_from(
225225
mapping_matrix=operated_mapping_matrix_list[index],
226226
reconstruction=reconstruction,
227-
xp=self.xp
227+
xp=self._xp
228228
)
229229
)
230230

autoarray/inversion/inversion/imaging/w_tilde.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +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
527+
xp=self._xp
528528
).array
529529

530530
mapped_reconstructed_image = Array2D(

autoarray/inversion/inversion/interferometer/abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def mapped_reconstructed_image_dict(
113113
inversion_util.mapped_reconstructed_data_via_mapping_matrix_from(
114114
mapping_matrix=linear_obj.mapping_matrix,
115115
reconstruction=reconstruction,
116-
xp=self.xp
116+
xp=self._xp
117117
)
118118
)
119119

autoarray/inversion/inversion/interferometer/mapping.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,23 @@ def curvature_matrix(self) -> np.ndarray:
9191
real_curvature_matrix = inversion_util.curvature_matrix_via_mapping_matrix_from(
9292
mapping_matrix=self.operated_mapping_matrix.real,
9393
noise_map=self.noise_map.real,
94-
xp=self.xp
94+
xp=self._xp
9595
)
9696

9797
imag_curvature_matrix = inversion_util.curvature_matrix_via_mapping_matrix_from(
9898
mapping_matrix=self.operated_mapping_matrix.imag,
9999
noise_map=self.noise_map.imag,
100-
xp=self.xp
100+
xp=self._xp
101101
)
102102

103-
curvature_matrix = self.xp.add(real_curvature_matrix, imag_curvature_matrix)
103+
curvature_matrix = self._xp.add(real_curvature_matrix, imag_curvature_matrix)
104104

105105
if len(self.no_regularization_index_list) > 0:
106106
curvature_matrix = inversion_util.curvature_matrix_with_added_to_diag_from(
107107
curvature_matrix=curvature_matrix,
108108
value=self.settings.no_regularization_add_to_curvature_diag_value,
109109
no_regularization_index_list=self.no_regularization_index_list,
110-
xp=self.xp
110+
xp=self._xp
111111
)
112112

113113
return curvature_matrix

autoarray/inversion/inversion/interferometer/w_tilde.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def curvature_matrix_diag(self) -> np.ndarray:
122122

123123
if self.settings.use_w_tilde_numpy:
124124
return inversion_util.curvature_matrix_via_w_tilde_from(
125-
w_tilde=self.w_tilde.w_matrix, mapping_matrix=self.mapping_matrix, xp=self.xp
125+
w_tilde=self.w_tilde.w_matrix, mapping_matrix=self.mapping_matrix, xp=self._xp
126126
)
127127

128128
mapper = self.cls_list_from(cls=AbstractMapper)[0]

0 commit comments

Comments
 (0)