arrayview 0.42.1__tar.gz → 0.43.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {arrayview-0.42.1 → arrayview-0.43.0}/PKG-INFO +1 -1
- {arrayview-0.42.1 → arrayview-0.43.0}/pyproject.toml +1 -1
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_diagnostics.py +18 -1
- arrayview-0.43.0/src/arrayview/_extra_ports.py +181 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_io.py +44 -8
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_launcher.py +41 -15
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_loading.py +12 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_query.py +18 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_websocket.py +159 -3
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_server.py +22 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_session.py +11 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_viewer.html +836 -34
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_vscode_browser.py +9 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_vscode_extension.py +1 -1
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_vscode_signal.py +101 -13
- arrayview-0.43.0/src/arrayview/arrayview-opener.vsix +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_api.py +7 -5
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_browser.py +125 -8
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_cli.py +10 -10
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_lifecycle_contract.py +45 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_mode_roundtrip.py +13 -6
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_nifti_series.py +3 -3
- arrayview-0.42.1/src/arrayview/arrayview-opener.vsix +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/.gitignore +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/LICENSE +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/README.md +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/ARCHITECTURE.md +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/__init__.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/__main__.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_analysis.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_app.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_config.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_dicom.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_diff.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_icon.png +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_imaging.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_instance_registry.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_launch_plan.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_launch_trace.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_lifecycle.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_overlays.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_platform.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_render.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_analysis.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_drop.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_export.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_persistence.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_preferences.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_preload.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_rendering.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_segmentation.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_state.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_routes_vectorfield.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_segmentation.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_session_spec.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_shell.html +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_source_safety.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_synthetic_mri.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_torch.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_tutorial.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_vectorfield.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_vscode.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/_watch.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/gsap.min.js +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/src/arrayview/todo.md +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/capture_patient_loading.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/capture_v_animation.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/conftest.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/lifecycle_matrix.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/make_vectorfield_test_arrays.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_1d_arrays.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_backend_shared.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_colorbar_hover_highlight.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_config.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_dicom.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_display_opener_results.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_doctor_commands.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_drop_import.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_instance_registry.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_invocation_contract_matrix.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_io.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_large_arrays.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_launch_blackbox.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_launch_diagnostics.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_launch_execution_ownership.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_launch_plan.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_launch_trace.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_loading_server.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_mode_consistency.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_mode_matrix.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_nifti_meta.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_preferences_ui.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_public_invocation_regressions.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_render_failure_reporting.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_render_progress.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_rgb_pixel_art.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_safe_server_stop.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_server_identity_status.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_session_spec.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_source_safety.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_spawn_ownership.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_torch.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_tutorial.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_tutorial_browser.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_value_formatting.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_view_handle_lifecycle.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_view_launch_execution.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_vscode_ack_protocol.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/test_window_level_preview.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/ui_audit.py +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/v_anim_frames/.gitkeep +0 -0
- {arrayview-0.42.1 → arrayview-0.43.0}/tests/visual_smoke.py +0 -0
|
@@ -75,6 +75,23 @@ _DIAGNOSES: list[tuple[str, str, str, str]] = [
|
|
|
75
75
|
"fine.",
|
|
76
76
|
_RETRY),
|
|
77
77
|
|
|
78
|
+
# ── A window that is alive but not answering ──────────────────────────
|
|
79
|
+
# Its process exists, so nothing treats it as dead and no other window may
|
|
80
|
+
# take the request from it — targeted requests are invisible to everyone
|
|
81
|
+
# else. The user sees nothing happen at all, and reloading that one window
|
|
82
|
+
# is the only recovery, so say so rather than suggesting a plain retry:
|
|
83
|
+
# retrying sends the next launch to the same stuck window.
|
|
84
|
+
(r"integrated browser open timeout|command discovery timeout|"
|
|
85
|
+
r"browser command did not return", ACTION,
|
|
86
|
+
"The VS Code window handling this request has stopped responding. It is "
|
|
87
|
+
"still running, so nothing else will take the request from it.",
|
|
88
|
+
_RELOAD + " Retrying without reloading sends the next launch to the "
|
|
89
|
+
"same window."),
|
|
90
|
+
(r"claimed but never|no progress after being claimed", ACTION,
|
|
91
|
+
"A VS Code window took this request and then went quiet, so the launch is "
|
|
92
|
+
"stuck with it.",
|
|
93
|
+
_RELOAD),
|
|
94
|
+
|
|
78
95
|
# ── Timing — slow, not broken ─────────────────────────────────────────
|
|
79
96
|
(r"did not start the viewer script", ACTION,
|
|
80
97
|
"VS Code opened an integrated-browser tab, but the tab did not navigate "
|
|
@@ -131,7 +148,7 @@ _DIAGNOSES: list[tuple[str, str, str, str]] = [
|
|
|
131
148
|
"Check the path — and that the share it lives on is mounted."),
|
|
132
149
|
(r"not a directory", FATAL,
|
|
133
150
|
"That path is not a directory.",
|
|
134
|
-
"Point --
|
|
151
|
+
"Point --match at a directory of arrays."),
|
|
135
152
|
(r"invalid vector ?field", FATAL,
|
|
136
153
|
"The vector field file could not be used with this array.",
|
|
137
154
|
"It needs the array's spatial shape plus a trailing components axis."),
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Extra listening ports on the already-running server.
|
|
2
|
+
|
|
3
|
+
Why this exists
|
|
4
|
+
---------------
|
|
5
|
+
|
|
6
|
+
In VS Code the viewer is fetched over a forwarded port. Measured 2026-08-06 on
|
|
7
|
+
the real host: a forward that VS Code has *just created* never loses a page
|
|
8
|
+
request (4/4, and 3/3 on a genuine cold start), while one that already exists
|
|
9
|
+
and has sat idle for tens of seconds drops the first two to four. A dropped
|
|
10
|
+
request never arrives — the opener can only discard the tab and open another,
|
|
11
|
+
which the user sees as the viewer flickering before the array appears.
|
|
12
|
+
|
|
13
|
+
While any viewer is open the server keeps the forward from going idle (see the
|
|
14
|
+
nudge in ``_routes_websocket``). The case that leaves is the cold one: no
|
|
15
|
+
viewer is open, so nothing has kept the forward alive, and the first launch pays.
|
|
16
|
+
|
|
17
|
+
A port that has never been forwarded does not have that problem. So a cold
|
|
18
|
+
launch is served from a fresh port, which VS Code forwards for the first time.
|
|
19
|
+
This module adds that port to the process that is *already* running, rather than
|
|
20
|
+
starting another server: same process, same loaded arrays, nothing extra to shut
|
|
21
|
+
down, and repeat opens keep using the original port.
|
|
22
|
+
|
|
23
|
+
Lifetime
|
|
24
|
+
--------
|
|
25
|
+
|
|
26
|
+
An extra port is closed as soon as no viewer is connected **on that port**.
|
|
27
|
+
Viewers report the port they arrived on, so a port is released the moment its
|
|
28
|
+
own viewer goes away, rather than waiting for every viewer everywhere to close —
|
|
29
|
+
otherwise one cold start early in a session would leave a port listening for the
|
|
30
|
+
rest of the day.
|
|
31
|
+
|
|
32
|
+
It must not be closed while a viewer loaded from it is alive: the viewer keeps
|
|
33
|
+
using HTTP on its own origin long after the page has rendered, so pulling the
|
|
34
|
+
port out from under it would break the viewer rather than tidy up after it.
|
|
35
|
+
|
|
36
|
+
A port that never receives a viewer at all — the launch failed, or the user
|
|
37
|
+
closed the tab before it loaded — is closed after ``UNUSED_GRACE_S``. Without
|
|
38
|
+
that, a failed cold start would leak a port with nothing to trigger its release.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
from __future__ import annotations
|
|
42
|
+
|
|
43
|
+
import asyncio
|
|
44
|
+
import socket
|
|
45
|
+
import time
|
|
46
|
+
|
|
47
|
+
import arrayview._session as _session_mod
|
|
48
|
+
from arrayview._session import _vprint
|
|
49
|
+
|
|
50
|
+
# Cold starts are rare — one per session in practice, because the first launch
|
|
51
|
+
# leaves a viewer open and every launch after that is warm. The cap is a guard
|
|
52
|
+
# against a pathological caller, not an expected working set.
|
|
53
|
+
MAX_EXTRA_PORTS = 3
|
|
54
|
+
|
|
55
|
+
# How long a freshly opened port may sit with no viewer on it before it is
|
|
56
|
+
# reclaimed. This is the leak guard for a launch that never produced a viewer;
|
|
57
|
+
# it has to outlast a slow array load, because the port is opened before the
|
|
58
|
+
# page is even requested and a large file can take a while to reach its socket.
|
|
59
|
+
UNUSED_GRACE_S = 180.0
|
|
60
|
+
|
|
61
|
+
# port -> {"socket", "server", "task", "viewers", "opened_at"}
|
|
62
|
+
_EXTRA_PORTS: dict[int, dict] = {}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def extra_ports() -> list[int]:
|
|
66
|
+
"""Ports currently being served in addition to the main one."""
|
|
67
|
+
return sorted(_EXTRA_PORTS)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _bind_free_port() -> socket.socket:
|
|
71
|
+
"""Bind a port the OS says is free, so nothing has to be guessed."""
|
|
72
|
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
73
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
74
|
+
sock.bind(("localhost", 0))
|
|
75
|
+
sock.listen(128)
|
|
76
|
+
return sock
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
async def open_extra_port() -> int | None:
|
|
80
|
+
"""Serve the running app on one more, never-before-used port.
|
|
81
|
+
|
|
82
|
+
Returns the port, or ``None`` if one could not be added. Never raises: a
|
|
83
|
+
launch that cannot get a fresh port is still a working launch on the main
|
|
84
|
+
port, only with the cold-start flicker it would have had anyway.
|
|
85
|
+
"""
|
|
86
|
+
if len(_EXTRA_PORTS) >= MAX_EXTRA_PORTS:
|
|
87
|
+
_vprint(
|
|
88
|
+
f"[ArrayView] not adding another port; {len(_EXTRA_PORTS)} already open"
|
|
89
|
+
)
|
|
90
|
+
return None
|
|
91
|
+
try:
|
|
92
|
+
import uvicorn
|
|
93
|
+
|
|
94
|
+
from arrayview import _server as _server_mod
|
|
95
|
+
|
|
96
|
+
sock = _bind_free_port()
|
|
97
|
+
port = sock.getsockname()[1]
|
|
98
|
+
config = uvicorn.Config(
|
|
99
|
+
_server_mod.app,
|
|
100
|
+
log_level="error",
|
|
101
|
+
timeout_keep_alive=30,
|
|
102
|
+
ws_ping_interval=None,
|
|
103
|
+
ws="websockets",
|
|
104
|
+
ws_per_message_deflate=True,
|
|
105
|
+
)
|
|
106
|
+
server = uvicorn.Server(config)
|
|
107
|
+
task = asyncio.create_task(server.serve(sockets=[sock]))
|
|
108
|
+
_EXTRA_PORTS[port] = {
|
|
109
|
+
"socket": sock,
|
|
110
|
+
"server": server,
|
|
111
|
+
"task": task,
|
|
112
|
+
"viewers": 0,
|
|
113
|
+
"opened_at": time.monotonic(),
|
|
114
|
+
}
|
|
115
|
+
asyncio.create_task(_close_if_unused(port))
|
|
116
|
+
# The socket is already listening and accepting before serve() runs, so
|
|
117
|
+
# a client that connects immediately is queued rather than refused.
|
|
118
|
+
_vprint(f"[ArrayView] serving a cold-start port on {port}")
|
|
119
|
+
return port
|
|
120
|
+
except Exception as exc: # pragma: no cover - defensive
|
|
121
|
+
_vprint(f"[ArrayView] could not add a cold-start port: {exc}")
|
|
122
|
+
return None
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def viewer_connected(port: int | None) -> None:
|
|
126
|
+
"""Note that a viewer arrived on *port*, if it is one of ours."""
|
|
127
|
+
entry = _EXTRA_PORTS.get(port)
|
|
128
|
+
if entry is not None:
|
|
129
|
+
entry["viewers"] += 1
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
async def viewer_disconnected(port: int | None) -> None:
|
|
133
|
+
"""Note a viewer left *port*, and release the port if it was the last one."""
|
|
134
|
+
entry = _EXTRA_PORTS.get(port)
|
|
135
|
+
if entry is None:
|
|
136
|
+
return
|
|
137
|
+
entry["viewers"] = max(0, entry["viewers"] - 1)
|
|
138
|
+
if entry["viewers"] == 0:
|
|
139
|
+
await _close_port(port)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
async def _close_if_unused(port: int) -> None:
|
|
143
|
+
"""Reclaim a port that never got a viewer.
|
|
144
|
+
|
|
145
|
+
Its own release is driven by the viewer that loaded from it, so a launch
|
|
146
|
+
that never produced one would otherwise leave the port listening forever.
|
|
147
|
+
"""
|
|
148
|
+
await asyncio.sleep(UNUSED_GRACE_S)
|
|
149
|
+
entry = _EXTRA_PORTS.get(port)
|
|
150
|
+
if entry is not None and entry["viewers"] == 0:
|
|
151
|
+
_vprint(f"[ArrayView] cold-start port {port} was never used")
|
|
152
|
+
await _close_port(port)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
async def _close_port(port: int) -> None:
|
|
156
|
+
entry = _EXTRA_PORTS.pop(port, None)
|
|
157
|
+
if entry is None:
|
|
158
|
+
return
|
|
159
|
+
try:
|
|
160
|
+
entry["server"].should_exit = True
|
|
161
|
+
await asyncio.wait_for(entry["task"], timeout=5)
|
|
162
|
+
except Exception:
|
|
163
|
+
entry["task"].cancel()
|
|
164
|
+
finally:
|
|
165
|
+
try:
|
|
166
|
+
entry["socket"].close()
|
|
167
|
+
except Exception:
|
|
168
|
+
pass
|
|
169
|
+
_vprint(f"[ArrayView] released cold-start port {port}")
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
async def close_extra_ports() -> None:
|
|
173
|
+
"""Release every extra port. Only safe with no viewer connected anywhere.
|
|
174
|
+
|
|
175
|
+
A backstop for the per-port release above: if a viewer is lost without its
|
|
176
|
+
disconnect being seen, this still reclaims the port once the server is idle.
|
|
177
|
+
"""
|
|
178
|
+
if not _EXTRA_PORTS or _session_mod.VIEWER_SOCKETS > 0:
|
|
179
|
+
return
|
|
180
|
+
for port in list(_EXTRA_PORTS):
|
|
181
|
+
await _close_port(port)
|
|
@@ -1139,7 +1139,7 @@ def load_dir_collection(
|
|
|
1139
1139
|
overlay match.
|
|
1140
1140
|
"""
|
|
1141
1141
|
if not base_patterns:
|
|
1142
|
-
raise ValueError("--
|
|
1142
|
+
raise ValueError("--match requires at least one positional image pattern.")
|
|
1143
1143
|
overlays = overlays or []
|
|
1144
1144
|
|
|
1145
1145
|
inferred_case_depth = None
|
|
@@ -1338,7 +1338,7 @@ def _load_file_series(path, *, load="lazy", stack="auto"):
|
|
|
1338
1338
|
|
|
1339
1339
|
One file per patient folder → 4D ``(*vol, P)``. When a folder holds
|
|
1340
1340
|
several arrays side by side there is no per-folder case to key on, so every
|
|
1341
|
-
file becomes its own case — the same collection ``--
|
|
1341
|
+
file becomes its own case — the same collection ``--match`` builds for that
|
|
1342
1342
|
directory.
|
|
1343
1343
|
|
|
1344
1344
|
If every file is NIfTI (.nii/.nii.gz), delegates to
|
|
@@ -1383,9 +1383,9 @@ def _load_file_series(path, *, load="lazy", stack="auto"):
|
|
|
1383
1383
|
|
|
1384
1384
|
# A folder holding several arrays side by side is a flat collection: one
|
|
1385
1385
|
# case per file. This used to raise and tell the user to retype the command
|
|
1386
|
-
# with --
|
|
1386
|
+
# with --match, which made "open this folder" unreachable from any caller
|
|
1387
1387
|
# that can only hand over a path (VS Code Explorer, drag-and-drop, the
|
|
1388
|
-
# /load route). Route it through the same builder --
|
|
1388
|
+
# /load route). Route it through the same builder --match uses so both
|
|
1389
1389
|
# spellings produce the same session instead of one of them failing.
|
|
1390
1390
|
if any(len(files) != 1 for files in patients.values()):
|
|
1391
1391
|
flat = sorted(fpath for files in patients.values() for fpath in files)
|
|
@@ -1445,7 +1445,7 @@ def _load_nifti_series(path, *, load="lazy", stack="auto"):
|
|
|
1445
1445
|
raise ValueError(
|
|
1446
1446
|
f"Patient folder {pdir!r} contains {len(nii_files)} NIfTI "
|
|
1447
1447
|
f"files: {[os.path.basename(f) for f in nii_files]}. "
|
|
1448
|
-
"Use --
|
|
1448
|
+
"Use --match with explicit file patterns for multi-file folders."
|
|
1449
1449
|
)
|
|
1450
1450
|
|
|
1451
1451
|
return _series_from_file_matrix(file_matrix, load=load, stack=stack)
|
|
@@ -1521,7 +1521,41 @@ def mmap_first_frame_is_cheap(arr, total_bytes) -> bool:
|
|
|
1521
1521
|
return runs <= max_runs and faulted <= fraction * total_bytes
|
|
1522
1522
|
|
|
1523
1523
|
|
|
1524
|
-
def
|
|
1524
|
+
def _read_npy_reporting_progress(mapped, progress):
|
|
1525
|
+
"""Copy a memmapped .npy into RAM in chunks, saying how far along it is.
|
|
1526
|
+
|
|
1527
|
+
For a large array this read is the whole wait between a click and the
|
|
1528
|
+
first frame — 9 s for a 1.6 GB file on a cold cache — and ``np.load``
|
|
1529
|
+
reports nothing while it runs. Copying in pieces costs the same reads
|
|
1530
|
+
and makes the wait explainable.
|
|
1531
|
+
|
|
1532
|
+
The flat view follows the array's own memory order, so the copy stays
|
|
1533
|
+
sequential whether the file is C- or Fortran-ordered. A .npy memmap is
|
|
1534
|
+
always contiguous in one of the two, so both reshapes are views.
|
|
1535
|
+
"""
|
|
1536
|
+
order = "F" if mapped.flags.f_contiguous and not mapped.flags.c_contiguous else "C"
|
|
1537
|
+
out = np.empty(mapped.shape, dtype=mapped.dtype, order=order)
|
|
1538
|
+
src = mapped.reshape(-1, order="A")
|
|
1539
|
+
dst = out.reshape(-1, order="A")
|
|
1540
|
+
total = int(src.shape[0])
|
|
1541
|
+
itemsize = int(mapped.dtype.itemsize)
|
|
1542
|
+
# ~200 updates: smooth enough to read as motion, coarse enough that the
|
|
1543
|
+
# per-chunk overhead stays lost in the read itself.
|
|
1544
|
+
step = max(1, total // 200)
|
|
1545
|
+
done = 0
|
|
1546
|
+
while done < total:
|
|
1547
|
+
end = min(done + step, total)
|
|
1548
|
+
dst[done:end] = src[done:end]
|
|
1549
|
+
done = end
|
|
1550
|
+
try:
|
|
1551
|
+
progress(done * itemsize, total * itemsize)
|
|
1552
|
+
except Exception:
|
|
1553
|
+
pass
|
|
1554
|
+
return out
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
def load_data_with_meta(filepath, key=None, *, load="lazy", stack="auto", select=None,
|
|
1558
|
+
progress=None):
|
|
1525
1559
|
"""Like load_data but also returns spatial metadata for NIfTI files.
|
|
1526
1560
|
|
|
1527
1561
|
Returns (array, meta_or_None). meta is None for non-NIfTI formats.
|
|
@@ -1547,10 +1581,10 @@ def load_data_with_meta(filepath, key=None, *, load="lazy", stack="auto", select
|
|
|
1547
1581
|
return _load_file_series(filepath, load=load, stack=stack)
|
|
1548
1582
|
if filepath.endswith(".nii") or filepath.endswith(".nii.gz"):
|
|
1549
1583
|
return _load_nifti_with_meta(filepath)
|
|
1550
|
-
return load_data(filepath, key=key), None
|
|
1584
|
+
return load_data(filepath, key=key, progress=progress), None
|
|
1551
1585
|
|
|
1552
1586
|
|
|
1553
|
-
def load_data(filepath, key=None):
|
|
1587
|
+
def load_data(filepath, key=None, *, progress=None):
|
|
1554
1588
|
if filepath.lower().endswith(".dcm"):
|
|
1555
1589
|
from ._dicom import load_dicom_series
|
|
1556
1590
|
|
|
@@ -1577,6 +1611,8 @@ def load_data(filepath, key=None):
|
|
|
1577
1611
|
if mmap_first_frame_is_cheap(mapped, size):
|
|
1578
1612
|
# Session creation upgrades this to an in-RAM array in the background.
|
|
1579
1613
|
return mapped
|
|
1614
|
+
if progress is not None:
|
|
1615
|
+
return _read_npy_reporting_progress(mapped, progress)
|
|
1580
1616
|
del mapped
|
|
1581
1617
|
return np.load(filepath)
|
|
1582
1618
|
elif filepath.endswith(".npz"):
|
|
@@ -2314,6 +2314,16 @@ def _handle_cli_spawned_daemon(
|
|
|
2314
2314
|
expected_server_id=daemon_server_id,
|
|
2315
2315
|
):
|
|
2316
2316
|
raise RuntimeError("ArrayView could not confirm no-display session cleanup")
|
|
2317
|
+
# Say what happened. This mode loads the array, proves it registers,
|
|
2318
|
+
# and then deliberately releases it so nothing is left holding the
|
|
2319
|
+
# source — which is the whole point of it, and is why no server
|
|
2320
|
+
# survives and no window opens. Printing nothing at all made a
|
|
2321
|
+
# successful check indistinguishable from silently doing nothing.
|
|
2322
|
+
print(
|
|
2323
|
+
f"[ArrayView] {name}: loaded and released. "
|
|
2324
|
+
f"No display was opened and no server is left running.",
|
|
2325
|
+
flush=True,
|
|
2326
|
+
)
|
|
2317
2327
|
except Exception:
|
|
2318
2328
|
_terminate_owned_process(daemon_proc)
|
|
2319
2329
|
_cleanup_source_staging_dirs(source_staging_dirs)
|
|
@@ -5079,7 +5089,7 @@ class _CliCollectionScanProgress:
|
|
|
5079
5089
|
|
|
5080
5090
|
|
|
5081
5091
|
def _print_dir_collection_summary(summary):
|
|
5082
|
-
print("[ArrayView] --
|
|
5092
|
+
print("[ArrayView] --match matched collection:")
|
|
5083
5093
|
print(f" cases: {len(summary['cases'])}")
|
|
5084
5094
|
print(f" spatial shape: {summary['spatial_shape']}")
|
|
5085
5095
|
print(f" image shape: {summary['shape']}")
|
|
@@ -5396,7 +5406,7 @@ def arrayview():
|
|
|
5396
5406
|
default=None,
|
|
5397
5407
|
help=(
|
|
5398
5408
|
"Segmentation mask overlay. In file mode, pass a concrete mask file. "
|
|
5399
|
-
"In --
|
|
5409
|
+
"In --match mode, pass NAME=PATTERN to add a named overlay role. "
|
|
5400
5410
|
"Repeat --overlay to load multiple overlays."
|
|
5401
5411
|
),
|
|
5402
5412
|
)
|
|
@@ -5406,7 +5416,7 @@ def arrayview():
|
|
|
5406
5416
|
action="append",
|
|
5407
5417
|
default=None,
|
|
5408
5418
|
help=(
|
|
5409
|
-
"In --
|
|
5419
|
+
"In --match mode, discover mask filenames below matching per-case "
|
|
5410
5420
|
"directories as sparse overlay roles. Missing masks render as empty. "
|
|
5411
5421
|
"Case directories are inferred automatically; --case-regex can "
|
|
5412
5422
|
"override unusual layouts. Repeat to include multiple directories."
|
|
@@ -5493,7 +5503,7 @@ def arrayview():
|
|
|
5493
5503
|
help="Display name for the array",
|
|
5494
5504
|
)
|
|
5495
5505
|
parser.add_argument(
|
|
5496
|
-
"--
|
|
5506
|
+
"--match",
|
|
5497
5507
|
action="store_true",
|
|
5498
5508
|
dest="stack_mode",
|
|
5499
5509
|
help=(
|
|
@@ -5502,6 +5512,16 @@ def arrayview():
|
|
|
5502
5512
|
"--overlay values become overlay role patterns."
|
|
5503
5513
|
),
|
|
5504
5514
|
)
|
|
5515
|
+
# --stack was renamed to --match. Without this it would not error: argparse
|
|
5516
|
+
# matches unambiguous prefixes, and --stack is a prefix of --stack-policy,
|
|
5517
|
+
# which takes an optional value — so an old command would run and quietly
|
|
5518
|
+
# ignore what the user asked for. Failing loudly is the point of it.
|
|
5519
|
+
parser.add_argument(
|
|
5520
|
+
"--stack",
|
|
5521
|
+
action="store_true",
|
|
5522
|
+
dest="stack_flag_removed",
|
|
5523
|
+
help=argparse.SUPPRESS,
|
|
5524
|
+
)
|
|
5505
5525
|
parser.add_argument(
|
|
5506
5526
|
"--series",
|
|
5507
5527
|
default=None,
|
|
@@ -5515,7 +5535,7 @@ def arrayview():
|
|
|
5515
5535
|
default=None,
|
|
5516
5536
|
dest="case_regex",
|
|
5517
5537
|
help=(
|
|
5518
|
-
"Regex used in --
|
|
5538
|
+
"Regex used in --match mode to extract a case id. Must define "
|
|
5519
5539
|
"a named (?P<case>...) group."
|
|
5520
5540
|
),
|
|
5521
5541
|
)
|
|
@@ -5523,7 +5543,7 @@ def arrayview():
|
|
|
5523
5543
|
"--dry-run",
|
|
5524
5544
|
action="store_true",
|
|
5525
5545
|
dest="dry_run",
|
|
5526
|
-
help="In --
|
|
5546
|
+
help="In --match mode, print matched cases and roles without opening a viewer.",
|
|
5527
5547
|
)
|
|
5528
5548
|
parser.add_argument(
|
|
5529
5549
|
"--stack-policy",
|
|
@@ -5532,7 +5552,7 @@ def arrayview():
|
|
|
5532
5552
|
choices=("auto", "dense", "ragged"),
|
|
5533
5553
|
dest="stack_policy",
|
|
5534
5554
|
help=(
|
|
5535
|
-
"Collection stacking policy for --
|
|
5555
|
+
"Collection stacking policy for --match mode: auto picks dense when "
|
|
5536
5556
|
"shapes agree and ragged otherwise; dense requires matching shapes; "
|
|
5537
5557
|
"ragged always keeps per-item shapes."
|
|
5538
5558
|
),
|
|
@@ -5643,19 +5663,25 @@ def arrayview():
|
|
|
5643
5663
|
f"--dims {args.dims!r} is invalid. "
|
|
5644
5664
|
"Use e.g. 'x,y,:,:' or ':,:,x,y' or '0,1'."
|
|
5645
5665
|
)
|
|
5666
|
+
if getattr(args, "stack_flag_removed", False):
|
|
5667
|
+
parser.error(
|
|
5668
|
+
"--stack has been renamed to --match, because it never controlled "
|
|
5669
|
+
"stacking: a directory of arrays is stacked either way. It selects "
|
|
5670
|
+
"which files form the collection."
|
|
5671
|
+
)
|
|
5646
5672
|
if args.dry_run and not args.stack_mode:
|
|
5647
|
-
parser.error("--dry-run requires --
|
|
5673
|
+
parser.error("--dry-run requires --match.")
|
|
5648
5674
|
excluded_cases = set()
|
|
5649
5675
|
if args.stack_mode:
|
|
5650
5676
|
if not args.files:
|
|
5651
|
-
parser.error("--
|
|
5677
|
+
parser.error("--match requires at least one image pattern.")
|
|
5652
5678
|
if args.compare or args.vectorfield or args.watch or args.rgb:
|
|
5653
5679
|
parser.error(
|
|
5654
|
-
"--
|
|
5680
|
+
"--match is incompatible with --compare, --vectorfield, "
|
|
5655
5681
|
"--watch, and --rgb."
|
|
5656
5682
|
)
|
|
5657
5683
|
if args.relay:
|
|
5658
|
-
parser.error("--
|
|
5684
|
+
parser.error("--match is incompatible with --relay.")
|
|
5659
5685
|
if args.stack_policy and not args.stack_mode:
|
|
5660
5686
|
if len(args.files) != 1:
|
|
5661
5687
|
parser.error(
|
|
@@ -5866,7 +5892,7 @@ def arrayview():
|
|
|
5866
5892
|
if mount is not None or nested_mount is not None:
|
|
5867
5893
|
_print_failure(
|
|
5868
5894
|
"network directory collections are not accessed directly. "
|
|
5869
|
-
"Copy the collection to local storage before using --
|
|
5895
|
+
"Copy the collection to local storage before using --match."
|
|
5870
5896
|
)
|
|
5871
5897
|
sys.exit(1)
|
|
5872
5898
|
if len(dir_patterns) == 1 and os.path.isdir(dir_patterns[0]):
|
|
@@ -5906,11 +5932,11 @@ def arrayview():
|
|
|
5906
5932
|
break
|
|
5907
5933
|
except MissingOverlayCasesError as e:
|
|
5908
5934
|
if not _confirm_partial_overlay_match(e):
|
|
5909
|
-
_print_failure(f"--
|
|
5935
|
+
_print_failure(f"--match could not match collection: {e}")
|
|
5910
5936
|
sys.exit(1)
|
|
5911
5937
|
excluded_cases.update(e.missing_cases)
|
|
5912
5938
|
except Exception as e:
|
|
5913
|
-
_print_failure(f"--
|
|
5939
|
+
_print_failure(f"--match could not match collection: {e}")
|
|
5914
5940
|
sys.exit(1)
|
|
5915
5941
|
_print_dir_collection_summary(summary)
|
|
5916
5942
|
if args.dry_run:
|
|
@@ -5919,7 +5945,7 @@ def arrayview():
|
|
|
5919
5945
|
from arrayview._source_safety import lexical_abspath
|
|
5920
5946
|
|
|
5921
5947
|
if args.overlay_dir:
|
|
5922
|
-
parser.error("--overlay-dir requires --
|
|
5948
|
+
parser.error("--overlay-dir requires --match.")
|
|
5923
5949
|
try:
|
|
5924
5950
|
file_overlay_specs = _normalize_file_overlay_specs(args.overlay)
|
|
5925
5951
|
except ValueError as e:
|
|
@@ -320,6 +320,14 @@ def register_loading_routes(app, *, notify_shells, setup_rgb) -> None:
|
|
|
320
320
|
PENDING_SESSION_EVENTS[sid] = pending_event
|
|
321
321
|
signature_before_load = file_signature(abs_path)
|
|
322
322
|
|
|
323
|
+
def _report_read_progress(done: int, total: int) -> None:
|
|
324
|
+
# Read from the viewer's websocket while it waits; see
|
|
325
|
+
# LOAD_PROGRESS. Kept to a plain assignment so the read loop
|
|
326
|
+
# pays nothing for it.
|
|
327
|
+
import arrayview._session as _sm
|
|
328
|
+
|
|
329
|
+
_sm.LOAD_PROGRESS[sid] = (done, total)
|
|
330
|
+
|
|
323
331
|
def _load_in_background() -> None:
|
|
324
332
|
try:
|
|
325
333
|
from ._io import load_data_with_meta
|
|
@@ -327,6 +335,7 @@ def register_loading_routes(app, *, notify_shells, setup_rgb) -> None:
|
|
|
327
335
|
data, spatial_meta = load_data_with_meta(
|
|
328
336
|
filepath,
|
|
329
337
|
select=body.get("select"),
|
|
338
|
+
progress=_report_read_progress,
|
|
330
339
|
)
|
|
331
340
|
session = Session(data, filepath=filepath, name=name)
|
|
332
341
|
session.sid = sid
|
|
@@ -369,6 +378,9 @@ def register_loading_routes(app, *, notify_shells, setup_rgb) -> None:
|
|
|
369
378
|
if staging_dir:
|
|
370
379
|
cleanup_staging_directory(staging_dir)
|
|
371
380
|
finally:
|
|
381
|
+
import arrayview._session as _sm
|
|
382
|
+
|
|
383
|
+
_sm.LOAD_PROGRESS.pop(sid, None)
|
|
372
384
|
PENDING_SESSIONS.discard(sid)
|
|
373
385
|
CANCELLED_PENDING_SESSIONS.discard(sid)
|
|
374
386
|
pending_event.set()
|
|
@@ -217,6 +217,24 @@ def register_query_routes(app, *, get_session_or_404, pil_image, pil_imageops) -
|
|
|
217
217
|
phases = journal["phases"]
|
|
218
218
|
if phase not in phases:
|
|
219
219
|
phases.append(phase)
|
|
220
|
+
# Timestamps the page's own milestones against the navigation that
|
|
221
|
+
# delivered it, so time-to-first-script can be measured per
|
|
222
|
+
# navigation rather than inferred from opener poll intervals.
|
|
223
|
+
navigation_key = journal.get("navigation_key")
|
|
224
|
+
if navigation_key:
|
|
225
|
+
try:
|
|
226
|
+
from arrayview._launch_trace import emit_route_launch_event
|
|
227
|
+
|
|
228
|
+
emit_route_launch_event(
|
|
229
|
+
"page.phase_recorded",
|
|
230
|
+
navigation_key=navigation_key,
|
|
231
|
+
tab_key=journal.get("tab_key"),
|
|
232
|
+
request_id=request_id,
|
|
233
|
+
navigation_attempt=journal.get("navigation_attempt"),
|
|
234
|
+
phase=phase,
|
|
235
|
+
)
|
|
236
|
+
except Exception:
|
|
237
|
+
pass
|
|
220
238
|
if bool(body.get("release_on_disconnect")):
|
|
221
239
|
journal["disconnect_release_grace_seconds"] = 30.0
|
|
222
240
|
return {
|