Skip to content

Commit bed54ba

Browse files
committed
Do not sanitize no-op masks on ndarrays during test assertions.
The existence of a mask is part of the return contract and should be tested, even if the mask itself is a no-op.
1 parent aa6fccc commit bed54ba

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

tests/test_SparseNdarray.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy
66
import pytest
77

8-
from utils import sanitize_ndarray, assert_identical_ndarrays, assert_close_ndarrays, safe_concatenate, mock_SparseNdarray_contents, simulate_SparseNdarray
8+
from utils import assert_identical_ndarrays, assert_close_ndarrays, safe_concatenate, mock_SparseNdarray_contents, simulate_SparseNdarray
99

1010
#######################################################
1111
#######################################################
@@ -40,12 +40,12 @@ def convert_SparseNdarray_to_numpy(x):
4040
elif contents is not None:
4141
_recursive_compute_reference(contents, ndim, triplets)
4242

43-
output = numpy.ma.MaskedArray(numpy.zeros(shape))
43+
output = numpy.zeros(shape)
44+
if x.is_masked:
45+
output = numpy.ma.MaskedArray(output)
4446
for pos, val in triplets:
4547
output[pos] = val
4648

47-
if isinstance(output.mask, bool) and not output.mask:
48-
output = output.data
4949
return output
5050

5151

@@ -55,13 +55,10 @@ def _compare_sparse_vectors(left, right):
5555

5656
assert len(idx_l) == len(idx_r)
5757
assert (idx_l == idx_r).all()
58-
59-
val_l = sanitize_ndarray(val_l)
60-
val_r = sanitize_ndarray(val_r)
6158
comp = (val_l == val_r)
6259

63-
masked = numpy.ma.is_masked(val_l)
64-
assert masked == numpy.ma.is_masked(val_r)
60+
masked = numpy.ma.isMaskedArray(val_l)
61+
assert masked == numpy.ma.isMaskedArray(val_r)
6562
if masked:
6663
assert (val_l.mask == val_r.mask).all()
6764
assert numpy.logical_or(comp, val_r.mask).all()

tests/utils.py

+5-20
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,9 @@
44
import delayedarray
55

66

7-
def sanitize_ndarray(x: numpy.ndarray):
8-
if numpy.ma.is_masked(x):
9-
if isinstance(x.mask, bool):
10-
if not x.mask:
11-
return x.data
12-
else:
13-
if not x.mask.any():
14-
return x.data
15-
return x
16-
17-
187
def assert_identical_ndarrays(x: numpy.ndarray, y: numpy.ndarray):
19-
x = sanitize_ndarray(x)
20-
y = sanitize_ndarray(y)
21-
assert numpy.ma.is_masked(x) == numpy.ma.is_masked(y)
22-
if numpy.ma.is_masked(x):
8+
assert numpy.ma.isMaskedArray(x) == numpy.ma.isMaskedArray(y)
9+
if numpy.ma.isMaskedArray(x):
2310
assert (x.mask == y.mask).all()
2411
comp = is_equal_with_nan(x.data, y.data)
2512
remask = numpy.logical_or(numpy.zeros(x.shape), x.mask) # using an OR to force broadcasting of buggy masks of different shape.
@@ -38,10 +25,8 @@ def is_equal_with_nan(left: numpy.ndarray, right: numpy.ndarray):
3825

3926

4027
def assert_close_ndarrays(x: numpy.ndarray, y: numpy.ndarray):
41-
x = sanitize_ndarray(x)
42-
y = sanitize_ndarray(y)
43-
assert numpy.ma.is_masked(x) == numpy.ma.is_masked(y)
44-
if numpy.ma.is_masked(x):
28+
assert numpy.ma.isMaskedArray(x) == numpy.ma.isMaskedArray(y)
29+
if numpy.ma.isMaskedArray(x):
4530
assert (x.mask == y.mask).all()
4631
comp = is_close_with_nan(x.data, y.data)
4732
remask = numpy.logical_or(numpy.zeros(x.shape, dtype=numpy.bool_), x.mask) # using an OR to force broadcasting of buggy masks of different shape.
@@ -60,7 +45,7 @@ def is_close_with_nan(left: numpy.ndarray, right: numpy.ndarray):
6045

6146

6247
def safe_concatenate(x: List[numpy.ndarray], axis: int = 0):
63-
if any(numpy.ma.is_masked(y) for y in x):
48+
if any(numpy.ma.isMaskedArray(y) for y in x):
6449
return numpy.ma.concatenate(x, axis=axis)
6550
else:
6651
return numpy.concatenate(x, axis=axis)

0 commit comments

Comments
 (0)