Skip to content

Commit

Permalink
Fixed various bugs canonicalizing arrays (#1526)
Browse files Browse the repository at this point in the history
* Fixed various bugs canonicalizing arrays

* Improved unit test for xarray canonicalization
  • Loading branch information
philippjfr authored and jlstevens committed Jun 15, 2017
1 parent 1c26f67 commit 3259d4b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions holoviews/core/data/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ def canonicalize(cls, dataset, data, coord_dims=None):
data = data[slices] if invert else data

# Transpose data
dims = [name for name in coord_dims[::-1]
dims = [name for name in coord_dims
if isinstance(cls.coords(dataset, name), np.ndarray)]
dropped = [dims.index(d) for d in dims if d not in dataset.kdims]
inds = [dims.index(kd.name)for kd in dataset.kdims]
inds = [i - sum([1 for d in dropped if i>=d]) for i in inds]
if dropped:
data = data.squeeze(axis=tuple(dropped))
if inds:
data = data.transpose(inds)
data = data.transpose(inds[::-1])

# Allow lower dimensional views into data
if len(dataset.kdims) < 2:
Expand Down
5 changes: 2 additions & 3 deletions holoviews/core/data/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,9 @@ def shape(cls, dataset, gridded=False):

@classmethod
def coords(cls, dataset, dim, ordered=False, expanded=False):
dim = dataset.get_dimension(dim, strict=True)
if expanded:
return util.expand_grid_coords(dataset, dim.name)
data = dataset.data.coords(dim.name)[0].points
return util.expand_grid_coords(dataset, dim)
data = dataset.data.coords(dim)[0].points
if ordered and np.all(data[1:] < data[:-1]):
data = data[::-1]
return data
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 @@ -162,7 +162,7 @@ def values(cls, dataset, dim, expanded=True, flat=True):
dim = dataset.get_dimension(dim, strict=True)
data = dataset.data[dim.name].data
if dim in dataset.vdims:
coord_dims = list(dataset.data.dims.keys())[::-1]
coord_dims = list(dataset.data[dim.name].dims)
if dask and isinstance(data, dask.array.Array):
data = data.compute()
data = cls.canonicalize(dataset, data, coord_dims=coord_dims)
Expand Down
22 changes: 20 additions & 2 deletions tests/testdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,12 @@ def test_xarray_dataset_with_scalar_dim_canonicalize(self):
ys = [0.1, 0.2, 0.3]
zs = np.array([[[0, 1], [2, 3], [4, 5]]])
xrarr = xr.DataArray(zs, coords={'x': xs, 'y': ys, 't': [1]}, dims=['t', 'y', 'x'])
ds = Dataset(xrarr, kdims=['x', 'y'], vdims=['z'], datatype=['xarray'])
self.assertEqual(ds.dimension_values(2, flat=False).ndim, 2)
xrds = xr.Dataset({'v': xrarr})
ds = Dataset(xrds, kdims=['x', 'y'], vdims=['v'], datatype=['xarray'])
canonical = ds.dimension_values(2, flat=False)
self.assertEqual(canonical.ndim, 2)
expected = np.array([[0, 1], [2, 3], [4, 5]])
self.assertEqual(canonical, expected)

# Disabled tests for NotImplemented methods
def test_dataset_add_dimensions_values_hm(self):
Expand Down Expand Up @@ -1138,6 +1142,20 @@ def init_column_data(self):
self.dataset_hm_alias = Dataset((self.xs, dask_y),
kdims=[('x', 'X')], vdims=[('y', 'Y')])

def test_xarray_dataset_with_scalar_dim_canonicalize(self):
import dask.array
import xarray as xr
xs = [0, 1]
ys = [0.1, 0.2, 0.3]
zs = dask.array.from_array(np.array([[[0, 1], [2, 3], [4, 5]]]), 2)
xrarr = xr.DataArray(zs, coords={'x': xs, 'y': ys, 't': [1]}, dims=['t', 'y', 'x'])
xrds = xr.Dataset({'v': xrarr})
ds = Dataset(xrds, kdims=['x', 'y'], vdims=['v'], datatype=['xarray'])
canonical = ds.dimension_values(2, flat=False)
self.assertEqual(canonical.ndim, 2)
expected = np.array([[0, 1], [2, 3], [4, 5]])
self.assertEqual(canonical, expected)

def init_grid_data(self):
import dask.array
self.grid_xs = [0, 1]
Expand Down

0 comments on commit 3259d4b

Please sign in to comment.