Link datashade lasso_select to dmap stream

Hi,
I have a large dataset that I want datashade and then datashade a subset based on the lasso_select. This is very similar to an example at

https://holoviews.org/reference/streams/bokeh/Selection1D_points.html#streams-bokeh-gallery-selection1d-points

except that I want to lasso_select from a datashaded scatter plot.
I would be interested in finding a way to fetch the lasso_select polygon to intersect that with the scatte upfron of datashade for hte dynamic map.

import numpy as np
import holoviews as hv
from holoviews import opts
from holoviews import streams
from holoviews.operation.datashader import datashade
hv.extension('bokeh')

opts.defaults(opts.Scatter(tools=['box_select', 'lasso_select']))

# Declare some points
points = hv.Scatter(np.random.randn(1000,2 ))

ds_static = datashade(points).opts(tools=['box_select', 'lasso_select'], active_tools=['box_select'])

# Declare points as source of selection stream
selection = streams.Selection1D(source=ds_static)

# Write function that uses the selection indices to slice points and compute stats
def selected_info(index):
    selected = points.iloc[index]
    print(selected)
    if index:
        label = 'Mean x, y: %.3f, %.3f' % tuple(selected.array().mean(axis=0))
    else:
        label = 'No selection'
    
    return selected.relabel(label).opts(color='red')

# Combine points and DynamicMap

ds_dynamic = datashade(hv.DynamicMap(selected_info, streams=[selection]), x_sampling=.1, y_sampling=.1 )
ds_static + ds_dynamic

Any suggestions please?

I only just merged a PR which supports accessing the lasso select polygon from Python, it will be released in HoloViews 1.13.3. Note also that linked brushing will do all the hard work for you, e.g. the example can basically be rewritten as:

points = hv.Scatter(np.random.randn(10000,2 ))

def selected_info(points):
    label = 'Mean x, y: %.3f, %.3f' % tuple(points.array().mean(axis=0))
    return points.relabel(label)

selected = points.apply(selected_info)
link_selections(datashade(selected))
1 Like

Thank you for quick reply, this sounds great! Any chance to be able to do this to some extent in holoviews version 1.12.7?

Hello, were you able to fetch the lasso_select polygon to intersect that with the scatter plot of datashade for the dynamic map.
I am really new to holoviz and trying to do something similar, would appreciate your input

Is there an option to obtain the geometry of the selection made.

Yes @tasvora , the link_selections feature is just amazing!
You can do the following:

from holoviews.selection import link_selections

ls = link_selections.instance()

ls(datashade(selected))

Then you could do a selection and further get to selection_expr by

print(ls.selection_expr)

which would print something like:
dim(‘x’, spatial_select, dim(‘y’), geometry=array([[1.29646153, 0.66127218],
[1.06936576, 0.28434529],
[1.06936576, 0.25293471],
[1.06936576, 0.25293471],
[1.13425027, 0.25293471],
[1.16669252, 0.25293471],
[1.19913478, 0.25293471],
[1.23157703, 0.25293471],
[1.26401928, 0.25293471],
[1.26401928, 0.25293471],
[1.26401928, 0.25293471]]))
where the array is defining the polygon.

I should note that for large datasets this works best if spatialpandas is installed. I am leaning on holoviews 1.14.5 to make this work efficiently. I have not yet been able to have the link_selections on datashaded work coparable fast in holoviews 1.14.8 as in 1.14.5 using polygon or lasso_select.