Skip to content

Commit

Permalink
🎨 Just improving logic structure. (#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
arafune authored Aug 3, 2024
1 parent a28189d commit 6adedbe
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,33 @@ def retrieve_unit_and_label(dim):
vdims = [vdim]
data = data.to_dataset(name=vdim.name)

if not isinstance(data, (xr.Dataset, xr.DataArray)):
if isinstance(data, (xr.Dataset, xr.DataArray)):
# Started to warn in xarray 2023.12.0:
# The return type of `Dataset.dims` will be changed to return a
# set of dimension names in future, in order to be more consistent
# with `DataArray.dims`. To access a mapping from dimension names to
# lengths, please use `Dataset.sizes`.
data_info = data.sizes if hasattr(data, "sizes") else data.dims
if not data.coords:
data = data.assign_coords(**{k: range(v) for k, v in data_info.items()})
if vdims is None:
vdims = list(data.data_vars)
if kdims is None:
xrdims = list(data_info)
xrcoords = list(data.coords)
kdims = [name for name in data.indexes.keys()
if isinstance(data[name].data, np.ndarray)]
kdims = sorted(kdims, key=lambda x: (xrcoords.index(x) if x in xrcoords else float('inf'), x))
if packed:
kdims = kdims[:-1]
elif set(xrdims) != set(kdims):
virtual_dims = [xd for xd in xrdims if xd not in kdims]
for c in data.coords:
if c not in kdims and set(data[c].dims) == set(virtual_dims):
kdims.append(c)
kdims = [retrieve_unit_and_label(kd) for kd in kdims]
vdims = [retrieve_unit_and_label(vd) for vd in vdims]
else:
if kdims is None:
kdims = kdim_param.default
if vdims is None:
Expand Down Expand Up @@ -176,32 +202,6 @@ def retrieve_unit_and_label(dim):
arr = xr.DataArray(arr, coords=coords, **xr_kwargs)
arrays[vdim.name] = arr
data = xr.Dataset(arrays)
else:
# Started to warn in xarray 2023.12.0:
# The return type of `Dataset.dims` will be changed to return a
# set of dimension names in future, in order to be more consistent
# with `DataArray.dims`. To access a mapping from dimension names to
# lengths, please use `Dataset.sizes`.
data_info = data.sizes if hasattr(data, "sizes") else data.dims
if not data.coords:
data = data.assign_coords(**{k: range(v) for k, v in data_info.items()})
if vdims is None:
vdims = list(data.data_vars)
if kdims is None:
xrdims = list(data_info)
xrcoords = list(data.coords)
kdims = [name for name in data.indexes.keys()
if isinstance(data[name].data, np.ndarray)]
kdims = sorted(kdims, key=lambda x: (xrcoords.index(x) if x in xrcoords else float('inf'), x))
if packed:
kdims = kdims[:-1]
elif set(xrdims) != set(kdims):
virtual_dims = [xd for xd in xrdims if xd not in kdims]
for c in data.coords:
if c not in kdims and set(data[c].dims) == set(virtual_dims):
kdims.append(c)
kdims = [retrieve_unit_and_label(kd) for kd in kdims]
vdims = [retrieve_unit_and_label(vd) for vd in vdims]

not_found = []
for d in kdims:
Expand Down

0 comments on commit 6adedbe

Please sign in to comment.