Skip to content

Commit

Permalink
Handle bug in gridded groupby when dropping dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 20, 2017
1 parent e30f9a2 commit b613c68
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions holoviews/core/data/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def groupby(cls, dataset, dim_names, container_type, group_type, **kwargs):
group_kwargs['kdims'] = kdims
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(kdims)
drop_dim = any(d not in group_kwargs['kdims'] for d in kdims)

# Find all the keys along supplied dimensions
keys = [dataset.data[d.name] for d in dimensions]
Expand All @@ -206,7 +206,7 @@ def groupby(cls, dataset, dim_names, container_type, group_type, **kwargs):
group_data = {dataset.vdims[0].name: np.atleast_1d(group_data)}
for dim, v in zip(dim_names, unique_key):
group_data[dim] = np.atleast_1d(v)
else:
elif not drop_dim:
for vdim in dataset.vdims:
group_data[vdim.name] = np.squeeze(group_data[vdim.name])
group_data = group_type(group_data, **group_kwargs)
Expand Down
5 changes: 3 additions & 2 deletions holoviews/core/data/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def init(cls, eltype, data, kdims, vdims):

@classmethod
def validate(cls, dataset):
pass
if len(dataset.vdims) > 1:
raise ValueError("Iris cubes do not support more than one value dimension")


@classmethod
Expand Down Expand Up @@ -187,7 +188,7 @@ def groupby(cls, dataset, dims, container_type=HoloMap, group_type=None, **kwarg
group_kwargs['kdims'] = slice_dims
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(slice_dims)
drop_dim = any(d not in group_kwargs['kdims'] for d in slice_dims)

unique_coords = product(*[cls.values(dataset, d, expanded=False)
for d in dims])
Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):
kdims=element_dims)
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(element_dims)
drop_dim = any(d not in group_kwargs['kdims'] for d in element_dims)

# XArray 0.7.2 does not support multi-dimensional groupby
# Replace custom implementation when
Expand Down
22 changes: 22 additions & 0 deletions tests/testdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,22 @@ def test_dataset_groupby_drop_dims_dynamic(self):
partial = ds.to(Dataset, kdims=['x'], vdims=['Val'], groupby='y', dynamic=True)
self.assertEqual(partial[19]['Val'], array[:, -1, :].T.flatten())

def test_dataset_groupby_drop_dims_with_vdim(self):
array = np.random.rand(3, 20, 10)
ds = Dataset({'x': range(10), 'y': range(20), 'z': range(3), 'Val': array, 'Val2': array*2},
kdims=['x', 'y', 'z'], vdims=['Val', 'Val2'])
with DatatypeContext([self.datatype, 'columns', 'dataframe']):
partial = ds.to(Dataset, kdims=['Val'], vdims=['Val2'], groupby='y')
self.assertEqual(partial.last['Val'], array[:, -1, :].T.flatten())

def test_dataset_groupby_drop_dims_dynamic_with_vdim(self):
array = np.random.rand(3, 20, 10)
ds = Dataset({'x': range(10), 'y': range(20), 'z': range(3), 'Val': array, 'Val2': array*2},
kdims=['x', 'y', 'z'], vdims=['Val', 'Val2'])
with DatatypeContext([self.datatype, 'columns', 'dataframe']):
partial = ds.to(Dataset, kdims=['Val'], vdims=['Val2'], groupby='y', dynamic=True)
self.assertEqual(partial[19]['Val'], array[:, -1, :].T.flatten())


class IrisDatasetTest(GridDatasetTest):
"""
Expand Down Expand Up @@ -929,6 +945,12 @@ def test_dataset_sample_hm(self):
def test_dataset_sample_hm_alias(self):
raise SkipTest("Not supported")

def test_dataset_groupby_drop_dims_with_vdim(self):
raise SkipTest("Not supported")

def test_dataset_groupby_drop_dims_dynamic_with_vdim(self):
raise SkipTest("Not supported")


class XArrayDatasetTest(GridDatasetTest):
"""
Expand Down

0 comments on commit b613c68

Please sign in to comment.