Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/rastr/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,52 @@ def sum(self) -> float:
with suppress_slice_warning():
return float(np.nansum(self.arr))

def cxmax(self) -> tuple[float, float]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot there's a lot of duplication between these methods. And in the future we might make it worse with cxquantile etc.

So I'd really like a shared helper, maybe named something like _cx_stat accepting a nanarg* method or equivalent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted a _cx_stat helper that accepts a nanarg* function and a name string. cxmax and cxmin are now one-liners delegating to it, so adding cxquantile etc. won't duplicate the coordinate lookup logic. See bb196fd.

"""Get the coordinate of the cell centre with the maximum value.

Returns the (x, y) coordinate of the centre of the pixel containing the
maximum value in the raster, ignoring NaN values. If multiple pixels share
the maximum value, the first occurrence in row-major order is returned.

Returns:
A tuple (x, y) representing the coordinate of the cell centre with
the maximum value.

Raises:
ValueError: If all values in the raster are NaN.
"""
if np.all(np.isnan(self.arr)):
msg = "Cannot find cxmax of an all-NaN raster."
raise ValueError(msg)
idx = np.unravel_index(np.nanargmax(self.arr), self.arr.shape)
row, col = int(idx[0]), int(idx[1])
x_coords = self.raster_meta.get_cell_x_coords(self.arr.shape[1])
y_coords = self.raster_meta.get_cell_y_coords(self.arr.shape[0])
return float(x_coords[col]), float(y_coords[row])

def cxmin(self) -> tuple[float, float]:
"""Get the coordinate of the cell centre with the minimum value.

Returns the (x, y) coordinate of the centre of the pixel containing the
minimum value in the raster, ignoring NaN values. If multiple pixels share
the minimum value, the first occurrence in row-major order is returned.

Returns:
A tuple (x, y) representing the coordinate of the cell centre with
the minimum value.

Raises:
ValueError: If all values in the raster are NaN.
"""
if np.all(np.isnan(self.arr)):
msg = "Cannot find cxmin of an all-NaN raster."
raise ValueError(msg)
idx = np.unravel_index(np.nanargmin(self.arr), self.arr.shape)
row, col = int(idx[0]), int(idx[1])
x_coords = self.raster_meta.get_cell_x_coords(self.arr.shape[1])
y_coords = self.raster_meta.get_cell_y_coords(self.arr.shape[0])
return float(x_coords[col]), float(y_coords[row])

def unique(self) -> NDArray:
"""Get the unique cell values in the raster, including NaN.

Expand Down
76 changes: 76 additions & 0 deletions tests/rastr/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5026,6 +5026,82 @@ def test_quantile_all_nan_raster_no_warning(self) -> None:
all_nan_slice_raster.quantile(0.8)


class TestCxmax:
"""Test the cxmax method of the Raster class."""

def test_basic(self, stats_test_raster: Raster) -> None:
"""Test cxmax returns coordinate of maximum value."""
# The 3x3 array is [[1,2,3],[4,5,6],[7,8,9]] with cell size 2.
# Max value 9 is at row=2, col=2.
# x = (2 + 0.5) * 2 = 5, y = (2 + 0.5) * 2 = 5
x, y = stats_test_raster.cxmax()
assert x == pytest.approx(5.0)
assert y == pytest.approx(5.0)

def test_with_nans(self, stats_test_raster_with_nans: Raster) -> None:
"""Test cxmax ignores NaN values."""
# Array is [[1,2,nan],[4,nan,6],[7,8,9]], max=9 at row=2, col=2
x, y = stats_test_raster_with_nans.cxmax()
assert x == pytest.approx(5.0)
assert y == pytest.approx(5.0)

def test_all_nan_raises(self) -> None:
"""Test cxmax raises ValueError for all-NaN raster."""
meta = RasterMeta(
crs=CRS.from_epsg(2193),
transform=Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0),
)
all_nan = Raster(arr=np.full((3, 3), np.nan), raster_meta=meta)
with pytest.raises(ValueError, match="Cannot find cxmax"):
all_nan.cxmax()

def test_negative_scale(self, example_neg_scaled_raster: Raster) -> None:
"""Test cxmax with negative y-scale transform."""
# Array is [[1,2],[3,4]], max=4 at row=1, col=1
# transform Affine(2.0, 0.0, 0.0, 0.0, -2.0, 0.0)
# x = (1 + 0.5) * 2 = 3, y = (1 + 0.5) * -2 = -3
x, y = example_neg_scaled_raster.cxmax()
assert x == pytest.approx(3.0)
assert y == pytest.approx(-3.0)


class TestCxmin:
"""Test the cxmin method of the Raster class."""

def test_basic(self, stats_test_raster: Raster) -> None:
"""Test cxmin returns coordinate of minimum value."""
# Min value 1 is at row=0, col=0.
# x = (0 + 0.5) * 2 = 1, y = (0 + 0.5) * 2 = 1
x, y = stats_test_raster.cxmin()
assert x == pytest.approx(1.0)
assert y == pytest.approx(1.0)

def test_with_nans(self, stats_test_raster_with_nans: Raster) -> None:
"""Test cxmin ignores NaN values."""
# Array is [[1,2,nan],[4,nan,6],[7,8,9]], min=1 at row=0, col=0
x, y = stats_test_raster_with_nans.cxmin()
assert x == pytest.approx(1.0)
assert y == pytest.approx(1.0)

def test_all_nan_raises(self) -> None:
"""Test cxmin raises ValueError for all-NaN raster."""
meta = RasterMeta(
crs=CRS.from_epsg(2193),
transform=Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0),
)
all_nan = Raster(arr=np.full((3, 3), np.nan), raster_meta=meta)
with pytest.raises(ValueError, match="Cannot find cxmin"):
all_nan.cxmin()

def test_negative_scale(self, example_neg_scaled_raster: Raster) -> None:
"""Test cxmin with negative y-scale transform."""
# Array is [[1,2],[3,4]], min=1 at row=0, col=0
# x = (0 + 0.5) * 2 = 1, y = (0 + 0.5) * -2 = -1
x, y = example_neg_scaled_raster.cxmin()
assert x == pytest.approx(1.0)
assert y == pytest.approx(-1.0)


class TestNormalize:
def test_example(self, example_raster: Raster):
# Act
Expand Down
Loading