Skip to content

Commit

Permalink
Added handling for isocontour kdims on MultiInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Aug 13, 2017
1 parent f39702f commit 1965e97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
29 changes: 25 additions & 4 deletions holoviews/core/data/multipath.py
Expand Up @@ -45,7 +45,17 @@ def init(cls, eltype, data, kdims, vdims):

@classmethod
def validate(cls, dataset):
pass
# Ensure that auxilliary key dimensions on each subpaths are scalar
if dataset.ndims <= 2:
return
ds = cls._inner_dataset_template(dataset)
for d in dataset.data:
ds.data = d
for dim in dataset.kdims[2:]:
if len(ds.dimension_values(dim, expanded=False)) > 1:
raise ValueError("'%s' key dimension value must have a constant value on each subpath, "
"for paths with value for each coordinate of the array declare a "
"value dimension instead." % dim)

@classmethod
def _inner_dataset_template(cls, dataset):
Expand Down Expand Up @@ -163,18 +173,29 @@ def redim(cls, dataset, dimensions):
def values(cls, dataset, dimension, expanded, flat):
"""
Returns a single concatenated array of all subpaths separated
by NaN values. If not expanded NaN separators are not included.
by NaN values. If expanded keyword is False an array of arrays
is returned.
"""
if not dataset.data:
return np.array([])
values = []
ds = cls._inner_dataset_template(dataset)
didx = dataset.get_dimension_index(dimension)
for d in dataset.data:
ds.data = d
values.append(ds.interface.values(ds, dimension, expanded, flat))
expand = expanded if didx>1 and dimension in dataset.kdims else True
dvals = ds.interface.values(ds, dimension, expand, flat)
values.append(dvals)
if expanded:
values.append([np.NaN])
return np.concatenate(values[:-1] if expanded else values) if values else []
elif not expand and len(dvals):
values[-1] = dvals[0]
if not values:
return np.array()
elif expanded:
return np.concatenate(values[:-1])
else:
return np.array(values)

@classmethod
def split(cls, dataset, start, end):
Expand Down
2 changes: 1 addition & 1 deletion holoviews/element/path.py
Expand Up @@ -31,7 +31,7 @@ class Path(Dataset, Element2D):
"""

kdims = param.List(default=[Dimension('x'), Dimension('y')],
constant=True, bounds=(2, 2), doc="""
constant=True, bounds=(2, None), doc="""
The label of the x- and y-dimension of the Image in form
of a string or dimension object.""")

Expand Down
23 changes: 21 additions & 2 deletions tests/testmultiinterface.py
Expand Up @@ -69,12 +69,22 @@ def test_multi_array_shape(self):
arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]) for i in range(2)]
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])
self.assertEqual(mds.shape, (5, 2))

def test_multi_array_values(self):
arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]) for i in range(2)]
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])
self.assertEqual(mds.dimension_values(0), np.array([0., 1, np.NaN, 1, 2]))

def test_multi_array_values_coordinates_nonexpanded(self):
arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]) for i in range(2)]
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])
self.assertEqual(mds.dimension_values(0, expanded=False), np.array([[0., 1], [1, 2]]))

def test_multi_array_values_coordinates_nonexpanded_constant_kdim(self):
arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2), np.ones(2)*i]) for i in range(2)]
mds = Path(arrays, kdims=['x', 'y', 'z'], datatype=['multitabular'])
self.assertEqual(mds.dimension_values(2, expanded=False), np.array([0, 1]))

def test_multi_array_redim(self):
arrays = [np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]) for i in range(2)]
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular']).redim(x='x2')
Expand All @@ -87,10 +97,19 @@ def test_multi_mixed_interface_raises(self):
error = "None of the available storage backends were able to support the supplied data format."
with self.assertRaisesRegexp(ValueError, error):
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])

def test_multi_mixed_dims_raises(self):
arrays = [{'x': range(10), 'y' if j else 'z': range(10)}
for i in range(2) for j in range(2)]
error = "None of the available storage backends were able to support the supplied data format."
with self.assertRaisesRegexp(ValueError, error):
mds = Path(arrays, kdims=['x', 'y'], datatype=['multitabular'])

def test_multi_nonconstant_kdims_raises(self):
arrays = [{'x': range(10), 'y': range(10), 'z': range(10)}
for i in range(2)]
error = ("z' key dimension value must have a constant value on each subpath, "
"for paths with value for each coordinate of the array declare a "
"value dimension instead.")
with self.assertRaisesRegexp(ValueError, error):
mds = Path(arrays, kdims=['x', 'y', 'z'], datatype=['multitabular'])

0 comments on commit 1965e97

Please sign in to comment.