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

feat(bokeh): Server side HoverTool for rasterized/datashaded plots with selector #6422

Open
wants to merge 62 commits into
base: main
Choose a base branch
from

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Oct 25, 2024

Previously, we sent all hover data to the front end, which could lead to significant overhead on larger plots. With this PR, the data is pushed server-side to the hover tool with a custom data model.

script
import itertools

import datashader as ds
import numpy as np
import pandas as pd
import panel as pn

import holoviews as hv
from holoviews.operation.datashader import datashade, dynspread, rasterize

hv.extension("bokeh")

num = 10000
seed = np.random.default_rng(1)


dists = {
    cat: pd.DataFrame(
        {
            "x": seed.normal(x, s, num),
            "y": seed.normal(y, s, num),
            "s": s,
            "val": val,
            "cat": cat,
        }
    )
    for x, y, s, val, cat in [
        (2, 2, 0.03, 0, "d1"),
        (2, -2, 0.10, 1, "d2"),
        (-2, -2, 0.50, 2, "d3"),
        (-2, 2, 1.00, 3, "d4"),
        (0, 0, 3.00, 4, "d5"),
    ]
}

df = pd.concat(dists, ignore_index=True)
df["y"] += 100  # Offset to distinguish x from y
points = hv.Points(df)


def dynspread_datashade(*args, **kwargs):
    return dynspread(datashade(*args, **kwargs))


def dynspread_rasterize(*args, **kwargs):
    return dynspread(rasterize(*args, **kwargs))


ops = (rasterize, dynspread_rasterize, datashade, dynspread_datashade)
aggs = (ds.min("s"), ds.by("cat"))
sels = (None, ds.min("s"))
# ops = (datashade,)
# aggs = (ds.by("cat"),)
# sels = (None,)
combinations = itertools.product(ops, aggs, sels)
plots = []
for op, agg, sel in combinations:
    title = f"{op.__name__.replace('_','+')}(agg={type(agg).__name__}, sel={type(sel).__name__ if sel else None})"
    plot = op(points, aggregator=agg, selector=sel).opts(
        tools=["hover"], title=title, width=400, height=400
    )
    plots.append(plot)

pn.panel(hv.Layout(plots).cols(4).opts(shared_axes=False)).servable()

image

@hoxbro hoxbro added the type: enhancement Minor feature or improvement to an existing feature label Oct 25, 2024
Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 94.52055% with 16 lines in your changes missing coverage. Please review.

Project coverage is 88.79%. Comparing base (1a73b88) to head (48f91a5).

Files with missing lines Patch % Lines
holoviews/plotting/bokeh/raster.py 83.33% 10 Missing ⚠️
holoviews/operation/datashader.py 95.41% 5 Missing ⚠️
holoviews/element/raster.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6422      +/-   ##
==========================================
+ Coverage   88.76%   88.79%   +0.02%     
==========================================
  Files         323      323              
  Lines       68679    68885     +206     
==========================================
+ Hits        60963    61166     +203     
- Misses       7716     7719       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hoxbro hoxbro changed the title enh: Update hovertool server side for rasterized plots enh: Make HoverTool update server side for rasterized/datashaded plots Oct 28, 2024
@hoxbro hoxbro changed the title enh: Make HoverTool update server side for rasterized/datashaded plots feat: Make HoverTool update server side for rasterized/datashaded plots Nov 5, 2024
@hoxbro hoxbro marked this pull request as ready for review November 19, 2024 19:29
@hoxbro hoxbro changed the title feat: Server side HoverTool for rasterized/datashaded plots with selector feat(bokeh): Server side HoverTool for rasterized/datashaded plots with selector Nov 21, 2024
holoviews/operation/datashader.py Show resolved Hide resolved
Comment on lines +1763 to +1766
# TODO: Investigate why this does not work
# element = element.clone(data=new_data, kdims=element.vdims.copy(), vdims=element.vdims.copy())
element = element.clone()
element.data = new_data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't understand this.

holoviews/plotting/bokeh/raster.py Show resolved Hide resolved
holoviews/tests/ui/bokeh/test_hover.py Show resolved Hide resolved
@hoxbro hoxbro requested a review from philippjfr December 2, 2024 10:18
@hoxbro hoxbro added this to the 1.20.1 milestone Dec 9, 2024
(isinstance(data, dict) and (*map(dimension_name, vdims), alpha.name) in data)):
(isinstance(data, dict) and (*map(dimension_name, vdims), alpha.name) in data) or
str(alpha) in getattr(data, "data_vars", [])
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it wasn't already but that's getting quite unwieldy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case please refactor 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. Do you want it to be put into a method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored in 8f4378e

Which is what I think you mean

@hoxbro hoxbro modified the milestones: 1.20.1, 1.21.0 Dec 10, 2024
@philippjfr
Copy link
Member

Let's remove the RGBA value by default when the index layer is enabled.

@hoxbro
Copy link
Member Author

hoxbro commented Dec 17, 2024

Let's remove the RGBA value by default when the index layer is enabled.

Done in 48f91a5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Minor feature or improvement to an existing feature
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

2 participants