Skip to content

Commit

Permalink
Return early in eq_hist() if all data masked out
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Jan 19, 2023
1 parent 73d3deb commit 4398ff9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
24 changes: 23 additions & 1 deletion datashader/tests/test_transfer_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ def test_shade_zeros(array):
@pytest.mark.parametrize('agg', aggs)
@pytest.mark.parametrize('attr', ['d'])
@pytest.mark.parametrize('rescale', [False, True])
#@pytest.mark.parametrize('rescale', [True])
def test_shade_rescale_discrete_levels(agg, attr, rescale):
x = getattr(agg, attr)
cmap = ['pink', 'red']
Expand All @@ -518,6 +517,29 @@ def test_shade_rescale_discrete_levels(agg, attr, rescale):
assert_eq_xr(img, sol)


empty_arrays = [
np.zeros((2, 2, 2), dtype=np.uint32),
np.full((2, 2, 2), np.nan, dtype=np.float64),
]
if cupy is not None:
empty_arrays += [
cupy.zeros((2, 2, 2), dtype=cupy.uint32),
cupy.full((2, 2, 2), cupy.nan, dtype=cupy.float64),
]
@pytest.mark.parametrize('empty_array', empty_arrays)
def test_shade_all_masked(empty_array):
# Issue #1166, return early with array of all nans if all of data is masked out.
# Before the fix this test results in:
# IndexError: index -1 is out of bounds for axis 0 with size 0
agg = xr.DataArray(
data=empty_array,
coords=dict(y=[0, 1], x=[0, 1], cat=['a', 'b']),
)
im = tf.shade(agg, how='eq_hist', cmap=["white", "white"])
assert isinstance(im.data, np.ndarray)
assert im.shape == (2, 2)


coords2 = [np.array([0, 2]), np.array([3, 5])]
img1 = tf.Image(np.array([[0xff00ffff, 0x00000000],
[0x00000000, 0xff00ff7d]], dtype='uint32'),
Expand Down
5 changes: 5 additions & 0 deletions datashader/transfer_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def eq_hist(data, mask=None, nbins=256*256):
interp = np.interp
array_module = np

if mask is not None and array_module.all(mask):
# Issue #1166, return early with array of all nans if all of data is masked out.
return array_module.full_like(data, np.nan), 0

data2 = data if mask is None else data[~mask]

# Run more accurate value counting if data is of boolean or integer type
Expand Down Expand Up @@ -196,6 +200,7 @@ def eq_hist(data, mask=None, nbins=256*256):




_interpolate_lookup = {'log': lambda d, m: np.log1p(np.where(m, np.nan, d)),
'cbrt': lambda d, m: np.where(m, np.nan, d)**(1/3.),
'linear': lambda d, m: np.where(m, np.nan, d),
Expand Down

0 comments on commit 4398ff9

Please sign in to comment.