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

shade(..., how="eq_hist") with no valid data raises index out of bounds error #1166

Closed
ianthomas23 opened this issue Jan 19, 2023 · 0 comments · Fixed by #1168
Closed

shade(..., how="eq_hist") with no valid data raises index out of bounds error #1166

ianthomas23 opened this issue Jan 19, 2023 · 0 comments · Fixed by #1168
Labels
Milestone

Comments

@ianthomas23
Copy link
Member

First reported in holoviews as holoviz/holoviews#5595.

A simpler datashader-only reproducer is:

import datashader.transfer_functions as tf
import numpy as np
import xarray as xr

agg = xr.DataArray(
    data=np.zeros((2, 2, 2), dtype=np.uint32),
    coords=dict(y=[0, 1], x=[0, 1], cat=['a', 'b']),
)
im = tf.shade(agg, how='eq_hist')
print(im)

which produces:

Traceback (most recent call last):
  File "/Users/iant/github_temp/datashader_temp/empty_cdf.py", line 9, in <module>
    im = tf.shade(agg, how='eq_hist')
  File "/Users/iant/github/datashader/datashader/transfer_functions/__init__.py", line 721, in shade
    return _colorize(agg, color_key, how, alpha, span, min_alpha, name, color_baseline, rescale_discrete_levels)
  File "/Users/iant/github/datashader/datashader/transfer_functions/__init__.py", line 436, in _colorize
    a = _interpolate_alpha(data, total, mask, how, alpha, span, min_alpha, rescale_discrete_levels)
  File "/Users/iant/github/datashader/datashader/transfer_functions/__init__.py", line 474, in _interpolate_alpha
    a_scaled = _normalize_interpolate_how(how)(total - offset, mask)
  File "/Users/iant/github/datashader/datashader/transfer_functions/__init__.py", line 220, in eq_hist
    cdf = cdf / float(cdf[-1])
IndexError: index -1 is out of bounds for axis 0 with size 0

The problem is caused by passing an xr.DataArray that contains all values that will be masked out (i.e. zeros or nans depending on context) to transfer_functions.image(..., how="eq_hist"). This calls the eq_hist() function that is unable to determine a cumulative distribution function as there is no valid data.

The fix is to detect if all the supplied data to eq_hist() is masked out and return to the caller early.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant