Skip to content

Commit

Permalink
Added XArray multi-dimensional coordinate validation
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 6, 2018
1 parent 28f2a47 commit 27591fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions holoviews/core/data/xarray.py
Expand Up @@ -115,6 +115,27 @@ def init(cls, eltype, data, kdims, vdims):
return data, {'kdims': kdims, 'vdims': vdims}, {}


@classmethod
def validate(cls, dataset, vdims=True):
Interface.validate(dataset, vdims)
# Check whether irregular (i.e. multi-dimensional) coordinate
# array dimensionality matches
irregular = []
for kd in dataset.kdims:
if cls.irregular(dataset, kd):
irregular.append((kd, dataset.data[kd.name].dims))
if irregular:
nonmatching = ['%s: %s' % (kd, dims) for kd, dims in irregular[1:]
if dims != irregular[0][1]]
if nonmatching:
nonmatching = ['%s: %s' % irregular[0]] + nonmatching
raise DataError("The dimensions of coordinate arrays "
"on irregular data must match. The "
"following kdims were found to have "
"non-matching array dimensions:\n\n%s"
% ('\n'.join(nonmatching)), cls)


@classmethod
def range(cls, dataset, dimension):
dim = dataset.get_dimension(dimension, strict=True).name
Expand Down
14 changes: 14 additions & 0 deletions tests/testbinneddatasets.py
Expand Up @@ -8,6 +8,7 @@

from holoviews.core.spaces import HoloMap
from holoviews.core.data import Dataset
from holoviews.core.data.interface import DataError
from holoviews.element import Histogram, QuadMesh
from holoviews.element.comparison import ComparisonTestCase

Expand Down Expand Up @@ -199,6 +200,19 @@ def test_construct_3d_from_xarray(self):
self.assertEqual(dataset.dimension_values('z', expanded=False), np.array([0, 1]))
self.assertEqual(dataset.dimension_values('A'), zs.T.flatten())

def test_construct_from_xarray_with_invalid_irregular_coordinate_arrays(self):
try:
import xarray as xr
except:
raise SkipError("Test requires xarray")
zs = np.arange(48*6).reshape(2, 4, 6, 6)
da = xr.DataArray(zs, dims=['z', 'y', 'x', 'b'],
coords = {'lat': (('y', 'b'), self.ys),
'lon': (('y', 'x'), self.xs),
'z': [0, 1]}, name='A')
with self.assertRaises(DataError):
Dataset(da, ['z', 'lon', 'lat'])

def test_3d_xarray_with_constant_dim_canonicalized_to_2d(self):
try:
import xarray as xr
Expand Down

0 comments on commit 27591fc

Please sign in to comment.