Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/_static/dependent_deselector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/zigzag_dependent_deselector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion docs/gui-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Comment thread
tabedzki marked this conversation as resolved.
Resolved
: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?
Expand Down
127 changes: 87 additions & 40 deletions pixelmap/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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":
Expand Down Expand Up @@ -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")

Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"""
Expand All @@ -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

Expand All @@ -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) {
Expand All @@ -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';
}
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 —
Expand All @@ -2848,7 +2895,7 @@ def _selection_tools_help_html(self) -> str:
return (
'<div style="font-size: 13px; line-height: 1.4; text-align: justify;">'
"Click any electrode to toggle it on or off. To edit many at once, "
"pick one of the five drag-box tools in the toolbar to the right of "
"pick one of the six drag-box tools in the toolbar to the right of "
"the probe plot (only one is active at a time) and drag a rectangle "
"over the probe:<br>"
'<table style="border-collapse: collapse; margin: 6px 0;">'
Expand Down Expand Up @@ -2902,7 +2949,7 @@ def create_layout(self):
<b>You can mix and match four selection methods:</b><br>
• <b>Presets:</b> Pre-configured channelmaps that respect wiring constraints<br>
• <b>Textual selection:</b> Type electrode ranges (e.g., "1-10,20-25") to add to the current selection<br>
• <b>Interactive:</b> Click electrodes directly or drag boxes (selection, deselection, zigzag/interleaved selection, or "deselect dependents") to manually select multiple sites<br>
• <b>Interactive:</b> Click electrodes directly or drag boxes (selection, deselection, zigzag/interleaved selection, "deselect dependents", or "zigzag-deselect dependents") to manually select multiple sites<br>
• <b>Selection from pre-existing IMRO file</b>: you can pre-load an IMRO file as a starting point before doing any of the above.<br><br>

Once you reach the <b>target number of electrodes</b> for the selected probe type (384 or 1536), you can <b>download your channelmap</b> as an IMRO file alongside a PDF rendering to easily remember what your channelmap looks like. Use this IMRO file in SpikeGLX to record from the selected channels, and then use the Kilosort .json file to spikesort your data accordingly.<br><br>
Expand Down
Loading
Loading