Skip to content

Commit 744b139

Browse files
Jammy2211claude
authored andcommitted
refactor: clamp brightest_coordinate_in_region_from to array bounds
Compute each pixel bound (py_min/py_max/px_min/px_max) via separate pixel_coordinates_2d_from calls and clamp against shape_native so a region that extends past the array edge no longer slices off-array. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 88b86d3 commit 744b139

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

autoarray/structures/arrays/uniform_2d.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,24 +426,31 @@ def brightest_coordinate_in_region_from(
426426
The coordinates of the brightest pixel in scaled units (converted from pixels units).
427427
"""
428428

429-
y1, y0 = self.geometry.pixel_coordinates_2d_from(
430-
scaled_coordinates_2d=(
431-
region[0] - self.pixel_scales[0] / 2.0,
432-
region[2] + self.pixel_scales[0] / 2.0,
433-
)
429+
ps_y, ps_x = self.pixel_scales
430+
431+
py_min, _ = self.geometry.pixel_coordinates_2d_from(
432+
scaled_coordinates_2d=(region[1] - ps_y / 2.0, 0.0)
434433
)
435-
x0, x1 = self.geometry.pixel_coordinates_2d_from(
436-
scaled_coordinates_2d=(
437-
region[1] - self.pixel_scales[1] / 2.0,
438-
region[3] + self.pixel_scales[1] / 2.0,
439-
)
434+
py_max, _ = self.geometry.pixel_coordinates_2d_from(
435+
scaled_coordinates_2d=(region[0] + ps_y / 2.0, 0.0)
440436
)
437+
_, px_min = self.geometry.pixel_coordinates_2d_from(
438+
scaled_coordinates_2d=(0.0, region[2] + ps_x / 2.0)
439+
)
440+
_, px_max = self.geometry.pixel_coordinates_2d_from(
441+
scaled_coordinates_2d=(0.0, region[3] - ps_x / 2.0)
442+
)
443+
444+
py_min = max(0, py_min)
445+
px_min = max(0, px_min)
446+
py_max = min(self.shape_native[0] - 1, py_max)
447+
px_max = min(self.shape_native[1] - 1, px_max)
441448

442-
extracted_region = self.native[y0:y1, x0:x1]
449+
extracted_region = self.native[py_min : py_max + 1, px_min : px_max + 1]
443450

444451
brightest_pixel_value = np.max(extracted_region)
445452
extracted_pixels = np.argwhere(extracted_region == brightest_pixel_value)[0]
446-
pixel_coordinates_2d = (y0 + extracted_pixels[0], x0 + extracted_pixels[1])
453+
pixel_coordinates_2d = (py_min + extracted_pixels[0], px_min + extracted_pixels[1])
447454

448455
if return_in_pixels:
449456
return pixel_coordinates_2d

test_autoarray/structures/arrays/test_uniform_2d.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,74 @@ def test__brightest_coordinate_in_region_from__5x5_array_asymmetric_region__corr
514514
assert brightest_coordinate == pytest.approx((-0.1, 0.2), 1.0e-4)
515515

516516

517+
def test__brightest_coordinate_in_region_from__region_offset_from_origin__correct_peak_coordinate():
518+
mask = aa.Mask2D.all_false(shape_native=(7, 7), pixel_scales=0.1)
519+
values = np.zeros((7, 7))
520+
values[5, 1] = 99.0
521+
array_2d = aa.Array2D(values=values, mask=mask)
522+
523+
brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
524+
region=(-0.25, -0.05, -0.25, -0.05)
525+
)
526+
527+
assert brightest_coordinate == pytest.approx((-0.2, -0.2), 1.0e-4)
528+
529+
530+
def test__brightest_coordinate_in_region_from__region_fully_offset_negative_quadrant__correct_peak_coordinate():
531+
mask = aa.Mask2D.all_false(shape_native=(11, 11), pixel_scales=0.1)
532+
values = np.zeros((11, 11))
533+
values[8, 2] = 77.0
534+
array_2d = aa.Array2D(values=values, mask=mask)
535+
536+
brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
537+
region=(-0.45, -0.15, -0.45, -0.15)
538+
)
539+
540+
assert brightest_coordinate == pytest.approx((-0.3, -0.3), 1.0e-4)
541+
542+
543+
def test__brightest_coordinate_in_region_from__region_offset_positive_quadrant__correct_peak_coordinate():
544+
mask = aa.Mask2D.all_false(shape_native=(11, 11), pixel_scales=0.1)
545+
values = np.zeros((11, 11))
546+
values[2, 8] = 55.0
547+
array_2d = aa.Array2D(values=values, mask=mask)
548+
549+
brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
550+
region=(0.15, 0.45, 0.15, 0.45)
551+
)
552+
553+
assert brightest_coordinate == pytest.approx((0.3, 0.3), 1.0e-4)
554+
555+
556+
def test__brightest_coordinate_in_region_from__region_clipped_to_array_bounds__correct_peak_coordinate():
557+
mask = aa.Mask2D.all_false(shape_native=(5, 5), pixel_scales=0.1)
558+
values = np.zeros((5, 5))
559+
values[0, 0] = 42.0
560+
array_2d = aa.Array2D(values=values, mask=mask)
561+
562+
brightest_coordinate = array_2d.brightest_coordinate_in_region_from(
563+
region=(0.15, 0.45, -0.45, -0.15)
564+
)
565+
566+
assert brightest_coordinate == pytest.approx((0.2, -0.2), 1.0e-4)
567+
568+
569+
def test__brightest_sub_pixel_coordinate_in_region_from__region_offset_from_origin__correct_sub_pixel_peak():
570+
mask = aa.Mask2D.all_false(shape_native=(7, 7), pixel_scales=0.1)
571+
values = np.zeros((7, 7))
572+
values[5, 1] = 100.0
573+
values[5, 2] = 50.0
574+
values[4, 1] = 50.0
575+
array_2d = aa.Array2D(values=values, mask=mask)
576+
577+
brightest_coordinate = array_2d.brightest_sub_pixel_coordinate_in_region_from(
578+
region=(-0.25, -0.05, -0.25, -0.05), box_size=1
579+
)
580+
581+
assert brightest_coordinate[0] < -0.15
582+
assert brightest_coordinate[1] > -0.2
583+
584+
517585
def test__brightest_sub_pixel_coordinate_in_region_from__4x4_array__correct_sub_pixel_peak():
518586
mask = aa.Mask2D.all_false(shape_native=(4, 4), pixel_scales=0.1)
519587
array_2d = aa.Array2D(

0 commit comments

Comments
 (0)