arrayview 0.58.3__py3-none-any.whl → 0.58.5__py3-none-any.whl

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/_launcher.py CHANGED
@@ -1038,6 +1038,12 @@ def _server_alive(port: int, timeout: float = 0.5) -> bool:
1038
1038
 
1039
1039
  def _server_pid(port: int) -> int | None:
1040
1040
  """Return the pid of the responding ArrayView server, or None if unreachable."""
1041
+ pids = _server_pids(port)
1042
+ return None if pids is None else pids[0]
1043
+
1044
+
1045
+ def _server_pids(port: int) -> tuple[int | None, int | None] | None:
1046
+ """Return (pid, parent pid) of the responding ArrayView server, or None."""
1041
1047
  url = f"http://{_LOOPBACK_HOST}:{port}/ping"
1042
1048
  try:
1043
1049
  with urllib.request.urlopen(url, timeout=0.5) as resp:
@@ -1045,7 +1051,7 @@ def _server_pid(port: int) -> int | None:
1045
1051
  return None
1046
1052
  payload = json.loads(resp.read().decode("utf-8"))
1047
1053
  if payload.get("ok") is True and payload.get("service") == "arrayview":
1048
- return payload.get("pid")
1054
+ return payload.get("pid"), payload.get("ppid")
1049
1055
  except Exception:
1050
1056
  pass
1051
1057
  return None
@@ -2644,7 +2650,10 @@ def _wait_for_spawned_server(
2644
2650
  return False
2645
2651
  except Exception:
2646
2652
  return False
2647
- if _server_pid(port) == expected_pid:
2653
+ # On Windows a venv's python.exe is a launcher that runs the real
2654
+ # interpreter as its child: the listener then reports *proc* as parent.
2655
+ pids = _server_pids(port)
2656
+ if pids is not None and expected_pid in pids:
2648
2657
  return True
2649
2658
  time.sleep(0.02)
2650
2659
  return False
@@ -2916,7 +2925,7 @@ _TUTORIAL_EXTRAS: dict[str, str] = {}
2916
2925
 
2917
2926
 
2918
2927
  def _register_tutorial_extras(port: int, expected_server_id: str | None = None) -> None:
2919
- """Register the tutorial's vector-field and ragged-collection sessions.
2928
+ """Register the tutorial's vector-field, MRI-head and ragged-collection sessions.
2920
2929
 
2921
2930
  Best effort: a failure here costs two sections, not the tour, so it is
2922
2931
  traced and swallowed rather than allowed to fail a launch that has already
@@ -2958,6 +2967,19 @@ def _register_tutorial_extras_impl(port, bundle, expected_server_id) -> None:
2958
2967
  except Exception as exc: # pragma: no cover - best effort
2959
2968
  _trace_launch_event("tutorial.extra_failed", extra="flow", error=str(exc))
2960
2969
 
2970
+ try:
2971
+ head = _load_session_from_filepath(
2972
+ port,
2973
+ bundle.head_file,
2974
+ "mri-head",
2975
+ expected_server_id=expected_server_id,
2976
+ )
2977
+ head_sid = head.get("sid")
2978
+ if head_sid and "error" not in head:
2979
+ _TUTORIAL_EXTRAS["head"] = str(head_sid)
2980
+ except Exception as exc: # pragma: no cover - best effort
2981
+ _trace_launch_event("tutorial.extra_failed", extra="head", error=str(exc))
2982
+
2961
2983
  try:
2962
2984
  stack = _load_session_from_filepath(
2963
2985
  port,
arrayview/_overlays.py CHANGED
@@ -9,6 +9,7 @@ from arrayview._render import (
9
9
  _composite_overlay_mask,
10
10
  mosaic_grid_cols,
11
11
  mosaic_tile_indices,
12
+ mosaic_tile_in_bounds,
12
13
  _extract_overlay_mask,
13
14
  _overlay_is_label_map,
14
15
  )
@@ -114,6 +115,8 @@ def _composite_mosaic_overlays(
114
115
  base_shape: tuple[int, ...],
115
116
  mosaic_cols: int | None = None,
116
117
  dim_z2: int = -1,
118
+ z_sel: tuple[int, int, int] | None = None,
119
+ z2_sel: tuple[int, int, int] | None = None,
117
120
  ) -> np.ndarray:
118
121
  """Composite each overlay as a mosaic using the base mosaic's frame indices.
119
122
 
@@ -135,9 +138,9 @@ def _composite_mosaic_overlays(
135
138
  else []
136
139
  )
137
140
  tile_shape = (int(base_shape[dim_y]), int(base_shape[dim_x]))
138
- tiles = mosaic_tile_indices(base_shape, dim_z, dim_z2, idx_tuple)
141
+ tiles = mosaic_tile_indices(base_shape, dim_z, dim_z2, idx_tuple, z_sel, z2_sel)
139
142
  n_frames = len(tiles)
140
- mosaic_cols = mosaic_grid_cols(base_shape, dim_z, dim_z2, mosaic_cols)
143
+ mosaic_cols = mosaic_grid_cols(base_shape, dim_z, dim_z2, mosaic_cols, z_sel)
141
144
 
142
145
  for i, sid in enumerate(sids):
143
146
  color, hidden = (
@@ -160,7 +163,7 @@ def _composite_mosaic_overlays(
160
163
  tuple(frame_idx),
161
164
  expected_shape=tile_shape,
162
165
  base_shape=base_shape,
163
- )
166
+ ) if mosaic_tile_in_bounds(base_shape, frame_idx) else None
164
167
  frames.append(
165
168
  ov_raw if ov_raw is not None else np.zeros(tile_shape, dtype=np.float32)
166
169
  )
arrayview/_render.py CHANGED
@@ -169,17 +169,62 @@ def mosaic_shape(batch):
169
169
  return tuple(mshape)
170
170
 
171
171
 
172
- def mosaic_tile_indices(shape, dim_z, dim_z2, idx):
172
+ def parse_mosaic_sel(value, n):
173
+ """``"start:stop:step"`` -> (start, stop, step) slices of a mosaic dim of size n.
174
+
175
+ None for every slice (also for empty or bad input), so a thinned and a
176
+ full request never share a cache key and a full one keys as before.
177
+ """
178
+ if value is None or value == "":
179
+ return None
180
+ try:
181
+ start, stop, step = (int(v) for v in str(value).split(":"))
182
+ except ValueError:
183
+ return None
184
+ n = int(n)
185
+ step = max(1, step)
186
+ start = min(max(0, start), max(0, n - 1))
187
+ stop = min(max(start + 1, stop), n)
188
+ if start == 0 and stop == n and step == 1:
189
+ return None
190
+ return (start, stop, step)
191
+
192
+
193
+ def mosaic_range(shape, dim, sel=None):
194
+ """Slices of *dim* a mosaic shows: all of them, or ``sel`` (start, stop, step).
195
+
196
+ A whole dim taken every few slices (start < step, stop = the end) always
197
+ has ceil(n / step) tiles, whichever slice it starts on, so the grid keeps
198
+ its shape while scrolling cycles the start; a last slot past the end is
199
+ left blank (see mosaic_tile_in_bounds).
200
+ """
201
+ if not sel:
202
+ return range(int(shape[dim]))
203
+ start, stop, step = sel
204
+ n = int(shape[dim])
205
+ if step > 1 and start < step and stop == n:
206
+ return range(start, start + -(-n // step) * step, step)
207
+ return range(*sel)
208
+
209
+
210
+ def mosaic_tile_in_bounds(shape, tile):
211
+ """False for a blank padding tile (an index past the end of its dim)."""
212
+ return all(0 <= int(v) < int(s) for v, s in zip(tile, shape))
213
+
214
+
215
+ def mosaic_tile_indices(shape, dim_z, dim_z2, idx, z_sel=None, z2_sel=None):
173
216
  """Index list of every mosaic tile, row by row.
174
217
 
175
- *dim_z* runs across the grid; *dim_z2* (``-1`` for none) runs down it,
176
- so tile ``k`` is ``dim_z = k % shape[dim_z]``, ``dim_z2 = k // shape[dim_z]``.
218
+ *dim_z* runs across the grid; *dim_z2* (``-1`` for none) runs down it.
219
+ ``z_sel`` / ``z2_sel`` (start, stop, step) thin either dim; tile ``k`` is
220
+ the ``k % cols``-th shown slice of dim_z and the ``k // cols``-th of dim_z2.
177
221
  """
178
222
  base = list(idx)
179
- rows = range(shape[dim_z2]) if dim_z2 >= 0 else (None,)
223
+ rows = mosaic_range(shape, dim_z2, z2_sel) if dim_z2 >= 0 else (None,)
224
+ cols = mosaic_range(shape, dim_z, z_sel)
180
225
  tiles = []
181
226
  for r in rows:
182
- for c in range(shape[dim_z]):
227
+ for c in cols:
183
228
  t = list(base)
184
229
  t[dim_z] = c
185
230
  if r is not None:
@@ -188,9 +233,9 @@ def mosaic_tile_indices(shape, dim_z, dim_z2, idx):
188
233
  return tiles
189
234
 
190
235
 
191
- def mosaic_grid_cols(shape, dim_z, dim_z2, mosaic_cols=None):
236
+ def mosaic_grid_cols(shape, dim_z, dim_z2, mosaic_cols=None, z_sel=None):
192
237
  """Column count a mosaic is laid out with (None = near-square)."""
193
- return int(shape[dim_z]) if dim_z2 >= 0 else mosaic_cols
238
+ return len(mosaic_range(shape, dim_z, z_sel)) if dim_z2 >= 0 else mosaic_cols
194
239
 
195
240
 
196
241
  def _build_mosaic_grid(all_data, n, gap=2, cols=None):
@@ -925,9 +970,14 @@ def render_mosaic(
925
970
  fit=None,
926
971
  dim_z2=-1,
927
972
  window_out=None,
973
+ z_sel=None,
974
+ z2_sel=None,
928
975
  ):
929
976
  """Render the mosaic grid of every slice along *dim_z* (and *dim_z2*, down).
930
977
 
978
+ ``z_sel`` / ``z2_sel`` (start, stop, step, from parse_mosaic_sel) show
979
+ only those slices of dim_z / dim_z2.
980
+
931
981
  ``fit=(w, h)`` returns the grid already shrunk to the viewer's canvas
932
982
  (as the live socket would before sending) and caches it that way, so a
933
983
  grid warmed ahead of time is ready to send: the shrink of a 240-slice
@@ -940,7 +990,7 @@ def render_mosaic(
940
990
  idx_norm[dim_z] = 0
941
991
  if dim_z2 >= 0:
942
992
  idx_norm[dim_z2] = 0
943
- key = (dim_x, dim_y, dim_z, tuple(idx_norm), colormap, dr, complex_mode, log_scale, mosaic_cols, vmin_override, vmax_override, dim_z2)
993
+ key = (dim_x, dim_y, dim_z, tuple(idx_norm), colormap, dr, complex_mode, log_scale, mosaic_cols, vmin_override, vmax_override, dim_z2, z_sel, z2_sel)
944
994
 
945
995
  windows = session.__dict__.setdefault("_mosaic_windows", {})
946
996
 
@@ -949,7 +999,7 @@ def render_mosaic(
949
999
  rgba = _build_mosaic_rgba(
950
1000
  session, dim_x, dim_y, dim_z, idx_tuple, colormap, dr, complex_mode,
951
1001
  log_scale, mosaic_cols, vmin_override, vmax_override, progress,
952
- dim_z2=dim_z2, window_out=win,
1002
+ dim_z2=dim_z2, window_out=win, z_sel=z_sel, z2_sel=z2_sel,
953
1003
  )
954
1004
  if win:
955
1005
  if len(windows) > 256:
@@ -1037,11 +1087,11 @@ def fit_rgba(rgba, canvas_w, canvas_h):
1037
1087
  def _build_mosaic_rgba(
1038
1088
  session, dim_x, dim_y, dim_z, idx_tuple, colormap, dr, complex_mode,
1039
1089
  log_scale, mosaic_cols, vmin_override, vmax_override, progress,
1040
- dim_z2=-1, window_out=None,
1090
+ dim_z2=-1, window_out=None, z_sel=None, z2_sel=None,
1041
1091
  ):
1042
- tiles = mosaic_tile_indices(session.shape, dim_z, dim_z2, idx_tuple)
1092
+ tiles = mosaic_tile_indices(session.shape, dim_z, dim_z2, idx_tuple, z_sel, z2_sel)
1043
1093
  n = len(tiles)
1044
- mosaic_cols = mosaic_grid_cols(session.shape, dim_z, dim_z2, mosaic_cols)
1094
+ mosaic_cols = mosaic_grid_cols(session.shape, dim_z, dim_z2, mosaic_cols, z_sel)
1045
1095
  # Reported per slice rather than estimated: each slice faults in roughly the
1046
1096
  # same number of pages, so slice i of n is an honest fraction of the wait.
1047
1097
  # On an array whose display axes are the outer ones this loop is the whole
@@ -1050,13 +1100,18 @@ def _build_mosaic_rgba(
1050
1100
  # cover the entire file.
1051
1101
  frames_raw = []
1052
1102
  for i, tile in enumerate(tiles):
1053
- frames_raw.append(extract_slice(session, dim_x, dim_y, tile))
1103
+ inside = mosaic_tile_in_bounds(session.shape, tile)
1104
+ frames_raw.append(extract_slice(session, dim_x, dim_y, tile) if inside else None)
1054
1105
  if progress is not None:
1055
1106
  progress(i + 1, n)
1056
- frames = [apply_complex_mode(f, complex_mode) for f in frames_raw]
1107
+ frames = [None if f is None else apply_complex_mode(f, complex_mode) for f in frames_raw]
1057
1108
  if log_scale:
1058
- frames = [np.log1p(np.abs(f)).astype(np.float32) for f in frames]
1059
- all_data = np.stack(frames)
1109
+ frames = [None if f is None else np.log1p(np.abs(f)).astype(np.float32) for f in frames]
1110
+ # Blank padding tiles: NaN, so they draw as empty grid and stay out of the window.
1111
+ shown = [f for f in frames if f is not None]
1112
+ blank = np.full(shown[0].shape, np.nan, dtype=np.float32)
1113
+ all_data = np.stack([blank if f is None else f for f in frames])
1114
+ window_data = np.stack(shown) if len(shown) < len(frames) else all_data
1060
1115
 
1061
1116
  if vmin_override is not None and vmax_override is not None:
1062
1117
  vmin, vmax = float(vmin_override), float(vmax_override)
@@ -1065,7 +1120,7 @@ def _build_mosaic_rgba(
1065
1120
  else:
1066
1121
  # One call partitions once for both ends; two calls sorted the whole
1067
1122
  # stack twice (~0.14 s of a 240-slice mosaic step).
1068
- vmin, vmax = (float(v) for v in np.percentile(all_data, [1, 99]))
1123
+ vmin, vmax = (float(v) for v in np.percentile(window_data, [1, 99]))
1069
1124
  if window_out is not None:
1070
1125
  window_out.append((vmin, vmax))
1071
1126
 
@@ -1111,15 +1166,17 @@ def _run_preload(
1111
1166
  log_scale=False,
1112
1167
  dim_z2=-1,
1113
1168
  mosaic_cols=None,
1169
+ z_sel=None,
1170
+ z2_sel=None,
1114
1171
  ):
1115
1172
  shape = session.spatial_shape if session.rgb_axis is not None else session.shape
1116
1173
  n = shape[slice_dim]
1117
1174
  H = shape[dim_y]
1118
1175
  W = shape[dim_x]
1119
1176
  if dim_z >= 0:
1120
- nz = shape[dim_z]
1177
+ nz = len(mosaic_range(shape, dim_z, z_sel))
1121
1178
  mrows, mcols = (
1122
- (shape[dim_z2], nz) if dim_z2 >= 0
1179
+ (len(mosaic_range(shape, dim_z2, z2_sel)), nz) if dim_z2 >= 0
1123
1180
  else (-(-nz // mosaic_cols), mosaic_cols) if mosaic_cols
1124
1181
  else mosaic_shape(nz)
1125
1182
  )
@@ -1153,6 +1210,8 @@ def _run_preload(
1153
1210
  log_scale,
1154
1211
  mosaic_cols=mosaic_cols,
1155
1212
  dim_z2=dim_z2,
1213
+ z_sel=z_sel,
1214
+ z2_sel=z2_sel,
1156
1215
  )
1157
1216
  else:
1158
1217
  if session.rgb_axis is not None:
@@ -1211,6 +1270,9 @@ __all__ = [
1211
1270
  "LABEL_COLORS",
1212
1271
  # Mosaic
1213
1272
  "render_mosaic",
1273
+ "parse_mosaic_sel",
1274
+ "mosaic_range",
1275
+ "mosaic_tile_in_bounds",
1214
1276
  # Preload
1215
1277
  "_run_preload",
1216
1278
  ]