Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent edge cases in XArrayInterface from crashing #5809

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading