Skip to content

Commit

Permalink
Added tests for categorical aggregates and shading
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 3, 2018
1 parent f07021f commit fbada3a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions holoviews/core/data/image.py
Expand Up @@ -66,14 +66,14 @@ def irregular(cls, dataset, dim):
@classmethod
def shape(cls, dataset, gridded=False):
if gridded:
return dataset.data.shape
return dataset.data.shape[:2]
else:
return cls.length(dataset), len(dataset.dimensions())


@classmethod
def length(cls, dataset):
return np.product(dataset.data.shape)
return np.product(dataset.data.shape[:2])


@classmethod
Expand Down
13 changes: 13 additions & 0 deletions holoviews/operation/datashader.py
Expand Up @@ -752,7 +752,20 @@ def rgb2hex(cls, rgb):
return "#{0:02x}{1:02x}{2:02x}".format(*(int(v*255) for v in rgb))


@classmethod
def to_xarray(cls, element):
if issubclass(element.interface, XArrayInterface):
return element
data = tuple(element.dimension_values(kd, expanded=False)
for kd in element.kdims)
data += tuple(element.dimension_values(vd, flat=False)
for vd in element.vdims)
dtypes = [dt for dt in element.datatype if dt != 'xarray']
return element.clone(data, datatype=['xarray']+dtypes)


def _process(self, element, key=None):
element = element.map(self.to_xarray, Image)
if isinstance(element, NdOverlay):
bounds = element.last.bounds
element = self.concatenate(element)
Expand Down
50 changes: 48 additions & 2 deletions tests/testdatashader.py → tests/operation/testdatashader.py
Expand Up @@ -2,13 +2,15 @@
from nose.plugins.attrib import attr

import numpy as np
from holoviews import Curve, Points, Image, Dataset, RGB, Path, Graph, TriMesh, QuadMesh
from holoviews import (Dimension, Curve, Points, Image, Dataset, RGB, Path,
Graph, TriMesh, QuadMesh, NdOverlay)
from holoviews.element.comparison import ComparisonTestCase

try:
import datashader as ds
from holoviews.operation.datashader import (
aggregate, regrid, ds_version, stack, directly_connect_edges, rasterize
aggregate, regrid, ds_version, stack, directly_connect_edges,
shade, rasterize
)
except:
ds_version = None
Expand Down Expand Up @@ -43,6 +45,17 @@ def test_aggregate_points_sampling(self):
x_sampling=0.5, y_sampling=0.5)
self.assertEqual(img, expected)

def test_aggregate_points_categorical(self):
points = Points([(0.2, 0.3, 'A'), (0.4, 0.7, 'B'), (0, 0.99, 'C')], vdims='z')
img = aggregate(points, dynamic=False, x_range=(0, 1), y_range=(0, 1),
width=2, height=2, aggregator=ds.count_cat('z'))
xs, ys = [0.25, 0.75], [0.25, 0.75]
expected = NdOverlay({'A': Image((xs, ys, [[1, 0], [0, 0]]), vdims='z Count'),
'B': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count'),
'C': Image((xs, ys, [[0, 0], [1, 0]]), vdims='z Count')},
kdims=['z'])
self.assertEqual(img, expected)

def test_aggregate_curve(self):
curve = Curve([(0.2, 0.3), (0.4, 0.7), (0.8, 0.99)])
expected = Image(([0.25, 0.75], [0.25, 0.75], [[1, 0], [1, 1]]),
Expand Down Expand Up @@ -77,6 +90,39 @@ def test_aggregate_dframe_nan_path(self):
self.assertEqual(img, expected)


@attr(optional=1)
class DatashaderShadeTests(ComparisonTestCase):

def test_shade_categorical_images_xarray(self):
xs, ys = [0.25, 0.75], [0.25, 0.75]
data = NdOverlay({'A': Image((xs, ys, [[1, 0], [0, 0]]), datatype=['xarray'], vdims='z Count'),
'B': Image((xs, ys, [[0, 0], [1, 0]]), datatype=['xarray'], vdims='z Count'),
'C': Image((xs, ys, [[0, 0], [1, 0]]), datatype=['xarray'], vdims='z Count')},
kdims=['z'])
shaded = shade(data)
r = [[228, 255], [66, 255]]
g = [[26, 255], [150, 255]]
b = [[28, 255], [129, 255]]
a = [[40, 0], [255, 0]]
expected = RGB((xs, ys, r, g, b, a), datatype=['grid'],
vdims=RGB.vdims+[Dimension('A', range=(0, 1))])
self.assertEqual(shaded, expected)

def test_shade_categorical_images_grid(self):
xs, ys = [0.25, 0.75], [0.25, 0.75]
data = NdOverlay({'A': Image((xs, ys, [[1, 0], [0, 0]]), datatype=['grid'], vdims='z Count'),
'B': Image((xs, ys, [[0, 0], [1, 0]]), datatype=['grid'], vdims='z Count'),
'C': Image((xs, ys, [[0, 0], [1, 0]]), datatype=['grid'], vdims='z Count')},
kdims=['z'])
shaded = shade(data)
r = [[228, 255], [66, 255]]
g = [[26, 255], [150, 255]]
b = [[28, 255], [129, 255]]
a = [[40, 0], [255, 0]]
expected = RGB((xs, ys, r, g, b, a), datatype=['grid'],
vdims=RGB.vdims+[Dimension('A', range=(0, 1))])
self.assertEqual(shaded, expected)



@attr(optional=1)
Expand Down

0 comments on commit fbada3a

Please sign in to comment.