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 5d5322e..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,25 +103,24 @@ 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.empty((0, 2), dtype=int) @@ -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) @@ -1009,43 +1003,23 @@ def plot_probe_layout( 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) - 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) + 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), + ) + # 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( @@ -1114,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/gui.py b/pixelmap/gui/gui.py index 3b4f12b..61a73dd 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 import matplotlib.pyplot as plt import numpy as np @@ -58,6 +60,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 @@ -84,19 +87,6 @@ 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 +114,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 #### ################################### @@ -302,6 +346,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() @@ -995,7 +1054,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() @@ -1234,17 +1293,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 @@ -1255,71 +1310,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": []} @@ -1366,74 +1550,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": [], } @@ -1505,7 +1635,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): @@ -1543,22 +1673,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. @@ -1623,55 +1790,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 _tip_depth_below_surface(self) -> float | None: - """Tip-to-brain-surface distance along the probe axis, in µm. + def _selected_atlas_name(self) -> str: + return str(self.atlas_name_input.value).strip() - ``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 = ( '—' @@ -1697,11 +1836,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. @@ -1773,14 +1912,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. @@ -1814,8 +1953,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 @@ -1823,17 +1961,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.""" @@ -2704,10 +2839,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, @@ -2731,7 +2862,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( @@ -2741,7 +2872,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". @@ -2751,8 +2882,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) 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/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 f9d0c3d..8d15a96 100644 --- a/tests/test_pdf_export.py +++ b/tests/test_pdf_export.py @@ -18,7 +18,7 @@ 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 @@ -104,11 +104,11 @@ def _rgba(c): def _electrode_collections(ax): - """Per-shank electrode PatchCollections, in shank order (survey bars excluded).""" + """Per-shank electrode PolyCollections, in shank order (survey bars excluded).""" return [ c for c in ax.collections - if isinstance(c, PatchCollection) and c.get_zorder() != SURVEY_BAR_ZORDER + if isinstance(c, PolyCollection) and c.get_zorder() != SURVEY_BAR_ZORDER ] @@ -124,7 +124,7 @@ 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. @@ -191,5 +191,5 @@ def test_no_forbidden_electrodes(gui, probe_type, monkeypatch): 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_wiring_lookup.py b/tests/test_wiring_lookup.py index 8e83d4a..6315763 100644 --- a/tests/test_wiring_lookup.py +++ b/tests/test_wiring_lookup.py @@ -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)