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

Use np.isscalar rather than our own guess #16

Merged
merged 2 commits into from
Jan 30, 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.3"
version = "0.9.4"
description = "Abstraction of gdal datasets for doing basic math operations"
readme = "README.md"
authors = [{ name = "Michael Dales", email = "mwd24@cam.ac.uk" }]
Expand Down
24 changes: 18 additions & 6 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,21 @@ def test_mult_float_layers() -> None:

assert (expected == actual).all()

def test_add_to_float_layer_by_const() -> None:
@pytest.mark.parametrize("c", [
(float(2.5)),
(int(2)),
(np.uint16(2)),
(np.float32(2.5)),
])
def test_add_to_float_layer_by_const(c) -> None:
data1 = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]])
layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1))
result = RasterLayer.empty_raster_layer_like(layer1)

comp = layer1 + 2.5
comp = layer1 + c
comp.save(result)

expected = data1 + 2.5
expected = data1 + c
actual = result.read_array(0, 0, 4, 2)

assert (expected == actual).all()
Expand Down Expand Up @@ -677,16 +683,22 @@ def test_nan_to_num_default() -> None:

assert (expected == actual).all()

def test_where_simple() -> None:
@pytest.mark.parametrize("ct", [
float,
int,
np.uint16,
np.float32,
])
def test_where_simple(ct) -> None:
data1 = np.array([[0, 1, 0, 2], [0, 0, 1, 1]])
layer1 = RasterLayer(gdal_dataset_with_data((0.0, 0.0), 0.02, data1))
result = RasterLayer.empty_raster_layer_like(layer1)

comp = LayerOperation.where(layer1 > 0, 1, 2)
comp = LayerOperation.where(layer1 > 0, ct(1), ct(2))
comp.ystep = 1
comp.save(result)

expected = np.where(data1 > 0, 1, 2)
expected = np.where(data1 > 0, ct(1), ct(2))
actual = result.read_array(0, 0, 4, 2)
assert (expected == actual).all()

Expand Down
40 changes: 26 additions & 14 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def test_pickle_uniform_area_layer() -> None:
with tempfile.TemporaryDirectory() as tempdir:
path = os.path.join(tempdir, "test.tif")
area = Area(
math.floor(-180 / pixel_scale) * pixel_scale,
math.ceil(90 / pixel_scale) * pixel_scale,
(math.floor(-180 / pixel_scale) * pixel_scale) + pixel_scale,
math.floor(-180 / pixel_scale) * pixel_scale,
math.ceil(90 / pixel_scale) * pixel_scale,
(math.floor(-180 / pixel_scale) * pixel_scale) + pixel_scale,
math.floor(-90 / pixel_scale) * pixel_scale
)
dataset = gdal_dataset_of_region(area, pixel_scale, filename=path)
Expand All @@ -73,15 +73,15 @@ def test_pickle_uniform_area_layer() -> None:

assert restore.pixel_scale == (pixel_scale, -pixel_scale)
assert restore.area == Area(
math.floor(-180 / pixel_scale) * pixel_scale,
math.ceil(90 / pixel_scale) * pixel_scale,
math.ceil(180 / pixel_scale) * pixel_scale,
math.floor(-180 / pixel_scale) * pixel_scale,
math.ceil(90 / pixel_scale) * pixel_scale,
math.ceil(180 / pixel_scale) * pixel_scale,
math.floor(-90 / pixel_scale) * pixel_scale
)
assert restore.window == Window(
0,
0,
math.ceil((restore.area.right - restore.area.left) / pixel_scale),
math.ceil((restore.area.right - restore.area.left) / pixel_scale),
math.ceil((restore.area.top - restore.area.bottom) / pixel_scale)
)

Expand All @@ -105,24 +105,36 @@ def test_pickle_group_layer() -> None:
result = restore.read_array(0, 0, 100, 100)
assert (expected == result).all()

def test_pickle_constant_layer() -> None:
layer = ConstantLayer(42.0)
@pytest.mark.parametrize("c", [
(float(2.5)),
(int(2)),
(np.uint16(2)),
(np.float32(2.5)),
])
def test_pickle_constant_layer(c) -> None:
layer = ConstantLayer(c)

p = pickle.dumps(layer)
restore = pickle.loads(p)

result = restore.read_array(0, 0, 1, 1)
assert (result == np.array([[42.0]])).all()

def test_pickle_simple_calc() -> None:
assert (result == np.array([[c]])).all()

@pytest.mark.parametrize("c", [
(float(2.5)),
(int(2)),
(np.uint16(2)),
(np.float32(2.5)),
])
def test_pickle_simple_calc(c) -> None:
with tempfile.TemporaryDirectory() as tempdir:
path = os.path.join(tempdir, "test.tif")
area = Area(-10, 10, 10, -10)
layer = RasterLayer(gdal_dataset_of_region(area, 0.2, filename=path))

calc = layer * 2.0
calc = layer * c
assert calc.sum() != 0
assert calc.sum() == layer.sum() * 2
assert calc.sum() == layer.sum() * c

p = pickle.dumps(calc)
restore = pickle.loads(p)
Expand Down
4 changes: 2 additions & 2 deletions yirgacheffe/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def __init__(self, lhs, operator=None, rhs=None, other=None, **kwargs):
self.operator = operator

if rhs is not None:
if isinstance(rhs, (float, int)):
if np.isscalar(rhs):
self.rhs = LayerConstant(rhs)
elif isinstance(rhs, (np.ndarray)):
if rhs.shape == ():
Expand All @@ -204,7 +204,7 @@ def __init__(self, lhs, operator=None, rhs=None, other=None, **kwargs):
self.rhs = None

if other is not None:
if isinstance(other, (float, int)):
if np.isscalar(other):
self.other = LayerConstant(other)
elif isinstance(other, (np.ndarray)):
if other.shape == ():
Expand Down