Skip to content

Commit

Permalink
Started fixing density map in scatter viewer for many points
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Jun 30, 2023
1 parent 5b6454c commit 3c57457
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
19 changes: 10 additions & 9 deletions glue_jupyter/bqplot/scatter/layer_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def __init__(self, view, viewer_state, layer_state=None, layer=None):
self.counts = None
self.image = ImageGL(scales=self.scales_image, image=EMPTY_IMAGE)
on_change([(self.state, 'density_map')])(self._on_change_density_map)
on_change([(self.state, 'bins')])(self._update_scatter)
self._viewer_state.add_global_callback(self._update_scatter)

self.view.figure.marks = (list(self.view.figure.marks)
+ [self.image, self.scatter, self.quiver])
Expand Down Expand Up @@ -117,7 +115,7 @@ def _on_change_cmap(self, ignore=None):

def _on_change_density_map(self):
self._update_visibility()
self._update_scatter()
self._update_density_map()

def _update_visibility(self, *args):
self.image.visible = self.state.density_map
Expand Down Expand Up @@ -149,7 +147,7 @@ def _workaround_unselected_style(self, change=None):
self.quiver.unselected_style = {'fill': 'none', 'stroke': 'none'}

@debounced(method=True)
def update_histogram(self):
def _update_density_map(self):
if isinstance(self.layer, Subset):
data = self.layer.data
subset_state = self.layer.subset_state
Expand All @@ -171,10 +169,8 @@ def update_histogram(self):
self.image.x = range_x
self.image.y = range_y
self.image.image = self.counts.T.astype(np.float32, copy=True)

def _update_scatter(self, **changes):
self.update_histogram()
self.update()
else:
self.image.image = None

def update(self):

Expand Down Expand Up @@ -212,13 +208,18 @@ def update(self):
self.enable()

if self.state.density_map:
pass
self.scatter.x = []
self.scatter.y = []
self.quiver.x = []
self.quiver.y = []
else:
self.scatter.x = x.astype(np.float32).ravel()
self.scatter.y = y.astype(np.float32).ravel()
self.quiver.x = self.scatter.x
self.quiver.y = self.scatter.y

self._update_density_map()

if isinstance(self.layer, Subset):

try:
Expand Down
39 changes: 39 additions & 0 deletions glue_jupyter/bqplot/scatter/tests/test_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,42 @@ def test_visual_scatter2d(
figure = scatter.figure_widget
figure.layout = {"width": "400px", "height": "250px"}
return figure


@visual_widget_test
def test_visual_scatter2d_density(
tmp_path,
page_session,
solara_test,
):

# Test that for many points, density maps are shown

np.random.seed(12345)

app = jglue()

x = np.random.normal(3, 1, 100)
y = np.random.normal(2, 1.5, 100)
c = np.hypot(x - 3, y - 2)
s = (x - 3)

data1 = app.add_data(a={"x": x, "y": y, "c": c, "s": s})[0]

xx = np.random.normal(3, 1, 1000000)
yy = np.random.normal(2, 1.5, 1000000)

data2 = app.add_data(a={"x": xx, "y": yy})[0]

scatter = app.scatter2d(show=False, data=data1)
scatter.add_data(data2)

scatter.state.layers[0].cmap_mode = 'Linear'
scatter.state.layers[0].cmap_att = data1.id['c']
scatter.state.layers[0].cmap = plt.cm.viridis
scatter.state.layers[0].size_mode = 'Linear'
scatter.state.layers[0].size_att = data1.id['s']

figure = scatter.figure_widget
figure.layout = {"width": "400px", "height": "250px"}
return figure

0 comments on commit 3c57457

Please sign in to comment.