From 2b6ce90714ff2e1f5588066f9cf731da647389d5 Mon Sep 17 00:00:00 2001 From: Christian Tabedzki <35670232+tabedzki@users.noreply.github.com> Date: Sat, 7 Nov 2026 08:41:56 -0500 Subject: [PATCH 1/2] perf: shrink the document every session builds on the event loop Each session builds, validates, serialises and sends its Bokeh document on the loop that serves every other session and /liveness. - Draw shank outlines, bank lines and labels with one renderer per style. A 4-shank document drops from 33 renderers and 485 models to 9 and 269. - Limit tap and box-select tools to the electrode and survey-bar renderers, so a tap on a shared outline renderer can't dim the other outlines. - Send numeric electrode columns as arrays, compute their geometry with NumPy, and skip Bokeh's per-cell check of that fixed-shape source. - Reuse the cached data URI for tool icons instead of re-encoding six PNGs. Assisted-by: ClaudeCode:claude-opus-5-5 --- pixelmap/gui/gui.py | 351 +++++++++++++------------------ tests/test_session_build_cost.py | 147 +++++++++++++ 2 files changed, 291 insertions(+), 207 deletions(-) create mode 100644 tests/test_session_build_cost.py diff --git a/pixelmap/gui/gui.py b/pixelmap/gui/gui.py index 61a73dd..2b5e172 100644 --- a/pixelmap/gui/gui.py +++ b/pixelmap/gui/gui.py @@ -16,7 +16,7 @@ from dataclasses import dataclass, field from io import BytesIO, StringIO from pathlib import Path -from typing import Any +from typing import Any, NamedTuple import matplotlib.pyplot as plt import numpy as np @@ -24,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, @@ -270,6 +271,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""" @@ -441,98 +491,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()), + } - 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 + # 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) + + 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.""" @@ -610,96 +639,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", - ) - - 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", - ) + """Add shank outlines and bank labels. - # 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 @@ -708,39 +669,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()) ) @@ -2239,27 +2170,27 @@ def setup_bokeh_plot(self): 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=GUI_ASSETS_DIR / "zigzag_dependent_deselector.png", + icon=_asset_data_uri("zigzag_dependent_deselector.png"), ) # Always-on gesture tools — hidden from toolbar (visible=False). @@ -2316,6 +2247,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. 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] From eb8d7e4dce914a56c77a3bc2eaeeb690ffee3898 Mon Sep 17 00:00:00 2001 From: Christian Tabedzki <35670232+tabedzki@users.noreply.github.com> Date: Sat, 7 Nov 2026 15:11:46 -0500 Subject: [PATCH 2/2] perf: build each session's GUI off the event loop `panel serve` runs the app per session on the loop that serves every session and /liveness. Serve a placeholder at once, build the GUI on one worker thread once the page connects, and swap it in on the loop under the document lock. Nothing on the worker touches a document: ?cfg= is read in create_app, and destroy hooks are registered on the loop, so ChannelmapGUI.__init__ no longer registers its own. A tab closed while queued skips the build, and one closed mid-build has its GUI cleaned up. Any build or swap failure shows the existing alert and is reported. Drop validate(False) from create_electrode_data: it toggles a process-global flag, and on_probe_type_change on the loop can overlap the worker's build, which could leave property validation off for the whole server. This costs about 41 ms per electrode source. Assisted-by: ClaudeCode:claude-opus-5-5 --- pixelmap/gui/gui.py | 163 +++++++-- tests/test_session_leak.py | 49 +++ tests/test_session_off_loop.py | 584 +++++++++++++++++++++++++++++++++ 3 files changed, 760 insertions(+), 36 deletions(-) create mode 100644 tests/test_session_off_loop.py diff --git a/pixelmap/gui/gui.py b/pixelmap/gui/gui.py index 2b5e172..bf279e2 100644 --- a/pixelmap/gui/gui.py +++ b/pixelmap/gui/gui.py @@ -6,6 +6,7 @@ import asyncio import base64 import contextlib +import contextvars import functools import json import logging @@ -13,6 +14,7 @@ import threading from collections.abc import Mapping from collections.abc import Set as AbstractSet +from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, field from io import BytesIO, StringIO from pathlib import Path @@ -24,7 +26,6 @@ import panel as pn import param from bokeh import events -from bokeh.core.properties import validate from bokeh.models import ( BoxSelectTool, ColorBar, @@ -420,10 +421,6 @@ def __init__(self, **params): # Create widgets self.create_widgets() - # Register cleanup on session destroy - if pn.state.curdoc: - pn.state.curdoc.on_session_destroyed(self._cleanup_session) - def _cleanup_session(self, session_context): """Clean up resources when session ends""" @@ -533,10 +530,11 @@ def create_electrode_data(self): **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) + # Not under Bokeh's `validate(False)`, tempting as this is its slowest + # step: that toggles a process-global flag, and sessions build on a + # worker while `on_probe_type_change` runs here on the loop. Overlapping + # saves and restores would leave validation off for the whole server. + self.electrode_source = ColumnDataSource(data=electrode_data) def _electrode_appearance_columns(self, shank_ids, electrode_ids) -> dict[str, Any]: """Selection- and survey-dependent columns, one value per electrode.""" @@ -3323,6 +3321,108 @@ def _release_dead_session_views(session_context): except Exception as exc: # noqa: BLE001 - a failed cleanup must not keep the session pinned print(f"View cleanup failed for {type(view).__name__}: {exc}") pn.state._views.pop(ref, None) + # A session that never loaded still has its build queued here, and that + # callback reaches the document, so the WeakKeyDictionary would never let go. + pn.state._onload.pop(doc, None) + + +# Session builds run here, one at a time: a burst of visitors then queues rather +# than fighting the loop for the GIL, at the price of the last one waiting longest. +_SESSION_BUILD_EXECUTOR = ThreadPoolExecutor(max_workers=1, thread_name_prefix="pixelmap-session-build") + + +class _SessionContent(NamedTuple): + gui: ChannelmapGUI + analytics_tracker: AnalyticsSessionTracker + children: list + + +def _build_session_content(cfg: str | None) -> _SessionContent: + """Construct a session's GUI. Runs on a worker under a server: no document access. + + Nothing here is attached to a document yet, so changing these objects has no + effect on one. Whatever needs the document or the request belongs in + `create_app` or `_fill_session`, which run on the loop. + """ + gui = ChannelmapGUI() + if cfg: + try: + applied = gui.apply_url_state(cfg) + if not applied: + print("Ignored invalid or unsupported ?cfg= share link.") + except Exception as e: # noqa: BLE001 - a bad share link must not break app start + print(f"Failed to apply shared layout from URL: {e}") + + analytics_tracker = AnalyticsSessionTracker() + layout = gui.create_layout() + + # Update status initially + gui.update_electrode_counter() + return _SessionContent(gui, analytics_tracker, [analytics_tracker.cookie_bridge, layout]) + + +def _register_session_hooks(doc, content: _SessionContent) -> None: + doc.on_session_destroyed(content.gui._cleanup_session) + doc.on_session_destroyed(content.analytics_tracker.handle_session_destroyed) + + +def _report_startup_failure(exc: Exception) -> pn.pane.Alert: + """Report a failed session build; returns what to show the visitor instead of a blank page.""" + error_reporting.report_error(exc, context="session startup", notify_user=False) + return pn.pane.Alert( + "PixelMap failed to start. Please reload the page; if this keeps " + "happening, report it at https://github.com/m-beau/pixelmap/issues.", + alert_type="danger", + ) + + +async def _fill_session(app_layout: pn.Column, cfg: str | None) -> None: + """Build the GUI on the worker, then swap it into ``app_layout`` on the loop. + + Deliberately not ``with_lock``: Bokeh holds the lock across an ``await``, and + a locked session can't be discarded, so a closed tab would pin its session + until its turn in the queue. Only the swap takes the lock. The session can + therefore die during either ``await`` -- the checks below are on the loop, as + is the discard, so nothing can change between a check and what follows it. + """ + doc = pn.state.curdoc + session_context = doc.session_context + + def build() -> _SessionContent | None: + if session_context.destroyed: # closed while queued + return None + return _build_session_content(cfg) + + def show(children: list) -> None: + # Bokeh drops every pending write if a locked callback raises, so a + # failed swap must end here, with the alert, not in Panel's handler. + try: + app_layout[:] = children + except Exception as exc: # noqa: BLE001 - any failure gets the error page + app_layout[:] = [_report_startup_failure(exc)] + + try: + # The copied context is what makes `pn.state.curdoc` this session's in + # the worker. Without it Panel falls back to Bokeh's process-global + # curdoc, which is whichever session the loop is serving at that moment. + content = await asyncio.get_running_loop().run_in_executor( + _SESSION_BUILD_EXECUTOR, contextvars.copy_context().run, build + ) + except Exception as exc: # noqa: BLE001 - any startup failure gets an error page + alert = _report_startup_failure(exc) + if not session_context.destroyed: + pn.state.execute(functools.partial(show, [alert]), schedule=True) + return + + if content is None: + return + if session_context.destroyed: + content.gui._cleanup_session(session_context) + return + # Before the swap, which waits for the lock: from here a closing tab cleans up. + _register_session_hooks(doc, content) + app_layout._analytics_tracker = content.analytics_tracker + pn.state.execute(functools.partial(show, content.children), schedule=True) def create_app(): @@ -3341,36 +3441,27 @@ def create_app(): ) error_reporting.install_loop_handler() - try: - gui = ChannelmapGUI() - except Exception as exc: # noqa: BLE001 - any startup failure gets an error page - # Show the visitor something instead of a blank page. - error_reporting.report_error(exc, context="session startup", notify_user=False) - return pn.pane.Alert( - "PixelMap failed to start. Please reload the page; if this keeps " - "happening, report it at https://github.com/m-beau/pixelmap/issues.", - alert_type="danger", - ) - + # Read on the loop: a worker can't be trusted with the current request. cfg = _extract_cfg_from_session_args() - if cfg: - try: - applied = gui.apply_url_state(cfg) - if not applied: - print("Ignored invalid or unsupported ?cfg= share link.") - except Exception as e: # noqa: BLE001 - a bad share link must not break app start - print(f"Failed to apply shared layout from URL: {e}") + doc = pn.state.curdoc - analytics_tracker = AnalyticsSessionTracker() - layout = gui.create_layout() + if doc is not None and doc.session_context is not None: + # Served: answer right away and build once the page has connected. + doc.on_session_destroyed(_release_dead_session_views) + app_layout = pn.Column(pn.pane.HTML("Loading PixelMap…"), margin=0) + pn.state.onload(functools.partial(_fill_session, app_layout, cfg)) + return app_layout - # Update status initially - gui.update_electrode_counter() + # No session (scripts, tests): nothing else is waiting on this thread. + try: + content = _build_session_content(cfg) + except Exception as exc: # noqa: BLE001 - any startup failure gets an error page + return _report_startup_failure(exc) - if pn.state.curdoc: - pn.state.curdoc.on_session_destroyed(analytics_tracker.handle_session_destroyed) - pn.state.curdoc.on_session_destroyed(_release_dead_session_views) + if doc is not None: + doc.on_session_destroyed(_release_dead_session_views) + _register_session_hooks(doc, content) - app_layout = pn.Column(analytics_tracker.cookie_bridge, layout, margin=0) - app_layout._analytics_tracker = analytics_tracker + app_layout = pn.Column(*content.children, margin=0) + app_layout._analytics_tracker = content.analytics_tracker return app_layout diff --git a/tests/test_session_leak.py b/tests/test_session_leak.py index 7391481..019a964 100644 --- a/tests/test_session_leak.py +++ b/tests/test_session_leak.py @@ -17,11 +17,13 @@ import socket import time import types +import weakref import panel as pn import pytest from bokeh.client import pull_session from bokeh.document import Document +from bokeh.events import DocumentReady from panel.io.state import state from pixelmap.gui import gui as gui_module @@ -97,6 +99,45 @@ def test_closed_sessions_release_their_gui(server, n_sessions): assert _dead_doc_views() == [] +def _fire_document_ready(bokeh_server, session) -> None: + """Sessions build once a browser reports ``document_ready``; ``pull_session`` never does.""" + doc = session.document + bokeh_server.io_loop.add_callback( + session.with_document_locked, + lambda: doc.callbacks.trigger_event(DocumentReady()), + ) + + +@pytest.mark.parametrize("n_sessions", [1, 3]) +def test_closed_sessions_that_built_a_gui_release_it(server, n_sessions): + url, bokeh_server = server + baseline = _live_guis() + + clients = [pull_session(url=url) for _ in range(n_sessions)] + for session in _sessions(bokeh_server): + _fire_document_ready(bokeh_server, session) + _wait_for(lambda: _live_guis() == baseline + n_sessions, 60, "every session to build its GUI") + # The swap follows the build on the loop; a close racing it is covered too. + for client in clients: + client.close() + + _wait_for(lambda: not _sessions(bokeh_server), 30, "server sessions to be discarded") + _wait_for(lambda: _live_guis() <= baseline, 15, "dead sessions' GUIs to be collected") + assert _dead_doc_views() == [] + + +def test_closed_sessions_leave_no_queued_load_behind(server): + """A queued onload reaches its document, so Panel's weak mapping would pin it forever.""" + url, bokeh_server = server + client = pull_session(url=url) + doc = _sessions(bokeh_server)[0].document + assert doc in state._onload # never loaded: no browser sent document_ready + + client.close() + _wait_for(lambda: not _sessions(bokeh_server), 30, "server session to be discarded") + _wait_for(lambda: doc not in state._onload, 15, "the queued load to be dropped") + + # -------------------------------------------------------------------------- # The hook itself # -------------------------------------------------------------------------- @@ -145,6 +186,14 @@ def test_hook_with_no_views_for_doc_is_noop(views): assert set(views) == {"c"} +def test_hook_drops_only_the_dead_documents_queued_load(views, monkeypatch): + dead, alive = Document(), Document() + onload = weakref.WeakKeyDictionary({dead: [("load", False)], alive: [("load", False)]}) + monkeypatch.setattr(state, "_onload", onload) + _release_dead_session_views(_ctx(dead)) + assert list(onload) == [alive] + + def test_hook_with_empty_views(views): _release_dead_session_views(_ctx(Document())) assert views == {} diff --git a/tests/test_session_off_loop.py b/tests/test_session_off_loop.py new file mode 100644 index 0000000..61cf49a --- /dev/null +++ b/tests/test_session_off_loop.py @@ -0,0 +1,584 @@ +"""A session's GUI is built off the Bokeh event loop, without racing the document. + +``panel serve`` runs the app per session on the loop that serves every session +and ``/liveness``. The session gets a placeholder at once; the GUI is built on +one worker thread and swapped in on the loop. These tests use a real server, +and fire the ``document_ready`` event a browser would send: ``pull_session`` +never sends it. +""" + +from __future__ import annotations + +import gc +import socket +import threading +import time +import weakref + +import panel as pn +import pytest +from bokeh.client import pull_session +from bokeh.core.property.bases import validation_on +from bokeh.document import Document +from bokeh.events import DocumentReady +from bokeh.io.doc import patch_curdoc +from panel.io.state import set_curdoc, state + +from pixelmap import error_reporting +from pixelmap.gui import gui as gui_module +from pixelmap.utils import url_share + +RealGUI = gui_module.ChannelmapGUI +LOADING = "Loading PixelMap" + + +def _wait_for(cond, timeout: float, what: str) -> None: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + if cond(): + return + time.sleep(0.02) + raise AssertionError(f"timed out waiting for {what}") + + +def _live_guis() -> int: + gc.collect() + return sum(isinstance(o, RealGUI) for o in gc.get_objects()) + + +class Served: + """A real threaded server whose app factory remembers what it returned.""" + + def __init__(self): + with socket.socket() as s: + s.bind(("127.0.0.1", 0)) + self.port = s.getsockname()[1] + self.apps = [] # weakrefs, so these tests can see sessions die + self.factory_threads = [] + self.server = pn.serve( + {"app": self._factory}, + port=self.port, + address="127.0.0.1", + show=False, + threaded=True, + check_unused_sessions_milliseconds=100, + unused_session_lifetime_milliseconds=100, + allow_websocket_origin=[f"127.0.0.1:{self.port}"], + ) + _wait_for(lambda: self.server.server_id in state._servers, 30, "server start") + self.bokeh = state._servers[self.server.server_id][0] + self.url = f"http://127.0.0.1:{self.port}/app" + + def _factory(self): + self.factory_threads.append(threading.current_thread()) + app = gui_module.create_app() + self.apps.append(weakref.ref(app)) + return app + + def sessions(self): + return [s for ctx in self.bokeh._tornado._applications.values() for s in ctx.sessions] + + def open(self, **arguments): + """Open a session; returns (client session, server session, app).""" + known = {s.id for s in self.sessions()} + client = pull_session(url=self.url, arguments=arguments or None) + server_session = next(s for s in self.sessions() if s.id not in known) + return client, server_session, self.apps[-1]() + + def on_loop(self, func): + """Run ``func`` on the server's event loop and wait for its result.""" + done = threading.Event() + box = {} + + def call(): + try: + box["value"] = func() + except BaseException as exc: # noqa: BLE001 - re-raised in the test thread + box["error"] = exc + done.set() + + self.bokeh.io_loop.add_callback(call) + assert done.wait(30), "the event loop did not respond" + if "error" in box: + raise box["error"] + return box.get("value") + + def fire_ready(self, session): + """Do what the browser's ``document_ready`` does.""" + doc = session.document + self.bokeh.io_loop.add_callback( + session.with_document_locked, + lambda: doc.callbacks.trigger_event(DocumentReady()), + ) + + def stop(self): + self.server.stop() + + +@pytest.fixture +def served(): + srv = Served() + yield srv + srv.stop() + + +@pytest.fixture +def reports(monkeypatch): + seen = [] + monkeypatch.setattr( + error_reporting, "report_error", lambda exc, **kw: seen.append((exc, kw)) + ) + return seen + + +class Gate: + """Replaces the GUI class: records its thread and blocks until released.""" + + def __init__(self, monkeypatch, *, block=True): + self.started = threading.Event() + self.release = threading.Event() + if not block: + self.release.set() + self.threads = [] + self.curdocs = [] + self.running = 0 + self.max_running = 0 + self.guis = [] + monkeypatch.setattr(gui_module, "ChannelmapGUI", self) + + def __call__(self, **params): + self.running += 1 + self.max_running = max(self.max_running, self.running) + self.threads.append(threading.current_thread()) + self.curdocs.append(weakref.ref(pn.state.curdoc)) # weak: leak tests watch the document + self.started.set() + try: + assert self.release.wait(30) + gui = RealGUI(**params) + self.guis.append(weakref.ref(gui)) + return gui + finally: + self.running -= 1 + + +def _shows(app, cls) -> bool: + return any(isinstance(o, cls) for o in app.objects) + + +def _is_placeholder(app) -> bool: + return len(app.objects) == 1 and LOADING in str(app.objects[0].object) + + +def _is_built(app) -> bool: + return not _is_placeholder(app) + + +def _gui_of(session) -> RealGUI: + return next( + cb.__self__ + for cb in session.document.session_destroyed_callbacks + if getattr(cb, "__name__", "") == "_cleanup_session" + ) + + +# --------------------------------------------------------------------------- +# Where the work runs +# --------------------------------------------------------------------------- + + +def test_session_starts_with_a_placeholder_and_no_gui(served, monkeypatch): + gate = Gate(monkeypatch) + client, _, app = served.open() + assert _is_placeholder(app) + assert gate.threads == [] + client.close() + + +def test_gui_is_built_on_the_session_worker_while_the_loop_stays_free(served, monkeypatch): + gate = Gate(monkeypatch) + client, session, app = served.open() + served.fire_ready(session) + assert gate.started.wait(30) + + # The build is blocked mid-way, and the loop still answers. + assert served.on_loop(lambda: "alive") == "alive" + assert _is_placeholder(app) + assert gate.threads[0].name.startswith("pixelmap-session-build") + assert gate.threads[0] is not served.factory_threads[0] + + gate.release.set() + _wait_for(lambda: _is_built(app), 30, "the GUI to be swapped in") + client.close() + + +def test_worker_sees_its_own_document_not_the_one_the_loop_is_serving(served, monkeypatch): + """Bokeh's curdoc patch stack is process-global: only the context is per-session.""" + gate = Gate(monkeypatch) + client, session, app = served.open() + other = Document() + other._session_context = lambda: None # state.curdoc requires a session context + with patch_curdoc(other): + served.fire_ready(session) + assert gate.started.wait(30) + gate.release.set() + _wait_for(lambda: gate.curdocs, 30, "the build to read curdoc") + assert gate.curdocs[0]() is session.document + _wait_for(lambda: _is_built(app), 30, "swap") + client.close() + + +def test_session_builds_never_overlap_and_run_in_order(served, monkeypatch): + gate = Gate(monkeypatch, block=False) + opened = [served.open() for _ in range(3)] + for _, session, _ in opened: + served.fire_ready(session) + _wait_for(lambda: all(_is_built(app) for *_, app in opened), 90, "all sessions to build") + + assert gate.max_running == 1 + assert len({t.ident for t in gate.threads}) == 1 + # FIFO: each build saw the session it belongs to. + assert [ref() for ref in gate.curdocs] == [session.document for _, session, _ in opened] + for client, *_ in opened: + client.close() + + +def test_worker_does_not_touch_any_document(served, monkeypatch): + """Everything that mutates a Document must happen on the loop.""" + touched = [] + for method in ("on_session_destroyed", "add_root", "remove_root", "on_event", "hold", "unhold"): + original = getattr(Document, method) + + def spy(self, *args, _original=original, _method=method, **kwargs): + touched.append((_method, threading.current_thread().name)) + return _original(self, *args, **kwargs) + + monkeypatch.setattr(Document, method, spy) + client, session, app = served.open() + served.fire_ready(session) + _wait_for(lambda: _is_built(app), 60, "swap") + + assert ("on_session_destroyed", served.factory_threads[0].name) in touched + assert [t for t in touched if t[1].startswith("pixelmap-session-build")] == [] + client.close() + + +def test_constructor_registers_nothing_on_the_current_document(): + """The worker has a document in context; the constructor must leave it alone.""" + doc = Document() + pn.state.curdoc = doc + try: + RealGUI() + finally: + pn.state.curdoc = None + assert doc.session_destroyed_callbacks == set() + + +# --------------------------------------------------------------------------- +# validate(False) toggled a process-global flag +# --------------------------------------------------------------------------- + + +def test_electrode_source_is_built_with_validation_on(monkeypatch): + gui = RealGUI() + seen = [] + real_source = gui_module.ColumnDataSource + + def spy(*args, **kwargs): + seen.append(validation_on()) + return real_source(*args, **kwargs) + + monkeypatch.setattr(gui_module, "ColumnDataSource", spy) + gui.create_electrode_data() + assert seen == [True] + + +def test_concurrent_electrode_sources_leave_validation_on(): + """Overlapping save/restore of the global flag used to leave it off for good.""" + guis = [RealGUI(), RealGUI()] + barrier = threading.Barrier(2) + + def hammer(gui): + barrier.wait() + for _ in range(15): + gui.create_electrode_data() + + workers = [threading.Thread(target=hammer, args=(g,)) for g in guis] + for w in workers: + w.start() + for w in workers: + w.join() + assert validation_on() + + +# --------------------------------------------------------------------------- +# Tab closed before the swap +# --------------------------------------------------------------------------- + + +def test_tab_closed_mid_build_releases_the_gui_and_the_document(served, monkeypatch, reports): + baseline = _live_guis() + gate = Gate(monkeypatch) + cleanups = [] + real_cleanup = RealGUI._cleanup_session + monkeypatch.setattr( + RealGUI, "_cleanup_session", + lambda self, ctx: (cleanups.append(ctx.id), real_cleanup(self, ctx))[1], + ) + client, session, app = served.open() + session_id, app_ref = session.id, weakref.ref(app) + doc_ref = weakref.ref(session.document) + served.fire_ready(session) + assert gate.started.wait(30) + + del app + client.close() + _wait_for(lambda: not served.sessions(), 30, "the session to be discarded") + assert session.destroyed + + gate.release.set() + _wait_for(lambda: cleanups == [session_id], 30, "the orphaned GUI's cleanup") + del session + _wait_for(lambda: _live_guis() <= baseline, 15, "the orphaned GUI to be collected") + _wait_for(lambda: gc.collect() is not None and app_ref() is None, 15, "the app to be collected") + _wait_for(lambda: gc.collect() is not None and doc_ref() is None, 15, "the document to be collected") + assert reports == [] + + +def test_tab_closed_while_queued_never_builds(served, monkeypatch, reports): + gate = Gate(monkeypatch) + first_client, first, first_app = served.open() + served.fire_ready(first) + assert gate.started.wait(30) # occupies the single worker + + client, session, _ = served.open() + served.fire_ready(session) + time.sleep(0.2) # let its onload queue the build + client.close() + _wait_for(lambda: session.destroyed, 30, "the queued session to be discarded") + + gate.release.set() + _wait_for(lambda: _is_built(first_app), 30, "the first session's swap") + assert len(gate.threads) == 1 + assert reports == [] + first_client.close() + + +def test_session_that_never_loads_leaks_nothing_and_never_builds(served, monkeypatch): + """A client that never sends document_ready (a bot, a stray curl) costs a placeholder.""" + gate = Gate(monkeypatch) + client, session, app = served.open() + doc_ref = weakref.ref(session.document) + client.close() + del client, app, session + _wait_for(lambda: not served.sessions(), 30, "the session to be discarded") + served.apps.clear() + + _wait_for(lambda: gc.collect() is not None and doc_ref() is None, 15, "the document to be collected") + assert gate.threads == [] + + +def test_session_closed_after_swap_runs_every_hook(served, monkeypatch): + calls = [] + real_cleanup = RealGUI._cleanup_session + monkeypatch.setattr( + RealGUI, "_cleanup_session", + lambda self, ctx: (calls.append("gui"), real_cleanup(self, ctx))[1], + ) + real_destroyed = gui_module.AnalyticsSessionTracker.handle_session_destroyed + monkeypatch.setattr( + gui_module.AnalyticsSessionTracker, "handle_session_destroyed", + lambda self, ctx: (calls.append("tracker"), real_destroyed(self, ctx))[1], + ) + real_release = gui_module._release_dead_session_views + monkeypatch.setattr( + gui_module, "_release_dead_session_views", + lambda ctx: (calls.append("views"), real_release(ctx))[1], + ) + client, session, app = served.open() + served.fire_ready(session) + _wait_for(lambda: _is_built(app), 60, "swap") + client.close() + _wait_for(lambda: len(calls) >= 3, 30, "the destroy hooks") + assert sorted(calls) == ["gui", "tracker", "views"] + + +# --------------------------------------------------------------------------- +# Build failure +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize("broken", ["ChannelmapGUI", "AnalyticsSessionTracker"]) +def test_build_failure_replaces_the_placeholder_with_the_alert(served, monkeypatch, reports, broken): + def boom(*args, **kwargs): + raise RuntimeError("wiring maps missing") + + monkeypatch.setattr(gui_module, broken, boom) + client, session, app = served.open() + served.fire_ready(session) + _wait_for(lambda: _shows(app, pn.pane.Alert), 30, "the failure alert") + + assert "PixelMap failed to start" in app.objects[0].object + assert not _is_placeholder(app) + exc, kwargs = reports[0] + assert isinstance(exc, RuntimeError) + assert kwargs == {"context": "session startup", "notify_user": False} + client.close() + + +def test_build_failure_after_the_tab_closed_is_reported_without_a_document(served, monkeypatch, reports): + gate = Gate(monkeypatch) + real_call = gate.__call__ + + def boom(**params): + real_call(**params) + raise RuntimeError("late failure") + + monkeypatch.setattr(gui_module, "ChannelmapGUI", boom) + client, session, _ = served.open() + served.fire_ready(session) + assert gate.started.wait(30) + client.close() + _wait_for(lambda: session.destroyed, 30, "the session to be discarded") + gate.release.set() + _wait_for(lambda: reports, 30, "the failure report") + assert isinstance(reports[0][0], RuntimeError) + assert reports[0][1]["context"] == "session startup" + + +def test_swap_failure_shows_the_alert_and_reports(served, monkeypatch, reports): + """A child that can't render fails on the loop, after the worker succeeded.""" + def unrenderable(self, *args, **kwargs): + raise RuntimeError("cannot render") + + real_layout = RealGUI.create_layout + + def poisoned_layout(self): + layout = real_layout(self) + monkeypatch.setattr(type(layout), "_get_model", unrenderable) + return layout + + monkeypatch.setattr(RealGUI, "create_layout", poisoned_layout) + client, session, app = served.open() + served.fire_ready(session) + _wait_for(lambda: _shows(app, pn.pane.Alert), 30, "the failure alert") + assert reports[0][1]["context"] == "session startup" + client.close() + + +# --------------------------------------------------------------------------- +# Per-session state +# --------------------------------------------------------------------------- + + +def _share_link(electrodes, probe_type="2.0-4shanks") -> str: + gui = RealGUI() + if probe_type != gui.probe_type: + gui.probe_type = probe_type + for electrode in electrodes: + gui.electrodes.select(gui_module.Electrode(*electrode)) + return gui.build_share_link().split(f"{url_share.QUERY_PARAM}=", 1)[1] + + +def test_share_links_reach_the_right_sessions(served): + """?cfg= is read on the loop, in the session's own request.""" + link_a = _share_link([(0, 0), (0, 1)]) + link_b = _share_link([(0, 5)], probe_type="1.0") + client_a, session_a, app_a = served.open(cfg=link_a) + client_b, session_b, app_b = served.open(cfg=link_b) + client_c, session_c, app_c = served.open() + # Latest first, so a build reading the request late would get another session's. + for session in (session_c, session_b, session_a): + served.fire_ready(session) + for app in (app_a, app_b, app_c): + _wait_for(lambda app=app: _is_built(app), 90, "swap") + + gui_a, gui_b, gui_c = (_gui_of(s) for s in (session_a, session_b, session_c)) + assert len(gui_a.electrodes.selected) == 2 + assert (gui_b.probe_type, len(gui_b.electrodes.selected)) == ("1.0", 1) + assert (gui_c.probe_type, len(gui_c.electrodes.selected)) == ("2.0-4shanks", 0) + # The counter is rendered before the swap, from the restored selection. + assert "Selected Electrodes: 2/" in gui_a.electrode_counter.object + for client in (client_a, client_b, client_c): + client.close() + + +@pytest.mark.parametrize("cfg", ["", "not-a-share-link", "0"]) +def test_bad_share_links_still_build(served, cfg, reports): + client, session, app = served.open(cfg=cfg) + served.fire_ready(session) + _wait_for(lambda: _is_built(app), 60, "swap") + assert not _shows(app, pn.pane.Alert) + assert len(_gui_of(session).electrodes.selected) == 0 + assert reports == [] + client.close() + + +def test_share_link_failure_is_not_a_startup_failure(served, monkeypatch, reports): + def boom(self, encoded): + raise ValueError("corrupt") + + monkeypatch.setattr(RealGUI, "apply_url_state", boom) + client, session, app = served.open(cfg="anything") + served.fire_ready(session) + _wait_for(lambda: _is_built(app), 60, "swap") + assert not _shows(app, pn.pane.Alert) + assert reports == [] + client.close() + + +def test_built_session_has_its_notifications_tracker_and_handler(served): + # Not the first session: that is what used to lose its notification area. + first_client, first, first_app = served.open() + served.fire_ready(first) + _wait_for(lambda: _is_built(first_app), 60, "first swap") + client, session, app = served.open() + served.fire_ready(session) + _wait_for(lambda: _is_built(app), 60, "second swap") + + assert app._analytics_tracker.cookie_bridge is app.objects[0] + + def in_session(): + with set_curdoc(session.document): + area = pn.state.notifications + return area, pn.config.exception_handler + + area, handler = served.on_loop(in_session) + assert area is not None + assert handler is error_reporting.session_exception_handler + client.close() + first_client.close() + + +# --------------------------------------------------------------------------- +# No document: scripts, tests, the `pn.serve` factory's callers +# --------------------------------------------------------------------------- + + +def test_create_app_without_a_document_builds_inline(): + app = gui_module.create_app() + assert isinstance(app, pn.Column) + assert app._analytics_tracker.cookie_bridge is app.objects[0] + assert len(app.objects) == 2 + assert not _is_placeholder(app) + + +def test_create_app_with_a_sessionless_document_builds_inline_and_registers_hooks(): + doc = Document() + pn.state.curdoc = doc + try: + app = gui_module.create_app() + finally: + pn.state.curdoc = None + assert not _is_placeholder(app) + names = {getattr(cb, "__name__", "") for cb in doc.session_destroyed_callbacks} + assert {"_cleanup_session", "handle_session_destroyed", "_release_dead_session_views"} <= names + + +def test_create_app_inline_failure_returns_the_alert(monkeypatch, reports): + def boom(*args, **kwargs): + raise RuntimeError("no wiring") + + monkeypatch.setattr(gui_module, "ChannelmapGUI", boom) + app = gui_module.create_app() + assert isinstance(app, pn.pane.Alert) + assert reports[0][1] == {"context": "session startup", "notify_user": False}