Skip to content

Commit

Permalink
Merge branch 'main' into feature/area_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rhayes777 committed Nov 1, 2024
2 parents 3fcd83a + 5e9e263 commit 1fa71a9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion autoarray/config/visualize/include.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 6 additions & 0 deletions autoarray/dataset/imaging/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def from_fits(
psf_path: Optional[Union[Path, str]] = None,
psf_hdu: int = 0,
noise_covariance_matrix: Optional[np.ndarray] = None,
check_noise_map: bool = True,
over_sampling: Optional[OverSamplingDataset] = OverSamplingDataset(),
) -> "Imaging":
"""
Expand Down Expand Up @@ -250,6 +251,8 @@ def from_fits(
The hdu the noise map is contained in the .fits file specified by `noise_map_path`.
noise_covariance_matrix
A noise-map covariance matrix representing the covariance between noise in every `data` value.
check_noise_map
If True, the noise-map is checked to ensure all values are above zero.
over_sampling
The over sampling schemes which divide the grids into sub grids of smaller pixels within their host image
pixels when using the grid to evaluate a function (e.g. images) to better approximate the 2D line integral
Expand Down Expand Up @@ -280,6 +283,7 @@ def from_fits(
noise_map=noise_map,
psf=psf,
noise_covariance_matrix=noise_covariance_matrix,
check_noise_map=check_noise_map,
over_sampling=over_sampling,
)

Expand Down Expand Up @@ -377,6 +381,7 @@ def apply_noise_scaling(self, mask: Mask2D, noise_value: float = 1e8) -> "Imagin
noise_covariance_matrix=self.noise_covariance_matrix,
over_sampling=self.over_sampling,
pad_for_convolver=False,
check_noise_map=False
)

logger.info(
Expand Down Expand Up @@ -429,6 +434,7 @@ def apply_over_sampling(
psf=self.psf,
over_sampling=over_sampling,
pad_for_convolver=False,
check_noise_map=False
)

def output_to_fits(
Expand Down
20 changes: 20 additions & 0 deletions autoarray/inversion/plot/inversion_plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def figures_2d_of_pixelization(
reconstructed_image: bool = False,
reconstruction: bool = False,
errors: bool = False,
signal_to_noise_map: bool = False,
regularization_weights: bool = False,
sub_pixels_per_image_pixels: bool = False,
mesh_pixels_per_image_pixels: bool = False,
Expand All @@ -146,6 +147,8 @@ def figures_2d_of_pixelization(
Whether to make a 2D plot (via `imshow` or `fill`) of the mapper's source-plane reconstruction.
errors
Whether to make a 2D plot (via `imshow` or `fill`) of the mapper's source-plane errors.
signal_to_noise_map
Whether to make a 2D plot (via `imshow` or `fill`) of the mapper's source-plane signal-to-noise-map.
sub_pixels_per_image_pixels
Whether to make a 2D plot (via `imshow`) of the number of sub pixels per image pixels in the 2D
data's mask.
Expand Down Expand Up @@ -243,6 +246,23 @@ def figures_2d_of_pixelization(
except TypeError:
pass

if signal_to_noise_map:
try:
signal_to_noise_values = (
self.inversion.reconstruction_dict[mapper_plotter.mapper]
/ self.inversion.errors_dict[mapper_plotter.mapper]
)

mapper_plotter.plot_source_from(
pixel_values=signal_to_noise_values,
auto_labels=AutoLabels(
title="Signal To Noise Map", filename="signal_to_noise_map"
),
)

except TypeError:
pass

# TODO : NEed to understand why this raises an error in voronoi_drawer.

if regularization_weights:
Expand Down
11 changes: 8 additions & 3 deletions autoarray/mask/abstract_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ def pixel_scale(self) -> float:
"""
for pixel_scale in self.pixel_scales:
if abs(pixel_scale - self.pixel_scales[0]) > 1.0e-8:
raise exc.MaskException(
"Cannot return a pixel_scale for a grid where each dimension has a "
"different pixel scale (e.g. pixel_scales[0] != pixel_scales[1])"
logger.warning(
f"""
The Mask has different pixel scales in each dimensions, which are {self.pixel_scales}.
This is not expected, and will lead to unexpected behaviour in the grid and mask classes.
The code will continue to run, but you should check that the pixel scales are as you expect and
that associated data structures (e.g. grids) are behaving as you expect.
"""
)

return self.pixel_scales[0]
Expand Down
2 changes: 1 addition & 1 deletion autoarray/structures/decorators/to_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def via_grid_2d_irr(self, result) -> Union[Grid2DIrregular, List[Grid2DIrregular
----------
result
The input result (e.g. of a decorated function) that is converted to an Grid2DIrregular or list of
Grid2DIrregular objects.
`Grid2DIrregular` objects.
"""
if not isinstance(result, list):
return Grid2DIrregular(values=result)
Expand Down
14 changes: 10 additions & 4 deletions autoarray/structures/grids/irregular_2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from autoarray.numpy_wrapper import np
import logging
from typing import List, Optional, Tuple, Union

from autoarray.numpy_wrapper import np
from autoarray.abstract_ndarray import AbstractNDArray
from autoarray.geometry.geometry_2d_irregular import Geometry2DIrregular
from autoarray.mask.mask_2d import Mask2D
Expand All @@ -10,6 +11,8 @@
from autoarray.structures.grids import grid_2d_util
from autoarray.geometry import geometry_util

logger = logging.getLogger(__name__)


class Grid2DIrregular(AbstractNDArray):
def __init__(self, values: Union[np.ndarray, List]):
Expand Down Expand Up @@ -351,9 +354,12 @@ def pixel_scale(self) -> float:
if self.pixel_scales[0] == self.pixel_scales[1]:
return self.pixel_scales[0]
else:
raise exc.GridException(
"Cannot return a pixel_scale for a grid where each dimension has a "
"different pixel scale (e.g. pixel_scales[0] != pixel_scales[1])"
logger.warning(
f"""
The `Grid2DIrregular` has pixel scales of {self.pixel_scales}, which are not the same in both
dimensions. This means that the pixel scale of the grid is not a single value and may cause
issues with calculations that assume a uniform pixel scale.
"""
)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions test_autoarray/inversion/inversion/test_mapper_valued.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,20 @@ def test__magnification_via_interpolation_from():
)

mapper = aa.m.MockMapper(
parameters=3,
parameters=4,
mask=mask,
interpolated_array=magnification,
mapping_matrix=np.ones((4, 3)),
mapping_matrix=np.ones((4, 4)),
)

mapper_valued = aa.MapperValued(values=np.array(magnification), mapper=mapper)

magnification = mapper_valued.magnification_via_interpolation_from()

assert magnification == pytest.approx(3.6666666666666665, 1.0e-4)
assert magnification == pytest.approx(4.0, 1.0e-4)

magnification = mapper_valued.magnification_via_interpolation_from(
shape_native=(3, 3), extent=(-1.0, 1.0, -1.0, 1.0)
)

assert magnification == pytest.approx(3.6666666666666665, 1.0e-4)
assert magnification == pytest.approx(4.0, 1.0e-4)
2 changes: 2 additions & 0 deletions test_autoarray/inversion/plot/test_inversion_plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test__individual_attributes_are_output_for_all_mappers(
mesh_pixels_per_image_pixels=True,
image_pixels_per_mesh_pixel=True,
errors=True,
signal_to_noise_map=True,
regularization_weights=True,
)

Expand All @@ -76,6 +77,7 @@ def test__individual_attributes_are_output_for_all_mappers(
assert path.join(plot_path, "mesh_pixels_per_image_pixels.png") in plot_patch.paths
assert path.join(plot_path, "image_pixels_per_mesh_pixel.png") in plot_patch.paths
assert path.join(plot_path, "errors.png") in plot_patch.paths
assert path.join(plot_path, "signal_to_noise_map.png") in plot_patch.paths
assert path.join(plot_path, "regularization_weights.png") in plot_patch.paths

plot_patch.paths = []
Expand Down

0 comments on commit 1fa71a9

Please sign in to comment.