Skip to content

Commit

Permalink
Ensure empty aggregate does not drop datetimes in shade (#4038)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Oct 7, 2019
1 parent 22cea84 commit 741bf3d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 13 additions & 6 deletions holoviews/operation/datashader.py
Expand Up @@ -11,6 +11,7 @@
import datashader.reductions as rd
import datashader.transfer_functions as tf
import dask.dataframe as dd

from param.parameterized import bothmethod

try:
Expand All @@ -20,7 +21,7 @@
hammer_bundle, connect_edges = object, object

from ..core import (Operation, Element, Dimension, NdOverlay,
CompositeOverlay, Dataset, Overlay)
CompositeOverlay, Dataset, Overlay, OrderedDict)
from ..core.data import PandasInterface, XArrayInterface, DaskInterface
from ..core.util import (
LooseVersion, basestring, cftime_types, cftime_to_timestamp,
Expand Down Expand Up @@ -1204,6 +1205,16 @@ def uint32_to_uint8(cls, img):
return np.flipud(img.view(dtype=np.uint8).reshape(img.shape + (4,)))


@classmethod
def uint32_to_uint8_xr(cls, img):
"""
Cast uint32 xarray DataArray to 4 uint8 channels.
"""
new_array = img.values.view(dtype=np.uint8).reshape(img.shape + (4,))
coords = OrderedDict(list(img.coords.items())+[('band', [0, 1, 2, 3])])
return xr.DataArray(new_array, coords=coords, dims=img.dims+('band',))


@classmethod
def rgb2hex(cls, rgb):
"""
Expand Down Expand Up @@ -1278,10 +1289,6 @@ def _process(self, element, key=None):
elif ds_version > '0.5.0' and self.p.normalization != 'eq_hist':
shade_opts['span'] = element.range(vdim)

for d in kdims:
if array[d.name].dtype.kind == 'M':
array[d.name] = array[d.name].astype('datetime64[us]').astype('int64')

with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'invalid value encountered in true_divide')
if np.isnan(array.data).all():
Expand All @@ -1293,7 +1300,7 @@ def _process(self, element, key=None):
params = dict(get_param_values(element), kdims=kdims,
bounds=bounds, vdims=RGB.vdims[:],
xdensity=xdensity, ydensity=ydensity)
return RGB(self.uint32_to_uint8(img.data), **params)
return RGB(self.uint32_to_uint8_xr(img), **params)



Expand Down
12 changes: 12 additions & 0 deletions holoviews/tests/operation/testdatashader.py
Expand Up @@ -442,6 +442,18 @@ def test_shade_categorical_images_grid(self):
vdims=RGB.vdims+[Dimension('A', range=(0, 1))])
self.assertEqual(shaded, expected)

def test_shade_dt_xaxis_constant_yaxis(self):
df = pd.DataFrame({'y': np.ones(100)}, index=pd.date_range('1980-01-01', periods=100, freq='1T'))
rgb = shade(rasterize(Curve(df), dynamic=False))
xs = np.array(['1980-01-01T00:16:30.000000', '1980-01-01T00:49:30.000000',
'1980-01-01T01:22:30.000000'], dtype='datetime64[us]')
ys = np.array([])
bounds = (np.datetime64('1980-01-01T00:00:00.000000'), 1.0,
np.datetime64('1980-01-01T01:39:00.000000'), 1.0)
expected = RGB((xs, ys, np.empty((0, 3, 4))), ['index', 'y'],
xdensity=1, ydensity=1, bounds=bounds)
self.assertEqual(rgb, expected)



class DatashaderRegridTests(ComparisonTestCase):
Expand Down

0 comments on commit 741bf3d

Please sign in to comment.