Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose np maximum and minimum functions #14

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "yirgacheffe"
version = "0.9.1"
version = "0.9.2"
description = "Abstraction of gdal datasets for doing basic math operations"
readme = "README.md"
authors = [{ name = "Michael Dales", email = "[email protected]" }]
Expand Down
30 changes: 29 additions & 1 deletion tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,4 +908,32 @@ def test_exp2() -> None:

expected = np.exp2(data1)
actual = result.read_array(0, 0, 4, 2)
assert (expected == actual).all()
assert (expected == actual).all()

def test_minimum_layers() -> None:
data1 = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 2.0, 7.0, 8.0]])
data2 = np.array([[3.0, 2.0, 1.0, 4.0], [8.0, 2.0, 5.0, 7.0]])
layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1))
layer2 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data2))
result = RasterLayer.empty_raster_layer_like(layer1)

comp = LayerOperation.minimum(layer1, layer2)
comp.save(result)

expected = np.minimum(data1, data2)
actual = result.read_array(0, 0, 4, 2)
assert (expected == actual).all()

def test_maximum_layers() -> None:
data1 = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 2.0, 7.0, 8.0]])
data2 = np.array([[3.0, 2.0, 1.0, 4.0], [8.0, 2.0, 5.0, 7.0]])
layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1))
layer2 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data2))
result = RasterLayer.empty_raster_layer_like(layer1)

comp = LayerOperation.maximum(layer1, layer2)
comp.save(result)

expected = np.maximum(data1, data2)
actual = result.read_array(0, 0, 4, 2)
assert (expected == actual).all()
20 changes: 20 additions & 0 deletions yirgacheffe/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ def where(cond, a, b):
other=b
)

@staticmethod
def maximum(a, b):
return LayerOperation(
a,
np.maximum,
b,
)

@staticmethod
def minimum(a, b):
return LayerOperation(
a,
np.minimum,
rhs=b,
)

def __init__(self, lhs, operator=None, rhs=None, other=None):
self.ystep = constants.YSTEP

Expand Down Expand Up @@ -507,3 +523,7 @@ def _eval(self, index, step):
result[yoffset][xoffset] = self.operator(lhs_val)

return result

where = LayerOperation.where
minumum = LayerOperation.minimum
maximum = LayerOperation.maximum
Loading