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

KeyError raised when using combination of checkbox, interactive dataframe, linked_selections, and hover_cols. #6106

Closed
1 task done
spenceforce opened this issue Feb 7, 2024 · 1 comment · Fixed by #6110
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@spenceforce
Copy link

Same error as #5412, but this example doesn't use streamz.

ALL software version info

(this library, plus any other relevant software, e.g. bokeh, python, notebook, OS, browser, etc)

Used a fresh holoviews installation using the instructions from installation page.

Relevant conda environment from mamba list

# Name                    Version                   Build  Channel
bokeh                     3.3.4           py311h92b7b1e_0
colorcet                  3.0.1           py311h06a4308_0
datashader                0.16.0          py311h06a4308_0
holoviews                 1.18.2          py311h06a4308_0
hvplot                    0.9.2           py311h06a4308_0
pandas                    2.1.4           py311ha02d727_0
panel                     1.3.8           py311h06a4308_0
param                     2.0.2           py311h06a4308_0
python                    3.11.7               h955ad1f_0

OS info:

  Operating System: Red Hat Enterprise Linux 8.5 (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8::baseos
            Kernel: Linux 4.18.0-348.20.1.el8_5.x86_64
      Architecture: x86-64

Description of expected behavior and the observed behavior

This error shows up when all conditions below are met:

  1. An interactive dataframe filtered on pn.widgets.CheckBoxGroup.
  2. Multiple plots using the dataframes hvplot plotting methods with the hover_cols argument.
  3. Multiple plots passed to hv.linked_selection.interaction().
  4. Checking a checkbox when all checkboxes are unchecked.

The expected behavior would be for the markers associated with the newly checked checkbox to appear in the plots.

The observed behavior is a KeyError from python and blank plots in the browser.

Note that if hover_cols isn't specified, or only one plot is passed to hv.linked_selection.interaction() this error doesn't occur. There's something about specifying hover_cols and having more than one plot in an interactive layout.

Complete, minimal, self-contained example code that reproduces the issue

I've tested this in a notebook and using the panel serve test.py command on the below code.

# test.py
import holoviews as hv
import pandas as pd
import panel as pn

import hvplot.pandas

df = pd.DataFrame({"category": ["A", "B", "B"], "x": [1, 2, 3], "y": [1, 2, 3]})
dfi = df.interactive()

options = sorted(df["category"].unique())
checkboxes = pn.widgets.CheckBoxGroup(
    name="Checkboxes", value=options, options=options, inline=True
)

filtered = dfi[dfi["category"].isin(checkboxes)]

ls = hv.link_selections.instance()
pn.Column(ls(filtered.hvplot.scatter(x="x", y="y", hover_cols=["category", "x", "y"]) + filtered.hvplot.scatter(x="x", y="y", hover_cols=["category", "x", "y"]))).servable()

Stack traceback and/or browser JavaScript console output

Stack traceback from panel server:

2024-02-07 17:01:08,766 ERROR: panel.reactive - Callback failed for object named "Checkboxes" changing property {'value': ['A']}
Traceback (most recent call last):
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/reactive.py", line 385, in _process_events
    self.param.update(**self_events)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2278, in update
    restore = self_._update(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2318, in _update
    self_._batch_call_watchers()
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2509, in _batch_call_watchers
    self_._execute_watcher(watcher, events)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2471, in _execute_watcher
    watcher.fn(*args, **kwargs)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/streams.py", line 777, in _watcher
    self.trigger([self])
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/streams.py", line 189, in trigger
    subscriber(**dict(union))
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 243, in refresh
    raise e
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 239, in refresh
    self._trigger_refresh(stream_key)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 257, in _trigger_refresh
    self.update(key)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 956, in update
    item = self.__getitem__(key)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 443, in __getitem__
    self.update_frame(frame)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/bokeh/element.py", line 2014, in update_frame
    ranges = self.compute_ranges(self.hmap, key, ranges)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 614, in compute_ranges
    self._compute_group_range(group, elements, ranges, framewise,
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 862, in _compute_group_range
    filtered = [r for i, r in zip(ids, values[g]) if i not in prev_ids]
                                       ~~~~~~^^^
KeyError: 'factors'
2024-02-07 17:01:08,769 Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x7f3bf384ce50>>, <Task finished name='Task-8695' coro=<ServerSession.with_document_locked() done, defined at /home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/bokeh/server/session.py:77> exception=KeyError('factors')>)
Traceback (most recent call last):
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/tornado/ioloop.py", line 738, in _run_callback
    ret = callback()
          ^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/tornado/ioloop.py", line 762, in _discard_future_result
    future.result()
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/bokeh/server/session.py", line 98, in _needs_document_lock_wrapper
    result = await result
             ^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/reactive.py", line 431, in _change_coroutine
    state._handle_exception(e)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/io/state.py", line 442, in _handle_exception
    raise exception
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/reactive.py", line 429, in _change_coroutine
    self._change_event(doc)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/reactive.py", line 447, in _change_event
    self._process_events(events)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/panel/reactive.py", line 385, in _process_events
    self.param.update(**self_events)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2278, in update
    restore = self_._update(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2318, in _update
    self_._batch_call_watchers()
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2509, in _batch_call_watchers
    self_._execute_watcher(watcher, events)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/param/parameterized.py", line 2471, in _execute_watcher
    watcher.fn(*args, **kwargs)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/streams.py", line 777, in _watcher
    self.trigger([self])
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/streams.py", line 189, in trigger
    subscriber(**dict(union))
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 243, in refresh
    raise e
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 239, in refresh
    self._trigger_refresh(stream_key)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 257, in _trigger_refresh
    self.update(key)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 956, in update
    item = self.__getitem__(key)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 443, in __getitem__
    self.update_frame(frame)
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/bokeh/element.py", line 2014, in update_frame
    ranges = self.compute_ranges(self.hmap, key, ranges)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 614, in compute_ranges
    self._compute_group_range(group, elements, ranges, framewise,
  File "/home/user/.anaconda3/envs/holoviz_error/lib/python3.11/site-packages/holoviews/plotting/plot.py", line 862, in _compute_group_range
    filtered = [r for i, r in zip(ids, values[g]) if i not in prev_ids]
                                       ~~~~~~^^^
KeyError: 'factors'

compute_ranges looks to be the culprit.

Screenshots or screencasts of the bug in action

Image of the blank plots. There should be a scatter marker at (1, 1)

image

  • I may be interested in making a pull request to address this
@hoxbro
Copy link
Member

hoxbro commented Feb 9, 2024

I think this can fixed by updating the errored line with: filtered = [r for i, r in zip(ids, values.get(g, [])) if i not in prev_ids]

You are welcome to submit a PR.

@hoxbro hoxbro added the type: bug Something isn't correct or isn't working label Feb 9, 2024
@hoxbro hoxbro added this to the 1.18.2 milestone Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants