Skip to content

Commit

Permalink
Prevent edge cases in XArrayInterface from crashing (#5809)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored Jul 15, 2023
1 parent e617d57 commit 003be41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def retrieve_unit_and_label(dim):
packed = True
else:
data = {d: v for d, v in zip(dimensions, data)}
elif isinstance(data, list) and data == []:
elif isinstance(data, (list, np.ndarray)) and len(data) == 0:
dimensions = [d.name for d in kdims + vdims]
data = {d: np.array([]) for d in dimensions[:ndims]}
data.update({d: np.empty((0,) * ndims) for d in dimensions[ndims:]})
Expand Down
1 change: 0 additions & 1 deletion holoviews/core/sheetcoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ def _boundsspec2slicespec(boundsspec,scs):

l_idx = int(np.ceil(l_m-0.5))
t_idx = int(np.ceil(t_m-0.5))
# CBENHANCEMENT: Python 2.6's math.trunc()?
r_idx = int(np.floor(r_m+0.5))
b_idx = int(np.floor(b_m+0.5))

Expand Down
14 changes: 13 additions & 1 deletion holoviews/tests/core/data/test_xarrayinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
except ImportError:
raise SkipTest("Could not import xarray, skipping XArrayInterface tests.")

from holoviews.core.data import Dataset, concat
from holoviews.core.data import Dataset, concat, XArrayInterface
from holoviews.core.dimension import Dimension
from holoviews.core.spaces import HoloMap
from holoviews.element import Image, RGB, HSV, QuadMesh
Expand Down Expand Up @@ -258,6 +258,18 @@ def test_mask_2d_array_transposed(self):
expected[mask] = np.nan
self.assertEqual(masked_array, expected)

def test_from_empty_numpy(self):
"""
Datashader sometimes pass an empty array to the interface
"""
kdims = ["dim_0", "dim_1"]
vdims = ["dim_2"]
ds = XArrayInterface.init(Image, np.array([]), kdims, vdims)
assert isinstance(ds[0], xr.Dataset)
assert ds[0][vdims[0]].size == 0
assert ds[1]["kdims"] == kdims
assert ds[1]["vdims"] == vdims

# Disabled tests for NotImplemented methods
def test_dataset_array_init_hm(self):
"Tests support for arrays (homogeneous)"
Expand Down

0 comments on commit 003be41

Please sign in to comment.