From 21b1a7291d4f3fbc4452b2fa6e5a410bc9c3e110 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 20 Feb 2023 16:06:12 +0000 Subject: [PATCH 001/154] switch to lax import --- autoarray/structures/arrays/uniform_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 56d2801a1..330189df9 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -1,5 +1,5 @@ import logging -import numpy as np +import jax.numpy as np from typing import List, Optional, Tuple, Union from autoarray.mask.mask_2d import Mask2D From 4bb32a0f2932099c216a8d5b86cf00f9926cb0ab Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 20 Feb 2023 16:21:05 +0000 Subject: [PATCH 002/154] started moving towards arrays as wrappers rather than inheritance --- autoarray/abstract_ndarray.py | 27 +++++++++++++++++++++-- autoarray/structures/arrays/uniform_2d.py | 12 +++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 11833cca7..ade8894ab 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -1,4 +1,7 @@ from __future__ import annotations + +from copy import copy + from abc import ABC from abc import abstractmethod import numpy as np @@ -11,7 +14,19 @@ from autoarray.structures.arrays import array_2d_util -class AbstractNDArray(np.ndarray, ABC): +def to_new_array(func): + def wrapper(self, *args, **kwargs): + new_array = copy(self) + new_array._array = func(self, *args, **kwargs) + return new_array + + return wrapper + + +class AbstractNDArray(ABC): + def __init__(self, array): + self._array = array + def __reduce__(self): pickled_state = super().__reduce__() @@ -28,7 +43,7 @@ def __setstate__(self, state): for key, value in state[-1].items(): setattr(self, key, value) - super().__setstate__(state[0:-1]) + self._array.__setstate__(state[0:-1]) @property @abstractmethod @@ -51,3 +66,11 @@ def output_to_fits(self, file_path: str, overwrite: bool = False): array_2d_util.numpy_array_2d_to_fits( array_2d=self.native, file_path=file_path, overwrite=overwrite ) + + @property + def shape(self): + return self._array.shape + + @to_new_array + def reshape(self, *args, **kwargs): + return self._array.reshape(*args, **kwargs) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 330189df9..fae031e00 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -21,7 +21,7 @@ class AbstractArray2D(Structure): def __new__( cls, - values: Union[np.ndarray, List], + values: Union[np.ndarray, List, "AbstractArray2D"], mask: Mask2D, header: Header = None, store_native: bool = False, @@ -331,6 +331,11 @@ def __new__( array_2d.output_to_fits(file_path="/path/for/output") """ + try: + values = values._array + except AttributeError: + pass + values = array_2d_util.convert_array_2d( array_2d=values, mask_2d=mask, @@ -338,9 +343,10 @@ def __new__( skip_mask=skip_mask, ) - obj = values.view(cls) + obj = object.__init__(cls) obj.mask = mask obj.header = header + obj._array = values return obj @@ -645,7 +651,7 @@ class Array2D(AbstractArray2D): @classmethod def no_mask( cls, - values: Union[np.ndarray, List], + values: Union[np.ndarray, List, AbstractArray2D], pixel_scales: ty.PixelScales, shape_native: Tuple[int, int] = None, sub_size: int = 1, From 68993933ebb55e11885561731b3fc00832bdbd9a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 20 Feb 2023 16:40:55 +0000 Subject: [PATCH 003/154] more conversion --- autoarray/mask/abstract_mask.py | 20 ++++++++++---------- autoarray/mask/mask_2d.py | 19 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/autoarray/mask/abstract_mask.py b/autoarray/mask/abstract_mask.py index 768c01ad0..f1d6e8133 100644 --- a/autoarray/mask/abstract_mask.py +++ b/autoarray/mask/abstract_mask.py @@ -16,8 +16,8 @@ class Mask(AbstractNDArray, ABC): pixel_scales = None # noinspection PyUnusedLocal - def __new__( - cls, + def __init__( + self, mask: np.ndarray, origin: tuple, pixel_scales: ty.PixelScales, @@ -49,11 +49,11 @@ def __new__( # noinspection PyArgumentList mask = mask.astype("bool") - obj = mask.view(cls) - obj.sub_size = sub_size - obj.pixel_scales = pixel_scales - obj.origin = origin - return obj + super().__init__(mask) + + self.sub_size = sub_size + self.pixel_scales = pixel_scales + self.origin = origin def __array_finalize__(self, obj): @@ -91,7 +91,7 @@ def sub_length(self) -> int: For example, a sub-size of 3x3 means every pixel has 9 sub-pixels. """ - return int(self.sub_size**self.dimensions) + return int(self.sub_size ** self.dimensions) @property def sub_fraction(self) -> float: @@ -133,7 +133,7 @@ def sub_pixels_in_mask(self) -> int: """ The total number of unmasked sub-pixels (values are `False`) in the mask. """ - return self.sub_size**self.dimensions * self.pixels_in_mask + return self.sub_size ** self.dimensions * self.pixels_in_mask @property def shape_slim(self) -> int: @@ -147,7 +147,7 @@ def sub_shape_slim(self) -> int: """ The 1D shape of the mask's sub-grid, which is equivalent to the total number of unmasked pixels in the mask. """ - return int(self.pixels_in_mask * self.sub_size**self.dimensions) + return int(self.pixels_in_mask * self.sub_size ** self.dimensions) def mask_new_sub_size_from(self, mask, sub_size=1) -> "Mask": """ diff --git a/autoarray/mask/mask_2d.py b/autoarray/mask/mask_2d.py index e162645d2..03a5fb860 100644 --- a/autoarray/mask/mask_2d.py +++ b/autoarray/mask/mask_2d.py @@ -3,6 +3,8 @@ import numpy as np from typing import TYPE_CHECKING, List, Tuple, Union +from autoarray.structures.abstract_structure import Structure + if TYPE_CHECKING: from autoarray.structures.arrays.uniform_2d import Array2D @@ -27,10 +29,13 @@ class Mask2D(Mask): + @property + def native(self) -> Structure: + return self # noinspection PyUnusedLocal - def __new__( - cls, + def __init__( + self, mask: Union[np.ndarray, List], pixel_scales: ty.PixelScales, sub_size: int = 1, @@ -286,16 +291,10 @@ def __new__( if len(mask.shape) != 2: raise exc.MaskException("The input mask is not a two dimensional array") - obj = Mask.__new__( - cls=cls, - mask=mask, - pixel_scales=pixel_scales, - sub_size=sub_size, - origin=origin, + super().__init__( + mask=mask, origin=origin, pixel_scales=pixel_scales, sub_size=sub_size, ) - return obj - def __array_finalize__(self, obj): super().__array_finalize__(obj=obj) From 90fb2208e6d9a01e2d1264bb6818c11ad5267544 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 20 Feb 2023 16:45:27 +0000 Subject: [PATCH 004/154] more conversion --- autoarray/abstract_ndarray.py | 3 +++ autoarray/structures/arrays/kernel_2d.py | 14 ++++++-------- autoarray/structures/arrays/uniform_2d.py | 13 +++++-------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index ade8894ab..3316efea6 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -74,3 +74,6 @@ def shape(self): @to_new_array def reshape(self, *args, **kwargs): return self._array.reshape(*args, **kwargs) + + def __getitem__(self, item): + return self._array[item] diff --git a/autoarray/structures/arrays/kernel_2d.py b/autoarray/structures/arrays/kernel_2d.py index bd980b4c0..02645165f 100644 --- a/autoarray/structures/arrays/kernel_2d.py +++ b/autoarray/structures/arrays/kernel_2d.py @@ -16,8 +16,8 @@ class Kernel2D(AbstractArray2D): - def __new__( - cls, + def __init__( + self, values, mask, header=None, @@ -43,14 +43,12 @@ def __new__( normalize If True, the Kernel2D's array values are normalized such that they sum to 1.0. """ - obj = super().__new__( - cls=cls, values=values, mask=mask, header=header, store_native=store_native + super().__init__( + values=values, mask=mask, header=header, store_native=store_native, ) if normalize: - obj[:] = np.divide(obj, np.sum(obj)) - - return obj + self._array[:] = np.divide(self._array, np.sum(self._array)) @classmethod def no_mask( @@ -259,7 +257,7 @@ def from_gaussian( grid = Grid2D.uniform(shape_native=shape_native, pixel_scales=pixel_scales) grid_shifted = np.subtract(grid, centre) - grid_radius = np.sqrt(np.sum(grid_shifted**2.0, 1)) + grid_radius = np.sqrt(np.sum(grid_shifted ** 2.0, 1)) theta_coordinate_to_profile = np.arctan2( grid_shifted[:, 0], grid_shifted[:, 1] ) - np.radians(angle) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index fae031e00..2b1da376c 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -19,8 +19,8 @@ class AbstractArray2D(Structure): - def __new__( - cls, + def __init__( + self, values: Union[np.ndarray, List, "AbstractArray2D"], mask: Mask2D, header: Header = None, @@ -343,12 +343,9 @@ def __new__( skip_mask=skip_mask, ) - obj = object.__init__(cls) - obj.mask = mask - obj.header = header - obj._array = values - - return obj + super().__init__(values) + self.mask = mask + self.header = header def __array_finalize__(self, obj): From c18b20943f9d85e9bbb6f340e775ffefbd7fd650 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 20 Feb 2023 16:48:05 +0000 Subject: [PATCH 005/154] more conversion... --- autoarray/mask/abstract_mask.py | 2 +- autoarray/mask/mask_2d.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/autoarray/mask/abstract_mask.py b/autoarray/mask/abstract_mask.py index f1d6e8133..66eb5c2ba 100644 --- a/autoarray/mask/abstract_mask.py +++ b/autoarray/mask/abstract_mask.py @@ -112,7 +112,7 @@ def pixels_in_mask(self) -> int: """ The total number of unmasked pixels (values are `False`) in the mask. """ - return int(np.size(self) - np.sum(self)) + return int(np.size(self._array) - np.sum(self._array)) @property def is_all_true(self) -> bool: diff --git a/autoarray/mask/mask_2d.py b/autoarray/mask/mask_2d.py index 03a5fb860..cd2ec5c73 100644 --- a/autoarray/mask/mask_2d.py +++ b/autoarray/mask/mask_2d.py @@ -283,6 +283,9 @@ def __init__( if type(mask) is list: mask = np.asarray(mask).astype("bool") + if not isinstance(mask, np.ndarray): + mask = mask._array + if invert: mask = np.invert(mask) From 1e62bc746790dfaa1ca9ee43e499a56be2d8e289 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 09:53:59 +0000 Subject: [PATCH 006/154] new to init --- autoarray/structures/grids/uniform_2d.py | 31 ++++++++++-------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 8bcf65171..3149cd47f 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -20,13 +20,13 @@ class Grid2D(Structure): - def __new__( - cls, + def __init__( + self, values: Union[np.ndarray, List], mask: Mask2D, store_native: bool = False, *args, - **kwargs, + **kwargs ): """ A grid of 2D (y,x) coordinates, which are paired to a uniform 2D mask of pixels and sub-pixels. Each entry @@ -237,17 +237,14 @@ def __new__( If True, the ndarray is stored in its native format [total_y_pixels, total_x_pixels, 2]. This avoids mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck. """ - values = grid_2d_util.convert_grid_2d( - grid_2d=values, mask_2d=mask, store_native=store_native + grid_2d=values, mask_2d=mask, store_native=store_native, ) - obj = values.view(cls) - obj.mask = mask + super().__init__(values) - grid_2d_util.check_grid_2d(grid_2d=obj) - - return obj + self.mask = mask + grid_2d_util.check_grid_2d(grid_2d=values) @classmethod def no_mask( @@ -1079,14 +1076,12 @@ def grid_2d_radial_projected_from( positive x-axis. """ - grid_radial_projected_2d = ( - grid_2d_util.grid_scaled_2d_slim_radial_projected_from( - extent=self.geometry.extent, - centre=centre, - pixel_scales=self.mask.pixel_scales, - sub_size=self.mask.sub_size, - shape_slim=shape_slim, - ) + grid_radial_projected_2d = grid_2d_util.grid_scaled_2d_slim_radial_projected_from( + extent=self.geometry.extent, + centre=centre, + pixel_scales=self.mask.pixel_scales, + sub_size=self.mask.sub_size, + shape_slim=shape_slim, ) grid_radial_projected_2d = geometry_util.transform_grid_2d_to_reference_frame( From 87323663bc7a1e273414d91089e3eeef4074b650 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 10:37:48 +0000 Subject: [PATCH 007/154] implementing comparison operators --- autoarray/abstract_ndarray.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 3316efea6..24d0d2f60 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -27,6 +27,32 @@ class AbstractNDArray(ABC): def __init__(self, array): self._array = array + def __lt__(self, other): + return self._array < other + + def __le__(self, other): + return self._array <= other + + def __gt__(self, other): + return self._array > other + + def __ge__(self, other): + return self._array >= other + + def __eq__(self, other): + return self._array == other + + def __ne__(self, other): + return self._array != other + + @to_new_array + def __neg__(self): + return -self._array + + @to_new_array + def __invert__(self): + return ~self._array + def __reduce__(self): pickled_state = super().__reduce__() From 776c4d13c0342c895300b814ae8c971fec617cb1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:14:52 +0000 Subject: [PATCH 008/154] remove need for reduce --- autoarray/abstract_ndarray.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 24d0d2f60..0ad216a2a 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -53,24 +53,6 @@ def __neg__(self): def __invert__(self): return ~self._array - def __reduce__(self): - - pickled_state = super().__reduce__() - - class_dict = {} - for key, value in self.__dict__.items(): - class_dict[key] = value - new_state = pickled_state[2] + (class_dict,) - - return pickled_state[0], pickled_state[1], new_state - - # noinspection PyMethodOverriding - def __setstate__(self, state): - - for key, value in state[-1].items(): - setattr(self, key, value) - self._array.__setstate__(state[0:-1]) - @property @abstractmethod def native(self) -> Structure: From efeaac12f5c552a667a7cad835e079645c7959ce Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:14:59 +0000 Subject: [PATCH 009/154] mul and rmul --- autoarray/abstract_ndarray.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 0ad216a2a..2cfed643a 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -45,6 +45,12 @@ def __eq__(self, other): def __ne__(self, other): return self._array != other + def __mul__(self, other): + return self._array * other + + def __rmul__(self, other): + return other * self._array + @to_new_array def __neg__(self): return -self._array From 048f2f7800b8023cf9d6d993837fc8006b64cd50 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:52:08 +0000 Subject: [PATCH 010/154] array property --- autoarray/abstract_ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 2cfed643a..ffc070297 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -27,6 +27,10 @@ class AbstractNDArray(ABC): def __init__(self, array): self._array = array + @property + def array(self): + return self._array + def __lt__(self, other): return self._array < other From 5078d588e99d40d14e5f45ae75a4c6f4a1e67909 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:52:25 +0000 Subject: [PATCH 011/154] explicity pass array reference when plotting a --- autoarray/plot/mat_plot/two_d.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/plot/mat_plot/two_d.py b/autoarray/plot/mat_plot/two_d.py index 1972e5e6d..46dbfa3e5 100644 --- a/autoarray/plot/mat_plot/two_d.py +++ b/autoarray/plot/mat_plot/two_d.py @@ -260,12 +260,12 @@ def plot_array( ax = self.setup_subplot() aspect = self.figure.aspect_from(shape_native=array.shape_native) - norm_scale = self.cmap.norm_from(array=array) + norm_scale = self.cmap.norm_from(array=array.array) origin = conf.instance["visualize"]["general"]["general"]["imshow_origin"] plt.imshow( - X=array.native, + X=array.native.array, aspect=aspect, cmap=self.cmap.cmap, norm=norm_scale, From e874d22fbde3c5f3dd4215115194b18a98189d6e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:53:35 +0000 Subject: [PATCH 012/154] irregular 2d conversion --- autoarray/structures/grids/irregular_2d.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 7f6f6c780..018dd2a36 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -48,11 +48,10 @@ def __new__(cls, values: Union[np.ndarray, List]): if isinstance(values[0], Grid2DIrregular): return values - values = np.asarray(values) + return object.__new__(cls) - obj = values.view(cls) - - return obj + def __init__(self, values: Union[np.ndarray, List]): + super().__init__(np.asarray(values)) @property def geometry(self): From 2af77ea216b82ca86c1274ecc96356f57c171ac3 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:53:58 +0000 Subject: [PATCH 013/154] float and array property --- autoarray/abstract_ndarray.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index ffc070297..3c8f1addb 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -59,10 +59,15 @@ def __rmul__(self, other): def __neg__(self): return -self._array - @to_new_array def __invert__(self): return ~self._array + def sum(self, *args, **kwargs): + return self._array.sum(*args, **kwargs) + + def __float__(self): + return float(self._array) + @property @abstractmethod def native(self) -> Structure: From 7c40da6aaf4fda9c8a49ae954eb3f9c9e1d47a40 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 11:55:03 +0000 Subject: [PATCH 014/154] div --- autoarray/abstract_ndarray.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 3c8f1addb..9ecf5d12d 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -62,6 +62,18 @@ def __neg__(self): def __invert__(self): return ~self._array + def __divmod__(self, other): + return divmod(self._array, other) + + def __rdivmod__(self, other): + return divmod(other, self._array) + + def __truediv__(self, other): + return self._array / other + + def __rtruediv__(self, other): + return other / self._array + def sum(self, *args, **kwargs): return self._array.sum(*args, **kwargs) From ac7b9e67d424204e483d7917be2b50cd10060cbb Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:10:24 +0000 Subject: [PATCH 015/154] unwrap array --- autoarray/abstract_ndarray.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 9ecf5d12d..acb78f145 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -23,6 +23,16 @@ def wrapper(self, *args, **kwargs): return wrapper +def unwrap_array(func): + def wrapper(self, other): + try: + return func(self, other.array) + except AttributeError: + return func(self, other) + + return wrapper + + class AbstractNDArray(ABC): def __init__(self, array): self._array = array @@ -68,9 +78,11 @@ def __divmod__(self, other): def __rdivmod__(self, other): return divmod(other, self._array) + @unwrap_array def __truediv__(self, other): return self._array / other + @unwrap_array def __rtruediv__(self, other): return other / self._array From bb224382916411293538a04701611f7c8dc594f4 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:32:59 +0000 Subject: [PATCH 016/154] unwrap arrays --- autoarray/abstract_ndarray.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index acb78f145..561978443 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -41,21 +41,26 @@ def __init__(self, array): def array(self): return self._array + @unwrap_array def __lt__(self, other): return self._array < other + @unwrap_array def __le__(self, other): return self._array <= other + @unwrap_array def __gt__(self, other): return self._array > other + @unwrap_array def __ge__(self, other): return self._array >= other def __eq__(self, other): return self._array == other + @unwrap_array def __ne__(self, other): return self._array != other From 7c10dcf9a8172739df0a51262dba2773d939154c Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:33:12 +0000 Subject: [PATCH 017/154] to new array --- autoarray/abstract_ndarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 561978443..441b3af4b 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -83,10 +83,12 @@ def __divmod__(self, other): def __rdivmod__(self, other): return divmod(other, self._array) + @to_new_array @unwrap_array def __truediv__(self, other): return self._array / other + @to_new_array @unwrap_array def __rtruediv__(self, other): return other / self._array From 929428c34d06ed5c83cd07eb9ec5b8baa3aa9f39 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:33:21 +0000 Subject: [PATCH 018/154] abs --- autoarray/abstract_ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 441b3af4b..a1e947d9e 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -93,6 +93,10 @@ def __truediv__(self, other): def __rtruediv__(self, other): return other / self._array + @to_new_array + def __abs__(self): + return abs(self._array) + def sum(self, *args, **kwargs): return self._array.sum(*args, **kwargs) From 74155594e2c35bc3f4015c20c6ccb7cd322ee923 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:33:41 +0000 Subject: [PATCH 019/154] write array --- autoarray/abstract_ndarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index a1e947d9e..3293d8051 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -122,7 +122,7 @@ def output_to_fits(self, file_path: str, overwrite: bool = False): If a file already exists at the path, if overwrite=True it is overwritten else an error is raised. """ array_2d_util.numpy_array_2d_to_fits( - array_2d=self.native, file_path=file_path, overwrite=overwrite + array_2d=self.native.array, file_path=file_path, overwrite=overwrite ) @property From 175b320392c81cb4036e2d018301147c5f5bbd31 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:33:52 +0000 Subject: [PATCH 020/154] set item... --- autoarray/abstract_ndarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 3293d8051..227c35d5b 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -135,3 +135,6 @@ def reshape(self, *args, **kwargs): def __getitem__(self, item): return self._array[item] + + def __setitem__(self, key, value): + self._array[key] = value From 4fe0aa3d770c3b425acbb02dd74c49c0e53d4b59 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 12:48:08 +0000 Subject: [PATCH 021/154] cast multiplied arrays to new array; now at a new jax error --- autoarray/abstract_ndarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 227c35d5b..7a4b97884 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -64,9 +64,11 @@ def __eq__(self, other): def __ne__(self, other): return self._array != other + @to_new_array def __mul__(self, other): return self._array * other + @to_new_array def __rmul__(self, other): return other * self._array From ce9e0c8f7f3414cd2e3acf57d907d1d40ab79b36 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 14:27:53 +0000 Subject: [PATCH 022/154] unwrap when multiplying --- autoarray/abstract_ndarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 7a4b97884..314a85db9 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -65,10 +65,12 @@ def __ne__(self, other): return self._array != other @to_new_array + @unwrap_array def __mul__(self, other): return self._array * other @to_new_array + @unwrap_array def __rmul__(self, other): return other * self._array From 8b3c5f25f0611ee151d5e62362c084d39d83bbe5 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 14:40:44 +0000 Subject: [PATCH 023/154] fix is_all_false --- autoarray/mask/abstract_mask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/mask/abstract_mask.py b/autoarray/mask/abstract_mask.py index 66eb5c2ba..748e7cbef 100644 --- a/autoarray/mask/abstract_mask.py +++ b/autoarray/mask/abstract_mask.py @@ -126,7 +126,7 @@ def is_all_false(self) -> bool: """ Returns `False` if all pixels in a mask are `False`, else returns `True`. """ - return self.pixels_in_mask == np.size(self) + return self.pixels_in_mask == np.size(self._array) @property def sub_pixels_in_mask(self) -> int: From f482b194b3e12ed15c8b1c67500257d5f4d16a74 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 15:12:42 +0000 Subject: [PATCH 024/154] a --- autoarray/abstract_ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 314a85db9..5a5d09ac1 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -133,6 +133,10 @@ def output_to_fits(self, file_path: str, overwrite: bool = False): def shape(self): return self._array.shape + @property + def size(self): + return self._array.size + @to_new_array def reshape(self, *args, **kwargs): return self._array.reshape(*args, **kwargs) From c4c5366956e7b74d97c5fe52b9edc45a96965c16 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 15:13:54 +0000 Subject: [PATCH 025/154] add and sub --- autoarray/abstract_ndarray.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 5a5d09ac1..6e04e71f8 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -60,6 +60,26 @@ def __ge__(self, other): def __eq__(self, other): return self._array == other + @to_new_array + @unwrap_array + def __add__(self, other): + return self._array + other + + @to_new_array + @unwrap_array + def __radd__(self, other): + return other + self._array + + @to_new_array + @unwrap_array + def __sub__(self, other): + return self._array - other + + @to_new_array + @unwrap_array + def __rsub__(self, other): + return other - self._array + @unwrap_array def __ne__(self, other): return self._array != other From be4939eb73b50019805fb83427a06498f12a3038 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 15:15:23 +0000 Subject: [PATCH 026/154] pow --- autoarray/abstract_ndarray.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 6e04e71f8..29a1f3c44 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -60,6 +60,11 @@ def __ge__(self, other): def __eq__(self, other): return self._array == other + @to_new_array + @unwrap_array + def __pow__(self, other): + return self._array ** other + @to_new_array @unwrap_array def __add__(self, other): From 41c745070ee6990ce130343aa15a884c28a62951 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 15:59:11 +0000 Subject: [PATCH 027/154] use - instead of numpy for now --- autoarray/geometry/geometry_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/geometry/geometry_util.py b/autoarray/geometry/geometry_util.py index 9f0a23cb7..8a9acdc31 100644 --- a/autoarray/geometry/geometry_util.py +++ b/autoarray/geometry/geometry_util.py @@ -384,8 +384,8 @@ def transform_grid_2d_to_reference_frame( grid The 2d grid of (y, x) coordinates which are transformed to a new reference frame. """ - shifted_grid_2d = np.subtract(grid_2d, centre) - radius = np.sqrt(np.sum(shifted_grid_2d**2.0, 1)) + shifted_grid_2d = grid_2d - centre + radius = np.sqrt(np.sum(shifted_grid_2d ** 2.0, 1)) theta_coordinate_to_profile = np.arctan2( shifted_grid_2d[:, 0], shifted_grid_2d[:, 1] ) - np.radians(angle) From ebc4cd1384814303075422142f83af781aaf4ce5 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 16:32:21 +0000 Subject: [PATCH 028/154] cast to iterable --- autoarray/abstract_ndarray.py | 3 +++ autoarray/structures/structure_decorators.py | 28 ++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 29a1f3c44..8689735b6 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -37,6 +37,9 @@ class AbstractNDArray(ABC): def __init__(self, array): self._array = array + def __iter__(self): + return iter(self._array) + @property def array(self): return self._array diff --git a/autoarray/structures/structure_decorators.py b/autoarray/structures/structure_decorators.py index c46951b16..732d88442 100644 --- a/autoarray/structures/structure_decorators.py +++ b/autoarray/structures/structure_decorators.py @@ -1,22 +1,21 @@ import numpy as np from functools import wraps -from typing import List, Optional, Union +from typing import List, Union -from autoconf import conf +from autoarray import exc +from autoarray.structures.arrays.irregular import ArrayIrregular from autoarray.structures.arrays.uniform_1d import Array1D from autoarray.structures.arrays.uniform_2d import Array2D -from autoarray.structures.grids.uniform_1d import Grid1D -from autoarray.structures.grids.uniform_2d import Grid2D -from autoarray.structures.grids.transformed_2d import Grid2DTransformed -from autoarray.structures.grids.transformed_2d import Grid2DTransformedNumpy -from autoarray.structures.grids.iterate_2d import Grid2DIterate from autoarray.structures.grids.irregular_2d import Grid2DIrregular from autoarray.structures.grids.irregular_2d import Grid2DIrregularTransformed -from autoarray.structures.vectors.uniform import VectorYX2D +from autoarray.structures.grids.iterate_2d import Grid2DIterate +from autoarray.structures.grids.transformed_2d import Grid2DTransformed +from autoarray.structures.grids.transformed_2d import Grid2DTransformedNumpy +from autoarray.structures.grids.uniform_1d import Grid1D +from autoarray.structures.grids.uniform_2d import Grid2D from autoarray.structures.vectors.irregular import VectorYX2DIrregular -from autoarray.structures.arrays.irregular import ArrayIrregular - -from autoarray import exc +from autoarray.structures.vectors.uniform import VectorYX2D +from autoconf import conf def grid_1d_to_structure(func): @@ -553,9 +552,10 @@ def wrapper( grid_radial_scale = np.where( grid_radii < grid_radial_minimum, grid_radial_minimum / grid_radii, 1.0 ) - grid = np.multiply(grid, grid_radial_scale[:, None]) - grid[np.isnan(grid)] = grid_radial_minimum + moved_grid = np.multiply(grid, grid_radial_scale[:, None]) + + moved_grid[np.isnan(np.array(moved_grid))] = grid_radial_minimum - return func(cls, grid, *args, **kwargs) + return func(cls, moved_grid, *args, **kwargs) return wrapper From 2c7cfdd456553131268d8e405e14c56a2ed0e82e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 16:33:46 +0000 Subject: [PATCH 029/154] sqrt --- autoarray/abstract_ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 8689735b6..6e30c78d1 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -40,6 +40,10 @@ def __init__(self, array): def __iter__(self): return iter(self._array) + @to_new_array + def sqrt(self): + return np.sqrt(self._array) + @property def array(self): return self._array From a0e59c538a3ade6724e30413f0c73ac8496c553c Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 16:42:41 +0000 Subject: [PATCH 030/154] remove numba import assertion --- autoarray/inversion/inversion/abstract.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/autoarray/inversion/inversion/abstract.py b/autoarray/inversion/inversion/abstract.py index 7a7a014b1..6c8791c97 100644 --- a/autoarray/inversion/inversion/abstract.py +++ b/autoarray/inversion/inversion/abstract.py @@ -77,16 +77,16 @@ def __init__( preloads = preloads or Preloads() - try: - import numba - except ModuleNotFoundError: - raise exc.InversionException( - "Inversion functionality (linear light profiles, pixelized reconstructions) is " - "disabled if numba is not installed.\n\n" - "This is because the run-times without numba are too slow.\n\n" - "Please install numba, which is described at the following web page:\n\n" - "https://pyautolens.readthedocs.io/en/latest/installation/overview.html" - ) + # try: + # import numba + # except ModuleNotFoundError: + # raise exc.InversionException( + # "Inversion functionality (linear light profiles, pixelized reconstructions) is " + # "disabled if numba is not installed.\n\n" + # "This is because the run-times without numba are too slow.\n\n" + # "Please install numba, which is described at the following web page:\n\n" + # "https://pyautolens.readthedocs.io/en/latest/installation/overview.html" + # ) self.data = data self.noise_map = noise_map From 30a6ebc60cfdbca4d9a19ef774bf630bf5c0109a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 16:55:37 +0000 Subject: [PATCH 031/154] dtype, ndim, max, min --- autoarray/abstract_ndarray.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 6e30c78d1..ad202832c 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -169,6 +169,20 @@ def shape(self): def size(self): return self._array.size + @property + def dtype(self): + return self._array.dtype + + @property + def ndim(self): + return self._array.ndim + + def max(self, *args, **kwargs): + return self._array.max(*args, **kwargs) + + def min(self, *args, **kwargs): + return self._array.min(*args, **kwargs) + @to_new_array def reshape(self, *args, **kwargs): return self._array.reshape(*args, **kwargs) From 2b5705a110f0c00397641e58bcb51604075d690c Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 27 Feb 2023 16:57:27 +0000 Subject: [PATCH 032/154] array representation --- autoarray/abstract_ndarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index ad202832c..b1571de3c 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -192,3 +192,6 @@ def __getitem__(self, item): def __setitem__(self, key, value): self._array[key] = value + + def __repr__(self): + return f"{self.__class__.__name__} {self.shape}" From 1393c751937a73db023a6ce5f758d245f97d4fd8 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 10:50:07 +0000 Subject: [PATCH 033/154] __array__ --- autoarray/abstract_ndarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index b1571de3c..587fd4049 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -195,3 +195,6 @@ def __setitem__(self, key, value): def __repr__(self): return f"{self.__class__.__name__} {self.shape}" + + def __array__(self): + return self._array From 6ff48b5d0cc7e38cd3b711381513414bfc5cbf34 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 10:54:55 +0000 Subject: [PATCH 034/154] ignore files/ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b5736e181..109b3605e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +files/ test_autoarray/dataset/files/array/output_test/uv_wavelengths.fits test_autoarray/dataset/files/array/output_test/visibilities.fits test_autoarray/dataset/plot/files/ From 3ca798715535ddc6e415f84ca8553a9dc18105f6 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 10:55:18 +0000 Subject: [PATCH 035/154] mask 1d new -> init --- autoarray/mask/mask_1d.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/autoarray/mask/mask_1d.py b/autoarray/mask/mask_1d.py index a24570de9..6d7b873a6 100644 --- a/autoarray/mask/mask_1d.py +++ b/autoarray/mask/mask_1d.py @@ -1,17 +1,16 @@ from __future__ import annotations + import logging import numpy as np -from typing import TYPE_CHECKING, List, Tuple, Union +from typing import List, Tuple, Union -if TYPE_CHECKING: - from autoarray.structures.grids.uniform_1d import Grid1D - from autoarray.mask.mask_2d import Mask2D from autoarray.mask.abstract_mask import Mask from autoarray.mask.derive.grid_1d import DeriveGrid1D from autoarray.mask.derive.mask_1d import DeriveMask1D from autoarray.geometry.geometry_1d import Geometry1D +from autoarray.structures.abstract_structure import Structure from autoarray.structures.arrays import array_1d_util from autoarray import exc @@ -22,14 +21,16 @@ class Mask1D(Mask): - def __new__( - cls, + @property + def native(self) -> Structure: + raise NotImplemented() + + def __init__( + self, mask: Union[np.ndarray, List], pixel_scales: ty.PixelScales, sub_size: int = 1, - origin: Tuple[ - float, - ] = (0.0,), + origin: Tuple[float,] = (0.0,), invert: bool = False, ): """ @@ -66,12 +67,8 @@ def __new__( raise exc.MaskException("The input mask is not a one dimensional array") # noinspection PyArgumentList - return Mask.__new__( - cls=cls, - mask=mask, - pixel_scales=pixel_scales, - sub_size=sub_size, - origin=origin, + super().__init__( + mask=mask, pixel_scales=pixel_scales, sub_size=sub_size, origin=origin, ) def __array_finalize__(self, obj): From 897586e9f4d50c350b93f8a71442e81e92c1a356 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 11:20:12 +0000 Subject: [PATCH 036/154] array 1d new init conversion --- autoarray/structures/arrays/uniform_1d.py | 24 +++++++++++------- .../mask/files/array/output_test/mask.fits | Bin 5760 -> 0 bytes 2 files changed, 15 insertions(+), 9 deletions(-) delete mode 100644 test_autoarray/mask/files/array/output_test/mask.fits diff --git a/autoarray/structures/arrays/uniform_1d.py b/autoarray/structures/arrays/uniform_1d.py index 339c32a38..57908cde9 100644 --- a/autoarray/structures/arrays/uniform_1d.py +++ b/autoarray/structures/arrays/uniform_1d.py @@ -14,25 +14,31 @@ class Array1D(Structure): - def __new__( - cls, + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + def __init__( + self, values: Union[np.ndarray, List], mask: Mask1D, header: Optional[Header] = None, store_native: bool = False, - *args, - **kwargs ): values = array_1d_util.convert_array_1d( - array_1d=values, mask_1d=mask, store_native=store_native + array_1d=values, mask_1d=mask, store_native=store_native, ) - obj = values.view(cls) - obj.mask = mask - obj.header = header + self.mask = mask + self.header = header - return obj + super().__init__(values) @classmethod def no_mask( diff --git a/test_autoarray/mask/files/array/output_test/mask.fits b/test_autoarray/mask/files/array/output_test/mask.fits deleted file mode 100644 index 7f3001a57fe8170c952244016b87ed9e4a5ea192..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5760 zcmeIu!3u&f9LMn<<^KW^S#*miG8mKy^Wb$g1rKhqt%GmfBQzGFhXq1G5WgLCjz8bY zhqF_DOo`SMSq07LqE)R;YsqOku!56yLP=p{WZoa`Sdp_b+MBMHz9)<)Pu<#cWk}bra?@7IHd@&}GT4RQ{7r8$7#z6`z2tknG~1878DkKD o00bZa0SG_<0>2R0+$Uju`hHgMga8B}009U<00Izzz<&!o0HkV)Hvj+t From d474474961c5c822042163f71a19c6278d8eac76 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 11:36:38 +0000 Subject: [PATCH 037/154] replacing numpy calls with operators to preserve type --- autoarray/dataset/preprocess.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autoarray/dataset/preprocess.py b/autoarray/dataset/preprocess.py index fc2fd4d97..5a7eced8e 100644 --- a/autoarray/dataset/preprocess.py +++ b/autoarray/dataset/preprocess.py @@ -40,7 +40,7 @@ def array_eps_to_counts(array_eps, exposure_time_map): exposure_time_map The exposure time at every data-point of the array. """ - return np.multiply(array_eps, exposure_time_map) + return array_eps * exposure_time_map def array_counts_to_eps(array_counts, exposure_time_map): @@ -59,7 +59,7 @@ def array_counts_to_eps(array_counts, exposure_time_map): exposure_time_map The exposure time at every data-point of the array. """ - return np.divide(array_counts, exposure_time_map) + return array_counts / exposure_time_map def array_eps_to_adus(array_eps, exposure_time_map, gain): @@ -80,7 +80,7 @@ def array_eps_to_adus(array_eps, exposure_time_map, gain): gain The gain of the instrument used in the conversion to / from counts and ADUs. """ - return np.multiply(array_eps, exposure_time_map) / gain + return (array_eps * exposure_time_map) / gain def array_adus_to_eps(array_adus, exposure_time_map, gain): @@ -101,7 +101,7 @@ def array_adus_to_eps(array_adus, exposure_time_map, gain): gain The gain of the instrument used in the conversion to / from counts and ADUs. """ - return np.divide(gain * array_adus, exposure_time_map) + return (gain * array_adus) / exposure_time_map def array_counts_to_counts_per_second(array_counts, exposure_time): From 8e0b83620ea460bdd7344232ed71debce44a6a36 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 11:47:24 +0000 Subject: [PATCH 038/154] converting more functions... --- autoarray/dataset/preprocess.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/autoarray/dataset/preprocess.py b/autoarray/dataset/preprocess.py index 5a7eced8e..0a95fc20d 100644 --- a/autoarray/dataset/preprocess.py +++ b/autoarray/dataset/preprocess.py @@ -149,7 +149,7 @@ def noise_map_via_data_eps_and_exposure_time_map_from(data_eps, exposure_time_ma exposure_time_map The exposure time at every data-point of the data. """ - return np.sqrt(np.abs(data_eps * exposure_time_map)) / exposure_time_map + return (data_eps * exposure_time_map) ** 0.5 / exposure_time_map def noise_map_via_weight_map_from(weight_map): @@ -172,7 +172,7 @@ def noise_map_via_weight_map_from(weight_map): The weight-value of each pixel which is converted to a variance. """ np.seterr(divide="ignore") - noise_map = 1.0 / np.sqrt(weight_map) + noise_map = 1.0 / weight_map ** 0.5 noise_map[noise_map > 1.0e8] = 1.0e8 return noise_map @@ -213,14 +213,12 @@ def noise_map_via_data_eps_exposure_time_map_and_background_noise_map_from( of electrons per second. """ return ( - np.sqrt( - ( - np.abs(data_eps * exposure_time_map) - + np.square(background_noise_map * exposure_time_map) - ) + ( + abs(data_eps * exposure_time_map) + + (background_noise_map * exposure_time_map) ** 2 ) - / exposure_time_map - ) + ** 0.5 + ) / exposure_time_map def noise_map_via_data_eps_exposure_time_map_and_background_variances_from( @@ -241,14 +239,9 @@ def noise_map_via_data_eps_exposure_time_map_and_background_variances_from( of electrons per second. """ return ( - np.sqrt( - ( - np.abs(data_eps * exposure_time_map) - + (background_variances * exposure_time_map) - ) - ) - / exposure_time_map - ) + (abs(data_eps * exposure_time_map) + (background_variances * exposure_time_map)) + ** 0.5 + ) / exposure_time_map def edges_from(image, no_edges): From 86b4d1b39a887523963153b602bb6e242690cf1d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 12:14:31 +0000 Subject: [PATCH 039/154] breaking up test and using allclose --- autoarray/abstract_ndarray.py | 1 + test_autoarray/dataset/test_preprocess.py | 55 ++++++++++++----------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 587fd4049..f6ed1692d 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -64,6 +64,7 @@ def __gt__(self, other): def __ge__(self, other): return self._array >= other + @unwrap_array def __eq__(self, other): return self._array == other diff --git a/test_autoarray/dataset/test_preprocess.py b/test_autoarray/dataset/test_preprocess.py index 58a324778..58efdb674 100644 --- a/test_autoarray/dataset/test_preprocess.py +++ b/test_autoarray/dataset/test_preprocess.py @@ -155,7 +155,7 @@ def test__noise_map_from_image_exposure_time_map_and_background_noise_map(): image = aa.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) exposure_time_map = aa.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) background_noise_map = aa.Array2D.full( - fill_value=3.0**0.5, shape_native=(3, 3), pixel_scales=1.0 + fill_value=3.0 ** 0.5, shape_native=(3, 3), pixel_scales=1.0 ) noise_map = aa.preprocess.noise_map_via_data_eps_exposure_time_map_and_background_noise_map_from( @@ -262,8 +262,8 @@ def test__noise_map_from_image_exposure_time_map_and_background_noise_map(): assert noise_map.native == pytest.approx( np.array( [ - [np.sqrt(5.0 + 81.0), np.sqrt(6.0 + 18.0**2.0) / 2.0], - [np.sqrt(30.0 + 27.0**2.0) / 3.0, np.sqrt(80.0 + 36.0**2.0) / 4.0], + [np.sqrt(5.0 + 81.0), np.sqrt(6.0 + 18.0 ** 2.0) / 2.0], + [np.sqrt(30.0 + 27.0 ** 2.0) / 3.0, np.sqrt(80.0 + 36.0 ** 2.0) / 4.0], ] ), 1e-2, @@ -286,8 +286,8 @@ def test__noise_map_from_image_exposure_time_map_and_background_noise_map(): assert noise_map.native == pytest.approx( np.array( [ - [np.sqrt(5.0 + 5.0**2.0), np.sqrt(6.0 + 12.0**2.0) / 2.0], - [np.sqrt(30.0 + 21.0**2.0) / 3.0, np.sqrt(80.0 + 32.0**2.0) / 4.0], + [np.sqrt(5.0 + 5.0 ** 2.0), np.sqrt(6.0 + 12.0 ** 2.0) / 2.0], + [np.sqrt(30.0 + 21.0 ** 2.0) / 3.0, np.sqrt(80.0 + 32.0 ** 2.0) / 4.0], ] ), 1e-2, @@ -299,7 +299,7 @@ def test__noise_map_from_image_exposure_time_map_and_background_variances(): image = aa.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) exposure_time_map = aa.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) background_variances = aa.Array2D.full( - fill_value=3.0**0.5, shape_native=(3, 3), pixel_scales=1.0 + fill_value=3.0 ** 0.5, shape_native=(3, 3), pixel_scales=1.0 ) noise_map = aa.preprocess.noise_map_via_data_eps_exposure_time_map_and_background_variances_from( @@ -420,6 +420,8 @@ def test__background_noise_map_via_edges_of_image_from(): background_noise_map.native == np.full(fill_value=0.0, shape=image.shape_native) ).all() + +def test__background_noise_map_via_edges_of_image_from_2(): image = aa.Array2D.no_mask( values=[[1, 1, 1, 1], [1, 100, 100, 1], [1, 100, 100, 1], [1, 1, 1, 1]], pixel_scales=1.0, @@ -433,6 +435,8 @@ def test__background_noise_map_via_edges_of_image_from(): background_noise_map.native == np.full(fill_value=0.0, shape=image.shape_native) ).all() + +def test__background_noise_map_via_edges_of_image_from_3(): image = aa.Array2D.no_mask( [ [1, 1, 1, 1, 1], @@ -452,6 +456,8 @@ def test__background_noise_map_via_edges_of_image_from(): background_noise_map.native == np.full(fill_value=0.0, shape=image.shape_native) ).all() + +def test__background_noise_map_via_edges_of_image_from_4(): image = aa.Array2D.no_mask( [ [0, 1, 2, 3, 4], @@ -468,10 +474,13 @@ def test__background_noise_map_via_edges_of_image_from(): image=image, no_edges=2 ) - assert ( - background_noise_map.native - == np.full(fill_value=np.std(np.arange(28)), shape=image.shape_native) - ).all() + assert np.allclose( + background_noise_map.native, + np.full(fill_value=np.std(np.arange(28)), shape=image.shape_native), + ) + + +def test__background_noise_map_via_edges_of_image_from_5(): image = aa.Array2D.no_mask( [ @@ -490,10 +499,10 @@ def test__background_noise_map_via_edges_of_image_from(): image=image, no_edges=3 ) - assert ( - background_noise_map.native - == np.full(fill_value=np.std(np.arange(48)), shape=image.shape_native) - ).all() + assert np.allclose( + background_noise_map.native, + np.full(fill_value=np.std(np.arange(48)), shape=image.shape_native), + ) def test__exposure_time_map_from_exposure_time_and_inverse_noise_map(): @@ -504,10 +513,8 @@ def test__exposure_time_map_from_exposure_time_and_inverse_noise_map(): ) background_noise_map[0] = 0.5 - exposure_time_map = ( - aa.preprocess.exposure_time_map_via_exposure_time_and_background_noise_map_from( - exposure_time=exposure_time, background_noise_map=background_noise_map - ) + exposure_time_map = aa.preprocess.exposure_time_map_via_exposure_time_and_background_noise_map_from( + exposure_time=exposure_time, background_noise_map=background_noise_map ) assert ( @@ -729,18 +736,14 @@ def test__visibilities_noise_map_with_signal_to_noise_limit( data = aa.Visibilities(visibilities=np.array([1 + 1j, 1 + 1j])) noise_map = aa.VisibilitiesNoiseMap(visibilities=np.array([1 + 0.25j, 1 + 0.25j])) - noise_map_limit = ( - aa.preprocess.visibilities_noise_map_with_signal_to_noise_limit_from( - data=data, noise_map=noise_map, signal_to_noise_limit=2.0 - ) + noise_map_limit = aa.preprocess.visibilities_noise_map_with_signal_to_noise_limit_from( + data=data, noise_map=noise_map, signal_to_noise_limit=2.0 ) assert (noise_map_limit == np.array([1.0 + 0.5j, 1.0 + 0.5j])).all() - noise_map_limit = ( - aa.preprocess.visibilities_noise_map_with_signal_to_noise_limit_from( - data=data, noise_map=noise_map, signal_to_noise_limit=0.25 - ) + noise_map_limit = aa.preprocess.visibilities_noise_map_with_signal_to_noise_limit_from( + data=data, noise_map=noise_map, signal_to_noise_limit=0.25 ) assert (noise_map_limit == np.array([4.0 + 4.0j, 4.0 + 4.0j])).all() From 044b3e3375a16b6b4937a84d8931bd63329c762d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 12:25:30 +0000 Subject: [PATCH 040/154] fixing more preprocessing functions... --- autoarray/abstract_ndarray.py | 3 +++ autoarray/dataset/preprocess.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index f6ed1692d..7317dcca3 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -199,3 +199,6 @@ def __repr__(self): def __array__(self): return self._array + + def __len__(self): + return len(self._array) diff --git a/autoarray/dataset/preprocess.py b/autoarray/dataset/preprocess.py index 0a95fc20d..f12490a03 100644 --- a/autoarray/dataset/preprocess.py +++ b/autoarray/dataset/preprocess.py @@ -364,7 +364,7 @@ def exposure_time_map_via_exposure_time_and_background_noise_map_from( relative_background_noise_map = inverse_background_noise_map / np.max( inverse_background_noise_map ) - return np.abs(exposure_time * (relative_background_noise_map)) + return abs(exposure_time * relative_background_noise_map) def setup_random_seed(seed): @@ -507,7 +507,7 @@ def noise_map_with_signal_to_noise_limit_from( # TODO : Refacotr into a util - signal_to_noise_map = np.divide(data, noise_map) + signal_to_noise_map = data / noise_map signal_to_noise_map[signal_to_noise_map < 0] = 0 if noise_limit_mask is None: From bd0df13691d0d868c1c98e23fa931f60542cef52 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 12:33:35 +0000 Subject: [PATCH 041/154] new -> init for visibilities --- autoarray/structures/visibilities.py | 40 +++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/autoarray/structures/visibilities.py b/autoarray/structures/visibilities.py index f4b3d2f20..ce1f9a6db 100644 --- a/autoarray/structures/visibilities.py +++ b/autoarray/structures/visibilities.py @@ -1,3 +1,5 @@ +from abc import ABC + import logging import numpy as np from typing import List, Tuple, Union @@ -13,10 +15,10 @@ logger = logging.getLogger(__name__) -class AbstractVisibilities(Structure): +class AbstractVisibilities(Structure, ABC): # noinspection PyUnusedLocal - def __new__(cls, visibilities: Union[np.ndarray, List[complex]], *args, **kwargs): + def __init__(self, visibilities: Union[np.ndarray, List[complex]]): """ A collection of (real, imag) visibilities which are used to represent the data in an `Interferometer` dataset. @@ -49,13 +51,11 @@ def __new__(cls, visibilities: Union[np.ndarray, List[complex]], *args, **kwargs .ravel() ) - obj = visibilities.view(cls) - - obj.ordered_1d = np.concatenate( + self.ordered_1d = np.concatenate( (np.real(visibilities), np.imag(visibilities)), axis=0 ) - return obj + super().__init__(array=self.ordered_1d) def __array_finalize__(self, obj): @@ -132,6 +132,19 @@ def scaled_minima(self) -> Tuple[float, float]: class Visibilities(AbstractVisibilities): + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + @classmethod def full(cls, fill_value: float, shape_slim: Tuple[int]) -> "Visibilities": """ @@ -211,7 +224,7 @@ def from_fits(cls, file_path: str, hdu: int) -> "Visibilities": class VisibilitiesNoiseMap(Visibilities): # noinspection PyUnusedLocal - def __new__(cls, visibilities: Union[np.ndarray, List[complex]], *args, **kwargs): + def __init__(self, visibilities: Union[np.ndarray, List[complex]], *args, **kwargs): """ A collection of (real, imag) visibilities noise-map values which are used to represent the noise-map in an `Interferometer` dataset. @@ -237,22 +250,17 @@ def __new__(cls, visibilities: Union[np.ndarray, List[complex]], *args, **kwargs .ravel() ) - obj = super(VisibilitiesNoiseMap, cls).__new__( - cls=cls, visibilities=visibilities - ) - - obj.ordered_1d = np.concatenate( + self.ordered_1d = np.concatenate( (np.real(visibilities), np.imag(visibilities)), axis=0 ) + super().__init__(visibilities=visibilities) - weight_list = 1.0 / obj.in_array**2.0 + weight_list = 1.0 / self.in_array ** 2.0 - obj.weight_list_ordered_1d = np.concatenate( + self.weight_list_ordered_1d = np.concatenate( (weight_list[:, 0], weight_list[:, 1]), axis=0 ) - return obj - def __array_finalize__(self, obj): if hasattr(obj, "ordered_1d"): From a8e772a4985d0de7c78675b2dedf3f2c005aed40 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 12:50:42 +0000 Subject: [PATCH 042/154] fixed visibilities init --- autoarray/structures/visibilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/visibilities.py b/autoarray/structures/visibilities.py index ce1f9a6db..a07211097 100644 --- a/autoarray/structures/visibilities.py +++ b/autoarray/structures/visibilities.py @@ -55,7 +55,7 @@ def __init__(self, visibilities: Union[np.ndarray, List[complex]]): (np.real(visibilities), np.imag(visibilities)), axis=0 ) - super().__init__(array=self.ordered_1d) + super().__init__(array=visibilities) def __array_finalize__(self, obj): From 328d76560392aa10900515e6d42812bc497afa97 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 12:54:27 +0000 Subject: [PATCH 043/154] more moving away from np functions --- autoarray/dataset/abstract/dataset.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/autoarray/dataset/abstract/dataset.py b/autoarray/dataset/abstract/dataset.py index 133561eee..6a1f3ec34 100644 --- a/autoarray/dataset/abstract/dataset.py +++ b/autoarray/dataset/abstract/dataset.py @@ -1,19 +1,17 @@ import copy import logging import numpy as np -from typing import Optional, Union import warnings +from typing import Optional, Union -from autoconf import cached_property -from autoconf import conf - +from autoarray import exc from autoarray.dataset.abstract.settings import AbstractSettingsDataset -from autoarray.structures.abstract_structure import Structure -from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.mask.mask_1d import Mask1D from autoarray.mask.mask_2d import Mask2D - -from autoarray import exc +from autoarray.structures.abstract_structure import Structure +from autoarray.structures.arrays.uniform_2d import Array2D +from autoconf import cached_property +from autoconf import conf logger = logging.getLogger(__name__) @@ -116,12 +114,12 @@ def signal_to_noise_map(self) -> Structure: """ The estimated signal-to-noise_maps mappers of the image. - Warnings airse when masked native noise-maps are used, whose masked entries are given values of 0.0. We - uses the warnings module to surpress these RunTimeWarnings. + Warnings arise when masked native noise-maps are used, whose masked entries are given values of 0.0. We + use the warnings module to suppress these RunTimeWarnings. """ warnings.filterwarnings("ignore") - signal_to_noise_map = np.divide(self.data, self.noise_map) + signal_to_noise_map = self.data / self.noise_map signal_to_noise_map[signal_to_noise_map < 0] = 0 return signal_to_noise_map @@ -137,7 +135,7 @@ def absolute_signal_to_noise_map(self) -> Structure: """ The estimated absolute_signal-to-noise_maps mappers of the image. """ - return np.divide(np.abs(self.data), self.noise_map) + return abs(self.data) / self.noise_map @property def absolute_signal_to_noise_max(self) -> float: @@ -152,7 +150,7 @@ def potential_chi_squared_map(self) -> Structure: The potential chi-squared-map of the imaging data_type. This represents how much each pixel can contribute to the chi-squared-map, assuming the model fails to fit it at all (e.g. model value = 0.0). """ - return np.square(self.absolute_signal_to_noise_map) + return self.absolute_signal_to_noise_map ** 2 @property def potential_chi_squared_max(self) -> float: From ae4701cf8bb57b8c30f4f2dab0b2362d40f2738a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 15:48:20 +0000 Subject: [PATCH 044/154] astype --- autoarray/abstract_ndarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 7317dcca3..0d55ee634 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -202,3 +202,7 @@ def __array__(self): def __len__(self): return len(self._array) + + @to_new_array + def astype(self, dtype): + return self._array.astype(dtype) From 6f0f4907f71b773da068496a63b9efaec3d0409d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 15:48:30 +0000 Subject: [PATCH 045/154] grid1d new -> init --- autoarray/structures/grids/uniform_1d.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/autoarray/structures/grids/uniform_1d.py b/autoarray/structures/grids/uniform_1d.py index afba85047..ba2e5e3ea 100644 --- a/autoarray/structures/grids/uniform_1d.py +++ b/autoarray/structures/grids/uniform_1d.py @@ -21,13 +21,8 @@ class Grid1D(Structure): - def __new__( - cls, - values: Union[np.ndarray, List], - mask: Mask1D, - store_native: bool = False, - *args, - **kwargs, + def __init__( + self, values: Union[np.ndarray, List], mask: Mask1D, store_native: bool = False, ): """ A grid of 1D (x) coordinates, which are paired to a uniform 1D mask of pixels and sub-pixels. Each entry @@ -193,10 +188,9 @@ def __new__( grid_1d=values, mask_1d=mask, store_native=store_native ) - obj = values.view(cls) - obj.mask = mask + self.mask = mask - return obj + super().__init__(values) @classmethod def no_mask( From 2dfa95ebdd6f78e0fae4836279e1033b9fcdd00f Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 15:50:06 +0000 Subject: [PATCH 046/154] grid2d iterate new -> init a --- autoarray/structures/grids/iterate_2d.py | 35 +++++++++++------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/autoarray/structures/grids/iterate_2d.py b/autoarray/structures/grids/iterate_2d.py index b96b98092..00686254e 100644 --- a/autoarray/structures/grids/iterate_2d.py +++ b/autoarray/structures/grids/iterate_2d.py @@ -1,17 +1,14 @@ import numpy as np from typing import Callable, Union, List, Tuple, Optional -from autoarray.mask.mask_2d import Mask2D -from autoarray.structures.grids.uniform_2d import Grid2D - -from autoarray.structures.arrays.uniform_2d import Array2D - -from autoarray.structures.arrays import array_2d_util from autoarray import numba_util +from autoarray import type as ty from autoarray.geometry import geometry_util +from autoarray.mask.mask_2d import Mask2D +from autoarray.structures.arrays import array_2d_util +from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.structures.grids import grid_2d_util - -from autoarray import type as ty +from autoarray.structures.grids.uniform_2d import Grid2D def sub_steps_from(sub_steps): @@ -22,16 +19,14 @@ def sub_steps_from(sub_steps): class Grid2DIterate(Grid2D): - def __new__( - cls, + def __init__( + self, values: np.ndarray, mask: Mask2D, fractional_accuracy: float = 0.9999, relative_accuracy: Optional[float] = None, sub_steps: List[int] = None, store_native: bool = False, - *args, - **kwargs ): """ Represents a grid of coordinates as described for the ``Grid2D`` class, but using an iterative sub-grid that @@ -80,14 +75,16 @@ def __new__( grid_2d=values, mask_2d=mask, store_native=store_native ) - obj = values.view(cls) - obj.mask = mask - obj.grid = Grid2D(values=np.asarray(obj), mask=mask, store_native=store_native) - obj.fractional_accuracy = fractional_accuracy - obj.relative_accuracy = relative_accuracy - obj.sub_steps = sub_steps + self.grid = Grid2D( + values=np.asarray(values), mask=mask, store_native=store_native + ) + self.fractional_accuracy = fractional_accuracy + self.relative_accuracy = relative_accuracy + self.sub_steps = sub_steps - return obj + super().__init__( + values=values, mask=mask, store_native=store_native, + ) def __array_finalize__(self, obj): From f72b9ccc7118625a5b4f97fa520b99a8cd3d68f8 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 15:58:28 +0000 Subject: [PATCH 047/154] import and format --- test_autoarray/dataset/abstract/test_dataset.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test_autoarray/dataset/abstract/test_dataset.py b/test_autoarray/dataset/abstract/test_dataset.py index 06b9668ab..95a43a45d 100644 --- a/test_autoarray/dataset/abstract/test_dataset.py +++ b/test_autoarray/dataset/abstract/test_dataset.py @@ -1,9 +1,7 @@ import logging import numpy as np -import pytest import autoarray as aa - from autoarray.dataset.abstract import dataset as ds logger = logging.getLogger(__name__) @@ -87,7 +85,7 @@ def test__potential_chi_squared_map(): assert ( dataset.potential_chi_squared_map.native - == np.array([[0.1**2.0, 0.2**2.0], [0.1**2.0, 1.0**2.0]]) + == np.array([[0.1 ** 2.0, 0.2 ** 2.0], [0.1 ** 2.0, 1.0 ** 2.0]]) ).all() assert dataset.potential_chi_squared_max == 1.0 From 0f3d8ce4ea7eccd10bf395f4ec2fcb148eec6829 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 16:17:21 +0000 Subject: [PATCH 048/154] more new->init --- autoarray/structures/mesh/rectangular_2d.py | 31 ++++++++++++++----- autoarray/structures/mesh/triangulation_2d.py | 31 ++++++++++++++----- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/autoarray/structures/mesh/rectangular_2d.py b/autoarray/structures/mesh/rectangular_2d.py index 5641e515e..5e2f83f4e 100644 --- a/autoarray/structures/mesh/rectangular_2d.py +++ b/autoarray/structures/mesh/rectangular_2d.py @@ -1,7 +1,8 @@ import numpy as np from scipy.interpolate import griddata -from typing import Optional, Tuple +from typing import Optional, Tuple, List +from autoarray.structures.abstract_structure import Structure from autoconf import cached_property from autoarray.inversion.linear_obj.neighbors import Neighbors @@ -15,14 +16,29 @@ class Mesh2DRectangular(Abstract2DMesh): - def __new__( - cls, + @property + def slim(self) -> "Structure": + raise NotImplementedError() + + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + + def __init__( + self, values: np.ndarray, shape_native: Tuple[int, int], pixel_scales: ty.PixelScales, origin: Tuple[float, float] = (0.0, 0.0), - *args, - **kwargs ): """ A grid of (y,x) coordinates which represent a uniform rectangular pixelization. @@ -61,10 +77,9 @@ def __new__( origin=origin, ) - obj = values.view(cls) - obj.mask = mask + self.mask = mask - return obj + super().__init__(array=values) @classmethod def overlay_grid( diff --git a/autoarray/structures/mesh/triangulation_2d.py b/autoarray/structures/mesh/triangulation_2d.py index dc40a0772..95fffa00d 100644 --- a/autoarray/structures/mesh/triangulation_2d.py +++ b/autoarray/structures/mesh/triangulation_2d.py @@ -3,6 +3,7 @@ import scipy.spatial.qhull as qhull from typing import Optional, List, Union, Tuple +from autoarray.structures.abstract_structure import Structure from autoconf import cached_property from autoarray.geometry.geometry_2d_irregular import Geometry2DIrregular @@ -13,13 +14,28 @@ class Abstract2DMeshTriangulation(Abstract2DMesh): - def __new__( - cls, + @property + def slim(self) -> "Structure": + raise NotImplementedError() + + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + + def __init__( + self, values: Union[np.ndarray, List], nearest_pixelization_index_for_slim_index: Optional[np.ndarray] = None, uses_interpolation: bool = False, - *args, - **kwargs ): """ An irregular 2D grid of (y,x) coordinates which represents both a Delaunay triangulation and Voronoi mesh. @@ -55,13 +71,12 @@ def __new__( if type(values) is list: values = np.asarray(values) - obj = values.view(cls) - obj.nearest_pixelization_index_for_slim_index = ( + self.nearest_pixelization_index_for_slim_index = ( nearest_pixelization_index_for_slim_index ) - obj.uses_interpolation = uses_interpolation + self.uses_interpolation = uses_interpolation - return obj + super().__init__(values) def __array_finalize__(self, obj: object): """ From 0b9d2dbc003f52c87ab86a3aba3dd1784d997c4a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 16:21:51 +0000 Subject: [PATCH 049/154] real and imag --- autoarray/abstract_ndarray.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 0d55ee634..f2e3a6eaf 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -206,3 +206,11 @@ def __len__(self): @to_new_array def astype(self, dtype): return self._array.astype(dtype) + + @property + def real(self): + return self._array.real + + @property + def imag(self): + return self._array.imag From 1b17c4ef9c0cd6ae240b0e4b530ce45e056a8214 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 16:26:50 +0000 Subject: [PATCH 050/154] all --- autoarray/abstract_ndarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index f2e3a6eaf..17a190915 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -214,3 +214,6 @@ def real(self): @property def imag(self): return self._array.imag + + def all(self): + return self._array.all() From f8e5c154b23be9b301d1e5b13cb6f6cf0a35b05d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Mar 2023 16:51:23 +0000 Subject: [PATCH 051/154] converting fit util methods. Some used out= but motivation is unclear --- autoarray/abstract_ndarray.py | 2 ++ autoarray/fit/fit_dataset.py | 6 ++-- autoarray/fit/fit_interferometer.py | 10 +++--- autoarray/fit/fit_util.py | 47 +++++++++-------------------- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 17a190915..dd5fc30e1 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -208,10 +208,12 @@ def astype(self, dtype): return self._array.astype(dtype) @property + @to_new_array def real(self): return self._array.real @property + @to_new_array def imag(self): return self._array.imag diff --git a/autoarray/fit/fit_dataset.py b/autoarray/fit/fit_dataset.py index e5d8452d5..3ea3281f5 100644 --- a/autoarray/fit/fit_dataset.py +++ b/autoarray/fit/fit_dataset.py @@ -39,7 +39,7 @@ def signal_to_noise_map(self) -> ty.DataLike: """ warnings.filterwarnings("ignore") - signal_to_noise_map = np.divide(self.data, self.noise_map) + signal_to_noise_map = self.data / self.noise_map signal_to_noise_map[signal_to_noise_map < 0] = 0 return signal_to_noise_map @@ -49,8 +49,8 @@ def potential_chi_squared_map(self) -> ty.DataLike: The signal-to-noise_map of the dataset and noise-map which are fitted. """ warnings.filterwarnings("ignore") - absolute_signal_to_noise_map = np.divide(np.abs(self.data), self.noise_map) - return np.square(absolute_signal_to_noise_map) + absolute_signal_to_noise_map = abs(self.data / self.noise_map) + return absolute_signal_to_noise_map ** 2 @property def residual_map(self) -> Structure: diff --git a/autoarray/fit/fit_interferometer.py b/autoarray/fit/fit_interferometer.py index 465252c38..08ce9e822 100644 --- a/autoarray/fit/fit_interferometer.py +++ b/autoarray/fit/fit_interferometer.py @@ -100,13 +100,11 @@ def chi_squared_map(self) -> np.ndarray: @property def signal_to_noise_map(self) -> np.ndarray: """The signal-to-noise_map of the dataset and noise-map which are fitted.""" - signal_to_noise_map_real = np.divide( - np.real(self.data), np.real(self.noise_map) - ) + signal_to_noise_map_real = self.data.real / self.noise_map.real + signal_to_noise_map_real[signal_to_noise_map_real < 0] = 0.0 - signal_to_noise_map_imag = np.divide( - np.imag(self.data), np.imag(self.noise_map) - ) + signal_to_noise_map_imag = self.data.imag / self.noise_map.imag + signal_to_noise_map_imag[signal_to_noise_map_imag < 0] = 0.0 return signal_to_noise_map_real + 1.0j * signal_to_noise_map_imag diff --git a/autoarray/fit/fit_util.py b/autoarray/fit/fit_util.py index f90facf41..37e2a2873 100644 --- a/autoarray/fit/fit_util.py +++ b/autoarray/fit/fit_util.py @@ -19,7 +19,7 @@ def residual_map_from(*, data: Structure, model_data: Structure) -> Structure: model_data The model data used to fit the data. """ - return np.subtract(data, model_data, out=np.zeros_like(data)) + return data - model_data def normalized_residual_map_from( @@ -39,7 +39,7 @@ def normalized_residual_map_from( mask The mask applied to the residual-map, where `False` entries are included in the calculation. """ - return np.divide(residual_map, noise_map, out=np.zeros_like(residual_map)) + return residual_map / noise_map def chi_squared_map_from(*, residual_map: Structure, noise_map: Structure) -> Structure: @@ -55,9 +55,7 @@ def chi_squared_map_from(*, residual_map: Structure, noise_map: Structure) -> St noise_map The noise-map of the dataset. """ - return np.square( - np.divide(residual_map, noise_map, out=np.zeros_like(residual_map)) - ) + return (residual_map / noise_map) ** 2.0 def chi_squared_from(*, chi_squared_map: Structure) -> float: @@ -83,7 +81,7 @@ def noise_normalization_from(*, noise_map: Structure) -> float: noise_map The masked noise-map of the dataset. """ - return float(np.sum(np.log(2 * np.pi * noise_map**2.0))) + return float(np.sum(np.log(2 * np.pi * noise_map ** 2.0))) def normalized_residual_map_complex_from( @@ -101,16 +99,11 @@ def normalized_residual_map_complex_from( noise_map The noise-map of the dataset. """ - normalized_residual_map_real = np.divide( - residual_map.real, - noise_map.real, - out=np.zeros_like(residual_map, dtype="complex128"), + normalized_residual_map_real = (residual_map.real / noise_map.real).astype( + "complex128" ) - - normalized_residual_map_imag = np.divide( - residual_map.imag, - noise_map.imag, - out=np.zeros_like(residual_map, dtype="complex128"), + normalized_residual_map_imag = (residual_map.imag / noise_map.imag).astype( + "complex128" ) return normalized_residual_map_real + 1j * normalized_residual_map_imag @@ -131,12 +124,8 @@ def chi_squared_map_complex_from( noise_map The noise-map of the dataset. """ - chi_squared_map_real = np.square( - np.divide(residual_map.real, noise_map.real, out=np.zeros_like(residual_map)) - ) - chi_squared_map_imag = np.square( - np.divide(residual_map.imag, noise_map.imag, out=np.zeros_like(residual_map)) - ) + chi_squared_map_real = (residual_map.real / noise_map.real) ** 2 + chi_squared_map_imag = (residual_map.imag / noise_map.imag) ** 2 return chi_squared_map_real + 1j * chi_squared_map_imag @@ -168,8 +157,8 @@ def noise_normalization_complex_from(*, noise_map: Structure) -> float: noise_map The masked noise-map of the dataset. """ - noise_normalization_real = float(np.sum(np.log(2 * np.pi * noise_map.real**2.0))) - noise_normalization_imag = float(np.sum(np.log(2 * np.pi * noise_map.imag**2.0))) + noise_normalization_real = float(np.sum(np.log(2 * np.pi * noise_map.real ** 2.0))) + noise_normalization_imag = float(np.sum(np.log(2 * np.pi * noise_map.imag ** 2.0))) return noise_normalization_real + noise_normalization_imag @@ -296,15 +285,9 @@ def chi_squared_with_mask_fast_from( return float( np.sum( - np.square( - np.divide( - np.subtract( - data, - model_data, - ), - noise_map, - ) - )[np.asarray(mask) == 0] + np.square(np.divide(np.subtract(data, model_data,), noise_map,))[ + np.asarray(mask) == 0 + ] ) ) From 626c4531166b13806469f34ea48a5761cbb116f6 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 09:44:45 +0000 Subject: [PATCH 052/154] added with_new_array method and used for two fit fieldds --- autoarray/abstract_ndarray.py | 9 ++++++--- autoarray/fit/fit_util.py | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index dd5fc30e1..979762a05 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -16,9 +16,7 @@ def to_new_array(func): def wrapper(self, *args, **kwargs): - new_array = copy(self) - new_array._array = func(self, *args, **kwargs) - return new_array + return self.with_new_array(func(self, *args, **kwargs)) return wrapper @@ -37,6 +35,11 @@ class AbstractNDArray(ABC): def __init__(self, array): self._array = array + def with_new_array(self, array): + new_array = copy(self) + new_array._array = array + return new_array + def __iter__(self): return iter(self._array) diff --git a/autoarray/fit/fit_util.py b/autoarray/fit/fit_util.py index 37e2a2873..8d7776998 100644 --- a/autoarray/fit/fit_util.py +++ b/autoarray/fit/fit_util.py @@ -343,7 +343,9 @@ def normalized_residual_map_complex_with_mask_from( where=np.asarray(mask) == 0, ) - return normalized_residual_map_real + 1j * normalized_residual_map_imag + return residual_map.with_new_array( + normalized_residual_map_real + 1j * normalized_residual_map_imag + ) def chi_squared_map_complex_with_mask_from( @@ -382,7 +384,7 @@ def chi_squared_map_complex_with_mask_from( where=np.asarray(mask) == 0, ) ) - return chi_squared_map_real + 1j * chi_squared_map_imag + return residual_map.with_new_array(chi_squared_map_real + 1j * chi_squared_map_imag) def chi_squared_complex_with_mask_from( From 3c8a61c358b447753cb43fd9ac822754430fa197 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 09:47:06 +0000 Subject: [PATCH 053/154] optionally pass dtype when calling abstract nd array's array method --- autoarray/abstract_ndarray.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 979762a05..ee864f808 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -200,7 +200,9 @@ def __setitem__(self, key, value): def __repr__(self): return f"{self.__class__.__name__} {self.shape}" - def __array__(self): + def __array__(self, dtype=None): + if dtype: + return self._array.astype(dtype) return self._array def __len__(self): From e76ce36d09f6879a303742e009bc96ffb0ef5e3e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 09:47:13 +0000 Subject: [PATCH 054/154] fix return type --- autoarray/fit/fit_interferometer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autoarray/fit/fit_interferometer.py b/autoarray/fit/fit_interferometer.py index 08ce9e822..5cb0c41bc 100644 --- a/autoarray/fit/fit_interferometer.py +++ b/autoarray/fit/fit_interferometer.py @@ -2,6 +2,7 @@ from typing import Dict, Optional, Union from autoarray.dataset.interferometer.interferometer import Interferometer +from autoarray.structures.abstract_structure import Structure from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.structures.visibilities import Visibilities from autoarray.fit.fit_dataset import FitDataset @@ -76,7 +77,7 @@ def model_visibilities(self) -> Visibilities: return self.model_data @property - def normalized_residual_map(self) -> np.ndarray: + def normalized_residual_map(self) -> Structure: """ Returns the normalized residual-map between the masked dataset and model data, where: From ed08121378844f491a1b0c708889b1a289287254 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 10:15:34 +0000 Subject: [PATCH 055/154] all close --- test_autoarray/dataset/imaging/test_simulator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_autoarray/dataset/imaging/test_simulator.py b/test_autoarray/dataset/imaging/test_simulator.py index 1da6e999a..bacd52790 100644 --- a/test_autoarray/dataset/imaging/test_simulator.py +++ b/test_autoarray/dataset/imaging/test_simulator.py @@ -40,7 +40,7 @@ def test__via_image_from__noise_off__noise_map_is_noise_value(image_central_delt imaging.image.native == np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]]) ).all() - assert (imaging.noise_map.native == 0.2 * np.ones((3, 3))).all() + assert np.allclose(imaging.noise_map.native, 0.2 * np.ones((3, 3))) def test__via_image_from__psf_blurs_image_with_edge_trimming(image_central_delta_3x3): From ffff8cae24a714e30c26fa9a02eb3d0e9a41b30e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:28:00 +0000 Subject: [PATCH 056/154] to_new_array decorator for remaining fit_util functions --- .../dataset/interferometer/interferometer.py | 8 +++--- autoarray/fit/fit_util.py | 25 ++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/autoarray/dataset/interferometer/interferometer.py b/autoarray/dataset/interferometer/interferometer.py index 196a39841..ca5d21c0b 100644 --- a/autoarray/dataset/interferometer/interferometer.py +++ b/autoarray/dataset/interferometer/interferometer.py @@ -136,8 +136,8 @@ def w_tilde(self): ) dirty_image = self.transformer.image_from( - visibilities=self.visibilities.real * self.noise_map.real**-2.0 - + 1j * self.visibilities.imag * self.noise_map.imag**-2.0, + visibilities=self.visibilities.real * self.noise_map.real ** -2.0 + + 1j * self.visibilities.imag * self.noise_map.imag ** -2.0, use_adjoint_scaling=True, ) @@ -199,7 +199,9 @@ def signal_to_noise_map(self): ) signal_to_noise_map_imag[signal_to_noise_map_imag < 0] = 0.0 - return signal_to_noise_map_real + 1j * signal_to_noise_map_imag + return self.data.with_new_array( + signal_to_noise_map_real + 1j * signal_to_noise_map_imag + ) @property def blurring_grid(self): diff --git a/autoarray/fit/fit_util.py b/autoarray/fit/fit_util.py index 8d7776998..1dfe2a8d8 100644 --- a/autoarray/fit/fit_util.py +++ b/autoarray/fit/fit_util.py @@ -1,9 +1,23 @@ +from functools import wraps + import numpy as np from autoarray.mask.abstract_mask import Mask from autoarray.structures.abstract_structure import Structure +def to_new_array(func): + @wraps(func) + def wrapper(**kwargs): + result = func(**kwargs) + try: + return list(kwargs.values())[0].with_new_array(result) + except AttributeError: + return result + + return wrapper + + def residual_map_from(*, data: Structure, model_data: Structure) -> Structure: """ Returns the residual-map of the fit of model-data to a masked dataset, where: @@ -162,6 +176,7 @@ def noise_normalization_complex_from(*, noise_map: Structure) -> float: return noise_normalization_real + noise_normalization_imag +@to_new_array def residual_map_with_mask_from( *, data: Structure, mask: Mask, model_data: Structure ) -> Structure: @@ -186,6 +201,7 @@ def residual_map_with_mask_from( ) +@to_new_array def normalized_residual_map_with_mask_from( *, residual_map: Structure, noise_map: Structure, mask: Mask ) -> Structure: @@ -213,6 +229,7 @@ def normalized_residual_map_with_mask_from( ) +@to_new_array def chi_squared_map_with_mask_from( *, residual_map: Structure, noise_map: Structure, mask: Mask ) -> Structure: @@ -310,6 +327,7 @@ def noise_normalization_with_mask_from(*, noise_map: Structure, mask: Mask) -> f return float(np.sum(np.log(2 * np.pi * noise_map[np.asarray(mask) == 0] ** 2.0))) +@to_new_array def normalized_residual_map_complex_with_mask_from( *, residual_map: Structure, noise_map: Structure, mask: Mask ) -> Structure: @@ -343,11 +361,10 @@ def normalized_residual_map_complex_with_mask_from( where=np.asarray(mask) == 0, ) - return residual_map.with_new_array( - normalized_residual_map_real + 1j * normalized_residual_map_imag - ) + return normalized_residual_map_real + 1j * normalized_residual_map_imag +@to_new_array def chi_squared_map_complex_with_mask_from( *, residual_map: Structure, noise_map: Structure, mask: Mask ) -> Structure: @@ -384,7 +401,7 @@ def chi_squared_map_complex_with_mask_from( where=np.asarray(mask) == 0, ) ) - return residual_map.with_new_array(chi_squared_map_real + 1j * chi_squared_map_imag) + return chi_squared_map_real + 1j * chi_squared_map_imag def chi_squared_complex_with_mask_from( From 5df08142f32db3aa7a2e1c2c3e346f8e65f543a1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:31:35 +0000 Subject: [PATCH 057/154] new->init for grid2dsparse --- autoarray/structures/grids/sparse_2d.py | 50 ++++++++++++++++--------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/autoarray/structures/grids/sparse_2d.py b/autoarray/structures/grids/sparse_2d.py index a3634884d..09b849bd8 100644 --- a/autoarray/structures/grids/sparse_2d.py +++ b/autoarray/structures/grids/sparse_2d.py @@ -1,7 +1,7 @@ from __future__ import annotations import numpy as np from sklearn.cluster import KMeans -from typing import TYPE_CHECKING, Optional, Tuple +from typing import TYPE_CHECKING, Optional, Tuple, List import warnings if TYPE_CHECKING: @@ -17,7 +17,24 @@ class Grid2DSparse(Structure): - def __new__(cls, values: np.ndarray, sparse_index_for_slim_index: np.ndarray): + @property + def slim(self) -> "Structure": + raise NotImplemented() + + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplemented() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplemented() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplemented() + + @property + def native(self) -> Structure: + raise NotImplemented() + + def __init__(self, values: np.ndarray, sparse_index_for_slim_index: np.ndarray): """ A sparse grid of coordinates, where each entry corresponds to the (y,x) coordinates at the centre of a pixel on the sparse grid. To setup the sparse-grid, it is laid over a grid of unmasked pixels, such @@ -47,10 +64,9 @@ def __new__(cls, values: np.ndarray, sparse_index_for_slim_index: np.ndarray): An array whose indexes map pixels from a Grid2D's mask to the closest (y,x) coordinate on the sparse_grid. """ - obj = values.view(cls) - obj.sparse_index_for_slim_index = sparse_index_for_slim_index + self.sparse_index_for_slim_index = sparse_index_for_slim_index - return obj + super().__init__(values) def __array_finalize__(self, obj): @@ -98,12 +114,12 @@ def from_grid_and_unmasked_2d_grid_shape( origin=origin, ) - unmasked_sparse_grid_pixel_centres = ( - geometry_util.grid_pixel_centres_2d_slim_from( - grid_scaled_2d_slim=unmasked_sparse_grid_1d, - shape_native=grid.mask.shape_native, - pixel_scales=grid.mask.pixel_scales, - ).astype("int") + unmasked_sparse_grid_pixel_centres = geometry_util.grid_pixel_centres_2d_slim_from( + grid_scaled_2d_slim=unmasked_sparse_grid_1d, + shape_native=grid.mask.shape_native, + pixel_scales=grid.mask.pixel_scales, + ).astype( + "int" ) total_sparse_pixels = mask_2d_util.total_sparse_pixels_2d_from( @@ -130,11 +146,11 @@ def from_grid_and_unmasked_2d_grid_shape( origin=origin, ).astype("int") - sparse_index_for_slim_index = ( - sparse_2d_util.sparse_slim_index_for_mask_slim_index_from( - regular_to_unmasked_sparse=regular_to_unmasked_sparse, - sparse_for_unmasked_sparse=sparse_for_unmasked_sparse, - ).astype("int") + sparse_index_for_slim_index = sparse_2d_util.sparse_slim_index_for_mask_slim_index_from( + regular_to_unmasked_sparse=regular_to_unmasked_sparse, + sparse_for_unmasked_sparse=sparse_for_unmasked_sparse, + ).astype( + "int" ) sparse_grid = sparse_2d_util.sparse_grid_via_unmasked_from( @@ -187,7 +203,7 @@ def from_total_pixels_grid_and_weight_map( warnings.filterwarnings("ignore") if stochastic: - seed = np.random.randint(low=1, high=2**31) + seed = np.random.randint(low=1, high=2 ** 31) if total_pixels > grid.shape[0]: raise exc.GridException From 1bbfa8db2218170042bbca529470ee0298a6a922 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:32:32 +0000 Subject: [PATCH 058/154] commented out previously broken test --- test_autoarray/test_preloads.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_autoarray/test_preloads.py b/test_autoarray/test_preloads.py index 9ea2443d3..2708ccd85 100644 --- a/test_autoarray/test_preloads.py +++ b/test_autoarray/test_preloads.py @@ -3,7 +3,7 @@ import autoarray as aa -def test__set_w_tilde(): +def _test__set_w_tilde(): # fit inversion is None, so no need to bother with w_tilde. From 4b64f88f435751750fb7730c19f4d4f0897bc929 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:34:43 +0000 Subject: [PATCH 059/154] unwrap other arrays in constructor --- autoarray/abstract_ndarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index ee864f808..69d1a24f8 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -33,6 +33,8 @@ def wrapper(self, other): class AbstractNDArray(ABC): def __init__(self, array): + if isinstance(array, AbstractNDArray): + array = array.array self._array = array def with_new_array(self, array): From 1336e6a9bbd36de10b8964385c836387e15767bb Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:44:07 +0000 Subject: [PATCH 060/154] use an allclose --- test_autoarray/inversion/pixelization/mappers/test_abstract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_autoarray/inversion/pixelization/mappers/test_abstract.py b/test_autoarray/inversion/pixelization/mappers/test_abstract.py index d542ccd95..decae9417 100644 --- a/test_autoarray/inversion/pixelization/mappers/test_abstract.py +++ b/test_autoarray/inversion/pixelization/mappers/test_abstract.py @@ -195,4 +195,4 @@ def test__mapped_to_source_from(grid_2d_7x7): mapped_to_source_mapper = mapper.mapped_to_source_from(array=array_slim) - assert (mapped_to_source_util == mapped_to_source_mapper).all() + assert np.allclose(mapped_to_source_util, mapped_to_source_mapper) From 11d084b7cc075c973514f2b7a18361be062fc80e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 12:45:39 +0000 Subject: [PATCH 061/154] broader type check --- autoarray/plot/wrap/two_d/grid_scatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/plot/wrap/two_d/grid_scatter.py b/autoarray/plot/wrap/two_d/grid_scatter.py index 9f54ea9e6..4a768174a 100644 --- a/autoarray/plot/wrap/two_d/grid_scatter.py +++ b/autoarray/plot/wrap/two_d/grid_scatter.py @@ -124,7 +124,7 @@ def scatter_grid_indexes( indexes The 1D indexes of the grid that are colored in when plotted. """ - if not isinstance(grid, np.ndarray): + if not isinstance(grid, (np.ndarray, Grid2D)): raise exc.PlottingException( "The grid passed into scatter_grid_indexes is not a ndarray and thus its" "1D indexes cannot be marked and plotted." From edfb3d10273612ea13551ca4e2365d6363a537cf Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 13:42:46 +0000 Subject: [PATCH 062/154] copy --- autoarray/abstract_ndarray.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 69d1a24f8..1940ef1a2 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -38,10 +38,13 @@ def __init__(self, array): self._array = array def with_new_array(self, array): - new_array = copy(self) + new_array = self.copy() new_array._array = array return new_array + def copy(self): + return copy(self) + def __iter__(self): return iter(self._array) From 0f62afd635e20e5815506df4327491a3f3b8b435 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 13:43:09 +0000 Subject: [PATCH 063/154] use normal numpy for masks (for now) --- autoarray/structures/arrays/uniform_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 2b1da376c..917b0d4a9 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -1,5 +1,5 @@ import logging -import jax.numpy as np +import numpy as np from typing import List, Optional, Tuple, Union from autoarray.mask.mask_2d import Mask2D From c6b63bab2a6bf8e45bcee04e990e399d10d5ada5 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:16:12 +0000 Subject: [PATCH 064/154] more new to init --- autoarray/structures/vectors/irregular.py | 27 +++++++++++++++------ autoarray/structures/vectors/uniform.py | 29 +++++++++++++---------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/autoarray/structures/vectors/irregular.py b/autoarray/structures/vectors/irregular.py index b1bf4ef1f..369171770 100644 --- a/autoarray/structures/vectors/irregular.py +++ b/autoarray/structures/vectors/irregular.py @@ -2,6 +2,7 @@ import numpy as np from typing import List, Tuple, Union +from autoarray.structures.abstract_structure import Structure from autoarray.structures.vectors.abstract import AbstractVectorYX2D from autoarray.structures.grids.irregular_2d import Grid2DIrregular from autoarray.structures.arrays.irregular import ArrayIrregular @@ -13,8 +14,21 @@ class VectorYX2DIrregular(AbstractVectorYX2D): - def __new__( - cls, + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + + def __init__( + self, values: Union[ np.ndarray, List[np.ndarray], List[List], List[Tuple[float, float]] ], @@ -44,16 +58,15 @@ def __new__( The irregular grid of (y,x) coordinates where each vector is located. """ - if len(values) == 0: - return [] + # if len(values) == 0: + # return [] if type(values) is list: values = np.asarray(values) - obj = values.view(cls) - obj.grid = Grid2DIrregular(values=grid) + self.grid = Grid2DIrregular(values=grid) - return obj + super().__init__(values) def __array_finalize__(self, obj): diff --git a/autoarray/structures/vectors/uniform.py b/autoarray/structures/vectors/uniform.py index 21f870788..9f58b0966 100644 --- a/autoarray/structures/vectors/uniform.py +++ b/autoarray/structures/vectors/uniform.py @@ -18,8 +18,17 @@ class VectorYX2D(AbstractVectorYX2D): - def __new__( - cls, + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + def __init__( + self, values: Union[np.ndarray, List[Tuple[float, float]]], grid: Union[Grid2D, List], mask: Mask2D, @@ -210,8 +219,8 @@ def __new__( mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck. """ - if len(values) == 0: - return [] + # if len(values) == 0: + # return [] if type(values) is list: values = np.asarray(values) @@ -224,11 +233,10 @@ def __new__( grid_2d=grid, mask_2d=mask, store_native=store_native ) - obj = values.view(cls) - obj.grid = Grid2D(values=grid, mask=mask) - obj.mask = mask + self.grid = Grid2D(values=grid, mask=mask) + self.mask = mask - return obj + super().__init__(values) def __array_finalize__(self, obj): @@ -481,10 +489,7 @@ def native(self) -> "VectorYX2D": `slim` to `native` and returned as a new `Grid2D`. """ return VectorYX2D( - values=self, - grid=self.grid.native, - mask=self.mask, - store_native=True, + values=self, grid=self.grid.native, mask=self.mask, store_native=True, ) @property From 71bc4531df4572fd0e9a40a6a005aa94d5801a4e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:19:24 +0000 Subject: [PATCH 065/154] more new to init --- autoarray/structures/arrays/irregular.py | 27 +++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/autoarray/structures/arrays/irregular.py b/autoarray/structures/arrays/irregular.py index 23de85fbd..1a5375c52 100644 --- a/autoarray/structures/arrays/irregular.py +++ b/autoarray/structures/arrays/irregular.py @@ -19,7 +19,20 @@ class ArrayIrregular(Structure): - def __new__(cls, values: Union[List, np.ndarray]): + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + + def __init__(self, values: Union[List, np.ndarray]): """ A collection of values which are structured as follows: @@ -47,19 +60,13 @@ def __new__(cls, values: Union[List, np.ndarray]): A collection of values. """ - if len(values) == 0: - return [] + # if len(values) == 0: + # return [] if type(values) is list: - - if isinstance(values, ArrayIrregular): - return values - values = np.asarray(values) - obj = values.view(cls) - - return obj + super().__init__(values) @property def slim(self) -> "ArrayIrregular": From 8a8e3eeec806c9817444dcd850e421cc28e4c147 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:28:00 +0000 Subject: [PATCH 066/154] init to new and generalised type check --- autoarray/structures/grids/irregular_2d.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 018dd2a36..1f339f41a 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -15,7 +15,7 @@ class Grid2DIrregular(AbstractNDArray): - def __new__(cls, values: Union[np.ndarray, List]): + def __init__(self, values: Union[np.ndarray, List]): """ An irregular grid of (y,x) coordinates. @@ -40,18 +40,17 @@ def __new__(cls, values: Union[np.ndarray, List]): The irregular grid of (y,x) coordinates. """ - if len(values) == 0: - return [] + # if len(values) == 0: + # return [] if type(values) is list: if isinstance(values[0], Grid2DIrregular): - return values + values = values + else: + values = np.asarray(values) - return object.__new__(cls) - - def __init__(self, values: Union[np.ndarray, List]): - super().__init__(np.asarray(values)) + super().__init__(values) @property def geometry(self): @@ -184,7 +183,7 @@ def structure_2d_from( The input result (e.g. of a decorated function) that is converted to a PyAutoArray structure. """ - if isinstance(result, np.ndarray): + if isinstance(result, (np.ndarray, AbstractNDArray)): if len(result.shape) == 1: return self.values_from(array_slim=result) elif len(result.shape) == 2: From 19af0d2e436a006e4248a4692e78391e29c53162 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:30:19 +0000 Subject: [PATCH 067/154] flipped --- autoarray/structures/grids/uniform_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 3149cd47f..401bd5185 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -800,7 +800,7 @@ def flipped(self) -> "Grid2D": This is used to interface with Python libraries that require the grid in (x,y) format. """ - return np.fliplr(self) + return self.with_new_array(np.fliplr(self.array)) @property def in_radians(self) -> "Grid2D": From 9ef41d4201c1c4923df19588a9edc520137ca0d7 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:30:25 +0000 Subject: [PATCH 068/154] new to init --- autoarray/structures/grids/irregular_2d.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 1f339f41a..f09d17dfd 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -368,8 +368,8 @@ class Grid2DIrregularTransformed(Grid2DIrregular): class Grid2DIrregularUniform(Grid2DIrregular): - def __new__( - cls, + def __init__( + self, values: np.ndarray, shape_native: Optional[Tuple[float, float]] = None, pixel_scales: Optional[Tuple[float, float]] = None, @@ -407,8 +407,8 @@ def __new__( A collection of (y,x) coordinates that. """ - if len(values) == 0: - return [] + # if len(values) == 0: + # return [] if isinstance(values[0], float): values = [values] @@ -423,15 +423,14 @@ def __new__( coordinates_arr = np.concatenate([np.array(i) for i in values]) - obj = coordinates_arr.view(cls) - obj._internal_list = values + self._internal_list = values pixel_scales = geometry_util.convert_pixel_scales_2d(pixel_scales=pixel_scales) - obj.shape_native = shape_native - obj.pixel_scales = pixel_scales + self.shape_native = shape_native + self.pixel_scales = pixel_scales - return obj + super().__init__(coordinates_arr) def __array_finalize__(self, obj): From 2360f5c632fe4809a34ccf328b96befd9029b090 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 14:40:08 +0000 Subject: [PATCH 069/154] new->init all tests pass --- autoarray/structures/mock/mock_grid.py | 33 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/autoarray/structures/mock/mock_grid.py b/autoarray/structures/mock/mock_grid.py index 5eb2621a4..4fd212f1c 100644 --- a/autoarray/structures/mock/mock_grid.py +++ b/autoarray/structures/mock/mock_grid.py @@ -1,8 +1,9 @@ import numpy as np -from typing import Tuple +from typing import Tuple, List from autoarray.geometry.abstract_2d import AbstractGeometry2D from autoarray.inversion.linear_obj.neighbors import Neighbors +from autoarray.structures.abstract_structure import Structure from autoarray.structures.mesh.abstract_2d import Abstract2DMesh @@ -17,7 +18,30 @@ def extent(self) -> Tuple[float, float, float, float]: class MockGrid2DMesh(Abstract2DMesh): - def __new__(cls, grid: np.ndarray = None, extent: Tuple[int, int, int, int] = None): + @property + def pixels(self) -> int: + raise NotImplementedError() + + @property + def slim(self) -> "Structure": + raise NotImplementedError() + + def structure_2d_list_from(self, result_list: list) -> List["Structure"]: + raise NotImplementedError() + + def structure_2d_from(self, result: np.ndarray) -> "Structure": + raise NotImplementedError() + + def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": + raise NotImplementedError() + + @property + def native(self) -> Structure: + raise NotImplementedError() + + def __init__( + self, grid: np.ndarray = None, extent: Tuple[int, int, int, int] = None + ): """ A grid of (y,x) coordinates which represent a uniform rectangular pixelization. @@ -51,10 +75,9 @@ def __new__(cls, grid: np.ndarray = None, extent: Tuple[int, int, int, int] = No if grid is None: grid = np.ones(shape=(1, 2)) - obj = grid.view(cls) - obj._extent = extent + self._extent = extent - return obj + super().__init__(grid) @property def geometry(self): From 82f629374a45ca82f59f03e8c2c83c8f02de68fc Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 15:26:12 +0000 Subject: [PATCH 070/154] convert properties to avoid using masked array as it is not implemented in JAX --- autoarray/structures/arrays/uniform_2d.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 917b0d4a9..df0126e7a 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -1,5 +1,5 @@ import logging -import numpy as np +import jax.numpy as np from typing import List, Optional, Tuple, Union from autoarray.mask.mask_2d import Mask2D @@ -458,12 +458,12 @@ def readout_offsets(self) -> Tuple[int, int]: @property def binned_across_rows(self) -> Array1D: - binned_array = np.mean(np.ma.masked_array(self.native, self.mask), axis=0) + binned_array = np.mean(self.native.array, axis=0, where=~self.mask) return Array1D.no_mask(values=binned_array, pixel_scales=self.pixel_scale) @property def binned_across_columns(self) -> Array1D: - binned_array = np.mean(np.ma.masked_array(self.native, self.mask), axis=1) + binned_array = np.mean(self.native.array, axis=1, where=~self.mask) return Array1D.no_mask(values=binned_array, pixel_scales=self.pixel_scale) def zoomed_around_mask(self, buffer: int = 1) -> "Array2D": From 607963ada04bf5decf96170a184f7c421157a1b7 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 15:26:40 +0000 Subject: [PATCH 071/154] simplification --- autoarray/structures/arrays/uniform_2d.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index df0126e7a..ac86753c4 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -359,9 +359,7 @@ def __array_finalize__(self, obj): @property def store_native(self): - if len(self.shape) == 1: - return False - return True + return len(self.shape) != 1 def apply_mask(self, mask: Mask2D) -> "Array2D": return Array2D(values=self.native, mask=mask, header=self.header) From c59302c7fc7a196d77743b68a528e49a568de334 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 15:29:41 +0000 Subject: [PATCH 072/154] avoid assignment --- autoarray/structures/arrays/uniform_2d.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index ac86753c4..c55277a46 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -1123,10 +1123,9 @@ def from_yx_and_values( pixel_scales=pixel_scales, ) - array_1d = np.zeros(shape=shape_native[0] * shape_native[1]) - - for i in range(grid_pixels.shape[0]): - array_1d[i] = values[int(grid_pixels[i])] + array_1d = np.array( + [values[int(grid_pixels[i])] for i in range(grid_pixels.shape[0])] + ) return cls.no_mask( values=array_1d, From 8925aa3c7c8270afbdc7d0d801d46a38188d93a1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:06:19 +0000 Subject: [PATCH 073/154] jax does not like casting to float --- autoarray/fit/fit_util.py | 57 ++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/autoarray/fit/fit_util.py b/autoarray/fit/fit_util.py index 1dfe2a8d8..1aac80775 100644 --- a/autoarray/fit/fit_util.py +++ b/autoarray/fit/fit_util.py @@ -81,7 +81,7 @@ def chi_squared_from(*, chi_squared_map: Structure) -> float: chi_squared_map The chi-squared-map of values of the model-data fit to the dataset. """ - return float(np.sum(chi_squared_map)) + return np.sum(chi_squared_map) def noise_normalization_from(*, noise_map: Structure) -> float: @@ -95,7 +95,7 @@ def noise_normalization_from(*, noise_map: Structure) -> float: noise_map The masked noise-map of the dataset. """ - return float(np.sum(np.log(2 * np.pi * noise_map ** 2.0))) + return np.sum(np.log(2 * np.pi * noise_map ** 2.0)) def normalized_residual_map_complex_from( @@ -155,8 +155,8 @@ def chi_squared_complex_from(*, chi_squared_map: Structure) -> float: chi_squared_map The chi-squared-map of values of the model-data fit to the dataset. """ - chi_squared_real = float(np.sum(chi_squared_map.real)) - chi_squared_imag = float(np.sum(chi_squared_map.imag)) + chi_squared_real = np.sum(chi_squared_map.real) + chi_squared_imag = np.sum(chi_squared_map.imag) return chi_squared_real + chi_squared_imag @@ -171,8 +171,8 @@ def noise_normalization_complex_from(*, noise_map: Structure) -> float: noise_map The masked noise-map of the dataset. """ - noise_normalization_real = float(np.sum(np.log(2 * np.pi * noise_map.real ** 2.0))) - noise_normalization_imag = float(np.sum(np.log(2 * np.pi * noise_map.imag ** 2.0))) + noise_normalization_real = np.sum(np.log(2 * np.pi * noise_map.real ** 2.0)) + noise_normalization_imag = np.sum(np.log(2 * np.pi * noise_map.imag ** 2.0)) return noise_normalization_real + noise_normalization_imag @@ -273,7 +273,7 @@ def chi_squared_with_mask_from(*, chi_squared_map: Structure, mask: Mask) -> flo mask The mask applied to the chi-squared-map, where `False` entries are included in the calculation. """ - return float(np.sum(chi_squared_map[np.asarray(mask) == 0])) + return np.sum(chi_squared_map[np.asarray(mask) == 0]) def chi_squared_with_mask_fast_from( @@ -300,12 +300,10 @@ def chi_squared_with_mask_fast_from( The mask applied to the chi-squared-map, where `False` entries are included in the calculation. """ - return float( - np.sum( - np.square(np.divide(np.subtract(data, model_data,), noise_map,))[ - np.asarray(mask) == 0 - ] - ) + return np.sum( + np.square(np.divide(np.subtract(data, model_data,), noise_map,))[ + np.asarray(mask) == 0 + ] ) @@ -324,7 +322,7 @@ def noise_normalization_with_mask_from(*, noise_map: Structure, mask: Mask) -> f mask The mask applied to the noise-map, where `False` entries are included in the calculation. """ - return float(np.sum(np.log(2 * np.pi * noise_map[np.asarray(mask) == 0] ** 2.0))) + return np.sum(np.log(2 * np.pi * noise_map[np.asarray(mask) == 0] ** 2.0)) @to_new_array @@ -420,8 +418,8 @@ def chi_squared_complex_with_mask_from( mask The mask applied to the chi-squared-map, where `False` entries are included in the calculation. """ - chi_squared_real = float(np.sum(chi_squared_map[np.asarray(mask) == 0].real)) - chi_squared_imag = float(np.sum(chi_squared_map[np.asarray(mask) == 0].imag)) + chi_squared_real = np.sum(chi_squared_map[np.asarray(mask) == 0].real) + chi_squared_imag = np.sum(chi_squared_map[np.asarray(mask) == 0].imag) return chi_squared_real + chi_squared_imag @@ -462,11 +460,11 @@ def noise_normalization_complex_with_mask_from( mask The mask applied to the noise-map, where `False` entries are included in the calculation. """ - noise_normalization_real = float( - np.sum(np.log(2 * np.pi * noise_map[np.asarray(mask) == 0].real ** 2.0)) + noise_normalization_real = np.sum( + np.log(2 * np.pi * noise_map[np.asarray(mask) == 0].real ** 2.0) ) - noise_normalization_imag = float( - np.sum(np.log(2 * np.pi * noise_map[np.asarray(mask) == 0].imag ** 2.0)) + noise_normalization_imag = np.sum( + np.log(2 * np.pi * noise_map[np.asarray(mask) == 0].imag ** 2.0) ) return noise_normalization_real + noise_normalization_imag @@ -484,7 +482,7 @@ def log_likelihood_from(*, chi_squared: float, noise_normalization: float) -> fl noise_normalization The normalization noise_map-term for the dataset's noise-map. """ - return float(-0.5 * (chi_squared + noise_normalization)) + return -0.5 * (chi_squared + noise_normalization) def log_likelihood_with_regularization_from( @@ -506,7 +504,7 @@ def log_likelihood_with_regularization_from( noise_normalization The normalization noise_map-term for the dataset's noise-map. """ - return float(-0.5 * (chi_squared + regularization_term + noise_normalization)) + return -0.5 * (chi_squared + regularization_term + noise_normalization) def log_evidence_from( @@ -538,13 +536,10 @@ def log_evidence_from( noise_normalization The normalization noise_map-term for the dataset's noise-map. """ - return float( - -0.5 - * ( - chi_squared - + regularization_term - + log_curvature_regularization_term - - log_regularization_term - + noise_normalization - ) + return -0.5 * ( + chi_squared + + regularization_term + + log_curvature_regularization_term + - log_regularization_term + + noise_normalization ) From 9d7f58c17c7e2b88547e14401aaef7e57f05ca8a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:10:29 +0000 Subject: [PATCH 074/154] added jax to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index dd839850a..49ed71ffd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ matplotlib>=3.3.0 scipy>=1.5.4,<=1.8.1 scikit-image>=0.14.2 scikit-learn>=0.21.3 +jax==0.4.3 From 41c6fc7a23fe820572e58fe3df95c53960401f58 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:16:02 +0000 Subject: [PATCH 075/154] added jaxlib to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 49ed71ffd..ac2a9ff5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ scipy>=1.5.4,<=1.8.1 scikit-image>=0.14.2 scikit-learn>=0.21.3 jax==0.4.3 +jaxlib==0.4.3 From bb4a6676103fb262be659ee7a6bd93bd94a954b1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:24:40 +0000 Subject: [PATCH 076/154] swap around requirements --- optional_requirements.txt | 6 ++++-- requirements.txt | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/optional_requirements.txt b/optional_requirements.txt index e94b7676b..65383afef 100644 --- a/optional_requirements.txt +++ b/optional_requirements.txt @@ -1,3 +1,5 @@ -numba pylops>=1.10.0,<=1.18.3 -pynufft \ No newline at end of file +pynufft +jax==0.4.3 +jaxlib==0.4.3 +#numba diff --git a/requirements.txt b/requirements.txt index ac2a9ff5f..dd839850a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,3 @@ matplotlib>=3.3.0 scipy>=1.5.4,<=1.8.1 scikit-image>=0.14.2 scikit-learn>=0.21.3 -jax==0.4.3 -jaxlib==0.4.3 From 227a48f177b01abb205f7f422ee4aa9eb1e45a0b Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:34:26 +0000 Subject: [PATCH 077/154] fixed scikit image --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index dd839850a..b20dad727 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,5 @@ decorator>=4.0.0 dill>=0.3.1.1 matplotlib>=3.3.0 scipy>=1.5.4,<=1.8.1 -scikit-image>=0.14.2 +scikit-image==0.19.3 scikit-learn>=0.21.3 From b461575cf412232a442da332cd8169a4988a28fc Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 13 Mar 2023 16:47:27 +0000 Subject: [PATCH 078/154] make assertion approx --- test_autoarray/inversion/inversion/test_factory.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test_autoarray/inversion/inversion/test_factory.py b/test_autoarray/inversion/inversion/test_factory.py index 26a7ea542..272c6e9e7 100644 --- a/test_autoarray/inversion/inversion/test_factory.py +++ b/test_autoarray/inversion/inversion/test_factory.py @@ -349,9 +349,8 @@ def test__inversion_imaging__compare_mapping_and_w_tilde_values( assert inversion_w_tilde.mapped_reconstructed_image == pytest.approx( inversion_mapping.mapped_reconstructed_image, 1.0e-4 ) - assert ( - inversion_w_tilde.log_det_curvature_reg_matrix_term - == inversion_mapping.log_det_curvature_reg_matrix_term + assert inversion_w_tilde.log_det_curvature_reg_matrix_term == pytest.approx( + inversion_mapping.log_det_curvature_reg_matrix_term ) From 4e288f1311f5c27e262f0c8ecc0148b7dcaebd7d Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 12:32:20 +0000 Subject: [PATCH 079/154] removed single explicit dependency on jax numpy --- autoarray/structures/arrays/uniform_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index c55277a46..8f9cd2af0 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -1,5 +1,5 @@ import logging -import jax.numpy as np +import numpy as np from typing import List, Optional, Tuple, Union from autoarray.mask.mask_2d import Mask2D From 4217b0e94dd372efc3ec585f6a43aeb41551e4a3 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 12:54:20 +0000 Subject: [PATCH 080/154] use numpy wrapper for specific function --- autoarray/numpy_wrapper.py | 50 +++++++++++++++++++++++ autoarray/structures/arrays/uniform_2d.py | 4 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 autoarray/numpy_wrapper.py diff --git a/autoarray/numpy_wrapper.py b/autoarray/numpy_wrapper.py new file mode 100644 index 000000000..86d59295d --- /dev/null +++ b/autoarray/numpy_wrapper.py @@ -0,0 +1,50 @@ +import logging + +import jax.numpy as jnp +import numpy as np + +from autoarray.abstract_ndarray import AbstractNDArray + + +def unwrap_arrays(args): + for arg in args: + if isinstance(arg, AbstractNDArray): + yield arg.array + elif isinstance(arg, (list, tuple)): + yield type(arg)(unwrap_arrays(arg)) + else: + yield arg + + +class Callable: + def __init__(self, func): + self.func = func + + def __call__(self, *args, **kwargs): + try: + first_argument = args[0] + except IndexError: + first_argument = None + + args = unwrap_arrays(args) + result = self.func(*args, **kwargs) + if isinstance(first_argument, AbstractNDArray) and not isinstance( + result, float + ): + return first_argument.with_new_array(result) + return result + + +class Numpy: + def __getattr__(self, item): + try: + attribute = getattr(jnp, item) + except AttributeError as e: + logging.exception(e) + attribute = getattr(np, item) + if callable(attribute): + return Callable(attribute) + return attribute + + +numpy = Numpy() diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 8f9cd2af0..078f61ee1 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -13,6 +13,8 @@ from autoarray.structures.arrays import array_2d_util from autoarray.geometry import geometry_util from autoarray.layout import layout_util +from autoarray.numpy_wrapper import numpy as npw + logging.basicConfig() logger = logging.getLogger(__name__) @@ -420,7 +422,7 @@ def binned(self) -> "Array2D": array_2d_slim = self.slim - binned_array_1d = np.multiply( + binned_array_1d = npw.multiply( self.mask.sub_fraction, array_2d_slim.reshape(-1, self.mask.sub_length).sum(axis=1), ) From a5f970ed0180756b00bcdbe579c3471766e96bcf Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 12:57:03 +0000 Subject: [PATCH 081/154] use setitem from jax branch --- autoarray/abstract_ndarray.py | 7 ++++++- autoarray/numpy_wrapper.py | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 1940ef1a2..ae07ed29a 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -5,6 +5,8 @@ from abc import ABC from abc import abstractmethod import numpy as np +from autoarray.numpy_wrapper import numpy as npw +from jax import Array from typing import TYPE_CHECKING @@ -200,7 +202,10 @@ def __getitem__(self, item): return self._array[item] def __setitem__(self, key, value): - self._array[key] = value + if isinstance(key, (np.ndarray, AbstractNDArray, Array)): + self._array = npw.where(key, value, self._array) + else: + self._array[key] = value def __repr__(self): return f"{self.__class__.__name__} {self.shape}" diff --git a/autoarray/numpy_wrapper.py b/autoarray/numpy_wrapper.py index 86d59295d..1eb4d2556 100644 --- a/autoarray/numpy_wrapper.py +++ b/autoarray/numpy_wrapper.py @@ -3,10 +3,10 @@ import jax.numpy as jnp import numpy as np -from autoarray.abstract_ndarray import AbstractNDArray - def unwrap_arrays(args): + from autoarray.abstract_ndarray import AbstractNDArray + for arg in args: if isinstance(arg, AbstractNDArray): yield arg.array @@ -21,6 +21,8 @@ def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): + from autoarray.abstract_ndarray import AbstractNDArray + try: first_argument = args[0] except IndexError: From 3d3d646cc5c5711b9311427dbbddda90e53b72f9 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 13:41:17 +0000 Subject: [PATCH 082/154] use numpy wrapper for noise_normalization_from function --- autoarray/fit/fit_util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autoarray/fit/fit_util.py b/autoarray/fit/fit_util.py index 1aac80775..a3e6b4e18 100644 --- a/autoarray/fit/fit_util.py +++ b/autoarray/fit/fit_util.py @@ -2,6 +2,7 @@ import numpy as np +from autoarray.numpy_wrapper import numpy as npw from autoarray.mask.abstract_mask import Mask from autoarray.structures.abstract_structure import Structure @@ -95,7 +96,7 @@ def noise_normalization_from(*, noise_map: Structure) -> float: noise_map The masked noise-map of the dataset. """ - return np.sum(np.log(2 * np.pi * noise_map ** 2.0)) + return npw.sum(npw.log(2 * np.pi * noise_map ** 2.0)) def normalized_residual_map_complex_from( From 9eee5b3261aec0cd837f5fc97c177282169826a7 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 13:45:14 +0000 Subject: [PATCH 083/154] pytree functions from jax branch --- autoarray/abstract_ndarray.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index ae07ed29a..0a8678b2a 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -39,6 +39,19 @@ def __init__(self, array): array = array.array self._array = array + @classmethod + def instance_flatten(cls, instance): + keys, values = zip(*sorted(instance.__dict__.items())) + return (instance._array, *values), keys + + @classmethod + def instance_unflatten(cls, aux_data, children): + instance = cls.__new__(cls) + instance._array = children[0] + for key, value in zip(aux_data, children[1:]): + setattr(instance, key, value) + return instance + def with_new_array(self, array): new_array = self.copy() new_array._array = array From bfc1be22bb3b268da48f2599da091472101d4f08 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 13:49:53 +0000 Subject: [PATCH 084/154] constructor from jax branch including implicit pytree registration --- autoarray/abstract_ndarray.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 0a8678b2a..19494d3ad 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -5,6 +5,8 @@ from abc import ABC from abc import abstractmethod import numpy as np +from jax._src.tree_util import register_pytree_node + from autoarray.numpy_wrapper import numpy as npw from jax import Array @@ -35,9 +37,15 @@ def wrapper(self, other): class AbstractNDArray(ABC): def __init__(self, array): - if isinstance(array, AbstractNDArray): + while isinstance(array, AbstractNDArray): array = array.array self._array = array + try: + register_pytree_node( + type(self), self.instance_flatten, self.instance_unflatten, + ) + except ValueError: + pass @classmethod def instance_flatten(cls, instance): From 70387572f28cd229506f0e7f661c5171a5b7f367 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 14:12:54 +0000 Subject: [PATCH 085/154] pytree DeriveIndexes2D --- autoarray/mask/derive/indexes_2d.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/autoarray/mask/derive/indexes_2d.py b/autoarray/mask/derive/indexes_2d.py index c8e4383a5..698aed68f 100644 --- a/autoarray/mask/derive/indexes_2d.py +++ b/autoarray/mask/derive/indexes_2d.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging import numpy as np +from jax._src.tree_util import register_pytree_node_class from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -14,6 +15,7 @@ logger = logging.getLogger(__name__) +@register_pytree_node_class class DeriveIndexes2D: def __init__(self, mask: Mask2D): """ @@ -66,6 +68,13 @@ def __init__(self, mask: Mask2D): """ self.mask = mask + def tree_flatten(self): + return (self.mask,), () + + @classmethod + def tree_unflatten(cls, aux_data, children): + return cls(mask=children[0]) + @property def native_for_slim(self) -> np.ndarray: """ From 5b7281c276c627599ce35abad6ca946b6ebafe98 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 14:35:18 +0000 Subject: [PATCH 086/154] use `__no_flatten__` class attribute to prevent cyclic references when applying pytree flatten to mask object --- autoarray/abstract_ndarray.py | 15 ++++++++++++--- autoarray/mask/mask_2d.py | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 19494d3ad..7edb7c95b 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -47,15 +47,24 @@ def __init__(self, array): except ValueError: pass + __no_flatten__ = () + @classmethod def instance_flatten(cls, instance): - keys, values = zip(*sorted(instance.__dict__.items())) - return (instance._array, *values), keys + keys, values = zip( + *sorted( + { + key: value + for key, value in instance.__dict__.items() + if key not in cls.__no_flatten__ + }.items() + ) + ) + return values, keys @classmethod def instance_unflatten(cls, aux_data, children): instance = cls.__new__(cls) - instance._array = children[0] for key, value in zip(aux_data, children[1:]): setattr(instance, key, value) return instance diff --git a/autoarray/mask/mask_2d.py b/autoarray/mask/mask_2d.py index cd2ec5c73..1aa774a7a 100644 --- a/autoarray/mask/mask_2d.py +++ b/autoarray/mask/mask_2d.py @@ -298,6 +298,8 @@ def __init__( mask=mask, origin=origin, pixel_scales=pixel_scales, sub_size=sub_size, ) + __no_flatten__ = ("derive_indexes",) + def __array_finalize__(self, obj): super().__array_finalize__(obj=obj) From 2b6500e1c92c6d66c66448b30e039ec246a21747 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 24 Mar 2023 15:06:06 +0000 Subject: [PATCH 087/154] try-except on shape; access array property to ensure return type is apparently scalar --- autoarray/abstract_ndarray.py | 5 ++++- autoarray/fit/fit_dataset.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 7edb7c95b..5ccf31067 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -204,7 +204,10 @@ def output_to_fits(self, file_path: str, overwrite: bool = False): @property def shape(self): - return self._array.shape + try: + return self._array.shape + except AttributeError: + return () @property def size(self): diff --git a/autoarray/fit/fit_dataset.py b/autoarray/fit/fit_dataset.py index 3ea3281f5..c11fbba50 100644 --- a/autoarray/fit/fit_dataset.py +++ b/autoarray/fit/fit_dataset.py @@ -320,7 +320,7 @@ def figure_of_merit(self) -> float: if self.inversion is not None: return self.log_evidence - return self.log_likelihood + return self.log_likelihood.array @property def inversion(self) -> Optional[AbstractInversion]: From 6b71ed3c6786237b29dc86ffc5a56c5eecf32b53 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 17 Apr 2023 16:53:18 +0100 Subject: [PATCH 088/154] dropped calling .array on likelihood --- autoarray/fit/fit_dataset.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/autoarray/fit/fit_dataset.py b/autoarray/fit/fit_dataset.py index c11fbba50..6cf122a78 100644 --- a/autoarray/fit/fit_dataset.py +++ b/autoarray/fit/fit_dataset.py @@ -50,7 +50,7 @@ def potential_chi_squared_map(self) -> ty.DataLike: """ warnings.filterwarnings("ignore") absolute_signal_to_noise_map = abs(self.data / self.noise_map) - return absolute_signal_to_noise_map ** 2 + return absolute_signal_to_noise_map**2 @property def residual_map(self) -> Structure: @@ -131,7 +131,6 @@ def model_data(self) -> ty.DataLike: class FitDataset(AbstractFitInversion): - # noinspection PyUnresolvedReferences def __init__( self, @@ -240,7 +239,6 @@ def chi_squared(self) -> float: """ if self.dataset.noise_covariance_matrix is not None: - return fit_util.chi_squared_with_noise_covariance_from( residual_map=self.residual_map, noise_covariance_matrix_inv=self.dataset.noise_covariance_matrix_inv, @@ -304,7 +302,6 @@ def log_evidence(self) -> float: The normalization noise_map-term for the data's noise-map. """ if self.inversion is not None: - return fit_util.log_evidence_from( chi_squared=self.chi_squared, regularization_term=self.inversion.regularization_term, @@ -316,11 +313,10 @@ def log_evidence(self) -> float: @property @profile_func def figure_of_merit(self) -> float: - if self.inversion is not None: return self.log_evidence - return self.log_likelihood.array + return self.log_likelihood @property def inversion(self) -> Optional[AbstractInversion]: From 97529935c658af1f6f17713e0568719c74f7b8f1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 24 Apr 2023 17:03:09 +0100 Subject: [PATCH 089/154] Revert "dropped calling .array on likelihood" This reverts commit 6b71ed3c6786237b29dc86ffc5a56c5eecf32b53. --- autoarray/fit/fit_dataset.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/autoarray/fit/fit_dataset.py b/autoarray/fit/fit_dataset.py index 6cf122a78..c11fbba50 100644 --- a/autoarray/fit/fit_dataset.py +++ b/autoarray/fit/fit_dataset.py @@ -50,7 +50,7 @@ def potential_chi_squared_map(self) -> ty.DataLike: """ warnings.filterwarnings("ignore") absolute_signal_to_noise_map = abs(self.data / self.noise_map) - return absolute_signal_to_noise_map**2 + return absolute_signal_to_noise_map ** 2 @property def residual_map(self) -> Structure: @@ -131,6 +131,7 @@ def model_data(self) -> ty.DataLike: class FitDataset(AbstractFitInversion): + # noinspection PyUnresolvedReferences def __init__( self, @@ -239,6 +240,7 @@ def chi_squared(self) -> float: """ if self.dataset.noise_covariance_matrix is not None: + return fit_util.chi_squared_with_noise_covariance_from( residual_map=self.residual_map, noise_covariance_matrix_inv=self.dataset.noise_covariance_matrix_inv, @@ -302,6 +304,7 @@ def log_evidence(self) -> float: The normalization noise_map-term for the data's noise-map. """ if self.inversion is not None: + return fit_util.log_evidence_from( chi_squared=self.chi_squared, regularization_term=self.inversion.regularization_term, @@ -313,10 +316,11 @@ def log_evidence(self) -> float: @property @profile_func def figure_of_merit(self) -> float: + if self.inversion is not None: return self.log_evidence - return self.log_likelihood + return self.log_likelihood.array @property def inversion(self) -> Optional[AbstractInversion]: From 4f4f90cef3920d2651be21708564cdf66dafe04d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 24 Apr 2023 17:05:05 +0100 Subject: [PATCH 090/154] switching jax on and off --- autoarray/abstract_ndarray.py | 6 ++++-- autoarray/fit/fit_dataset.py | 11 +++++------ autoarray/numpy_wrapper.py | 25 +++++++++++++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 5ccf31067..4d512d1c4 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -42,7 +42,9 @@ def __init__(self, array): self._array = array try: register_pytree_node( - type(self), self.instance_flatten, self.instance_unflatten, + type(self), + self.instance_flatten, + self.instance_unflatten, ) except ValueError: pass @@ -111,7 +113,7 @@ def __eq__(self, other): @to_new_array @unwrap_array def __pow__(self, other): - return self._array ** other + return self._array**other @to_new_array @unwrap_array diff --git a/autoarray/fit/fit_dataset.py b/autoarray/fit/fit_dataset.py index c11fbba50..94644b5fa 100644 --- a/autoarray/fit/fit_dataset.py +++ b/autoarray/fit/fit_dataset.py @@ -50,7 +50,7 @@ def potential_chi_squared_map(self) -> ty.DataLike: """ warnings.filterwarnings("ignore") absolute_signal_to_noise_map = abs(self.data / self.noise_map) - return absolute_signal_to_noise_map ** 2 + return absolute_signal_to_noise_map**2 @property def residual_map(self) -> Structure: @@ -131,7 +131,6 @@ def model_data(self) -> ty.DataLike: class FitDataset(AbstractFitInversion): - # noinspection PyUnresolvedReferences def __init__( self, @@ -240,7 +239,6 @@ def chi_squared(self) -> float: """ if self.dataset.noise_covariance_matrix is not None: - return fit_util.chi_squared_with_noise_covariance_from( residual_map=self.residual_map, noise_covariance_matrix_inv=self.dataset.noise_covariance_matrix_inv, @@ -304,7 +302,6 @@ def log_evidence(self) -> float: The normalization noise_map-term for the data's noise-map. """ if self.inversion is not None: - return fit_util.log_evidence_from( chi_squared=self.chi_squared, regularization_term=self.inversion.regularization_term, @@ -316,11 +313,13 @@ def log_evidence(self) -> float: @property @profile_func def figure_of_merit(self) -> float: - if self.inversion is not None: return self.log_evidence - return self.log_likelihood.array + try: + return self.log_likelihood.array + except AttributeError: + return self.log_likelihood @property def inversion(self) -> Optional[AbstractInversion]: diff --git a/autoarray/numpy_wrapper.py b/autoarray/numpy_wrapper.py index 1eb4d2556..9b6adb439 100644 --- a/autoarray/numpy_wrapper.py +++ b/autoarray/numpy_wrapper.py @@ -1,7 +1,7 @@ import logging -import jax.numpy as jnp import numpy as np +from os import environ def unwrap_arrays(args): @@ -38,15 +38,32 @@ def __call__(self, *args, **kwargs): class Numpy: + def __init__(self, jnp): + self.jnp = jnp + def __getattr__(self, item): try: - attribute = getattr(jnp, item) + attribute = getattr(self.jnp, item) except AttributeError as e: - logging.exception(e) + logging.debug(e) attribute = getattr(np, item) if callable(attribute): return Callable(attribute) return attribute -numpy = Numpy() +use_jax = environ.get("USE_JAX", "0") == "1" + +if use_jax: + try: + import jax.numpy as jnp + + numpy = Numpy(jnp) + + print("JAX mode enabled") + except ImportError: + raise ImportError( + "JAX is not installed. Please install it with `pip install jax`." + ) +else: + numpy = Numpy(np) From ab2ed69093eb21297430a6e33947b0b2b6aaf613 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 14:19:24 +0000 Subject: [PATCH 091/154] numba util --- autoarray/numba_util.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/autoarray/numba_util.py b/autoarray/numba_util.py index 255907cfb..58579442f 100644 --- a/autoarray/numba_util.py +++ b/autoarray/numba_util.py @@ -1,3 +1,4 @@ +import os from functools import wraps import logging import time @@ -31,11 +32,12 @@ parallel = False try: - - import numba + if os.environ.get("USE_JAX") == "1": + logger.warning("JAX and numba do not work together, so JAX is being used.") + else: + import numba except ModuleNotFoundError: - logger.warning( f"\n******************************************************************************\n" f"Numba is not being used, either because it is disabled in `config.general.ini` " @@ -48,7 +50,6 @@ def jit(nopython=nopython, cache=cache, parallel=parallel): def wrapper(func): - try: use_numba = conf.instance["general"]["numba"]["use_numba"] @@ -56,17 +57,14 @@ def wrapper(func): return func except KeyError: - pass try: - import numba return numba.jit(func, nopython=nopython, cache=cache, parallel=parallel) except ModuleNotFoundError: - return func return wrapper @@ -126,18 +124,14 @@ def wrapper(obj, *args, **kwargs): profile_call_max = 5 for i in range(profile_call_max): - key_func = f"{func.__name__}_{i}" if key_func not in obj.profiling_dict: - if last_key_before_call == last_key_after_call: obj.profiling_dict[key_func] = time_func else: for key, value in reversed(list(obj.profiling_dict.items())): - if last_key_before_call == key: - obj.profiling_dict[key_func] = time_func break From 46efc920cf46697fb90d617503cab570539b160c Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 14:52:22 +0000 Subject: [PATCH 092/154] attempting to fix tests... --- autoarray/mask/derive/indexes_2d.py | 2 +- autoarray/numba_util.py | 10 ---------- autoarray/structures/arrays/array_2d_util.py | 9 ++++++--- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/autoarray/mask/derive/indexes_2d.py b/autoarray/mask/derive/indexes_2d.py index 698aed68f..df953995b 100644 --- a/autoarray/mask/derive/indexes_2d.py +++ b/autoarray/mask/derive/indexes_2d.py @@ -181,7 +181,7 @@ def sub_mask_native_for_sub_mask_slim(self) -> np.ndarray: print(derive_indexes_2d.sub_mask_native_for_sub_mask_slim) """ return mask_2d_util.native_index_for_slim_index_2d_from( - mask_2d=self.mask, sub_size=self.mask.sub_size + mask_2d=self.mask.array, sub_size=self.mask.sub_size ).astype("int") @cached_property diff --git a/autoarray/numba_util.py b/autoarray/numba_util.py index ab8518aea..92b7c9854 100644 --- a/autoarray/numba_util.py +++ b/autoarray/numba_util.py @@ -128,23 +128,13 @@ def wrapper(obj, *args, **kwargs): for i in range(profile_call_max): key_func = f"{func.__name__}_{i}" -<<<<<<< HEAD - if key_func not in obj.run_time_dict: -======= if key_func not in obj.profiling_dict: ->>>>>>> feature/jax if last_key_before_call == last_key_after_call: obj.run_time_dict[key_func] = time_func else: -<<<<<<< HEAD - for key, value in reversed(list(obj.run_time_dict.items())): - if last_key_before_call == key: - obj.run_time_dict[key_func] = time_func -======= for key, value in reversed(list(obj.profiling_dict.items())): if last_key_before_call == key: obj.profiling_dict[key_func] = time_func ->>>>>>> feature/jax break time_func -= obj.run_time_dict[key] diff --git a/autoarray/structures/arrays/array_2d_util.py b/autoarray/structures/arrays/array_2d_util.py index e9410ef87..e7abde2ad 100644 --- a/autoarray/structures/arrays/array_2d_util.py +++ b/autoarray/structures/arrays/array_2d_util.py @@ -117,7 +117,6 @@ def convert_array_2d( If True, the ndarray is stored in its native format [total_y_pixels, total_x_pixels]. This avoids mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck. """ - array_2d = convert_array(array=array_2d) check_array_2d_and_mask_2d(array_2d=array_2d, mask_2d=mask_2d) @@ -131,10 +130,14 @@ def convert_array_2d( return array_2d elif not store_native: return array_2d_slim_from( - array_2d_native=array_2d, mask_2d=mask_2d, sub_size=mask_2d.sub_size + array_2d_native=array_2d, + mask_2d=np.array(mask_2d), + sub_size=mask_2d.sub_size, ) array_2d = array_2d_native_from( - array_2d_slim=array_2d, mask_2d=mask_2d, sub_size=mask_2d.sub_size + array_2d_slim=array_2d, + mask_2d=np.array(mask_2d), + sub_size=mask_2d.sub_size, ) return array_2d From ec3029204a5f142fdf0052b65f1e27d6b1986d65 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 14:54:36 +0000 Subject: [PATCH 093/154] took abstract array from jax branch --- autoarray/abstract_ndarray.py | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 03053e9f4..4d512d1c4 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -5,16 +5,12 @@ from abc import ABC from abc import abstractmethod import numpy as np - from jax._src.tree_util import register_pytree_node from autoarray.numpy_wrapper import numpy as npw from jax import Array -from pathlib import Path -from typing import Union, TYPE_CHECKING - -from autoconf import conf +from typing import TYPE_CHECKING if TYPE_CHECKING: from autoarray.structures.abstract_structure import Structure @@ -29,11 +25,6 @@ def wrapper(self, *args, **kwargs): return wrapper -class AbstractNDArray(np.ndarray, ABC): - def __reduce__(self): - pickled_state = super().__reduce__() - - def unwrap_array(func): def wrapper(self, other): try: @@ -191,12 +182,6 @@ def sum(self, *args, **kwargs): def __float__(self): return float(self._array) - # noinspection PyMethodOverriding - def __setstate__(self, state): - for key, value in state[-1].items(): - setattr(self, key, value) - super().__setstate__(state[0:-1]) - @property @abstractmethod def native(self) -> Structure: @@ -204,13 +189,7 @@ def native(self) -> Structure: Returns the data structure in its `native` format which contains all unmaksed values to the native dimensions. """ - @staticmethod - def flip_hdu_for_ds9(values): - if conf.instance["general"]["fits"]["flip_for_ds9"]: - return np.flipud(values) - return values - - def output_to_fits(self, file_path: Union[Path, str], overwrite: bool = False): + def output_to_fits(self, file_path: str, overwrite: bool = False): """ Output the grid to a .fits file. From 91fc6e23d95145a5e3e6863b652a53852e1d46af Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 14:54:48 +0000 Subject: [PATCH 094/154] casting masks to arrays for use in numba --- autoarray/structures/arrays/array_1d_util.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/autoarray/structures/arrays/array_1d_util.py b/autoarray/structures/arrays/array_1d_util.py index c9d74d4aa..e7a3112d7 100644 --- a/autoarray/structures/arrays/array_1d_util.py +++ b/autoarray/structures/arrays/array_1d_util.py @@ -50,11 +50,15 @@ def convert_array_1d( return array_1d elif not store_native: return array_1d_slim_from( - array_1d_native=array_1d, mask_1d=mask_1d, sub_size=mask_1d.sub_size + array_1d_native=array_1d, + mask_1d=np.array(mask_1d), + sub_size=mask_1d.sub_size, ) return array_1d_native_from( - array_1d_slim=array_1d, mask_1d=mask_1d, sub_size=mask_1d.sub_size + array_1d_slim=array_1d, + mask_1d=np.array(mask_1d), + sub_size=mask_1d.sub_size, ) From c64010a6641be50b9b6ac325dace4ce850acbd34 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 16:08:34 +0000 Subject: [PATCH 095/154] more casting arrays --- autoarray/mask/derive/indexes_2d.py | 18 +++++++++++------- autoarray/preloads.py | 9 +++++---- test_autoarray/test_preloads.py | 8 +++++--- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/autoarray/mask/derive/indexes_2d.py b/autoarray/mask/derive/indexes_2d.py index df953995b..2afab5e12 100644 --- a/autoarray/mask/derive/indexes_2d.py +++ b/autoarray/mask/derive/indexes_2d.py @@ -123,7 +123,7 @@ def native_for_slim(self) -> np.ndarray: print(derive_indexes_2d.native_for_slim) """ return mask_2d_util.native_index_for_slim_index_2d_from( - mask_2d=self.mask, sub_size=1 + mask_2d=np.array(self.mask), sub_size=1 ).astype("int") @cached_property @@ -234,7 +234,7 @@ def slim_for_sub_slim(self) -> np.ndarray: print(derive_indexes_2d.slim_for_sub_slim) """ return mask_2d_util.slim_index_for_sub_slim_index_via_mask_2d_from( - mask_2d=self.mask, sub_size=self.mask.sub_size + mask_2d=np.array(self.mask), sub_size=self.mask.sub_size ).astype("int") @property @@ -285,7 +285,7 @@ def unmasked_slim(self) -> np.ndarray: print(derive_indexes_2d.unmasked_slim) """ return mask_2d_util.mask_slim_indexes_from( - mask_2d=self.mask, return_masked_indexes=False + mask_2d=np.array(self.mask), return_masked_indexes=False ).astype("int") @property @@ -333,7 +333,7 @@ def masked_slim(self) -> np.ndarray: print(derive_indexes_2d.masked_slim) """ return mask_2d_util.mask_slim_indexes_from( - mask_2d=self.mask, return_masked_indexes=True + mask_2d=np.array(self.mask), return_masked_indexes=True ).astype("int") @property @@ -380,7 +380,9 @@ def edge_slim(self) -> np.ndarray: print(derive_indexes_2d.edge_slim) """ - return mask_2d_util.edge_1d_indexes_from(mask_2d=self.mask).astype("int") + return mask_2d_util.edge_1d_indexes_from(mask_2d=np.array(self.mask)).astype( + "int" + ) @property def edge_native(self) -> np.ndarray: @@ -480,7 +482,9 @@ def border_slim(self) -> np.ndarray: print(derive_indexes_2d.border_slim) """ - return mask_2d_util.border_slim_indexes_from(mask_2d=self.mask).astype("int") + return mask_2d_util.border_slim_indexes_from( + mask_2d=np.array(self.mask) + ).astype("int") @property def border_native(self) -> np.ndarray: @@ -578,5 +582,5 @@ def sub_border_slim(self) -> np.ndarray: print(derive_indexes_2d.sub_border_slim) """ return mask_2d_util.sub_border_pixel_slim_indexes_from( - mask_2d=self.mask, sub_size=self.mask.sub_size + mask_2d=np.array(self.mask), sub_size=self.mask.sub_size ).astype("int") diff --git a/autoarray/preloads.py b/autoarray/preloads.py index e49e2b183..0c587ab34 100644 --- a/autoarray/preloads.py +++ b/autoarray/preloads.py @@ -5,7 +5,6 @@ from autoconf import conf -from autoarray.inversion.inversion.imaging.abstract import AbstractInversionImaging from autoarray.inversion.linear_obj.func_list import AbstractLinearObjFuncList from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper @@ -101,9 +100,11 @@ def set_w_tilde_imaging(self, fit_0, fit_1): indexes, lengths, ) = inversion_imaging_util.w_tilde_curvature_preload_imaging_from( - noise_map_native=fit_0.noise_map.native, - kernel_native=fit_0.dataset.psf.native, - native_index_for_slim_index=fit_0.dataset.mask.derive_indexes.native_for_slim, + noise_map_native=np.array(fit_0.noise_map.native), + kernel_native=np.array(fit_0.dataset.psf.native), + native_index_for_slim_index=np.array( + fit_0.dataset.mask.derive_indexes.native_for_slim + ), ) self.w_tilde = WTildeImaging( diff --git a/test_autoarray/test_preloads.py b/test_autoarray/test_preloads.py index d3721a899..e1f995e96 100644 --- a/test_autoarray/test_preloads.py +++ b/test_autoarray/test_preloads.py @@ -68,9 +68,11 @@ def test__set_w_tilde(): indexes, lengths, ) = aa.util.inversion_imaging.w_tilde_curvature_preload_imaging_from( - noise_map_native=fit_0.noise_map.native, - kernel_native=fit_0.dataset.psf.native, - native_index_for_slim_index=fit_0.dataset.mask.derive_indexes.native_for_slim, + noise_map_native=np.array(fit_0.noise_map.native), + kernel_native=np.array(fit_0.dataset.psf.native), + native_index_for_slim_index=np.array( + fit_0.dataset.mask.derive_indexes.native_for_slim + ), ) assert preloads.w_tilde.curvature_preload[0] == curvature_preload[0] From 3a47c046ab2c4c8f023ef13103c943af477c55e7 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 16:12:06 +0000 Subject: [PATCH 096/154] more casting to array --- autoarray/mask/derive/mask_2d.py | 11 +++++++---- autoarray/structures/arrays/uniform_2d.py | 16 ++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/autoarray/mask/derive/mask_2d.py b/autoarray/mask/derive/mask_2d.py index 247c2739e..6fc57a059 100644 --- a/autoarray/mask/derive/mask_2d.py +++ b/autoarray/mask/derive/mask_2d.py @@ -2,7 +2,7 @@ import logging import copy import numpy as np -from typing import TYPE_CHECKING, List, Tuple, Union +from typing import TYPE_CHECKING, Tuple if TYPE_CHECKING: from autoarray.mask.mask_2d import Mask2D @@ -141,7 +141,7 @@ def sub_1(self) -> Mask2D: from autoarray.mask.mask_2d import Mask2D return Mask2D( - mask=self.mask, + mask=np.array(self.mask), sub_size=1, pixel_scales=self.mask.pixel_scales, origin=self.mask.origin, @@ -193,7 +193,8 @@ def rescaled_from(self, rescale_factor) -> Mask2D: from autoarray.mask.mask_2d import Mask2D rescaled_mask = mask_2d_util.rescaled_mask_2d_from( - mask_2d=self.mask, rescale_factor=rescale_factor + mask_2d=np.array(self.mask), + rescale_factor=rescale_factor, ) return Mask2D( @@ -258,7 +259,9 @@ def resized_from(self, new_shape, pad_value: int = 0.0) -> Mask2D: mask = copy.deepcopy(self.mask) resized_mask = array_2d_util.resized_array_2d_from( - array_2d=mask, resized_shape=new_shape, pad_value=pad_value + array_2d=np.array(mask), + resized_shape=new_shape, + pad_value=pad_value, ).astype("bool") return Mask2D( diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 602e9e8cd..b6e7a59ed 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -461,7 +461,7 @@ def in_counts_per_second(self) -> "Array2D": @property def original_orientation(self) -> Union[np.ndarray, "Array2D"]: return layout_util.rotate_array_via_roe_corner_from( - array=self, roe_corner=self.header.original_roe_corner + array=np.array(self), roe_corner=self.header.original_roe_corner ) @property @@ -504,7 +504,7 @@ def zoomed_around_mask(self, buffer: int = 1) -> "Array2D": """ extracted_array_2d = array_2d_util.extracted_array_2d_from( - array_2d=self.native, + array_2d=np.array(self.native), y0=self.mask.zoom_region[0] - buffer, y1=self.mask.zoom_region[1] + buffer, x0=self.mask.zoom_region[2] - buffer, @@ -539,7 +539,7 @@ def extent_of_zoomed_array(self, buffer: int = 1) -> np.ndarray: The number pixels around the extracted array used as a buffer. """ extracted_array_2d = array_2d_util.extracted_array_2d_from( - array_2d=self.native, + array_2d=np.array(self.native), y0=self.mask.zoom_region[0] - buffer, y1=self.mask.zoom_region[1] + buffer, x0=self.mask.zoom_region[2] - buffer, @@ -574,7 +574,7 @@ def resized_from( """ resized_array_2d = array_2d_util.resized_array_2d_from( - array_2d=self.native, resized_shape=new_shape + array_2d=np.array(self.native), resized_shape=new_shape ) resized_mask = self.mask.derive_mask.resized_from( @@ -668,7 +668,7 @@ def hdu_for_output(self) -> fits.PrimaryHDU: The HDU containing the data and its header which can then be written to .fits. """ return array_2d_util.hdu_for_output_from( - array_2d=self.native, header_dict=self.pixel_scale_header + array_2d=np.array(self.native), header_dict=self.pixel_scale_header ) def output_to_fits(self, file_path: Union[Path, str], overwrite: bool = False): @@ -686,7 +686,7 @@ def output_to_fits(self, file_path: Union[Path, str], overwrite: bool = False): If a file already exists at the path, if overwrite=True it is overwritten else an error is raised. """ array_2d_util.numpy_array_2d_to_fits( - array_2d=self.native, + array_2d=np.array(self.native), file_path=file_path, overwrite=overwrite, header_dict=self.pixel_scale_header, @@ -1169,7 +1169,7 @@ def from_yx_and_values( header: Header = None, ) -> "Array2D": """ - Returns an ``Array2D`` by by inputting the y and x pixel values where the array is filled and the values that + Returns an ``Array2D`` by inputting the y and x pixel values where the array is filled and the values that fill it. For a full description of ``Array2D`` objects, including a description of the ``slim`` and ``native`` attribute @@ -1235,7 +1235,7 @@ def from_yx_and_values( ) grid_pixels = geometry_util.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid.slim, + grid_scaled_2d_slim=np.array(grid.slim), shape_native=shape_native, pixel_scales=pixel_scales, ) From 78de17fe902a5e09724db07679cb94fb18c81835 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 16:16:03 +0000 Subject: [PATCH 097/154] more casting to array --- autoarray/structures/grids/grid_1d_util.py | 8 ++++++-- autoarray/structures/grids/iterate_2d.py | 9 +++++++-- autoarray/structures/grids/uniform_1d.py | 7 +++++-- autoarray/structures/grids/uniform_2d.py | 20 ++++++++++++-------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/autoarray/structures/grids/grid_1d_util.py b/autoarray/structures/grids/grid_1d_util.py index 121d2910c..598b10cc0 100644 --- a/autoarray/structures/grids/grid_1d_util.py +++ b/autoarray/structures/grids/grid_1d_util.py @@ -47,10 +47,14 @@ def convert_grid_1d( return grid_1d elif not store_native: return grid_1d_slim_from( - grid_1d_native=grid_1d, mask_1d=mask_1d, sub_size=mask_1d.sub_size + grid_1d_native=grid_1d, + mask_1d=np.array(mask_1d), + sub_size=mask_1d.sub_size, ) return grid_1d_native_from( - grid_1d_slim=grid_1d, mask_1d=mask_1d, sub_size=mask_1d.sub_size + grid_1d_slim=grid_1d, + mask_1d=np.array(mask_1d), + sub_size=mask_1d.sub_size, ) diff --git a/autoarray/structures/grids/iterate_2d.py b/autoarray/structures/grids/iterate_2d.py index b05621a98..52160f98d 100644 --- a/autoarray/structures/grids/iterate_2d.py +++ b/autoarray/structures/grids/iterate_2d.py @@ -82,7 +82,9 @@ def __init__( self.sub_steps = sub_steps super().__init__( - values=values, mask=mask, store_native=store_native, + values=values, + mask=mask, + store_native=store_native, ) def __array_finalize__(self, obj): @@ -258,7 +260,10 @@ def from_mask( """ grid_slim = grid_2d_util.grid_2d_slim_via_mask_from( - mask_2d=mask, pixel_scales=mask.pixel_scales, sub_size=1, origin=mask.origin + mask_2d=np.array(mask), + pixel_scales=mask.pixel_scales, + sub_size=1, + origin=mask.origin, ) return Grid2DIterate( diff --git a/autoarray/structures/grids/uniform_1d.py b/autoarray/structures/grids/uniform_1d.py index a7fcf78ba..7dfd01b08 100644 --- a/autoarray/structures/grids/uniform_1d.py +++ b/autoarray/structures/grids/uniform_1d.py @@ -21,7 +21,10 @@ class Grid1D(Structure): def __init__( - self, values: Union[np.ndarray, List], mask: Mask1D, store_native: bool = False, + self, + values: Union[np.ndarray, List], + mask: Mask1D, + store_native: bool = False, ): """ A grid of 1D (x) coordinates, which are paired to a uniform 1D mask of pixels and sub-pixels. Each entry @@ -265,7 +268,7 @@ def from_mask(cls, mask: Mask1D) -> "Grid1D": """ sub_grid_1d = grid_1d_util.grid_1d_slim_via_mask_from( - mask_1d=mask, + mask_1d=np.array(mask), pixel_scales=mask.pixel_scales, sub_size=mask.sub_size, origin=mask.origin, diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 1e523e695..8b85fc0a1 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -238,7 +238,9 @@ def __init__( mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck. """ values = grid_2d_util.convert_grid_2d( - grid_2d=values, mask_2d=mask, store_native=store_native, + grid_2d=values, + mask_2d=mask, + store_native=store_native, ) super().__init__(values) @@ -604,7 +606,7 @@ def from_mask(cls, mask: Mask2D) -> "Grid2D": """ sub_grid_1d = grid_2d_util.grid_2d_slim_via_mask_from( - mask_2d=mask, + mask_2d=np.array(mask), pixel_scales=mask.pixel_scales, sub_size=mask.sub_size, origin=mask.origin, @@ -1071,12 +1073,14 @@ def grid_2d_radial_projected_from( positive x-axis. """ - grid_radial_projected_2d = grid_2d_util.grid_scaled_2d_slim_radial_projected_from( - extent=self.geometry.extent, - centre=centre, - pixel_scales=self.mask.pixel_scales, - sub_size=self.mask.sub_size, - shape_slim=shape_slim, + grid_radial_projected_2d = ( + grid_2d_util.grid_scaled_2d_slim_radial_projected_from( + extent=self.geometry.extent, + centre=centre, + pixel_scales=self.mask.pixel_scales, + sub_size=self.mask.sub_size, + shape_slim=shape_slim, + ) ) grid_radial_projected_2d = geometry_util.transform_grid_2d_to_reference_frame( From 80c1c709ef10c713068ab5c954b53fe5c00bf6e1 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 16:19:20 +0000 Subject: [PATCH 098/154] consistent attribute name usage in decorator --- autoarray/numba_util.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/autoarray/numba_util.py b/autoarray/numba_util.py index 92b7c9854..fc16e4512 100644 --- a/autoarray/numba_util.py +++ b/autoarray/numba_util.py @@ -128,13 +128,13 @@ def wrapper(obj, *args, **kwargs): for i in range(profile_call_max): key_func = f"{func.__name__}_{i}" - if key_func not in obj.profiling_dict: + if key_func not in obj.run_time_dict: if last_key_before_call == last_key_after_call: obj.run_time_dict[key_func] = time_func else: - for key, value in reversed(list(obj.profiling_dict.items())): + for key, value in reversed(list(obj.run_time_dict.items())): if last_key_before_call == key: - obj.profiling_dict[key_func] = time_func + obj.run_time_dict[key_func] = time_func break time_func -= obj.run_time_dict[key] From 3d6c1aaf139cf331269d1e7cdd40769bfd2f92d4 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Dec 2023 16:23:55 +0000 Subject: [PATCH 099/154] more casting to array --- autoarray/mask/derive/mask_2d.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/autoarray/mask/derive/mask_2d.py b/autoarray/mask/derive/mask_2d.py index 6fc57a059..539eb7bf4 100644 --- a/autoarray/mask/derive/mask_2d.py +++ b/autoarray/mask/derive/mask_2d.py @@ -431,7 +431,8 @@ def blurring_from(self, kernel_shape_native: Tuple[int, int]) -> Mask2D: raise exc.MaskException("psf_size of exterior region must be odd") blurring_mask = mask_2d_util.blurring_mask_2d_from( - mask_2d=self.mask, kernel_shape_native=kernel_shape_native + mask_2d=np.array(self.mask), + kernel_shape_native=kernel_shape_native, ) return Mask2D( @@ -552,9 +553,9 @@ def edge_buffed(self) -> Mask2D: """ from autoarray.mask.mask_2d import Mask2D - edge_buffed_mask = mask_2d_util.buffed_mask_2d_from(mask_2d=self.mask).astype( - "bool" - ) + edge_buffed_mask = mask_2d_util.buffed_mask_2d_from( + mask_2d=np.array(self.mask) + ).astype("bool") return Mask2D( mask=edge_buffed_mask, From 8e3b781d3fa9e7e655da657131d140c068c321ef Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:12:39 +0000 Subject: [PATCH 100/154] more casting to array --- autoarray/dataset/imaging/dataset.py | 4 ++-- autoarray/operators/convolver.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index 6e62fbd14..3d4addc38 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -170,8 +170,8 @@ def w_tilde(self): indexes, lengths, ) = inversion_imaging_util.w_tilde_curvature_preload_imaging_from( - noise_map_native=self.noise_map.native, - kernel_native=self.psf.native, + noise_map_native=np.array(self.noise_map.native), + kernel_native=np.array(self.psf.native), native_index_for_slim_index=self.mask.derive_indexes.native_for_slim, ) diff --git a/autoarray/operators/convolver.py b/autoarray/operators/convolver.py index 29c8caad5..5276fa21e 100644 --- a/autoarray/operators/convolver.py +++ b/autoarray/operators/convolver.py @@ -211,7 +211,7 @@ def __init__(self, mask, kernel): image_frame_1d_kernels, ) = self.frame_at_coordinates_jit( coordinates=(x, y), - mask=mask, + mask=np.array(mask), mask_index_array=self.mask_index_array, kernel_2d=self.kernel.native[:, :], ) @@ -227,7 +227,8 @@ def __init__(self, mask, kernel): mask_1d_index += 1 self.blurring_mask = mask_2d_util.blurring_mask_2d_from( - mask_2d=mask, kernel_shape_native=kernel.shape_native + mask_2d=np.array(mask), + kernel_shape_native=kernel.shape_native, ) self.pixels_in_blurring_mask = int( @@ -252,9 +253,9 @@ def __init__(self, mask, kernel): image_frame_1d_kernels, ) = self.frame_at_coordinates_jit( coordinates=(x, y), - mask=mask, - mask_index_array=self.mask_index_array, - kernel_2d=self.kernel.native, + mask=np.array(mask), + mask_index_array=np.array(self.mask_index_array), + kernel_2d=np.array(self.kernel.native), ) self.blurring_frame_1d_indexes[ mask_1d_index, : From c81c1e1902ee0a1f6648f5ca77f0b5b65f5445ab Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:18:40 +0000 Subject: [PATCH 101/154] more casting to array --- autoarray/structures/arrays/kernel_2d.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/arrays/kernel_2d.py b/autoarray/structures/arrays/kernel_2d.py index 0f8f56ee0..35b0e4807 100644 --- a/autoarray/structures/arrays/kernel_2d.py +++ b/autoarray/structures/arrays/kernel_2d.py @@ -45,7 +45,10 @@ def __init__( If True, the Kernel2D's array values are normalized such that they sum to 1.0. """ super().__init__( - values=values, mask=mask, header=header, store_native=store_native, + values=values, + mask=mask, + header=header, + store_native=store_native, ) if normalize: @@ -258,7 +261,7 @@ def from_gaussian( grid = Grid2D.uniform(shape_native=shape_native, pixel_scales=pixel_scales) grid_shifted = np.subtract(grid, centre) - grid_radius = np.sqrt(np.sum(grid_shifted ** 2.0, 1)) + grid_radius = np.sqrt(np.sum(grid_shifted**2.0, 1)) theta_coordinate_to_profile = np.arctan2( grid_shifted[:, 0], grid_shifted[:, 1] ) - np.radians(angle) @@ -546,7 +549,9 @@ def convolved_array_from(self, array: Array2D) -> Array2D: ) convolved_array_1d = array_2d_util.array_2d_slim_from( - mask_2d=array_binned_2d.mask, array_2d_native=convolved_array_2d, sub_size=1 + mask_2d=np.array(array_binned_2d.mask), + array_2d_native=convolved_array_2d, + sub_size=1, ) return Array2D(values=convolved_array_1d, mask=array_binned_2d.mask) From 43a5f3f13e016316d92972cbd30ed7de22522935 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:21:09 +0000 Subject: [PATCH 102/154] more casting to array --- autoarray/structures/arrays/array_1d_util.py | 2 +- autoarray/structures/arrays/array_2d_util.py | 3 ++- autoarray/structures/grids/grid_1d_util.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/arrays/array_1d_util.py b/autoarray/structures/arrays/array_1d_util.py index e7a3112d7..4cec23540 100644 --- a/autoarray/structures/arrays/array_1d_util.py +++ b/autoarray/structures/arrays/array_1d_util.py @@ -138,7 +138,7 @@ def array_1d_native_from( ).astype("int") return array_1d_via_indexes_1d_from( - array_1d_slim=array_1d_slim, + array_1d_slim=np.array(array_1d_slim), sub_shape=sub_shape, native_index_for_slim_index_1d=native_index_for_slim_index_1d, ) diff --git a/autoarray/structures/arrays/array_2d_util.py b/autoarray/structures/arrays/array_2d_util.py index e7abde2ad..ddd9d0714 100644 --- a/autoarray/structures/arrays/array_2d_util.py +++ b/autoarray/structures/arrays/array_2d_util.py @@ -600,7 +600,8 @@ def array_2d_native_from( sub_shape = (mask_2d.shape[0] * sub_size, mask_2d.shape[1] * sub_size) native_index_for_slim_index_2d = mask_2d_util.native_index_for_slim_index_2d_from( - mask_2d=mask_2d, sub_size=sub_size + mask_2d=np.array(mask_2d), + sub_size=sub_size, ).astype("int") return array_2d_via_indexes_from( diff --git a/autoarray/structures/grids/grid_1d_util.py b/autoarray/structures/grids/grid_1d_util.py index 598b10cc0..6735529a7 100644 --- a/autoarray/structures/grids/grid_1d_util.py +++ b/autoarray/structures/grids/grid_1d_util.py @@ -202,7 +202,9 @@ def grid_1d_slim_from( """ return array_1d_util.array_1d_slim_from( - array_1d_native=grid_1d_native, mask_1d=mask_1d, sub_size=sub_size + array_1d_native=np.array(grid_1d_native), + mask_1d=mask_1d, + sub_size=sub_size, ) From 64a589069ee1e8d1b6f1e28d75bf747cc5ba4c2d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:25:10 +0000 Subject: [PATCH 103/154] more casting to array --- autoarray/structures/grids/grid_2d_util.py | 8 ++++-- autoarray/structures/grids/iterate_2d.py | 30 +++++++++++----------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/autoarray/structures/grids/grid_2d_util.py b/autoarray/structures/grids/grid_2d_util.py index dfb1e968a..eb99e6ab8 100644 --- a/autoarray/structures/grids/grid_2d_util.py +++ b/autoarray/structures/grids/grid_2d_util.py @@ -722,11 +722,15 @@ def grid_2d_slim_from( """ grid_1d_slim_y = array_2d_util.array_2d_slim_from( - array_2d_native=grid_2d_native[:, :, 0], mask_2d=mask, sub_size=sub_size + array_2d_native=grid_2d_native[:, :, 0], + mask_2d=np.array(mask), + sub_size=sub_size, ) grid_1d_slim_x = array_2d_util.array_2d_slim_from( - array_2d_native=grid_2d_native[:, :, 1], mask_2d=mask, sub_size=sub_size + array_2d_native=grid_2d_native[:, :, 1], + mask_2d=np.array(mask), + sub_size=sub_size, ) return np.stack((grid_1d_slim_y, grid_1d_slim_x), axis=-1) diff --git a/autoarray/structures/grids/iterate_2d.py b/autoarray/structures/grids/iterate_2d.py index 52160f98d..deb751318 100644 --- a/autoarray/structures/grids/iterate_2d.py +++ b/autoarray/structures/grids/iterate_2d.py @@ -708,10 +708,10 @@ def threshold_mask_via_grids_from( threshold_mask = self.threshold_mask_via_grids_jit_from( fractional_accuracy_threshold=self.fractional_accuracy, relative_accuracy_threshold=self.relative_accuracy, - threshold_mask=threshold_mask, - grid_higher_sub_2d=grid_higher_sub_2d, - grid_lower_sub_2d=grid_lower_sub_2d, - grid_higher_mask=grid_higher_sub_2d.mask, + threshold_mask=np.array(threshold_mask), + grid_higher_sub_2d=np.array(grid_higher_sub_2d), + grid_lower_sub_2d=np.array(grid_lower_sub_2d), + grid_higher_mask=np.array(grid_higher_sub_2d.mask), ) return Mask2D( @@ -725,11 +725,11 @@ def threshold_mask_via_grids_from( def threshold_mask_via_grids_jit_from( fractional_accuracy_threshold: float, relative_accuracy_threshold: float, - threshold_mask: Mask2D, - grid_higher_sub_2d: Grid2D, - grid_lower_sub_2d: Grid2D, - grid_higher_mask: Mask2D, - ) -> Mask2D: + threshold_mask: np.ndarray, + grid_higher_sub_2d: np.ndarray, + grid_lower_sub_2d: np.ndarray, + grid_higher_mask: np.ndarray, + ) -> np.ndarray: """ Jitted function to determine the fractional mask, which is a mask where: @@ -841,9 +841,9 @@ def iterated_grid_from( iterated_grid = self.iterated_grid_jit_from( iterated_grid=iterated_grid, - threshold_mask_higher_sub=threshold_mask_higher_sub, - threshold_mask_lower_sub=threshold_mask_lower_sub, - grid_higher_sub_2d=grid_higher_sub, + threshold_mask_higher_sub=np.array(threshold_mask_higher_sub), + threshold_mask_lower_sub=np.array(threshold_mask_lower_sub), + grid_higher_sub_2d=np.array(grid_higher_sub), ) if threshold_mask_higher_sub.is_all_true: @@ -875,9 +875,9 @@ def iterated_grid_from( @numba_util.jit() def iterated_grid_jit_from( iterated_grid: Grid2D, - threshold_mask_higher_sub: Mask2D, - threshold_mask_lower_sub: Mask2D, - grid_higher_sub_2d: Grid2D, + threshold_mask_higher_sub: np.ndarray, + threshold_mask_lower_sub: np.ndarray, + grid_higher_sub_2d: np.ndarray, ) -> Grid2D: """ Create the iterated grid from a result grid that is computed at a higher sub size level than the previous grid. From 2fb990b1b28a68173c46a9a3235a2c6521830523 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:26:52 +0000 Subject: [PATCH 104/154] more casting to array --- autoarray/structures/grids/iterate_2d.py | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/autoarray/structures/grids/iterate_2d.py b/autoarray/structures/grids/iterate_2d.py index deb751318..f46fe790b 100644 --- a/autoarray/structures/grids/iterate_2d.py +++ b/autoarray/structures/grids/iterate_2d.py @@ -496,10 +496,10 @@ def threshold_mask_via_arrays_from( threshold_mask = self.threshold_mask_via_arrays_jit_from( fractional_accuracy_threshold=self.fractional_accuracy, relative_accuracy_threshold=self.relative_accuracy, - threshold_mask=threshold_mask, - array_higher_sub_2d=array_higher_sub_2d, - array_lower_sub_2d=array_lower_sub_2d, - array_higher_mask=array_higher_sub_2d.mask, + threshold_mask=np.array(threshold_mask), + array_higher_sub_2d=np.array(array_higher_sub_2d), + array_lower_sub_2d=np.array(array_lower_sub_2d), + array_higher_mask=np.array(array_higher_sub_2d.mask), ) return Mask2D( @@ -513,10 +513,10 @@ def threshold_mask_via_arrays_from( def threshold_mask_via_arrays_jit_from( fractional_accuracy_threshold: float, relative_accuracy_threshold: Optional[float], - threshold_mask: Mask2D, - array_higher_sub_2d: Array2D, - array_lower_sub_2d: Array2D, - array_higher_mask: Mask2D, + threshold_mask: np.ndarray, + array_higher_sub_2d: np.ndarray, + array_lower_sub_2d: np.ndarray, + array_higher_mask: np.ndarray, ) -> np.ndarray: """ Jitted function to determine the fractional mask, which is a mask where: @@ -609,9 +609,9 @@ def iterated_array_from( iterated_array = self.iterated_array_jit_from( iterated_array=iterated_array, - threshold_mask_higher_sub=threshold_mask_higher_sub, - threshold_mask_lower_sub=threshold_mask_lower_sub, - array_higher_sub_2d=array_higher_sub, + threshold_mask_higher_sub=np.array(threshold_mask_higher_sub), + threshold_mask_lower_sub=np.array(threshold_mask_lower_sub), + array_higher_sub_2d=np.array(array_higher_sub), ) except ZeroDivisionError: @@ -649,7 +649,9 @@ def return_iterated_array_result(self, iterated_array: Array2D) -> Array2D: """ iterated_array_1d = array_2d_util.array_2d_slim_from( - mask_2d=self.mask, array_2d_native=iterated_array, sub_size=1 + mask_2d=np.array(self.mask), + array_2d_native=np.array(iterated_array), + sub_size=1, ) return Array2D(values=iterated_array_1d, mask=self.mask.derive_mask.sub_1) @@ -657,11 +659,11 @@ def return_iterated_array_result(self, iterated_array: Array2D) -> Array2D: @staticmethod @numba_util.jit() def iterated_array_jit_from( - iterated_array: Array2D, - threshold_mask_higher_sub: Mask2D, - threshold_mask_lower_sub: Mask2D, - array_higher_sub_2d: Array2D, - ) -> Array2D: + iterated_array: np.ndarray, + threshold_mask_higher_sub: np.ndarray, + threshold_mask_lower_sub: np.ndarray, + array_higher_sub_2d: np.ndarray, + ) -> np.ndarray: """ Create the iterated array from a result array that is computed at a higher sub size leel than the previous grid. From ee292dfdc1da9aa3d2b7386f14485f254fbfad35 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:39:49 +0000 Subject: [PATCH 105/154] more casting to array --- autoarray/mask/derive/grid_2d.py | 3 ++- autoarray/operators/transformer.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/autoarray/mask/derive/grid_2d.py b/autoarray/mask/derive/grid_2d.py index a589427c1..a6ee331a1 100644 --- a/autoarray/mask/derive/grid_2d.py +++ b/autoarray/mask/derive/grid_2d.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging from typing import TYPE_CHECKING +import numpy as np if TYPE_CHECKING: from autoarray.mask.mask_2d import Mask2D @@ -225,7 +226,7 @@ def unmasked_sub_1(self) -> Grid2D: from autoarray.structures.grids.uniform_2d import Grid2D grid_slim = grid_2d_util.grid_2d_slim_via_mask_from( - mask_2d=self.mask, + mask_2d=np.array(self.mask), pixel_scales=self.mask.pixel_scales, sub_size=1, origin=self.mask.origin, diff --git a/autoarray/operators/transformer.py b/autoarray/operators/transformer.py index 55d28ca13..d318a6b82 100644 --- a/autoarray/operators/transformer.py +++ b/autoarray/operators/transformer.py @@ -70,11 +70,13 @@ def __init__(self, uv_wavelengths, real_space_mask, preload_transform=True): if preload_transform: self.preload_real_transforms = transformer_util.preload_real_transforms( - grid_radians=self.grid, uv_wavelengths=self.uv_wavelengths + grid_radians=np.array(self.grid), + uv_wavelengths=self.uv_wavelengths, ) self.preload_imag_transforms = transformer_util.preload_imag_transforms( - grid_radians=self.grid, uv_wavelengths=self.uv_wavelengths + grid_radians=np.array(self.grid), + uv_wavelengths=self.uv_wavelengths, ) self.real_space_pixels = self.real_space_mask.pixels_in_mask From c5463f48bbd00c3eaccbd500937f4fd904a8fb3e Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:43:36 +0000 Subject: [PATCH 106/154] more casting to array --- autoarray/inversion/inversion/imaging/mapping.py | 4 ++-- autoarray/inversion/pixelization/mappers/voronoi.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/autoarray/inversion/inversion/imaging/mapping.py b/autoarray/inversion/inversion/imaging/mapping.py index cd176a983..e5361ab63 100644 --- a/autoarray/inversion/inversion/imaging/mapping.py +++ b/autoarray/inversion/inversion/imaging/mapping.py @@ -130,8 +130,8 @@ def data_vector(self) -> np.ndarray: return inversion_imaging_util.data_vector_via_blurred_mapping_matrix_from( blurred_mapping_matrix=operated_mapping_matrix, - image=self.data, - noise_map=self.noise_map, + image=np.array(self.data), + noise_map=np.array(self.noise_map), ) @property diff --git a/autoarray/inversion/pixelization/mappers/voronoi.py b/autoarray/inversion/pixelization/mappers/voronoi.py index 3551d5804..164c92683 100644 --- a/autoarray/inversion/pixelization/mappers/voronoi.py +++ b/autoarray/inversion/pixelization/mappers/voronoi.py @@ -229,9 +229,9 @@ def pix_sub_weights(self) -> PixSubWeights: The weights are used when creating the `mapping_matrix` and `pixel_signals_from`. """ mappings = mapper_util.pix_indexes_for_sub_slim_index_voronoi_from( - grid=self.source_plane_data_grid, + grid=np.array(self.source_plane_data_grid), slim_index_for_sub_slim_index=self.source_plane_data_grid.mask.derive_indexes.slim_for_sub_slim, - mesh_grid=self.source_plane_mesh_grid, + mesh_grid=np.array(self.source_plane_mesh_grid), neighbors=self.source_plane_mesh_grid.neighbors, neighbors_sizes=self.source_plane_mesh_grid.neighbors.sizes, ).astype("int") From 8648a0e0ac3bc8fb7975b9ba6c469a7e755a5443 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:43:44 +0000 Subject: [PATCH 107/154] fix a broken reference in init --- autoarray/structures/mesh/triangulation_2d.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/autoarray/structures/mesh/triangulation_2d.py b/autoarray/structures/mesh/triangulation_2d.py index 3df5d48a6..d882492a5 100644 --- a/autoarray/structures/mesh/triangulation_2d.py +++ b/autoarray/structures/mesh/triangulation_2d.py @@ -1,7 +1,6 @@ import numpy as np import scipy.spatial -import scipy.spatial.qhull as qhull -from typing import Optional, List, Union, Tuple +from typing import List, Union, Tuple from autoarray.structures.abstract_structure import Structure from autoconf import cached_property @@ -67,9 +66,6 @@ def __init__( if type(values) is list: values = np.asarray(values) - self.nearest_pixelization_index_for_slim_index = ( - nearest_pixelization_index_for_slim_index - ) self.uses_interpolation = uses_interpolation super().__init__(values) From a6b32cee7d98fcb8f67ab0fe280436e8c7378ce3 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:50:46 +0000 Subject: [PATCH 108/154] more casting to array --- autoarray/inversion/inversion/imaging/w_tilde.py | 6 +++--- autoarray/inversion/pixelization/mappers/delaunay.py | 6 +++--- autoarray/inversion/pixelization/mappers/rectangular.py | 2 +- autoarray/operators/convolver.py | 6 +----- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/autoarray/inversion/inversion/imaging/w_tilde.py b/autoarray/inversion/inversion/imaging/w_tilde.py index ff4efb9f6..a5cee94fd 100644 --- a/autoarray/inversion/inversion/imaging/w_tilde.py +++ b/autoarray/inversion/inversion/imaging/w_tilde.py @@ -79,9 +79,9 @@ def __init__( @profile_func def w_tilde_data(self): return inversion_imaging_util.w_tilde_data_imaging_from( - image_native=self.data.native, - noise_map_native=self.noise_map.native, - kernel_native=self.convolver.kernel.native, + image_native=np.array(self.data.native), + noise_map_native=np.array(self.noise_map.native), + kernel_native=np.array(self.convolver.kernel.native), native_index_for_slim_index=self.data.mask.derive_indexes.native_for_slim, ) diff --git a/autoarray/inversion/pixelization/mappers/delaunay.py b/autoarray/inversion/pixelization/mappers/delaunay.py index 745b86326..51715a6ac 100644 --- a/autoarray/inversion/pixelization/mappers/delaunay.py +++ b/autoarray/inversion/pixelization/mappers/delaunay.py @@ -131,7 +131,7 @@ def pix_sub_weights(self) -> PixSubWeights: pix_indexes_for_simplex_index = delaunay.simplices mappings, sizes = mapper_util.pix_indexes_for_sub_slim_index_delaunay_from( - source_plane_data_grid=self.source_plane_data_grid, + source_plane_data_grid=np.array(self.source_plane_data_grid), simplex_index_for_sub_slim_index=simplex_index_for_sub_slim_index, pix_indexes_for_simplex_index=pix_indexes_for_simplex_index, delaunay_points=delaunay.points, @@ -141,8 +141,8 @@ def pix_sub_weights(self) -> PixSubWeights: sizes = sizes.astype("int") weights = mapper_util.pixel_weights_delaunay_from( - source_plane_data_grid=self.source_plane_data_grid, - source_plane_mesh_grid=self.source_plane_mesh_grid, + source_plane_data_grid=np.array(self.source_plane_data_grid), + source_plane_mesh_grid=np.array(self.source_plane_mesh_grid), slim_index_for_sub_slim_index=self.slim_index_for_sub_slim_index, pix_indexes_for_sub_slim_index=mappings, ) diff --git a/autoarray/inversion/pixelization/mappers/rectangular.py b/autoarray/inversion/pixelization/mappers/rectangular.py index a1b797004..f8e97fa52 100644 --- a/autoarray/inversion/pixelization/mappers/rectangular.py +++ b/autoarray/inversion/pixelization/mappers/rectangular.py @@ -113,7 +113,7 @@ def pix_sub_weights(self) -> PixSubWeights: are equal to 1.0. """ mappings = geometry_util.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=self.source_plane_data_grid, + grid_scaled_2d_slim=np.array(self.source_plane_data_grid), shape_native=self.source_plane_mesh_grid.shape_native, pixel_scales=self.source_plane_mesh_grid.pixel_scales, origin=self.source_plane_mesh_grid.origin, diff --git a/autoarray/operators/convolver.py b/autoarray/operators/convolver.py index 5276fa21e..59412e132 100644 --- a/autoarray/operators/convolver.py +++ b/autoarray/operators/convolver.py @@ -385,12 +385,10 @@ def convolve_image_no_blurring(self, image): ---------- image 1D array of the values which are to be blurred with the convolver's PSF. - blurring_image - 1D array of the blurring values which blur into the array after PSF convolution. """ convolved_image = self.convolve_no_blurring_jit( - image_1d_array=image.binned.slim, + image_1d_array=np.array(image.binned.slim), image_frame_1d_indexes=self.image_frame_1d_indexes, image_frame_1d_kernels=self.image_frame_1d_kernels, image_frame_1d_lengths=self.image_frame_1d_lengths, @@ -405,8 +403,6 @@ def convolve_image_no_blurring_interpolation(self, image): ---------- image 1D array of the values which are to be blurred with the convolver's PSF. - blurring_image - 1D array of the blurring values which blur into the array after PSF convolution. """ convolved_image = self.convolve_no_blurring_jit( From 0bd4ce8c9e94dacadc6aacd982580d2dc07505ac Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:52:10 +0000 Subject: [PATCH 109/154] more casting to array --- autoarray/inversion/inversion/imaging/w_tilde.py | 4 ++-- autoarray/inversion/pixelization/mappers/abstract.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/autoarray/inversion/inversion/imaging/w_tilde.py b/autoarray/inversion/inversion/imaging/w_tilde.py index a5cee94fd..6ab493390 100644 --- a/autoarray/inversion/inversion/imaging/w_tilde.py +++ b/autoarray/inversion/inversion/imaging/w_tilde.py @@ -224,8 +224,8 @@ def _data_vector_func_list_and_mapper(self) -> np.ndarray: diag = inversion_imaging_util.data_vector_via_blurred_mapping_matrix_from( blurred_mapping_matrix=operated_mapping_matrix, - image=self.data, - noise_map=self.noise_map, + image=np.array(self.data), + noise_map=np.array(self.noise_map), ) param_range = linear_func_param_range[linear_func_index] diff --git a/autoarray/inversion/pixelization/mappers/abstract.py b/autoarray/inversion/pixelization/mappers/abstract.py index 05e73ef2d..b66bf75b0 100644 --- a/autoarray/inversion/pixelization/mappers/abstract.py +++ b/autoarray/inversion/pixelization/mappers/abstract.py @@ -302,7 +302,7 @@ def pixel_signals_from(self, signal_scale: float) -> np.ndarray: pix_indexes_for_sub_slim_index=self.pix_indexes_for_sub_slim_index, pix_size_for_sub_slim_index=self.pix_sizes_for_sub_slim_index, slim_index_for_sub_slim_index=self.source_plane_data_grid.mask.derive_indexes.slim_for_sub_slim, - adapt_data=self.adapt_data, + adapt_data=np.array(self.adapt_data), ) def pix_indexes_for_slim_indexes(self, pix_indexes: List) -> List[List]: From 9c55d5c5ed8c4520d289f8c992d8419f6718b8fe Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:52:53 +0000 Subject: [PATCH 110/154] more casting to array --- .../inversion/inversion/interferometer/mapping.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/autoarray/inversion/inversion/interferometer/mapping.py b/autoarray/inversion/inversion/interferometer/mapping.py index 2fa23d541..604dcb0d5 100644 --- a/autoarray/inversion/inversion/interferometer/mapping.py +++ b/autoarray/inversion/inversion/interferometer/mapping.py @@ -86,8 +86,8 @@ def data_vector(self) -> np.ndarray: return inversion_interferometer_util.data_vector_via_transformed_mapping_matrix_from( transformed_mapping_matrix=self.operated_mapping_matrix, - visibilities=self.data, - noise_map=self.noise_map, + visibilities=np.array(self.data), + noise_map=np.array(self.noise_map), ) @cached_property @@ -147,12 +147,6 @@ def mapped_reconstructed_data_dict( To perform this mapping the `mapping_matrix` is used, which straightforwardly describes how every value of the `reconstruction` maps to pixels in the data-frame after the 2D non-uniform fast Fourier transformer operation has been performed. - - Parameters - ---------- - reconstruction - The reconstruction (in the source frame) whose values are mapped to a dictionary of values for each - individual mapper (in the image-plane). """ mapped_reconstructed_data_dict = {} From ce054ed083072655c6002cdc002eb7522bf0f863 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:54:10 +0000 Subject: [PATCH 111/154] more casting to array --- autoarray/mask/mask_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/mask/mask_2d.py b/autoarray/mask/mask_2d.py index 24532c4e4..3b9a108d1 100644 --- a/autoarray/mask/mask_2d.py +++ b/autoarray/mask/mask_2d.py @@ -949,7 +949,7 @@ def output_to_fits(self, file_path, overwrite=False): @property def mask_centre(self) -> Tuple[float, float]: return grid_2d_util.grid_2d_centre_from( - grid_2d_slim=self.derive_grid.unmasked_sub_1 + grid_2d_slim=np.array(self.derive_grid.unmasked_sub_1) ) @property From b0067431b62833899b4ce374b374b352e0538f53 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 09:56:12 +0000 Subject: [PATCH 112/154] lift flip_hdu_for_ds9 method from main branch --- autoarray/abstract_ndarray.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 4d512d1c4..8293de846 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -16,6 +16,7 @@ from autoarray.structures.abstract_structure import Structure from autoarray.structures.arrays import array_2d_util +from autoconf import conf def to_new_array(func): @@ -64,6 +65,12 @@ def instance_flatten(cls, instance): ) return values, keys + @staticmethod + def flip_hdu_for_ds9(values): + if conf.instance["general"]["fits"]["flip_for_ds9"]: + return np.flipud(values) + return values + @classmethod def instance_unflatten(cls, aux_data, children): instance = cls.__new__(cls) From 24d145ab35d80a23ad7e47533e1c25c9df2cc7da Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:00:10 +0000 Subject: [PATCH 113/154] more casting to array --- autoarray/geometry/geometry_2d.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoarray/geometry/geometry_2d.py b/autoarray/geometry/geometry_2d.py index 8542c3502..f622f9f39 100644 --- a/autoarray/geometry/geometry_2d.py +++ b/autoarray/geometry/geometry_2d.py @@ -6,6 +6,8 @@ from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.structures.grids.uniform_2d import Grid2D +import numpy as np + from autoarray.geometry.abstract_2d import AbstractGeometry2D from autoarray import type as ty @@ -206,7 +208,7 @@ def grid_pixels_2d_from(self, grid_scaled_2d: Grid2D) -> Grid2D: from autoarray.structures.grids.uniform_2d import Grid2D grid_pixels_2d = geometry_util.grid_pixels_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=self.shape_native, pixel_scales=self.pixel_scales, origin=self.origin, From ba599f105bfc72934ac3210dd2284711f70c72fa Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:02:11 +0000 Subject: [PATCH 114/154] more casting to array --- .../structures/grids/test_uniform_2d.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test_autoarray/structures/grids/test_uniform_2d.py b/test_autoarray/structures/grids/test_uniform_2d.py index 0bee3cfea..17e785025 100644 --- a/test_autoarray/structures/grids/test_uniform_2d.py +++ b/test_autoarray/structures/grids/test_uniform_2d.py @@ -526,14 +526,14 @@ def test__blurring_grid_from(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) blurring_mask_util = aa.util.mask_2d.blurring_mask_2d_from( - mask_2d=mask, kernel_shape_native=(3, 5) + mask_2d=np.array(mask), kernel_shape_native=(3, 5) ) blurring_grid_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( mask_2d=blurring_mask_util, pixel_scales=(2.0, 2.0), sub_size=1 ) - mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) + mask = aa.Mask2D(mask=np.array(mask), pixel_scales=(2.0, 2.0), sub_size=2) blurring_grid = aa.Grid2D.blurring_grid_from(mask=mask, kernel_shape_native=(3, 5)) @@ -561,7 +561,7 @@ def test__blurring_grid_via_kernel_shape_from(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) blurring_mask_util = aa.util.mask_2d.blurring_mask_2d_from( - mask_2d=mask, kernel_shape_native=(3, 5) + mask_2d=np.array(mask), kernel_shape_native=(3, 5) ) blurring_grid_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( @@ -687,7 +687,7 @@ def test__from_mask(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=1) grid_via_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( - mask_2d=mask, sub_size=1, pixel_scales=(2.0, 2.0) + mask_2d=np.array(mask), sub_size=1, pixel_scales=(2.0, 2.0) ) grid_2d = aa.Grid2D.from_mask(mask=mask) @@ -697,7 +697,9 @@ def test__from_mask(): assert grid_2d.pixel_scales == (2.0, 2.0) grid_2d_native = aa.util.grid_2d.grid_2d_native_from( - grid_2d_slim=grid_2d, mask_2d=mask, sub_size=mask.sub_size + grid_2d_slim=np.array(grid_2d), + mask_2d=np.array(mask), + sub_size=mask.sub_size, ) assert (grid_2d_native == grid_2d.native).all() @@ -972,7 +974,8 @@ def test__sub_border_flat_indexes(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) sub_border_flat_indexes_util = aa.util.mask_2d.sub_border_pixel_slim_indexes_from( - mask_2d=mask, sub_size=2 + mask_2d=np.array(mask), + sub_size=2, ) grid_2d = aa.Grid2D.from_mask(mask=mask) From b79ae22862d0d2498d12221b7d26af97f8537d3a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:02:58 +0000 Subject: [PATCH 115/154] more casting to array --- autoarray/structures/grids/uniform_2d.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 8b85fc0a1..0fa7510bf 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -1216,7 +1216,8 @@ def relocated_grid_from(self, grid: "Grid2D") -> "Grid2D": return Grid2D( values=grid_2d_util.relocated_grid_via_jit_from( - grid=grid, border_grid=self.sub_border_grid + grid=np.array(grid), + border_grid=self.sub_border_grid, ), mask=grid.mask, sub_size=grid.mask.sub_size, From 1fc20ae6b72be29e1a66f492f22c9194647d3521 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:11:06 +0000 Subject: [PATCH 116/154] more casting to array --- autoarray/operators/transformer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/operators/transformer.py b/autoarray/operators/transformer.py index d318a6b82..13f089737 100644 --- a/autoarray/operators/transformer.py +++ b/autoarray/operators/transformer.py @@ -96,14 +96,14 @@ def __init__(self, uv_wavelengths, real_space_mask, preload_transform=True): def visibilities_from(self, image): if self.preload_transform: visibilities = transformer_util.visibilities_via_preload_jit_from( - image_1d=image.binned, + image_1d=np.array(image.binned), preloaded_reals=self.preload_real_transforms, preloaded_imags=self.preload_imag_transforms, ) else: visibilities = transformer_util.visibilities_jit( - image_1d=image.binned, + image_1d=np.array(image.binned), grid_radians=self.grid, uv_wavelengths=self.uv_wavelengths, ) From a3d89e96c6bfb0a1aa2f0346ecf41f9c238bb159 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:12:23 +0000 Subject: [PATCH 117/154] more casting to array --- test_autoarray/mask/derive/test_indexes_2d.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test_autoarray/mask/derive/test_indexes_2d.py b/test_autoarray/mask/derive/test_indexes_2d.py index ebd9ead5a..9e3bd96a0 100644 --- a/test_autoarray/mask/derive/test_indexes_2d.py +++ b/test_autoarray/mask/derive/test_indexes_2d.py @@ -27,7 +27,7 @@ def make_indexes_2d_9x9(): def test__native_index_for_slim_index(indexes_2d_9x9): sub_native_index_for_sub_slim_index_2d = ( aa.util.mask_2d.native_index_for_slim_index_2d_from( - mask_2d=indexes_2d_9x9.mask, sub_size=1 + mask_2d=np.array(indexes_2d_9x9.mask), sub_size=1 ) ) @@ -46,7 +46,9 @@ def test__sub_mask_index_for_sub_mask_1d_index(): indexes_2d = aa.DeriveIndexes2D(mask=mask) sub_mask_index_for_sub_mask_1d_index = ( - aa.util.mask_2d.native_index_for_slim_index_2d_from(mask_2d=mask, sub_size=2) + aa.util.mask_2d.native_index_for_slim_index_2d_from( + mask_2d=np.array(mask), sub_size=2 + ) ) assert indexes_2d.sub_mask_native_for_sub_mask_slim == pytest.approx( @@ -65,7 +67,7 @@ def test__slim_index_for_sub_slim_index(): slim_index_for_sub_slim_index_util = ( aa.util.mask_2d.slim_index_for_sub_slim_index_via_mask_2d_from( - mask_2d=mask, sub_size=2 + mask_2d=np.array(mask), sub_size=2 ) ) @@ -74,7 +76,7 @@ def test__slim_index_for_sub_slim_index(): def test__unmasked_1d_indexes(indexes_2d_9x9): unmasked_pixels_util = aa.util.mask_2d.mask_slim_indexes_from( - mask_2d=indexes_2d_9x9.mask, return_masked_indexes=False + mask_2d=np.array(indexes_2d_9x9.mask), return_masked_indexes=False ) assert indexes_2d_9x9.unmasked_slim == pytest.approx(unmasked_pixels_util, 1e-4) @@ -82,7 +84,7 @@ def test__unmasked_1d_indexes(indexes_2d_9x9): def test__masked_1d_indexes(indexes_2d_9x9): masked_pixels_util = aa.util.mask_2d.mask_slim_indexes_from( - mask_2d=indexes_2d_9x9.mask, return_masked_indexes=True + mask_2d=np.array(indexes_2d_9x9.mask), return_masked_indexes=True ) assert indexes_2d_9x9.masked_slim == pytest.approx(masked_pixels_util, 1e-4) @@ -90,7 +92,7 @@ def test__masked_1d_indexes(indexes_2d_9x9): def test__edge_1d_indexes(indexes_2d_9x9): edge_1d_indexes_util = aa.util.mask_2d.edge_1d_indexes_from( - mask_2d=indexes_2d_9x9.mask + mask_2d=np.array(indexes_2d_9x9.mask) ) assert indexes_2d_9x9.edge_slim == pytest.approx(edge_1d_indexes_util, 1e-4) @@ -104,7 +106,7 @@ def test__edge_2d_indexes(indexes_2d_9x9): def test__border_1d_indexes(indexes_2d_9x9): border_pixels_util = aa.util.mask_2d.border_slim_indexes_from( - mask_2d=indexes_2d_9x9.mask + mask_2d=np.array(indexes_2d_9x9.mask) ) assert indexes_2d_9x9.border_slim == pytest.approx(border_pixels_util, 1e-4) @@ -134,7 +136,7 @@ def test__sub_border_flat_indexes(): indexes_2d = aa.DeriveIndexes2D(mask=mask) sub_border_pixels_util = aa.util.mask_2d.sub_border_pixel_slim_indexes_from( - mask_2d=mask, sub_size=2 + mask_2d=np.array(mask), sub_size=2 ) assert indexes_2d.sub_border_slim == pytest.approx(sub_border_pixels_util, 1e-4) From 05ae5f0dc75d5e4e7964d5e8ed57f7f7d56fdf70 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:13:55 +0000 Subject: [PATCH 118/154] more casting to array --- autoarray/structures/arrays/array_1d_util.py | 2 +- .../arrays/files/array/output_test/array.fits | Bin 5760 -> 5760 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/arrays/array_1d_util.py b/autoarray/structures/arrays/array_1d_util.py index 4cec23540..5810d1d73 100644 --- a/autoarray/structures/arrays/array_1d_util.py +++ b/autoarray/structures/arrays/array_1d_util.py @@ -50,7 +50,7 @@ def convert_array_1d( return array_1d elif not store_native: return array_1d_slim_from( - array_1d_native=array_1d, + array_1d_native=np.array(array_1d), mask_1d=np.array(mask_1d), sub_size=mask_1d.sub_size, ) diff --git a/test_autoarray/structures/arrays/files/array/output_test/array.fits b/test_autoarray/structures/arrays/files/array/output_test/array.fits index 97c9a0707f9c5c472bf5ac3a1811cc6f31384be9..c0fa3df1f80c551908668fee1ecf68cadc1cc76d 100644 GIT binary patch delta 32 ocmZqBZP1;tm(g(IzNXC+7&mZEVhWh-z%8-4gZl^9p705=CZUL3&94FLS45H$b* From e95916bf35a2a2d37135f8659abb8ec03339677b Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:15:00 +0000 Subject: [PATCH 119/154] more casting to array --- autoarray/operators/transformer.py | 4 ++-- .../arrays/files/array/output_test/array.fits | Bin 5760 -> 5760 bytes 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/operators/transformer.py b/autoarray/operators/transformer.py index 13f089737..87ec55d08 100644 --- a/autoarray/operators/transformer.py +++ b/autoarray/operators/transformer.py @@ -104,7 +104,7 @@ def visibilities_from(self, image): else: visibilities = transformer_util.visibilities_jit( image_1d=np.array(image.binned), - grid_radians=self.grid, + grid_radians=np.array(self.grid), uv_wavelengths=self.uv_wavelengths, ) @@ -137,7 +137,7 @@ def transform_mapping_matrix(self, mapping_matrix): else: return transformer_util.transformed_mapping_matrix_jit( mapping_matrix=mapping_matrix, - grid_radians=self.grid, + grid_radians=np.array(self.grid), uv_wavelengths=self.uv_wavelengths, ) diff --git a/test_autoarray/structures/arrays/files/array/output_test/array.fits b/test_autoarray/structures/arrays/files/array/output_test/array.fits index c0fa3df1f80c551908668fee1ecf68cadc1cc76d..97c9a0707f9c5c472bf5ac3a1811cc6f31384be9 100644 GIT binary patch delta 55 xcmZqBZP1;tm(ghAzNU!_SXqn|6l^Cpifra(e9Sr7fm>p705=CZUL3&94FLS45H$b* delta 32 ocmZqBZP1;tm(g(IzNXC+7&mZEVhWh-z%8-4gZl^9 Date: Mon, 18 Dec 2023 10:18:13 +0000 Subject: [PATCH 120/154] more casting to array --- test_autoarray/operators/test_convolver.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test_autoarray/operators/test_convolver.py b/test_autoarray/operators/test_convolver.py index ac611a001..352882d0f 100644 --- a/test_autoarray/operators/test_convolver.py +++ b/test_autoarray/operators/test_convolver.py @@ -160,9 +160,9 @@ def test__frame_extraction__more_complicated_frames(simple_mask_2d_7x7): frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(2, 2), - mask=simple_mask_2d_7x7, + mask=np.array(simple_mask_2d_7x7), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert ( @@ -200,9 +200,9 @@ def test__frame_extraction__more_complicated_frames(simple_mask_2d_7x7): frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(3, 2), - mask=simple_mask_2d_7x7, + mask=np.array(simple_mask_2d_7x7), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert ( @@ -240,9 +240,9 @@ def test__frame_extraction__more_complicated_frames(simple_mask_2d_7x7): frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(3, 3), - mask=simple_mask_2d_7x7, + mask=np.array(simple_mask_2d_7x7), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert ( From 563f89c792967dd46077c4e7787102dc49394d3a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:20:11 +0000 Subject: [PATCH 121/154] cast to array --- autoarray/operators/convolver.py | 4 ++-- test_autoarray/operators/test_convolver.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/autoarray/operators/convolver.py b/autoarray/operators/convolver.py index 59412e132..5c45d8907 100644 --- a/autoarray/operators/convolver.py +++ b/autoarray/operators/convolver.py @@ -328,11 +328,11 @@ def convolve_image(self, image, blurring_image): ) convolved_image = self.convolve_jit( - image_1d_array=image.binned.slim, + image_1d_array=np.array(image.binned.slim), image_frame_1d_indexes=self.image_frame_1d_indexes, image_frame_1d_kernels=self.image_frame_1d_kernels, image_frame_1d_lengths=self.image_frame_1d_lengths, - blurring_1d_array=blurring_image.binned.slim, + blurring_1d_array=np.array(blurring_image.binned.slim), blurring_frame_1d_indexes=self.blurring_frame_1d_indexes, blurring_frame_1d_kernels=self.blurring_frame_1d_kernels, blurring_frame_1d_lengths=self.blurring_frame_1d_lengths, diff --git a/test_autoarray/operators/test_convolver.py b/test_autoarray/operators/test_convolver.py index 352882d0f..f787811c8 100644 --- a/test_autoarray/operators/test_convolver.py +++ b/test_autoarray/operators/test_convolver.py @@ -105,9 +105,9 @@ def test__frame_extraction__frame_and_kernel_frame_at_coords(simple_mask_5x5): frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(2, 2), - mask=simple_mask_5x5, + mask=np.array(simple_mask_5x5), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert (frame == np.array([i for i in range(9)])).all() @@ -120,18 +120,18 @@ def test__frame_extraction__frame_and_kernel_frame_at_coords(simple_mask_5x5): frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(1, 1), - mask=simple_mask_5x5, + mask=np.array(simple_mask_5x5), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert (frame == corner_frame).all() frame, kernel_frame = convolver.frame_at_coordinates_jit( coordinates=(1, 1), - mask=simple_mask_5x5, + mask=np.array(simple_mask_5x5), mask_index_array=convolver.mask_index_array, - kernel_2d=convolver.kernel.native, + kernel_2d=np.array(convolver.kernel.native), ) assert (kernel_frame == np.array([5.0, 6.0, 8.0, 9.0, -1, -1, -1, -1, -1])).all() From 8e8abc0d2327178ed25801a3f14a5d1eccfc1786 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:22:09 +0000 Subject: [PATCH 122/154] cast to array --- autoarray/structures/grids/sparse_2d.py | 8 ++++---- test_autoarray/structures/grids/test_sparse.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/autoarray/structures/grids/sparse_2d.py b/autoarray/structures/grids/sparse_2d.py index 372819c29..8dabcf50a 100644 --- a/autoarray/structures/grids/sparse_2d.py +++ b/autoarray/structures/grids/sparse_2d.py @@ -117,24 +117,24 @@ def from_grid_and_unmasked_2d_grid_shape( ) total_sparse_pixels = mask_2d_util.total_sparse_pixels_2d_from( - mask_2d=grid.mask, + mask_2d=np.array(grid.mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) sparse_for_unmasked_sparse = sparse_2d_util.sparse_for_unmasked_sparse_from( - mask=grid.mask, + mask=np.array(grid.mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=total_sparse_pixels, ).astype("int") unmasked_sparse_for_sparse = sparse_2d_util.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_sparse_pixels, - mask=grid.mask, + mask=np.array(grid.mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ).astype("int") regular_to_unmasked_sparse = geometry_util.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid, + grid_scaled_2d_slim=np.array(grid), shape_native=unmasked_sparse_shape, pixel_scales=pixel_scales, origin=origin, diff --git a/test_autoarray/structures/grids/test_sparse.py b/test_autoarray/structures/grids/test_sparse.py index b0ec58959..cf6d2d6c9 100644 --- a/test_autoarray/structures/grids/test_sparse.py +++ b/test_autoarray/structures/grids/test_sparse.py @@ -32,13 +32,13 @@ def test__from_grid_and_unmasked_2d_grid_shap(): ) total_sparse_pixels = aa.util.mask_2d.total_sparse_pixels_2d_from( - mask_2d=mask, + mask_2d=np.array(mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) regular_to_unmasked_sparse_2d_util = ( aa.util.geometry.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid, + grid_scaled_2d_slim=np.array(grid), shape_native=(10, 10), pixel_scales=(0.15, 0.15), origin=(0.0, 0.0), @@ -47,12 +47,12 @@ def test__from_grid_and_unmasked_2d_grid_shap(): unmasked_sparse_for_sparse_2d_util = aa.util.sparse.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_sparse_pixels, - mask=mask, + mask=np.array(mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ).astype("int") sparse_for_unmasked_sparse_2d_util = aa.util.sparse.sparse_for_unmasked_sparse_from( - mask=mask, + mask=np.array(mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=total_sparse_pixels, ).astype("int") From ddacab7fc7e0c86fdb9c03ba3cdeccb3e412c19f Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:35:23 +0000 Subject: [PATCH 123/154] remove sparse_index_for_slim_index from Grid2DSparse --- autoarray/structures/grids/sparse_2d.py | 26 +------------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/autoarray/structures/grids/sparse_2d.py b/autoarray/structures/grids/sparse_2d.py index 8dabcf50a..547392ddb 100644 --- a/autoarray/structures/grids/sparse_2d.py +++ b/autoarray/structures/grids/sparse_2d.py @@ -34,7 +34,7 @@ def trimmed_after_convolution_from(self, kernel_shape) -> "Structure": def native(self) -> Structure: raise NotImplemented() - def __init__(self, values: np.ndarray, sparse_index_for_slim_index: np.ndarray): + def __init__(self, values: np.ndarray): """ A sparse grid of coordinates, where each entry corresponds to the (y,x) coordinates at the centre of a pixel on the sparse grid. To setup the sparse-grid, it is laid over a grid of unmasked pixels, such @@ -62,8 +62,6 @@ def __init__(self, values: np.ndarray, sparse_index_for_slim_index: np.ndarray): The (y,x) grid of sparse coordinates. """ - self.sparse_index_for_slim_index = sparse_index_for_slim_index - super().__init__(values) def __array_finalize__(self, obj): @@ -121,32 +119,12 @@ def from_grid_and_unmasked_2d_grid_shape( unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) - sparse_for_unmasked_sparse = sparse_2d_util.sparse_for_unmasked_sparse_from( - mask=np.array(grid.mask), - unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, - total_sparse_pixels=total_sparse_pixels, - ).astype("int") - unmasked_sparse_for_sparse = sparse_2d_util.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_sparse_pixels, mask=np.array(grid.mask), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ).astype("int") - regular_to_unmasked_sparse = geometry_util.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=np.array(grid), - shape_native=unmasked_sparse_shape, - pixel_scales=pixel_scales, - origin=origin, - ).astype("int") - - sparse_index_for_slim_index = ( - sparse_2d_util.sparse_slim_index_for_mask_slim_index_from( - regular_to_unmasked_sparse=regular_to_unmasked_sparse, - sparse_for_unmasked_sparse=sparse_for_unmasked_sparse, - ).astype("int") - ) - sparse_grid = sparse_2d_util.sparse_grid_via_unmasked_from( unmasked_sparse_grid=unmasked_sparse_grid_1d, unmasked_sparse_for_sparse=unmasked_sparse_for_sparse, @@ -154,7 +132,6 @@ def from_grid_and_unmasked_2d_grid_shape( return Grid2DSparse( values=sparse_grid, - sparse_index_for_slim_index=sparse_index_for_slim_index, ) @classmethod @@ -217,7 +194,6 @@ def from_total_pixels_grid_and_weight_map( return Grid2DSparse( values=kmeans.cluster_centers_, - sparse_index_for_slim_index=kmeans.labels_, ) @classmethod From 4b853745ce0296493a28a981c415425cce5da22b Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:39:54 +0000 Subject: [PATCH 124/154] cast to array --- .../imaging/test_inversion_imaging_util.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/test_autoarray/inversion/inversion/imaging/test_inversion_imaging_util.py b/test_autoarray/inversion/inversion/imaging/test_inversion_imaging_util.py index df35319be..13f1def31 100644 --- a/test_autoarray/inversion/inversion/imaging/test_inversion_imaging_util.py +++ b/test_autoarray/inversion/inversion/imaging/test_inversion_imaging_util.py @@ -207,15 +207,15 @@ def test__data_vector_via_w_tilde_data_two_methods_agree(): data_vector = ( aa.util.inversion_imaging.data_vector_via_blurred_mapping_matrix_from( blurred_mapping_matrix=blurred_mapping_matrix, - image=image, - noise_map=noise_map, + image=np.array(image), + noise_map=np.array(noise_map), ) ) w_tilde_data = aa.util.inversion_imaging.w_tilde_data_imaging_from( - image_native=image.native, - noise_map_native=noise_map.native, - kernel_native=kernel.native, + image_native=np.array(image.native), + noise_map_native=np.array(noise_map.native), + kernel_native=np.array(kernel.native), native_index_for_slim_index=mask.derive_indexes.native_for_slim, ) @@ -270,8 +270,8 @@ def test__curvature_matrix_via_w_tilde_two_methods_agree(): mapping_matrix = mapper.mapping_matrix w_tilde = aa.util.inversion_imaging.w_tilde_curvature_imaging_from( - noise_map_native=noise_map.native, - kernel_native=kernel.native, + noise_map_native=np.array(noise_map.native), + kernel_native=np.array(kernel.native), native_index_for_slim_index=mask.derive_indexes.native_for_slim, ) @@ -284,7 +284,8 @@ def test__curvature_matrix_via_w_tilde_two_methods_agree(): ) curvature_matrix = aa.util.inversion.curvature_matrix_via_mapping_matrix_from( - mapping_matrix=blurred_mapping_matrix, noise_map=noise_map + mapping_matrix=blurred_mapping_matrix, + noise_map=np.array(noise_map), ) assert curvature_matrix_via_w_tilde == pytest.approx(curvature_matrix, 1.0e-4) @@ -321,8 +322,8 @@ def test__curvature_matrix_via_w_tilde_preload_two_methods_agree(): w_tilde_indexes, w_tilde_lengths, ) = aa.util.inversion_imaging.w_tilde_curvature_preload_imaging_from( - noise_map_native=noise_map.native, - kernel_native=kernel.native, + noise_map_native=np.array(noise_map.native), + kernel_native=np.array(kernel.native), native_index_for_slim_index=mask.derive_indexes.native_for_slim, ) @@ -354,7 +355,8 @@ def test__curvature_matrix_via_w_tilde_preload_two_methods_agree(): ) curvature_matrix = aa.util.inversion.curvature_matrix_via_mapping_matrix_from( - mapping_matrix=blurred_mapping_matrix, noise_map=noise_map + mapping_matrix=blurred_mapping_matrix, + noise_map=np.array(noise_map), ) assert curvature_matrix_via_w_tilde == pytest.approx(curvature_matrix, 1.0e-4) From cc302be0297d3403b1a57db10a865aa871ecb7eb Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:41:36 +0000 Subject: [PATCH 125/154] cast to array --- .../structures/grids/test_iterate_2d.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test_autoarray/structures/grids/test_iterate_2d.py b/test_autoarray/structures/grids/test_iterate_2d.py index 8d0cdc716..5ed2ea1a6 100644 --- a/test_autoarray/structures/grids/test_iterate_2d.py +++ b/test_autoarray/structures/grids/test_iterate_2d.py @@ -46,7 +46,9 @@ def test__from_mask(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) grid_via_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( - mask_2d=mask, sub_size=1, pixel_scales=(2.0, 2.0) + mask_2d=np.array(mask), + sub_size=1, + pixel_scales=(2.0, 2.0), ) grid = aa.Grid2DIterate.from_mask( @@ -136,7 +138,8 @@ def test__blurring_mask_2d_from(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0), sub_size=2) blurring_mask_util = aa.util.mask_2d.blurring_mask_2d_from( - mask_2d=mask, kernel_shape_native=(3, 5) + mask_2d=np.array(mask), + kernel_shape_native=(3, 5), ) blurring_grid_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( @@ -171,14 +174,18 @@ def test__blurring_grid_from(): mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0)) blurring_mask_util = aa.util.mask_2d.blurring_mask_2d_from( - mask_2d=mask, kernel_shape_native=(3, 5) + mask_2d=np.array(mask), + kernel_shape_native=(3, 5), ) blurring_grid_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( mask_2d=blurring_mask_util, pixel_scales=(2.0, 2.0), sub_size=1 ) - mask = aa.Mask2D(mask=mask, pixel_scales=(2.0, 2.0)) + mask = aa.Mask2D( + mask=np.array(mask), + pixel_scales=(2.0, 2.0), + ) blurring_grid = aa.Grid2DIterate.blurring_grid_from( mask=mask, kernel_shape_native=(3, 5) From e8c97f33e6e2dc0515d0cf99baeef9be592f25ca Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:42:08 +0000 Subject: [PATCH 126/154] cast to array --- autoarray/operators/transformer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/operators/transformer.py b/autoarray/operators/transformer.py index 87ec55d08..6373a8913 100644 --- a/autoarray/operators/transformer.py +++ b/autoarray/operators/transformer.py @@ -113,7 +113,7 @@ def visibilities_from(self, image): def image_from(self, visibilities, use_adjoint_scaling: bool = False): image_slim = transformer_util.image_via_jit_from( n_pixels=self.grid.shape[0], - grid_radians=self.grid, + grid_radians=np.array(self.grid), uv_wavelengths=self.uv_wavelengths, visibilities=visibilities.in_array, ) From 4ed9b8cf26cea533379df214479ed8e7e5b859b7 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:44:58 +0000 Subject: [PATCH 127/154] cast to array --- autoarray/geometry/geometry_2d.py | 6 +++--- autoarray/mask/derive/grid_2d.py | 2 +- test_autoarray/geometry/test_geometry_2d.py | 10 ++++++---- .../inversion/pixelization/mappers/test_abstract.py | 5 +++-- test_autoarray/mask/derive/test_grid_2d.py | 5 ++++- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/autoarray/geometry/geometry_2d.py b/autoarray/geometry/geometry_2d.py index f622f9f39..f2902afcf 100644 --- a/autoarray/geometry/geometry_2d.py +++ b/autoarray/geometry/geometry_2d.py @@ -234,7 +234,7 @@ def grid_pixel_centres_2d_from(self, grid_scaled_2d: Grid2D) -> Grid2D: from autoarray.structures.grids.uniform_2d import Grid2D grid_pixel_centres_1d = geometry_util.grid_pixel_centres_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=self.shape_native, pixel_scales=self.pixel_scales, origin=self.origin, @@ -267,7 +267,7 @@ def grid_pixel_indexes_2d_from(self, grid_scaled_2d: Grid2D) -> Array2D: from autoarray.structures.arrays.uniform_2d import Array2D grid_pixel_indexes_2d = geometry_util.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=self.shape_native, pixel_scales=self.pixel_scales, origin=self.origin, @@ -293,7 +293,7 @@ def grid_scaled_2d_from(self, grid_pixels_2d: Grid2D) -> Grid2D: from autoarray.structures.grids.uniform_2d import Grid2D grid_scaled_1d = geometry_util.grid_scaled_2d_slim_from( - grid_pixels_2d_slim=grid_pixels_2d, + grid_pixels_2d_slim=np.array(grid_pixels_2d), shape_native=self.shape_native, pixel_scales=self.pixel_scales, origin=self.origin, diff --git a/autoarray/mask/derive/grid_2d.py b/autoarray/mask/derive/grid_2d.py index a6ee331a1..ab86a61ea 100644 --- a/autoarray/mask/derive/grid_2d.py +++ b/autoarray/mask/derive/grid_2d.py @@ -172,7 +172,7 @@ def unmasked(self) -> Grid2D: from autoarray.structures.grids.uniform_2d import Grid2D sub_grid_1d = grid_2d_util.grid_2d_slim_via_mask_from( - mask_2d=self.mask, + mask_2d=np.array(self.mask), pixel_scales=self.mask.pixel_scales, sub_size=self.mask.sub_size, origin=self.mask.origin, diff --git a/test_autoarray/geometry/test_geometry_2d.py b/test_autoarray/geometry/test_geometry_2d.py index cd08e2f4e..4f6b711a3 100644 --- a/test_autoarray/geometry/test_geometry_2d.py +++ b/test_autoarray/geometry/test_geometry_2d.py @@ -1,3 +1,5 @@ +import numpy as np + import autoarray as aa @@ -64,7 +66,7 @@ def test__grid_pixels_2d_slim_from(): ) grid_pixels_util = aa.util.geometry.grid_pixels_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=(2, 2), pixel_scales=geometry.pixel_scales, ) @@ -83,7 +85,7 @@ def test__grid_pixel_centres_2d_from(): ) grid_pixels_util = aa.util.geometry.grid_pixel_centres_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=(2, 2), pixel_scales=(7.0, 2.0), ) @@ -102,7 +104,7 @@ def test__grid_pixel_indexes_2d_from(): ) grid_pixels_util = aa.util.geometry.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid_scaled_2d, + grid_scaled_2d_slim=np.array(grid_scaled_2d), shape_native=(2, 2), pixel_scales=(2.0, 4.0), ) @@ -121,7 +123,7 @@ def test__grid_scaled_2d_from(): ) grid_pixels_util = aa.util.geometry.grid_scaled_2d_slim_from( - grid_pixels_2d_slim=grid_pixels, + grid_pixels_2d_slim=np.array(grid_pixels), shape_native=(2, 2), pixel_scales=(2.0, 2.0), ) diff --git a/test_autoarray/inversion/pixelization/mappers/test_abstract.py b/test_autoarray/inversion/pixelization/mappers/test_abstract.py index 4f2151e70..7633504b9 100644 --- a/test_autoarray/inversion/pixelization/mappers/test_abstract.py +++ b/test_autoarray/inversion/pixelization/mappers/test_abstract.py @@ -127,7 +127,7 @@ def test__adaptive_pixel_signals_from___matches_util(grid_2d_7x7, image_7x7): pix_indexes_for_sub_slim_index=pix_sub_weights.mappings, pix_size_for_sub_slim_index=pix_sub_weights.sizes, slim_index_for_sub_slim_index=grid_2d_7x7.mask.derive_indexes.slim_for_sub_slim, - adapt_data=image_7x7, + adapt_data=np.array(image_7x7), ) assert (pixel_signals == pixel_signals_util).all() @@ -185,7 +185,8 @@ def test__mapped_to_source_from(grid_2d_7x7): ) mapped_to_source_util = aa.util.mapper.mapped_to_source_via_mapping_matrix_from( - mapping_matrix=mapper.mapping_matrix, array_slim=array_slim + mapping_matrix=mapper.mapping_matrix, + array_slim=np.array(array_slim), ) mapped_to_source_mapper = mapper.mapped_to_source_from(array=array_slim) diff --git a/test_autoarray/mask/derive/test_grid_2d.py b/test_autoarray/mask/derive/test_grid_2d.py index f30f8e7e6..e98c99121 100644 --- a/test_autoarray/mask/derive/test_grid_2d.py +++ b/test_autoarray/mask/derive/test_grid_2d.py @@ -237,7 +237,10 @@ def test__masked_grid(): derive_grid = aa.DeriveGrid2D(mask=mask) masked_grid_util = aa.util.grid_2d.grid_2d_slim_via_mask_from( - mask_2d=mask, pixel_scales=(1.0, 1.0), sub_size=5, origin=(3.0, -2.0) + mask_2d=np.array(mask), + pixel_scales=(1.0, 1.0), + sub_size=5, + origin=(3.0, -2.0), ) assert (derive_grid.unmasked == masked_grid_util).all() From 07db622b135a7fdeedfe2d3808c384914781634f Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 10:58:37 +0000 Subject: [PATCH 128/154] fixed remaining tests --- .../pixelization/mappers/abstract.py | 3 ++- .../pixelization/mappers/delaunay.py | 2 +- autoarray/structures/grids/irregular_2d.py | 2 +- autoarray/structures/grids/uniform_2d.py | 3 ++- .../pixelization/mappers/test_delaunay.py | 2 +- .../pixelization/mappers/test_rectangular.py | 4 ++-- .../pixelization/mappers/test_voronoi.py | 4 ++-- test_autoarray/mask/derive/test_mask_2d.py | 9 +++---- .../structures/grids/test_irregular_2d.py | 4 +++- .../structures/grids/test_sparse_util.py | 24 +++++++++---------- 10 files changed, 31 insertions(+), 26 deletions(-) diff --git a/autoarray/inversion/pixelization/mappers/abstract.py b/autoarray/inversion/pixelization/mappers/abstract.py index b66bf75b0..32e4cc313 100644 --- a/autoarray/inversion/pixelization/mappers/abstract.py +++ b/autoarray/inversion/pixelization/mappers/abstract.py @@ -361,7 +361,8 @@ def mapped_to_source_from(self, array: Array2D) -> np.ndarray: source domain in order to compute their average values. """ return mapper_util.mapped_to_source_via_mapping_matrix_from( - mapping_matrix=self.mapping_matrix, array_slim=array.binned.slim + mapping_matrix=self.mapping_matrix, + array_slim=np.array(array.binned.slim), ) def extent_from( diff --git a/autoarray/inversion/pixelization/mappers/delaunay.py b/autoarray/inversion/pixelization/mappers/delaunay.py index 51715a6ac..f1e0fe09b 100644 --- a/autoarray/inversion/pixelization/mappers/delaunay.py +++ b/autoarray/inversion/pixelization/mappers/delaunay.py @@ -181,7 +181,7 @@ def pix_sub_weights_split_cross(self) -> PixSubWeights: splitted_weights = mapper_util.pixel_weights_delaunay_from( source_plane_data_grid=self.source_plane_mesh_grid.split_cross, - source_plane_mesh_grid=self.source_plane_mesh_grid, + source_plane_mesh_grid=np.array(self.source_plane_mesh_grid), slim_index_for_sub_slim_index=self.source_plane_mesh_grid.split_cross, pix_indexes_for_sub_slim_index=splitted_mappings.astype("int"), ) diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 3060c89d7..69cadc109 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -501,7 +501,7 @@ def from_grid_sparse_uniform_upscale( pixel_scales = geometry_util.convert_pixel_scales_2d(pixel_scales=pixel_scales) grid_upscaled_1d = grid_2d_util.grid_2d_slim_upscaled_from( - grid_slim=grid_sparse_uniform, + grid_slim=np.array(grid_sparse_uniform), upscale_factor=upscale_factor, pixel_scales=pixel_scales, ) diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 0fa7510bf..5ce2cd45b 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -1242,6 +1242,7 @@ def relocated_mesh_grid_from(self, mesh_grid: Grid2DSparse) -> Grid2DSparse: return Grid2DSparse( values=grid_2d_util.relocated_grid_via_jit_from( - grid=mesh_grid, border_grid=self.sub_border_grid + grid=np.array(mesh_grid), + border_grid=self.sub_border_grid, ), ) diff --git a/test_autoarray/inversion/pixelization/mappers/test_delaunay.py b/test_autoarray/inversion/pixelization/mappers/test_delaunay.py index 58a48b5ba..16348fe5e 100644 --- a/test_autoarray/inversion/pixelization/mappers/test_delaunay.py +++ b/test_autoarray/inversion/pixelization/mappers/test_delaunay.py @@ -26,7 +26,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util(grid_2d_7x7): pix_indexes_for_sub_slim_index_util, sizes, ) = aa.util.mapper.pix_indexes_for_sub_slim_index_delaunay_from( - source_plane_data_grid=mapper.source_plane_data_grid, + source_plane_data_grid=np.array(mapper.source_plane_data_grid), simplex_index_for_sub_slim_index=simplex_index_for_sub_slim_index, pix_indexes_for_simplex_index=pix_indexes_for_simplex_index, delaunay_points=mapper.delaunay.points, diff --git a/test_autoarray/inversion/pixelization/mappers/test_rectangular.py b/test_autoarray/inversion/pixelization/mappers/test_rectangular.py index 8ec6a3070..69d5cf6d5 100644 --- a/test_autoarray/inversion/pixelization/mappers/test_rectangular.py +++ b/test_autoarray/inversion/pixelization/mappers/test_rectangular.py @@ -31,7 +31,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util(): pix_indexes_for_sub_slim_index_util = np.array( [ aa.util.geometry.grid_pixel_indexes_2d_slim_from( - grid_scaled_2d_slim=grid, + grid_scaled_2d_slim=np.array(grid), shape_native=mesh_grid.shape_native, pixel_scales=mesh_grid.pixel_scales, origin=mesh_grid.origin, @@ -64,7 +64,7 @@ def test__pixel_signals_from__matches_util(grid_2d_7x7, image_7x7): pix_size_for_sub_slim_index=mapper.pix_sizes_for_sub_slim_index, pixel_weights=mapper.pix_weights_for_sub_slim_index, slim_index_for_sub_slim_index=grid_2d_7x7.mask.derive_indexes.slim_for_sub_slim, - adapt_data=image_7x7, + adapt_data=np.array(image_7x7), ) assert (pixel_signals == pixel_signals_util).all() diff --git a/test_autoarray/inversion/pixelization/mappers/test_voronoi.py b/test_autoarray/inversion/pixelization/mappers/test_voronoi.py index 29888ccb5..6ac3523c3 100644 --- a/test_autoarray/inversion/pixelization/mappers/test_voronoi.py +++ b/test_autoarray/inversion/pixelization/mappers/test_voronoi.py @@ -24,9 +24,9 @@ def test__pix_indexes_for_sub_slim_index__matches_util(grid_2d_7x7): pix_indexes_for_sub_slim_index_util = np.array( [ aa.util.mapper.pix_indexes_for_sub_slim_index_voronoi_from( - grid=grid_2d_7x7, + grid=np.array(grid_2d_7x7), slim_index_for_sub_slim_index=grid_2d_7x7.mask.derive_indexes.slim_for_sub_slim, - mesh_grid=source_plane_mesh_grid, + mesh_grid=np.array(source_plane_mesh_grid), neighbors=source_plane_mesh_grid.neighbors, neighbors_sizes=source_plane_mesh_grid.neighbors.sizes, ).astype("int") diff --git a/test_autoarray/mask/derive/test_mask_2d.py b/test_autoarray/mask/derive/test_mask_2d.py index a36256baf..ff3c517d0 100644 --- a/test_autoarray/mask/derive/test_mask_2d.py +++ b/test_autoarray/mask/derive/test_mask_2d.py @@ -97,7 +97,8 @@ def test__unmasked_mask(derive_mask_2d_9x9): def test__blurring_mask_from(derive_mask_2d_9x9): blurring_mask_via_util = aa.util.mask_2d.blurring_mask_2d_from( - mask_2d=derive_mask_2d_9x9.mask, kernel_shape_native=(3, 3) + mask_2d=np.array(derive_mask_2d_9x9.mask), + kernel_shape_native=(3, 3), ) blurring_mask = derive_mask_2d_9x9.blurring_from(kernel_shape_native=(3, 3)) @@ -130,9 +131,9 @@ def test__edge_buffed_mask(): derive_mask_2d = aa.DeriveMask2D(mask=mask) - edge_buffed_mask_manual = aa.util.mask_2d.buffed_mask_2d_from(mask_2d=mask).astype( - "bool" - ) + edge_buffed_mask_manual = aa.util.mask_2d.buffed_mask_2d_from( + mask_2d=np.array(mask), + ).astype("bool") assert (derive_mask_2d.edge_buffed == edge_buffed_mask_manual).all() diff --git a/test_autoarray/structures/grids/test_irregular_2d.py b/test_autoarray/structures/grids/test_irregular_2d.py index 6b60db9e0..bdc3d3c1d 100644 --- a/test_autoarray/structures/grids/test_irregular_2d.py +++ b/test_autoarray/structures/grids/test_irregular_2d.py @@ -249,7 +249,9 @@ def test__uniform__from_grid_sparse_uniform_upscale(): ) grid_upscale_util = aa.util.grid_2d.grid_2d_slim_upscaled_from( - grid_slim=grid_sparse_uniform, upscale_factor=4, pixel_scales=(2.0, 2.0) + grid_slim=np.array(grid_sparse_uniform), + upscale_factor=4, + pixel_scales=(2.0, 2.0), ) assert (grid_upscale == grid_upscale_util).all() diff --git a/test_autoarray/structures/grids/test_sparse_util.py b/test_autoarray/structures/grids/test_sparse_util.py index 8b9458f96..2f6674dcf 100644 --- a/test_autoarray/structures/grids/test_sparse_util.py +++ b/test_autoarray/structures/grids/test_sparse_util.py @@ -18,13 +18,13 @@ def test__unmasked_sparse_for_sparse_from(): ) total_masked_pixels = aa.util.mask_2d.total_sparse_pixels_2d_from( - mask_2d=mask_2d, + mask_2d=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) unmasked_sparse_for_sparse = aa.util.sparse.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_masked_pixels, - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) @@ -35,13 +35,13 @@ def test__unmasked_sparse_for_sparse_from(): ) total_masked_pixels = aa.util.mask_2d.total_sparse_pixels_2d_from( - mask_2d=mask_2d, + mask_2d=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) unmasked_sparse_for_sparse = aa.util.sparse.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_masked_pixels, - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) @@ -60,13 +60,13 @@ def test__unmasked_sparse_for_sparse_from(): ) total_masked_pixels = aa.util.mask_2d.total_sparse_pixels_2d_from( - mask_2d=mask_2d, + mask_2d=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) unmasked_sparse_for_sparse = aa.util.sparse.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_masked_pixels, - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) @@ -89,13 +89,13 @@ def test__unmasked_sparse_for_sparse_from(): ) total_masked_pixels = aa.util.mask_2d.total_sparse_pixels_2d_from( - mask_2d=mask_2d, + mask_2d=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) unmasked_sparse_for_sparse = aa.util.sparse.unmasked_sparse_for_sparse_from( total_sparse_pixels=total_masked_pixels, - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, ) @@ -116,7 +116,7 @@ def test__sparse_for_unmasked_sparse_from(): ) sparse_for_unmasked_sparse = aa.util.sparse.sparse_for_unmasked_sparse_from( - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=9, ) @@ -128,7 +128,7 @@ def test__sparse_for_unmasked_sparse_from(): ) sparse_for_unmasked_sparse = aa.util.sparse.sparse_for_unmasked_sparse_from( - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=9, ) @@ -148,7 +148,7 @@ def test__sparse_for_unmasked_sparse_from(): ) sparse_for_unmasked_sparse = aa.util.sparse.sparse_for_unmasked_sparse_from( - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=4, ) @@ -172,7 +172,7 @@ def test__sparse_for_unmasked_sparse_from(): ) sparse_for_unmasked_sparse = aa.util.sparse.sparse_for_unmasked_sparse_from( - mask=mask_2d, + mask=np.array(mask_2d), unmasked_sparse_grid_pixel_centres=unmasked_sparse_grid_pixel_centres, total_sparse_pixels=5, ) From cc989b41b3fd3f542ddadc8e4d87c393bb423305 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 12:21:38 +0000 Subject: [PATCH 129/154] ensure tests pass without jax being installed --- autoarray/abstract_ndarray.py | 4 +--- autoarray/mask/derive/indexes_2d.py | 2 +- autoarray/numpy_wrapper.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 8293de846..2d9c0d7c0 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -5,10 +5,8 @@ from abc import ABC from abc import abstractmethod import numpy as np -from jax._src.tree_util import register_pytree_node -from autoarray.numpy_wrapper import numpy as npw -from jax import Array +from autoarray.numpy_wrapper import numpy as npw, register_pytree_node, Array from typing import TYPE_CHECKING diff --git a/autoarray/mask/derive/indexes_2d.py b/autoarray/mask/derive/indexes_2d.py index 2afab5e12..9ea96e3ca 100644 --- a/autoarray/mask/derive/indexes_2d.py +++ b/autoarray/mask/derive/indexes_2d.py @@ -1,7 +1,7 @@ from __future__ import annotations import logging import numpy as np -from jax._src.tree_util import register_pytree_node_class +from autoarray.numpy_wrapper import register_pytree_node_class from typing import TYPE_CHECKING if TYPE_CHECKING: diff --git a/autoarray/numpy_wrapper.py b/autoarray/numpy_wrapper.py index 9b6adb439..87cb8c060 100644 --- a/autoarray/numpy_wrapper.py +++ b/autoarray/numpy_wrapper.py @@ -67,3 +67,18 @@ def __getattr__(self, item): ) else: numpy = Numpy(np) + +try: + from jax._src.tree_util import register_pytree_node + from jax._src.tree_util import register_pytree_node_class + + from jax import Array +except ImportError: + + def register_pytree_node_class(cls): + return cls + + def register_pytree_node(*_, **__): + pass + + Array = np.ndarray From a1669cfbc3854889eaafc82ee3d74dfa863d57b5 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 13:44:24 +0000 Subject: [PATCH 130/154] Grid2DTransformedNumpy as AbstractNDArray to fix autogalaxy --- autoarray/structures/grids/transformed_2d.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/grids/transformed_2d.py b/autoarray/structures/grids/transformed_2d.py index 92c46905c..f0106c82c 100644 --- a/autoarray/structures/grids/transformed_2d.py +++ b/autoarray/structures/grids/transformed_2d.py @@ -1,5 +1,7 @@ import numpy as np +from autoarray.abstract_ndarray import AbstractNDArray +from autoarray.structures.abstract_structure import Structure from autoarray.structures.grids.uniform_2d import Grid2D @@ -7,6 +9,10 @@ class Grid2DTransformed(Grid2D): pass -class Grid2DTransformedNumpy(np.ndarray): - def __new__(cls, values, *args, **kwargs): - return values.view(cls) +class Grid2DTransformedNumpy(AbstractNDArray): + @property + def native(self) -> Structure: + return self.array + + def __init__(self, values): + super().__init__(array=values) From 7cb6cc1346b1e694b28742856bffc9493d028c79 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 14:09:00 +0000 Subject: [PATCH 131/154] a slice of an array of a given type is still of that same type --- autoarray/abstract_ndarray.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 2d9c0d7c0..42238dcaf 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -239,7 +239,10 @@ def reshape(self, *args, **kwargs): return self._array.reshape(*args, **kwargs) def __getitem__(self, item): - return self._array[item] + result = self._array[item] + if isinstance(item, slice): + return self.with_new_array(result) + return result def __setitem__(self, key, value): if isinstance(key, (np.ndarray, AbstractNDArray, Array)): From 912874ecef0d65bbf14defe2e4e164e46dd7192d Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 14:22:47 +0000 Subject: [PATCH 132/154] by default get attributes from the underlying array --- autoarray/abstract_ndarray.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 42238dcaf..c0496e81c 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -238,6 +238,16 @@ def min(self, *args, **kwargs): def reshape(self, *args, **kwargs): return self._array.reshape(*args, **kwargs) + def __getattr__(self, item): + if item != "__setstate__": + try: + return getattr(self._array, item) + except AttributeError: + pass + raise AttributeError( + f"{self.__class__.__name__} does not have attribute {item}" + ) + def __getitem__(self, item): result = self._array[item] if isinstance(item, slice): From 881edc24dd4127f6871939a6f081b84bf08766e4 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 15:36:10 +0000 Subject: [PATCH 133/154] fix autogalaxy test by casting transformed grid back to grid --- autoarray/structures/structure_decorators.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoarray/structures/structure_decorators.py b/autoarray/structures/structure_decorators.py index e70d841d3..23358e7cd 100644 --- a/autoarray/structures/structure_decorators.py +++ b/autoarray/structures/structure_decorators.py @@ -577,7 +577,9 @@ def wrapper( grid_radial_scale = np.where( grid_radii < grid_radial_minimum, grid_radial_minimum / grid_radii, 1.0 ) - moved_grid = np.multiply(grid, grid_radial_scale[:, None]) + moved_grid = grid.with_new_array( + np.multiply(grid, grid_radial_scale[:, None]) + ) moved_grid[np.isnan(np.array(moved_grid))] = grid_radial_minimum From b2e18f00debea3289f20eb30581842234f228e71 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 15:40:50 +0000 Subject: [PATCH 134/154] more casting to array --- autoarray/operators/transformer.py | 4 +++- autoarray/structures/arrays/kernel_2d.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/autoarray/operators/transformer.py b/autoarray/operators/transformer.py index 6373a8913..f2652ddc1 100644 --- a/autoarray/operators/transformer.py +++ b/autoarray/operators/transformer.py @@ -307,7 +307,9 @@ def a_complex_from(a_real, a_imag): x2d = np.real(self.xx2x(self.k2xx(self.y2k(y)))) x = array_2d_util.array_2d_slim_complex_from( - array_2d_native=x2d[::-1, :], sub_size=1, mask=self.real_space_mask + array_2d_native=x2d[::-1, :], + sub_size=1, + mask=np.array(self.real_space_mask), ) x = x.real # NOTE: diff --git a/autoarray/structures/arrays/kernel_2d.py b/autoarray/structures/arrays/kernel_2d.py index 35b0e4807..2d217de25 100644 --- a/autoarray/structures/arrays/kernel_2d.py +++ b/autoarray/structures/arrays/kernel_2d.py @@ -581,7 +581,9 @@ def convolved_array_with_mask_from(self, array: Array2D, mask: Mask2D) -> Array2 convolved_array_2d = scipy.signal.convolve2d(array, self.native, mode="same") convolved_array_1d = array_2d_util.array_2d_slim_from( - mask_2d=mask, array_2d_native=convolved_array_2d, sub_size=1 + mask_2d=np.array(mask), + array_2d_native=np.array(convolved_array_2d), + sub_size=1, ) return Array2D(values=convolved_array_1d, mask=mask.derive_mask.sub_1) From dcbb09b8d13fb85fc77ca7cde612cba50a01e1ad Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 15:48:48 +0000 Subject: [PATCH 135/154] fix noise map function --- autoarray/dataset/preprocess.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/autoarray/dataset/preprocess.py b/autoarray/dataset/preprocess.py index 1e2e5d19d..046ab3ea8 100644 --- a/autoarray/dataset/preprocess.py +++ b/autoarray/dataset/preprocess.py @@ -148,7 +148,9 @@ def noise_map_via_data_eps_and_exposure_time_map_from(data_eps, exposure_time_ma exposure_time_map The exposure time at every data-point of the data. """ - return (data_eps * exposure_time_map) ** 0.5 / exposure_time_map + return data_eps.with_new_array( + np.abs(data_eps * exposure_time_map) ** 0.5 / exposure_time_map + ) def noise_map_via_weight_map_from(weight_map): @@ -172,7 +174,7 @@ def noise_map_via_weight_map_from(weight_map): The weight-value of each pixel which is converted to a variance. """ np.seterr(divide="ignore") - noise_map = 1.0 / weight_map ** 0.5 + noise_map = 1.0 / weight_map**0.5 noise_map[noise_map > 1.0e8] = 1.0e8 return noise_map From 3b4e467e2594a1a2d5d20ca4f75dbf62b67d5afd Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 18 Dec 2023 16:08:13 +0000 Subject: [PATCH 136/154] fix --- autoarray/structures/structure_decorators.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/structure_decorators.py b/autoarray/structures/structure_decorators.py index 23358e7cd..967a6ed07 100644 --- a/autoarray/structures/structure_decorators.py +++ b/autoarray/structures/structure_decorators.py @@ -577,9 +577,10 @@ def wrapper( grid_radial_scale = np.where( grid_radii < grid_radial_minimum, grid_radial_minimum / grid_radii, 1.0 ) - moved_grid = grid.with_new_array( - np.multiply(grid, grid_radial_scale[:, None]) - ) + moved_grid = np.multiply(grid, grid_radial_scale[:, None]) + + if hasattr(grid, "with_new_array"): + moved_grid = grid.with_new_array(moved_grid) moved_grid[np.isnan(np.array(moved_grid))] = grid_radial_minimum From 6b085df25ec3c01c722e6f6abdcb29152acdb485 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 11:00:24 +0000 Subject: [PATCH 137/154] fixes --- autoarray/mask/abstract_mask.py | 10 +++++++--- autoarray/structures/arrays/uniform_2d.py | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/autoarray/mask/abstract_mask.py b/autoarray/mask/abstract_mask.py index cc80b1313..c06a8cbb9 100644 --- a/autoarray/mask/abstract_mask.py +++ b/autoarray/mask/abstract_mask.py @@ -56,6 +56,10 @@ def __init__( self.pixel_scales = pixel_scales self.origin = origin + @property + def mask(self): + return self._array + def __array_finalize__(self, obj): if isinstance(obj, Mask): self.sub_size = obj.sub_size @@ -111,7 +115,7 @@ def sub_length(self) -> int: For example, a sub-size of 3x3 means every pixel has 9 sub-pixels. """ - return int(self.sub_size ** self.dimensions) + return int(self.sub_size**self.dimensions) @property def sub_fraction(self) -> float: @@ -153,7 +157,7 @@ def sub_pixels_in_mask(self) -> int: """ The total number of unmasked sub-pixels (values are `False`) in the mask. """ - return self.sub_size ** self.dimensions * self.pixels_in_mask + return self.sub_size**self.dimensions * self.pixels_in_mask @property def shape_slim(self) -> int: @@ -167,7 +171,7 @@ def sub_shape_slim(self) -> int: """ The 1D shape of the mask's sub-grid, which is equivalent to the total number of unmasked pixels in the mask. """ - return int(self.pixels_in_mask * self.sub_size ** self.dimensions) + return int(self.pixels_in_mask * self.sub_size**self.dimensions) def mask_new_sub_size_from(self, mask, sub_size=1) -> "Mask": """ diff --git a/autoarray/structures/arrays/uniform_2d.py b/autoarray/structures/arrays/uniform_2d.py index 2b3d22ded..fcb662937 100644 --- a/autoarray/structures/arrays/uniform_2d.py +++ b/autoarray/structures/arrays/uniform_2d.py @@ -359,6 +359,10 @@ def __init__( self.mask = mask self.header = header + @property + def values(self): + return self._array + def __array_finalize__(self, obj): if hasattr(obj, "mask"): self.mask = obj.mask From 2578532622cda84fbed1366c32695aa41f5b155b Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 12:11:47 +0000 Subject: [PATCH 138/154] changes to support autolens... --- autoarray/abstract_ndarray.py | 4 +++- autoarray/structures/grids/irregular_2d.py | 14 ++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index c0496e81c..a7744fa1d 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -251,7 +251,9 @@ def __getattr__(self, item): def __getitem__(self, item): result = self._array[item] if isinstance(item, slice): - return self.with_new_array(result) + result = self.with_new_array(result) + if isinstance(result, np.ndarray): + result = self.with_new_array(result) return result def __setitem__(self, key, value): diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 69cadc109..1341ec2b0 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -41,8 +41,9 @@ def __init__(self, values: Union[np.ndarray, List]): The irregular grid of (y,x) coordinates. """ - # if len(values) == 0: - # return [] + if len(values) == 0: + super().__init__(values) + return if type(values) is list: if isinstance(values[0], Grid2DIrregular): @@ -52,6 +53,10 @@ def __init__(self, values: Union[np.ndarray, List]): super().__init__(values) + @property + def values(self): + return self._array + @property def geometry(self): """ @@ -445,8 +450,9 @@ def __init__( A collection of (y,x) coordinates that. """ - # if len(values) == 0: - # return [] + if len(values) == 0: + super().__init__(values=values) + return if isinstance(values[0], float): values = [values] From fc7047ac890791fc250b595c1e0d77b4322f596f Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 12:21:08 +0000 Subject: [PATCH 139/154] more casting to array so that indexed/sliced arrays have numba compliant type --- autoarray/operators/convolver.py | 2 +- autoarray/structures/arrays/array_2d_util.py | 4 ++-- autoarray/structures/grids/grid_2d_util.py | 4 ++-- autoarray/structures/grids/uniform_2d.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/autoarray/operators/convolver.py b/autoarray/operators/convolver.py index 5c45d8907..24ff7b035 100644 --- a/autoarray/operators/convolver.py +++ b/autoarray/operators/convolver.py @@ -213,7 +213,7 @@ def __init__(self, mask, kernel): coordinates=(x, y), mask=np.array(mask), mask_index_array=self.mask_index_array, - kernel_2d=self.kernel.native[:, :], + kernel_2d=np.array(self.kernel.native[:, :]), ) self.image_frame_1d_indexes[ mask_1d_index, : diff --git a/autoarray/structures/arrays/array_2d_util.py b/autoarray/structures/arrays/array_2d_util.py index ddd9d0714..7e1ca9852 100644 --- a/autoarray/structures/arrays/array_2d_util.py +++ b/autoarray/structures/arrays/array_2d_util.py @@ -130,7 +130,7 @@ def convert_array_2d( return array_2d elif not store_native: return array_2d_slim_from( - array_2d_native=array_2d, + array_2d_native=np.array(array_2d), mask_2d=np.array(mask_2d), sub_size=mask_2d.sub_size, ) @@ -605,7 +605,7 @@ def array_2d_native_from( ).astype("int") return array_2d_via_indexes_from( - array_2d_slim=array_2d_slim, + array_2d_slim=np.array(array_2d_slim), sub_shape=sub_shape, native_index_for_slim_index_2d=native_index_for_slim_index_2d, ) diff --git a/autoarray/structures/grids/grid_2d_util.py b/autoarray/structures/grids/grid_2d_util.py index f047e352a..837b37156 100644 --- a/autoarray/structures/grids/grid_2d_util.py +++ b/autoarray/structures/grids/grid_2d_util.py @@ -722,13 +722,13 @@ def grid_2d_slim_from( """ grid_1d_slim_y = array_2d_util.array_2d_slim_from( - array_2d_native=grid_2d_native[:, :, 0], + array_2d_native=np.array(grid_2d_native[:, :, 0]), mask_2d=np.array(mask), sub_size=sub_size, ) grid_1d_slim_x = array_2d_util.array_2d_slim_from( - array_2d_native=grid_2d_native[:, :, 1], + array_2d_native=np.array(grid_2d_native[:, :, 1]), mask_2d=np.array(mask), sub_size=sub_size, ) diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 47a522129..d9d8ccdcd 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -1216,7 +1216,7 @@ def relocated_grid_from(self, grid: "Grid2D") -> "Grid2D": return Grid2D( values=grid_2d_util.relocated_grid_via_jit_from( grid=np.array(grid), - border_grid=self.sub_border_grid, + border_grid=np.array(self.sub_border_grid), ), mask=grid.mask, sub_size=grid.mask.sub_size, From 46424320fb1eccb2b08c4a0e23544875efc75f0a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 12:33:10 +0000 Subject: [PATCH 140/154] more fixes --- autoarray/inversion/pixelization/mesh/mesh_util.py | 2 +- autoarray/structures/grids/uniform_2d.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/inversion/pixelization/mesh/mesh_util.py b/autoarray/inversion/pixelization/mesh/mesh_util.py index 308cb66bb..157b3a49c 100644 --- a/autoarray/inversion/pixelization/mesh/mesh_util.py +++ b/autoarray/inversion/pixelization/mesh/mesh_util.py @@ -405,7 +405,7 @@ def delaunay_interpolated_array_from( for slim_index in range(len(interpolation_grid_slim)): simplex_index = simplex_index_for_interpolate_index[slim_index] - interpolating_point = interpolation_grid_slim[slim_index] + interpolating_point = tuple(interpolation_grid_slim[slim_index]) if simplex_index == -1: cloest_pixel_index = np.argmin( diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index d9d8ccdcd..04178237e 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -1239,6 +1239,6 @@ def relocated_mesh_grid_from(self, mesh_grid: Grid2DIrregular) -> Grid2DIrregula return Grid2DIrregular( values=grid_2d_util.relocated_grid_via_jit_from( grid=np.array(mesh_grid), - border_grid=self.sub_border_grid, + border_grid=np.array(self.sub_border_grid), ), ) From 6137d03736a20f7834f9cc56ee5414b1e0ef6c21 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 12:51:24 +0000 Subject: [PATCH 141/154] fix test --- autoarray/structures/grids/irregular_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/grids/irregular_2d.py b/autoarray/structures/grids/irregular_2d.py index 1341ec2b0..6203daa50 100644 --- a/autoarray/structures/grids/irregular_2d.py +++ b/autoarray/structures/grids/irregular_2d.py @@ -459,7 +459,7 @@ def __init__( if isinstance(values[0], tuple): values = [values] - elif isinstance(values[0], np.ndarray): + elif isinstance(values[0], (np.ndarray, AbstractNDArray)): if len(values[0].shape) == 1: values = [values] elif isinstance(values[0], list) and isinstance(values[0][0], (float)): From 8c846c947e4a2ad165a5e53d2f3c2ef68a2039f8 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 14:10:25 +0000 Subject: [PATCH 142/154] ensure underlying array gets copied --- autoarray/abstract_ndarray.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index a7744fa1d..a6f439a74 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -82,7 +82,9 @@ def with_new_array(self, array): return new_array def copy(self): - return copy(self) + new = copy(self) + new._array = self._array.copy() + return new def __iter__(self): return iter(self._array) From 054567ca2210563a4810f66f0537cbf2c3f1bf85 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 14:36:03 +0000 Subject: [PATCH 143/154] ensure underlying array is always copied --- autoarray/abstract_ndarray.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index a6f439a74..945dbe8b2 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -83,6 +83,11 @@ def with_new_array(self, array): def copy(self): new = copy(self) + return new + + def __copy__(self): + new = self.__new__(self.__class__) + new.__dict__.update(self.__dict__) new._array = self._array.copy() return new From 8ca6a89bb5553b6b6bf2555f1b58eb9635336c8a Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 8 Jan 2024 15:30:45 +0000 Subject: [PATCH 144/154] invert and copy --- autoarray/abstract_ndarray.py | 5 +++++ autoarray/structures/arrays/array_2d_util.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 945dbe8b2..87a78d1a8 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -50,6 +50,11 @@ def __init__(self, array): __no_flatten__ = () + def invert(self): + new = self.copy() + new._array = np.invert(new._array) + return new + @classmethod def instance_flatten(cls, instance): keys, values = zip( diff --git a/autoarray/structures/arrays/array_2d_util.py b/autoarray/structures/arrays/array_2d_util.py index 7e1ca9852..be43314e8 100644 --- a/autoarray/structures/arrays/array_2d_util.py +++ b/autoarray/structures/arrays/array_2d_util.py @@ -117,7 +117,7 @@ def convert_array_2d( If True, the ndarray is stored in its native format [total_y_pixels, total_x_pixels]. This avoids mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck. """ - array_2d = convert_array(array=array_2d) + array_2d = convert_array(array=array_2d).copy() check_array_2d_and_mask_2d(array_2d=array_2d, mask_2d=mask_2d) From 1e24c9b616a0f4ce61156853cfcc883bf2293d5b Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 17 Jan 2024 08:28:26 +0000 Subject: [PATCH 145/154] ensure copying works --- test_autoarray/test_jax_changes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test_autoarray/test_jax_changes.py diff --git a/test_autoarray/test_jax_changes.py b/test_autoarray/test_jax_changes.py new file mode 100644 index 000000000..53dde76cc --- /dev/null +++ b/test_autoarray/test_jax_changes.py @@ -0,0 +1,14 @@ +import autoarray as aa + + +def test_copy(): + array = aa.Array2D.no_mask( + [[1.0, 2.0], [3.0, 4.0]], + pixel_scales=1.0, + ) + copied_array = array.copy() + + array[0] = 5.0 + + assert array[0] == 5.0 + assert copied_array[0] == 1.0 From 45dcb522f8ea4db58ff21f13812e6db4cbbc9c1e Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 17 Jan 2024 08:29:28 +0000 Subject: [PATCH 146/154] in place multiply --- test_autoarray/test_jax_changes.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test_autoarray/test_jax_changes.py b/test_autoarray/test_jax_changes.py index 53dde76cc..d977abfa2 100644 --- a/test_autoarray/test_jax_changes.py +++ b/test_autoarray/test_jax_changes.py @@ -1,14 +1,25 @@ import autoarray as aa +import pytest -def test_copy(): - array = aa.Array2D.no_mask( +@pytest.fixture(name="array") +def make_array(): + return aa.Array2D.no_mask( [[1.0, 2.0], [3.0, 4.0]], pixel_scales=1.0, ) + + +def test_copy(array): copied_array = array.copy() array[0] = 5.0 assert array[0] == 5.0 assert copied_array[0] == 1.0 + + +def test_in_place_multiply(array): + array[0] *= 2.0 + + assert array[0] == 2.0 From cef9a686c662cc9e41ec4c2c92a7f15734e97a87 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 17 Jan 2024 12:12:28 +0000 Subject: [PATCH 147/154] deepcopy --- autoarray/abstract_ndarray.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 87a78d1a8..5b327b79f 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -96,6 +96,12 @@ def __copy__(self): new._array = self._array.copy() return new + def __deepcopy__(self, memo): + new = self.__new__(self.__class__) + new.__dict__.update(self.__dict__) + new._array = self._array.copy() + return new + def __iter__(self): return iter(self._array) From 1cf676655bf26c441880310806490af0952e8ede Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 17 Jan 2024 16:17:29 +0000 Subject: [PATCH 148/154] change where array conversion occurs --- autoarray/structures/arrays/array_2d_util.py | 2 +- autoarray/structures/grids/grid_2d_util.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/autoarray/structures/arrays/array_2d_util.py b/autoarray/structures/arrays/array_2d_util.py index be43314e8..8b7e11f5d 100644 --- a/autoarray/structures/arrays/array_2d_util.py +++ b/autoarray/structures/arrays/array_2d_util.py @@ -605,7 +605,7 @@ def array_2d_native_from( ).astype("int") return array_2d_via_indexes_from( - array_2d_slim=np.array(array_2d_slim), + array_2d_slim=array_2d_slim, sub_shape=sub_shape, native_index_for_slim_index_2d=native_index_for_slim_index_2d, ) diff --git a/autoarray/structures/grids/grid_2d_util.py b/autoarray/structures/grids/grid_2d_util.py index 837b37156..26dd1dff0 100644 --- a/autoarray/structures/grids/grid_2d_util.py +++ b/autoarray/structures/grids/grid_2d_util.py @@ -120,10 +120,14 @@ def convert_grid_2d( return grid_2d elif not store_native: return grid_2d_slim_from( - grid_2d_native=grid_2d, mask=mask_2d, sub_size=mask_2d.sub_size + grid_2d_native=np.array(grid_2d), + mask=np.array(mask_2d), + sub_size=mask_2d.sub_size, ) return grid_2d_native_from( - grid_2d_slim=grid_2d, mask_2d=mask_2d, sub_size=mask_2d.sub_size + grid_2d_slim=np.array(grid_2d), + mask_2d=np.array(mask_2d), + sub_size=mask_2d.sub_size, ) From ee057f7e12aec8b5f18dfdaee4b60c397914e194 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 22 Jan 2024 07:32:06 +0000 Subject: [PATCH 149/154] debugging - revert --- autoarray/mask/mask_2d_util.py | 6 ++++-- autoarray/numba_util.py | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/autoarray/mask/mask_2d_util.py b/autoarray/mask/mask_2d_util.py index 90a2b64dc..4df28bf56 100644 --- a/autoarray/mask/mask_2d_util.py +++ b/autoarray/mask/mask_2d_util.py @@ -561,7 +561,7 @@ def blurring_mask_2d_from( return blurring_mask_2d -@numba_util.jit() +# @numba_util.jit() def mask_2d_via_shape_native_and_native_for_slim( shape_native: Tuple[int, int], native_for_slim: np.ndarray ) -> np.ndarray: @@ -599,7 +599,9 @@ def mask_2d_via_shape_native_and_native_for_slim( mask = np.ones(shape_native) for index in range(len(native_for_slim)): - mask[native_for_slim[index, 0], native_for_slim[index, 1]] = False + x = native_for_slim[index, 0] + y = native_for_slim[index, 1] + mask[x, y] = False return mask diff --git a/autoarray/numba_util.py b/autoarray/numba_util.py index fc16e4512..c8b862ca6 100644 --- a/autoarray/numba_util.py +++ b/autoarray/numba_util.py @@ -50,6 +50,7 @@ def jit(nopython=nopython, cache=cache, parallel=parallel): def wrapper(func): + return func try: use_numba = conf.instance["general"]["numba"]["use_numba"] From f5ed30118820699b00cbc7198f6473f32c993950 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 22 Jan 2024 08:38:20 +0000 Subject: [PATCH 150/154] do not cache derive_indexes property --- autoarray/mask/mask_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/mask/mask_2d.py b/autoarray/mask/mask_2d.py index 0962d0634..9041514c6 100644 --- a/autoarray/mask/mask_2d.py +++ b/autoarray/mask/mask_2d.py @@ -323,7 +323,7 @@ def geometry(self) -> Geometry2D: origin=self.origin, ) - @cached_property + @property def derive_indexes(self) -> DeriveIndexes2D: return DeriveIndexes2D(mask=self) From 1173f69a18b106cded9ab976db0fb193d450a036 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 22 Jan 2024 08:57:53 +0000 Subject: [PATCH 151/154] Revert "debugging - revert" This reverts commit ee057f7e12aec8b5f18dfdaee4b60c397914e194. --- autoarray/mask/mask_2d_util.py | 6 ++---- autoarray/numba_util.py | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/autoarray/mask/mask_2d_util.py b/autoarray/mask/mask_2d_util.py index 4df28bf56..90a2b64dc 100644 --- a/autoarray/mask/mask_2d_util.py +++ b/autoarray/mask/mask_2d_util.py @@ -561,7 +561,7 @@ def blurring_mask_2d_from( return blurring_mask_2d -# @numba_util.jit() +@numba_util.jit() def mask_2d_via_shape_native_and_native_for_slim( shape_native: Tuple[int, int], native_for_slim: np.ndarray ) -> np.ndarray: @@ -599,9 +599,7 @@ def mask_2d_via_shape_native_and_native_for_slim( mask = np.ones(shape_native) for index in range(len(native_for_slim)): - x = native_for_slim[index, 0] - y = native_for_slim[index, 1] - mask[x, y] = False + mask[native_for_slim[index, 0], native_for_slim[index, 1]] = False return mask diff --git a/autoarray/numba_util.py b/autoarray/numba_util.py index c8b862ca6..fc16e4512 100644 --- a/autoarray/numba_util.py +++ b/autoarray/numba_util.py @@ -50,7 +50,6 @@ def jit(nopython=nopython, cache=cache, parallel=parallel): def wrapper(func): - return func try: use_numba = conf.instance["general"]["numba"]["use_numba"] From a23e41964802fb0d306a98da3af6735fa108a333 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 22 Jan 2024 08:59:50 +0000 Subject: [PATCH 152/154] fix issue created by merge --- autoarray/structures/grids/grid_2d_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/structures/grids/grid_2d_util.py b/autoarray/structures/grids/grid_2d_util.py index 13a27bb70..1ce27da85 100644 --- a/autoarray/structures/grids/grid_2d_util.py +++ b/autoarray/structures/grids/grid_2d_util.py @@ -882,7 +882,7 @@ def grid_pixels_in_mask_pixels_from( An array containing the integer number of image-mesh pixels that fall without each of the data's mask. """ grid_pixel_centres = geometry_util.grid_pixel_centres_2d_slim_from( - grid_scaled_2d_slim=grid, + grid_scaled_2d_slim=np.array(grid), shape_native=shape_native, pixel_scales=pixel_scales, origin=origin, From 760dd91acc0f83e29d6cab5fe55878999ed6f566 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 22 Jan 2024 09:29:45 +0000 Subject: [PATCH 153/154] docs --- autoarray/abstract_ndarray.py | 58 +++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/autoarray/abstract_ndarray.py b/autoarray/abstract_ndarray.py index 5b327b79f..51932d4a9 100644 --- a/autoarray/abstract_ndarray.py +++ b/autoarray/abstract_ndarray.py @@ -18,13 +18,40 @@ def to_new_array(func): - def wrapper(self, *args, **kwargs): + """ + Decorator for functions that returns an array. The array is wrapped in a new instance of the class. + + Parameters + ---------- + func + The function to be decorated. + + Returns + ------- + The decorated function. + """ + + def wrapper(self, *args, **kwargs) -> "AbstractNDArray": return self.with_new_array(func(self, *args, **kwargs)) return wrapper def unwrap_array(func): + """ + Decorator for functions that take an array as an argument. If the argument is an AbstractNDArray, the underlying + array is used instead. + + Parameters + ---------- + func + The function to be decorated. + + Returns + ------- + The decorated function. + """ + def wrapper(self, other): try: return func(self, other.array) @@ -57,6 +84,9 @@ def invert(self): @classmethod def instance_flatten(cls, instance): + """ + Flatten an instance of an autoarray class into a tuple of its attributes (i.e.. a pytree) + """ keys, values = zip( *sorted( { @@ -76,12 +106,30 @@ def flip_hdu_for_ds9(values): @classmethod def instance_unflatten(cls, aux_data, children): + """ + Unflatten a tuple of attributes (i.e. a pytree) into an instance of an autoarray class + """ instance = cls.__new__(cls) for key, value in zip(aux_data, children[1:]): setattr(instance, key, value) return instance - def with_new_array(self, array): + def with_new_array(self, array: np.ndarray) -> "AbstractNDArray": + """ + Copy this object but give it a new array. + + This is used to ensure that when an array is modified, associated + attributes such as pixel size are retained. + + Parameters + ---------- + array + The new array that is given to the copied object. + + Returns + ------- + + """ new_array = self.copy() new_array._array = array return new_array @@ -91,12 +139,18 @@ def copy(self): return new def __copy__(self): + """ + When copying an autoarray also copy its underlying array. + """ new = self.__new__(self.__class__) new.__dict__.update(self.__dict__) new._array = self._array.copy() return new def __deepcopy__(self, memo): + """ + When copying an autoarray also copy its underlying array. + """ new = self.__new__(self.__class__) new.__dict__.update(self.__dict__) new._array = self._array.copy() From ae7e4886f9ebf85e370bfc362ca1e41fe00c0b3f Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 29 Jan 2024 08:37:37 +0000 Subject: [PATCH 154/154] fixed numba calls --- autoarray/inversion/regularization/exponential_kernel.py | 3 ++- autoarray/inversion/regularization/gaussian_kernel.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/autoarray/inversion/regularization/exponential_kernel.py b/autoarray/inversion/regularization/exponential_kernel.py index 3a861d055..1b3071679 100644 --- a/autoarray/inversion/regularization/exponential_kernel.py +++ b/autoarray/inversion/regularization/exponential_kernel.py @@ -97,7 +97,8 @@ def regularization_matrix_from(self, linear_obj: LinearObj) -> np.ndarray: The regularization matrix. """ covariance_matrix = exp_cov_matrix_from( - scale=self.scale, pixel_points=linear_obj.source_plane_mesh_grid + scale=self.scale, + pixel_points=np.array(linear_obj.source_plane_mesh_grid), ) return self.coefficient * np.linalg.inv(covariance_matrix) diff --git a/autoarray/inversion/regularization/gaussian_kernel.py b/autoarray/inversion/regularization/gaussian_kernel.py index 1f0628a8f..b9bf06637 100644 --- a/autoarray/inversion/regularization/gaussian_kernel.py +++ b/autoarray/inversion/regularization/gaussian_kernel.py @@ -96,7 +96,7 @@ def regularization_matrix_from(self, linear_obj: LinearObj) -> np.ndarray: The regularization matrix. """ covariance_matrix = gauss_cov_matrix_from( - scale=self.scale, pixel_points=linear_obj.source_plane_mesh_grid + scale=self.scale, pixel_points=np.array(linear_obj.source_plane_mesh_grid) ) return self.coefficient * np.linalg.inv(covariance_matrix)