Skip to content

Commit

Permalink
Datashader fixes (#2344)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 16, 2018
1 parent 5475da3 commit a3eb99e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
10 changes: 8 additions & 2 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,14 @@ def dt_to_int(value, time_unit='us'):
"""
Converts a datetime type to an integer with the supplied time unit.
"""
if time_unit == 'ns':
tscale = 1./np.timedelta64(1, time_unit).tolist()
if isinstance(value, np.datetime64):
value = np.datetime64(value, 'ns')
if time_unit == 'ns':
tscale = 1
else:
tscale = (np.timedelta64(1, time_unit)/np.timedelta64(1, 'ns')) * 1000.
elif time_unit == 'ns':
tscale = 1000.
else:
tscale = 1./np.timedelta64(1, time_unit).tolist().total_seconds()
if pd and isinstance(value, pd.Timestamp):
Expand Down
10 changes: 7 additions & 3 deletions holoviews/operation/datashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def _get_sampling(self, element, x, y):
ytype = 'datetime'
elif not np.isfinite(ystart) and not np.isfinite(yend):
if element.get_dimension_type(y) in datetime_types:
xstart, xend = 0, 10000
xtype = 'datetime'
ystart, yend = 0, 10000
ytype = 'datetime'
else:
ystart, yend = 0, 1
elif ystart == yend:
Expand Down Expand Up @@ -239,7 +239,7 @@ def get_agg_data(cls, obj, category=None):
paths = [p.compute() if isinstance(p, dd.DataFrame) else p for p in paths]
df = pd.concat(paths)
else:
df = paths[0]
df = paths[0] if paths else pd.DataFrame([], columns=[x.name, y.name])
if category and df[category].dtype.name != 'category':
df[category] = df[category].astype('category')

Expand Down Expand Up @@ -338,6 +338,10 @@ def _process(self, element, key=None):
xarray = xr.DataArray(np.full((height, width), np.NaN, dtype=np.float32),
dims=['y', 'x'], coords={'x': xs, 'y': ys})
return self.p.element_type(xarray)
elif not len(data):
xarray = xr.DataArray(np.full((height, width), np.NaN, dtype=np.float32),
dims=[y.name, x.name], coords={x.name: xs, y.name: ys})
return self.p.element_type(xarray)

cvs = ds.Canvas(plot_width=width, plot_height=height,
x_range=x_range, y_range=y_range)
Expand Down
14 changes: 14 additions & 0 deletions tests/operation/testdatashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def test_aggregate_curve_datetimes(self):
datatype=['xarray'], bounds=bounds, vdims='Count')
self.assertEqual(img, expected)

def test_aggregate_curve_datetimes_microsecond_timebase(self):
dates = pd.date_range(start="2016-01-01", end="2016-01-03", freq='1D')
xstart = np.datetime64('2015-12-31T23:59:59.723518000', 'us')
xend = np.datetime64('2016-01-03T00:00:00.276482000', 'us')
curve = Curve((dates, [1, 2, 3]))
img = aggregate(curve, width=2, height=2, x_range=(xstart, xend), dynamic=False)
bounds = (np.datetime64('2015-12-31T23:59:59.585277000'), 1.0,
np.datetime64('2016-01-03T00:00:00.414723000'), 3.0)
dates = [np.datetime64('2016-01-01T11:59:59.861759000',),
np.datetime64('2016-01-02T12:00:00.138241000')]
expected = Image((dates, [1.5, 2.5], [[1, 0], [0, 2]]),
datatype=['xarray'], bounds=bounds, vdims='Count')
self.assertEqual(img, expected)

def test_aggregate_ndoverlay(self):
ds = Dataset([(0.2, 0.3, 0), (0.4, 0.7, 1), (0, 0.99, 2)], kdims=['x', 'y', 'z'])
ndoverlay = ds.to(Points, ['x', 'y'], [], 'z').overlay()
Expand Down
28 changes: 26 additions & 2 deletions tests/testcoreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,30 @@ def test_datetime_to_us_int(self):
dt = datetime.datetime(2017, 1, 1)
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime64_to_us_int(self):
def test_datetime64_s_to_ns_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 's')
self.assertEqual(dt_to_int(dt, 'ns'), 1483228800000000000000.0)

def test_datetime64_us_to_ns_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 'us')
self.assertEqual(dt_to_int(dt, 'ns'), 1483228800000000000000.0)

def test_datetime64_to_ns_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt, 'ns'), 1483228800000000000000.0)

def test_datetime64_us_to_us_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 'us')
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime64_s_to_us_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 's')
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_timestamp_to_us_int(self):
dt = pd.Timestamp(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime_to_s_int(self):
dt = datetime.datetime(2017, 1, 1)
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)
Expand All @@ -574,6 +590,14 @@ def test_datetime64_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_datetime64_us_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 'us')
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_datetime64_s_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 's')
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_timestamp_to_s_int(self):
dt = pd.Timestamp(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)
Expand Down

0 comments on commit a3eb99e

Please sign in to comment.