SharpMeow commited on
Commit
fea4425
·
verified ·
1 Parent(s): 5136010

Scroll Lens 1.1.1

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +226 -103
  3. lens.py +1 -1
  4. viewer.js +53 -32
README.md CHANGED
@@ -57,7 +57,7 @@ in another.
57
  2. **Your own point**: pick a scan, type a point (level-0 voxels, z y x) or press *Surprise me*, then *Run the
58
  models*. A 256³ cube uses about 5 seconds of your daily ZeroGPU time (a call takes 7 to 12 s), a 128³ cube under a
59
  second. ZeroGPU starts a call only while about 21 s (256³) or 11 s (128³) of your quota is left. When it refuses,
60
- or no GPU is free, the models run on the Space's CPU on the middle 128³ of the cube instead (about 10 seconds, one
61
  such run at a time).
62
  3. **Look**: the three views are drawn in your browser, so everything answers at once. Drag a slider, or drag inside a
63
  view to move the crosshair: the other views follow as you move. The wheel or the arrow keys step one slice (Shift:
 
57
  2. **Your own point**: pick a scan, type a point (level-0 voxels, z y x) or press *Surprise me*, then *Run the
58
  models*. A 256³ cube uses about 5 seconds of your daily ZeroGPU time (a call takes 7 to 12 s), a 128³ cube under a
59
  second. ZeroGPU starts a call only while about 21 s (256³) or 11 s (128³) of your quota is left. When it refuses,
60
+ or no GPU is free, the models run on the Space's CPU on the 128³ around your point instead (about 10 seconds, one
61
  such run at a time).
62
  3. **Look**: the three views are drawn in your browser, so everything answers at once. Drag a slider, or drag inside a
63
  view to move the crosshair: the other views follow as you move. The wheel or the arrow keys step one slice (Shift:
app.py CHANGED
@@ -12,7 +12,6 @@ import base64
12
  import collections
13
  import functools
14
  import gzip
15
- import itertools
16
  import json
17
  import os
18
  import sys
@@ -134,21 +133,22 @@ def _is_gpu_refusal(exc):
134
  or "No GPU was available" in str(exc))
135
 
136
 
137
- def cpu_run(eng, cube, layers):
138
  if not CPU_SLOT.acquire(blocking=False):
139
  raise LensError("the Space's CPU is busy with another fallback run; try again in a minute, or sign in to "
140
  "Hugging Face for more ZeroGPU time")
141
  try:
142
  import torch
143
  torch.set_num_threads(max(1, (os.cpu_count() or 2) - 1)) # leave a core for the page
144
- return eng.run(_center(cube, CPU_SIZE), layers, device="cpu", tta=False)
145
  finally:
146
  CPU_SLOT.release()
147
 
148
 
149
- def run_models(cube, layers, tta, allow_cpu=True):
150
- """Run on the GPU when there is one; on ZeroGPU fall back to the CPU when the GPU is refused. Returns the result
151
- and a note for the visitor (empty when it ran as asked)."""
 
152
  eng = engine()
153
  import torch
154
  if ON_ZERO_GPU:
@@ -162,16 +162,16 @@ def run_models(cube, layers, tta, allow_cpu=True):
162
  raise
163
  if not allow_cpu:
164
  raise LensError(f"ZeroGPU refused this call: {_gpu_text(exc)[:300]} Pass allow_cpu=True to run "
165
- f"the middle {CPU_SIZE}³ on the CPU instead.") from None
166
  log(f"GPU refused ({exc}); running on the CPU")
167
  note = (f"ZeroGPU refused the run ({_gpu_text(exc).rstrip('. ')[:200]}), so the models ran on the "
168
- f"Space's CPU, on the middle {CPU_SIZE}³ voxels. ZeroGPU time comes back each day; signing in "
169
- "to Hugging Face gives more.")
170
- return cpu_run(eng, cube, layers), note
171
  device = "cuda" if torch.cuda.is_available() else "cpu"
172
  if device == "cpu" and cube.shape[0] > CPU_SIZE:
173
- return (cpu_run(eng, cube, layers),
174
- f"No GPU here, so the models ran on the CPU on the middle {CPU_SIZE}³ voxels.")
175
  return eng.run(cube, layers, device=device, tta=tta), ""
176
 
177
 
@@ -212,11 +212,24 @@ class one_run:
212
  return False
213
 
214
 
215
- def _center(cube, size):
216
- if cube.shape[0] <= size:
217
- return cube
218
- o = (cube.shape[0] - size) // 2
219
- return cube[o:o + size, o:o + size, o:o + size]
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
 
222
  # ---------------------------------------------------------------- cubes in memory
@@ -410,20 +423,32 @@ def new_view(cube, point, source, note="", title=None, request_size=None, cube_i
410
 
411
 
412
  def empty_state():
413
- return {"view": None, "pos": [], "neg": [], "rev": 0}
414
 
415
 
416
- REVS = itertools.count(1) # one increasing revision for every answer, so the viewer keeps only the newest
 
417
 
418
 
419
  def next_rev():
420
- return next(REVS)
 
 
 
 
 
421
 
422
 
423
  def cube_key(view):
424
  return f"{view['volume']}:{','.join(map(str, view['origin']))}:{view['size']}"
425
 
426
 
 
 
 
 
 
 
427
  def click_concept(state):
428
  """The visitor's concept: unit mean of the positive clicks' embeddings, and of the negative ones (or None)."""
429
  import numpy as np
@@ -483,6 +508,134 @@ def ack_only(key, clicks):
483
  "clicks": {"acked": [str(c.get("id")) for c in clicks]}})
484
 
485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
  # ---------------------------------------------------------------- drawing on the server (the agent tools)
487
 
488
  def _take(a, axis, i):
@@ -640,11 +793,12 @@ def run_cube(vid, z, y, x, size, layers, tta, allow_cpu=True):
640
  origin = lens.cube_origin(vid, point, size)
641
  ct = lens.read_cube(vid, origin, size)
642
  fg = lens.check_cube(ct)
643
- result, note = run_models(ct, layers, bool(tta), allow_cpu=allow_cpu)
 
644
  n = ran_size(result)
645
- if n != size: # the CPU fallback ran the middle of the cube
646
- origin = tuple(int(o + (size - n) // 2) for o in origin)
647
- fg = lens.foreground(_center(ct, n))
648
  src = "gpu" if str(result.get("device", "")).startswith("cuda") else "cpu"
649
  meta = lens.receipt(vid, origin, n, point, result, eng.describe(),
650
  {"source": src, "note_run": note, "foreground": round(fg, 4), "requested_size": size})
@@ -789,9 +943,9 @@ def tool_new_view(vid, origin, size, point, cube, result, source, note=""):
789
  """Everything the agent tools need about one cube: the scan cube and all arrays (kept in VIEW_CACHE, three at a
790
  time). The page keeps its cubes in CUBES instead."""
791
  import numpy as np
792
- if cube.shape[0] != size: # the CPU fallback ran a smaller cube at the center
793
- o = (size - cube.shape[0]) // 2
794
- origin = tuple(int(v + o) for v in origin)
795
  size = int(cube.shape[0])
796
  nz = cube[cube > 0]
797
  lo, hi = (np.percentile(nz, (0.5, 99.5)) if nz.size else (0, 255))
@@ -829,9 +983,9 @@ def tool_load_point(vid, z, y, x, size, layers, tta):
829
  origin = lens.cube_origin(vid, point, size)
830
  cube = lens.read_cube(vid, origin, size)
831
  lens.check_cube(cube)
832
- result, note = run_models(cube, layers, tta)
833
  src = "gpu" if result.get("device", "").startswith("cuda") else "cpu"
834
- ran = _center(cube, ran_size(result)) # the CPU fallback runs the middle of the cube
835
  return tool_new_view(vid, origin, size, point, np.ascontiguousarray(ran), result, src, note)
836
 
837
 
@@ -889,7 +1043,7 @@ def look(vid: str, z: int, y: int, x: int, size: int = 256, layers: str = "dino,
889
  layers: comma-separated models to run: dino, ink, surface.
890
  tta: average the eight mirrorings for ink and surface (8 times their GPU time).
891
  compact: keep 128 components of each embedding and 16 levels of each probability (about a quarter the size).
892
- allow_cpu: when ZeroGPU refuses, run the middle 128 cube on the Space's CPU instead of failing.
893
 
894
  Returns:
895
  The .npz file.
@@ -903,10 +1057,11 @@ def look(vid: str, z: int, y: int, x: int, size: int = 256, layers: str = "dino,
903
  origin = lens.cube_origin(vid, point, size)
904
  cube = lens.read_cube(vid, origin, size)
905
  lens.check_cube(cube)
906
- result, note = run_models(cube, layers, tta, allow_cpu=allow_cpu)
 
907
  n = ran_size(result)
908
- if n != size:
909
- origin = tuple(int(o + (size - n) // 2) for o in origin)
910
  meta = lens.receipt(vid, origin, n, point, result, ENGINE.describe(), {"source": "api", "note_run": note,
911
  "foreground": lens.foreground(cube)})
912
  if "grid" in result and "ink" in result:
@@ -1375,25 +1530,30 @@ def build_demo():
1375
  return wrapped
1376
  return deco
1377
 
1378
- def shown(state):
1379
- return [state, viewer_value(state), numbers(state)]
 
1380
 
1381
  def fresh(): # a new cube: clear the old saved files and receipt
1382
  return [None, None]
1383
 
1384
  @soft(5)
1385
  def on_run(vid, z, y, x, size, layers, tta, state, request: gr.Request):
 
1386
  with one_run(request):
1387
  view = load_point(vid, z, y, x, size, layers, tta)
1388
- state = dict(state, view=view, rev=next_rev())
1389
  return shown(state) + fresh()
1390
 
1391
  def open_gallery(cube_id, state):
1392
  if not cube_id:
1393
  raise LensError("pick a gallery cube")
1394
- state = dict(state, view=load_gallery(cube_id), rev=next_rev())
1395
- p = state["view"]["point"]
1396
- return shown(state) + fresh() + [state["view"]["volume"], p[0], p[1], p[2], state["view"]["size"], cube_id]
 
 
 
1397
 
1398
  @soft(11)
1399
  def on_gallery(cube_id, state):
@@ -1420,74 +1580,34 @@ def build_demo():
1420
  def on_viewer_select(state, evt: gr.EventData):
1421
  """Clicks from the viewer: {"key": cube, "clicks": [{"id", "index": [z, y, x], "value": "pos" or "neg"}]}.
1422
  The viewer sends every click the server has not yet acknowledged, so none is lost when two arrive while one
1423
- is being handled; the ids keep a resent click from counting twice."""
1424
  data = getattr(evt, "_data", None) or {}
1425
  key = str(data.get("key") or "")
1426
- clicks = (data.get("clicks") or [])[:64]
1427
- view = state["view"]
1428
- if (view is None or key != view["cache"]) and key.startswith("gallery:"):
1429
- try: # the session expired, or another tab moved on: the browser shows this cube
1430
- view = load_gallery(key.split(":", 1)[1])
1431
- state = dict(state, view=view, pos=[], neg=[], acked=[])
1432
- except LensError:
1433
- view = None
1434
- if view is None or key != view["cache"]:
1435
- gr.Warning("This cube is no longer in this page's session; open it again to search it.")
1436
- return [state, ack_only(key, clicks), gr.skip()]
1437
- state = dict(state, pos=list(state["pos"]), neg=list(state["neg"]))
1438
- acked = list(state.get("acked", []))
1439
- seen = set(acked)
1440
- problem = None
1441
- try:
1442
- cube = cube_of(view)
1443
- except LensError as exc: # a run cube the Space no longer holds: say so once, take no clicks
1444
- cube, problem = None, str(exc)
1445
- for click in clicks:
1446
- cid = str(click.get("id"))
1447
- if cid in seen:
1448
- continue
1449
- seen.add(cid) # acknowledged even when refused, so the viewer does not resend it
1450
- acked.append(cid)
1451
- if cube is None:
1452
- continue
1453
- if len(state["pos"]) + len(state["neg"]) >= MAX_CLICKS:
1454
- problem = f"a page keeps at most {MAX_CLICKS} clicks; clear some to add more"
1455
- continue
1456
  try:
1457
- vox = [int(v) for v in click.get("index")]
1458
- if len(vox) != 3 or not all(0 <= v < view["size"] for v in vox):
1459
- raise LensError("that click is outside the cube")
1460
- vec = cube.vector(vox)
1461
- except (TypeError, ValueError, LensError) as exc:
1462
- problem = str(exc) if isinstance(exc, LensError) else "a click needs a voxel z, y, x"
1463
- continue
1464
- kind = "neg" if click.get("value") == "neg" else "pos"
1465
- state["tick"] = state.get("tick", 0) + 1
1466
- state[kind].append({"volume": view["volume"], "cube": cube_key(view), "voxel": vox, "t": state["tick"],
1467
- "voxel_zyx": [int(v + o) for v, o in zip(vox, view["origin"])], "vec": vec})
1468
- state["acked"] = acked[-256:] # in arrival order: the newest ids stay
1469
- state["rev"] = next_rev()
1470
  if problem:
1471
  gr.Warning(problem)
1472
- if cube is None:
1473
  return [state, ack_only(key, clicks), gr.skip()]
1474
  return shown(state)
1475
 
1476
  @soft(3)
1477
- def on_undo(state):
1478
- """Forget the most recent click (More like this or Not this) on any cube."""
1479
- clicks = [(c.get("t", 0), kind, i) for kind in ("pos", "neg") for i, c in enumerate(state[kind])
1480
- if c["cube"] != "file"]
1481
- if not clicks:
1482
- raise LensError("no click to undo")
1483
- _, kind, i = max(clicks)
1484
- state = dict(state, rev=next_rev())
1485
- state[kind] = state[kind][:i] + state[kind][i + 1:]
1486
  return shown(state)
1487
 
1488
  @soft(3)
1489
- def on_clear(state):
1490
- state = dict(state, pos=[], neg=[], rev=next_rev())
 
1491
  return shown(state)
1492
 
1493
  @soft(5)
@@ -1503,8 +1623,7 @@ def build_demo():
1503
  """Load concept.npy, and concept_not.npy with it if you have one: they replace your clicks."""
1504
  paths = [p for p in (paths if isinstance(paths, list) else [paths]) if p]
1505
  if not paths: # the file was removed: drop the loaded concept, keep the clicks
1506
- state = dict(state, pos=[c for c in state["pos"] if c["cube"] != "file"],
1507
- neg=[c for c in state["neg"] if c["cube"] != "file"], rev=next_rev())
1508
  return shown(state)
1509
  pos, neg = [], []
1510
  for p in paths[:2]:
@@ -1512,12 +1631,13 @@ def build_demo():
1512
  (neg if "_not" in Path(p).name.lower() else pos).append(item)
1513
  if not pos:
1514
  raise LensError("load concept.npy too: concept_not.npy alone has nothing to look for")
1515
- state = dict(state, pos=pos, neg=neg, rev=next_rev())
1516
  return shown(state)
1517
 
1518
  @soft(2)
1519
  def on_save(state):
1520
- return save_files(state), (state["view"] or {}).get("receipt")
 
1521
 
1522
  def on_load(state, request: gr.Request):
1523
  """Open the page from its link: ?cube=ID opens a gallery cube; ?v=SCAN&z=&y=&x=&size= fills Your own point
@@ -1548,17 +1668,20 @@ def build_demo():
1548
  gr.Warning(f"This link could not be followed in full: {'; '.join(problems)}.")
1549
  if not cube_id:
1550
  return shown(state) + fields + [size, gr.skip(), tab]
 
1551
  try:
1552
- state = dict(state, view=load_gallery(cube_id), rev=next_rev())
1553
  except (LensError, OSError) as exc:
1554
  log(f"link not loaded: {exc}")
1555
  gr.Warning(f"The gallery cube could not be opened: {exc}")
1556
  return shown(state) + fields + [size, gr.skip(), tab]
 
 
1557
  if not q.get("v"):
1558
- p = state["view"]["point"]
1559
- fields = [state["view"]["volume"], p[0], p[1], p[2]]
1560
  if not q.get("size"):
1561
- size = gr.update(value=state["view"]["size"])
1562
  return shown(state) + fields + [size, cube_id, tab]
1563
 
1564
  with gr.Blocks(title="Scroll Lens", delete_cache=(1800, 3600)) as demo:
 
12
  import collections
13
  import functools
14
  import gzip
 
15
  import json
16
  import os
17
  import sys
 
133
  or "No GPU was available" in str(exc))
134
 
135
 
136
+ def cpu_run(eng, cube, layers, focus=None):
137
  if not CPU_SLOT.acquire(blocking=False):
138
  raise LensError("the Space's CPU is busy with another fallback run; try again in a minute, or sign in to "
139
  "Hugging Face for more ZeroGPU time")
140
  try:
141
  import torch
142
  torch.set_num_threads(max(1, (os.cpu_count() or 2) - 1)) # leave a core for the page
143
+ return eng.run(_window(cube, CPU_SIZE, focus), layers, device="cpu", tta=False)
144
  finally:
145
  CPU_SLOT.release()
146
 
147
 
148
+ def run_models(cube, layers, tta, allow_cpu=True, focus=None):
149
+ """Run on the GPU when there is one; on ZeroGPU fall back to the CPU when the GPU is refused, on the CPU_SIZE
150
+ window around `focus` (the point, in cube voxels; see window_offset). Returns the result and a note for the
151
+ visitor (empty when it ran as asked)."""
152
  eng = engine()
153
  import torch
154
  if ON_ZERO_GPU:
 
162
  raise
163
  if not allow_cpu:
164
  raise LensError(f"ZeroGPU refused this call: {_gpu_text(exc)[:300]} Pass allow_cpu=True to run "
165
+ f"the {CPU_SIZE}³ around the point on the CPU instead.") from None
166
  log(f"GPU refused ({exc}); running on the CPU")
167
  note = (f"ZeroGPU refused the run ({_gpu_text(exc).rstrip('. ')[:200]}), so the models ran on the "
168
+ f"Space's CPU, on the {CPU_SIZE}³ voxels around your point. ZeroGPU time comes back each day; "
169
+ "signing in to Hugging Face gives more.")
170
+ return cpu_run(eng, cube, layers, focus), note
171
  device = "cuda" if torch.cuda.is_available() else "cpu"
172
  if device == "cpu" and cube.shape[0] > CPU_SIZE:
173
+ return (cpu_run(eng, cube, layers, focus),
174
+ f"No GPU here, so the models ran on the CPU on the {CPU_SIZE}³ voxels around your point.")
175
  return eng.run(cube, layers, device=device, tta=tta), ""
176
 
177
 
 
212
  return False
213
 
214
 
215
+ def window_offset(size, n, focus=None):
216
+ """Where the CPU fallback's n³ window sits in a size³ cube: around `focus` (the point, in cube voxels), on the
217
+ 8-voxel patch grid and inside the cube, so the point is always in it (near a scan's edge the cube is not centered
218
+ on the point); the middle when no point is given."""
219
+ if n >= size:
220
+ return (0, 0, 0)
221
+ if focus is None:
222
+ return ((size - n) // 2,) * 3
223
+ return tuple(int(min(max((int(f) - n // 2) // lens.PATCH * lens.PATCH, 0), size - n)) for f in focus)
224
+
225
+
226
+ def _window(cube, n, focus=None):
227
+ o = window_offset(cube.shape[0], n, focus)
228
+ return cube[o[0]:o[0] + n, o[1]:o[1] + n, o[2]:o[2] + n]
229
+
230
+
231
+ def _local(point, origin):
232
+ return tuple(int(p) - int(o) for p, o in zip(point, origin))
233
 
234
 
235
  # ---------------------------------------------------------------- cubes in memory
 
423
 
424
 
425
  def empty_state():
426
+ return {"view": None, "pos": [], "neg": [], "acked": [], "keys": [], "view_ticket": 0, "rev": 0}
427
 
428
 
429
+ _REV_LOCK = threading.Lock()
430
+ _LAST_REV = [0]
431
 
432
 
433
  def next_rev():
434
+ """A number larger than every one before, also across restarts of the Space (microseconds since 1970, below
435
+ 2**53 so the browser reads it exactly). Every answer to the viewer carries one, and the viewer shows only the
436
+ newest; opens take one as their ticket (show_view)."""
437
+ with _REV_LOCK:
438
+ _LAST_REV[0] = max(_LAST_REV[0] + 1, time.time_ns() // 1000)
439
+ return _LAST_REV[0]
440
 
441
 
442
  def cube_key(view):
443
  return f"{view['volume']}:{','.join(map(str, view['origin']))}:{view['size']}"
444
 
445
 
446
+ def place_of(cube):
447
+ """cube_key for a cube in CUBES (its scan, origin and size), for clicks on a cube no longer on screen."""
448
+ origin = [int(v) for v in cube.meta["cube_origin_zyx"]]
449
+ return f"{cube.meta['volume']}:{','.join(map(str, origin))}:{cube.size}"
450
+
451
+
452
  def click_concept(state):
453
  """The visitor's concept: unit mean of the positive clicks' embeddings, and of the negative ones (or None)."""
454
  import numpy as np
 
508
  "clicks": {"acked": [str(c.get("id")) for c in clicks]}})
509
 
510
 
511
+ # ---------------------------------------------------------------- a visitor's page state, changed in place
512
+ #
513
+ # Gradio hands every event of one visitor the same state dict, and keeps whatever the event returns. An event that
514
+ # rebuilt the dict from what it read when it started would put that old copy back when it finished late: clicks made
515
+ # during a 20-second model run would be lost, and a click answered after the visitor opened another cube would bring
516
+ # the old cube back. So the page events change that one dict in place, under STATE_LOCK, only once their slow part
517
+ # (reading a cube, the models) is done, and the viewer orders their answers by revision.
518
+
519
+ STATE_LOCK = threading.RLock()
520
+ KEEP_KEYS = 16 # the cubes a page has shown that its clicks may still name
521
+ KEEP_ACKS = 256
522
+
523
+
524
+ def snapshot(state, rev=True):
525
+ """A copy of the visitor's state to draw from (viewer_value, numbers, save_files), with a new revision."""
526
+ with STATE_LOCK:
527
+ if rev:
528
+ state["rev"] = next_rev()
529
+ return dict(state, pos=list(state["pos"]), neg=list(state["neg"]), acked=list(state.get("acked", [])),
530
+ keys=list(state.get("keys", [])))
531
+
532
+
533
+ def show_view(state, view, ticket, force=False):
534
+ """Put `view` on screen unless the visitor has since opened another cube (a later ticket, from next_rev when
535
+ the open began). A finished model run always shows (`force`): it spent the visitor's GPU time. Returns whether
536
+ `view` is on screen now."""
537
+ with STATE_LOCK:
538
+ if not force and ticket < state.get("view_ticket", 0):
539
+ return False
540
+ state["view"] = view
541
+ state["view_ticket"] = max(ticket, state.get("view_ticket", 0))
542
+ state["keys"] = ([k for k in state.get("keys", []) if k != view["cache"]] + [view["cache"]])[-KEEP_KEYS:]
543
+ return True
544
+
545
+
546
+ def _ack(state, ids):
547
+ acked = list(state.get("acked", []))
548
+ seen = set(acked)
549
+ acked += [i for i in dict.fromkeys(str(i) for i in ids) if i not in seen]
550
+ state["acked"] = acked[-KEEP_ACKS:] # in arrival order: the newest ids stay
551
+
552
+
553
+ def take_clicks(state, key, clicks):
554
+ """Add the viewer's clicks on the cube `key` (a CUBES key) to the visitor's concept. The viewer resends every
555
+ click until it is acknowledged, so the ids stop a click counting twice; a click on a cube this page has moved on
556
+ from still counts (a concept spans cubes) but never brings that cube back. Returns (taken, problem): whether the
557
+ clicks could be used, and why some were refused."""
558
+ with STATE_LOCK:
559
+ view = state["view"]
560
+ known = key in state.get("keys", []) or (view is not None and key == view["cache"])
561
+ if not known:
562
+ with STATE_LOCK:
563
+ _ack(state, [c.get("id") for c in clicks])
564
+ return False, "This cube is no longer in this page's session; open it again to search it."
565
+ try:
566
+ cube = CUBES.get(key) # a gallery cube reloads from disk; a run cube may have dropped out
567
+ except LensError as exc:
568
+ with STATE_LOCK:
569
+ _ack(state, [c.get("id") for c in clicks])
570
+ return False, str(exc)
571
+ place, problem = place_of(cube), None
572
+ with STATE_LOCK:
573
+ seen = set(state.get("acked", []))
574
+ for click in clicks:
575
+ cid = str(click.get("id"))
576
+ if cid in seen:
577
+ continue
578
+ seen.add(cid)
579
+ _ack(state, [cid]) # acknowledged even when refused, so the viewer does not resend it
580
+ if len(state["pos"]) + len(state["neg"]) >= MAX_CLICKS:
581
+ problem = f"a page keeps at most {MAX_CLICKS} clicks; clear some to add more"
582
+ continue
583
+ try:
584
+ vox = [int(v) for v in click.get("index")]
585
+ if len(vox) != 3 or not all(0 <= v < cube.size for v in vox):
586
+ raise LensError("that click is outside the cube")
587
+ vec = cube.vector(vox)
588
+ except (TypeError, ValueError, LensError) as exc:
589
+ problem = str(exc) if isinstance(exc, LensError) else "a click needs a voxel z, y, x"
590
+ continue
591
+ kind = "neg" if click.get("value") == "neg" else "pos"
592
+ origin = [int(v) for v in cube.meta["cube_origin_zyx"]]
593
+ state["tick"] = state.get("tick", 0) + 1
594
+ state[kind] = state[kind] + [{"id": cid, "volume": cube.meta["volume"], "cube": place, "voxel": vox,
595
+ "t": state["tick"], "voxel_zyx": [v + o for v, o in zip(vox, origin)],
596
+ "vec": vec}]
597
+ return True, problem
598
+
599
+
600
+ def undo_click(state, cid=None):
601
+ """Forget the click with id `cid` (one still on its way is dropped when it arrives), or else the most recent
602
+ click (More like this or Not this) on any cube."""
603
+ with STATE_LOCK:
604
+ if cid:
605
+ cid = str(cid)
606
+ for kind in ("pos", "neg"):
607
+ if any(c.get("id") == cid for c in state[kind]):
608
+ state[kind] = [c for c in state[kind] if c.get("id") != cid]
609
+ return
610
+ _ack(state, [cid])
611
+ return
612
+ clicks = [(c.get("t", 0), kind, i) for kind in ("pos", "neg") for i, c in enumerate(state[kind])
613
+ if c["cube"] != "file"]
614
+ if not clicks:
615
+ raise LensError("no click to undo")
616
+ _, kind, i = max(clicks)
617
+ state[kind] = state[kind][:i] + state[kind][i + 1:]
618
+
619
+
620
+ def clear_clicks(state, ids=()):
621
+ """Forget every click and loaded concept; `ids` are clicks the viewer sent but has no answer for yet, dropped
622
+ when they arrive."""
623
+ with STATE_LOCK:
624
+ state["pos"], state["neg"] = [], []
625
+ _ack(state, list(ids)[:KEEP_ACKS])
626
+
627
+
628
+ def set_concept(state, pos, neg):
629
+ """Replace the visitor's clicks with a loaded concept.npy (and concept_not.npy), or with `pos` and `neg` None
630
+ drop the loaded files and keep the clicks."""
631
+ with STATE_LOCK:
632
+ if pos is None:
633
+ state["pos"] = [c for c in state["pos"] if c["cube"] != "file"]
634
+ state["neg"] = [c for c in state["neg"] if c["cube"] != "file"]
635
+ else:
636
+ state["pos"], state["neg"] = list(pos), list(neg)
637
+
638
+
639
  # ---------------------------------------------------------------- drawing on the server (the agent tools)
640
 
641
  def _take(a, axis, i):
 
793
  origin = lens.cube_origin(vid, point, size)
794
  ct = lens.read_cube(vid, origin, size)
795
  fg = lens.check_cube(ct)
796
+ focus = _local(point, origin)
797
+ result, note = run_models(ct, layers, bool(tta), allow_cpu=allow_cpu, focus=focus)
798
  n = ran_size(result)
799
+ if n != size: # the CPU fallback ran the window around the point
800
+ origin = tuple(int(o + d) for o, d in zip(origin, window_offset(size, n, focus)))
801
+ fg = lens.foreground(_window(ct, n, focus))
802
  src = "gpu" if str(result.get("device", "")).startswith("cuda") else "cpu"
803
  meta = lens.receipt(vid, origin, n, point, result, eng.describe(),
804
  {"source": src, "note_run": note, "foreground": round(fg, 4), "requested_size": size})
 
943
  """Everything the agent tools need about one cube: the scan cube and all arrays (kept in VIEW_CACHE, three at a
944
  time). The page keeps its cubes in CUBES instead."""
945
  import numpy as np
946
+ if cube.shape[0] != size: # the CPU fallback ran a smaller cube around the point
947
+ off = window_offset(size, cube.shape[0], _local(point, origin))
948
+ origin = tuple(int(v + d) for v, d in zip(origin, off))
949
  size = int(cube.shape[0])
950
  nz = cube[cube > 0]
951
  lo, hi = (np.percentile(nz, (0.5, 99.5)) if nz.size else (0, 255))
 
983
  origin = lens.cube_origin(vid, point, size)
984
  cube = lens.read_cube(vid, origin, size)
985
  lens.check_cube(cube)
986
+ result, note = run_models(cube, layers, tta, focus=_local(point, origin))
987
  src = "gpu" if result.get("device", "").startswith("cuda") else "cpu"
988
+ ran = _window(cube, ran_size(result), _local(point, origin)) # the CPU fallback runs around the point
989
  return tool_new_view(vid, origin, size, point, np.ascontiguousarray(ran), result, src, note)
990
 
991
 
 
1043
  layers: comma-separated models to run: dino, ink, surface.
1044
  tta: average the eight mirrorings for ink and surface (8 times their GPU time).
1045
  compact: keep 128 components of each embedding and 16 levels of each probability (about a quarter the size).
1046
+ allow_cpu: when ZeroGPU refuses, run the 128 cube around the point on the Space's CPU instead of failing.
1047
 
1048
  Returns:
1049
  The .npz file.
 
1057
  origin = lens.cube_origin(vid, point, size)
1058
  cube = lens.read_cube(vid, origin, size)
1059
  lens.check_cube(cube)
1060
+ focus = _local(point, origin)
1061
+ result, note = run_models(cube, layers, tta, allow_cpu=allow_cpu, focus=focus)
1062
  n = ran_size(result)
1063
+ if n != size: # the CPU fallback ran the window around the point
1064
+ origin = tuple(int(o + d) for o, d in zip(origin, window_offset(size, n, focus)))
1065
  meta = lens.receipt(vid, origin, n, point, result, ENGINE.describe(), {"source": "api", "note_run": note,
1066
  "foreground": lens.foreground(cube)})
1067
  if "grid" in result and "ink" in result:
 
1530
  return wrapped
1531
  return deco
1532
 
1533
+ def shown(state): # the visitor's own state dict, and what the viewer and the text show of it
1534
+ snap = snapshot(state)
1535
+ return [state, viewer_value(snap), numbers(snap)]
1536
 
1537
  def fresh(): # a new cube: clear the old saved files and receipt
1538
  return [None, None]
1539
 
1540
  @soft(5)
1541
  def on_run(vid, z, y, x, size, layers, tta, state, request: gr.Request):
1542
+ ticket = next_rev()
1543
  with one_run(request):
1544
  view = load_point(vid, z, y, x, size, layers, tta)
1545
+ show_view(state, view, ticket, force=True)
1546
  return shown(state) + fresh()
1547
 
1548
  def open_gallery(cube_id, state):
1549
  if not cube_id:
1550
  raise LensError("pick a gallery cube")
1551
+ ticket = next_rev()
1552
+ view = load_gallery(cube_id)
1553
+ if not show_view(state, view, ticket):
1554
+ return [gr.skip()] * 11 # the visitor opened another cube meanwhile, and that one stays
1555
+ p = view["point"]
1556
+ return shown(state) + fresh() + [view["volume"], p[0], p[1], p[2], view["size"], cube_id]
1557
 
1558
  @soft(11)
1559
  def on_gallery(cube_id, state):
 
1580
  def on_viewer_select(state, evt: gr.EventData):
1581
  """Clicks from the viewer: {"key": cube, "clicks": [{"id", "index": [z, y, x], "value": "pos" or "neg"}]}.
1582
  The viewer sends every click the server has not yet acknowledged, so none is lost when two arrive while one
1583
+ is being handled (take_clicks)."""
1584
  data = getattr(evt, "_data", None) or {}
1585
  key = str(data.get("key") or "")
1586
+ clicks = [c for c in (data.get("clicks") or [])[:64] if isinstance(c, dict)]
1587
+ if state["view"] is None and key.startswith("gallery:"):
1588
+ ticket = next_rev() # the session expired while the browser kept showing this gallery cube
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1589
  try:
1590
+ show_view(state, load_gallery(key.split(":", 1)[1]), ticket)
1591
+ except LensError:
1592
+ pass
1593
+ taken, problem = take_clicks(state, key, clicks)
 
 
 
 
 
 
 
 
 
1594
  if problem:
1595
  gr.Warning(problem)
1596
+ if not taken:
1597
  return [state, ack_only(key, clicks), gr.skip()]
1598
  return shown(state)
1599
 
1600
  @soft(3)
1601
+ def on_undo(state, evt: gr.EventData):
1602
+ """Forget a click: the one the viewer names (it may still be on its way), else the most recent on any cube."""
1603
+ data = getattr(evt, "_data", None) or {}
1604
+ undo_click(state, data.get("id"))
 
 
 
 
 
1605
  return shown(state)
1606
 
1607
  @soft(3)
1608
+ def on_clear(state, evt: gr.EventData):
1609
+ data = getattr(evt, "_data", None) or {}
1610
+ clear_clicks(state, [str(i) for i in (data.get("ids") or []) if isinstance(i, (str, int))])
1611
  return shown(state)
1612
 
1613
  @soft(5)
 
1623
  """Load concept.npy, and concept_not.npy with it if you have one: they replace your clicks."""
1624
  paths = [p for p in (paths if isinstance(paths, list) else [paths]) if p]
1625
  if not paths: # the file was removed: drop the loaded concept, keep the clicks
1626
+ set_concept(state, None, None)
 
1627
  return shown(state)
1628
  pos, neg = [], []
1629
  for p in paths[:2]:
 
1631
  (neg if "_not" in Path(p).name.lower() else pos).append(item)
1632
  if not pos:
1633
  raise LensError("load concept.npy too: concept_not.npy alone has nothing to look for")
1634
+ set_concept(state, pos, neg)
1635
  return shown(state)
1636
 
1637
  @soft(2)
1638
  def on_save(state):
1639
+ snap = snapshot(state, rev=False)
1640
+ return save_files(snap), (snap["view"] or {}).get("receipt")
1641
 
1642
  def on_load(state, request: gr.Request):
1643
  """Open the page from its link: ?cube=ID opens a gallery cube; ?v=SCAN&z=&y=&x=&size= fills Your own point
 
1668
  gr.Warning(f"This link could not be followed in full: {'; '.join(problems)}.")
1669
  if not cube_id:
1670
  return shown(state) + fields + [size, gr.skip(), tab]
1671
+ ticket = next_rev()
1672
  try:
1673
+ view = load_gallery(cube_id)
1674
  except (LensError, OSError) as exc:
1675
  log(f"link not loaded: {exc}")
1676
  gr.Warning(f"The gallery cube could not be opened: {exc}")
1677
  return shown(state) + fields + [size, gr.skip(), tab]
1678
+ if not show_view(state, view, ticket): # the visitor already opened another cube
1679
+ return [gr.skip()] * 3 + fields + [size, gr.skip(), tab]
1680
  if not q.get("v"):
1681
+ p = view["point"]
1682
+ fields = [view["volume"], p[0], p[1], p[2]]
1683
  if not q.get("size"):
1684
+ size = gr.update(value=view["size"])
1685
  return shown(state) + fields + [size, cube_id, tab]
1686
 
1687
  with gr.Blocks(title="Scroll Lens", delete_cache=(1800, 3600)) as demo:
lens.py CHANGED
@@ -40,7 +40,7 @@ import urllib.request
40
  from pathlib import Path
41
 
42
  HERE = Path(__file__).resolve().parent
43
- VERSION = "1.1.0"
44
  BUCKET = "https://vesuvius-challenge-open-data.s3.us-east-1.amazonaws.com"
45
  CHUNK = 128 # every listed scan is stored as uncompressed uint8 chunks of 128^3 voxels
46
  SIZES = (128, 256) # cube sides in voxels; 256 is the models' training patch, 128 fits a CPU
 
40
  from pathlib import Path
41
 
42
  HERE = Path(__file__).resolve().parent
43
+ VERSION = "1.1.1"
44
  BUCKET = "https://vesuvius-challenge-open-data.s3.us-east-1.amazonaws.com"
45
  CHUNK = 128 # every listed scan is stored as uncompressed uint8 chunks of 128^3 voxels
46
  SIZES = (128, 256) # cube sides in voxels; 256 is the models' training patch, 128 fits a CPU
viewer.js CHANGED
@@ -32,7 +32,8 @@ const st = {
32
  lo: 0, hi: 255, autoLo: 0, autoHi: 255, bright: 0, contrast: 0,
33
  slices: [0, 0, 0], on: {}, mode: 'move', opacity: 0.7, top: 8,
34
  refThr: 0, refTop: 1, clkThr: 0, clkTop: 1,
35
- marks: {pos: [], neg: []}, pending: [], inflight: null, clickSeq: 0, loading: 0, linkSlices: null,
 
36
  };
37
  const PAGE_ID = Math.random().toString(36).slice(2, 8);
38
  const MODE_NAME = `slv-mode-${PAGE_ID}`; // one radio group for the three click modes
@@ -241,7 +242,7 @@ function paint(a) {
241
  // the clicks made in this cube whose patch lies in this slice
242
  const tok = st.slices[a] >> 3;
243
  for (const kind of ['pos', 'neg']) {
244
- const list = st.marks[kind].concat(st.pending.filter(m => m.kind === kind).map(m => m.voxel));
245
  for (const vox of list) {
246
  if ((vox[a] >> 3) !== tok) continue;
247
  const x = ox + (vox[c] + 0.5) * sc, y = oy + (vox[r] + 0.5) * sc, h = 6 * dpr + 2;
@@ -298,15 +299,16 @@ function chunkRange(o, size) {
298
  return out;
299
  }
300
 
301
- async function fetchRetry(url, tries = 3) {
302
  let last;
303
  for (let t = 0; t < tries; t++) {
304
  try {
305
- const resp = await fetch(url);
306
  if (resp.status === 404) return null; // no chunk stored there: all outside the scroll (403 is a refusal)
307
  if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
308
  return await resp.arrayBuffer();
309
  } catch (e) {
 
310
  last = e;
311
  await new Promise(res => setTimeout(res, 800 * (t + 1)));
312
  }
@@ -314,7 +316,7 @@ async function fetchRetry(url, tries = 3) {
314
  throw last;
315
  }
316
 
317
- async function loadCT(m, token) {
318
  const S = m.size, [oz, oy, ox] = m.origin;
319
  const ckey = `${m.volume}:${m.origin.join(',')}:${S}`;
320
  if (ctCache.has(ckey)) { const ct = ctCache.get(ckey); ctCache.delete(ckey); ctCache.set(ckey, ct); return ct; }
@@ -323,7 +325,7 @@ async function loadCT(m, token) {
323
  for (const cz of chunkRange(oz, S)) for (const cy of chunkRange(oy, S)) for (const cx of chunkRange(ox, S)) keys.push([cz, cy, cx]);
324
  let done = 0;
325
  const one = async key => {
326
- const buf = await fetchRetry(`${m.ct.base}/${key.join('/')}`);
327
  if (token !== st.loading) return;
328
  done += 1;
329
  setStatus(`Reading the scan from the open-data bucket: ${done} of ${keys.length} chunks`, done / keys.length);
@@ -364,9 +366,9 @@ function autoWindow(ct) {
364
  return [lo, Math.max(hi, lo + 1)];
365
  }
366
 
367
- async function loadLayers(m) {
368
  if (!m.data) return {};
369
- const resp = await fetch(m.data);
370
  if (!resp.ok) throw new Error(`model layers: HTTP ${resp.status}`);
371
  let buf = await resp.arrayBuffer();
372
  const head = new Uint8Array(buf, 0, 2);
@@ -411,58 +413,76 @@ function linkSlicesOnce() {
411
  }
412
  st.linkSlices = linkSlicesOnce();
413
 
 
 
 
414
  async function onValue(raw) {
415
  let m = null;
416
  try { m = raw ? JSON.parse(raw) : null; } catch (e) { m = null; }
417
  if (!m || !m.key) return;
418
- if (m.ack_only) { // clicks the server could not use: stop resending them
419
- if (m.key === st.key) {
420
- const acked = new Set((m.clicks && m.clicks.acked) || []);
421
- st.pending = st.pending.filter(p => !acked.has(p.id));
422
- if (st.inflight) { clearTimeout(st.inflight); st.inflight = null; }
423
- st.flushTries = 0;
424
- redraw([0, 1, 2], true);
425
- }
426
  return;
427
  }
 
428
  if (m.key !== st.key || st.failed) {
429
  await newCube(m);
430
- } else if (m.rev > st.rev) {
431
  applyClicks(m);
432
  redraw();
433
  }
434
  }
435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  function applyClicks(m) {
437
  st.meta = m;
438
  st.rev = m.rev;
439
  st.clk = decodeSim(m.clicks && m.clicks.sim);
440
  st.marks = {pos: (m.clicks && m.clicks.pos) || [], neg: (m.clicks && m.clicks.neg) || []};
441
- const acked = new Set((m.clicks && m.clicks.acked) || []);
442
- st.pending = st.pending.filter(p => !acked.has(p.id));
443
- st.flushTries = 0;
444
- if (st.inflight) { clearTimeout(st.inflight); st.inflight = null; }
445
  updateThresholds();
446
  flushClicks();
447
  }
448
 
449
- // Clicks go to Python one request at a time, each carrying every click not yet acknowledged, so a click made while
450
- // another is being handled is sent next instead of being dropped; the ids stop a resent click counting twice.
 
451
  function flushClicks() {
452
- if (st.inflight || !st.pending.length || !st.key) return;
453
  if ((st.flushTries = (st.flushTries || 0) + 1) > 3) {
454
  st.pending = [];
 
455
  st.flushTries = 0;
456
  setStatus('The Space did not answer these clicks; try again in a moment.', null);
457
  redraw([0, 1, 2], true);
458
  return;
459
  }
 
 
 
 
460
  st.inflight = setTimeout(() => { st.inflight = null; flushClicks(); }, 15000);
461
- trigger('select', {key: st.key, clicks: st.pending.map(p => ({id: p.id, index: p.voxel, value: p.kind}))});
462
  }
463
 
464
  async function newCube(m) {
465
  const token = ++st.loading;
 
 
466
  const S = m.size;
467
  const same = st.size === S;
468
  st.key = m.key;
@@ -470,8 +490,6 @@ async function newCube(m) {
470
  st.n = S / PATCH;
471
  st.ct = st.ink = st.surface = st.pca = st.ref = null;
472
  st.failed = false;
473
- st.pending = [];
474
- st.rev = -1;
475
  applyClicks(m);
476
  const local = m.point.map((p, i) => Math.min(S - 1, Math.max(0, p - m.origin[i])));
477
  const link = st.linkSlices;
@@ -488,7 +506,7 @@ async function newCube(m) {
488
  redraw(); // black views, not the previous cube, while this one loads
489
  setStatus(`Loading ${m.title || m.volume}...`, 0);
490
  try {
491
- const [ct, layers] = await Promise.all([loadCT(m, token), loadLayers(m)]);
492
  if (token !== st.loading || !ct) return;
493
  st.ct = ct;
494
  Object.assign(st, {ink: layers.ink || null, surface: layers.surface || null, pca: layers.pca || null,
@@ -548,11 +566,14 @@ element.querySelectorAll('button[data-act]').forEach(btn => btn.addEventListener
548
  } else if (act === 'auto') {
549
  setSlider('bright', 0); setSlider('contrast', 0); windowLevel(); redraw();
550
  } else if (act === 'undo') {
551
- if (st.pending.length) { st.pending.pop(); redraw([0, 1, 2], true); }
552
- else trigger('undo', {key: st.key});
 
 
553
  } else if (act === 'clear') {
 
554
  st.pending = [];
555
- trigger('clear', {key: st.key});
556
  } else if (act === 'use' && m) {
557
  trigger('apply', {volume: m.volume, point: st.slices.map((s, i) => s + m.origin[i])});
558
  flash(btn, 'Copied to Your own point');
@@ -655,7 +676,7 @@ function sendClick(kind, vox) {
655
  setStatus('This cube has no 3D DINO embeddings, so it cannot be searched. Run it with the 3D DINO model.', null);
656
  return;
657
  }
658
- st.pending.push({id: `${PAGE_ID}-${++st.clickSeq}`, kind, voxel: vox});
659
  if (kind === 'pos' && !st.on.clicks) {
660
  st.on.clicks = true;
661
  element.querySelector('input[data-layer="clicks"]').checked = true;
 
32
  lo: 0, hi: 255, autoLo: 0, autoHi: 255, bright: 0, contrast: 0,
33
  slices: [0, 0, 0], on: {}, mode: 'move', opacity: 0.7, top: 8,
34
  refThr: 0, refTop: 1, clkThr: 0, clkTop: 1,
35
+ marks: {pos: [], neg: []}, pending: [], inflight: null, sent: [], clickSeq: 0, loading: 0, linkSlices: null,
36
+ maxRev: 0, abort: null,
37
  };
38
  const PAGE_ID = Math.random().toString(36).slice(2, 8);
39
  const MODE_NAME = `slv-mode-${PAGE_ID}`; // one radio group for the three click modes
 
242
  // the clicks made in this cube whose patch lies in this slice
243
  const tok = st.slices[a] >> 3;
244
  for (const kind of ['pos', 'neg']) {
245
+ const list = st.marks[kind].concat(st.pending.filter(m => m.kind === kind && m.key === st.key).map(m => m.voxel));
246
  for (const vox of list) {
247
  if ((vox[a] >> 3) !== tok) continue;
248
  const x = ox + (vox[c] + 0.5) * sc, y = oy + (vox[r] + 0.5) * sc, h = 6 * dpr + 2;
 
299
  return out;
300
  }
301
 
302
+ async function fetchRetry(url, signal, tries = 3) {
303
  let last;
304
  for (let t = 0; t < tries; t++) {
305
  try {
306
+ const resp = await fetch(url, {signal});
307
  if (resp.status === 404) return null; // no chunk stored there: all outside the scroll (403 is a refusal)
308
  if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
309
  return await resp.arrayBuffer();
310
  } catch (e) {
311
+ if (signal && signal.aborted) throw e; // another cube was picked: stop, do not retry
312
  last = e;
313
  await new Promise(res => setTimeout(res, 800 * (t + 1)));
314
  }
 
316
  throw last;
317
  }
318
 
319
+ async function loadCT(m, token, signal) {
320
  const S = m.size, [oz, oy, ox] = m.origin;
321
  const ckey = `${m.volume}:${m.origin.join(',')}:${S}`;
322
  if (ctCache.has(ckey)) { const ct = ctCache.get(ckey); ctCache.delete(ckey); ctCache.set(ckey, ct); return ct; }
 
325
  for (const cz of chunkRange(oz, S)) for (const cy of chunkRange(oy, S)) for (const cx of chunkRange(ox, S)) keys.push([cz, cy, cx]);
326
  let done = 0;
327
  const one = async key => {
328
+ const buf = await fetchRetry(`${m.ct.base}/${key.join('/')}`, signal);
329
  if (token !== st.loading) return;
330
  done += 1;
331
  setStatus(`Reading the scan from the open-data bucket: ${done} of ${keys.length} chunks`, done / keys.length);
 
366
  return [lo, Math.max(hi, lo + 1)];
367
  }
368
 
369
+ async function loadLayers(m, signal) {
370
  if (!m.data) return {};
371
+ const resp = await fetch(m.data, {signal});
372
  if (!resp.ok) throw new Error(`model layers: HTTP ${resp.status}`);
373
  let buf = await resp.arrayBuffer();
374
  const head = new Uint8Array(buf, 0, 2);
 
413
  }
414
  st.linkSlices = linkSlicesOnce();
415
 
416
+ // Every answer from Python carries a revision (app.next_rev), and answers can arrive out of order: a click answered
417
+ // after another cube was opened, say. Each answer's acknowledgements count, but only the newest answer is shown, so
418
+ // a late one never brings back a cube the visitor has left.
419
  async function onValue(raw) {
420
  let m = null;
421
  try { m = raw ? JSON.parse(raw) : null; } catch (e) { m = null; }
422
  if (!m || !m.key) return;
423
+ const acked = takeAcks(m);
424
+ if (m.ack_only || !(m.rev > st.maxRev)) {
425
+ if (acked) redraw([0, 1, 2], true);
426
+ flushClicks();
 
 
 
 
427
  return;
428
  }
429
+ st.maxRev = m.rev;
430
  if (m.key !== st.key || st.failed) {
431
  await newCube(m);
432
+ } else {
433
  applyClicks(m);
434
  redraw();
435
  }
436
  }
437
 
438
+ function takeAcks(m) {
439
+ const acked = new Set((m.clicks && m.clicks.acked) || []);
440
+ if (!acked.size) return false;
441
+ const before = st.pending.length;
442
+ st.pending = st.pending.filter(p => !acked.has(p.id));
443
+ if (st.inflight && st.sent.some(id => acked.has(id))) { // the request in flight was answered: send the next
444
+ clearTimeout(st.inflight);
445
+ st.inflight = null;
446
+ st.sent = [];
447
+ st.flushTries = 0;
448
+ }
449
+ return st.pending.length !== before;
450
+ }
451
+
452
  function applyClicks(m) {
453
  st.meta = m;
454
  st.rev = m.rev;
455
  st.clk = decodeSim(m.clicks && m.clicks.sim);
456
  st.marks = {pos: (m.clicks && m.clicks.pos) || [], neg: (m.clicks && m.clicks.neg) || []};
 
 
 
 
457
  updateThresholds();
458
  flushClicks();
459
  }
460
 
461
+ // Clicks go to Python one request at a time, each carrying every click on one cube not yet acknowledged, so a click
462
+ // made while another is being handled is sent next instead of being dropped; the ids stop a resent click counting
463
+ // twice. Each click keeps the cube it was made on, so one made just before another cube opens still counts there.
464
  function flushClicks() {
465
+ if (st.inflight || !st.pending.length) return;
466
  if ((st.flushTries = (st.flushTries || 0) + 1) > 3) {
467
  st.pending = [];
468
+ st.sent = [];
469
  st.flushTries = 0;
470
  setStatus('The Space did not answer these clicks; try again in a moment.', null);
471
  redraw([0, 1, 2], true);
472
  return;
473
  }
474
+ const key = st.pending[0].key;
475
+ const batch = st.pending.filter(p => p.key === key);
476
+ for (const p of batch) p.sent = true;
477
+ st.sent = batch.map(p => p.id);
478
  st.inflight = setTimeout(() => { st.inflight = null; flushClicks(); }, 15000);
479
+ trigger('select', {key, clicks: batch.map(p => ({id: p.id, index: p.voxel, value: p.kind}))});
480
  }
481
 
482
  async function newCube(m) {
483
  const token = ++st.loading;
484
+ if (st.abort) st.abort.abort(); // stop the downloads for the cube before
485
+ const abort = st.abort = new AbortController();
486
  const S = m.size;
487
  const same = st.size === S;
488
  st.key = m.key;
 
490
  st.n = S / PATCH;
491
  st.ct = st.ink = st.surface = st.pca = st.ref = null;
492
  st.failed = false;
 
 
493
  applyClicks(m);
494
  const local = m.point.map((p, i) => Math.min(S - 1, Math.max(0, p - m.origin[i])));
495
  const link = st.linkSlices;
 
506
  redraw(); // black views, not the previous cube, while this one loads
507
  setStatus(`Loading ${m.title || m.volume}...`, 0);
508
  try {
509
+ const [ct, layers] = await Promise.all([loadCT(m, token, abort.signal), loadLayers(m, abort.signal)]);
510
  if (token !== st.loading || !ct) return;
511
  st.ct = ct;
512
  Object.assign(st, {ink: layers.ink || null, surface: layers.surface || null, pca: layers.pca || null,
 
566
  } else if (act === 'auto') {
567
  setSlider('bright', 0); setSlider('contrast', 0); windowLevel(); redraw();
568
  } else if (act === 'undo') {
569
+ const last = st.pending.pop(); // the newest click, on any cube
570
+ if (last && last.sent) trigger('undo', {key: st.key, id: last.id}); // Python may have it already
571
+ else if (!last) trigger('undo', {key: st.key});
572
+ redraw([0, 1, 2], true);
573
  } else if (act === 'clear') {
574
+ const sent = st.pending.filter(p => p.sent).map(p => p.id);
575
  st.pending = [];
576
+ trigger('clear', {key: st.key, ids: sent});
577
  } else if (act === 'use' && m) {
578
  trigger('apply', {volume: m.volume, point: st.slices.map((s, i) => s + m.origin[i])});
579
  flash(btn, 'Copied to Your own point');
 
676
  setStatus('This cube has no 3D DINO embeddings, so it cannot be searched. Run it with the 3D DINO model.', null);
677
  return;
678
  }
679
+ st.pending.push({id: `${PAGE_ID}-${++st.clickSeq}`, kind, voxel: vox, key: st.key});
680
  if (kind === 'pos' && !st.on.clicks) {
681
  st.on.clicks = true;
682
  element.querySelector('input[data-layer="clicks"]').checked = true;