diff --git a/docs/_static/dependent_deselector.png b/docs/_static/dependent_deselector.png index 001cd32..3b6f885 100644 Binary files a/docs/_static/dependent_deselector.png and b/docs/_static/dependent_deselector.png differ diff --git a/docs/_static/zigzag_dependent_deselector.png b/docs/_static/zigzag_dependent_deselector.png new file mode 100644 index 0000000..52cdbb7 Binary files /dev/null and b/docs/_static/zigzag_dependent_deselector.png differ diff --git a/docs/gui-guide.md b/docs/gui-guide.md index fbb3f31..33c0adc 100644 --- a/docs/gui-guide.md +++ b/docs/gui-guide.md @@ -95,7 +95,7 @@ Type electrode ranges directly, e.g. `1-10,20-25`, to add them to the current se You can hover over any electrode to see its ID, shank, depth (Z position), and current status in a tooltip. Clicking a single electrode (de)selects it. -You can also drag boxes to (de)select ranges of electrodes with one of five box tools (select the active tool from the toolbar on the right of the probe plot): +You can also drag boxes to (de)select ranges of electrodes with one of six box tools (select the active tool from the toolbar on the right of the probe plot): ```{list-table} :widths: 12 88 @@ -120,6 +120,10 @@ You can also drag boxes to (de)select ranges of electrodes with one of five box :height: 40px ::: - **Deselect dependents** — drag over unavailable (black) electrodes to find the selected (red) electrodes that blocked them due to shared lines or ADCs, and deselect those selected electrodes, freeing the blocked electrodes and all their other dependents. +* - :::{image} _static/zigzag_dependent_deselector.png + :height: 40px + ::: + - **Zigzag-deselect dependents** — drag over blocked electrodes to free only the fixed zigzag-pattern sites by deselecting the red electrodes blocking them, wherever they sit (inside or outside the box). Only zigzag-pattern red electrodes can be blockers, so the complementary non-zigzag sites stay selected. This tool selects nothing. ``` ##### In which order are electrodes selected inside a box? diff --git a/pixelmap/gui/gui.py b/pixelmap/gui/gui.py index e617d87..3b4f12b 100644 --- a/pixelmap/gui/gui.py +++ b/pixelmap/gui/gui.py @@ -749,28 +749,10 @@ def on_electrode_selection(self, _, old, new): self.electrodes.select(Electrode(shank_id, electrode_id)) elif self.select_mode == "dependent_deselect": - # Find unavailable (black) electrodes in the box span, then deselect - # all selected electrodes whose wiring conflicts caused them to be unavailable. - print(f"Box deselect-dependents: {len(new)} electrodes in span") - unavailable_in_span = set() - for idx in new: - shank_id = self.electrode_source.data["shank_id"][idx] - electrode_id = self.electrode_source.data["electrode_id"][idx] - electrode = Electrode(shank_id, electrode_id) - if electrode in self.electrodes.unavailable: - unavailable_in_span.add(electrode) - - # For each unavailable electrode in the span, find which selected - # electrode(s) caused it to be unavailable (i.e. it is in their wiring map). - to_deselect = set() - for u in unavailable_in_span: - for s in self.electrodes.selected: - if u in self.electrodes.wiring_map[s]: - to_deselect.add(s) - - print(f" → deselecting {len(to_deselect)} responsible selected electrode(s)") - for s in to_deselect: - self.electrodes.deselect(s) + self._deselect_dependents(new) + + elif self.select_mode == "zigzag_dependent_deselect": + self._deselect_dependents(new, subset=self.get_zigzag_subset()) # Update electrode visualization self.update_electrode_colors() @@ -787,6 +769,33 @@ def on_electrode_selection(self, _, old, new): self.electrode_source.selected.indices = [] + def _deselect_dependents(self, indices, subset=None): + """Free blocked sites in the box, optionally restricted to electrode IDs. + + Collect blockers before mutating selection. Selected sites in the box + are not targets, and this operation never selects the freed sites. + The caller owns the gesture's undo snapshot and visualization refresh. + """ + subset = None if subset is None else set(subset) + unavailable_in_span = set() + for idx in indices: + shank_id = self.electrode_source.data["shank_id"][idx] + electrode_id = self.electrode_source.data["electrode_id"][idx] + if subset is not None and electrode_id not in subset: + continue + electrode = Electrode(shank_id, electrode_id) + if electrode in self.electrodes.unavailable: + unavailable_in_span.add(electrode) + + to_deselect = set() + for u in unavailable_in_span: + for s in self.electrodes.selected: + if u in self.electrodes.wiring_map[s]: + to_deselect.add(s) + + for s in to_deselect: + self.electrodes.deselect(s) + def get_zigzag_subset(self): N_per_shank = PROBE_FEATURES[self.probe_subtype]["n_sites_per_shank"] if self.probe_type == "1.0": @@ -2093,6 +2102,7 @@ def setup_bokeh_plot(self): zigzagselect_box_string = "Zigzag-select Electrodes" interleavedselect_box_string = "Interleaved-select Electrodes" dependentdeselect_box_string = "Deselect Dependents" + zigzagdependentdeselect_box_string = "Zigzag-deselect Dependents" self.box_select_tool = BoxSelectTool(description=select_box_string, icon=GUI_ASSETS_DIR / "selector.png") @@ -2112,6 +2122,11 @@ def setup_bokeh_plot(self): description=dependentdeselect_box_string, icon=GUI_ASSETS_DIR / "dependent_deselector.png" ) + self.box_zigzag_dependent_deselect_tool = BoxSelectTool( + description=zigzagdependentdeselect_box_string, + icon=GUI_ASSETS_DIR / "zigzag_dependent_deselector.png", + ) + # Always-on gesture tools — hidden from toolbar (visible=False). # Left-click drag = pan; scroll = zoom. # Note: right-click drag zoom is not supported by this Bokeh version. @@ -2143,6 +2158,7 @@ def setup_bokeh_plot(self): self.box_zigzagselect_tool, self.box_interleavedselect_tool, self.box_dependent_deselect_tool, + self.box_zigzag_dependent_deselect_tool, ] self.plot = figure( @@ -2204,7 +2220,11 @@ def setup_bokeh_plot(self): # Hidden data source for tool state communication and CustomJS to monitor tool changes self.tool_state_source = ColumnDataSource(data={"active_tool": [""]}) - self.setup_tool_monitoring(select_box_string, deselect_box_string, zigzagselect_box_string, interleavedselect_box_string, dependentdeselect_box_string) + self.setup_tool_monitoring( + select_box_string, deselect_box_string, zigzagselect_box_string, + interleavedselect_box_string, dependentdeselect_box_string, + zigzagdependentdeselect_box_string, + ) def setup_interactions(self): """Setup click and selection interactions""" @@ -2225,7 +2245,11 @@ def setup_interactions(self): "indices", guarded("electrode selection")(self.on_electrode_selection) ) - def setup_tool_monitoring(self, select_box_string, deselect_box_string, zigzagselect_box_string, interleavedselect_box_string, dependentdeselect_box_string): + def setup_tool_monitoring( + self, select_box_string, deselect_box_string, zigzagselect_box_string, + interleavedselect_box_string, dependentdeselect_box_string, + zigzagdependentdeselect_box_string, + ): """JS callback to expose active tools in toolbar""" # workaround https://stackoverflow.com/questions/58210752/how-to-get-currently-active-tool-in-bokeh-figure @@ -2240,26 +2264,30 @@ def setup_tool_monitoring(self, select_box_string, deselect_box_string, zigzagse let zigzagselect_tool = null; let interleavedselect_tool = null; let dependentdeselect_tool = null; + let zigzagdependentdeselect_tool = null; tools.forEach((tool, index) => { - if (tool.description === 'select_box_string') { + if (tool.description === select_box_string) { select_tool = tool; } - if (tool.description === 'deselect_box_string') { + if (tool.description === deselect_box_string) { deselect_tool = tool; } - if (tool.description === 'zigzagselect_box_string') { + if (tool.description === zigzagselect_box_string) { zigzagselect_tool = tool; } - if (tool.description === 'interleavedselect_box_string') { + if (tool.description === interleavedselect_box_string) { interleavedselect_tool = tool; } - if (tool.description === 'dependentdeselect_box_string') { + if (tool.description === dependentdeselect_box_string) { dependentdeselect_tool = tool; } + if (tool.description === zigzagdependentdeselect_box_string) { + zigzagdependentdeselect_tool = tool; + } }); - if (select_tool && deselect_tool && zigzagselect_tool && interleavedselect_tool && dependentdeselect_tool) { + if (select_tool && deselect_tool && zigzagselect_tool && interleavedselect_tool && dependentdeselect_tool && zigzagdependentdeselect_tool) { if (select_tool.active) { tools_info = 'select'; } else if (deselect_tool.active) { @@ -2270,6 +2298,8 @@ def setup_tool_monitoring(self, select_box_string, deselect_box_string, zigzagse tools_info = 'interleaved_select'; } else if (dependentdeselect_tool.active) { tools_info = 'dependent_deselect'; + } else if (zigzagdependentdeselect_tool.active) { + tools_info = 'zigzag_dependent_deselect'; } else { tools_info = 'neither_active'; } @@ -2280,13 +2310,17 @@ def setup_tool_monitoring(self, select_box_string, deselect_box_string, zigzagse tool_state.data = {active_tool: [tools_info]}; """ - js_code = js_code.replace("dependentdeselect_box_string", dependentdeselect_box_string) - js_code = js_code.replace("interleavedselect_box_string", interleavedselect_box_string) - js_code = js_code.replace("zigzagselect_box_string", zigzagselect_box_string) - js_code = js_code.replace("deselect_box_string", deselect_box_string) - js_code = js_code.replace("select_box_string", select_box_string) - - JS_selection_monitor = CustomJS(args={"tool_state": self.tool_state_source}, code=js_code) + # Pass descriptions as values so overlapping names cannot corrupt + # each other through order-dependent placeholder replacement. + JS_selection_monitor = CustomJS(args={ + "tool_state": self.tool_state_source, + "select_box_string": select_box_string, + "deselect_box_string": deselect_box_string, + "zigzagselect_box_string": zigzagselect_box_string, + "interleavedselect_box_string": interleavedselect_box_string, + "dependentdeselect_box_string": dependentdeselect_box_string, + "zigzagdependentdeselect_box_string": zigzagdependentdeselect_box_string, + }, code=js_code) # Hack - monitor random GUI events to trigger javascript fetch of selection box type self.plot.js_on_event("selectiongeometry", JS_selection_monitor) # Selection events @@ -2317,6 +2351,9 @@ def on_tool_state_change(self, attr, old, new): elif active_tool == "dependent_deselect": self.select_mode = "dependent_deselect" print("→ DESELECT-DEPENDENTS box activated") + elif active_tool == "zigzag_dependent_deselect": + self.select_mode = "zigzag_dependent_deselect" + print("→ ZIGZAG-DESELECT-DEPENDENTS box activated") else: self.select_mode = "select" print(f"→ Unexpected result: {active_tool}, defaulting to SELECT box") @@ -2824,10 +2861,20 @@ def get_download_buttons(self): "The black electrodes inside your box turn grey and become " "selectable."), ), + ( + "zigzag_dependent_deselector.png", + "Zigzag-deselect dependents", + ("Frees only the fixed zigzag-pattern black electrodes inside the box " + "by deselecting the red electrodes blocking them, wherever they " + "sit (inside or outside the box). Only zigzag-pattern red " + "electrodes can be blockers, so the complementary non-zigzag " + "sites stay selected. Selects nothing; use Zigzag-select next " + "to select the freed sites."), + ), ) def _selection_tools_help_html(self) -> str: - """In-GUI reference for the five drag-box tools in the plot toolbar. + """In-GUI reference for the six drag-box tools in the plot toolbar. Most users never open the online docs, so the tools — "deselect dependents" above all, whose target is *outside* the box you drag — @@ -2848,7 +2895,7 @@ def _selection_tools_help_html(self) -> str: return ( '