Skip to content

Commit 86c3b4c

Browse files
authored
Fix color-by-visibility in panel (#250)
1 parent 1263de6 commit 86c3b4c

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

spikeinterface_gui/probeview.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,33 +554,38 @@ def _panel_refresh(self):
554554
self.y_range.start = zoom_bounds[2]
555555
self.y_range.end = zoom_bounds[3]
556556

557-
# Defer to avoid nested Bokeh callbacks
558-
# pn.state.execute(_do_update, schedule=True)
559557

560558
def _panel_compute_unit_glyph_patches(self):
561559
"""Compute glyph patches without modifying Bokeh models."""
562560
current_alphas = self.glyphs_data_source.data['alpha']
563561
current_sizes = self.glyphs_data_source.data['size']
564562
current_line_colors = self.glyphs_data_source.data['line_color']
563+
current_colors = self.glyphs_data_source.data['color']
565564

566565
alpha_patches = []
567566
size_patches = []
568567
line_color_patches = []
568+
color_patches = []
569+
570+
if self.controller.main_settings['color_mode'] in ('color_by_visibility', 'color_only_visible'):
571+
self.controller.refresh_colors()
569572

570573
for idx, unit_id in enumerate(self.controller.unit_ids):
571-
color = self.get_unit_color(unit_id)
574+
new_color = self.get_unit_color(unit_id)
572575
is_visible = self.controller.get_unit_visibility(unit_id)
573576

574577
new_alpha = self.alpha_selected if is_visible else self.alpha_unselected
575578
new_size = self.unit_marker_size_selected if is_visible else self.unit_marker_size_unselected
576-
new_line_color = "black" if is_visible else color
579+
new_line_color = "black" if is_visible else new_color
577580

578581
if current_alphas[idx] != new_alpha:
579582
alpha_patches.append((idx, new_alpha))
580583
if current_sizes[idx] != new_size:
581584
size_patches.append((idx, new_size))
582585
if current_line_colors[idx] != new_line_color:
583586
line_color_patches.append((idx, new_line_color))
587+
if current_colors[idx] != new_color:
588+
color_patches.append((idx, new_color))
584589

585590
patch_dict = {}
586591
if alpha_patches:
@@ -589,6 +594,8 @@ def _panel_compute_unit_glyph_patches(self):
589594
patch_dict['size'] = size_patches
590595
if line_color_patches:
591596
patch_dict['line_color'] = line_color_patches
597+
if color_patches:
598+
patch_dict['color'] = color_patches
592599

593600
return patch_dict
594601

spikeinterface_gui/unitlistview.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,18 @@ def _do_update():
677677
def _panel_refresh_colors(self):
678678
import matplotlib.colors as mcolors
679679

680-
unit_ids_data = []
681-
for unit_id in self.table.value.index.values:
682-
unit_ids_data.append(
683-
{
684-
"id": str(unit_id),
685-
"color": mcolors.to_hex(self.controller.get_unit_color(unit_id))
686-
}
687-
)
688-
self.table.value.loc[:, "unit_id"] = unit_ids_data
680+
if self.controller.main_settings['color_mode'] in ('color_by_visibility', 'color_only_visible'):
681+
self.controller.refresh_colors()
682+
# in this mode the color is dynamic based on visibility, so we need to refresh all colors
683+
unit_ids_data = []
684+
for unit_id in self.table.value.index.values:
685+
unit_ids_data.append(
686+
{
687+
"id": str(unit_id),
688+
"color": mcolors.to_hex(self.controller.get_unit_color(unit_id))
689+
}
690+
)
691+
self.table.value.loc[:, "unit_id"] = unit_ids_data
689692

690693
def _panel_on_unit_color_changed(self):
691694
# here we update the unit colors, since they are then fixed in the table
@@ -709,6 +712,7 @@ def _panel_on_only_selection(self):
709712
selected_unit = self.table.selection[0]
710713
unit_id = self.table.value.index.values[selected_unit]
711714
self.controller.set_visible_unit_ids([unit_id])
715+
self._panel_refresh_colors()
712716
# update the visible column
713717
df = self.table.value
714718
df.loc[self.controller.unit_ids, "visible"] = self.controller.get_units_visibility_mask()

0 commit comments

Comments
 (0)