From 2d02f17d12afcc85aa6be9ba5184305752c2d6f9 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Tue, 26 Nov 2024 18:53:47 +0000 Subject: [PATCH 1/5] signal to noise value scasling adding --- autoarray/dataset/imaging/dataset.py | 38 ++++++++++++++----- .../pixelization/mappers/abstract.py | 12 ++++-- .../dataset/imaging/test_dataset.py | 16 ++++++++ 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index c464563d..210a5669 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -340,7 +340,12 @@ def apply_mask(self, mask: Mask2D) -> "Imaging": return dataset - def apply_noise_scaling(self, mask: Mask2D, noise_value: float = 1e8) -> "Imaging": + def apply_noise_scaling( + self, + mask: Mask2D, + noise_value: float = 1e8, + signal_to_noise_value: Optional[float] = None, + ) -> "Imaging": """ Apply a mask to the imaging dataset using noise scaling, whereby the mask increases noise-map values to be extremely large such that they are never included in the likelihood calculation, but it does @@ -358,16 +363,31 @@ def apply_noise_scaling(self, mask: Mask2D, noise_value: float = 1e8) -> "Imagin ---------- mask The 2D mask that is applied to the image and noise-map, to scale the noise-map values to large values. + noise_value + The value that the noise-map values are set to in the masked region where noise scaling is applied. + signal_to_noise_value + The noise-map values are instead set to values such that they give this signal-to-noise_maps ratio. + This overwrites the noise_value parameter. """ - data = np.where(np.invert(mask), 0.0, self.data.native) - data = Array2D.no_mask( - values=data, - shape_native=self.data.shape_native, - pixel_scales=self.data.pixel_scales, - ) - noise_map = self.noise_map.native - noise_map[mask == False] = noise_value + if signal_to_noise_value is None: + data = np.where(np.invert(mask), 0.0, self.data.native) + data = Array2D.no_mask( + values=data, + shape_native=self.data.shape_native, + pixel_scales=self.data.pixel_scales, + ) + noise_map = self.noise_map.native + noise_map[mask == False] = noise_value + + else: + data = self.data.native + noise_map = np.where( + mask == False, + np.abs(data.native) / signal_to_noise_value, + self.noise_map.native, + ) + noise_map = Array2D.no_mask( values=noise_map, shape_native=self.data.shape_native, diff --git a/autoarray/inversion/pixelization/mappers/abstract.py b/autoarray/inversion/pixelization/mappers/abstract.py index 6d610758..dd45b899 100644 --- a/autoarray/inversion/pixelization/mappers/abstract.py +++ b/autoarray/inversion/pixelization/mappers/abstract.py @@ -394,12 +394,16 @@ def mapped_to_source_from(self, array: Array2D) -> np.ndarray: ) def extent_from( - self, values: np.ndarray = None, zoom_to_brightest: bool = True + self, + values: np.ndarray = None, + zoom_to_brightest: bool = True, + zoom_percent: Optional[float] = None, ) -> Tuple[float, float, float, float]: if zoom_to_brightest and values is not None: - zoom_percent = conf.instance["visualize"]["general"]["zoom"][ - "inversion_percent" - ] + if zoom_percent is None: + zoom_percent = conf.instance["visualize"]["general"]["zoom"][ + "inversion_percent" + ] fractional_value = np.max(values) * zoom_percent fractional_bool = values > fractional_value diff --git a/test_autoarray/dataset/imaging/test_dataset.py b/test_autoarray/dataset/imaging/test_dataset.py index 46f6adf4..5fe794b9 100644 --- a/test_autoarray/dataset/imaging/test_dataset.py +++ b/test_autoarray/dataset/imaging/test_dataset.py @@ -1,3 +1,4 @@ +import copy import os from os import path @@ -158,6 +159,21 @@ def test__apply_noise_scaling(imaging_7x7, mask_2d_7x7): assert masked_imaging_7x7.noise_map.native[4, 4] == 1e5 +def test__apply_noise_scaling__use_signal_to_noise_value(imaging_7x7, mask_2d_7x7): + imaging_7x7 = copy.copy(imaging_7x7) + + imaging_7x7.data[24] = 2.0 + + masked_imaging_7x7 = imaging_7x7.apply_noise_scaling( + mask=mask_2d_7x7, signal_to_noise_value=0.1 + ) + + assert masked_imaging_7x7.data.native[3, 4] == 1.0 + assert masked_imaging_7x7.noise_map.native[3, 4] == 10.0 + assert masked_imaging_7x7.data.native[3, 3] == 2.0 + assert masked_imaging_7x7.noise_map.native[3, 3] == 20.0 + + def test__apply_mask__noise_covariance_matrix(): image = aa.Array2D.ones(shape_native=(2, 2), pixel_scales=(1.0, 1.0)) From 838bc7a9e3b1fd4a14550c55772be8b3bc25c74b Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Tue, 26 Nov 2024 19:01:26 +0000 Subject: [PATCH 2/5] add option should_zero_data --- autoarray/dataset/imaging/dataset.py | 38 +++++++++++++------ .../dataset/imaging/test_dataset.py | 2 +- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index 210a5669..74269166 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -345,11 +345,22 @@ def apply_noise_scaling( mask: Mask2D, noise_value: float = 1e8, signal_to_noise_value: Optional[float] = None, + should_zero_data : bool = True ) -> "Imaging": """ - Apply a mask to the imaging dataset using noise scaling, whereby the mask increases noise-map values to be - extremely large such that they are never included in the likelihood calculation, but it does - not remove the image data values, which are set to zero. + Apply a mask to the imaging dataset using noise scaling, whereby the maskmay zero the data and increase + noise-map values to change how they enter the likelihood calculation. + + Given this data region is masked, it is likely thr data itself should not be included and therefore + the masked data values are set to zero. This can be disabled by setting `should_zero_data=False`. + + Two forms of scaling are supported depending on whether the `signal_to_noise_value` is input: + + - `noise_value`: The noise-map values in the masked region are set to this value, typically a very large value, + such that they are never included in the likelihood calculation. + + - `signal_to_noise_value`: The noise-map values in the masked region are set to values such that they give + this signal-to-noise ratio. This overwrites the `noise_value` parameter. For certain modeling tasks, the mask defines regions of the data that are used to calculate the likelihood. For example, all data points in a mask may be used to create a pixel-grid, which is used in the likelihood. @@ -368,20 +379,25 @@ def apply_noise_scaling( signal_to_noise_value The noise-map values are instead set to values such that they give this signal-to-noise_maps ratio. This overwrites the noise_value parameter. + should_zero_data + If True, the data values in the masked region are set to zero. """ - if signal_to_noise_value is None: + if should_zero_data: data = np.where(np.invert(mask), 0.0, self.data.native) - data = Array2D.no_mask( - values=data, - shape_native=self.data.shape_native, - pixel_scales=self.data.pixel_scales, - ) + else: + data = self.data.native + + data = Array2D.no_mask( + values=data, + shape_native=self.data.shape_native, + pixel_scales=self.data.pixel_scales, + ) + + if signal_to_noise_value is None: noise_map = self.noise_map.native noise_map[mask == False] = noise_value - else: - data = self.data.native noise_map = np.where( mask == False, np.abs(data.native) / signal_to_noise_value, diff --git a/test_autoarray/dataset/imaging/test_dataset.py b/test_autoarray/dataset/imaging/test_dataset.py index 5fe794b9..9f43626e 100644 --- a/test_autoarray/dataset/imaging/test_dataset.py +++ b/test_autoarray/dataset/imaging/test_dataset.py @@ -165,7 +165,7 @@ def test__apply_noise_scaling__use_signal_to_noise_value(imaging_7x7, mask_2d_7x imaging_7x7.data[24] = 2.0 masked_imaging_7x7 = imaging_7x7.apply_noise_scaling( - mask=mask_2d_7x7, signal_to_noise_value=0.1 + mask=mask_2d_7x7, signal_to_noise_value=0.1, should_zero_data=False ) assert masked_imaging_7x7.data.native[3, 4] == 1.0 From cfee369c525fdffac88cd07e6bf9ee882fb9050c Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Tue, 26 Nov 2024 19:45:13 +0000 Subject: [PATCH 3/5] move should zero data --- autoarray/dataset/imaging/dataset.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index 74269166..5a01e57f 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -383,6 +383,16 @@ def apply_noise_scaling( If True, the data values in the masked region are set to zero. """ + if signal_to_noise_value is None: + noise_map = self.noise_map.native + noise_map[mask == False] = noise_value + else: + noise_map = np.where( + mask == False, + np.abs(self.data.native) / signal_to_noise_value, + self.noise_map.native, + ) + if should_zero_data: data = np.where(np.invert(mask), 0.0, self.data.native) else: @@ -394,16 +404,6 @@ def apply_noise_scaling( pixel_scales=self.data.pixel_scales, ) - if signal_to_noise_value is None: - noise_map = self.noise_map.native - noise_map[mask == False] = noise_value - else: - noise_map = np.where( - mask == False, - np.abs(data.native) / signal_to_noise_value, - self.noise_map.native, - ) - noise_map = Array2D.no_mask( values=noise_map, shape_native=self.data.shape_native, From cf25d88150ff47cd11f91760a8e547441b937e7f Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Tue, 26 Nov 2024 21:32:49 +0000 Subject: [PATCH 4/5] should zero fix? --- autoarray/dataset/imaging/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index 5a01e57f..5d1675ad 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -389,7 +389,7 @@ def apply_noise_scaling( else: noise_map = np.where( mask == False, - np.abs(self.data.native) / signal_to_noise_value, + np.median(self.data.native[mask == False]) / signal_to_noise_value, self.noise_map.native, ) From 8b07b251023969873027b41ea954cdd9157c98e1 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Tue, 26 Nov 2024 21:33:28 +0000 Subject: [PATCH 5/5] should zero fix? --- autoarray/dataset/imaging/dataset.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index 5d1675ad..0fff9f0f 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -387,9 +387,12 @@ def apply_noise_scaling( noise_map = self.noise_map.native noise_map[mask == False] = noise_value else: + + edge_mask = mask.derive_mask.edge + noise_map = np.where( mask == False, - np.median(self.data.native[mask == False]) / signal_to_noise_value, + np.median(self.data.native[edge_mask == False]) / signal_to_noise_value, self.noise_map.native, )