handle xarray dataarrays with no coordinates
- Dominant language
- Python
- Stars
- 1.4k
- Forks
- 124
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 1
Description
#### ALL software version info
hvplot 0.7.1
xarray 0.17.0
python 3.8
#### Description of expected behavior and the observed behavior
Many xarray datasets have labeled dimensions but no coordinate, e.g.
```python
da = xr.DataArray(np.ones((10, 10)), dims=['y', 'x'])
```
Hvplot often produces errors around these datasets that seem unnecessary.
#### Complete, minimal, self-contained example code that reproduces the issue
```
import xarray as xr
import hvplot.xarray
# no coordinates for 2d array: this works fine
da = xr.DataArray(np.ones((10, 10)), dims=['y', 'x'])
da.hvplot.image('x', 'y')
# 3d array with coordinates, also fine
da = xr.DataArray(np.ones((2, 10, 10)), dims=['time', 'y', 'x'],
coords={'time': ('time', np.arange(2)),
'x': ('x', np.arange(10)),
'y': ('y', np.arange(10))})
da.hvplot.image('x', 'y')
# 3d array with some missing coordinates: does not work
da = xr.DataArray(np.ones((2, 10, 10)), dims=['time', 'y', 'x'],
coords={'time': ('time', [0, 1])})
da.hvplot.image('x', 'y')
```
In my mind, this should work exactly the same as the prior example with coordinates.
#### Stack traceback and/or browser JavaScript console output
the final example raises
```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/srv/conda/envs/notebook/lib/python3.8/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
968
969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
971 return None
972 else:
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/dimension.py in _repr_mimebundle_(self, include, exclude)
1315 combined and returned.
1316 """
-> 1317 return Store.render(self)
1318
1319
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/options.py in render(cls, obj)
1403 data, metadata = {}, {}
1404 for hook in hooks:
-> 1405 ret = hook(obj)
1406 if ret is None:
1407 continue
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
280 if not ip.display_formatter.formatters['text/plain'].pprint:
281 return None
--> 282 return display(obj, raw_output=True)
283
284
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw_output, **kwargs)
250 elif isinstance(obj, (CompositeOverlay, ViewableElement)):
251 with option_state(obj):
--> 252 output = element_display(obj)
253 elif isinstance(obj, (Layout, NdLayout, AdjointLayout)):
254 with option_state(obj):
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
144 try:
145 max_frames = OutputSettings.options['max_frames']
--> 146 mimebundle = fn(element, max_frames=max_frames)
147 if mimebundle is None:
148 return {}, {}
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in element_display(element, max_frames)
190 return None
191
--> 192 return render(element)
193
194
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
66 renderer = renderer.instance(fig='png')
67
---> 68 return renderer.components(obj, **kwargs)
69
70
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/renderer.py in components(self, obj, fmt, comm, **kwargs)
408 doc = Document()
409 with config.set(embed=embed):
--> 410 model = plot.layout._render_model(doc, comm)
411 if embed:
412 return render_model(model, comm)
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/viewable.py in _render_model(self, doc, comm)
425 if comm is None:
426 comm = state._comm_manager.get_server_comm()
--> 427 model = self.get_root(doc, comm)
428
429 if config.embed:
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/viewable.py in get_root(self, doc, comm, preprocess)
482 """
483 doc = init_doc(doc)
--> 484 root = self._get_model(doc, comm=comm)
485 if preprocess:
486 self._preprocess(root)
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/layout/base.py in _get_model(self, doc, root, parent, comm)
111 if root is None:
112 root = model
--> 113 objects = self._get_objects(model, [], doc, root, comm)
114 props = dict(self._init_params(), objects=objects)
115 model.update(**self._process_param_change(props))
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/layout/base.py in _get_objects(self, model, old_objects, doc, root, comm)
101 else:
102 try:
--> 103 child = pane._get_model(doc, root, model, comm)
104 except RerenderError:
105 return self._get_objects(model, current_objects[:i], doc, root, comm)
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/pane/holoviews.py in _get_model(self, doc, root, parent, comm)
237 plot = self.object
238 else:
--> 239 plot = self._render(doc, comm, root)
240
241 plot.pane = self
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/pane/holoviews.py in _render(self, doc, comm, root)
302 kwargs['comm'] = comm
303
--> 304 return renderer.get_plot(self.object, **kwargs)
305
306 def _cleanup(self, root):
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/bokeh/renderer.py in get_plot(self_or_cls, obj, doc, renderer, **kwargs)
71 combining the bokeh model with another plot.
72 """
---> 73 plot = super(BokehRenderer, self_or_cls).get_plot(obj, doc, renderer, **kwargs)
74 if plot.document is None:
75 plot.document = Document() if self_or_cls.notebook_context else curdoc()
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/renderer.py in get_plot(self_or_cls, obj, doc, renderer, comm, **kwargs)
241 init_key = tuple(v if d is None else d for v, d in
242 zip(plot.keys[0], defaults))
--> 243 plot.update(init_key)
244 else:
245 plot = obj
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/plot.py in update(self, key)
980 def update(self, key):
981 if len(self) == 1 and ((key == 0) or (key == self.keys[0])) and not self.drawn:
--> 982 return self.initialize_plot()
983 item = self.__getitem__(key)
984 self.traverse(lambda x: setattr(x, '_updated', True))
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/bokeh/element.py in initialize_plot(self, ranges, plot, plots, source)
1403 self.handles['plot'] = plot
1404
-> 1405 self._init_glyphs(plot, element, ranges, source)
1406 if not self.overlaid:
1407 self._update_plot(key, plot, style_element)
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/bokeh/element.py in _init_glyphs(self, plot, element, ranges, source)
1347 else:
1348 style = self.style[self.cyclic_index]
-> 1349 data, mapping, style = self.get_data(element, ranges, style)
1350 current_id = element._plot_id
1351
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/plotting/bokeh/raster.py in get_data(self, element, ranges, style)
110 if i > 2 and 'hover' not in self.handles:
111 break
--> 112 img = element.dimension_values(i, flat=False)
113 if img.dtype.kind == 'b':
114 img = img.astype(np.int8)
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/data/__init__.py in pipelined_fn(*args, **kwargs)
203
204 try:
--> 205 result = method_fn(*args, **kwargs)
206 if PipelineMeta.disable:
207 return result
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/data/__init__.py in dimension_values(self, dimension, expanded, flat)
1103 """
1104 dim = self.get_dimension(dimension, strict=True)
-> 1105 values = self.interface.values(self, dim, expanded, flat)
1106 if dim.nodata is not None:
1107 # Ensure nodata applies to boolean data in py2
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/data/xarray.py in values(cls, dataset, dim, expanded, flat, compute, keep_index)
392 data = cupy.asnumpy(data)
393 if not keep_index:
--> 394 data = cls.canonicalize(dataset, data, data_coords=data_coords,
395 virtual_coords=virtual_coords)
396 return data.T.flatten() if flat and not keep_index else data
/srv/conda/envs/notebook/lib/python3.8/site-packages/holoviews/core/data/grid.py in canonicalize(cls, dataset, data, data_coords, virtual_coords)
338 if d not in dataset.kdims+virtual_coords]
339 if dropped:
--> 340 data = np.squeeze(data, axis=tuple(dropped))
341
342 if not any(cls.irregular(dataset, d) for d in dataset.kdims):
<__array_function__ internals> in squeeze(*args, **kwargs)
/srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in squeeze(a, axis)
1504 return squeeze()
1505 else:
-> 1506 return squeeze(axis=axis)
1507
1508
ValueError: cannot select an axis to squeeze out which has size not equal to one
:Image [x,y] (value)
```
#### Screenshots or screencasts of the bug in action
Contributor guide
Assessment
This issue has not been assessed yet.