diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..4b1da71 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,43 @@ +# GitHub uses the last matching rule for each file. +# Deployment, operations, and anything not assigned below. +* @tabedzki + +# Backend: public API, channel-map generation, probe definitions, and wiring. +/pixelmap/__init__.py @m-beau +/pixelmap/backend.py @m-beau +/pixelmap/constants.py @m-beau +/pixelmap/types.py @m-beau +/pixelmap/wiring_maps/ @m-beau +/pixelmap/utils/imro.py @m-beau +/pixelmap/utils/probe_features.py @m-beau +/pixelmap/utils/survey.py @m-beau +/pixelmap/anatomy/ @m-beau +/scripts/refresh_registry_snapshot.py @m-beau +/generate_channel_maps.ipynb @m-beau + +# Tests and reference data for backend behavior. +/tests/test_backend.py @m-beau +/tests/test_core_functionality.py @m-beau +/tests/test_public_api.py @m-beau +/tests/test_imro*.py @m-beau +/tests/test_wiring*.py @m-beau +/tests/test_survey.py @m-beau +/tests/test_anatomy*.py @m-beau +/tests/generate_imro_reference.py @m-beau +/tests/fixtures/ @m-beau + +# Documentation, paper, and example layouts. +*.md @m-beau +*.rst @m-beau +/docs/ @m-beau +/paper/ @m-beau +/example*/ @m-beau +/CITATION.cff @m-beau + +# GUI changes request review from both maintainers. +/pixelmap/gui/ @tabedzki @m-beau +/tests/test_gui*.py @tabedzki @m-beau + +# Documentation build infrastructure remains part of deployment. +/docs/conf.py @tabedzki +/.readthedocs.yaml @tabedzki 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..714206f 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? @@ -131,7 +135,7 @@ The order is invisible most of the time, because every electrode in the box ends The same first-come-first-served rule applies to textual selection (electrode IDs are consumed in the order you write them). :::{tip} -The zigzag and interleaved selector always select the same preset of electrodes independently from where you start dragging the box (e.g. the interleaved selector will always select sites 1-2, 5-6, 9-10, never 3-4, 7-8, 11-12...). If you want to select the complementary channels of a range (e.g. not 1-2, 5-6, 9-10 but instead 3-4, 7-8, 11-12...), you can either use the textual selection box, or you can first make (1-2, 5-6, 9-10) unavailable through zigzag/interleaved-selecting associated electrodes somewhere elese on the probe then use the regular selection box to select the remaining available electrodes (your target). +The zigzag and interleaved selector always select the same preset of electrodes independently from where you start dragging the box (e.g. the interleaved selector will always select sites 1-2, 5-6, 9-10, never 3-4, 7-8, 11-12...). If you want to select the complementary channels of a range (e.g. not 1-2, 5-6, 9-10 but instead 3-4, 7-8, 11-12...), you can either use the textual selection box, or you can first make (1-2, 5-6, 9-10) unavailable through zigzag/interleaved-selecting associated electrodes somewhere else on the probe then use the regular selection box to select the remaining available electrodes (your target). ::: diff --git a/entrypoint.sh b/entrypoint.sh index 301e35c..0cccb76 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -68,7 +68,11 @@ log "NUM_PROCS $NUM_PROCS" # # --mem-log-frequency periodically logs RSS (via psutil) so an OOM kill # (SIGKILL, exit 137) leaves a trail in the logs instead of nothing. +# +# --setup warms what the first session would otherwise load on the event loop, +# freezing every session and /liveness (see scripts/warm_first_session.py). exec "$PYTHON_BIN" -m panel serve ./app.py \ + --setup ./scripts/warm_first_session.py \ --address "$ADDRESS" \ --port "$INTERNAL_PORT" \ --allow-websocket-origin "$ALLOW_WEBSOCKET_ORIGIN" \ diff --git a/pixelmap/__init__.py b/pixelmap/__init__.py index cd8afc3..c6c1b0d 100644 --- a/pixelmap/__init__.py +++ b/pixelmap/__init__.py @@ -69,6 +69,6 @@ "save_to_imro_file", ] -__version__ = "1.5.0" +__version__ = "1.5.1" __author__ = "Maxime Beau" __email__ = "maxime@princeton.edu" diff --git a/pixelmap/anatomy/atlas.py b/pixelmap/anatomy/atlas.py index 7414015..6b2fff4 100644 --- a/pixelmap/anatomy/atlas.py +++ b/pixelmap/anatomy/atlas.py @@ -574,14 +574,54 @@ def landmark_policy(name: str) -> str | None: return None -@functools.lru_cache(maxsize=8) +#: Memo for :func:`derive_origin_from_ac`. A plain dict rather than +#: ``lru_cache`` because :func:`peek_origin_from_ac` needs to ask "is this one +#: known?" without computing it. Unbounded: one three-float entry per atlas. +_ac_origin_memo: dict[str, tuple[float, float, float] | None] = {} +#: One lock per atlas name, so concurrent callers (several sessions, or a quick +#: A→B→A switch) wait for the scan already running instead of starting another. +_ac_origin_locks: dict[str, threading.Lock] = {} +_ac_origin_locks_guard = threading.Lock() + + def derive_origin_from_ac(name: str) -> tuple[float, float, float] | None: """Origin at the anterior-commissure decussation, from the annotation. Returns canonical ``(AP, ML, DV)`` µm, or ``None`` if the atlas delineates no anterior commissure. Same recipe used (and validated) for the WHS rat: the AC's midline-crossing centroid. Requires the atlas (downloads if absent). + + Slow the first time: it scans every voxel of the annotation (0.8 s for a + 25 µm atlas, ~8 s at 10 µm), so the GUI only ever calls it from a worker + thread and uses :func:`peek_origin_from_ac` on the event loop. Memoised + per atlas and single-flight per name; exceptions are not cached, so a + failed lookup is retried. + """ + if name in _ac_origin_memo: + return _ac_origin_memo[name] + with _ac_origin_locks_guard: + lock = _ac_origin_locks.setdefault(name, threading.Lock()) + with lock: + if name not in _ac_origin_memo: # else a concurrent caller's scan won + _ac_origin_memo[name] = _derive_origin_from_ac(name) + return _ac_origin_memo[name] + + +derive_origin_from_ac.cache_clear = _ac_origin_memo.clear + + +def peek_origin_from_ac(name: str) -> tuple[bool, tuple[float, float, float] | None]: + """``(known, origin)`` for :func:`derive_origin_from_ac`, without computing it. + + ``known`` is False until something has derived this atlas's origin. + Instant and touches no atlas data, so it is safe on the event loop. """ + if name in _ac_origin_memo: + return True, _ac_origin_memo[name] + return False, None + + +def _derive_origin_from_ac(name: str) -> tuple[float, float, float] | None: atlas = get_atlas(name) ac_ids = [int(s["id"]) for s in atlas.structures.values() if "anterior" in s["name"].lower() and "commis" in s["name"].lower()] diff --git a/pixelmap/anatomy/schematic.py b/pixelmap/anatomy/schematic.py index b5c0f4c..6938d8f 100644 --- a/pixelmap/anatomy/schematic.py +++ b/pixelmap/anatomy/schematic.py @@ -58,16 +58,21 @@ def _region_rgb(atlas, region_id: int) -> tuple[int, int, int]: def _region_fill_rgba(label_img: np.ndarray, atlas) -> np.ndarray: - """Map a 2D label slice to an RGBA image colored by atlas region.""" - rgba = np.zeros((*label_img.shape, 4), dtype=float) - for region_id in np.unique(label_img): - if region_id == 0: # outside-brain / undefined + """Map a 2D label slice to an RGBA image colored by atlas region. + + One color per *distinct* region, then a single gather through the inverse + index. Masking the slice once per region instead (the obvious loop) costs + a full pass per region — 2.4 s of a 3.5 s render on a 10 µm atlas, whose + slices hold hundreds of regions. + """ + region_ids, inverse = np.unique(label_img, return_inverse=True) + palette = np.zeros((len(region_ids), 4), dtype=float) + for i, region_id in enumerate(region_ids): + if region_id == 0: # outside-brain / undefined: stays transparent continue r, g, b = _region_rgb(atlas, int(region_id)) - mask = label_img == region_id - rgba[mask, :3] = (r / 255.0, g / 255.0, b / 255.0) - rgba[mask, 3] = _REGION_ALPHA - return rgba + palette[i] = (r / 255.0, g / 255.0, b / 255.0, _REGION_ALPHA) + return palette[inverse.reshape(label_img.shape)] def _outline_rgba(label_img: np.ndarray) -> np.ndarray: diff --git a/pixelmap/backend.py b/pixelmap/backend.py index bfa6b51..38e0701 100644 --- a/pixelmap/backend.py +++ b/pixelmap/backend.py @@ -12,7 +12,7 @@ import numpy as np import pandas as pd from matplotlib import cm, patches -from matplotlib.collections import PatchCollection +from matplotlib.collections import PolyCollection from .constants import ( LEGACY_INT_TO_IMRO_FORMAT, @@ -103,28 +103,27 @@ def _wiring_index(wiring_df): def find_forbidden_electrodes(selected_electrodes, wiring_df): "Return list of forbidden electrodes [[shank_id, electrode_id], ...] given selected electrodes and wiring diagram." + return _forbidden_electrodes(selected_electrodes, *_wiring_index(wiring_df)) - table, shanks, index = _wiring_index(wiring_df) - rows, sel_shanks, sel_electrodes = [], [], [] +def _forbidden_electrodes(selected_electrodes, table, shanks, index): + """``find_forbidden_electrodes`` against a prebuilt :func:`_wiring_index`.""" + rows = [] for shank_id, electrode_id in selected_electrodes: loc = index.get((float(shank_id), float(electrode_id))) assert loc is not None, f"electrode {electrode_id} on shank {shank_id} not found in wiring map!" rows.append(loc[0]) - sel_shanks.append(float(shank_id)) - sel_electrodes.append(float(electrode_id)) + selected = np.asarray(selected_electrodes, dtype=float).reshape(-1, 2) # Every selected electrode's wiring-table row: the electrodes sharing its # channel. Keep the wired (non-NaN), non-self cells; boolean indexing is # row-major, so the order matches the old per-electrode loop. row_electrodes = table[rows] # (n_selected, n_columns) row_shanks = np.broadcast_to(shanks, row_electrodes.shape) - is_self = (row_shanks == np.array(sel_shanks)[:, None]) & ( - row_electrodes == np.array(sel_electrodes)[:, None] - ) + is_self = (row_shanks == selected[:, :1]) & (row_electrodes == selected[:, 1:]) keep = ~np.isnan(row_electrodes) & ~is_self if not keep.any(): - return np.array([]).astype(int) # 1-D when empty, as callers expect + return np.empty((0, 2), dtype=int) return np.column_stack([row_shanks[keep], row_electrodes[keep]]).astype(int) def make_wiring_maps(wiring_maps_dir): @@ -165,9 +164,10 @@ def _load_wiring_maps(wiring_maps_dir): wiring_df = pd.read_csv(wiring_file) wiring_maps[probe_type] = {} + wiring = _wiring_index(wiring_df) # once per table, not per electrode for (shank_id, electrode_id, _, _) in positions_df.values: electrode = Electrode(shank_id=shank_id, electrode_id=electrode_id) - conflicting_electrodes = find_forbidden_electrodes([(shank_id, electrode_id)], wiring_df) + conflicting_electrodes = _forbidden_electrodes([(shank_id, electrode_id)], *wiring) conflicting_electrodes = {Electrode(e[0], e[1]) for e in conflicting_electrodes} wiring_maps[probe_type][electrode] = conflicting_electrodes @@ -685,18 +685,38 @@ def _darken_hex(hex_color: str, factor: float = 0.6) -> str: return f"#{r:02x}{g:02x}{b:02x}" -def _add_rects(ax, rects, facecolors, zorder=1): - """Add borderless rectangles to ``ax`` as a single PatchCollection. +_SELECTED_COLOR, _UNSELECTED_COLOR, _FORBIDDEN_COLOR = "red", "lightgrey", "black" +_SELECTED_RGBA = mcolors.to_rgba(_SELECTED_COLOR, 1.0) +_FORBIDDEN_RGBA = mcolors.to_rgba(_FORBIDDEN_COLOR, 1.0) +_UNSELECTED_RGBA = mcolors.to_rgba(_UNSELECTED_COLOR, 0.7) + + +def _electrode_rgba(is_selected, is_forbidden): + """Per-electrode facecolour: selected wins over forbidden over unselected.""" + return np.select( + [np.asarray(is_selected)[:, None], np.asarray(is_forbidden)[:, None]], + [_SELECTED_RGBA, _FORBIDDEN_RGBA], + default=_UNSELECTED_RGBA, + ) + + +def _add_rects(ax, x0, y0, width, height, facecolors, zorder=1): + """Add borderless rectangles (lower-left corners ``x0, y0``) as one collection. A probe has thousands of electrodes; adding each as its own patch made the PDF export spend seconds in per-artist bookkeeping (data-limit updates, transform setup, separate draw calls) on the server's event loop. One - collection renders identically for a fraction of the cost. + collection built straight from a vertex array renders identically for a + fraction of the cost. """ - if not rects: + x0 = np.asarray(x0, dtype=float) + if not x0.size: return + y0 = np.asarray(y0, dtype=float) + x1, y1 = x0 + width, y0 + height + verts = np.stack([np.column_stack(c) for c in ((x0, y0), (x1, y0), (x1, y1), (x0, y1))], axis=1) ax.add_collection( - PatchCollection(rects, facecolors=facecolors, edgecolors="none", linewidths=0, zorder=zorder), + PolyCollection(verts, facecolors=facecolors, edgecolors="none", linewidths=0, zorder=zorder), autolim=False, ) @@ -712,11 +732,6 @@ def plot_probe_layout( """ - # Format parameters - selected_color = "red" - unselected_color = "lightgrey" - forbidden_color = "black" - if probe_type in ["1.0", "2.0-1shank"]: n_shanks = 1 else: # "2.0-4shanks" and future multi-shank types @@ -827,48 +842,27 @@ def plot_probe_layout( # Draw electrodes (and optional survey sidebar bars) is_selected = np.isin(positions[:, 1], selected_electrodes[:, 1]) is_forbidden = np.isin(positions[:, 1], forbidden_electrodes[:, 1]) - electrode_rects, electrode_colors = [], [] - bar_rects, bar_colors = [], [] - for (electrode, orig_x, y), sel, forb in zip(positions[:, 1:], is_selected, is_forbidden): - if sel: - color = selected_color - alpha = 1.0 - elif forb: - color = forbidden_color - alpha = 1 - else: - color = unselected_color - alpha = 0.7 - - # Map original x positions to shank width (normalized then scaled) - if probe_type in ["1.0"]: - # Original positions: 11,27,43,59 μm (range: 48 μm, center at 35) - # Normalize to [-1, 1] range: (x - 35) / 24 - x_norm = (orig_x - 35) / 24 - x = x_norm * (shank_width * 0.8) / 2 # Use 80% of shank width - else: # linear - # Original positions: 0,32 μm (range: 32 μm, center at 16) - # Normalize to [-1, 1] range: (x - 16) / 16 - x_norm = (orig_x - 16) / 16 - x = x_norm * (shank_width * 0.6) / 2 # Use 60% of shank width - - electrode_rects.append(patches.Rectangle( - (x - electrode_width // 2, y - electrode_height // 2), - electrode_width, - electrode_height, - )) - electrode_colors.append(mcolors.to_rgba(color, alpha)) - - # Survey sidebar bar (always on right side in PDF) - if survey_colors is not None: - bar_rects.append(patches.Rectangle( - (survey_bar_center_x - survey_bar_width / 2, y - electrode_height / 2), - survey_bar_width, electrode_height, - )) - bar_colors.append(survey_colors.get((0, int(electrode)), "#dddddd")) - - _add_rects(ax, electrode_rects, electrode_colors) - _add_rects(ax, bar_rects, bar_colors, zorder=50) + electrodes, orig_x, ys = positions[:, 1], positions[:, 2], positions[:, 3] + # Map original x positions to shank width (normalized then scaled) + if probe_type in ["1.0"]: + # Original positions: 11,27,43,59 μm (range: 48 μm, center at 35) + # Normalize to [-1, 1] range: (x - 35) / 24; use 80% of shank width + xs = (orig_x - 35) / 24 * (shank_width * 0.8) / 2 + else: # linear + # Original positions: 0,32 μm (range: 32 μm, center at 16) + # Normalize to [-1, 1] range: (x - 16) / 16; use 60% of shank width + xs = (orig_x - 16) / 16 * (shank_width * 0.6) / 2 + _add_rects( + ax, xs - electrode_width // 2, ys - electrode_height // 2, + electrode_width, electrode_height, _electrode_rgba(is_selected, is_forbidden), + ) + # Survey sidebar bars (always on right side in PDF) + if survey_colors is not None: + _add_rects( + ax, np.full(len(ys), survey_bar_center_x - survey_bar_width / 2), ys - electrode_height / 2, + survey_bar_width, electrode_height, + [survey_colors.get((0, int(e)), "#dddddd") for e in electrodes], zorder=50, + ) ax.set_xlim(-shank_width // 2 - 20, shank_width // 2 + 20) ax.set_ylim(min_y - tip_height - 50, max_y + 100) @@ -932,7 +926,6 @@ def plot_probe_layout( for shank_id in range(n_shanks): shank_m = positions[:, 0] == shank_id - shank_m_selected = selected_electrodes[:, 0] == shank_id x_center = shank_id * shank_spacing # Draw shank outline with pointy tip @@ -1004,50 +997,29 @@ def plot_probe_layout( # Draw electrodes (and optional survey sidebar bars) shank_positions = positions[shank_m] - shank_selected = selected_electrodes[shank_m_selected] - is_selected = np.isin(shank_positions[:, 0], shank_selected[:, 0]) & np.isin( - shank_positions[:, 1], shank_selected[:, 1] + # Both lists are filtered to this shank, so matching on electrode ID + # alone matches (shank, electrode) pairs. + shank_selected = selected_electrodes[selected_electrodes[:, 0] == shank_id, 1] + shank_forbidden = forbidden_electrodes[forbidden_electrodes[:, 0] == shank_id, 1] + is_selected = np.isin(shank_positions[:, 1], shank_selected) + is_forbidden = np.isin(shank_positions[:, 1], shank_forbidden) + shank_ids, electrodes, orig_x, ys = shank_positions.T + # Map electrode positions to shank width (normalized then scaled) + # Original positions: 0,32 μm (center at 16) - always 2.0 + xs = x_center + (orig_x - true_shank_spacing * shank_id - 16) / 16 * (shank_width * 0.5) / 2 + _add_rects( + ax, xs - electrode_width // 2, ys - electrode_height // 2, + electrode_width, electrode_height, _electrode_rgba(is_selected, is_forbidden), ) - is_forbidden = np.isin(shank_positions[:, 0], forbidden_electrodes[:, 0]) & np.isin( - shank_positions[:, 1], forbidden_electrodes[:, 1] - ) - electrode_rects, electrode_colors = [], [] - bar_rects, bar_colors = [], [] - for (shank_id_pos, electrode, orig_x, y), sel, forb in zip(shank_positions, is_selected, is_forbidden): - # Check if this electrode is selected - if sel: - electrode_color = selected_color - alpha = 1.0 - elif forb: - electrode_color = forbidden_color - alpha = 1 - else: - electrode_color = unselected_color - alpha = 0.7 - - # Map electrode positions to shank width (normalized then scaled) - # Original positions: 0,32 μm (center at 16) - always 2.0 - x_norm = (orig_x - true_shank_spacing * shank_id - 16) / 16 - x = x_center + x_norm * (shank_width * 0.5) / 2 - - electrode_rects.append(patches.Rectangle( - (x - electrode_width // 2, y - electrode_height // 2), - electrode_width, - electrode_height, - )) - electrode_colors.append(mcolors.to_rgba(electrode_color, alpha)) - - # Survey sidebar bar (always on right side of each shank in PDF) - if survey_colors is not None: - bar_x = x_center + ms_survey_bar_center_offset - bar_rects.append(patches.Rectangle( - (bar_x - ms_survey_bar_width / 2, y - electrode_height / 2), - ms_survey_bar_width, electrode_height, - )) - bar_colors.append(survey_colors.get((int(shank_id_pos), int(electrode)), "#dddddd")) - - _add_rects(ax, electrode_rects, electrode_colors) - _add_rects(ax, bar_rects, bar_colors, zorder=50) + # Survey sidebar bars (always on right side of each shank in PDF) + if survey_colors is not None: + bar_x = x_center + ms_survey_bar_center_offset + _add_rects( + ax, np.full(len(ys), bar_x - ms_survey_bar_width / 2), ys - electrode_height / 2, + ms_survey_bar_width, electrode_height, + [survey_colors.get((int(sh), int(e)), "#dddddd") for sh, e in zip(shank_ids, electrodes)], + zorder=50, + ) # Add shank label ax.text( @@ -1116,19 +1088,19 @@ def plot_probe_layout( from matplotlib.lines import Line2D legend_elements = [ - Line2D([0], [0], marker="s", color="w", markerfacecolor=selected_color, markersize=10, label="Selected"), + Line2D([0], [0], marker="s", color="w", markerfacecolor=_SELECTED_COLOR, markersize=10, label="Selected"), Line2D( [0], [0], marker="s", color="w", - markerfacecolor=unselected_color, + markerfacecolor=_UNSELECTED_COLOR, markersize=10, alpha=0.7, label="Unselected", ), Line2D( - [0], [0], marker="s", color="w", markerfacecolor=forbidden_color, markersize=10, alpha=1, label="Forbidden" + [0], [0], marker="s", color="w", markerfacecolor=_FORBIDDEN_COLOR, markersize=10, alpha=1, label="Forbidden" ), ] # Pin ``loc`` explicitly. Without it matplotlib uses loc="best", which on diff --git a/pixelmap/error_reporting.py b/pixelmap/error_reporting.py index 4e324c1..9e9e33c 100644 --- a/pixelmap/error_reporting.py +++ b/pixelmap/error_reporting.py @@ -150,7 +150,7 @@ def load_webhook_config(environ: Mapping[str, str] | None = None) -> WebhookConf def _format_traceback(exc: BaseException) -> str: try: - return "".join(traceback.format_exception(type(exc), exc, exc.__traceback__)) + return "".join(traceback.format_exception(exc)) except Exception: # noqa: BLE001 - formatting must never fail the report return f"" @@ -214,23 +214,26 @@ def post_webhook(payload: dict[str, Any], config: WebhookConfig) -> bool: def _dedupe_key(exc: BaseException) -> tuple: - location = None - tb = exc.__traceback__ - while tb is not None and tb.tb_next is not None: - tb = tb.tb_next - if tb is not None: - location = (tb.tb_frame.f_code.co_filename, tb.tb_lineno) + frames = traceback.extract_tb(exc.__traceback__) + location = (frames[-1].filename, frames[-1].lineno) if frames else None return (type(exc).__qualname__, _safe_str(exc)[:200], location) -def _toast(message: str) -> None: - """Show an error toast in the current session, if it has a notification area.""" +def notify(level: str, message: str, **kwargs) -> None: + """Emit a Panel notification, or do nothing if the session has no area. + + `pn.state.notifications` is None outside a served session (tests, scripts). + Reaching through it unguarded raises inside a Bokeh callback, and Bokeh + discards every pending document write when a locked callback raises -- so + one missing notification silently loses all the plot updates the callback + had already made. Swallowing the notification is always the lesser evil. + """ try: notifications = pn.state.notifications if notifications is not None: - notifications.error(message, duration=10_000) + getattr(notifications, level)(message, **kwargs) except Exception: - logger.debug("Could not show error toast", exc_info=True) + logger.debug("Could not show %s notification", level, exc_info=True) class ErrorReporter: @@ -286,7 +289,7 @@ def _report(self, exc: BaseException, context: str | None, notify_user: bool) -> message = _truncate(message, MAX_TOAST_CHARS) if config.enabled: message += " (the error has been reported)" - _toast(message) + notify("error", message, duration=10_000) if config.enabled and self._should_send(exc, config): self._dispatch(build_payload(exc, context, _current_session_id()), config) diff --git a/pixelmap/gui/assets/dependent_deselector.png b/pixelmap/gui/assets/dependent_deselector.png index 001cd32..3b6f885 100644 Binary files a/pixelmap/gui/assets/dependent_deselector.png and b/pixelmap/gui/assets/dependent_deselector.png differ diff --git a/pixelmap/gui/assets/selector_boxes.svg b/pixelmap/gui/assets/selector_boxes.svg index 4ffc1d1..38b1c52 100644 --- a/pixelmap/gui/assets/selector_boxes.svg +++ b/pixelmap/gui/assets/selector_boxes.svg @@ -23,13 +23,13 @@ inkscape:pagecheckerboard="0" inkscape:deskcolor="#d1d1d1" inkscape:document-units="mm" - inkscape:zoom="1.4628675" - inkscape:cx="271.72659" - inkscape:cy="458.68815" - inkscape:window-width="2560" - inkscape:window-height="1381" - inkscape:window-x="1440" - inkscape:window-y="31" + inkscape:zoom="1.3912991" + inkscape:cx="419.3922" + inkscape:cy="853.87822" + inkscape:window-width="1728" + inkscape:window-height="1055" + inkscape:window-x="0" + inkscape:window-y="34" inkscape:window-maximized="0" inkscape:current-layer="layer1" /> + sodipodi:nodetypes="cc" /> diff --git a/pixelmap/gui/assets/zigzag_dependent_deselector.png b/pixelmap/gui/assets/zigzag_dependent_deselector.png new file mode 100644 index 0000000..52cdbb7 Binary files /dev/null and b/pixelmap/gui/assets/zigzag_dependent_deselector.png differ diff --git a/pixelmap/gui/gui.py b/pixelmap/gui/gui.py index e617d87..d3f1cc0 100644 --- a/pixelmap/gui/gui.py +++ b/pixelmap/gui/gui.py @@ -5,6 +5,7 @@ import asyncio import base64 +import contextlib import functools import json import logging @@ -15,6 +16,7 @@ from dataclasses import dataclass, field from io import BytesIO, StringIO from pathlib import Path +from typing import Any, NamedTuple import matplotlib.pyplot as plt import numpy as np @@ -22,6 +24,7 @@ import panel as pn import param from bokeh import events +from bokeh.core.properties import validate from bokeh.models import ( BoxSelectTool, ColorBar, @@ -58,6 +61,7 @@ SUPPORTED_QuadBase_PRESETS, ) from pixelmap.error_reporting import guarded +from pixelmap.error_reporting import notify as _notify from pixelmap.types import Electrode from pixelmap.utils import imro, survey, url_share from pixelmap.utils.survey import default_survey_range @@ -74,29 +78,12 @@ # Enable Panel extensions. # -# This module-level call only covers notebook/library use and the *first* -# session of a server. `pn.config.notifications` is stored per-session -# (keyed on `pn.state.curdoc`), and under `panel serve` this module is -# imported inside the first session's document context, so every later -# session would inherit the default of False. `create_app` therefore -# repeats the call per session -- see the note there. +# This module-level call is for notebook/library use. `create_app` repeats it +# per session -- see the note there. PANEL_EXTENSIONS = ("tabulator",) pn.extension(*PANEL_EXTENSIONS, notifications=True) -def _notify(level: str, message: str, **kwargs) -> None: - """Emit a Panel notification, or do nothing if the session has no area. - - `pn.state.notifications` is None outside a served session (tests, scripts). - Reaching through it unguarded raises inside a Bokeh callback, and Bokeh - discards every pending document write when a locked callback raises -- so - one missing notification silently loses all the plot updates the callback - had already made. Swallowing the notification is always the lesser evil. - """ - notifications = pn.state.notifications - if notifications is None: - return - getattr(notifications, level)(message, **kwargs) @functools.lru_cache(maxsize=16) @@ -124,6 +111,60 @@ def _darken_hex(hex_color: str, factor: float = 0.6) -> str: return f"#{r:02x}{g:02x}{b:02x}" +def _measure_tip_depth(tip, pitch_deg, yaw_deg, atlas_name) -> float | None: + """Tip-to-surface depth in µm, or ``None`` if outside or the lookup fails.""" + try: + return tip_depth_below_surface_um( + tip, pitch_deg=pitch_deg, yaw_deg=yaw_deg, atlas_name=atlas_name + ) + except Exception as exc: # noqa: BLE001 - a readout must never sink the GUI + print(f"Tip depth lookup failed: {exc}") + return None + + +_BUTTON_COMPUTE = "Compute anatomical overlay 🧠" +_BUTTON_DOWNLOAD = "Download & compute atlas 🧠 ⏳" +_BUTTON_DOWNLOADING = "Downloading atlas… (first use only)" + + +@dataclass(frozen=True) +class _AnatomyRequest: + """Every input an overlay compute reads, snapshotted on the event loop. + + The compute itself runs in a worker thread, which must not touch widgets: + Panel/Bokeh models are only safe to read and write under the document lock. + Capturing everything up front also pins the atlas, so a dropdown change + mid-compute can't make one render mix two atlases. + """ + + atlas_name: str + probe_xs: dict + plot_centers: dict + y_max: float + tip: tuple + pitch_deg: float + yaw_deg: float + shank_orientation_deg: float + bregma_um: tuple | None + ap_squish: float + ml_squish: float + dv_squish: float + tilt_deg: float + generation: int + locator_fig: Any = None # reused across renders; see _compute_anatomy + + +@dataclass +class _AnatomyResult: + """What a worker-thread compute hands back for the loop to draw.""" + + bands: list | None = None + error: Exception | None = None + locator_html: str | None = None + tip_depth: float | None = None + locator_fig: Any = None + + ################################### #### Electrode selection logic #### ################################### @@ -226,6 +267,55 @@ def _restore_selection(self, target: frozenset[Electrode]): #### GUI app #### ################# +class ShankOutlineGeometry(NamedTuple): + """What a probe's outline draws, ready for one renderer per style.""" + + x_limits: tuple[float, float] + outline_xs: list[list[float]] + outline_ys: list[list[float]] + bank_lines_xs: list[list[float]] + bank_lines_ys: list[list[float]] + shank_labels: tuple[list[float], list[float], list[str]] # xs, ys, texts + bank_labels: tuple[list[float], list[float], list[str]] # xs, ys, texts + + +def shank_outline_geometry(positions: np.ndarray, single_shank: bool) -> ShankOutlineGeometry: + """Compute outlines, bank lines and labels from ``(shank, electrode, x, y)`` rows.""" + half_width = 50 + if single_shank: + centers = [0] + tip_fraction = 0.1 + x_limits = (-half_width - 150, half_width + 150) + else: + centers = [shank_id * 250 for shank_id in range(4)] + tip_fraction = 0.08 + x_limits = (-half_width - 100, centers[-1] + half_width + 100) + + geometry = ShankOutlineGeometry(x_limits, [], [], [], [], ([], [], []), ([], [], [])) + for shank_id, center in enumerate(centers): + ys = positions[positions[:, 0] == shank_id, -1] + max_y, min_y = float(np.max(ys)), float(np.min(ys)) + tip_y = min_y - (max_y - min_y) * tip_fraction + left, right = center - half_width, center + half_width + + geometry.outline_xs.append([left, right, right, center, left, left]) + geometry.outline_ys.append([max_y + 100, max_y + 100, min_y, tip_y, min_y, max_y + 100]) + + if not single_shank: + geometry.shank_labels[0].append(center) + geometry.shank_labels[1].append(tip_y - 250) + geometry.shank_labels[2].append(f"Shank {shank_id}") + + for bank, bank_y in enumerate(ys[::384].tolist()): + geometry.bank_lines_xs.append([left, right]) + geometry.bank_lines_ys.append([bank_y, bank_y]) + if center == centers[-1]: # bank labels only beside the rightmost shank + geometry.bank_labels[0].append(right + 17) + geometry.bank_labels[1].append(bank_y) + geometry.bank_labels[2].append(f"Bank {bank}") + return geometry + + class ChannelmapGUI(param.Parameterized): """Main GUI class for interactive channelmap generation using Bokeh""" @@ -302,6 +392,21 @@ def __init__(self, **params): self.survey_color_bar_title = None self._survey_range_suspend = False + # Anatomy overlay state. Once an overlay is requested, live-update it + # whenever any pose / bregma / squish / tilt input changes; set on + # Compute, not on the first draw, so edits made while that first + # compute runs reach it too. + self._anatomy_overlay_active = False + # Off-loop compute bookkeeping (see _drain_anatomy_requests): whether + # inputs changed since the last compute started, whether a compute is + # running, a counter bumped by Clear so a compute that outlives its + # overlay is dropped, and a flag batched field writes set to hold off + # recomputes until they're done. + self._anatomy_dirty = False + self._anatomy_compute_running = False + self._anatomy_generation = 0 + self._suppress_anatomy_recompute = False + # Load initial data self.load_probe_data() @@ -382,98 +487,77 @@ def load_probe_data(self): def create_electrode_data(self): """Create the electrode data for Bokeh visualization""" positions = self.positions_df.values - - # Calculate electrode positions and colors - electrode_data = { - "x": [], - "y": [], - "width": [], - "height": [], - "shank_id": [], - "electrode_id": [], - "color": [], - "alpha": [], - "line_color": [], - "line_width": [], - "status": [], - "val": [], - "bar_x": [], - "bar_height": [], - "bar_color": [], - "bar_alpha": [], - } + shank_ids, electrode_ids, orig_x, y = positions.T # Parameters for visualization BAR_WIDTH = 16 BAR_GAP = 4 # gap between shank outline and survey bar + shank_width = 100 + half_span = shank_width * 0.7 / 2 - if self.probe_type in ["1.0", "2.0-1shank"]: - # Single shank - shank_width = 100 - electrode_width = 15 - electrode_height = 9 if self.probe_type == "2.0-1shank" else 14 - shank_spacing = None - else: - # Multi-shank - shank_width = 100 - shank_spacing = 250 - electrode_width = 12 - electrode_height = 8 + # Map each electrode's x position to the drawn shank's width. + if self.probe_type == "1.0": + electrode_width, electrode_height = 15, 14 + x_center_shank = np.zeros(len(positions)) + x = (orig_x - 35) / 24 * half_span + elif self.probe_type == "2.0-1shank": + electrode_width, electrode_height = 15, 9 + x_center_shank = np.zeros(len(positions)) + x = (orig_x - 16) / 16 * half_span + else: # 4-shanks 2.0 or NXT + electrode_width, electrode_height = 12, 8 + x_center_shank = shank_ids * 250 + x = x_center_shank + (orig_x - x_center_shank - 16) / 16 * half_span # Bars sit just outside the shank outline, mirroring the column the # electrode lives in: left-column contacts → bar on the left of the # outline, right-column contacts → bar on the right. bar_offset = shank_width / 2 + BAR_GAP + BAR_WIDTH / 2 + bar_x = x_center_shank + np.where(x >= x_center_shank, 1, -1) * bar_offset + + # Numeric columns are arrays: Bokeh sends those as binary instead of + # encoding each of the thousands of values separately. + electrode_data = { + "x": x, + "y": y, + "width": np.full(len(positions), electrode_width), + "height": np.full(len(positions), electrode_height), + "shank_id": shank_ids, + "electrode_id": electrode_ids, + "bar_x": bar_x, + "bar_height": np.full(len(positions), electrode_height * 1.5), + **self._electrode_appearance_columns(shank_ids.tolist(), electrode_ids.tolist()), + } + + # Skip Bokeh's check of every cell, which took half of this method and + # runs on the event loop for each session: the shape is fixed right here. + with validate(False): + self.electrode_source = ColumnDataSource(data=electrode_data) - for shank_id, electrode_id, orig_x, y in positions: - - # Map x position to shank width - if self.probe_type == "1.0": - x_norm = (orig_x - 35) / 24 - x = x_norm * (shank_width * 0.7) / 2 - x_center_shank = 0.0 - elif self.probe_type == "2.0-1shank": - x_norm = (orig_x - 16) / 16 - x = x_norm * (shank_width * 0.7) / 2 - x_center_shank = 0.0 - else: # 4-shanks 2.0 or NXT - # Calculate shank center - x_center_shank = shank_id * shank_spacing - # Map electrode position within shank - x_norm = (orig_x - x_center_shank - 16) / 16 - x = x_center_shank + x_norm * (shank_width * 0.7) / 2 - - # Bar x: just outside the shank outline, on the side matching - # the electrode's column relative to the shank center. - side = 1 if (x - x_center_shank) >= 0 else -1 - bar_x = x_center_shank + side * bar_offset - - # Determine electrode status and color + def _electrode_appearance_columns(self, shank_ids, electrode_ids) -> dict[str, Any]: + """Selection- and survey-dependent columns, one value per electrode.""" + n_electrodes = len(shank_ids) + colors = [""] * n_electrodes + alphas = np.empty(n_electrodes, dtype=np.float64) + line_colors = [""] * n_electrodes + line_widths = np.empty(n_electrodes, dtype=np.int32) + statuses = [""] * n_electrodes + vals = np.empty(n_electrodes, dtype=np.float64) + bar_colors = [""] * n_electrodes + bar_alphas = np.empty(n_electrodes, dtype=np.float64) + + for i, (shank_id, electrode_id) in enumerate(zip(shank_ids, electrode_ids, strict=True)): electrode = Electrode(shank_id, electrode_id) - status, color, alpha, line_color, line_width = self.get_electrode_plotting_params(electrode) - val = self._get_survey_val(electrode) - bar_color, bar_alpha = self._get_bar_color_alpha(electrode) - - electrode_data["x"].append(x) - electrode_data["y"].append(y) - electrode_data["width"].append(electrode_width) - electrode_data["height"].append(electrode_height) - electrode_data["shank_id"].append(shank_id) - electrode_data["electrode_id"].append(electrode_id) - electrode_data["color"].append(color) - electrode_data["alpha"].append(alpha) - electrode_data["line_color"].append(line_color) - electrode_data["line_width"].append(line_width) - electrode_data["status"].append(status) - electrode_data["val"].append(val) - electrode_data["bar_x"].append(bar_x) - electrode_data["bar_height"].append(electrode_height * 1.5) - electrode_data["bar_color"].append(bar_color) - electrode_data["bar_alpha"].append(bar_alpha) - - # Create ColumnDataSource - self.electrode_source = ColumnDataSource(data=electrode_data) + statuses[i], colors[i], alphas[i], line_colors[i], line_widths[i] = ( + self.get_electrode_plotting_params(electrode) + ) + vals[i] = self._get_survey_val(electrode) + bar_colors[i], bar_alphas[i] = self._get_bar_color_alpha(electrode) + return { + "color": colors, "alpha": alphas, "line_color": line_colors, "line_width": line_widths, + "status": statuses, "val": vals, "bar_color": bar_colors, "bar_alpha": bar_alphas, + } def get_electrode_plotting_params(self, electrode: Electrode): """Get electrode fill/border appearance based on selection state.""" @@ -551,96 +635,28 @@ def setup_electrode_visualization(self): def add_shank_outlines(self): - """Add shank outlines and bank labels""" - positions = self.positions_df.values - - if self.probe_type in ["1.0", "2.0-1shank"]: - # Single shank outline - shank_width = 100 - xlim = [-shank_width / 2 - 150, shank_width / 2 + 150] - max_y = np.max(positions[:, -1]) - min_y = np.min(positions[:, -1]) - tip_height = (max_y - min_y) * 0.1 - - # Shank outline - shank_x = [-shank_width / 2, shank_width / 2, shank_width / 2, 0, -shank_width / 2, -shank_width / 2] - shank_y = [max_y + 100, max_y + 100, min_y, min_y - tip_height, min_y, max_y + 100] - - self.plot.line(shank_x, shank_y, line_width=3, color="black", alpha=1) - self.plot.x_range = Range1d(xlim[0], xlim[1]) - - # Bank labels - for bank_i in np.arange(0, len(positions), 384): - if bank_i < len(positions): - bank_y = positions[bank_i, -1] - self.plot.line( - [-shank_width / 2, shank_width / 2], [bank_y, bank_y], line_width=2, color="gray", alpha=0.7 - ) - self.plot.text( - [shank_width / 2 + 17], - [bank_y], - text=[f"Bank {bank_i // 384}"], - text_font_size="10pt", - text_color="gray", - ) + """Add shank outlines and bank labels. - else: - # Multi-shank outlines - shank_width = 100 - shank_spacing = 250 - xlim = [-shank_width / 2 - 100, 3 * shank_spacing + shank_width / 2 + 100] - - for shank_id in range(4): - shank_mask = positions[:, 0] == shank_id - x_center = shank_id * shank_spacing - - max_y = np.max(positions[shank_mask, -1]) - min_y = np.min(positions[shank_mask, -1]) - tip_height = (max_y - min_y) * 0.08 - - # Shank outline - shank_x = [ - x_center - shank_width / 2, - x_center + shank_width / 2, - x_center + shank_width / 2, - x_center, - x_center - shank_width / 2, - x_center - shank_width / 2, - ] - shank_y = [max_y + 100, max_y + 100, min_y, min_y - tip_height, min_y, max_y + 100] - - self.plot.line(shank_x, shank_y, line_width=3, color="black", alpha=1) - self.plot.x_range = Range1d(xlim[0], xlim[1]) - - # Shank label - self.plot.text( - [x_center], - [min_y - tip_height - 250], - text=[f"Shank {shank_id}"], - text_font_size="12pt", - text_color="black", - text_align="center", - ) - - # Bank lines and labels - for bank_i in np.arange(0, len(positions[shank_mask]), 384): - if bank_i < len(positions[shank_mask]): - bank_y = positions[shank_mask][bank_i, -1] - self.plot.line( - [x_center - shank_width / 2, x_center + shank_width / 2], - [bank_y, bank_y], - line_width=2, - color="gray", - alpha=0.7, - ) - if shank_id == 3: # Bank labels (only on rightmost shank) - self.plot.text( - [x_center + shank_width / 2 + 17], - [bank_y], - text=[f"Bank {bank_i // 384}"], - text_font_size="10pt", - text_color="gray", - ) + One renderer per style, not one per shape: each renderer brings about + ten Bokeh models, which every session then builds, validates, + serialises and sends on the event loop. + """ + geometry = shank_outline_geometry( + self.positions_df.values, + single_shank=self.probe_type in ["1.0", "2.0-1shank"], + ) + self.plot.x_range = Range1d(*geometry.x_limits) + self.plot.multi_line(geometry.outline_xs, geometry.outline_ys, line_width=3, color="black", alpha=1) + self.plot.multi_line(geometry.bank_lines_xs, geometry.bank_lines_ys, line_width=2, color="gray", alpha=0.7) + + label_xs, label_ys, labels = geometry.shank_labels + if labels: + self.plot.text( + label_xs, label_ys, text=labels, + text_font_size="12pt", text_color="black", text_align="center", + ) + label_xs, label_ys, labels = geometry.bank_labels + self.plot.text(label_xs, label_ys, text=labels, text_font_size="10pt", text_color="gray") # Set appropriate axis limits self.plot.axis.visible = False @@ -649,39 +665,9 @@ def add_shank_outlines(self): def update_electrode_colors(self): """Update electrode colors in the Bokeh plot""" - n_electrodes = len(self.electrode_source.data["shank_id"]) - - # Pre-allocate numpy arrays for better memory efficiency - colors = np.empty(n_electrodes, dtype=object) - alphas = np.empty(n_electrodes, dtype=np.float64) - line_colors = np.empty(n_electrodes, dtype=object) - line_widths = np.empty(n_electrodes, dtype=np.int32) - statuses = np.empty(n_electrodes, dtype=object) - vals = np.empty(n_electrodes, dtype=np.float64) - bar_colors = np.empty(n_electrodes, dtype=object) - bar_alphas = np.empty(n_electrodes, dtype=np.float64) - - for i in range(n_electrodes): - shank_id = self.electrode_source.data["shank_id"][i] - electrode_id = self.electrode_source.data["electrode_id"][i] - electrode = Electrode(shank_id, electrode_id) - - status, color, alpha, line_color, line_width = self.get_electrode_plotting_params(electrode) - - colors[i] = color - alphas[i] = alpha - line_colors[i] = line_color - line_widths[i] = line_width - statuses[i] = status - vals[i] = self._get_survey_val(electrode) - bar_colors[i], bar_alphas[i] = self._get_bar_color_alpha(electrode) - - # Update the data source with new data, replacing old arrays + data = self.electrode_source.data self.electrode_source.data.update( - {"color": colors.tolist(), "alpha": alphas.tolist(), - "line_color": line_colors.tolist(), "line_width": line_widths.tolist(), - "status": statuses.tolist(), "val": vals.tolist(), - "bar_color": bar_colors.tolist(), "bar_alpha": bar_alphas.tolist()} + self._electrode_appearance_columns(data["shank_id"].tolist(), data["electrode_id"].tolist()) ) @@ -749,28 +735,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 +755,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": @@ -986,7 +981,7 @@ def generate_pdf_content(self): # Collect anatomy overlay data (if active) anatomy_bands = None - if getattr(self, "_anatomy_overlay_active", False): + if self._anatomy_overlay_active: band_src = self.region_band_source.data if band_src["x"]: plot_centers = self._shank_plot_centers() @@ -1225,17 +1220,13 @@ def _reposition_region_labels(self): @pn.io.with_lock async def _on_compute_anatomy_click(self, event): - """Compute button: download off-thread if needed, then draw. + """Compute button: (re)draw the overlay off the event loop. - The download is the only part of the overlay that can block for - seconds, and on the deployed server this callback runs on the Bokeh - event loop — the same one serving every other session and the - container healthcheck. So a cold atlas is fetched in a worker thread - and only the (fast, in-memory) drawing happens back on the loop. + See :meth:`_drain_anatomy_requests` for why the work runs in a thread. ``with_lock`` is not optional. Panel schedules async callbacks with ``nolock`` set unless the function carries ``lock = True`` - (``panel.io.server.async_execute``), and this method writes to Bokeh + (``panel.io.server.async_execute``), and the drawing writes to Bokeh ``ColumnDataSource`` models directly. Without the document lock every such write raises "_pending_writes should be non-None ...", which Bokeh turns into a *silently* dead overlay: the exception surfaces in @@ -1246,71 +1237,200 @@ async def _on_compute_anatomy_click(self, event): Bokeh holds the lock across the ``await`` rather than around each statement (``ServerSession._needs_document_lock``), so this stays a per-session lock: the event loop is free to serve other users while - the download runs, and the session cannot be reaped mid-download. + the work runs, and the session cannot be reaped mid-compute. """ - name = str(self.atlas_name_input.value).strip() - if anatomy_atlas.is_downloaded(name): - self.compute_anatomy_overlay() - return + self._anatomy_overlay_active = True + self._anatomy_dirty = True + await self._drain_anatomy_requests() - self.compute_anatomy_button.name = "Downloading atlas… (first use only)" - self.compute_anatomy_button.disabled = True - try: - await asyncio.to_thread(anatomy_atlas.ensure_downloaded, name) - except Exception as exc: # noqa: BLE001 - any download failure is reported to the user - print(f"Atlas download failed: {exc}") - self.compute_anatomy_button.name = "Download & compute atlas 🧠 ⏳" - _notify( - "error", - f"Could not download '{name}'. Check your connection and retry.", - ) + def _request_anatomy_recompute(self): + """Schedule an off-loop overlay recompute from a synchronous callback. + + Coalesces: while a compute is in flight the new drain returns at once, + and the running one picks up the latest inputs when it finishes, so a + burst of edits costs one extra compute, not one each. + """ + self._anatomy_dirty = True + pn.state.execute(self._drain_anatomy_requests) + + @pn.io.with_lock + async def _drain_anatomy_requests(self): + """Run overlay computes in a worker thread until the inputs stop changing. + + On the server, GUI callbacks run on the Bokeh event loop that serves + every session and the healthcheck, and every part of an overlay is slow + there: a cold atlas takes seconds to download, a cached one 0.7–3.3 s + to slice and render (allen_mouse_25um → 10um), and an AC origin up to + ~8 s to locate. So all of that happens in worker threads, and only the + snapshot and the drawing touch the loop. + + One drain per session at a time (``_anatomy_compute_running``); a + request arriving mid-compute sets ``_anatomy_dirty`` and is served by + the next lap. Each lap snapshots the widgets only after its downloads + and origin lookup, so it always draws the *current* pose and atlas — + including an atlas picked while the previous one was still downloading. + """ + if self._anatomy_compute_running: return + self._anatomy_compute_running = True + try: + while self._anatomy_dirty and self._anatomy_overlay_active: + self._anatomy_dirty = False + name = self._selected_atlas_name() + if not anatomy_atlas.is_downloaded(name): + self.compute_anatomy_button.name = _BUTTON_DOWNLOADING + self.compute_anatomy_button.disabled = True + try: + await asyncio.to_thread(anatomy_atlas.ensure_downloaded, name) + except Exception as exc: # noqa: BLE001 - any download failure is reported to the user + print(f"Atlas download failed: {exc}") + # Whatever is on screen is for another atlas now, and + # leaving the overlay live would retry the download on + # every later edit. Clicking Compute retries. + self.clear_anatomy_overlay() + self._refresh_compute_button() + _notify( + "error", + f"Could not download '{name}'. Check your connection and retry.", + ) + return + if self._origin_spec(name)["status"] == "ac_pending": + # Before the snapshot, so this lap draws with the origin. + await self._locate_ac_origin(name) + if self._anatomy_dirty: + continue # inputs moved while we waited: re-snapshot + request = self._anatomy_request() + result = await asyncio.to_thread(self._compute_anatomy, request) + self._apply_anatomy(request, result) finally: + self._anatomy_compute_running = False self.compute_anatomy_button.disabled = False - self.compute_anatomy_overlay() def compute_anatomy_overlay(self): - """Compute region bands for the current pose and render them.""" - atlas_name = str(self.atlas_name_input.value).strip() - if not anatomy_atlas.is_downloaded(atlas_name): - self.compute_anatomy_button.name = "Downloading atlas… (first use only)" - self.compute_anatomy_button.disabled = True - - probe_xs = self._shank_probe_xs() - plot_centers = self._shank_plot_centers() - y_max = float(self.positions_df["y"].max()) - - # If the origin landmark was "pending" (AC of a not-yet-downloaded - # atlas), computing below downloads it — remember to fill it in after. - origin_was_pending = ( - self._origin_spec(self.atlas_name_input.value)["status"] == "ac_pending" - ) + """Compute and draw the overlay for the current pose (the Compute button). - tip = self._tip_atlas_um() + Runs the same off-loop drain as the GUI. With no event loop running + (scripts, tests) Panel runs it to completion before returning; inside a + running loop (the served app, Jupyter) it is scheduled instead. + """ + self._anatomy_overlay_active = True + self._request_anatomy_recompute() + def _anatomy_request(self) -> _AnatomyRequest: + """Snapshot the overlay's inputs. Event loop only; cheap, no atlas reads.""" + atlas_name = self._selected_atlas_name() + # Bregma dot whenever an estimate exists or we're in bregma mode (using + # the current, possibly-edited, values). DV-squish + tilt warp the brain + # image only in bregma mode (where the squish/tilt inputs are live). + bregma_mode = bool(self.bregma_relative_toggle.value) + status = self._origin_spec(atlas_name)["status"] + bregma_um = None + ap_squish = ml_squish = dv_squish = 1.0 + tilt_deg = 0.0 + if bregma_mode or status in ("defined", "estimate"): + bregma_um = ( + float(self.bregma_ap_input.value), + float(self.bregma_ml_input.value), + float(self.bregma_dv_input.value), + ) + if bregma_mode: + ap_squish = float(self.ap_squish_input.value) or 1.0 + ml_squish = float(self.ml_squish_input.value) or 1.0 + dv_squish = float(self.dv_squish_input.value) or 1.0 + tilt_deg = float(self.tilt_input.value) + return _AnatomyRequest( + atlas_name=atlas_name, + probe_xs=self._shank_probe_xs(), + plot_centers=self._shank_plot_centers(), + y_max=float(self.positions_df["y"].max()), + tip=self._tip_atlas_um(), + pitch_deg=float(self.pitch_input.value), + yaw_deg=float(self.yaw_input.value), + shank_orientation_deg=float(self.shank_orientation_input.value), + bregma_um=bregma_um, + ap_squish=ap_squish, + ml_squish=ml_squish, + dv_squish=dv_squish, + tilt_deg=tilt_deg, + generation=self._anatomy_generation, + locator_fig=self._locator_fig, + ) + + def _compute_anatomy(self, req: _AnatomyRequest) -> _AnatomyResult: + """Every atlas read an overlay needs. Runs in a worker thread. + + Must not touch widgets, Bokeh models or session state — only ``req`` + and the atlas. + """ try: bands = compute_region_bands( - shank_positions=probe_xs, - y_range=(0.0, y_max), - tip_atlas=tip, - pitch_deg=float(self.pitch_input.value), - yaw_deg=float(self.yaw_input.value), - shank_orientation_deg=float(self.shank_orientation_input.value), - atlas_name=atlas_name, + shank_positions=req.probe_xs, + y_range=(0.0, req.y_max), + tip_atlas=req.tip, + pitch_deg=req.pitch_deg, + yaw_deg=req.yaw_deg, + shank_orientation_deg=req.shank_orientation_deg, + atlas_name=req.atlas_name, step_um=25.0, ) except Exception as exc: # noqa: BLE001 - any lookup failure is reported to the user - print(f"Anatomy lookup failed: {exc}") - self.compute_anatomy_button.name = "Compute anatomical overlay 🧠" - self.compute_anatomy_button.disabled = False + return _AnatomyResult(error=exc) + + result = _AnatomyResult(bands=bands, locator_fig=req.locator_fig) + try: + # Reuse this session's figure: building one per render cost 3.1 MB + # of RSS per overlay compute, and the overlay recomputes on every + # pose edit. + fig = render_locator( + req.atlas_name, + tip_atlas=req.tip, + pitch_deg=req.pitch_deg, + yaw_deg=req.yaw_deg, + shank_orientation_deg=req.shank_orientation_deg, + shank_positions=req.probe_xs, + y_range=(0.0, req.y_max), + bregma_um=req.bregma_um, + ap_squish=req.ap_squish, + ml_squish=req.ml_squish, + dv_squish=req.dv_squish, + tilt_deg=req.tilt_deg, + fig=req.locator_fig, + ) + result.locator_fig = fig + result.locator_html = self._fig_to_locator_html(fig) + except Exception as exc: # noqa: BLE001 - a failed locator shouldn't sink the overlay + print(f"Locator render failed: {exc}") + result.tip_depth = _measure_tip_depth( + req.tip, req.pitch_deg, req.yaw_deg, req.atlas_name + ) + + # Hand back the chunk buffers this render churned through. They are + # already freed; this is what unmaps them, so repeated overlay computes + # don't ratchet RSS upward for the life of the process. + anatomy_atlas.reclaim_free_memory() + return result + + def _apply_anatomy(self, req: _AnatomyRequest, result: _AnatomyResult): + """Draw a finished compute. Event loop only (writes Bokeh models).""" + if req.generation != self._anatomy_generation: + # Cleared while computing: drop the stale result. + if result.locator_fig is not self._locator_fig: + plt.close(result.locator_fig) + return + self._locator_fig = result.locator_fig + self._refresh_compute_button() + if result.error is not None: + print(f"Anatomy lookup failed: {result.error}") return + bands = result.bands width = self._shank_plot_width() half_width = width / 2 # Pull label text outside the shank outline so it doesn't overlap the # electrode rects; the offset widens when a survey overlay is loaded so - # labels clear the survey sidebar bars (see _region_label_offset). + # labels clear the survey sidebar bars. label_offset = self._region_label_offset(half_width) + plot_centers = req.plot_centers band_data = {"x": [], "y": [], "width": [], "height": [], "color": [], "acronym": []} label_data = {"x": [], "y": [], "text": [], "color": [], "center": []} @@ -1357,74 +1477,20 @@ def compute_anatomy_overlay(self): self._update_anatomy_legend(bands) # The atlas is now downloaded, so the origin corner can be shown. self._update_anatomy_origin_note() + if result.locator_html is not None: + self.anatomy_locator.object = result.locator_html - # Bregma dot whenever an estimate exists or we're in bregma mode (using - # the current, possibly-edited, values). DV-squish + tilt warp the brain - # image only in bregma mode (where the squish/tilt inputs are live). - bregma_mode = bool(self.bregma_relative_toggle.value) - # The atlas is downloaded now (bands were computed), so a previously - # "pending" AC landmark can be filled in (without clobbering user edits). - if origin_was_pending: - self._update_reference_params() - self.bregma_estimate_header.object = self._bregma_header_html() - has_origin = self._origin_spec(atlas_name)["status"] in ("defined", "estimate") - bregma_um = None - ap_squish = ml_squish = dv_squish = 1.0 - tilt_deg = 0.0 - if bregma_mode or has_origin: - bregma_um = ( - float(self.bregma_ap_input.value), - float(self.bregma_ml_input.value), - float(self.bregma_dv_input.value), - ) - if bregma_mode: - ap_squish = float(self.ap_squish_input.value) or 1.0 - ml_squish = float(self.ml_squish_input.value) or 1.0 - dv_squish = float(self.dv_squish_input.value) or 1.0 - tilt_deg = float(self.tilt_input.value) - - try: - # Reuse this session's figure: building one per render cost 3.1 MB - # of RSS per overlay compute, and the overlay recomputes on every - # pose edit. - _fig = render_locator( - atlas_name, - tip_atlas=tip, - pitch_deg=float(self.pitch_input.value), - yaw_deg=float(self.yaw_input.value), - shank_orientation_deg=float(self.shank_orientation_input.value), - shank_positions=probe_xs, - y_range=(0.0, y_max), - bregma_um=bregma_um, - ap_squish=ap_squish, - ml_squish=ml_squish, - dv_squish=dv_squish, - tilt_deg=tilt_deg, - fig=self._locator_fig, - ) - self._locator_fig = _fig - self.anatomy_locator.object = self._fig_to_locator_html(_fig) - except Exception as exc: # noqa: BLE001 - a failed locator shouldn't sink the overlay - print(f"Locator render failed: {exc}") - - # Hand back the chunk buffers this render churned through. They are - # already freed; this is what unmaps them, so repeated overlay computes - # don't ratchet RSS upward for the life of the process. - anatomy_atlas.reclaim_free_memory() - - # Overlay now exists → later input edits live-update it. - self._anatomy_overlay_active = True self.anatomy_locator_section.visible = True # Only now can the depth readout show a value: the overlay is up, and - # computing it downloaded the atlas if it wasn't already cached (the - # readout refuses to trigger a download itself). - self._update_tip_depth_readout() - self.compute_anatomy_button.name = "Compute anatomical overlay 🧠" - self.compute_anatomy_button.disabled = False + # computing it downloaded the atlas if it wasn't already cached. + self._update_tip_depth_readout(result.tip_depth) def clear_anatomy_overlay(self): """Wipe the region bands, labels, boundaries and reset the legend.""" self._anatomy_overlay_active = False # stop live-updating + # Any compute still in flight finishes in its worker thread, but its + # result is for an overlay the user just dismissed: drop it. + self._anatomy_generation += 1 self.region_band_source.data = { "x": [], "y": [], "width": [], "height": [], "color": [], "acronym": [], } @@ -1496,7 +1562,7 @@ def _anatomy_coord_note_html(self) -> str: native code as an FYI, but only when the atlas is already downloaded so switching atlases in the dropdown stays cheap. """ - name = str(self.atlas_name_input.value).strip() + name = self._selected_atlas_name() native = None try: if anatomy_atlas.is_downloaded(name): @@ -1534,22 +1600,59 @@ def _on_atlas_change(self, *events): The field updates are batched under a recompute guard so a single atlas switch triggers one overlay refresh, not one per field changed. """ - self._suppress_anatomy_recompute = True - try: + with self._anatomy_recompute_suppressed(): self._update_reference_params(*events) self._update_tip_to_atlas_center(*events) self._update_anatomy_origin_note(*events) self._update_landmark_labels() self.bregma_estimate_header.object = self._bregma_header_html() + # Reflect download state in the button label so users know before clicking. + self._refresh_compute_button() + name = self._selected_atlas_name() + if self._anatomy_overlay_active: + # The recompute locates a pending AC origin on its way through. + self._request_anatomy_recompute() + elif (self._origin_spec(name)["status"] == "ac_pending" + and anatomy_atlas.is_downloaded(name)): + # No overlay to do it, but the atlas is local, so the origin can be + # shown now — once a worker thread has scanned for it. + pn.state.execute(functools.partial(self._locate_ac_origin, name)) + + @pn.io.with_lock + async def _locate_ac_origin(self, name: str): + """Derive an atlas's AC origin in a worker thread, then fill the fields. + + The derivation reads every voxel of the annotation (~8 s on a 10 µm + atlas), so it must not run on the event loop the way it used to on + every switch to a human or fish atlas. + """ + try: + await asyncio.to_thread(anatomy_atlas.derive_origin_from_ac, name) + except Exception as exc: # noqa: BLE001 - AC not derivable: banner stays "pending" + print(f"Anterior-commissure lookup failed: {exc}") + return + if self._selected_atlas_name() == name: # still selected + self._fill_reference_params() + + def _fill_reference_params(self): + """Refill the origin fields and banner without triggering a recompute.""" + with self._anatomy_recompute_suppressed(): + self._update_reference_params() + self.bregma_estimate_header.object = self._bregma_header_html() + + def _refresh_compute_button(self): + """Label the Compute button by whether the selected atlas is local.""" + downloaded = anatomy_atlas.is_downloaded(self._selected_atlas_name()) + self.compute_anatomy_button.name = _BUTTON_COMPUTE if downloaded else _BUTTON_DOWNLOAD + + @contextlib.contextmanager + def _anatomy_recompute_suppressed(self): + """Batch field writes so they don't each request an overlay recompute.""" + self._suppress_anatomy_recompute = True + try: + yield finally: self._suppress_anatomy_recompute = False - # Reflect download state in the button label so users know before clicking. - name = str(self.atlas_name_input.value).strip() - if anatomy_atlas.is_downloaded(name): - self.compute_anatomy_button.name = "Compute anatomical overlay 🧠" - else: - self.compute_anatomy_button.name = "Download & compute atlas 🧠 ⏳" - self._recompute_anatomy_if_active() def _update_landmark_labels(self): """Rename the toggle + coordinate fields to the atlas's landmark. @@ -1614,55 +1717,27 @@ def _update_anatomy_origin_note(self, *events): """Refresh the coordinate note when the atlas selection changes.""" self.anatomy_coord_note.object = self._anatomy_coord_note_html() - def _atlas_is_downloaded(self) -> bool: - """True if the selected atlas is on disk, so reading it won't download.""" - try: - return anatomy_atlas.is_downloaded(str(self.atlas_name_input.value).strip()) - except Exception: # noqa: BLE001 - unreadable atlas means not downloaded - return False + def _selected_atlas_name(self) -> str: + return str(self.atlas_name_input.value).strip() - def _tip_depth_below_surface(self) -> float | None: - """Tip-to-brain-surface distance along the probe axis, in µm. - - ``None`` when it can't be computed: the atlas isn't downloaded (we - never trigger a download just to refresh a readout), the tip is outside - the brain, or the atlas lookup failed. - """ - if not self._atlas_is_downloaded(): - return None - try: - return tip_depth_below_surface_um( - self._tip_atlas_um(), - pitch_deg=float(self.pitch_input.value), - yaw_deg=float(self.yaw_input.value), - atlas_name=str(self.atlas_name_input.value).strip(), - ) - except Exception as exc: # noqa: BLE001 - a readout must never sink the GUI - print(f"Tip depth lookup failed: {exc}") - return None - - def _tip_depth_readout_html(self) -> str: + def _tip_depth_readout_html(self, depth: float | None) -> str: """Banner text for the depth-below-surface readout above the tip fields. Blank until an overlay exists: before Compute (or after Clear) there is no anatomy on screen for a depth to refer to, and a number sitting there would look like a live measurement of a pose nothing has been checked against. + + ``depth`` is measured by the off-loop overlay compute; this never reads + the atlas itself, so it is safe on the event loop. """ - if not getattr(self, "_anatomy_overlay_active", False): + if not self._anatomy_overlay_active: value_html = ( '—' ' ' "(compute the overlay below)" ) - elif not self._atlas_is_downloaded(): - value_html = ( - '—' - ' ' - "(available once the atlas is downloaded)" - ) else: - depth = self._tip_depth_below_surface() if depth is None: value_html = ( '—' @@ -1688,11 +1763,11 @@ def _tip_depth_readout_html(self) -> str: "" ) - def _update_tip_depth_readout(self, *events): + def _update_tip_depth_readout(self, depth: float | None = None): """Refresh the depth readout (cheap; skipped if the widget isn't built).""" if not hasattr(self, "tip_depth_readout"): return - self.tip_depth_readout.object = self._tip_depth_readout_html() + self.tip_depth_readout.object = self._tip_depth_readout_html(depth) def _atlas_tip_center(self, name: str): """Atlas center as ``(AP, ML, DV)`` µm, or ``None`` if not readable cheaply. @@ -1764,14 +1839,14 @@ def _origin_spec(self, name) -> dict: return spec # no skull landmark for this species → user defines origin spec["landmark"] = landmark if landmark == "anterior commissure": - # Human (AC-PC): derive it from the annotation when downloaded. - if not anatomy_atlas.is_downloaded(name): + # Human (AC-PC): derived from the annotation, but only ever off the + # event loop — it scans the whole volume (up to ~8 s). Until that + # has happened (see _locate_ac_origin and the overlay + # compute) report it as pending. + known, ac = anatomy_atlas.peek_origin_from_ac(name) + if not known: spec["status"] = "ac_pending" return spec - try: - ac = anatomy_atlas.derive_origin_from_ac(name) - except Exception: # noqa: BLE001 - AC not derivable: handled as ac is None below - ac = None if ac is None: # AC is the convention but this atlas doesn't delineate it # (e.g. the coarse human atlas) → keep the name, user sets it. @@ -1805,8 +1880,7 @@ def _on_bregma_toggle(self, *events): for w in (self.dv_squish_input, self.ap_squish_input, self.ml_squish_input, self.tilt_input): w.disabled = not on - self._suppress_anatomy_recompute = True - try: + with self._anatomy_recompute_suppressed(): if on: # Sensible bregma-relative default: at bregma, 4 mm deep. self.tip_ap_input.value = 0.0 @@ -1814,17 +1888,14 @@ def _on_bregma_toggle(self, *events): self.tip_dv_input.value = 4000.0 else: self._update_tip_to_atlas_center() - finally: - self._suppress_anatomy_recompute = False self._recompute_anatomy_if_active() def _recompute_anatomy_if_active(self, *events): """Re-run the overlay on input changes, but only once one exists.""" - if getattr(self, "_suppress_anatomy_recompute", False): + if self._suppress_anatomy_recompute: return - if getattr(self, "_anatomy_overlay_active", False): - # compute_anatomy_overlay refreshes the depth readout on its way out. - self.compute_anatomy_overlay() + if self._anatomy_overlay_active: + self._request_anatomy_recompute() # never inline: see _drain_anatomy_requests def _update_anatomy_legend(self, bands: list): """Render a deduplicated region legend on the right-side panel.""" @@ -2093,23 +2164,29 @@ 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") + self.box_select_tool = BoxSelectTool(description=select_box_string, icon=_asset_data_uri("selector.png")) self.box_deselect_tool = BoxSelectTool( - description=deselect_box_string, icon=GUI_ASSETS_DIR / "deselector.png" + description=deselect_box_string, icon=_asset_data_uri("deselector.png") ) self.box_zigzagselect_tool = BoxSelectTool( - description=zigzagselect_box_string, icon=GUI_ASSETS_DIR / "zigzag_selector.png" + description=zigzagselect_box_string, icon=_asset_data_uri("zigzag_selector.png") ) self.box_interleavedselect_tool = BoxSelectTool( - description=interleavedselect_box_string, icon=GUI_ASSETS_DIR / "interleaved_selector.png" + description=interleavedselect_box_string, icon=_asset_data_uri("interleaved_selector.png") ) self.box_dependent_deselect_tool = BoxSelectTool( - description=dependentdeselect_box_string, icon=GUI_ASSETS_DIR / "dependent_deselector.png" + description=dependentdeselect_box_string, icon=_asset_data_uri("dependent_deselector.png") + ) + + self.box_zigzag_dependent_deselect_tool = BoxSelectTool( + description=zigzagdependentdeselect_box_string, + icon=_asset_data_uri("zigzag_dependent_deselector.png"), ) # Always-on gesture tools — hidden from toolbar (visible=False). @@ -2143,6 +2220,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( @@ -2165,6 +2243,12 @@ def setup_bokeh_plot(self): self.setup_electrode_visualization() # Restrict hover tool to electrode rectangles only (prevents ??? on overlay glyphs) hover_tool.renderers = [self.electrode_renderer] + # Selection tools hit only what shares the electrode data. Left on "auto", + # a tap on a shared outline renderer selects that one outline and Bokeh + # dims all the others. + electrode_renderers = [self.electrode_renderer, self.survey_bar_renderer] + for tool in self.plot.select({"type": (TapTool, BoxSelectTool)}): + tool.renderers = electrode_renderers # Set initial y_range so only the bottom third of the probe is visible: # probe tips pinned at the bottom, top two-thirds hidden above the frame. @@ -2204,7 +2288,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 +2313,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 +2332,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 +2366,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 +2378,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 +2419,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") @@ -2667,10 +2772,6 @@ def create_widgets(self): self.atlas_name_input.param.watch(self._on_atlas_change, "value") self.bregma_relative_toggle.param.watch(self._on_bregma_toggle, "value") - # Once an overlay exists, live-update it (bands + slices) whenever any - # pose / bregma / squish / tilt input changes, so edits are visible - # immediately instead of only on the next Compute click. - self._anatomy_overlay_active = False # atlas_name + bregma toggle do their own batched recompute (via # _on_atlas_change / _on_bregma_toggle), so they're not in this list. for _w in (self.tip_ap_input, self.tip_ml_input, self.tip_dv_input, @@ -2694,7 +2795,7 @@ def create_widgets(self): margin=(0, 0), ) self.compute_anatomy_button = pn.widgets.Button( - name="Compute anatomical overlay 🧠", button_type="primary", + name=_BUTTON_COMPUTE, button_type="primary", sizing_mode="stretch_width", margin=(0, 0, 0, 0), ) self.clear_anatomy_button = pn.widgets.Button( @@ -2704,7 +2805,7 @@ def create_widgets(self): self.clear_anatomy_button.on_click(lambda event: self.clear_anatomy_overlay()) self.anatomy_legend = pn.pane.HTML(self._empty_legend_html(), sizing_mode="stretch_width") self.anatomy_locator = pn.pane.HTML("", sizing_mode="stretch_width") - # Reused across renders (see compute_anatomy_overlay); dropped when the + # Reused across renders (see _compute_anatomy); dropped when the # overlay is cleared so an idle session doesn't sit on a canvas. self._locator_fig = None # Hidden until the first overlay is computed; cleared by "Clear overlay". @@ -2714,8 +2815,7 @@ def create_widgets(self): ) # Set initial button label based on whether the default atlas is already cached. - if not anatomy_atlas.is_downloaded(atlas_default): - self.compute_anatomy_button.name = "Download & compute atlas 🧠 ⏳" + self._refresh_compute_button() # IMRO file dropper self.imro_file_loader = pn.widgets.FileInput(width=300) @@ -2824,10 +2924,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 +2958,7 @@ def _selection_tools_help_html(self) -> str: return ( '
' "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:
" '' @@ -2902,7 +3012,7 @@ def create_layout(self): You can mix and match four selection methods:
• Presets: Pre-configured channelmaps that respect wiring constraints
• Textual selection: Type electrode ranges (e.g., "1-10,20-25") to add to the current selection
- • Interactive: Click electrodes directly or drag boxes (selection, deselection, zigzag/interleaved selection, or "deselect dependents") to manually select multiple sites
+ • Interactive: Click electrodes directly or drag boxes (selection, deselection, zigzag/interleaved selection, "deselect dependents", or "zigzag-deselect dependents") to manually select multiple sites
• Selection from pre-existing IMRO file: you can pre-load an IMRO file as a starting point before doing any of the above.

Once you reach the target number of electrodes for the selected probe type (384 or 1536), you can download your channelmap 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.

@@ -3215,9 +3325,8 @@ def create_app(): """Create and configure the Panel app""" # Re-arm the extensions for *this* session. Panel keys `config` on the # current document, so the module-level `pn.extension` above only ever - # configured whichever session happened to trigger the import -- under - # `panel serve` that is session #1, and everyone after it got - # `pn.state.notifications is None`. Must stay inside the app factory. + # configured whatever happened to trigger the import, so each session arms + # its own. Must stay inside the app factory. # `exception_handler` is per-session for the same reason: it turns any # callback error into a toast + webhook report instead of a re-raise. pn.extension( diff --git a/pixelmap/utils/imro.py b/pixelmap/utils/imro.py index 5d9d3af..faedb8f 100644 --- a/pixelmap/utils/imro.py +++ b/pixelmap/utils/imro.py @@ -5,7 +5,6 @@ import numpy as np import pandas as pd -import panel as pn from pixelmap.backend import format_imro_string, get_electrodes from pixelmap.constants import ( @@ -192,23 +191,26 @@ def parse_imro_list(imro_list): hp_filter) +def _fail(message): + """Toast ``message`` in the GUI session, if any, then raise it.""" + # Imported here so `import pixelmap` doesn't pull in Panel and Bokeh. + from pixelmap.error_reporting import notify + + notify("error", message, duration=10_000) + raise AssertionError(message) + + def ref_id_to_string(reference_id, imro_fmt): ref_map = {v: k for k, v in REF_ELECTRODES[imro_fmt].items() if k != "Join Tips"} if reference_id not in ref_map: - error_message = f"Unexpected reference id {reference_id} for IMRO format {imro_fmt}!" - pn.state.notifications.error(error_message, - duration=10_000) - raise AssertionError(error_message) + _fail(f"Unexpected reference id {reference_id} for IMRO format {imro_fmt}!") return ref_map[reference_id] def check_entry_elements(n_expected_elements, entry, probe_type): n_entry_elements = len(entry) if n_entry_elements != n_expected_elements: - error_message = f"Corrupt .imro file - IMRO table entries for {probe_type} probes should have {n_expected_elements} elements, not {n_entry_elements}." - pn.state.notifications.error(error_message, - duration=10_000) - raise AssertionError(error_message) + _fail(f"Corrupt .imro file - IMRO table entries for {probe_type} probes should have {n_expected_elements} elements, not {n_entry_elements}.") def generate_imro_channelmap( diff --git a/pixelmap/utils/survey.py b/pixelmap/utils/survey.py index a5550d6..3b45f23 100644 --- a/pixelmap/utils/survey.py +++ b/pixelmap/utils/survey.py @@ -81,13 +81,11 @@ def parse_survey_file(content: str) -> pd.DataFrame: f"Expected header: Shank\\tXum\\tZum\\tVal." ) - df = df[list(SURVEY_COLUMNS)] - df = df.assign( - shank=pd.to_numeric(df["shank"], errors="raise"), - xum=pd.to_numeric(df["xum"], errors="raise"), - zum=pd.to_numeric(df["zum"], errors="raise"), - val=pd.to_numeric(df["val"], errors="raise"), - ).astype({"shank": int, "xum": int, "zum": int, "val": float}) + df = ( + df[list(SURVEY_COLUMNS)] + .apply(pd.to_numeric, errors="raise") + .astype({"shank": int, "xum": int, "zum": int, "val": float}) + ) return df diff --git a/scripts/warm_first_session.py b/scripts/warm_first_session.py new file mode 100644 index 0000000..d1e5911 --- /dev/null +++ b/scripts/warm_first_session.py @@ -0,0 +1,35 @@ +"""Pay the first session's one-time costs before the server accepts connections. + +``panel serve`` runs ``app.py`` per session on the Bokeh event loop, which also +serves every other session and ``/liveness``. Whatever is still cold when the +first visitor arrives (the imports, the wiring pickle, the default atlas's +metadata) would freeze that loop. + +``entrypoint.sh`` runs this through ``panel serve --setup``. Building one +throwaway GUI warms the one-time costs of a session's build, without a list here +to keep in step with the constructor. It has no document, so it creates no +session or analytics event. + +This runs before the port is bound only with ``NUM_PROCS=1``, the image's +default. Above that, Panel runs it on each process's event loop after the +server starts, which costs about what the first session would pay anyway. + +A failure only costs the speed-up, and the first session reports it properly, +so it must not stop the server: Panel aborts startup if this script raises. +""" + +import logging +import time + +_started = time.perf_counter() +try: + from pixelmap.gui.gui import ChannelmapGUI + + ChannelmapGUI() +except Exception: # see the module docstring + logging.getLogger("pixelmap.startup").warning( + "First-session warmup failed; the first visitor will pay for it", + exc_info=True, + ) +else: + print(f"First-session warmup finished in {time.perf_counter() - _started:.1f} s") diff --git a/tests/test_anatomy_atlas.py b/tests/test_anatomy_atlas.py index f6b0826..0ce71ce 100644 --- a/tests/test_anatomy_atlas.py +++ b/tests/test_anatomy_atlas.py @@ -187,6 +187,63 @@ def test_derive_origin_from_ac_none_without_ac(self, monkeypatch): atlas_module.derive_origin_from_ac.cache_clear() assert atlas_module.derive_origin_from_ac("y") is None + def test_peek_reports_unknown_until_derived(self, monkeypatch): + """The event loop asks ``peek``; only a worker thread pays for the scan.""" + calls = [] + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", + lambda name: calls.append(name) or (1.0, 2.0, 3.0)) + + assert atlas_module.peek_origin_from_ac("a") == (False, None) + assert calls == [], "peeking must never compute" + assert atlas_module.derive_origin_from_ac("a") == (1.0, 2.0, 3.0) + assert atlas_module.peek_origin_from_ac("a") == (True, (1.0, 2.0, 3.0)) + assert atlas_module.derive_origin_from_ac("a") == (1.0, 2.0, 3.0) + assert calls == ["a"], "a derived origin is memoised" + + def test_peek_distinguishes_no_ac_from_not_yet_derived(self, monkeypatch): + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", lambda name: None) + assert atlas_module.peek_origin_from_ac("b") == (False, None) + assert atlas_module.derive_origin_from_ac("b") is None + assert atlas_module.peek_origin_from_ac("b") == (True, None) + + def test_a_failed_derivation_is_not_memoised(self, monkeypatch): + def _boom(name): + raise OSError("annotation unreadable") + + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", _boom) + with pytest.raises(OSError): + atlas_module.derive_origin_from_ac("c") + assert atlas_module.peek_origin_from_ac("c") == (False, None) + + def test_concurrent_callers_share_one_scan(self, monkeypatch): + """Several sessions picking the same human atlas pay for one scan.""" + import threading + import time + + calls = [] + + def _slow(name): + calls.append(name) + time.sleep(0.05) + return (1.0, 2.0, 3.0) + + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", _slow) + results = [] + workers = [threading.Thread(target=lambda: results.append( + atlas_module.derive_origin_from_ac("e"))) for _ in range(4)] + for w in workers: + w.start() + for w in workers: + w.join() + assert calls == ["e"] + assert results == [(1.0, 2.0, 3.0)] * 4 + + def test_cache_clear_forgets_derived_origins(self, monkeypatch): + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", lambda name: (0.0, 0.0, 0.0)) + atlas_module.derive_origin_from_ac("d") + atlas_module.derive_origin_from_ac.cache_clear() + assert atlas_module.peek_origin_from_ac("d") == (False, None) + def test_lookup_resolves_through_ap_flip(self, monkeypatch): # "psr" reverses AP: a marker at the native posterior pole (index 0) must # be read at large canonical AP, not at AP=0 (the anterior pole). diff --git a/tests/test_anatomy_schematic.py b/tests/test_anatomy_schematic.py index c965770..4710dfa 100644 --- a/tests/test_anatomy_schematic.py +++ b/tests/test_anatomy_schematic.py @@ -93,6 +93,56 @@ def test_region_fill_uses_atlas_colors(fake_brainglobe): assert rgba[0, 1, 3] > 0 and rgba[0, 2, 3] > 0 +def _region_fill_rgba_reference(label_img, atlas): + """The original one-mask-per-region implementation, kept as an oracle.""" + rgba = np.zeros((*label_img.shape, 4), dtype=float) + for region_id in np.unique(label_img): + if region_id == 0: + continue + r, g, b = schematic_module._region_rgb(atlas, int(region_id)) + mask = label_img == region_id + rgba[mask, :3] = (r / 255.0, g / 255.0, b / 255.0) + rgba[mask, 3] = schematic_module._REGION_ALPHA + return rgba + + +class TestRegionFillMatchesThePerRegionLoop: + """The fill is vectorised: it used to build one full-slice mask per region, + which was 2.4 s of every 3.5 s live update on allen_mouse_10um.""" + + def test_unknown_region_ids_fall_back_to_gray(self, fake_brainglobe): + atlas = _FakeAtlas("fake") + rgba = schematic_module._region_fill_rgba(np.array([[0, 99]], dtype=np.int32), atlas) + np.testing.assert_allclose(rgba[0, 1, :3], (200 / 255,) * 3) + assert rgba[0, 1, 3] == schematic_module._REGION_ALPHA + + def test_an_all_outside_slice_is_fully_transparent(self, fake_brainglobe): + rgba = schematic_module._region_fill_rgba(np.zeros((3, 4), dtype=np.int32), + _FakeAtlas("fake")) + assert rgba.shape == (3, 4, 4) + assert not rgba.any() + + def test_an_empty_slice_gives_an_empty_image(self, fake_brainglobe): + rgba = schematic_module._region_fill_rgba(np.zeros((0, 5), dtype=np.int32), + _FakeAtlas("fake")) + assert rgba.shape == (0, 5, 4) + + @pytest.mark.parametrize("dtype", [np.int32, np.uint32, np.int64, np.uint16]) + def test_many_regions_match_the_reference(self, fake_brainglobe, dtype): + atlas = _FakeAtlas("fake") + rng = np.random.default_rng(0) + ids = np.array([0, 1, 2, 7, 500, 60_000], dtype=dtype) # 7, 500, 60k: unknown + label_img = rng.choice(ids, size=(40, 30)).astype(dtype) + atlas.structures = { + **atlas.structures, + 500: {"acronym": "X", "name": "X", "rgb_triplet": [10, 20, 30]}, + } + np.testing.assert_array_equal( + schematic_module._region_fill_rgba(label_img, atlas), + _region_fill_rgba_reference(label_img, atlas), + ) + + def test_outline_marks_region_boundaries(fake_brainglobe): label_img = np.array([[1, 1, 2, 2]], dtype=np.int32) outline = schematic_module._outline_rgba(label_img) diff --git a/tests/test_gui_overlays.py b/tests/test_gui_overlays.py index eae78dc..4d6c458 100644 --- a/tests/test_gui_overlays.py +++ b/tests/test_gui_overlays.py @@ -22,6 +22,7 @@ from pixelmap.anatomy import atlas as atlas_module from pixelmap.anatomy import regions as regions_module +from pixelmap.gui import gui as gui_module from pixelmap.gui.gui import ChannelmapGUI from pixelmap.types import Electrode @@ -89,6 +90,7 @@ def __init__(self, name, **_kwargs): atlas_module.release_atlas_memory() atlas_module.get_atlas.cache_clear() atlas_module.canonical_annotation.cache_clear() + atlas_module.derive_origin_from_ac.cache_clear() atlas_module._region_info_from_id.cache_clear() monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _FakeAtlas) # Pretend it's cached so the GUI treats it as a local atlas throughout. @@ -97,6 +99,7 @@ def __init__(self, name, **_kwargs): atlas_module.release_atlas_memory() atlas_module.get_atlas.cache_clear() atlas_module.canonical_annotation.cache_clear() + atlas_module.derive_origin_from_ac.cache_clear() atlas_module._region_info_from_id.cache_clear() @@ -361,6 +364,16 @@ def test_clearing_wipes_every_anatomy_layer(self, posed_gui): assert "No anatomical overlay computed yet" in posed_gui.anatomy_legend.object +def _measured_depth(gui): + """The depth the overlay compute would measure for ``gui``'s current pose.""" + return gui_module._measure_tip_depth( + gui._tip_atlas_um(), + float(gui.pitch_input.value), + float(gui.yaw_input.value), + gui._selected_atlas_name(), + ) + + class TestTipDepthReadout: """The readout is tied to the overlay: blank until Compute, blank after Clear.""" @@ -393,38 +406,42 @@ def test_readout_follows_the_tip_once_an_overlay_exists(self, posed_gui): posed_gui.compute_anatomy_overlay() posed_gui.tip_dv_input.value = 2000.0 # 1 mm below the fake surface assert "1,0" in posed_gui.tip_depth_readout.object - assert posed_gui._tip_depth_below_surface() == pytest.approx(1012.5, abs=25.0) + assert _measured_depth(posed_gui) == pytest.approx(1012.5, abs=25.0) def test_tilting_the_probe_lengthens_the_measured_path(self, posed_gui): posed_gui.compute_anatomy_overlay() - vertical = posed_gui._tip_depth_below_surface() + vertical = _measured_depth(posed_gui) posed_gui.pitch_input.value = 30.0 - tilted = posed_gui._tip_depth_below_surface() + tilted = _measured_depth(posed_gui) assert tilted > vertical assert tilted == pytest.approx(vertical / np.cos(np.deg2rad(30.0)), abs=30.0) def test_tip_above_the_brain_is_reported_as_outside(self, posed_gui): posed_gui.compute_anatomy_overlay() posed_gui.tip_dv_input.value = 100.0 - assert posed_gui._tip_depth_below_surface() is None + assert _measured_depth(posed_gui) is None assert "outside the brain" in posed_gui.tip_depth_readout.object - def test_undownloaded_atlas_is_reported_without_triggering_a_download( - self, posed_gui, monkeypatch + @pytest.mark.parametrize("depth", [None, 0.0, 1012.5]) + def test_refreshing_the_readout_never_reads_the_atlas( + self, posed_gui, monkeypatch, depth ): - """Refreshing a readout must never kick off a tens-of-MB download.""" + """The readout runs on the event loop, so it must only format a depth + the off-loop compute already measured — never touch the atlas, which + could mean a tens-of-MB download.""" posed_gui.compute_anatomy_overlay() - def _boom(name): - raise AssertionError("must not read an atlas that isn't downloaded") + def _boom(*args, **kwargs): + raise AssertionError("the readout read the atlas") - monkeypatch.setattr(atlas_module, "is_downloaded", lambda name: False) + monkeypatch.setattr(atlas_module, "is_downloaded", _boom) # regions.py imports the symbol directly, so patch it where it's used. monkeypatch.setattr(regions_module, "canonical_annotation", _boom) - posed_gui._update_tip_depth_readout() + posed_gui._update_tip_depth_readout(depth) - assert "once the atlas is downloaded" in posed_gui.tip_depth_readout.object + expected = "outside the brain" if depth is None else f"{depth:,.0f} µm" + assert expected in posed_gui.tip_depth_readout.object class TestBothOverlaysTogether: @@ -683,6 +700,294 @@ def _boom(name): assert gui.compute_anatomy_button.name == "Download & compute atlas 🧠 ⏳" +class TestAtlasSwitchNeverDownloadsOnTheEventLoop: + """Regression tests for a server-wide freeze on atlas switch. + + With an overlay on screen, picking a not-yet-cached atlas from the dropdown + live-recomputed the overlay *synchronously* — so the multi-second atlas + download ran on the Bokeh event loop, freezing every session and the + healthcheck (the watchdog logged 5–10 s stalls in + ``_on_atlas_change → _recompute_anatomy_if_active → compute_anatomy_overlay``). + The Compute button already pushed downloads to a worker thread; the live + update path has to do the same. + """ + + COLD = "cold_gui_atlas" + COLDER = "colder_gui_atlas" + + @pytest.fixture + def traced(self, posed_gui, monkeypatch): + """Record every atlas construction and download, with its thread. + + Atlases in ``cold`` count as not downloaded until ``ensure_downloaded`` + has fetched them; constructing one before then is exactly the inline + network fetch that froze the server. + """ + events: list[tuple[str, str, threading.Thread]] = [] + cold = {self.COLD, self.COLDER} + fake_atlas = atlas_module.BrainGlobeAtlas + + class _TracingAtlas(fake_atlas): + def __init__(self, name, **kwargs): + events.append(("construct", name, threading.current_thread())) + super().__init__(name, **kwargs) + + def _fake_download(name): + events.append(("download", name, threading.current_thread())) + cold.discard(name) + + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", _TracingAtlas) + monkeypatch.setattr(atlas_module, "is_downloaded", lambda name=FAKE_ATLAS: name not in cold) + monkeypatch.setattr(atlas_module, "ensure_downloaded", _fake_download) + return posed_gui, events, cold + + def _assert_downloaded_off_loop_before_use(self, events, name): + mine = [(kind, thread) for kind, n, thread in events if n == name] + assert mine, f"{name} was never downloaded or opened" + kind, thread = mine[0] + assert kind == "download", ( + f"{name} was opened before being downloaded — on a cold cache that " + "is an inline network fetch" + ) + assert thread is not threading.main_thread(), ( + f"{name} was downloaded on the event loop thread" + ) + + def test_switching_to_a_cold_atlas_downloads_off_the_event_loop(self, traced): + gui, events, _cold = traced + gui.compute_anatomy_overlay() + assert gui._anatomy_overlay_active is True + + gui.atlas_name_input.value = self.COLD + + self._assert_downloaded_off_loop_before_use(events, self.COLD) + # And the overlay is redrawn for the new atlas once it has arrived. + assert gui._anatomy_overlay_active is True + assert len(gui.region_band_source.data["acronym"]) > 0 + assert gui.compute_anatomy_button.disabled is False + assert gui.compute_anatomy_button.name == "Compute anatomical overlay 🧠" + + def test_switching_again_mid_download_fetches_the_latest_off_the_loop(self, traced, monkeypatch): + """The atlas can change while the first download is still running. + + The draw that follows a download reads the *current* dropdown value, so + it must not assume that is the atlas it just fetched. + """ + gui, events, cold = traced + gui.compute_anatomy_overlay() + + def _download_then_switch(name): + events.append(("download", name, threading.current_thread())) + cold.discard(name) + if name == self.COLD: + gui.atlas_name_input.value = self.COLDER # user picks again + + monkeypatch.setattr(atlas_module, "ensure_downloaded", _download_then_switch) + + gui.atlas_name_input.value = self.COLD + + downloads = [n for kind, n, _t in events if kind == "download"] + assert downloads == [self.COLD, self.COLDER], ( + "each atlas should be fetched exactly once, the second by the " + "in-flight download rather than a parallel or inline one" + ) + self._assert_downloaded_off_loop_before_use(events, self.COLDER) + assert len(gui.region_band_source.data["acronym"]) > 0 + assert gui.compute_anatomy_button.disabled is False + + def test_a_failed_download_on_switch_leaves_the_button_usable(self, traced, monkeypatch): + gui, events, _cold = traced + gui.compute_anatomy_overlay() + + def _boom(name): + raise ConnectionError("atlas host unreachable") + + monkeypatch.setattr(atlas_module, "ensure_downloaded", _boom) + + gui.atlas_name_input.value = self.COLD + + assert not [e for e in events if e[1] == self.COLD and e[2] is threading.main_thread()] + assert gui.compute_anatomy_button.disabled is False + assert gui.compute_anatomy_button.name == "Download & compute atlas 🧠 ⏳" + + def test_switching_to_a_cold_atlas_without_an_overlay_downloads_nothing(self, traced): + gui, events, _cold = traced + assert gui._anatomy_overlay_active is False + + gui.atlas_name_input.value = self.COLD + + assert not [e for e in events if e[1] == self.COLD] + assert gui.compute_anatomy_button.name == "Download & compute atlas 🧠 ⏳" + + def test_switching_to_a_warm_atlas_still_recomputes_immediately(self, traced): + gui, events, cold = traced + gui.compute_anatomy_overlay() + cold.discard(self.COLD) # already on disk + gui.region_band_source.data = {k: [] for k in gui.region_band_source.data} + + gui.atlas_name_input.value = self.COLD + + assert not [e for e in events if e[0] == "download"] + assert len(gui.region_band_source.data["acronym"]) > 0 + + +def _assert_off_loop(threads, what: str): + """``what`` ran, and never on the main (event loop) thread.""" + assert threads, f"{what} never ran" + assert all(t is not threading.main_thread() for t in threads), ( + f"{what} ran on the event loop thread" + ) + + +class TestOverlayComputeRunsOffTheEventLoop: + """Regression tests: even a *cached* atlas froze the server on every edit. + + Slicing the annotation and rendering the locator takes 0.7 s per overlay + compute on allen_mouse_25um and 3.3 s on allen_mouse_10um (measured), and + the overlay recomputes on every pose keystroke — all of it on the Bokeh + event loop that serves every session. Locating an anterior-commissure + origin scans the whole volume (0.8–8 s), and ran on the loop on every + switch to a human or fish atlas. All of these must happen in a worker. + """ + + @pytest.fixture + def threads(self, monkeypatch): + """Record which thread each atlas-reading step of the overlay runs on.""" + seen: dict[str, list[threading.Thread]] = {} + + def _tracing(label, fn): + def wrapper(*args, **kwargs): + seen.setdefault(label, []).append(threading.current_thread()) + return fn(*args, **kwargs) + return wrapper + + for label in ("compute_region_bands", "render_locator", "tip_depth_below_surface_um"): + monkeypatch.setattr(gui_module, label, _tracing(label, getattr(gui_module, label))) + return seen + + def test_a_live_pose_edit_computes_off_the_event_loop(self, posed_gui, threads): + posed_gui.compute_anatomy_overlay() + before = list(posed_gui.region_band_source.data["y"]) + threads.clear() + + posed_gui.tip_dv_input.value = 2200.0 + + for label in ("compute_region_bands", "render_locator", "tip_depth_below_surface_um"): + _assert_off_loop(threads.get(label), label) + assert list(posed_gui.region_band_source.data["y"]) != before + # 2200 µm tip, surface at ≈ 987.5 µm → ≈ 1212 µm below it. + assert "1,2" in posed_gui.tip_depth_readout.object + + def test_clicking_compute_on_a_cached_atlas_computes_off_the_loop(self, served, threads): + gui, session = served + + asyncio.run(session.with_document_locked(gui._on_compute_anatomy_click, None)) + + for label in ("compute_region_bands", "render_locator"): + _assert_off_loop(threads.get(label), label) + assert len(gui.region_band_source.data["acronym"]) > 0 + assert gui.compute_anatomy_button.disabled is False + + def test_edits_during_a_compute_are_coalesced_into_one_more(self, posed_gui, monkeypatch): + """A burst of keystrokes must not queue one multi-second compute each.""" + posed_gui.compute_anatomy_overlay() + real = gui_module.compute_region_bands + tips = [] + + def _slow_bands(**kwargs): + tips.append(kwargs["tip_atlas"][2]) + if len(tips) == 1: # the user keeps typing while this one runs + for dv in (2100.0, 2200.0, 2300.0): + posed_gui.tip_dv_input.value = dv + return real(**kwargs) + + monkeypatch.setattr(gui_module, "compute_region_bands", _slow_bands) + + posed_gui.tip_dv_input.value = 2000.0 + + assert tips == [2000.0, 2300.0], "expected the first edit, then only the latest" + assert posed_gui._anatomy_compute_running is False + + def test_clearing_mid_compute_discards_the_result(self, posed_gui, monkeypatch): + posed_gui.compute_anatomy_overlay() + real = gui_module.compute_region_bands + + def _cleared_meanwhile(**kwargs): + bands = real(**kwargs) + posed_gui.clear_anatomy_overlay() # the user clicks Clear + return bands + + monkeypatch.setattr(gui_module, "compute_region_bands", _cleared_meanwhile) + + posed_gui.tip_dv_input.value = 2200.0 + + assert posed_gui._anatomy_overlay_active is False + assert posed_gui.region_band_source.data["x"] == [] + assert posed_gui.anatomy_locator.object == "" + + def test_a_lookup_failure_leaves_the_button_usable(self, posed_gui, monkeypatch): + posed_gui.compute_anatomy_overlay() + + def _boom(**kwargs): + raise ValueError("bad pose") + + monkeypatch.setattr(gui_module, "compute_region_bands", _boom) + + posed_gui.tip_dv_input.value = 2200.0 + + assert posed_gui._anatomy_compute_running is False + assert posed_gui.compute_anatomy_button.disabled is False + assert posed_gui.compute_anatomy_button.name == "Compute anatomical overlay 🧠" + + +class TestAnteriorCommissureOriginIsLocatedOffTheEventLoop: + """Human and fish atlases use the AC as origin; finding it scans every voxel.""" + + AC_ATLAS = "fake_zfish_atlas" # "zfish" → anterior-commissure landmark + AC_ORIGIN = (100.0, 200.0, 300.0) # (AP, ML, DV) µm + + @pytest.fixture + def ac_threads(self, posed_gui, monkeypatch): + seen: list[threading.Thread] = [] + + def _derive(name): + seen.append(threading.current_thread()) + return self.AC_ORIGIN + + monkeypatch.setattr(atlas_module, "_derive_origin_from_ac", _derive) + return posed_gui, seen + + def _assert_origin_filled(self, gui): + assert (gui.bregma_ap_input.value, gui.bregma_ml_input.value, + gui.bregma_dv_input.value) == self.AC_ORIGIN + assert "anterior commissure (defined)" in gui.bregma_estimate_header.object + + def test_switching_to_a_cached_ac_atlas_derives_it_off_the_loop(self, ac_threads): + gui, seen = ac_threads + assert gui._anatomy_overlay_active is False + + gui.atlas_name_input.value = self.AC_ATLAS + + _assert_off_loop(seen, "the whole-volume AC scan") + self._assert_origin_filled(gui) + + def test_with_an_overlay_the_recompute_derives_it_off_the_loop(self, ac_threads): + gui, seen = ac_threads + gui.compute_anatomy_overlay() + + gui.atlas_name_input.value = self.AC_ATLAS + + _assert_off_loop(seen, "the whole-volume AC scan") + self._assert_origin_filled(gui) + assert len(gui.region_band_source.data["acronym"]) > 0 + + def test_origin_spec_never_scans_on_its_own(self, ac_threads): + """It runs on the loop several times per switch, so it may only peek.""" + gui, seen = ac_threads + assert gui._origin_spec(self.AC_ATLAS)["status"] == "ac_pending" + assert seen == [] + + def test_no_unlocked_async_gui_callbacks(): """Every async GUI callback needs the same treatment. diff --git a/tests/test_pdf_export.py b/tests/test_pdf_export.py index a256670..8d15a96 100644 --- a/tests/test_pdf_export.py +++ b/tests/test_pdf_export.py @@ -16,8 +16,9 @@ matplotlib.use("agg") import matplotlib.pyplot as plt +import numpy as np import pytest -from matplotlib.collections import PatchCollection +from matplotlib.collections import PolyCollection from pixelmap import backend from pixelmap.gui.gui import ChannelmapGUI @@ -92,6 +93,25 @@ def test_legend_matches_previous_best_position(gui, probe_type): assert legend.get_window_extent().bounds == pytest.approx(pinned) +def _rgba(c): + return tuple(round(float(v), 6) for v in c) + + +SELECTED_RGBA = _rgba(matplotlib.colors.to_rgba("red", 1.0)) +UNSELECTED_RGBA = _rgba(matplotlib.colors.to_rgba("lightgrey", 0.7)) +FORBIDDEN_RGBA = _rgba(matplotlib.colors.to_rgba("black", 1.0)) +SURVEY_BAR_ZORDER = 50 + + +def _electrode_collections(ax): + """Per-shank electrode PolyCollections, in shank order (survey bars excluded).""" + return [ + c + for c in ax.collections + if isinstance(c, PolyCollection) and c.get_zorder() != SURVEY_BAR_ZORDER + ] + + @pytest.mark.parametrize("probe_type", PROBE_TYPES) @pytest.mark.parametrize("with_survey", [False, True]) def test_electrodes_drawn_as_collections(gui, probe_type, with_survey): @@ -104,34 +124,72 @@ def test_electrodes_drawn_as_collections(gui, probe_type, with_survey): kwargs = {"survey_colors": {(0, 0): "#ff0000"}, "survey_range": (0.0, 1.0)} ax = _plot(gui, **kwargs) - collections = [c for c in ax.collections if isinstance(c, PatchCollection)] + collections = [c for c in ax.collections if isinstance(c, PolyCollection)] per_shank = 2 if with_survey else 1 assert len(collections) == n_shanks * per_shank # Only shank outlines / anatomy bands remain as standalone patches. assert len(ax.patches) < 50 # Every electrode is still drawn, exactly once. - drawn = sum(len(c.get_paths()) for c in collections if c.get_zorder() != 50) + drawn = sum(len(c.get_paths()) for c in _electrode_collections(ax)) assert drawn == len(gui.positions_df) def test_selected_electrodes_coloured(gui): """Selected electrodes keep the opaque red facecolour inside the collection.""" _select_preset(gui, "2.0-1shank") + (electrodes,) = _electrode_collections(_plot(gui)) + + colours = [_rgba(c) for c in electrodes.get_facecolor()] + assert set(colours) <= {SELECTED_RGBA, UNSELECTED_RGBA, FORBIDDEN_RGBA} + assert colours.count(SELECTED_RGBA) == len(gui.imro_list) - 1 # minus header row + + +@pytest.mark.parametrize("probe_type", ["2.0-4shanks", "QuadBase"]) +def test_multishank_forbidden_matches_shank_electrode_pairs(gui, probe_type, monkeypatch): + """An electrode is black only if its (shank, electrode) pair is forbidden. + + Shank and electrode IDs used to be matched independently, so an electrode ID + forbidden on one shank was also drawn black on every other shank that had + any forbidden electrode. + """ + _select_preset(gui, probe_type) + selected = np.array([[0, 0], [1, 1]]) + monkeypatch.setattr(backend, "find_selected_electrodes", lambda _imro: selected) + forbidden = backend.find_forbidden_electrodes(selected, gui.wiring_df) + forbidden_pairs = set(map(tuple, forbidden.tolist())) - set(map(tuple, selected.tolist())) + ax = _plot(gui) - (electrodes,) = [c for c in ax.collections if isinstance(c, PatchCollection)] + positions = gui.positions_df.values + drawn_selected, drawn_forbidden = set(), set() + for shank_id, coll in enumerate(_electrode_collections(ax)): + shank_positions = positions[positions[:, 0] == shank_id] + colours = [_rgba(c) for c in coll.get_facecolor()] + assert len(colours) == len(shank_positions) + for (s, e, *_), colour in zip(shank_positions, colours): + if colour == SELECTED_RGBA: + drawn_selected.add((int(s), int(e))) + elif colour == FORBIDDEN_RGBA: + drawn_forbidden.add((int(s), int(e))) + + assert drawn_selected == {(0, 0), (1, 1)} + assert drawn_forbidden == forbidden_pairs + + +@pytest.mark.parametrize("probe_type", ["2.0-1shank", "2.0-4shanks"]) +def test_no_forbidden_electrodes(gui, probe_type, monkeypatch): + """With no wiring conflicts, both branches draw without any black electrode.""" + _select_preset(gui, probe_type) + monkeypatch.setattr( + backend, "find_forbidden_electrodes", lambda *_a: np.empty((0, 2), dtype=int) + ) - def rgba(c): - return tuple(round(float(v), 6) for v in c) + ax = _plot(gui) - colours = [rgba(c) for c in electrodes.get_facecolor()] - selected = rgba(matplotlib.colors.to_rgba("red", 1.0)) - unselected = rgba(matplotlib.colors.to_rgba("lightgrey", 0.7)) - forbidden = rgba(matplotlib.colors.to_rgba("black", 1.0)) - assert set(colours) <= {selected, unselected, forbidden} - assert colours.count(selected) == len(gui.imro_list) - 1 # minus header row + colours = {_rgba(c) for coll in _electrode_collections(ax) for c in coll.get_facecolor()} + assert FORBIDDEN_RGBA not in colours def test_add_rects_empty_is_noop(): _fig, ax = plt.subplots() - backend._add_rects(ax, [], []) + backend._add_rects(ax, [], [], 1.0, 1.0, []) assert len(ax.collections) == 0 diff --git a/tests/test_public_api.py b/tests/test_public_api.py index f8770b0..e94804c 100644 --- a/tests/test_public_api.py +++ b/tests/test_public_api.py @@ -67,6 +67,7 @@ "pickle", "Path", "PatchCollection", + "PolyCollection", "pn", ] @@ -202,3 +203,13 @@ def test_documented_workflow_round_trips(tmp_path, probe_type, preset): assert parsed_probe_type == probe_type assert len(selected) == 384 assert len(np.unique(selected, axis=0)) == 384 + + +def test_importing_the_package_does_not_load_the_gui_stack(): + """Script/API users shouldn't pay ~0.7 s to import Panel and Bokeh.""" + import subprocess + import sys + + code = "import sys, pixelmap; print(sorted({'panel', 'bokeh'} & set(sys.modules)))" + out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, check=True) + assert out.stdout.strip() == "[]" diff --git a/tests/test_session_build_cost.py b/tests/test_session_build_cost.py new file mode 100644 index 0000000..2f30047 --- /dev/null +++ b/tests/test_session_build_cost.py @@ -0,0 +1,147 @@ +"""Keep a session's Bokeh document small: every session pays for it on the event loop. + +``panel serve`` builds, validates, serialises and sends each session's document +on the loop that serves every other session and ``/liveness``, and all of that +scales with the number of models and the size of their data. +""" + +from __future__ import annotations + +import numpy as np +import pytest +from bokeh.models import BoxSelectTool, GlyphRenderer, MultiLine, Rect, TapTool, Text + +from pixelmap.constants import WIRING_FILE_MAP +from pixelmap.gui.gui import ChannelmapGUI, shank_outline_geometry +from pixelmap.types import Electrode + +NUMERIC_COLUMNS = ( + "x", "y", "width", "height", "shank_id", "electrode_id", "alpha", "line_width", + "val", "bar_x", "bar_height", "bar_alpha", +) +SINGLE_SHANK = np.array([[0, e, 0, y] for e, y in enumerate(range(0, 7680, 10))], dtype=float) + + +@pytest.fixture(scope="module", params=sorted(WIRING_FILE_MAP)) +def probe_gui(request): + gui = ChannelmapGUI() + gui.probe_type = request.param + gui.load_probe_data() + gui.setup_bokeh_plot() + return gui + + +def _glyph_counts(gui): + renderers = gui.plot.select({"type": GlyphRenderer}) + return { + kind: sum(isinstance(r.glyph, kind) for r in renderers) + for kind in (Rect, MultiLine, Text) + } + + +def test_outlines_use_one_renderer_per_style_on_every_probe(probe_gui): + counts = _glyph_counts(probe_gui) + assert counts[MultiLine] == 2 # outlines, bank lines + # Bank labels, the anatomy region labels and, on multi-shank probes, shank labels. + assert counts[Text] <= 3 + + +@pytest.mark.parametrize("tool_type", [TapTool, BoxSelectTool]) +def test_selection_tools_only_hit_electrodes_never_decoration(probe_gui, tool_type): + # A shared outline renderer is hit-tested per item, so a tap on one outline + # would "select" it and Bokeh would dim every other outline. + tools = probe_gui.plot.select({"type": tool_type}) + assert tools + for tool in tools: + assert set(tool.renderers) == {probe_gui.electrode_renderer, probe_gui.survey_bar_renderer} + + +def test_numeric_electrode_columns_are_arrays_for_binary_encoding(probe_gui): + for column in NUMERIC_COLUMNS: + assert isinstance(probe_gui.electrode_source.data[column], np.ndarray), column + + +def test_electrode_columns_keep_one_value_per_electrode(probe_gui): + lengths = {len(v) for v in probe_gui.electrode_source.data.values()} + assert lengths == {len(probe_gui.positions_df)} + + +def test_tool_icons_reuse_the_cached_data_uri(probe_gui): + icons = [probe_gui.box_select_tool.icon, probe_gui.box_deselect_tool.icon] + assert all(icon.startswith("data:image/png;base64,") for icon in icons) + + +@pytest.fixture +def default_gui(): + gui = ChannelmapGUI() + yield gui + gui.electrodes.clear_selection() + + +def test_rebuilding_keeps_the_current_selection_and_survey(default_gui): + gui = default_gui + first = Electrode(*gui.positions_df[["shank", "electrode"]].iloc[0].tolist()) + gui.electrodes.selected.add(first) + gui.survey_values = {first: 0.0} # zero is a real value, not "missing" + + gui.create_electrode_data() + + data = gui.electrode_source.data + assert (data["status"][0], data["color"][0], data["alpha"][0]) == ("Selected", "red", 1.0) + assert data["val"][0] == 0.0 + assert data["status"][1] == "Unselected" + assert np.isnan(data["val"][1]) # no survey value for this electrode + gui.survey_values = None + + +def test_updating_colors_matches_a_full_rebuild(default_gui): + gui = default_gui + gui.electrodes.selected.add(Electrode(*gui.positions_df[["shank", "electrode"]].iloc[5].tolist())) + + gui.update_electrode_colors() + updated = dict(gui.electrode_source.data) + gui.create_electrode_data() + + assert updated["status"][5] == "Selected" + for column, values in gui.electrode_source.data.items(): + np.testing.assert_array_equal(updated[column], values, err_msg=column) + + +def test_single_shank_geometry_matches_the_drawn_probe(): + geometry = shank_outline_geometry(SINGLE_SHANK, single_shank=True) + + assert geometry.outline_xs == [[-50, 50, 50, 0, -50, -50]] + assert geometry.outline_ys[0][:3] == [7770.0, 7770.0, 0.0] + assert geometry.outline_ys[0][3] == pytest.approx(-767.0) + assert geometry.shank_labels == ([], [], []) + assert geometry.bank_labels[2] == ["Bank 0", "Bank 1"] # banks start at 0 and 384 + assert geometry.bank_lines_ys == [[0.0, 0.0], [3840.0, 3840.0]] + + +def test_multi_shank_geometry_draws_every_shank_and_labels_banks_on_the_last(): + positions = np.vstack([SINGLE_SHANK + [shank, 0, 0, 0] for shank in range(4)]) + + geometry = shank_outline_geometry(positions, single_shank=False) + + assert len(geometry.outline_xs) == 4 + assert geometry.outline_xs[3][3] == 750 # tip of the rightmost shank + assert geometry.shank_labels[2] == ["Shank 0", "Shank 1", "Shank 2", "Shank 3"] + assert len(geometry.bank_lines_xs) == 8 # two banks on each shank + assert set(geometry.bank_labels[0]) == {817} # right of the rightmost shank + + +@pytest.mark.parametrize("single_shank", [True, False]) +def test_geometry_of_a_probe_shorter_than_one_bank_has_one_bank_line(single_shank): + positions = np.array([[s, e, 0, e * 20.0] for s in range(4) for e in range(3)]) + if single_shank: + positions = positions[positions[:, 0] == 0] + + geometry = shank_outline_geometry(positions, single_shank=single_shank) + + assert len(geometry.bank_lines_xs) == (1 if single_shank else 4) + assert geometry.bank_labels[2] == ["Bank 0"] + + +def test_geometry_of_a_single_electrode_has_a_flat_tip(): + geometry = shank_outline_geometry(np.array([[0, 0, 0, 0.0]]), single_shank=True) + assert geometry.outline_ys[0][2:4] == [0.0, 0.0] diff --git a/tests/test_startup_warmup.py b/tests/test_startup_warmup.py new file mode 100644 index 0000000..f10a129 --- /dev/null +++ b/tests/test_startup_warmup.py @@ -0,0 +1,104 @@ +"""Tests for scripts/warm_first_session.py, which explains why the warmup exists.""" + +from __future__ import annotations + +import logging +import re +import runpy +import subprocess +import sys +from pathlib import Path +from unittest.mock import MagicMock, Mock + +import panel as pn + +from pixelmap.anatomy import atlas as atlas_module +from pixelmap.gui import gui as gui_module + +ROOT = Path(__file__).resolve().parent.parent +ENTRYPOINT = ROOT / "entrypoint.sh" +SCRIPT = ROOT / "scripts" / "warm_first_session.py" +DEFAULT_ATLAS = "allen_mouse_25um" + + +def _run_warmup(caplog): + """Run the script as Panel would; return its namespace and logged failures.""" + with caplog.at_level(logging.WARNING, logger="pixelmap.startup"): + namespace = runpy.run_path(str(SCRIPT)) # Panel aborts startup if this raises + return namespace, [r for r in caplog.records if r.name == "pixelmap.startup"] + + +def test_entrypoint_runs_the_warmup_before_serving(): + entrypoint = ENTRYPOINT.read_text() + match = re.search(r"--setup\s+(\./\S+)", entrypoint) + assert match, "entrypoint.sh must pass --setup to panel serve" + assert (ROOT / match.group(1)).resolve() == SCRIPT + + +def test_warmup_fills_the_wiring_cache_in_a_fresh_process(): + probe = ( + "import runpy\n" + f"runpy.run_path({str(SCRIPT)!r})\n" + "from pixelmap import backend\n" + "assert backend._load_wiring_maps.cache_info().currsize == 1\n" + ) + done = subprocess.run( + [sys.executable, "-c", probe], capture_output=True, text=True, cwd=ROOT, timeout=300, check=False + ) + assert done.returncode == 0, done.stderr + + +def test_warmup_builds_one_gui_and_keeps_no_session_state(monkeypatch, caplog): + real = gui_module.ChannelmapGUI + spy = Mock(wraps=real) + monkeypatch.setattr(gui_module, "ChannelmapGUI", spy) + views_before = set(pn.state._views) + + namespace, failures = _run_warmup(caplog) + + spy.assert_called_once() + assert failures == [] # the script swallows errors, so the spy alone proves nothing + assert pn.state.curdoc is None + assert set(pn.state._views) == views_before + assert not any(isinstance(value, real) for value in namespace.values()) + + +def test_a_failing_warmup_does_not_stop_the_server(monkeypatch, caplog): + monkeypatch.setattr(gui_module, "ChannelmapGUI", Mock(side_effect=RuntimeError("no wiring"))) + _, failures = _run_warmup(caplog) + assert [r for r in failures if r.exc_info] + + +def test_a_failing_import_does_not_stop_the_server(monkeypatch, caplog): + # None in sys.modules makes `import pixelmap.gui.gui` raise ImportError. + monkeypatch.setitem(sys.modules, "pixelmap.gui.gui", None) + _, failures = _run_warmup(caplog) + assert [r for r in failures if r.exc_info] + + +def test_warmup_opens_the_default_atlas_when_it_is_on_disk(monkeypatch, caplog): + fake_atlas = MagicMock(return_value=MagicMock(orientation="asr", resolution=(25.0, 25.0, 25.0))) + monkeypatch.setattr(atlas_module, "get_downloaded_atlases", lambda: [DEFAULT_ATLAS]) + monkeypatch.setattr(atlas_module, "_annotation_is_cached", lambda atlas: True) + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", fake_atlas) + atlas_module.get_atlas.cache_clear() + try: + _, failures = _run_warmup(caplog) + assert failures == [] + fake_atlas.assert_any_call(DEFAULT_ATLAS, check_latest=False) + assert atlas_module.get_atlas.cache_info().currsize == 1 + finally: + atlas_module.get_atlas.cache_clear() + + +def test_warmup_with_no_atlas_on_disk_never_opens_or_downloads_one(monkeypatch, caplog): + fake_atlas = Mock() + monkeypatch.setattr(atlas_module, "get_downloaded_atlases", list) # always empty + monkeypatch.setattr(atlas_module, "BrainGlobeAtlas", fake_atlas) + atlas_module.get_atlas.cache_clear() + try: + _, failures = _run_warmup(caplog) + finally: + atlas_module.get_atlas.cache_clear() + assert failures == [] + fake_atlas.assert_not_called() diff --git a/tests/test_wiring_lookup.py b/tests/test_wiring_lookup.py index a80307a..6315763 100644 --- a/tests/test_wiring_lookup.py +++ b/tests/test_wiring_lookup.py @@ -51,7 +51,7 @@ def _reference_forbidden(selected_electrodes, wiring_df): for se in table_ids[table_row] if (~np.any(np.isnan(se))) & ~((shank_id == se[0]) & (electrode_id == se[1])) ] - return np.array(forbidden_electrodes).astype(int) + return np.array(forbidden_electrodes).astype(int).reshape(-1, 2) def _reference_coordinates(electrodes, wiring_df): @@ -163,8 +163,8 @@ def test_empty_selection(probe): empty = np.zeros((0, 2), dtype=int) got = backend.find_forbidden_electrodes(empty, wiring_df) want = _reference_forbidden(empty, wiring_df) - # The original returns a 1-D empty array; callers depend on that shape. - assert got.shape == want.shape == (0,) + # (0, 2), not 1-D: callers index columns (plot_probe_layout crashed on 1-D). + assert got.shape == want.shape == (0, 2) assert backend.find_electrode_coordinates(empty, wiring_df) == [] @@ -231,3 +231,37 @@ def test_lookups_much_faster_than_per_electrode_scan(): backend.find_electrode_coordinates(sel, wiring_df), )) assert new * 5 < old, f"new {new * 1000:.0f} ms vs reference {old * 1000:.0f} ms" + + +# -------------------------------------------------------------------------- +# Wiring-map rebuild +# -------------------------------------------------------------------------- + +def _copy_wiring_csvs(tmp_path): + for csv in WIRING_DIR.glob("*.csv"): + (tmp_path / csv.name).write_bytes(csv.read_bytes()) + return tmp_path + + +def test_rebuilt_wiring_maps_match_the_shipped_cache(tmp_path): + """No pickle: the maps are rebuilt from the CSVs, identical to the shipped ones.""" + import pickle + + rebuilt = backend._load_wiring_maps(str(_copy_wiring_csvs(tmp_path))) + with open(WIRING_DIR / "wiring_maps.pkl", "rb") as f: + shipped = pickle.load(f) + assert {k: dict(v) for k, v in rebuilt.items()} == { + k: {e: frozenset(c) for e, c in v.items()} for k, v in shipped.items() + } + + +def test_rebuild_indexes_each_wiring_table_once(tmp_path, monkeypatch): + """Re-indexing per electrode made a cold rebuild take ~55 s, inside the + first session's GUI constructor on the event loop.""" + calls = [] + real = backend._wiring_index + monkeypatch.setattr(backend, "_wiring_index", lambda df: calls.append(1) or real(df)) + + backend._load_wiring_maps(str(_copy_wiring_csvs(tmp_path))) + + assert len(calls) == len(WIRING_FILE_MAP) diff --git a/tests/test_zigzag_dependent_deselect.py b/tests/test_zigzag_dependent_deselect.py new file mode 100644 index 0000000..c5ff319 --- /dev/null +++ b/tests/test_zigzag_dependent_deselect.py @@ -0,0 +1,170 @@ +"""Regression coverage for freeing only zigzag-pattern wiring dependents.""" + +import pytest +from bokeh.models import BoxSelectTool + +from pixelmap.gui.gui import ChannelmapGUI +from pixelmap.types import Electrode + + +@pytest.fixture(scope="module") +def gui(): + return ChannelmapGUI() + + +@pytest.fixture(autouse=True) +def clean_selection(gui): + gui.probe_type = "1.0" + gui.electrodes.clear_selection() + gui.electrodes._undo_stack.clear() + gui.electrodes._redo_stack.clear() + gui.select_mode = "zigzag_dependent_deselect" + yield + gui.electrodes.clear_selection() + + +def _sites(gui): + data = gui.electrode_source.data + return [Electrode(s, e) for s, e in zip(data["shank_id"], data["electrode_id"])] + + +def _indices(gui, electrodes): + return [i for i, e in enumerate(_sites(gui)) if e in electrodes] + + +def _dense_source(gui): + """Select bank 0 and box the matching bank-1 range on shank 0.""" + source = {Electrode(0, e) for e in range(384)} + chunk = {Electrode(0, e) for e in range(384, 768)} + for e in sorted(source, key=lambda e: e.electrode_id): + gui.electrodes.select(e) + assert gui.electrodes.selected == source + assert chunk <= gui.electrodes.unavailable + return source, chunk + + +@pytest.mark.parametrize("probe_type", ["1.0", "2.0-1shank", "2.0-4shanks", "QuadBase"]) +def test_zigzag_wiring_closure(gui, probe_type): + gui.probe_type = probe_type + subset = set(gui.get_zigzag_subset()) + zigzag = {e for e in gui.electrodes.wiring_map if e.electrode_id in subset} + assert zigzag + for e in zigzag: + assert gui.electrodes.wiring_map[e] <= zigzag, (probe_type, e) + + +@pytest.mark.parametrize("probe_type", ["1.0", "2.0-1shank"]) +def test_dense_source_keeps_complement_and_frees_only_zigzag(gui, probe_type): + gui.probe_type = probe_type + gui.select_mode = "zigzag_dependent_deselect" + source, chunk = _dense_source(gui) + subset = set(gui.get_zigzag_subset()) + zigzag_chunk = {e for e in chunk if e.electrode_id in subset} + assert len(zigzag_chunk) == len(chunk) // 2 + + gui.on_electrode_selection(None, [], _indices(gui, chunk)) + + assert gui.electrodes.selected == {e for e in source if e.electrode_id not in subset} + assert zigzag_chunk <= gui.electrodes.available + assert chunk - zigzag_chunk <= gui.electrodes.unavailable + assert not chunk & gui.electrodes.selected + + +def test_no_unavailable_electrodes_is_noop(gui): + gui.electrodes.select(Electrode(0, 0)) + before = gui.electrodes.selected.copy() + gui.on_electrode_selection(None, [], _indices(gui, {Electrode(0, 2), Electrode(0, 4)})) + assert gui.electrodes.selected == before + + +def test_only_non_zigzag_unavailable_is_noop(gui): + source, chunk = _dense_source(gui) + subset = set(gui.get_zigzag_subset()) + targets = {e for e in chunk if e.electrode_id not in subset} + gui.on_electrode_selection(None, [], _indices(gui, targets)) + assert gui.electrodes.selected == source + assert chunk <= gui.electrodes.unavailable + + +def test_selected_electrodes_in_box_stay_selected(gui): + source, chunk = _dense_source(gui) + # These red sites have different wiring from the blocked sites in the box. + red = {Electrode(0, 0), Electrode(0, 1)} + targets = red | {Electrode(0, 386), Electrode(0, 387)} + gui.on_electrode_selection(None, [], _indices(gui, targets)) + assert red <= gui.electrodes.selected + assert Electrode(0, 2) not in gui.electrodes.selected + assert Electrode(0, 3) in gui.electrodes.selected + assert gui.electrodes.selected <= source + assert not chunk & gui.electrodes.selected + + +def test_red_blocker_inside_box_is_deselected(gui): + """Red sites in the box are not targets, but they are still deselected + when they block a targeted black zigzag site in the same box.""" + _dense_source(gui) + # 1.0: 2 blocks 386 (both zigzag); 3 blocks 387 (both non-zigzag). + targets = {Electrode(0, 2), Electrode(0, 3), Electrode(0, 386), Electrode(0, 387)} + gui.on_electrode_selection(None, [], _indices(gui, targets)) + assert Electrode(0, 2) not in gui.electrodes.selected + assert Electrode(0, 386) in gui.electrodes.available + assert Electrode(0, 3) in gui.electrodes.selected + assert Electrode(0, 387) in gui.electrodes.unavailable + + +def test_empty_selection_is_noop(gui): + gui.on_electrode_selection(None, [], []) + assert gui.electrodes.selected == set() + assert not gui.electrodes.can_undo() + + +def test_single_electrode_still_toggles(gui): + target = _sites(gui)[0] + gui.on_electrode_selection(None, [], [0]) + assert gui.electrodes.selected == {target} + gui.on_electrode_selection(None, [], [0]) + assert gui.electrodes.selected == set() + + +def test_drag_is_one_undo_step(gui): + source, chunk = _dense_source(gui) + unavailable = gui.electrodes.unavailable.copy() + gui.on_electrode_selection(None, [], _indices(gui, chunk)) + assert gui.electrodes.selected != source + assert gui.electrodes.undo() is True + assert gui.electrodes.selected == source + assert gui.electrodes.unavailable == unavailable + assert gui.electrodes.undo() is False + + +def test_existing_dependent_deselect_frees_whole_chunk(gui): + _, chunk = _dense_source(gui) + gui.select_mode = "dependent_deselect" + gui.on_electrode_selection(None, [], _indices(gui, chunk)) + assert gui.electrodes.selected == set() + assert chunk <= gui.electrodes.available + + +def test_tool_state_maps_new_mode(gui): + gui.select_mode = "select" + gui.on_tool_state_change(None, {}, {"active_tool": ["zigzag_dependent_deselect"]}) + assert gui.select_mode == "zigzag_dependent_deselect" + + +def test_toolbar_and_monitor_include_sixth_tool(gui): + boxes = [tool for tool in gui.plot.toolbar.tools if isinstance(tool, BoxSelectTool)] + assert len(boxes) == 6 + assert boxes[-1].description == "Zigzag-deselect Dependents" + monitor = gui.plot.js_event_callbacks["selectiongeometry"][0] + assert "Zigzag-deselect Dependents" in monitor.args.values() + assert "zigzag_dependent_deselect" in monitor.code + + +def test_side_panel_help_documents_every_box_tool(gui): + """Every toolbar box tool has an entry with an embedded icon in the help.""" + boxes = [tool for tool in gui.plot.toolbar.tools if isinstance(tool, BoxSelectTool)] + assert len(gui._SELECTION_TOOL_DOCS) == len(boxes) + html = gui._selection_tools_help_html() + assert html.count('src="data:image/png;base64,') == len(boxes) + assert "Zigzag-deselect dependents" in html +