Skip to content

Commit

Permalink
Merge pull request #154 from Jammy2211/feature/brightest_pixel
Browse files Browse the repository at this point in the history
Feature/brightest pixel
  • Loading branch information
Jammy2211 authored Dec 12, 2024
2 parents 8b07b25 + 100e521 commit 9e6ab87
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 5 deletions.
8 changes: 3 additions & 5 deletions autoarray/dataset/imaging/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def apply_noise_scaling(
mask: Mask2D,
noise_value: float = 1e8,
signal_to_noise_value: Optional[float] = None,
should_zero_data : bool = True
should_zero_data: bool = True,
) -> "Imaging":
"""
Apply a mask to the imaging dataset using noise scaling, whereby the maskmay zero the data and increase
Expand Down Expand Up @@ -387,12 +387,10 @@ 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[edge_mask == False]) / signal_to_noise_value,
np.median(self.data.native[mask.derive_mask.edge == False])
/ signal_to_noise_value,
self.noise_map.native,
)

Expand Down
97 changes: 97 additions & 0 deletions autoarray/structures/arrays/uniform_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,103 @@ def binned_across_columns(self) -> Array1D:
# binned_array = (self.native*np.invert(self.mask)).sum(axis=1)/np.invert(self.mask).sum(axis=1)
return Array1D.no_mask(values=binned_array, pixel_scales=self.pixel_scale)

def brightest_coordinate_in_region_from(
self, region: Optional[Tuple[float, float, float, float]]
) -> Tuple[float, float]:
"""
Returns the brightest pixel in the array inside an input region of form (y0, y1, x0, x1) where
the region is in scaled coordinates.
For example, if the input region is `region=(-0.15, 0.25, 0.35, 0.55)` the code finds all pixels inside of
this region in scaled units, finds the brightest pixel of those pixels and then returns the scaled
coordinate location of that brightest pixel.
The centre of the brightest pixel is returned, the function `brightest_sub_pixel_coordinate_in_region_from`
performs a sub pixel calculation to return the brightest sub pixel coordinate.
Parameters
----------
region
The (y0, y1, x0, x1) region in scaled coordinates over which the brightest coordinate is located.
Returns
-------
The coordinates of the brightest pixel in scaled units (converted from pixels units).
"""

y1, y0 = self.geometry.pixel_coordinates_2d_from(
scaled_coordinates_2d=(
region[0] - self.pixel_scales[0] / 2.0,
region[2] + self.pixel_scales[0] / 2.0,
)
)
x0, x1 = self.geometry.pixel_coordinates_2d_from(
scaled_coordinates_2d=(
region[1] - self.pixel_scales[1] / 2.0,
region[3] + self.pixel_scales[1] / 2.0,
)
)

extracted_region = self.native[y0:y1, x0:x1]

brightest_pixel_value = np.max(extracted_region)
extracted_pixels = np.argwhere(extracted_region == brightest_pixel_value)[0]
pixel_coordinates_2d = (y0 + extracted_pixels[0], x0 + extracted_pixels[1])

return self.geometry.scaled_coordinates_2d_from(
pixel_coordinates_2d=pixel_coordinates_2d
)

def brightest_sub_pixel_coordinate_in_region_from(
self, region: Optional[Tuple[float, float, float, float]], box_size: int = 1
) -> Tuple[float, float]:
"""
Returns the brightest sub-pixel in the array inside an input region of form (y0, y1, x0, x1) where
the region is in scaled coordinates.
For example, if the input region is `region=(-0.15, 0.25, 0.35, 0.55)` the code finds all pixels inside of
this region in scaled units, finds the brightest pixel of those pixels, and then on a 3x3 grid surrounding
this pixel determines the locaiton of the brightest sub pixel using a weighted centre calculation.
The centre of the brightest pixel is returned, the function `brightest_sub_pixel_coordinate_in_region_from`
performs a sub pixel calculation to return the brightest sub pixel coordinate.
Parameters
----------
region
The (y0, y1, x0, x1) region in scaled coordinates over which the brightest coordinate is located.
Returns
-------
The coordinates of the brightest pixel in scaled units (converted from pixels units).
"""
brightest_pixel = self.brightest_coordinate_in_region_from(region=region)

y, x = self.geometry.pixel_coordinates_2d_from(
scaled_coordinates_2d=brightest_pixel
)
region_start_y, region_end_y = max(0, y - box_size), min(
self.shape_native[0], y + box_size + 1
)
region_start_x, region_end_x = max(0, x - box_size), min(
self.shape_native[1], x + box_size + 1
)
region = self.native[region_start_y:region_end_y, region_start_x:region_end_x]

y_indices, x_indices = np.meshgrid(
range(region_start_y, region_end_y),
range(region_start_x, region_end_x),
indexing="ij",
)

weights = region.flatten()
subpixel_y = np.sum(weights * y_indices.flatten()) / np.sum(weights)
subpixel_x = np.sum(weights * x_indices.flatten()) / np.sum(weights)

return self.geometry.scaled_coordinates_2d_from(
pixel_coordinates_2d=(subpixel_y, subpixel_x)
)

def zoomed_around_mask(self, buffer: int = 1) -> "Array2D":
"""
Extract the 2D region of an array corresponding to the rectangle encompassing all unmasked values.
Expand Down
92 changes: 92 additions & 0 deletions test_autoarray/structures/arrays/test_uniform_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,98 @@ def test__binned_across_columns():
assert (array.binned_across_columns == np.array([1.5, 6.0, 9.0])).all()


def test__brightest_pixel_in_region_from():
mask = aa.Mask2D.all_false(shape_native=(4, 4), pixel_scales=0.1)
array_2d = aa.Array2D(
values=[
[1.0, 2.0, 3.0, 4.0],
[6.0, 7.0, 8.0, 9.0],
[11.0, 12.0, 13.0, 15.0],
[16.0, 17.0, 18.0, 20.0],
],
mask=mask,
)

brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
region=(-0.26, 0.06, -0.06, 0.06)
)

assert brightest_coordinate == pytest.approx((-0.15, 0.05), 1.0e-4)

brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
region=(-0.051, 0.151, -0.151, 0.149)
)

assert brightest_coordinate == pytest.approx((-0.05, 0.05), 1.0e-4)

mask = aa.Mask2D.all_false(shape_native=(5, 5), pixel_scales=0.1)
array_2d = aa.Array2D(
values=[
[1.0, 2.0, 3.0, 4.0, 5.0],
[6.0, 7.0, 8.0, 9.0, 10.0],
[11.0, 12.0, 13.0, 14.0, 15.0],
[16.0, 17.0, 18.0, 19.0, 20.0],
[21.0, 22.0, 23.0, 24.0, 25.0],
],
mask=mask,
)

brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
region=(-0.15, 0.15, -0.15, 0.15)
)

assert brightest_coordinate == pytest.approx((-0.1, 0.1), 1.0e-4)

brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
region=(-0.25, 0.15, -0.15, 0.15)
)

assert brightest_coordinate == pytest.approx((-0.2, 0.1), 1.0e-4)

brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
region=(-0.15, 0.15, -0.15, 0.25)
)

assert brightest_coordinate == pytest.approx((-0.1, 0.2), 1.0e-4)


def test__brightest_sub_pixel_in_region_from():
mask = aa.Mask2D.all_false(shape_native=(4, 4), pixel_scales=0.1)
array_2d = aa.Array2D(
values=[
[1.0, 2.0, 3.0, 4.0],
[6.0, 7.0, 8.0, 9.0],
[11.0, 12.0, 13.0, 15.0],
[16.0, 17.0, 18.0, 20.0],
],
mask=mask,
)

brightest_coordinate = array_2d.brightest_sub_pixel_coordinate_in_region_from(
region=(-0.26, 0.06, -0.06, 0.06)
)

assert brightest_coordinate == pytest.approx((-0.1078947, 0.056315), 1.0e-4)

mask = aa.Mask2D.all_false(shape_native=(5, 5), pixel_scales=0.1)
array_2d = aa.Array2D(
values=[
[1.0, 2.0, 3.0, 4.0, 5.0],
[6.0, 7.0, 8.0, 9.0, 10.0],
[11.0, 12.0, 13.0, 14.0, 15.0],
[16.0, 17.0, 18.0, 19.0, 20.0],
[21.0, 22.0, 23.0, 24.0, 25.0],
],
mask=mask,
)

brightest_coordinate = array_2d.brightest_sub_pixel_coordinate_in_region_from(
region=(-0.15, 0.15, -0.15, 0.15)
)

assert brightest_coordinate == pytest.approx((-0.11754, 0.103508), 1.0e-4)


def test__header__modified_julian_date():
header_sci_obj = {"DATE-OBS": "2000-01-01", "TIME-OBS": "00:00:00"}

Expand Down

0 comments on commit 9e6ab87

Please sign in to comment.