Skip to content

Commit

Permalink
Fix for xarray coordinate validation (#5169)
Browse files Browse the repository at this point in the history
* Fix for xarray coordinate validation

* Various fixes

* Expand test case
  • Loading branch information
philippjfr committed Dec 21, 2021
1 parent 27da6a3 commit e203515
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,17 @@ def retrieve_unit_and_label(dim):
# not need to be canonicalized
if any(len(da.coords[c].shape) > 1 for c in da.coords):
continue
undeclared = [
c for c in da.coords if c not in kdims and len(da[c].shape) == 1 and
da[c].shape[0] > 1]
undeclared = []
for c in da.coords:
if c in kdims or len(da[c].shape) != 1 or da[c].shape[0] <= 1:
# Skip if coord is declared, represents irregular coordinates or is constant
continue
elif all(d in kdims for d in da[c].dims):
continue # Skip if coord is alias for another dimension
elif any(all(d in da[kd.name].dims for d in da[c].dims) for kd in kdims):
# Skip if all the dims on the coord are present on another coord
continue
undeclared.append(c)
if undeclared:
raise DataError(
'The coordinates on the %r DataArray do not match the '
Expand Down
8 changes: 8 additions & 0 deletions holoviews/tests/core/data/testxarrayinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def get_multi_dim_irregular_dataset(self):
'time': pd.date_range('2014-09-06', periods=3),
'reference_time': pd.Timestamp('2014-09-05')})

def test_ignore_dependent_dimensions_if_not_specified(self):
coords = OrderedDict([('time', [0, 1]), ('lat', [0, 1]), ('lon', [0, 1])])
da = xr.DataArray(np.arange(8).reshape((2, 2, 2)),
coords, ['time', 'lat', 'lon']).assign_coords(
lat1=xr.DataArray([2,3], dims=['lat']))
assert Dataset(da, ['time', 'lat', 'lon'], vdims='value').kdims == ['time', 'lat', 'lon']
assert Dataset(da, ['time', 'lat1', 'lon'], vdims='value').kdims == ['time', 'lat1', 'lon']

def test_xarray_dataset_irregular_shape(self):
ds = Dataset(self.get_multi_dim_irregular_dataset())
shape = ds.interface.shape(ds, gridded=True)
Expand Down

0 comments on commit e203515

Please sign in to comment.