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

Support hover_cols with rasterize #1206

Open
balshersingh10 opened this issue Dec 1, 2023 · 2 comments
Open

Support hover_cols with rasterize #1206

balshersingh10 opened this issue Dec 1, 2023 · 2 comments
Labels
type: enhancement New feature or request
Milestone

Comments

@balshersingh10
Copy link

balshersingh10 commented Dec 1, 2023

ALL software version info

Details

Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
asttokens                 2.4.1              pyhd8ed1ab_0    conda-forge
attrs                     23.1.0             pyh71513ae_1    conda-forge
bleach                    6.1.0              pyhd8ed1ab_0    conda-forge
blosc                     1.21.5               h0f2a231_0    conda-forge
bokeh                     3.3.1              pyhd8ed1ab_0    conda-forge
boto3                     1.29.5             pyhd8ed1ab_0    conda-forge
botocore                  1.32.5             pyhd8ed1ab_0    conda-forge
branca                    0.7.0              pyhd8ed1ab_1    conda-forge
brotli                    1.1.0                hd590300_1    conda-forge
brotli-bin                1.1.0                hd590300_1    conda-forge
brotli-python             1.1.0           py311hb755f60_1    conda-forge
bzip2                     1.0.8                h7b6447c_0  
c-ares                    1.22.1               hd590300_0    conda-forge
ca-certificates           2023.08.22           h06a4308_0  
cairo                     1.18.0               h3faef2a_0    conda-forge
cartopy                   0.22.0          py311h320fe9a_1    conda-forge
certifi                   2023.11.17      py311h06a4308_0  
cfitsio                   4.3.0                hbdc6101_0    conda-forge
cftime                    1.6.3           py311h1f0f07a_0    conda-forge
charset-normalizer        3.3.2              pyhd8ed1ab_0    conda-forge
click                     8.1.7           unix_pyh707e725_0    conda-forge
click-plugins             1.1.1                      py_0    conda-forge
cligj                     0.7.2              pyhd8ed1ab_1    conda-forge
cloudpickle               3.0.0              pyhd8ed1ab_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
colorcet                  3.0.1              pyhd8ed1ab_0    conda-forge
comm                      0.1.4              pyhd8ed1ab_0    conda-forge
connectorx                0.3.2           py311h4aa2743_3    conda-forge
contourpy                 1.2.0           py311h9547e67_0    conda-forge
cycler                    0.12.1             pyhd8ed1ab_0    conda-forge
dask-core                 2023.11.0          pyhd8ed1ab_0    conda-forge
datashader                0.16.0             pyhd8ed1ab_0    conda-forge
debugpy                   1.8.0           py311hb755f60_1    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
et_xmlfile                1.1.0           py311h06a4308_0  
exceptiongroup            1.2.0              pyhd8ed1ab_0    conda-forge
executing                 2.0.1              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                hcb278e6_1    conda-forge
fiona                     1.9.5           py311hbac4ec9_0    conda-forge
folium                    0.15.0             pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.2               h14ed4e7_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.45.0          py311h459d7ec_0    conda-forge
freetype                  2.12.1               h267a509_2    conda-forge
freexl                    2.0.0                h743c826_0    conda-forge
fsspec                    2023.10.0          pyhca7485f_0    conda-forge
gdal                      3.7.2           py311h815a124_5    conda-forge
geopandas                 0.14.1             pyhd8ed1ab_0    conda-forge
geopandas-base            0.14.1             pyha770c72_0    conda-forge
geos                      3.12.0               h59595ed_0    conda-forge
geotiff                   1.7.1               hee599c5_13    conda-forge
geoviews                  1.11.0             pyhd8ed1ab_0    conda-forge
geoviews-core             1.11.0             pyha770c72_0    conda-forge
gettext                   0.21.1               h27087fc_0    conda-forge
giflib                    5.2.1                h0b41bf4_3    conda-forge
greenlet                  3.0.1           py311hb755f60_0    conda-forge
hdf4                      4.2.15               h501b40f_6    conda-forge
hdf5                      1.14.2          nompi_h4f84152_100    conda-forge
holoviews                 1.18.1             pyhd8ed1ab_0    conda-forge
hvplot                    0.9.0              pyhd8ed1ab_0    conda-forge
icu                       73.2                 h59595ed_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.8.0              pyha770c72_0    conda-forge
importlib_metadata        6.8.0                hd8ed1ab_0    conda-forge
ipykernel                 6.26.0             pyhf8b6a83_0    conda-forge
ipython                   8.18.0             pyh0d859eb_0    conda-forge
ipywidgets                8.0.4           py311h06a4308_0  
jedi                      0.19.1             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
jmespath                  1.0.1              pyhd8ed1ab_0    conda-forge
joblib                    1.3.2              pyhd8ed1ab_0    conda-forge
json-c                    0.17                 h7ab15ed_0    conda-forge
jupyter_bokeh             3.0.7           py311h5eee18b_0  
jupyter_client            8.6.0              pyhd8ed1ab_0    conda-forge
jupyter_core              5.5.0           py311h38be061_0    conda-forge
jupyterlab_widgets        3.0.9           py311h06a4308_0  
kealib                    1.5.2                hcd42e92_1    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
kiwisolver                1.4.5           py311h9547e67_1    conda-forge
krb5                      1.21.2               h659d440_0    conda-forge
lcms2                     2.15                 h7f713cb_2    conda-forge
ld_impl_linux-64          2.38                 h1181459_1  
lerc                      4.0.0                h27087fc_0    conda-forge
libaec                    1.1.2                h59595ed_1    conda-forge
libarchive                3.7.2                h039dbb9_0    conda-forge
libblas                   3.9.0           20_linux64_openblas    conda-forge
libboost-headers          1.82.0               ha770c72_6    conda-forge
libbrotlicommon           1.1.0                hd590300_1    conda-forge
libbrotlidec              1.1.0                hd590300_1    conda-forge
libbrotlienc              1.1.0                hd590300_1    conda-forge
libcblas                  3.9.0           20_linux64_openblas    conda-forge
libcurl                   8.4.0                hca28451_0    conda-forge
libdeflate                1.19                 hd590300_0    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libev                     4.33                 h516909a_1    conda-forge
libexpat                  2.5.0                hcb278e6_1    conda-forge
libffi                    3.4.4                h6a678d5_0  
libgcc-ng                 13.2.0               h807b86a_3    conda-forge
libgdal                   3.7.2                h6238fc3_5    conda-forge
libgfortran-ng            13.2.0               h69a702a_3    conda-forge
libgfortran5              13.2.0               ha4646dd_3    conda-forge
libglib                   2.78.1               hebfc3b9_0    conda-forge
libgomp                   13.2.0               h807b86a_3    conda-forge
libiconv                  1.17                 h166bdaf_0    conda-forge
libjpeg-turbo             2.1.5.1              hd590300_1    conda-forge
libkml                    1.3.0             h01aab08_1018    conda-forge
liblapack                 3.9.0           20_linux64_openblas    conda-forge
libllvm14                 14.0.6               hcd5def8_4    conda-forge
libnetcdf                 4.9.2           nompi_h80fb2b6_112    conda-forge
libnghttp2                1.58.0               h47da74e_0    conda-forge
libnsl                    2.0.1                hd590300_0    conda-forge
libopenblas               0.3.25          pthreads_h413a1c8_0    conda-forge
libpng                    1.6.39               h753d276_0    conda-forge
libpq                     16.1                 hfc447b1_0    conda-forge
librttopo                 1.1.0               hb58d41b_14    conda-forge
libsodium                 1.0.18               h36c2ea0_1    conda-forge
libspatialindex           1.9.3                h9c3ff4c_4    conda-forge
libspatialite             5.1.0                h090f1da_1    conda-forge
libsqlite                 3.44.0               h2797004_0    conda-forge
libssh2                   1.11.0               h0841786_0    conda-forge
libstdcxx-ng              13.2.0               h7e041cc_3    conda-forge
libtiff                   4.6.0                h29866fb_1    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libwebp-base              1.3.2                hd590300_0    conda-forge
libxcb                    1.15                 h0b41bf4_0    conda-forge
libxml2                   2.11.6               h232c23b_0    conda-forge
libzip                    1.10.1               h2629f0a_3    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
linkify-it-py             2.0.0              pyhd8ed1ab_0    conda-forge
llvmlite                  0.41.1          py311ha6695c7_0    conda-forge
locket                    1.0.0              pyhd8ed1ab_0    conda-forge
lz4-c                     1.9.4                hcb278e6_0    conda-forge
lzo                       2.10              h516909a_1000    conda-forge
mapclassify               2.6.1              pyhd8ed1ab_0    conda-forge
markdown                  3.5.1              pyhd8ed1ab_0    conda-forge
markdown-it-py            3.0.0              pyhd8ed1ab_0    conda-forge
markupsafe                2.1.3           py311h459d7ec_1    conda-forge
matplotlib-base           3.8.0           py311h54ef318_1    conda-forge
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
mdit-py-plugins           0.4.0              pyhd8ed1ab_0    conda-forge
mdurl                     0.1.0              pyhd8ed1ab_0    conda-forge
minizip                   4.0.3                h0ab5242_0    conda-forge
multipledispatch          0.6.0                      py_0    conda-forge
munch                     4.0.0              pyhd8ed1ab_0    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
ncurses                   6.4                  h6a678d5_0  
nest-asyncio              1.5.8              pyhd8ed1ab_0    conda-forge
netcdf4                   1.6.5           nompi_py311he8ad708_100    conda-forge
networkx                  3.2.1              pyhd8ed1ab_0    conda-forge
nspr                      4.35                 h27087fc_0    conda-forge
nss                       3.94                 h1d7d5a4_0    conda-forge
numba                     0.58.1          py311h96b013e_0    conda-forge
numpy                     1.26.0          py311h64a7726_0    conda-forge
ocre-python-library       1.37.1                   pypi_0    pypi
openjpeg                  2.5.0                h488ebb8_3    conda-forge
openpyxl                  3.0.10          py311h5eee18b_0  
openssl                   3.1.4                hd590300_0    conda-forge
packaging                 23.2               pyhd8ed1ab_0    conda-forge
pandas                    2.1.3           py311h320fe9a_0    conda-forge
panel                     1.3.1              pyhd8ed1ab_0    conda-forge
param                     2.0.1              pyhca7485f_0    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
partd                     1.4.1              pyhd8ed1ab_0    conda-forge
pcre2                     10.40                hc3806b6_0    conda-forge
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    10.0.1          py311h8aef010_1    conda-forge
pip                       23.3            py311h06a4308_0  
pixman                    0.42.2               h59595ed_0    conda-forge
platformdirs              4.0.0              pyhd8ed1ab_0    conda-forge
plotly                    5.18.0             pyhd8ed1ab_0    conda-forge
poppler                   23.08.0              hf2349cb_2    conda-forge
poppler-data              0.4.12               hd8ed1ab_0    conda-forge
postgresql                16.1                 h8972f4a_0    conda-forge
proj                      9.3.0                h1d62c97_2    conda-forge
prompt-toolkit            3.0.41             pyha770c72_0    conda-forge
prompt_toolkit            3.0.41               hd8ed1ab_0    conda-forge
psutil                    5.9.5           py311h459d7ec_1    conda-forge
psycopg2                  2.9.7           py311h03dec38_1    conda-forge
pthread-stubs             0.4               h36c2ea0_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pyct                      0.4.6                      py_0    conda-forge
pyct-core                 0.4.6                      py_0    conda-forge
pygments                  2.17.2             pyhd8ed1ab_0    conda-forge
pyparsing                 3.1.1              pyhd8ed1ab_0    conda-forge
pypdf2                    3.0.1                    pypi_0    pypi
pyproj                    3.6.1           py311h1facc83_4    conda-forge
pyqt5                     5.15.10                  pypi_0    pypi
pyqt5-qt5                 5.15.2                   pypi_0    pypi
pyqt5-sip                 12.13.0                  pypi_0    pypi
pyshp                     2.3.1              pyhd8ed1ab_0    conda-forge
pyside2                   5.13.2                   pypi_0    pypi
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.11.5          hab00c5b_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-tzdata             2023.3             pyhd8ed1ab_0    conda-forge
python_abi                3.11                    4_cp311    conda-forge
pytz                      2023.3.post1       pyhd8ed1ab_0    conda-forge
pyviz_comms               3.0.0              pyhd8ed1ab_0    conda-forge
pyyaml                    6.0.1           py311h459d7ec_1    conda-forge
pyzmq                     25.1.1          py311h34ded2d_2    conda-forge
readline                  8.2                  h5eee18b_0  
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
rtree                     1.1.0           py311h3bb2b0f_0    conda-forge
s3transfer                0.7.0              pyhd8ed1ab_0    conda-forge
scikit-learn              1.3.2           py311hc009520_1    conda-forge
scipy                     1.11.3          py311h64a7726_1    conda-forge
setuptools                68.0.0          py311h06a4308_0  
shapely                   2.0.2           py311he06c224_0    conda-forge
shiboken2                 5.13.2                   pypi_0    pypi
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.10               h9fff704_0    conda-forge
sqlalchemy                2.0.23          py311h459d7ec_0    conda-forge
sqlite                    3.41.2               h5eee18b_0  
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
tenacity                  8.2.3              pyhd8ed1ab_0    conda-forge
threadpoolctl             3.2.0              pyha21a80b_0    conda-forge
tiledb                    2.16.3               hf0b6e87_3    conda-forge
tk                        8.6.12               h1ccaba5_0  
toolz                     0.12.0             pyhd8ed1ab_0    conda-forge
tornado                   6.3.3           py311h459d7ec_1    conda-forge
tqdm                      4.66.1             pyhd8ed1ab_0    conda-forge
traitlets                 5.13.0             pyhd8ed1ab_0    conda-forge
typing-extensions         4.8.0                hd8ed1ab_0    conda-forge
typing_extensions         4.8.0              pyha770c72_0    conda-forge
tzcode                    2023c                h0b41bf4_0    conda-forge
tzdata                    2023c                h04d1e81_0  
uc-micro-py               1.0.1              pyhd8ed1ab_0    conda-forge
uriparser                 0.9.7                hcb278e6_1    conda-forge
urllib3                   1.26.18            pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.12             pyhd8ed1ab_0    conda-forge
webencodings              0.5.1              pyhd8ed1ab_2    conda-forge
wheel                     0.41.2          py311h06a4308_0  
widgetsnbextension        4.0.5           py311h06a4308_0  
xarray                    2023.11.0          pyhd8ed1ab_0    conda-forge
xerces-c                  3.2.4                hac6953d_3    conda-forge
xlrd                      2.0.1              pyhd8ed1ab_3    conda-forge
xorg-kbproto              1.0.7             h7f98852_1002    conda-forge
xorg-libice               1.1.1                hd590300_0    conda-forge
xorg-libsm                1.2.4                h7391055_0    conda-forge
xorg-libx11               1.8.7                h8ee46fc_0    conda-forge
xorg-libxau               1.0.11               hd590300_0    conda-forge
xorg-libxdmcp             1.1.3                h7f98852_0    conda-forge
xorg-libxext              1.3.4                h0b41bf4_2    conda-forge
xorg-libxrender           0.9.11               hd590300_0    conda-forge
xorg-renderproto          0.11.1            h7f98852_1002    conda-forge
xorg-xextproto            7.3.0             h0b41bf4_1003    conda-forge
xorg-xproto               7.0.31            h7f98852_1007    conda-forge
xyzservices               2023.10.1          pyhd8ed1ab_0    conda-forge
xz                        5.4.2                h5eee18b_0  
yaml                      0.2.5                h7f98852_2    conda-forge
zeromq                    4.3.5                h59595ed_0    conda-forge
zipp                      3.17.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               hd590300_5    conda-forge
zstd                      1.5.5                hfc55251_0    conda-forge

Description of expected behavior and the observed behavior

When hover tooltip is enabled, Timestamp not working. I discussed this issue in discourse, Andrew helped me to fix some things, still Timestamp is not working.
https://discourse.holoviz.org/t/how-to-set-up-hovertool-for-multiple-columns-with-hvplot/4294/16?u=balsher.singh

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

import pandas as pd
import hvplot.pandas
import numpy as np
from bokeh.models import HoverTool, CustomJSHover
import holoviews as hv
import hvplot
import panel as pn
import geoviews as gv
gv.extension('bokeh')

# Create a dummy DataFrame
np.random.seed(0)
n = 10000  # number of points
df = pd.DataFrame({
    'Longitude': np.random.uniform(-180, 180, n),
    'Latitude': np.random.uniform(-90, 90, n),
    'Timestamp': pd.date_range(start='2023-01-01', periods=n, freq='D'),
    'Value': np.random.rand(n) * 100  # This will represent 'selected_col'
})


selected_col = 'Value'
tile = hv.element.tiles.EsriNatGeo().opts(width=1000, height=650)
_hover_code = """
    const projections = Bokeh.require("core/util/projections");
    const {snap_x, snap_y} = special_vars
    const coords = projections.wgs84_mercator.invert(snap_x, snap_y)
    return "" + (coords[%d]).toFixed(4)
"""
hover = HoverTool(
    tooltips=[
        ("Timestamp", '@{Timestamp}{%F %T}'),
        ("(Longitude,Latitude)", "$x{custom}, $y{custom}"),
        (selected_col, f"@image{{0.0}}"),
    ],
    formatters={
        "$x": CustomJSHover(code=_hover_code % 0),
        "$y": CustomJSHover(code=_hover_code % 1),
        '@{Timestamp}': 'datetime',
    },
)
plot = df.hvplot.points(
    'Longitude', 'Latitude',
    geo=True, 
    tiles=tile,
    tools=[hover],
    c=selected_col,
    cmap='Reds',
    colorbar=True,
    rasterize=True,
    dynamic=False,
)
hvplot.show(plot)

Stack traceback and/or browser JavaScript console output

[bokeh] setting log level to: 'info'
bokeh.min.js?v=76cb725c10c0dc0fcfcd57552964929a1997934b09c9392795906e419823fd85132cff7725188cf3205f1bed65127750d10bd13fc527cb2f80a50f24b45a369f:226 [bokeh] Websocket connection 0 is now open
bokeh.min.js?v=76cb725c10c0dc0fcfcd57552964929a1997934b09c9392795906e419823fd85132cff7725188cf3205f1bed65127750d10bd13fc527cb2f80a50f24b45a369f:163 Bokeh items were rendered successfully
bokeh.min.js?v=76cb725c10c0dc0fcfcd57552964929a1997934b09c9392795906e419823fd85132cff7725188cf3205f1bed65127750d10bd13fc527cb2f80a50f24b45a369f:165 [bokeh] document idle at 537 ms

Screenshots or screencasts of the bug in action

image

@balshersingh10
Copy link
Author

@ahuang11 I created the issue here

@ahuang11 ahuang11 changed the title Timestamp not working with Hover in hvplot.points hover_cols not working in conjunction with rasterize Dec 1, 2023
@maximlt
Copy link
Member

maximlt commented Dec 10, 2023

Hi @balshersingh10, @Hoxbro kindly worked out a solution you can find below. It leverages the new selector parameter added to the rasterize operation in HoloViews (see holoviz/holoviews#5805 for more details; it's not yet properly documented). I believe he also had to handle some issues with displaying/converting the timestamp in the hover, which explains why there's yet another CustomJSHover.

In general it's important to think about the displayed data when rasterizing a dataset. For color mapping, each pixel is an aggregation of the data points it contains, the default is a count, in your example as you set c it's a mean. For hover, selector supports one of first/last/min/max, in the example I picked somewhat arbitrarily the max of Value. Picking first would have returned whatever first value was in the dataset in that pixel.

import datashader as ds
import holoviews as hv
import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn

from bokeh.models import HoverTool, CustomJSHover
from holoviews.operation.datashader import rasterize

# Create a dummy DataFrame
np.random.seed(0)
n = 10000  # number of points
df = pd.DataFrame(
    {
        "Longitude": np.random.uniform(-180, 180, n),
        "Latitude": np.random.uniform(-90, 90, n),
        "Timestamp": pd.date_range(start="2023-01-01", periods=n, freq="D"),
        "Value": np.random.rand(n) * 100,  # This will represent 'selected_col'
    }
)


selected_col = "Value"
tile = hv.element.tiles.EsriNatGeo().opts(width=1000, height=650)
_hover_code = """
    const projections = Bokeh.require("core/util/projections");
    const {snap_x, snap_y} = special_vars
    const coords = projections.wgs84_mercator.invert(snap_x, snap_y)
    return "" + (coords[%d]).toFixed(4)
"""

_date_hover_code = """
    let d = new Date(0)
    d.setUTCSeconds(Math.floor(value / 1e9))
    return d.toDateString()
"""

hover = HoverTool(
    tooltips=[
        ("Timestamp", "@Timestamp{custom}"),
        ("(Longitude,Latitude)", "$x{custom}, $y{custom}"),
        (selected_col, f"@image{{0.0}}"),
    ],
    formatters={
        "$x": CustomJSHover(code=_hover_code % 0),
        "$y": CustomJSHover(code=_hover_code % 1),
        "@Timestamp": CustomJSHover(code=_date_hover_code),
    },
)
plot = df.hvplot.points("Longitude", "Latitude", hover_cols=['Value'], geo=True)

rplot = rasterize(plot, aggregator=ds.mean('Value'), selector=ds.max("Value"), dynamic=False).opts(
    tools=[hover], cmap="Reds", colorbar=True
)
tile * rplot

image

@maximlt maximlt changed the title hover_cols not working in conjunction with rasterize Support hover_cols with rasterize Dec 11, 2023
@maximlt maximlt added the type: enhancement New feature or request label Dec 11, 2023
@maximlt maximlt added this to the 0.10.x milestone Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants