Skip to content

Commit

Permalink
signal to noise value scasling adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Nov 26, 2024
1 parent ed40f86 commit 2d02f17
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
38 changes: 29 additions & 9 deletions autoarray/dataset/imaging/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions autoarray/inversion/pixelization/mappers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test_autoarray/dataset/imaging/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import os
from os import path

Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 2d02f17

Please sign in to comment.