Skip to content

Commit

Permalink
Improve handling of masks
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Oct 24, 2024
1 parent dd0432e commit 1e51588
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/iris/_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def _hash_ndarray(a: np.ndarray) -> np.ndarray:

# Hash the bytes representing the array data.
hash.update(b"data=")
if isinstance(a, np.ma.MaskedArray):
if np.ma.is_masked(a):
# Hash only the unmasked data
hash.update(a.compressed().tobytes())
# Hash the mask
Expand Down
17 changes: 17 additions & 0 deletions lib/iris/tests/unit/concatenate/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
(np.array([np.nan, 1.0]), np.array([np.nan, 1.0]), True),
(np.ma.array([1, 2], mask=[0, 1]), np.ma.array([1, 2], mask=[0, 1]), True),
(np.ma.array([1, 2], mask=[0, 1]), np.ma.array([1, 2], mask=[0, 0]), False),
(np.ma.array([1, 2], mask=[1, 1]), np.ma.array([1, 2], mask=[1, 1]), True),
(np.ma.array([1, 2], mask=[0, 0]), np.ma.array([1, 2], mask=[0, 0]), True),
(da.arange(6).reshape((2, 3)), da.arange(6, chunks=1).reshape((2, 3)), True),
(da.arange(20, chunks=1), da.arange(20, chunks=2), True),
(
Expand All @@ -33,6 +35,21 @@
da.ma.masked_array([1, 3], mask=[0, 1]),
True,
),
(
np.arange(2),
da.ma.masked_array(np.arange(2), mask=[0, 0]),
True,
),
(
np.arange(2),
da.ma.masked_array(np.arange(2), mask=[0, 1]),
False,
),
(
da.ma.masked_array(np.arange(10), mask=np.zeros(10)),
da.ma.masked_array(np.arange(10), mask=np.ma.nomask),
True,
),
(
np.ma.array([1, 2], mask=[0, 1]),
np.ma.array([1, 3], mask=[0, 1], fill_value=10),
Expand Down

0 comments on commit 1e51588

Please sign in to comment.