Skip to content

Commit

Permalink
Correct implementation of rescale_discrete_levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 authored and jbednar committed May 4, 2022
1 parent cf3a5df commit 1cacb25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 7 additions & 6 deletions datashader/tests/test_transfer_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def create_dask_array_np(*args, **kwargs):
'a': np.array([[0, 4289306879, 4287070463],
[4284834047, 0, 4282597631],
[4280361215, 4278190335, 0]], dtype='u4'),
'b': np.array([[0, 4289306879, 4287070463],
[4285228543, 0, 4282597631],
[4280755711, 4278190335, 0]], dtype='u4')}
'b': np.array([[0, 4291543295, 4288846335],
[4286609919, 0, 4283518207],
[4281281791, 4278190335, 0]], dtype='u4')}
eq_hist_sol_rescale_discrete_levels['c'] = eq_hist_sol_rescale_discrete_levels['b']


Expand Down Expand Up @@ -499,14 +499,15 @@ 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']
img = tf.shade(x, cmap=cmap, how='eq_hist', rescale_discrete_levels=rescale)
if rescale:
sol = np.array([[0xff8d85ff, 0xff716bff, 0xff5450ff],
[0xff3835ff, 0xff8d85ff, 0xff1c1aff],
[0xff0000ff, 0xff8d85ff, 0xff8d85ff]], dtype='uint32')
sol = np.array([[0xff8981ff, 0xff6d67ff, 0xff524dff],
[0xff3633ff, 0xff8981ff, 0xff1b19ff],
[0xff0000ff, 0xff8981ff, 0xff8981ff]], dtype='uint32')
else:
sol = np.array([[0xffcbc0ff, 0xffa299ff, 0xff7973ff],
[0xff514cff, 0xffcbc0ff, 0xff2826ff],
Expand Down
10 changes: 6 additions & 4 deletions datashader/transfer_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,22 @@ def eq_hist(data, mask=None, nbins=256*256):

# Run more accurate value counting if data is of boolean or integer type
# and unique value array is smaller than nbins.
if data2.dtype == bool or (np.issubdtype(data2.dtype, np.integer) and data2.max() < nbins):
if data2.dtype == bool or (np.issubdtype(data2.dtype, np.integer) and data2.ptp() < nbins):
values, counts = np.unique(data2, return_counts=True)
vmin, vmax = values.min(), values.max()
vmin, vmax = values[0], values[-1]
interval = vmax-vmin
bin_centers = np.arange(vmin, vmax+1)
hist = np.zeros(interval+1, dtype='uint64')
hist[values-vmin] = counts
discrete_levels = len(values)
else:
hist, bin_edges = np.histogram(data2, bins=nbins)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
discrete_levels = None
cdf = hist.cumsum()
cdf = cdf / float(cdf[-1])
out = interp(data, bin_centers, cdf).reshape(data.shape)
return out if mask is None else np.where(mask, np.nan, out), data2.max()
return out if mask is None else np.where(mask, np.nan, out), discrete_levels



Expand Down Expand Up @@ -273,7 +275,7 @@ def _interpolate(agg, cmap, how, alpha, span, min_alpha, name, rescale_discrete_
masked_data = np.where(~mask, data, np.nan)
span = np.nanmin(masked_data), np.nanmax(masked_data)

if rescale_discrete_levels: # Only valid for how='eq_hist'
if rescale_discrete_levels and discrete_levels is not None: # Only valid for how='eq_hist'
span = _rescale_discrete_levels(discrete_levels, span)
else:
if how == 'eq_hist':
Expand Down

0 comments on commit 1cacb25

Please sign in to comment.