11from functools import wraps
2- import jax .numpy as jnp
32import numpy as np
43
54from 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
104103def 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
129128def 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
183182def 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
206205def 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
229228def 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
268267def 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
326325def 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
422421def 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
440439def 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 )
0 commit comments