Skip to content

Commit

Permalink
Merge pull request #14 from quantifyearth/mwd-min-max
Browse files Browse the repository at this point in the history
Expose np maximum and minimum functions
  • Loading branch information
mdales authored Jan 23, 2025
2 parents 0e35eca + 9c8041a commit 8763538
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
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

0 comments on commit 8763538

Please sign in to comment.