diff --git a/autoarray/__init__.py b/autoarray/__init__.py index 0fef39dd..83a95ced 100644 --- a/autoarray/__init__.py +++ b/autoarray/__init__.py @@ -4,7 +4,6 @@ from . import fixtures from . import mock as m from .numba_util import profile_func -from .preloads import Preloads from .dataset import preprocess from .dataset.abstract.dataset import AbstractDataset from .dataset.abstract.w_tilde import AbstractWTilde diff --git a/autoarray/config/general.yaml b/autoarray/config/general.yaml index 16a8cd60..a6cb8d5d 100644 --- a/autoarray/config/general.yaml +++ b/autoarray/config/general.yaml @@ -16,6 +16,3 @@ pixelization: voronoi_nn_max_interpolation_neighbors: 300 structures: native_binned_only: false # If True, data structures are only stored in their native and binned format. This is used to reduce memory usage in autocti. -test: - preloads_check_threshold: 1.0 # If the figure of merit of a fit with and without preloads is greater than this threshold, the check preload test fails and an exception raised for a model-fit. - diff --git a/autoarray/config/visualize/include.yaml b/autoarray/config/visualize/include.yaml index 270b6b2f..f010d9f8 100644 --- a/autoarray/config/visualize/include.yaml +++ b/autoarray/config/visualize/include.yaml @@ -7,7 +7,7 @@ include_1d: mask: false # Include a Mask ? origin: false # Include the (x,) origin of the data's coordinate system ? include_2d: - border: true # Include the border of the mask (all pixels on the outside of the mask) ? + border: false # Include the border of the mask (all pixels on the outside of the mask) ? grid: false # Include the data's 2D grid of (y,x) coordinates ? mapper_image_plane_mesh_grid: false # For an Inversion, include the pixel centres computed in the image-plane / data frame? mapper_source_plane_data_grid: false # For an Inversion, include the centres of the image-plane grid mapped to the source-plane / frame in source-plane figures? diff --git a/autoarray/exc.py b/autoarray/exc.py index 657ec832..eed76b04 100644 --- a/autoarray/exc.py +++ b/autoarray/exc.py @@ -106,17 +106,6 @@ class PlottingException(Exception): pass -class PreloadsException(Exception): - """ - Raises exceptions associated with the `preloads.py` module and `Preloads` class. - - For example if the preloaded quantities lead to a change in figure of merit of a fit compared to a fit without - preloading. - """ - - pass - - class ProfilingException(Exception): """ Raises exceptions associated with in-built profiling tools (e.g. the `profile_func` decorator). diff --git a/autoarray/geometry/geometry_util.py b/autoarray/geometry/geometry_util.py index 71fb9b1f..5117252d 100644 --- a/autoarray/geometry/geometry_util.py +++ b/autoarray/geometry/geometry_util.py @@ -390,10 +390,12 @@ def transform_grid_2d_to_reference_frame( theta_coordinate_to_profile = np.arctan2( shifted_grid_2d[:, 0], shifted_grid_2d[:, 1] ) - np.radians(angle) - return np.vstack([ - radius * np.sin(theta_coordinate_to_profile), - radius * np.cos(theta_coordinate_to_profile) - ]).T + return np.vstack( + [ + radius * np.sin(theta_coordinate_to_profile), + radius * np.cos(theta_coordinate_to_profile), + ] + ).T def transform_grid_2d_from_reference_frame( diff --git a/autoarray/inversion/inversion/abstract.py b/autoarray/inversion/inversion/abstract.py index 14790641..1bca77c0 100644 --- a/autoarray/inversion/inversion/abstract.py +++ b/autoarray/inversion/inversion/abstract.py @@ -31,7 +31,6 @@ def __init__( dataset: Union[Imaging, Interferometer, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Optional["Preloads"] = None, run_time_dict: Optional[Dict] = None, ): """ @@ -70,17 +69,10 @@ def __init__( input dataset's data and whose values are solved for via the inversion. settings Settings controlling how an inversion is fitted for example which linear algebra formalism is used. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example certain matrices used by the linear algebra could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. """ - from autoarray.preloads import Preloads - - preloads = preloads or Preloads() - # try: # import numba # except ModuleNotFoundError: @@ -98,7 +90,6 @@ def __init__( self.settings = settings - self.preloads = preloads self.run_time_dict = run_time_dict @property @@ -322,10 +313,6 @@ def operated_mapping_matrix(self) -> np.ndarray: If there are multiple linear objects, the blurred mapping matrices are stacked such that their simultaneous linear equations are solved simultaneously. """ - - if self.preloads.operated_mapping_matrix is not None: - return self.preloads.operated_mapping_matrix - return np.hstack(self.operated_mapping_matrix_list) @cached_property @@ -356,9 +343,6 @@ def regularization_matrix(self) -> Optional[np.ndarray]: If the `settings.force_edge_pixels_to_zeros` is `True`, the edge pixels of each mapper in the inversion are regularized so high their value is forced to zero. """ - if self.preloads.regularization_matrix is not None: - return self.preloads.regularization_matrix - return block_diag( *[linear_obj.regularization_matrix for linear_obj in self.linear_obj_list] ) @@ -509,12 +493,12 @@ def reconstruction(self) -> np.ndarray: solutions = np.zeros(np.shape(self.curvature_reg_matrix)[0]) - solutions[ - values_to_solve - ] = inversion_util.reconstruction_positive_only_from( - data_vector=data_vector_input, - curvature_reg_matrix=curvature_reg_matrix_input, - settings=self.settings, + solutions[values_to_solve] = ( + inversion_util.reconstruction_positive_only_from( + data_vector=data_vector_input, + curvature_reg_matrix=curvature_reg_matrix_input, + settings=self.settings, + ) ) return solutions else: @@ -735,9 +719,6 @@ def log_det_regularization_matrix_term(self) -> float: if not self.has(cls=AbstractRegularization): return 0.0 - if self.preloads.log_det_regularization_matrix_term is not None: - return self.preloads.log_det_regularization_matrix_term - try: lu = splu(csc_matrix(self.regularization_matrix_reduced)) diagL = lu.L.diagonal() diff --git a/autoarray/inversion/inversion/factory.py b/autoarray/inversion/inversion/factory.py index f88bc36e..349d0c16 100644 --- a/autoarray/inversion/inversion/factory.py +++ b/autoarray/inversion/inversion/factory.py @@ -18,14 +18,12 @@ from autoarray.inversion.inversion.imaging.w_tilde import InversionImagingWTilde from autoarray.inversion.inversion.settings import SettingsInversion from autoarray.structures.arrays.uniform_2d import Array2D -from autoarray.preloads import Preloads def inversion_from( dataset: Union[Imaging, Interferometer, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -51,9 +49,6 @@ def inversion_from( input dataset's data and whose values are solved for via the inversion. settings Settings controlling how an inversion is fitted for example which linear algebra formalism is used. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example certain matrices used by the linear algebra could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. @@ -66,7 +61,6 @@ def inversion_from( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -82,7 +76,6 @@ def inversion_imaging_from( dataset, linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -112,9 +105,6 @@ def inversion_imaging_from( input dataset's data and whose values are solved for via the inversion. settings Settings controlling how an inversion is fitted for example which linear algebra formalism is used. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example certain matrices used by the linear algebra could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. @@ -127,8 +117,6 @@ def inversion_imaging_from( for linear_obj in linear_obj_list ): use_w_tilde = False - elif preloads.use_w_tilde is not None: - use_w_tilde = preloads.use_w_tilde else: use_w_tilde = settings.use_w_tilde @@ -136,17 +124,13 @@ def inversion_imaging_from( use_w_tilde = False if use_w_tilde: - if preloads.w_tilde is not None: - w_tilde = preloads.w_tilde - else: - w_tilde = dataset.w_tilde + w_tilde = dataset.w_tilde return InversionImagingWTilde( dataset=dataset, w_tilde=w_tilde, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -154,7 +138,6 @@ def inversion_imaging_from( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -163,7 +146,6 @@ def inversion_interferometer_from( dataset: Union[Interferometer, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -197,9 +179,6 @@ def inversion_interferometer_from( input dataset's data and whose values are solved for via the inversion. settings Settings controlling how an inversion is fitted for example which linear algebra formalism is used. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example certain matrices used by the linear algebra could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. @@ -222,17 +201,13 @@ def inversion_interferometer_from( if not settings.use_linear_operators: if use_w_tilde: - if preloads.w_tilde is not None: - w_tilde = preloads.w_tilde - else: - w_tilde = dataset.w_tilde + w_tilde = dataset.w_tilde return InversionInterferometerWTilde( dataset=dataset, w_tilde=w_tilde, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -241,7 +216,6 @@ def inversion_interferometer_from( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -250,6 +224,5 @@ def inversion_interferometer_from( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/inversion/imaging/abstract.py b/autoarray/inversion/inversion/imaging/abstract.py index 5eccfa7d..1ea826f8 100644 --- a/autoarray/inversion/inversion/imaging/abstract.py +++ b/autoarray/inversion/inversion/imaging/abstract.py @@ -22,7 +22,6 @@ def __init__( dataset: Union[Imaging, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads=None, run_time_dict: Optional[Dict] = None, ): """ @@ -65,22 +64,14 @@ def __init__( input dataset's data and whose values are solved for via the inversion. settings Settings controlling how an inversion is fitted for example which linear algebra formalism is used. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example certain matrices used by the linear algebra could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. """ - from autoarray.preloads import Preloads - - preloads = preloads or Preloads() - super().__init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -104,11 +95,13 @@ def operated_mapping_matrix_list(self) -> List[np.ndarray]: """ return [ - self.convolver.convolve_mapping_matrix( - mapping_matrix=linear_obj.mapping_matrix + ( + self.convolver.convolve_mapping_matrix( + mapping_matrix=linear_obj.mapping_matrix + ) + if linear_obj.operated_mapping_matrix_override is None + else self.linear_func_operated_mapping_matrix_dict[linear_obj] ) - if linear_obj.operated_mapping_matrix_override is None - else self.linear_func_operated_mapping_matrix_dict[linear_obj] for linear_obj in self.linear_obj_list ] @@ -140,12 +133,6 @@ def linear_func_operated_mapping_matrix_dict(self) -> Dict: A dictionary mapping every linear function object to its operated mapping matrix. """ - if self.preloads.linear_func_operated_mapping_matrix_dict is not None: - return self._updated_cls_key_dict_from( - cls=AbstractLinearObjFuncList, - preload_dict=self.preloads.linear_func_operated_mapping_matrix_dict, - ) - linear_func_operated_mapping_matrix_dict = {} for linear_func in self.cls_list_from(cls=AbstractLinearObjFuncList): @@ -156,9 +143,9 @@ def linear_func_operated_mapping_matrix_dict(self) -> Dict: mapping_matrix=linear_func.mapping_matrix ) - linear_func_operated_mapping_matrix_dict[ - linear_func - ] = operated_mapping_matrix + linear_func_operated_mapping_matrix_dict[linear_func] = ( + operated_mapping_matrix + ) return linear_func_operated_mapping_matrix_dict @@ -192,12 +179,6 @@ def data_linear_func_matrix_dict(self): A matrix of shape [data_pixels, total_fixed_linear_functions] that for each data pixel, maps it to the sum of the values of a linear object function convolved with the PSF kernel at the data pixel. """ - if self.preloads.data_linear_func_matrix_dict is not None: - return self._updated_cls_key_dict_from( - cls=AbstractLinearObjFuncList, - preload_dict=self.preloads.data_linear_func_matrix_dict, - ) - linear_func_list = self.cls_list_from(cls=AbstractLinearObjFuncList) data_linear_func_matrix_dict = {} @@ -237,13 +218,6 @@ def mapper_operated_mapping_matrix_dict(self) -> Dict: ------- A dictionary mapping every mapper object to its operated mapping matrix. """ - - if self.preloads.mapper_operated_mapping_matrix_dict is not None: - return self._updated_cls_key_dict_from( - cls=AbstractMapper, - preload_dict=self.preloads.mapper_operated_mapping_matrix_dict, - ) - mapper_operated_mapping_matrix_dict = {} for mapper in self.cls_list_from(cls=AbstractMapper): diff --git a/autoarray/inversion/inversion/imaging/mapping.py b/autoarray/inversion/inversion/imaging/mapping.py index 96f5d36d..5b3e4096 100644 --- a/autoarray/inversion/inversion/imaging/mapping.py +++ b/autoarray/inversion/inversion/imaging/mapping.py @@ -24,7 +24,6 @@ def __init__( dataset: Union[Imaging, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads=None, run_time_dict: Optional[Dict] = None, ): """ @@ -55,7 +54,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -70,9 +68,6 @@ def _data_vector_mapper(self) -> np.ndarray: in the inversion, and is separated into a separate method to enable preloading of the mapper `data_vector`. """ - if self.preloads.data_vector_mapper is not None: - return self.preloads.data_vector_mapper - if not self.has(cls=AbstractMapper): return None @@ -117,16 +112,8 @@ def data_vector(self) -> np.ndarray: The calculation is described in more detail in `inversion_util.data_vector_via_blurred_mapping_matrix_from`. """ - if self.preloads.data_vector_mapper is not None: - return self.preloads.data_vector_mapper - - if self.preloads.operated_mapping_matrix is not None: - operated_mapping_matrix = self.preloads.operated_mapping_matrix - else: - operated_mapping_matrix = self.operated_mapping_matrix - return inversion_imaging_util.data_vector_via_blurred_mapping_matrix_from( - blurred_mapping_matrix=operated_mapping_matrix, + blurred_mapping_matrix=self.operated_mapping_matrix, image=np.array(self.data), noise_map=np.array(self.noise_map), ) @@ -143,9 +130,6 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]: other calculations to enable preloading of this calculation. """ - if self.preloads.curvature_matrix_mapper_diag is not None: - return self.preloads.curvature_matrix_mapper_diag - if not self.has(cls=AbstractMapper): return None @@ -201,11 +185,6 @@ def curvature_matrix(self): array of memory. """ - if self.preloads.curvature_matrix is not None: - # Need to copy because of how curvature_reg_matirx overwrites memory. - - return copy.copy(self.preloads.curvature_matrix) - return inversion_util.curvature_matrix_via_mapping_matrix_from( mapping_matrix=self.operated_mapping_matrix, noise_map=self.noise_map, diff --git a/autoarray/inversion/inversion/imaging/w_tilde.py b/autoarray/inversion/inversion/imaging/w_tilde.py index 4540f71d..5f791873 100644 --- a/autoarray/inversion/inversion/imaging/w_tilde.py +++ b/autoarray/inversion/inversion/imaging/w_tilde.py @@ -14,7 +14,6 @@ from autoarray.inversion.inversion.settings import SettingsInversion from autoarray.inversion.linear_obj.func_list import AbstractLinearObjFuncList from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper -from autoarray.preloads import Preloads from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.inversion.inversion import inversion_util @@ -28,7 +27,6 @@ def __init__( w_tilde: WTildeImaging, linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -63,7 +61,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) @@ -94,9 +91,6 @@ def _data_vector_mapper(self) -> np.ndarray: in the inversion, and is separated into a separate method to enable preloading of the mapper `data_vector`. """ - if self.preloads.data_vector_mapper is not None: - return self.preloads.data_vector_mapper - if not self.has(cls=AbstractMapper): return None @@ -153,9 +147,6 @@ def _data_vector_x1_mapper(self) -> np.ndarray: which circumvents `np.concatenate` for speed up. """ - if self.preloads.data_vector_mapper is not None: - return self.preloads.data_vector_mapper - linear_obj = self.linear_obj_list[0] return inversion_imaging_util.data_vector_via_w_tilde_data_imaging_from( @@ -177,9 +168,6 @@ def _data_vector_multi_mapper(self) -> np.ndarray: which computes the `data_vector` of each object and concatenates them. """ - if self.preloads.data_vector_mapper is not None: - return self.preloads.data_vector_mapper - return np.concatenate( [ inversion_imaging_util.data_vector_via_w_tilde_data_imaging_from( @@ -255,11 +243,6 @@ def curvature_matrix(self) -> np.ndarray: array of memory. """ - if self.preloads.curvature_matrix is not None: - # Need to copy because of how curvature_reg_matirx overwrites memory. - - return copy.copy(self.preloads.curvature_matrix) - if self.has(cls=AbstractLinearObjFuncList): curvature_matrix = self._curvature_matrix_func_list_and_mapper elif self.total(cls=AbstractMapper) == 1: @@ -292,9 +275,6 @@ def _curvature_matrix_mapper_diag(self) -> Optional[np.ndarray]: other calculations to enable preloading of this calculation. """ - if self.preloads.curvature_matrix_mapper_diag is not None: - return self.preloads.curvature_matrix_mapper_diag - if not self.has(cls=AbstractMapper): return None @@ -451,44 +431,21 @@ def _curvature_matrix_func_list_and_mapper(self) -> np.ndarray: for func_index, linear_func in enumerate(linear_func_list): linear_func_param_range = linear_func_param_range_list[func_index] - if self.preloads.data_linear_func_matrix_dict is not None: - off_diag = inversion_imaging_util.curvature_matrix_off_diags_via_data_linear_func_matrix_from( - data_linear_func_matrix=self.data_linear_func_matrix_dict[ - linear_func - ], - data_to_pix_unique=mapper.unique_mappings.data_to_pix_unique, - data_weights=mapper.unique_mappings.data_weights, - pix_lengths=mapper.unique_mappings.pix_lengths, - pix_pixels=mapper.params, - ) - - elif self.preloads.mapper_operated_mapping_matrix_dict is not None: - operated_mapping_matrix = self.mapper_operated_mapping_matrix_dict[ - mapper - ] - - curvature_weights = ( - self.linear_func_operated_mapping_matrix_dict[linear_func] - ) / self.noise_map[:, None] ** 2 - - off_diag = np.dot(operated_mapping_matrix.T, curvature_weights) - - else: - curvature_weights = ( - self.linear_func_operated_mapping_matrix_dict[linear_func] - / self.noise_map[:, None] ** 2 - ) - - off_diag = inversion_imaging_util.curvature_matrix_off_diags_via_mapper_and_linear_func_curvature_vector_from( - data_to_pix_unique=mapper.unique_mappings.data_to_pix_unique, - data_weights=mapper.unique_mappings.data_weights, - pix_lengths=mapper.unique_mappings.pix_lengths, - pix_pixels=mapper.params, - curvature_weights=curvature_weights, - image_frame_1d_lengths=self.convolver.image_frame_1d_lengths, - image_frame_1d_indexes=self.convolver.image_frame_1d_indexes, - image_frame_1d_kernels=self.convolver.image_frame_1d_kernels, - ) + curvature_weights = ( + self.linear_func_operated_mapping_matrix_dict[linear_func] + / self.noise_map[:, None] ** 2 + ) + + off_diag = inversion_imaging_util.curvature_matrix_off_diags_via_mapper_and_linear_func_curvature_vector_from( + data_to_pix_unique=mapper.unique_mappings.data_to_pix_unique, + data_weights=mapper.unique_mappings.data_weights, + pix_lengths=mapper.unique_mappings.pix_lengths, + pix_pixels=mapper.params, + curvature_weights=curvature_weights, + image_frame_1d_lengths=self.convolver.image_frame_1d_lengths, + image_frame_1d_indexes=self.convolver.image_frame_1d_indexes, + image_frame_1d_kernels=self.convolver.image_frame_1d_kernels, + ) curvature_matrix[ mapper_param_range[0] : mapper_param_range[1], diff --git a/autoarray/inversion/inversion/interferometer/abstract.py b/autoarray/inversion/inversion/interferometer/abstract.py index 1d27d34f..47e1c84b 100644 --- a/autoarray/inversion/inversion/interferometer/abstract.py +++ b/autoarray/inversion/inversion/interferometer/abstract.py @@ -7,7 +7,6 @@ from autoarray.mask.mask_2d import Mask2D from autoarray.inversion.linear_obj.linear_obj import LinearObj from autoarray.inversion.inversion.settings import SettingsInversion -from autoarray.preloads import Preloads from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.inversion.inversion import inversion_util @@ -21,7 +20,6 @@ def __init__( dataset: Union[Interferometer, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -51,7 +49,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/inversion/interferometer/mapping.py b/autoarray/inversion/inversion/interferometer/mapping.py index 6a171999..9cde492e 100644 --- a/autoarray/inversion/inversion/interferometer/mapping.py +++ b/autoarray/inversion/inversion/interferometer/mapping.py @@ -10,7 +10,6 @@ ) from autoarray.inversion.linear_obj.linear_obj import LinearObj from autoarray.inversion.inversion.settings import SettingsInversion -from autoarray.preloads import Preloads from autoarray.structures.visibilities import Visibilities from autoarray.inversion.inversion.interferometer import inversion_interferometer_util @@ -25,7 +24,6 @@ def __init__( dataset: Union[Interferometer, DatasetInterface], linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -58,7 +56,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/inversion/interferometer/w_tilde.py b/autoarray/inversion/inversion/interferometer/w_tilde.py index 46ef6794..c3fd233b 100644 --- a/autoarray/inversion/inversion/interferometer/w_tilde.py +++ b/autoarray/inversion/inversion/interferometer/w_tilde.py @@ -12,7 +12,6 @@ from autoarray.inversion.linear_obj.linear_obj import LinearObj from autoarray.inversion.inversion.settings import SettingsInversion from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper -from autoarray.preloads import Preloads from autoarray.structures.visibilities import Visibilities from autoarray.inversion.inversion import inversion_util @@ -27,7 +26,6 @@ def __init__( w_tilde: WTildeInterferometer, linear_obj_list: List[LinearObj], settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ): """ @@ -66,7 +64,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/mock/mock_inversion.py b/autoarray/inversion/mock/mock_inversion.py index c9455135..b221163e 100644 --- a/autoarray/inversion/mock/mock_inversion.py +++ b/autoarray/inversion/mock/mock_inversion.py @@ -4,7 +4,6 @@ from autoarray.inversion.inversion.dataset_interface import DatasetInterface from autoarray.inversion.inversion.abstract import AbstractInversion from autoarray.inversion.inversion.settings import SettingsInversion -from autoarray.preloads import Preloads class MockInversion(AbstractInversion): @@ -31,7 +30,6 @@ def __init__( log_det_curvature_reg_matrix_term=None, log_det_regularization_matrix_term=None, settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), ): dataset = DatasetInterface( data=data, @@ -42,7 +40,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list or [], settings=settings, - preloads=preloads, ) self._operated_mapping_matrix = operated_mapping_matrix diff --git a/autoarray/inversion/mock/mock_inversion_imaging.py b/autoarray/inversion/mock/mock_inversion_imaging.py index 65f224ac..64076db5 100644 --- a/autoarray/inversion/mock/mock_inversion_imaging.py +++ b/autoarray/inversion/mock/mock_inversion_imaging.py @@ -5,7 +5,6 @@ from autoarray.inversion.inversion.imaging.mapping import InversionImagingMapping from autoarray.inversion.inversion.imaging.w_tilde import InversionImagingWTilde from autoarray.inversion.inversion.settings import SettingsInversion -from autoarray.preloads import Preloads class MockInversionImaging(InversionImagingMapping): @@ -19,7 +18,6 @@ def __init__( linear_func_operated_mapping_matrix_dict=None, data_linear_func_matrix_dict=None, settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), ): dataset = DatasetInterface( data=data, @@ -31,7 +29,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, ) self._operated_mapping_matrix = operated_mapping_matrix @@ -78,7 +75,6 @@ def __init__( linear_obj_list=None, curvature_matrix_mapper_diag=None, settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), ): dataset = DatasetInterface( data=data, @@ -91,7 +87,6 @@ def __init__( w_tilde=w_tilde or MockWTildeImaging(), linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, ) self.__curvature_matrix_mapper_diag = curvature_matrix_mapper_diag diff --git a/autoarray/inversion/mock/mock_inversion_interferometer.py b/autoarray/inversion/mock/mock_inversion_interferometer.py index a0c09250..58de7152 100644 --- a/autoarray/inversion/mock/mock_inversion_interferometer.py +++ b/autoarray/inversion/mock/mock_inversion_interferometer.py @@ -5,7 +5,6 @@ InversionInterferometerMapping, ) from autoarray.inversion.inversion.settings import SettingsInversion -from autoarray.preloads import Preloads class MockInversionInterferometer(InversionInterferometerMapping): @@ -17,7 +16,6 @@ def __init__( linear_obj_list=None, operated_mapping_matrix=None, settings: SettingsInversion = SettingsInversion(), - preloads: Preloads = Preloads(), ): dataset = DatasetInterface( data=data, @@ -29,7 +27,6 @@ def __init__( dataset=dataset, linear_obj_list=linear_obj_list, settings=settings, - preloads=preloads, ) self._operated_mapping_matrix = operated_mapping_matrix diff --git a/autoarray/inversion/mock/mock_mesh.py b/autoarray/inversion/mock/mock_mesh.py index 04fca5c0..def02657 100644 --- a/autoarray/inversion/mock/mock_mesh.py +++ b/autoarray/inversion/mock/mock_mesh.py @@ -7,7 +7,6 @@ from autoarray.inversion.pixelization.mappers.mapper_grids import MapperGrids from autoarray.structures.grids.uniform_2d import Grid2D from autoarray.structures.grids.irregular_2d import Grid2DIrregular -from autoarray.preloads import Preloads class MockMesh(AbstractMesh): @@ -24,7 +23,6 @@ def mapper_grids_from( source_plane_mesh_grid: Optional[Abstract2DMesh] = None, image_plane_mesh_grid: Optional[Grid2DIrregular] = None, adapt_data: Optional[np.ndarray] = None, - preloads: Optional[Preloads] = None, run_time_dict: Optional[Dict] = None, ) -> MapperGrids: return MapperGrids( @@ -34,7 +32,6 @@ def mapper_grids_from( source_plane_mesh_grid=source_plane_mesh_grid, image_plane_mesh_grid=self.image_plane_mesh_grid, adapt_data=adapt_data, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/mock/mock_pixelization.py b/autoarray/inversion/mock/mock_pixelization.py index dc06118a..72ced89e 100644 --- a/autoarray/inversion/mock/mock_pixelization.py +++ b/autoarray/inversion/mock/mock_pixelization.py @@ -29,7 +29,6 @@ def mapper_grids_from( image_plane_mesh_grid=None, adapt_data=None, settings=None, - preloads=None, run_time_dict=None, ): return self.mapper diff --git a/autoarray/inversion/pixelization/border_relocator.py b/autoarray/inversion/pixelization/border_relocator.py index da3cd86e..8af010a7 100644 --- a/autoarray/inversion/pixelization/border_relocator.py +++ b/autoarray/inversion/pixelization/border_relocator.py @@ -117,12 +117,12 @@ def sub_border_pixel_slim_indexes_from( int(border_pixel) ] - sub_border_pixels[ - border_1d_index - ] = grid_2d_util.furthest_grid_2d_slim_index_from( - grid_2d_slim=sub_grid_2d_slim, - slim_indexes=sub_border_pixels_of_border_pixel, - coordinate=mask_centre, + sub_border_pixels[border_1d_index] = ( + grid_2d_util.furthest_grid_2d_slim_index_from( + grid_2d_slim=sub_grid_2d_slim, + slim_indexes=sub_border_pixels_of_border_pixel, + coordinate=mask_centre, + ) ) return sub_border_pixels diff --git a/autoarray/inversion/pixelization/mappers/mapper_grids.py b/autoarray/inversion/pixelization/mappers/mapper_grids.py index 037c1a19..9a12e2f9 100644 --- a/autoarray/inversion/pixelization/mappers/mapper_grids.py +++ b/autoarray/inversion/pixelization/mappers/mapper_grids.py @@ -2,9 +2,6 @@ import numpy as np from typing import TYPE_CHECKING, Dict, Optional -if TYPE_CHECKING: - from autoarray import Preloads - from autoarray.mask.mask_2d import Mask2D from autoarray.structures.arrays.uniform_2d import Array2D from autoarray.structures.grids.uniform_2d import Grid2D @@ -22,7 +19,6 @@ def __init__( source_plane_mesh_grid: Optional[Abstract2DMesh] = None, image_plane_mesh_grid: Optional[Grid2DIrregular] = None, adapt_data: Optional[np.ndarray] = None, - preloads: Optional[Preloads] = None, run_time_dict: Optional[Dict] = None, ): """ @@ -59,21 +55,15 @@ def __init__( adapt_data An image which is used to determine the `image_plane_mesh_grid` and therefore adapt the distribution of pixels of the Delaunay grid to the data it discretizes. - preloads - Preloads in memory certain arrays which may be known beforehand in order to speed up the calculation, - for example the `source_plane_mesh_grid` could be preloaded. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. """ - from autoarray.preloads import Preloads - self.mask = mask self.source_plane_data_grid = source_plane_data_grid self.source_plane_mesh_grid = source_plane_mesh_grid self.image_plane_mesh_grid = image_plane_mesh_grid self.adapt_data = adapt_data - self.preloads = preloads or Preloads() self.run_time_dict = run_time_dict @property diff --git a/autoarray/inversion/pixelization/mesh/abstract.py b/autoarray/inversion/pixelization/mesh/abstract.py index dc180723..95d3d1ce 100644 --- a/autoarray/inversion/pixelization/mesh/abstract.py +++ b/autoarray/inversion/pixelization/mesh/abstract.py @@ -5,7 +5,6 @@ from autoarray.inversion.pixelization.border_relocator import BorderRelocator from autoarray.structures.grids.uniform_2d import Grid2D from autoarray.structures.grids.irregular_2d import Grid2DIrregular -from autoarray.preloads import Preloads from autoarray.numba_util import profile_func @@ -19,7 +18,6 @@ def relocated_grid_from( self, border_relocator: BorderRelocator, source_plane_data_grid: Grid2D, - preloads: Preloads = Preloads(), ) -> Grid2D: """ Relocates all coordinates of the input `source_plane_data_grid` that are outside of a @@ -43,16 +41,10 @@ def relocated_grid_from( edge. source_plane_data_grid A 2D (y,x) grid of coordinates, whose coordinates outside the border are relocated to its edge. - preloads - Contains quantities which may already be computed and can be preloaded to speed up calculations, in this - case the relocated grid. """ - if preloads.relocated_grid is None: - if border_relocator is not None: - return border_relocator.relocated_grid_from(grid=source_plane_data_grid) - return source_plane_data_grid - - return preloads.relocated_grid + if border_relocator is not None: + return border_relocator.relocated_grid_from(grid=source_plane_data_grid) + return source_plane_data_grid @profile_func def relocated_mesh_grid_from( @@ -103,7 +95,6 @@ def mapper_grids_from( source_plane_mesh_grid: Optional[Grid2DIrregular] = None, image_plane_mesh_grid: Optional[Grid2DIrregular] = None, adapt_data: np.ndarray = None, - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ) -> MapperGrids: raise NotImplementedError diff --git a/autoarray/inversion/pixelization/mesh/rectangular.py b/autoarray/inversion/pixelization/mesh/rectangular.py index 0f4cc204..514e5b6d 100644 --- a/autoarray/inversion/pixelization/mesh/rectangular.py +++ b/autoarray/inversion/pixelization/mesh/rectangular.py @@ -4,7 +4,7 @@ from autoarray.structures.grids.uniform_2d import Grid2D from autoarray.structures.mesh.rectangular_2d import Mesh2DRectangular -from autoarray.preloads import Preloads + from autoarray.inversion.pixelization.mappers.mapper_grids import MapperGrids from autoarray.inversion.pixelization.mesh.abstract import AbstractMesh from autoarray.inversion.pixelization.border_relocator import BorderRelocator @@ -62,7 +62,6 @@ def mapper_grids_from( source_plane_mesh_grid: Grid2D = None, image_plane_mesh_grid: Grid2D = None, adapt_data: np.ndarray = None, - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ) -> MapperGrids: """ @@ -95,9 +94,6 @@ def mapper_grids_from( Not used for a rectangular pixelization. adapt_data Not used for a rectangular pixelization. - preloads - Object which may contain preloaded arrays of quantities computed in the pixelization, which are passed via - this object speed up the calculation. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. """ @@ -107,7 +103,6 @@ def mapper_grids_from( relocated_grid = self.relocated_grid_from( border_relocator=border_relocator, source_plane_data_grid=source_plane_data_grid, - preloads=preloads, ) mesh_grid = self.mesh_grid_from(source_plane_data_grid=relocated_grid) @@ -117,7 +112,6 @@ def mapper_grids_from( source_plane_mesh_grid=mesh_grid, image_plane_mesh_grid=image_plane_mesh_grid, adapt_data=adapt_data, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/pixelization/mesh/triangulation.py b/autoarray/inversion/pixelization/mesh/triangulation.py index 91ee0363..17b7536f 100644 --- a/autoarray/inversion/pixelization/mesh/triangulation.py +++ b/autoarray/inversion/pixelization/mesh/triangulation.py @@ -3,7 +3,6 @@ from autoarray.structures.grids.uniform_2d import Grid2D from autoarray.structures.grids.irregular_2d import Grid2DIrregular -from autoarray.preloads import Preloads from autoarray.inversion.pixelization.mappers.mapper_grids import MapperGrids from autoarray.inversion.pixelization.mesh.abstract import AbstractMesh from autoarray.inversion.pixelization.border_relocator import BorderRelocator @@ -18,7 +17,6 @@ def mapper_grids_from( source_plane_mesh_grid: Optional[Grid2DIrregular] = None, image_plane_mesh_grid: Optional[Grid2DIrregular] = None, adapt_data: np.ndarray = None, - preloads: Preloads = Preloads(), run_time_dict: Optional[Dict] = None, ) -> MapperGrids: """ @@ -61,9 +59,6 @@ def mapper_grids_from( transformation applied to it to create the `source_plane_mesh_grid`. adapt_data Not used for a rectangular mesh. - preloads - Object which may contain preloaded arrays of quantities computed in the mesh, which are passed via - this object speed up the calculation. run_time_dict A dictionary which contains timing of certain functions calls which is used for profiling. """ @@ -73,7 +68,6 @@ def mapper_grids_from( source_plane_data_grid = self.relocated_grid_from( border_relocator=border_relocator, source_plane_data_grid=source_plane_data_grid, - preloads=preloads, ) relocated_source_plane_mesh_grid = self.relocated_mesh_grid_from( @@ -96,6 +90,5 @@ def mapper_grids_from( source_plane_mesh_grid=source_plane_mesh_grid, image_plane_mesh_grid=image_plane_mesh_grid, adapt_data=adapt_data, - preloads=preloads, run_time_dict=run_time_dict, ) diff --git a/autoarray/inversion/plot/inversion_plotters.py b/autoarray/inversion/plot/inversion_plotters.py index 000c9879..4d1b3fd7 100644 --- a/autoarray/inversion/plot/inversion_plotters.py +++ b/autoarray/inversion/plot/inversion_plotters.py @@ -212,10 +212,9 @@ def figures_2d_of_pixelization( "inversion" ]["reconstruction_vmax_factor"] - self.mat_plot_2d.cmap.kwargs[ - "vmax" - ] = reconstruction_vmax_factor * np.max( - self.inversion.reconstruction + self.mat_plot_2d.cmap.kwargs["vmax"] = ( + reconstruction_vmax_factor + * np.max(self.inversion.reconstruction) ) vmax_custom = True diff --git a/autoarray/mask/mask_2d_util.py b/autoarray/mask/mask_2d_util.py index db2751b0..47db2413 100644 --- a/autoarray/mask/mask_2d_util.py +++ b/autoarray/mask/mask_2d_util.py @@ -316,9 +316,7 @@ def elliptical_radius_from( y_scaled_elliptical = r_scaled * np.sin(theta_rotated) x_scaled_elliptical = r_scaled * np.cos(theta_rotated) - return np.sqrt( - x_scaled_elliptical**2.0 + (y_scaled_elliptical / axis_ratio) ** 2.0 - ) + return np.sqrt(x_scaled_elliptical**2.0 + (y_scaled_elliptical / axis_ratio) ** 2.0) @numba_util.jit() diff --git a/autoarray/numpy_wrapper.py b/autoarray/numpy_wrapper.py index 54edb6c4..3f534d99 100644 --- a/autoarray/numpy_wrapper.py +++ b/autoarray/numpy_wrapper.py @@ -9,7 +9,6 @@ import jax from jax import numpy as np, jit - print("JAX mode enabled") except ImportError: raise ImportError( diff --git a/autoarray/operators/convolver.py b/autoarray/operators/convolver.py index 21ce28f0..5a52329b 100644 --- a/autoarray/operators/convolver.py +++ b/autoarray/operators/convolver.py @@ -223,12 +223,12 @@ def __init__(self, mask, kernel): mask_index_array=self.mask_index_array, kernel_2d=np.array(self.kernel.native[:, :]), ) - self.image_frame_1d_indexes[ - mask_1d_index, : - ] = image_frame_1d_indexes - self.image_frame_1d_kernels[ - mask_1d_index, : - ] = image_frame_1d_kernels + self.image_frame_1d_indexes[mask_1d_index, :] = ( + image_frame_1d_indexes + ) + self.image_frame_1d_kernels[mask_1d_index, :] = ( + image_frame_1d_kernels + ) self.image_frame_1d_lengths[mask_1d_index] = image_frame_1d_indexes[ image_frame_1d_indexes >= 0 ].shape[0] @@ -265,15 +265,15 @@ def __init__(self, mask, kernel): mask_index_array=np.array(self.mask_index_array), kernel_2d=np.array(self.kernel.native), ) - self.blurring_frame_1d_indexes[ - mask_1d_index, : - ] = image_frame_1d_indexes - self.blurring_frame_1d_kernels[ - mask_1d_index, : - ] = image_frame_1d_kernels - self.blurring_frame_1d_lengths[ - mask_1d_index - ] = image_frame_1d_indexes[image_frame_1d_indexes >= 0].shape[0] + self.blurring_frame_1d_indexes[mask_1d_index, :] = ( + image_frame_1d_indexes + ) + self.blurring_frame_1d_kernels[mask_1d_index, :] = ( + image_frame_1d_kernels + ) + self.blurring_frame_1d_lengths[mask_1d_index] = ( + image_frame_1d_indexes[image_frame_1d_indexes >= 0].shape[0] + ) mask_1d_index += 1 @staticmethod @@ -317,33 +317,28 @@ def frame_at_coordinates_jit(coordinates, mask, mask_index_array, kernel_2d): return frame, kernel_frame - def jax_convolve(self, image, blurring_image, method='auto'): + def jax_convolve(self, image, blurring_image, method="auto"): slim_to_2D_index_image = jnp.nonzero( - jnp.logical_not(self.mask.array), - size=image.shape[0] + jnp.logical_not(self.mask.array), size=image.shape[0] ) slim_to_2D_index_blurring = jnp.nonzero( - jnp.logical_not(self.blurring_mask), - size=blurring_image.shape[0] + jnp.logical_not(self.blurring_mask), size=blurring_image.shape[0] ) expanded_image_native = jnp.zeros(self.mask.shape) - expanded_image_native = expanded_image_native.at[ - slim_to_2D_index_image - ].set(image.array) - expanded_image_native = expanded_image_native.at[ - slim_to_2D_index_blurring - ].set(blurring_image.array) + expanded_image_native = expanded_image_native.at[slim_to_2D_index_image].set( + image.array + ) + expanded_image_native = expanded_image_native.at[slim_to_2D_index_blurring].set( + blurring_image.array + ) kernel = np.array(self.kernel.native.array) convolve_native = jax.scipy.signal.convolve( - expanded_image_native, - kernel, - mode='same', - method=method + expanded_image_native, kernel, mode="same", method=method ) convolve_slim = convolve_native[slim_to_2D_index_image] return convolve_slim - def convolve_image(self, image, blurring_image, jax_method='fft'): + def convolve_image(self, image, blurring_image, jax_method="fft"): """ For a given 1D array and blurring array, convolve the two using this convolver. @@ -371,14 +366,10 @@ def exception_message(): self.blurring_mask is None, lambda _: jax.debug.callback(exception_message), lambda _: None, - None + None, ) - return self.jax_convolve( - image, - blurring_image, - method=jax_method - ) + return self.jax_convolve(image, blurring_image, method=jax_method) else: if self.blurring_mask is None: diff --git a/autoarray/operators/over_sampling/over_sample_util.py b/autoarray/operators/over_sampling/over_sample_util.py index 1a0ae429..d539051a 100644 --- a/autoarray/operators/over_sampling/over_sample_util.py +++ b/autoarray/operators/over_sampling/over_sample_util.py @@ -219,9 +219,9 @@ def sub_slim_index_for_sub_native_index_from(sub_mask_2d: np.ndarray): for sub_mask_y in range(sub_mask_2d.shape[0]): for sub_mask_x in range(sub_mask_2d.shape[1]): if sub_mask_2d[sub_mask_y, sub_mask_x] == False: - sub_slim_index_for_sub_native_index[ - sub_mask_y, sub_mask_x - ] = sub_mask_1d_index + sub_slim_index_for_sub_native_index[sub_mask_y, sub_mask_x] = ( + sub_mask_1d_index + ) sub_mask_1d_index += 1 return sub_slim_index_for_sub_native_index @@ -407,18 +407,32 @@ def grid_2d_slim_over_sampled_via_mask_from( for x1 in range(sub): if use_jax: # while this makes it run, it is very, very slow - grid_slim = grid_slim.at[sub_index, 0].set(-( - y_scaled - y_sub_half + y1 * y_sub_step + (y_sub_step / 2.0) - )) + grid_slim = grid_slim.at[sub_index, 0].set( + -( + y_scaled + - y_sub_half + + y1 * y_sub_step + + (y_sub_step / 2.0) + ) + ) grid_slim = grid_slim.at[sub_index, 1].set( - x_scaled - x_sub_half + x1 * x_sub_step + (x_sub_step / 2.0) + x_scaled + - x_sub_half + + x1 * x_sub_step + + (x_sub_step / 2.0) ) else: grid_slim[sub_index, 0] = -( - y_scaled - y_sub_half + y1 * y_sub_step + (y_sub_step / 2.0) + y_scaled + - y_sub_half + + y1 * y_sub_step + + (y_sub_step / 2.0) ) grid_slim[sub_index, 1] = ( - x_scaled - x_sub_half + x1 * x_sub_step + (x_sub_step / 2.0) + x_scaled + - x_sub_half + + x1 * x_sub_step + + (x_sub_step / 2.0) ) sub_index += 1 diff --git a/autoarray/plot/wrap/base/colorbar.py b/autoarray/plot/wrap/base/colorbar.py index 272d93fc..b0650013 100644 --- a/autoarray/plot/wrap/base/colorbar.py +++ b/autoarray/plot/wrap/base/colorbar.py @@ -130,9 +130,9 @@ def tick_labels_from( cb_unit = units.colorbar_label middle_index = (len(manual_tick_labels) - 1) // 2 - manual_tick_labels[ - middle_index - ] = rf"{manual_tick_labels[middle_index]}{cb_unit}" + manual_tick_labels[middle_index] = ( + rf"{manual_tick_labels[middle_index]}{cb_unit}" + ) return manual_tick_labels diff --git a/autoarray/preloads.py b/autoarray/preloads.py deleted file mode 100644 index bb9c7d28..00000000 --- a/autoarray/preloads.py +++ /dev/null @@ -1,565 +0,0 @@ -import logging -import numpy as np -import os -from typing import List - -from autoconf import conf - -from autoarray.inversion.linear_obj.func_list import AbstractLinearObjFuncList -from autoarray.inversion.pixelization.mappers.abstract import AbstractMapper - -from autoarray import exc -from autoarray.inversion.inversion.imaging import inversion_imaging_util - -logger = logging.getLogger(__name__) - -logger.setLevel(level="INFO") - - -class Preloads: - def __init__( - self, - w_tilde=None, - use_w_tilde=None, - image_plane_mesh_grid_pg_list=None, - relocated_grid=None, - mapper_list=None, - operated_mapping_matrix=None, - linear_func_operated_mapping_matrix_dict=None, - data_linear_func_matrix_dict=None, - mapper_operated_mapping_matrix_dict=None, - curvature_matrix=None, - data_vector_mapper=None, - curvature_matrix_mapper_diag=None, - regularization_matrix=None, - log_det_regularization_matrix_term=None, - traced_mesh_grids_list_of_planes=None, - image_plane_mesh_grid_list=None, - ): - self.w_tilde = w_tilde - self.use_w_tilde = use_w_tilde - - self.image_plane_mesh_grid_pg_list = image_plane_mesh_grid_pg_list - self.relocated_grid = relocated_grid - self.mapper_list = mapper_list - self.operated_mapping_matrix = operated_mapping_matrix - self.linear_func_operated_mapping_matrix_dict = ( - linear_func_operated_mapping_matrix_dict - ) - self.data_linear_func_matrix_dict = data_linear_func_matrix_dict - self.mapper_operated_mapping_matrix_dict = mapper_operated_mapping_matrix_dict - self.curvature_matrix = curvature_matrix - self.data_vector_mapper = data_vector_mapper - self.curvature_matrix_mapper_diag = curvature_matrix_mapper_diag - self.regularization_matrix = regularization_matrix - self.log_det_regularization_matrix_term = log_det_regularization_matrix_term - - self.traced_mesh_grids_list_of_planes = traced_mesh_grids_list_of_planes - self.image_plane_mesh_grid_list = image_plane_mesh_grid_list - - @property - def check_threshold(self): - return conf.instance["general"]["test"]["preloads_check_threshold"] - - def set_w_tilde_imaging(self, fit_0, fit_1): - """ - The w-tilde linear algebra formalism speeds up inversions by computing beforehand quantities that enable - efficiently construction of the curvature matrix. These quantities can only be used if the noise-map is - fixed, therefore this function preloads these w-tilde quantities if the noise-map does not change. - - This function compares the noise map of two fit's corresponding to two model instances, and preloads wtilde - if the noise maps of both fits are the same. - - The preload is typically used through search chaining pipelines, as it is uncommon for the noise map to be - scaled during the model-fit (although it is common for a fixed but scaled noise map to be used). - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - self.w_tilde = None - self.use_w_tilde = False - - if fit_0.inversion is None: - return - - if not fit_0.inversion.has(cls=AbstractMapper): - return - - if np.max(abs(fit_0.noise_map - fit_1.noise_map)) < 1e-8: - logger.info("PRELOADS - Computing W-Tilde... May take a moment.") - - from autoarray.dataset.imaging.w_tilde import WTildeImaging - - ( - preload, - indexes, - lengths, - ) = inversion_imaging_util.w_tilde_curvature_preload_imaging_from( - 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( - curvature_preload=preload, - indexes=indexes.astype("int"), - lengths=lengths.astype("int"), - noise_map_value=fit_0.noise_map[0], - ) - - self.use_w_tilde = True - - logger.info("PRELOADS - W-Tilde preloaded for this model-fit.") - - def set_relocated_grid(self, fit_0, fit_1): - """ - If the `MassProfile`'s in a model are fixed their traced grids (which may have had coordinates relocated at - the border) does not change during the model=fit and can therefore be preloaded. - - This function compares the relocated grids of the mappers of two fit corresponding to two model instances, and - preloads the grid if the grids of both fits are the same. - - The preload is typically used in adapt searches, where the mass model is fixed and the parameters are - varied. - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - self.relocated_grid = None - - if fit_0.inversion is None: - return - - if ( - fit_0.inversion.total(cls=AbstractMapper) > 1 - or fit_0.inversion.total(cls=AbstractMapper) == 0 - ): - return - - mapper_0 = fit_0.inversion.cls_list_from(cls=AbstractMapper)[0] - mapper_1 = fit_1.inversion.cls_list_from(cls=AbstractMapper)[0] - - if ( - mapper_0.source_plane_data_grid.shape[0] - == mapper_1.source_plane_data_grid.shape[0] - ): - if ( - np.max( - abs( - mapper_0.source_plane_data_grid - - mapper_1.source_plane_data_grid - ) - ) - < 1.0e-8 - ): - self.relocated_grid = mapper_0.source_plane_data_grid - - logger.info( - "PRELOADS - Relocated grid of pxielization preloaded for this model-fit." - ) - - def set_mapper_list(self, fit_0, fit_1): - """ - If the `MassProfile`'s and `Mesh`'s in a model are fixed, the mapping of image-pixels to the - source-pixels does not change during the model-fit and the list of `Mapper`'s containing this information can - be preloaded. This includes preloading the `mapping_matrix`. - - This function compares the mapping matrix of two fit's corresponding to two model instances, and preloads the - list of mappers if the mapping matrix of both fits are the same. - - The preload is typically used in searches where only light profiles vary (e.g. when only the lens's light is - being fitted for). - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - self.mapper_list = None - - if fit_0.inversion is None: - return - - if fit_0.inversion.total(cls=AbstractMapper) == 0: - return - - from autoarray.inversion.inversion.interferometer.lop import ( - InversionInterferometerMappingPyLops, - ) - - if isinstance(fit_0.inversion, InversionInterferometerMappingPyLops): - return - - inversion_0 = fit_0.inversion - inversion_1 = fit_1.inversion - - if inversion_0.mapping_matrix.shape[1] == inversion_1.mapping_matrix.shape[1]: - if np.allclose(inversion_0.mapping_matrix, inversion_1.mapping_matrix): - self.mapper_list = inversion_0.cls_list_from(cls=AbstractMapper) - - logger.info( - "PRELOADS - Mappers of planes preloaded for this model-fit." - ) - - def set_operated_mapping_matrix_with_preloads(self, fit_0, fit_1): - """ - If the `MassProfile`'s and `Mesh`'s in a model are fixed, the mapping of image-pixels to the - source-pixels does not change during the model-fit and matrices used to perform the linear algebra in an - inversion can be preloaded, which help efficiently construct the curvature matrix. - - This function compares the operated mapping matrix of two fit's corresponding to two model instances, and - preloads the mapper if the mapping matrix of both fits are the same. - - The preload is typically used in searches where only light profiles vary (e.g. when only the lens's light is - being fitted for). - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - self.operated_mapping_matrix = None - - from autoarray.inversion.inversion.interferometer.lop import ( - InversionInterferometerMappingPyLops, - ) - - if isinstance(fit_0.inversion, InversionInterferometerMappingPyLops): - return - - inversion_0 = fit_0.inversion - inversion_1 = fit_1.inversion - - if inversion_0 is None: - return - - if ( - inversion_0.operated_mapping_matrix.shape[1] - == inversion_1.operated_mapping_matrix.shape[1] - ): - if ( - np.max( - abs( - inversion_0.operated_mapping_matrix - - inversion_1.operated_mapping_matrix - ) - ) - < 1e-8 - ): - self.operated_mapping_matrix = inversion_0.operated_mapping_matrix - - logger.info( - "PRELOADS - Inversion linear algebra quantities preloaded for this model-fit." - ) - - def set_linear_func_inversion_dicts(self, fit_0, fit_1): - """ - If the `MassProfile`'s and `Mesh`'s in a model are fixed, the mapping of image-pixels to the - source-pixels does not change during the model-fit and matrices used to perform the linear algebra in an - inversion can be preloaded, which help efficiently construct the curvature matrix. - - This function compares the operated mapping matrix of two fit's corresponding to two model instances, and - preloads the mapper if the mapping matrix of both fits are the same. - - The preload is typically used in searches where only light profiles vary (e.g. when only the lens's light is - being fitted for). - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - from autoarray.inversion.pixelization.pixelization import Pixelization - - self.linear_func_operated_mapping_matrix_dict = None - - inversion_0 = fit_0.inversion - inversion_1 = fit_1.inversion - - if inversion_0 is None: - return - - if not inversion_0.has(cls=AbstractMapper): - return - - if not inversion_0.has(cls=AbstractLinearObjFuncList): - return - - try: - inversion_0.linear_func_operated_mapping_matrix_dict - except NotImplementedError: - return - - if not hasattr(inversion_0, "linear_func_operated_mapping_matrix_dict"): - return - - should_preload = False - - for operated_mapping_matrix_0, operated_mapping_matrix_1 in zip( - inversion_0.linear_func_operated_mapping_matrix_dict.values(), - inversion_1.linear_func_operated_mapping_matrix_dict.values(), - ): - if ( - np.max(abs(operated_mapping_matrix_0 - operated_mapping_matrix_1)) - < 1e-8 - ): - should_preload = True - - if should_preload: - self.linear_func_operated_mapping_matrix_dict = ( - inversion_0.linear_func_operated_mapping_matrix_dict - ) - self.data_linear_func_matrix_dict = inversion_0.data_linear_func_matrix_dict - - logger.info( - "PRELOADS - Inversion linear light profile operated mapping matrix / data linear func matrix preloaded for this model-fit." - ) - - def set_curvature_matrix(self, fit_0, fit_1): - """ - If the `MassProfile`'s and `Mesh`'s in a model are fixed, the mapping of image-pixels to the - source-pixels does not change during the model-fit and therefore its associated curvature matrix is also - fixed, meaning the curvature matrix preloaded. - - If linear ``LightProfiles``'s are included, the regions of the curvature matrix associatd with these - objects vary but the diagonals of the mapper do not change. In this case, the `curvature_matrix_mapper_diag` - is preloaded. - - This function compares the curvature matrix of two fit's corresponding to two model instances, and preloads - this value if it is the same for both fits. - - The preload is typically used in **PyAutoGalaxy** inversions using a `Rectangular` pixelization. - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - - self.curvature_matrix = None - self.data_vector_mapper = None - self.curvature_matrix_mapper_diag = None - self.mapper_operated_mapping_matrix_dict = None - - inversion_0 = fit_0.inversion - inversion_1 = fit_1.inversion - - if inversion_0 is None: - return - - try: - inversion_0._curvature_matrix_mapper_diag - except NotImplementedError: - return - - if inversion_0.curvature_matrix.shape == inversion_1.curvature_matrix.shape: - if ( - np.max(abs(inversion_0.curvature_matrix - inversion_1.curvature_matrix)) - < 1e-8 - ): - self.curvature_matrix = inversion_0.curvature_matrix - - logger.info( - "PRELOADS - Inversion Curvature Matrix preloaded for this model-fit." - ) - - return - - if inversion_0._curvature_matrix_mapper_diag is not None: - if ( - np.max( - abs( - inversion_0._curvature_matrix_mapper_diag - - inversion_1._curvature_matrix_mapper_diag - ) - ) - < 1e-8 - ): - self.mapper_operated_mapping_matrix_dict = ( - inversion_0.mapper_operated_mapping_matrix_dict - ) - self.data_vector_mapper = inversion_0._data_vector_mapper - self.curvature_matrix_mapper_diag = ( - inversion_0._curvature_matrix_mapper_diag - ) - - logger.info( - "PRELOADS - Inversion Curvature Matrix Mapper Diag preloaded for this model-fit." - ) - - def set_regularization_matrix_and_term(self, fit_0, fit_1): - """ - If the `MassProfile`'s and `Mesh`'s in a model are fixed, the mapping of image-pixels to the - source-pixels does not change during the model-fit and therefore its associated regularization matrices are - also fixed, meaning the log determinant of the regularization matrix term of the Bayesian evidence can be - preloaded. - - This function compares the value of the log determinant of the regularization matrix of two fit's corresponding - to two model instances, and preloads this value if it is the same for both fits. - - The preload is typically used in searches where only light profiles vary (e.g. when only the lens's light is - being fitted for). - - Parameters - ---------- - fit_0 - The first fit corresponding to a model with a specific set of unit-values. - fit_1 - The second fit corresponding to a model with a different set of unit-values. - """ - self.regularization_matrix = None - self.log_det_regularization_matrix_term = None - - inversion_0 = fit_0.inversion - inversion_1 = fit_1.inversion - - if inversion_0 is None: - return - - if inversion_0.total(cls=AbstractMapper) == 0: - return - - if ( - abs( - inversion_0.log_det_regularization_matrix_term - - inversion_1.log_det_regularization_matrix_term - ) - < 1e-8 - ): - self.regularization_matrix = inversion_0.regularization_matrix - self.log_det_regularization_matrix_term = ( - inversion_0.log_det_regularization_matrix_term - ) - - logger.info( - "PRELOADS - Inversion Log Det Regularization Matrix Term preloaded for this model-fit." - ) - - def check_via_fit(self, fit): - import copy - - settings_inversion = copy.deepcopy(fit.settings_inversion) - - fit_with_preloads = fit.refit_with_new_preloads( - preloads=self, settings_inversion=settings_inversion - ) - - fit_without_preloads = fit.refit_with_new_preloads( - preloads=self.__class__(use_w_tilde=False), - settings_inversion=settings_inversion, - ) - - if os.environ.get("PYAUTOFIT_TEST_MODE") == "1": - return - - try: - if ( - abs( - fit_with_preloads.figure_of_merit - - fit_without_preloads.figure_of_merit - ) - > self.check_threshold - ): - raise exc.PreloadsException( - f""" - The log likelihood of fits using and not using preloads are not consistent by a value larger than - the preloads check threshold of {self.check_threshold}, indicating preloading has gone wrong. - - The likelihood values are: - - With Preloads: {fit_with_preloads.figure_of_merit} - Without Preloads: {fit_without_preloads.figure_of_merit} - - Double check that the model-fit is set up correctly and that the preloads are being used correctly. - - This exception can be turned off by setting the general.yaml -> test -> check_preloads to False - in the config files. However, care should be taken when doing this. - """ - ) - - except exc.InversionException: - data_vector_difference = np.max( - np.abs( - fit_with_preloads.inversion.data_vector - - fit_without_preloads.inversion.data_vector - ) - ) - - if data_vector_difference > 1.0e-4: - raise exc.PreloadsException( - f""" - The data vectors of fits using and not using preloads are not consistent, indicating - preloading has gone wrong. - - The maximum value a data vector absolute value difference is: {data_vector_difference} - """ - ) - - curvature_reg_matrix_difference = np.max( - np.abs( - fit_with_preloads.inversion.curvature_reg_matrix - - fit_without_preloads.inversion.curvature_reg_matrix - ) - ) - - if curvature_reg_matrix_difference > 1.0e-4: - raise exc.PreloadsException( - f""" - The curvature matrices of fits using and not using preloads are not consistent, indicating - preloading has gone wrong. - - The maximum value of a curvature matrix absolute value difference is: {curvature_reg_matrix_difference} - """ - ) - - @property - def info(self) -> List[str]: - """ - The information on what has or has not been preloaded, which is written to the file `preloads.summary`. - - Returns - ------- - A list of strings containing statements on what has or has not been preloaded. - """ - line = [f"W Tilde = {self.w_tilde is not None}\n"] - line += [f"Relocated Grid = {self.relocated_grid is not None}\n"] - line += [f"Mapper = {self.mapper_list is not None}\n"] - line += [ - f"Blurred Mapping Matrix = {self.operated_mapping_matrix is not None}\n" - ] - line += [ - f"Inversion Linear Func (Linear Light Profile) Dicts = {self.linear_func_operated_mapping_matrix_dict is not None}\n" - ] - line += [f"Curvature Matrix = {self.curvature_matrix is not None}\n"] - line += [ - f"Curvature Matrix Mapper Diag = {self.curvature_matrix_mapper_diag is not None}\n" - ] - line += [f"Regularization Matrix = {self.regularization_matrix is not None}\n"] - line += [ - f"Log Det Regularization Matrix Term = {self.log_det_regularization_matrix_term is not None}\n" - ] - - return line diff --git a/autoarray/structures/grids/grid_2d_util.py b/autoarray/structures/grids/grid_2d_util.py index e3eb09c7..d81c405b 100644 --- a/autoarray/structures/grids/grid_2d_util.py +++ b/autoarray/structures/grids/grid_2d_util.py @@ -57,6 +57,7 @@ def check_grid_2d(grid_2d: np.ndarray): def check_grid_2d_and_mask_2d(grid_2d: np.ndarray, mask_2d: Mask2D): if len(grid_2d.shape) == 2: + def exception_message(): raise exc.GridException( f""" @@ -68,17 +69,19 @@ def exception_message(): The mask number of pixels is {mask_2d.pixels_in_mask}. """ ) + if use_jax: jax.lax.cond( grid_2d.shape[0] != mask_2d.pixels_in_mask, lambda _: jax.debug.callback(exception_message), lambda _: None, - None + None, ) elif grid_2d.shape[0] != mask_2d.pixels_in_mask: exception_message() elif len(grid_2d.shape) == 3: + def exception_message(): raise exc.GridException( f""" @@ -89,12 +92,13 @@ def exception_message(): The mask shape_native is {mask_2d.shape_native}. """ ) + if use_jax: jax.lax.cond( (grid_2d.shape[0], grid_2d.shape[1]) != mask_2d.shape_native, lambda _: jax.debug.callback(exception_message), lambda _: None, - None + None, ) elif (grid_2d.shape[0], grid_2d.shape[1]) != mask_2d.shape_native: exception_message() @@ -283,8 +287,12 @@ def grid_2d_slim_via_mask_from( for x in range(mask_2d.shape[1]): if not mask_2d[y, x]: if use_jax: - grid_slim = grid_slim.at[index, 0].set(-(y - centres_scaled[0]) * pixel_scales[0]) - grid_slim = grid_slim.at[index, 1].set((x - centres_scaled[1]) * pixel_scales[1]) + grid_slim = grid_slim.at[index, 0].set( + -(y - centres_scaled[0]) * pixel_scales[0] + ) + grid_slim = grid_slim.at[index, 1].set( + (x - centres_scaled[1]) * pixel_scales[1] + ) else: grid_slim[index, 0] = -(y - centres_scaled[0]) * pixel_scales[0] grid_slim[index, 1] = (x - centres_scaled[1]) * pixel_scales[1] @@ -786,9 +794,7 @@ def grid_2d_slim_upscaled_from( The pixel scale of the uniform grid that laid over the irregular grid of (y,x) coordinates. """ - grid_2d_slim_upscaled = np.zeros( - shape=(grid_slim.shape[0] * upscale_factor**2, 2) - ) + grid_2d_slim_upscaled = np.zeros(shape=(grid_slim.shape[0] * upscale_factor**2, 2)) upscale_index = 0 diff --git a/autoarray/structures/grids/uniform_2d.py b/autoarray/structures/grids/uniform_2d.py index 3ff0f5c8..9522326f 100644 --- a/autoarray/structures/grids/uniform_2d.py +++ b/autoarray/structures/grids/uniform_2d.py @@ -845,10 +845,10 @@ def distances_to_coordinate_from( coordinate The (y,x) coordinate from which the distance of every grid (y,x) coordinate is computed. """ - squared_distance = self.squared_distances_to_coordinate_from(coordinate=coordinate) - distances = np.sqrt( - squared_distance.array + squared_distance = self.squared_distances_to_coordinate_from( + coordinate=coordinate ) + distances = np.sqrt(squared_distance.array) return Array2D(values=distances, mask=self.mask) def grid_2d_radial_projected_shape_slim_from( diff --git a/test_autoarray/config/general.yaml b/test_autoarray/config/general.yaml index 73b76904..6f331d14 100644 --- a/test_autoarray/config/general.yaml +++ b/test_autoarray/config/general.yaml @@ -1,6 +1,5 @@ analysis: n_cores: 1 - preload_attempts: 250 fits: flip_for_ds9: false grid: @@ -36,6 +35,4 @@ structures: native_binned_only: false # If True, data structures are only stored in their native and binned format. This is used to reduce memory usage in autocti. test: check_likelihood_function: true # if True, when a search is resumed the likelihood of a previous sample is recalculated to ensure it is consistent with the previous run. - check_preloads: false - exception_override: false - preloads_check_threshold: 1.0 # If the figure of merit of a fit with and without preloads is greater than this threshold, the check preload test fails and an exception raised for a model-fit. + exception_override: false \ No newline at end of file diff --git a/test_autoarray/config/visualize.yaml b/test_autoarray/config/visualize.yaml index 568a1134..8934bb46 100644 --- a/test_autoarray/config/visualize.yaml +++ b/test_autoarray/config/visualize.yaml @@ -5,7 +5,7 @@ general: zoom_around_mask: true disable_zoom_for_fits: true # If True, the zoom-in around the masked region is disabled when outputting .fits files, which is useful to retain the same dimensions as the input data. include_2d: - border: true + border: false mapper_image_plane_mesh_grid: false mapper_source_plane_data_grid: false mapper_source_plane_mesh_grid: false @@ -33,7 +33,7 @@ include: mask: false origin: false include_2d: - border: true + border: false mapper_image_plane_mesh_grid: false mapper_source_plane_data_grid: false mapper_source_plane_mesh_grid: false diff --git a/test_autoarray/inversion/inversion/test_abstract.py b/test_autoarray/inversion/inversion/test_abstract.py index 1db88cf9..149e73b9 100644 --- a/test_autoarray/inversion/inversion/test_abstract.py +++ b/test_autoarray/inversion/inversion/test_abstract.py @@ -319,61 +319,6 @@ def test__regularization_matrix(): assert inversion.regularization_matrix == pytest.approx(regularization_matrix) -def test__preloads__operated_mapping_matrix(): - operated_mapping_matrix = 2.0 * np.ones((9, 3)) - - preloads = aa.Preloads( - operated_mapping_matrix=operated_mapping_matrix, - ) - - # noinspection PyTypeChecker - inversion = aa.m.MockInversionImaging( - noise_map=np.ones(9), linear_obj_list=aa.m.MockMapper(), preloads=preloads - ) - - assert inversion.operated_mapping_matrix[0, 0] == 2.0 - - -def test__linear_func_operated_mapping_matrix_dict(): - dict_0 = {"key0": np.array([1.0, 2.0])} - - preloads = aa.Preloads(linear_func_operated_mapping_matrix_dict=dict_0) - - # noinspection PyTypeChecker - inversion = aa.m.MockInversionImagingWTilde( - noise_map=np.ones(9), - linear_obj_list=[aa.m.MockLinearObjFuncList()], - preloads=preloads, - ) - - assert list(inversion.linear_func_operated_mapping_matrix_dict.values())[ - 0 - ] == pytest.approx(dict_0["key0"], 1.0e-4) - - -def test__curvature_matrix_mapper_diag_preload(): - curvature_matrix_mapper_diag = 2.0 * np.ones((9, 3)) - - preloads = aa.Preloads(curvature_matrix_mapper_diag=curvature_matrix_mapper_diag) - - # noinspection PyTypeChecker - inversion = aa.m.MockInversionImagingWTilde( - noise_map=np.ones(9), linear_obj_list=aa.m.MockMapper(), preloads=preloads - ) - - assert inversion._curvature_matrix_mapper_diag == pytest.approx( - curvature_matrix_mapper_diag, 1.0e-4 - ) - - -def test__preload_of_regularization_matrix__overwrites_calculation(): - inversion = aa.m.MockInversion( - preloads=aa.Preloads(regularization_matrix=np.ones((2, 2))) - ) - - assert (inversion.regularization_matrix == np.ones((2, 2))).all() - - def test__reconstruction_reduced(): linear_obj_list = [ aa.m.MockLinearObj(parameters=2, regularization=aa.m.MockRegularization()), @@ -592,17 +537,6 @@ def test__regularization_term(): assert inversion.regularization_term == 34.0 -def test__preload_of_log_det_regularization_term_overwrites_calculation(): - inversion = aa.m.MockInversion( - linear_obj_list=[ - aa.m.MockLinearObj(parameters=3, regularization=aa.m.MockRegularization()) - ], - preloads=aa.Preloads(log_det_regularization_matrix_term=1.0), - ) - - assert inversion.log_det_regularization_matrix_term == 1.0 - - def test__determinant_of_positive_definite_matrix_via_cholesky(): matrix = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) diff --git a/test_autoarray/inversion/inversion/test_factory.py b/test_autoarray/inversion/inversion/test_factory.py index c557adf5..79ea5602 100644 --- a/test_autoarray/inversion/inversion/test_factory.py +++ b/test_autoarray/inversion/inversion/test_factory.py @@ -426,143 +426,6 @@ def test__inversion_imaging__linear_obj_func_with_w_tilde( ) -def test__inversion_imaging__linear_obj_func_with_w_tilde__include_preload_data_linear_func_matrix( - masked_imaging_7x7, - rectangular_mapper_7x7_3x3, - delaunay_mapper_9_3x3, -): - masked_imaging_7x7 = copy.copy(masked_imaging_7x7) - masked_imaging_7x7.data[4] = 2.0 - masked_imaging_7x7.noise_map[3] = 4.0 - masked_imaging_7x7.psf[0] = 0.1 - masked_imaging_7x7.psf[4] = 0.9 - - mask = masked_imaging_7x7.mask - - grid = aa.Grid2D.from_mask(mask=mask) - - mapping_matrix = np.full(fill_value=0.5, shape=(9, 2)) - mapping_matrix[0, 0] = 0.8 - mapping_matrix[1, 1] = 0.4 - - linear_obj = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - linear_obj_1 = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - linear_obj_2 = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - inversion_mapping = aa.Inversion( - dataset=masked_imaging_7x7, - linear_obj_list=[ - rectangular_mapper_7x7_3x3, - linear_obj, - delaunay_mapper_9_3x3, - linear_obj_1, - linear_obj_2, - ], - settings=aa.SettingsInversion(use_w_tilde=False), - ) - - preloads = aa.Preloads( - data_linear_func_matrix_dict=inversion_mapping.data_linear_func_matrix_dict - ) - - inversion_w_tilde = aa.Inversion( - dataset=masked_imaging_7x7, - linear_obj_list=[ - rectangular_mapper_7x7_3x3, - linear_obj, - delaunay_mapper_9_3x3, - linear_obj_1, - linear_obj_2, - ], - preloads=preloads, - settings=aa.SettingsInversion(use_w_tilde=True), - ) - - assert inversion_mapping.data_vector == pytest.approx( - inversion_w_tilde.data_vector, 1.0e-4 - ) - assert inversion_mapping.curvature_matrix == pytest.approx( - inversion_w_tilde.curvature_matrix, 1.0e-4 - ) - - -def test__inversion_imaging__linear_obj_func_with_w_tilde__include_preload_mapper_operated_mapping_matrix_dict( - masked_imaging_7x7, - rectangular_mapper_7x7_3x3, - delaunay_mapper_9_3x3, -): - masked_imaging_7x7 = copy.copy(masked_imaging_7x7) - masked_imaging_7x7.data[4] = 2.0 - masked_imaging_7x7.noise_map[3] = 4.0 - masked_imaging_7x7.psf[0] = 0.1 - masked_imaging_7x7.psf[4] = 0.9 - - mask = masked_imaging_7x7.mask - - grid = aa.Grid2D.from_mask(mask=mask) - - mapping_matrix = np.full(fill_value=0.5, shape=(9, 2)) - mapping_matrix[0, 0] = 0.8 - mapping_matrix[1, 1] = 0.4 - - linear_obj = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - linear_obj_1 = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - linear_obj_2 = aa.m.MockLinearObjFuncList( - parameters=2, grid=grid, mapping_matrix=mapping_matrix - ) - - inversion_mapping = aa.Inversion( - dataset=masked_imaging_7x7, - linear_obj_list=[ - rectangular_mapper_7x7_3x3, - linear_obj, - delaunay_mapper_9_3x3, - linear_obj_1, - linear_obj_2, - ], - settings=aa.SettingsInversion(use_w_tilde=False), - ) - - preloads = aa.Preloads( - mapper_operated_mapping_matrix_dict=inversion_mapping.mapper_operated_mapping_matrix_dict - ) - - inversion_w_tilde = aa.Inversion( - dataset=masked_imaging_7x7, - linear_obj_list=[ - rectangular_mapper_7x7_3x3, - linear_obj, - delaunay_mapper_9_3x3, - linear_obj_1, - linear_obj_2, - ], - preloads=preloads, - settings=aa.SettingsInversion(use_w_tilde=True), - ) - - assert inversion_mapping.data_vector == pytest.approx( - inversion_w_tilde.data_vector, 1.0e-4 - ) - - assert inversion_mapping.curvature_matrix == pytest.approx( - inversion_w_tilde.curvature_matrix, 1.0e-4 - ) - - def test__inversion_interferometer__via_mapper( interferometer_7_no_fft, rectangular_mapper_7x7_3x3, diff --git a/test_autoarray/inversion/pixelization/mesh/test_rectangular.py b/test_autoarray/inversion/pixelization/mesh/test_rectangular.py deleted file mode 100644 index aa18b5ec..00000000 --- a/test_autoarray/inversion/pixelization/mesh/test_rectangular.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest - -import autoarray as aa - - -def test__preloads_used_for_relocated_grid(mask_2d_7x7): - mesh = aa.mesh.Rectangular(shape=(3, 3)) - - relocated_grid = aa.Grid2D.uniform(shape_native=(3, 3), pixel_scales=1.0) - - border_relocator = aa.BorderRelocator(mask=mask_2d_7x7, sub_size=1) - - mapper_grids = mesh.mapper_grids_from( - mask=mask_2d_7x7, - border_relocator=border_relocator, - source_plane_data_grid=relocated_grid, - source_plane_mesh_grid=None, - preloads=aa.Preloads(relocated_grid=relocated_grid), - ) - - assert mapper_grids.source_plane_data_grid == pytest.approx(relocated_grid, 1.0e-4) diff --git a/test_autoarray/inversion/pixelization/mesh/test_triangulation.py b/test_autoarray/inversion/pixelization/mesh/test_triangulation.py deleted file mode 100644 index eaf8ca5c..00000000 --- a/test_autoarray/inversion/pixelization/mesh/test_triangulation.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest - -import autoarray as aa - - -def test___preloads_used_for_relocated_grid(mask_2d_7x7): - mesh = aa.mesh.Delaunay() - - relocated_grid = aa.Grid2D.uniform(shape_native=(3, 3), pixel_scales=1.0) - - border_relocator = aa.BorderRelocator(mask=mask_2d_7x7, sub_size=1) - - mapper_grids = mesh.mapper_grids_from( - mask=mask_2d_7x7, - border_relocator=border_relocator, - source_plane_data_grid=relocated_grid, - source_plane_mesh_grid=relocated_grid, - preloads=aa.Preloads(relocated_grid=relocated_grid), - ) - - assert mapper_grids.source_plane_data_grid == pytest.approx(relocated_grid, 1.0e-4) diff --git a/test_autoarray/test_preloads.py b/test_autoarray/test_preloads.py deleted file mode 100644 index 9a0ddee1..00000000 --- a/test_autoarray/test_preloads.py +++ /dev/null @@ -1,518 +0,0 @@ -import numpy as np - -import autoarray as aa - - -def test__set_w_tilde(): - # fit inversion is None, so no need to bother with w_tilde. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads(w_tilde=1, use_w_tilde=1) - preloads.set_w_tilde_imaging(fit_0=fit_0, fit_1=fit_1) - - assert preloads.w_tilde is None - assert preloads.use_w_tilde is False - - # Noise maps of fit are different but there is an inversion, so we should not preload w_tilde and use w_tilde. - - inversion = aa.m.MockInversion(linear_obj_list=[aa.m.MockMapper()]) - - fit_0 = aa.m.MockFitImaging( - inversion=inversion, - noise_map=aa.Array2D.zeros(shape_native=(3, 1), pixel_scales=0.1), - ) - fit_1 = aa.m.MockFitImaging( - inversion=inversion, - noise_map=aa.Array2D.ones(shape_native=(3, 1), pixel_scales=0.1), - ) - - preloads = aa.Preloads(w_tilde=1, use_w_tilde=1) - preloads.set_w_tilde_imaging(fit_0=fit_0, fit_1=fit_1) - - assert preloads.w_tilde is None - assert preloads.use_w_tilde is False - - # Noise maps of fits are the same so preload w_tilde and use it. - - noise_map = aa.Array2D.ones(shape_native=(5, 5), pixel_scales=0.1) - - mask = aa.Mask2D( - mask=np.array( - [ - [True, True, True, True, True], - [True, False, False, False, True], - [True, False, False, False, True], - [True, False, False, False, True], - [True, True, True, True, True], - ] - ), - pixel_scales=noise_map.pixel_scales, - ) - - dataset = aa.m.MockDataset(psf=aa.Kernel2D.no_blur(pixel_scales=1.0), mask=mask) - - fit_0 = aa.m.MockFitImaging( - inversion=inversion, dataset=dataset, noise_map=noise_map - ) - fit_1 = aa.m.MockFitImaging( - inversion=inversion, dataset=dataset, noise_map=noise_map - ) - - preloads = aa.Preloads(w_tilde=1, use_w_tilde=1) - preloads.set_w_tilde_imaging(fit_0=fit_0, fit_1=fit_1) - - ( - curvature_preload, - indexes, - lengths, - ) = aa.util.inversion_imaging.w_tilde_curvature_preload_imaging_from( - 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] - assert preloads.w_tilde.indexes[0] == indexes[0] - assert preloads.w_tilde.lengths[0] == lengths[0] - assert preloads.w_tilde.noise_map_value == 1.0 - assert preloads.use_w_tilde == True - - -def test__set_relocated_grid(): - # Inversion is None so there is no mapper, thus preload mapper to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads(relocated_grid=1) - preloads.set_relocated_grid(fit_0=fit_0, fit_1=fit_1) - - assert preloads.relocated_grid is None - - # Mapper's mapping matrices are different, thus preload mapper to None. - - inversion_0 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(source_plane_data_grid=np.ones((3, 2)))] - ) - inversion_1 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(source_plane_data_grid=2.0 * np.ones((3, 2)))] - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(relocated_grid=1) - preloads.set_relocated_grid(fit_0=fit_0, fit_1=fit_1) - - assert preloads.relocated_grid is None - - # Mapper's mapping matrices are the same, thus preload mapper. - - inversion_0 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(source_plane_data_grid=np.ones((3, 2)))] - ) - inversion_1 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(source_plane_data_grid=np.ones((3, 2)))] - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(relocated_grid=1) - preloads.set_relocated_grid(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.relocated_grid == np.ones((3, 2))).all() - - -def test__set_mapper_list(): - # Inversion is None so there is no mapper, thus preload mapper to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads(mapper_list=1) - preloads.set_mapper_list(fit_0=fit_0, fit_1=fit_1) - - assert preloads.mapper_list is None - - # Mapper's mapping matrices are different, thus preload mapper to None. - - inversion_0 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(mapping_matrix=np.ones((3, 2)))] - ) - inversion_1 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(mapping_matrix=2.0 * np.ones((3, 2)))] - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(mapper_list=1) - preloads.set_mapper_list(fit_0=fit_0, fit_1=fit_1) - - assert preloads.mapper_list is None - - # Mapper's mapping matrices are the same, thus preload mapper. - - inversion_0 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(mapping_matrix=np.ones((3, 2)))] - ) - inversion_1 = aa.m.MockInversion( - linear_obj_list=[aa.m.MockMapper(mapping_matrix=np.ones((3, 2)))] - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(mapper_list=1) - preloads.set_mapper_list(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.mapper_list[0].mapping_matrix == np.ones((3, 2))).all() - - # Multiple mappers pre inversion still preloads full mapper list. - - inversion_0 = aa.m.MockInversion( - linear_obj_list=[ - aa.m.MockMapper(mapping_matrix=np.ones((3, 2))), - aa.m.MockMapper(mapping_matrix=np.ones((3, 2))), - ] - ) - inversion_1 = aa.m.MockInversion( - linear_obj_list=[ - aa.m.MockMapper(mapping_matrix=np.ones((3, 2))), - aa.m.MockMapper(mapping_matrix=np.ones((3, 2))), - ] - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(mapper_list=1) - preloads.set_mapper_list(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.mapper_list[0].mapping_matrix == np.ones((3, 2))).all() - assert (preloads.mapper_list[1].mapping_matrix == np.ones((3, 2))).all() - - -def test__set_operated_mapping_matrix_with_preloads(): - # Inversion is None thus preload it to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads( - operated_mapping_matrix=1, - ) - preloads.set_operated_mapping_matrix_with_preloads(fit_0=fit_0, fit_1=fit_1) - - assert preloads.operated_mapping_matrix is None - - # Inversion's blurred mapping matrices are different thus no preloading. - - operated_mapping_matrix_0 = np.array( - [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]] - ) - - operated_mapping_matrix_1 = np.array( - [[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]] - ) - - inversion_0 = aa.m.MockInversionImaging( - operated_mapping_matrix=operated_mapping_matrix_0 - ) - inversion_1 = aa.m.MockInversionImaging( - operated_mapping_matrix=operated_mapping_matrix_1 - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads( - operated_mapping_matrix=1, - ) - preloads.set_operated_mapping_matrix_with_preloads(fit_0=fit_0, fit_1=fit_1) - - assert preloads.operated_mapping_matrix is None - - # Inversion's blurred mapping matrices are the same therefore preload it and the curvature sparse terms. - - inversion_0 = aa.m.MockInversionImaging( - operated_mapping_matrix=operated_mapping_matrix_0, - ) - inversion_1 = aa.m.MockInversionImaging( - operated_mapping_matrix=operated_mapping_matrix_0, - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads( - operated_mapping_matrix=1, - ) - preloads.set_operated_mapping_matrix_with_preloads(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.operated_mapping_matrix == operated_mapping_matrix_0).all() - - -def test__set_linear_func_operated_mapping_matrix_dict(): - # Inversion is None thus preload it to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads( - linear_func_operated_mapping_matrix_dict=0, - ) - preloads.set_linear_func_inversion_dicts(fit_0=fit_0, fit_1=fit_1) - - assert preloads.linear_func_operated_mapping_matrix_dict is None - assert preloads.data_linear_func_matrix_dict is None - - # Inversion's blurred mapping matrices are different thus no preloading. - - dict_0 = {"key0": np.array([1.0, 2.0])} - dict_1 = {"key1": np.array([1.0, 3.0])} - - inversion_0 = aa.m.MockInversionImaging( - linear_obj_list=[aa.m.MockLinearObjFuncList()], - linear_func_operated_mapping_matrix_dict=dict_0, - ) - inversion_1 = aa.m.MockInversionImaging( - linear_obj_list=[aa.m.MockLinearObjFuncList()], - linear_func_operated_mapping_matrix_dict=dict_1, - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads() - preloads.set_linear_func_inversion_dicts(fit_0=fit_0, fit_1=fit_1) - - assert preloads.linear_func_operated_mapping_matrix_dict is None - assert preloads.data_linear_func_matrix_dict is None - - # Inversion's blurred mapping matrices are the same therefore preload it and the curvature sparse terms. - - inversion_0 = aa.m.MockInversionImaging( - linear_obj_list=[aa.m.MockLinearObjFuncList(), aa.m.MockMapper()], - linear_func_operated_mapping_matrix_dict=dict_0, - data_linear_func_matrix_dict=dict_0, - ) - inversion_1 = aa.m.MockInversionImaging( - linear_obj_list=[aa.m.MockLinearObjFuncList(), aa.m.MockMapper()], - linear_func_operated_mapping_matrix_dict=dict_0, - data_linear_func_matrix_dict=dict_0, - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads() - preloads.set_linear_func_inversion_dicts(fit_0=fit_0, fit_1=fit_1) - - assert ( - preloads.linear_func_operated_mapping_matrix_dict["key0"] == dict_0["key0"] - ).all() - assert (preloads.data_linear_func_matrix_dict["key0"] == dict_0["key0"]).all() - - -def test__set_curvature_matrix(): - # Inversion is None thus preload curvature_matrix to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads( - curvature_matrix=1, data_vector_mapper=1, curvature_matrix_mapper_diag=1 - ) - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert preloads.curvature_matrix is None - - # Inversion's curvature matrices are different thus no preloading. - - curvature_matrix_0 = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) - - curvature_matrix_1 = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) - - fit_0 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_0, - data_vector_mapper=1, - curvature_matrix_mapper_diag=1, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - fit_1 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_1, - data_vector_mapper=1, - curvature_matrix_mapper_diag=1, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - - preloads = aa.Preloads(curvature_matrix=1) - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert preloads.curvature_matrix is None - - # Inversion's curvature matrices are the same therefore preload it and the curvature sparse terms. - - preloads = aa.Preloads(curvature_matrix=2) - - fit_0 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_0, - data_vector_mapper=1, - curvature_matrix_mapper_diag=1, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - fit_1 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_0, - data_vector_mapper=1, - curvature_matrix_mapper_diag=1, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.curvature_matrix == curvature_matrix_0).all() - - -def test__set_curvature_matrix__curvature_matrix_mapper_diag(): - # Inversion is None thus preload curvature_matrix to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads(data_vector_mapper=0, curvature_matrix_mapper_diag=1) - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert preloads.data_vector_mapper is None - assert preloads.curvature_matrix_mapper_diag is None - assert preloads.mapper_operated_mapping_matrix_dict is None - - # Inversion's curvature matrices are different thus no preloading. - - curvature_matrix_0 = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) - - curvature_matrix_1 = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) - - fit_0 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_0, - curvature_matrix_mapper_diag=curvature_matrix_0, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - fit_1 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_1, - curvature_matrix_mapper_diag=curvature_matrix_1, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - - preloads = aa.Preloads( - data_vector_mapper=0, - curvature_matrix_mapper_diag=1, - mapper_operated_mapping_matrix_dict=2, - ) - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert preloads.data_vector_mapper is None - assert preloads.curvature_matrix_mapper_diag is None - assert preloads.mapper_operated_mapping_matrix_dict is None - - # Inversion's curvature matrices are the same therefore preload it and the curvature sparse terms. - - preloads = aa.Preloads(data_vector_mapper=10, curvature_matrix_mapper_diag=2) - - fit_0 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_0, - data_vector_mapper=0, - curvature_matrix_mapper_diag=curvature_matrix_0, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - fit_1 = aa.m.MockFitImaging( - inversion=aa.m.MockInversion( - curvature_matrix=curvature_matrix_1, - data_vector_mapper=0, - curvature_matrix_mapper_diag=curvature_matrix_0, - mapper_operated_mapping_matrix_dict={"key0": 1}, - ) - ) - - preloads.set_curvature_matrix(fit_0=fit_0, fit_1=fit_1) - - assert preloads.data_vector_mapper == 0 - assert (preloads.curvature_matrix_mapper_diag == curvature_matrix_0).all() - assert preloads.mapper_operated_mapping_matrix_dict == {"key0": 1} - - -def test__set_regularization_matrix_and_term(): - regularization = aa.m.MockRegularization(regularization_matrix=np.eye(2)) - - # Inversion is None thus preload log_det_regularization_matrix_term to None. - - fit_0 = aa.m.MockFitImaging(inversion=None) - fit_1 = aa.m.MockFitImaging(inversion=None) - - preloads = aa.Preloads(log_det_regularization_matrix_term=1) - preloads.set_regularization_matrix_and_term(fit_0=fit_0, fit_1=fit_1) - - assert preloads.regularization_matrix is None - assert preloads.log_det_regularization_matrix_term is None - - # Inversion's log_det_regularization_matrix_term are different thus no preloading. - - inversion_0 = aa.m.MockInversion( - log_det_regularization_matrix_term=0, - linear_obj_list=[aa.m.MockLinearObj(regularization=regularization)], - ) - - inversion_1 = aa.m.MockInversion( - log_det_regularization_matrix_term=1, - linear_obj_list=[aa.m.MockLinearObj(regularization=regularization)], - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads = aa.Preloads(log_det_regularization_matrix_term=1) - preloads.set_regularization_matrix_and_term(fit_0=fit_0, fit_1=fit_1) - - assert preloads.regularization_matrix is None - assert preloads.log_det_regularization_matrix_term is None - - # Inversion's regularization matrix terms are the same therefore preload it and the regularization matrix. - - preloads = aa.Preloads(log_det_regularization_matrix_term=2) - - inversion_0 = aa.m.MockInversion( - log_det_regularization_matrix_term=1, - linear_obj_list=[aa.m.MockMapper(regularization=regularization)], - ) - - inversion_1 = aa.m.MockInversion( - log_det_regularization_matrix_term=1, - linear_obj_list=[aa.m.MockMapper(regularization=regularization)], - ) - - fit_0 = aa.m.MockFitImaging(inversion=inversion_0) - fit_1 = aa.m.MockFitImaging(inversion=inversion_1) - - preloads.set_regularization_matrix_and_term(fit_0=fit_0, fit_1=fit_1) - - assert (preloads.regularization_matrix == np.eye(2)).all() - assert preloads.log_det_regularization_matrix_term == 1