Skip to content

Commit

Permalink
Fix issue where ufunc ops had side effects on the input masks
Browse files Browse the repository at this point in the history
  • Loading branch information
fbunt committed Apr 18, 2023
1 parent 16e40ac commit ce10f8b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion raster_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def merge_masks(masks):
mask = None
for m in masks:
if mask is None:
mask = m
mask = m.copy()
else:
mask |= m
return mask
Expand Down
38 changes: 38 additions & 0 deletions tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,44 @@ def test_ufuncs_multiple_input_against_raster(ufunc):
assert np.allclose(r._ds.mask, mask, equal_nan=True)


def test_ufunc_different_masks():
r1 = arange_raster((3, 3)).set_null_value(0)
r1.mask[..., :2, :] = True
r2 = arange_raster((3, 3)).set_null_value(0)
r2.mask[..., :, :2] = True
r1_mask = np.array(
[
[
[1, 1, 1],
[1, 1, 1],
[0, 0, 0],
]
]
)
r2_mask = np.array(
[
[
[1, 1, 0],
[1, 1, 0],
[1, 1, 0],
]
]
)
result_mask = r1_mask | r2_mask
assert np.allclose(r1.mask.compute(), r1_mask)
assert np.allclose(r2.mask.compute(), r2_mask)
assert np.allclose(
result_mask, np.array([[1, 1, 1], [1, 1, 1], [1, 1, 0]])
)

result = r1 * r2
# Make sure that there where no side effects on the input raster masks
assert np.allclose(r1.mask.compute(), r1_mask)
assert np.allclose(r2.mask.compute(), r2_mask)
result_data = np.where(result_mask, result.null_value, 64)
assert np.allclose(result, result_data)


def test_invert():
x = arange_nd((4, 5, 5))
rs = Raster(x)
Expand Down

0 comments on commit ce10f8b

Please sign in to comment.