File size: 26,976 Bytes
bf88397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
"""Local DICOM viewing and inference adapter for the retained OCT submission.

The Gradio controls and display behavior match OCT_submission. Prediction uses
this project's saved model, normalization and postprocessing in an isolated
worker; model weights and research records are unchanged.
"""

from __future__ import annotations

import argparse
import copy
import html
import json
import math
import os
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import uuid
from collections import OrderedDict
from dataclasses import dataclass, field
from pathlib import Path
from threading import Lock, RLock

import numpy as np
import pydicom
import torch
from PIL import Image, ImageDraw, ImageOps

from src.io import source_frames, structural_header

ROOT = Path(__file__).resolve().parents[1]
CHECKPOINT = ROOT / "artifacts/checkpoint.pt"
STRUCTURAL_OCT = "1.2.840.10008.5.1.4.1.1.77.1.5.4"
FAMILIES = {
    "heidelberg_spectralis": "Heidelberg Spectralis",
    "topcon_maestro2": "Topcon Maestro2",
    "topcon_triton": "Topcon Triton",
    "zeiss_cirrus": "Zeiss Cirrus",
}
AUTO = "auto"
NAMES = {
    "sct_280677004": "ILM",
    "dcm_128289": "RNFL · outer",
    "dcm_128290": "GCL · outer",
    "dcm_128291": "IPL · outer",
    "dcm_128292": "INL · outer",
    "dcm_128293": "OPL · outer",
    "sct_76710003": "ELM",
    "dcm_128320": "EZ · inner",
    "dcm_128321": "EZ · midline",
    "dcm_128323": "Interdigitation zone · inner",
    "dcm_128297": "RPE · inner",
    "dcm_128299": "RPE · outer",
    "dcm_128300": "Bruch’s membrane · outer",
    "dcm_128298": "RPE · centre",
    "dcm_128301": "Choroid–sclera interface",
}
PALETTE = [
    "#22d3ee",
    "#a3e635",
    "#fbbf24",
    "#fb7185",
    "#c084fc",
    "#34d399",
    "#f472b6",
    "#60a5fa",
    "#f97316",
    "#e879f9",
    "#2dd4bf",
    "#fde047",
    "#a5b4fc",
    "#fda4af",
    "#d9f99d",
]
COLORS = {key: PALETTE[i % len(PALETTE)] for i, key in enumerate(NAMES)}


class ViewerError(ValueError):
    """A recoverable error suitable for displaying in the viewer."""


@dataclass
class Prediction:
    positions: np.ndarray  # K, number of predicted B-scans, original A
    keys: list[str]
    frames: list[int]
    family: str
    compute: str
    complete_volume: bool
    seconds: float
    export: Path


@dataclass
class Volume:
    identity: str
    directory: Path
    paths: list[Path]
    headers: list
    shape: tuple[int, int, int]
    detected_family: str | None
    axial_mm: float | None
    lateral_mm: float | None
    filename: str
    prediction: Prediction | None = None
    view_frame: int = 0
    view_settings: tuple | None = None
    frames: OrderedDict = field(default_factory=OrderedDict)
    cancelled: bool = False
    updated: float = field(default_factory=time.monotonic)

    def frame(self, index: int) -> np.ndarray:
        index = max(0, min(int(index), self.shape[0] - 1))
        self.updated = time.monotonic()
        if index not in self.frames:
            try:
                for path, header in self.headers:
                    offset = int(header.get("ConcatenationFrameOffsetNumber", 0))
                    if offset <= index < offset + int(header.NumberOfFrames):
                        pixels = np.asarray(
                            next(source_frames(path, indices=[index - offset]))
                        )
                        break
                else:
                    raise IndexError("Frame outside volume")
            except Exception as exc:
                raise ViewerError(
                    "Could not decode this DICOM. Install requirements.txt "
                    "for JPEG2000 and lossless JPEG support. " + str(exc)
                ) from exc
            if pixels.shape != self.shape[1:]:
                raise ViewerError(
                    "Decoded image dimensions do not match the DICOM header."
                )
            self.frames[index] = pixels
            while len(self.frames) > 8:
                self.frames.popitem(last=False)
        self.frames.move_to_end(index)
        return self.frames[index]


class SessionStore:
    def __init__(self):
        self.volumes: dict[str, Volume] = {}
        self.lock = RLock()

    def add(self, volume: Volume) -> str:
        with self.lock:
            self.reap()
            self.volumes[volume.identity] = volume
        return volume.identity

    def get(self, identity: str | None) -> Volume:
        with self.lock:
            if not identity or identity not in self.volumes:
                raise ViewerError("Upload a structural OCT DICOM first.")
            volume = self.volumes[identity]
            volume.updated = time.monotonic()
            return volume

    def remove(self, identity: str | None):
        with self.lock:
            volume = self.volumes.pop(identity, None)
            if volume:
                volume.cancelled = True
                shutil.rmtree(volume.directory, ignore_errors=True)

    def reap(self):
        for identity, volume in list(self.volumes.items()):
            if time.monotonic() - volume.updated > 7200:
                self.remove(identity)


STORE = SessionStore()


def detect_family(header) -> str | None:
    description = " ".join(
        str(header.get(tag, ""))
        for tag in ("Manufacturer", "ManufacturerModelName", "SeriesDescription")
    ).lower()
    if "spectralis" in description or "heidelberg" in description:
        return "heidelberg_spectralis"
    if "maestro" in description:
        return "topcon_maestro2"
    if "triton" in description:
        return "topcon_triton"
    if "cirrus" in description or "zeiss" in description:
        return "zeiss_cirrus"
    return None


def pixel_spacing(header) -> tuple[float | None, float | None]:
    candidates = []
    for key in ("SharedFunctionalGroupsSequence", "PerFrameFunctionalGroupsSequence"):
        sequence = header.get(key, [])
        if sequence:
            measures = sequence[0].get("PixelMeasuresSequence", [])
            if measures:
                candidates.append(measures[0].get("PixelSpacing"))
    candidates.append(header.get("PixelSpacing"))
    for values in candidates:
        if values is not None and len(values) == 2:
            row, col = map(float, values)
            if math.isfinite(row) and math.isfinite(col) and row > 0 and col > 0:
                return row, col
    return None, None


def load_upload(files: list[str] | str) -> Volume:
    if isinstance(files, str):
        files = [files]
    if not files:
        raise ViewerError("Choose a .dcm file to begin.")
    directory = Path(tempfile.mkdtemp(prefix="myxos-oct-viewer-"))
    os.chmod(directory, 0o700)
    try:
        data = directory / "data"
        data.mkdir(mode=0o700)
        paths = []
        all_headers = []
        for index, uploaded in enumerate(files):
            source = Path(uploaded)
            if not source.is_file():
                raise ViewerError(
                    "An uploaded DICOM is no longer available. Upload it again."
                )
            # A private copy decouples inference from Gradio's upload-cache lifetime.
            path = data / f"part-{index:03d}.dcm"
            shutil.copyfile(source, path)
            os.chmod(path, 0o600)
            header = pydicom.dcmread(path, stop_before_pixels=True)
            if str(header.get("SOPClassUID", "")) != STRUCTURAL_OCT:
                raise ViewerError(
                    "This is not a structural OCT DICOM. Upload the OCT volume, rather than an OCTA or segmentation export."
                )
            if int(header.get("SamplesPerPixel", 1)) != 1:
                raise ViewerError(
                    "The selected checkpoint expects grayscale structural OCT."
                )
            all_headers.append(header)
            paths.append(path)
        for tag in ("StudyInstanceUID", "SeriesInstanceUID", "ConcatenationUID"):
            identifiers = {str(h.get(tag)) for h in all_headers if h.get(tag)}
            if len(identifiers) > 1:
                raise ViewerError(
                    "Upload one OCT volume, or the contiguous DICOM parts of that same volume."
                )
        headers = [(path, structural_header(path)) for path in paths]
        headers.sort(
            key=lambda item: int(item[1].get("ConcatenationFrameOffsetNumber", 0))
        )
        count = 0
        height, width = int(headers[0][1].Rows), int(headers[0][1].Columns)
        for path, header in headers:
            if int(header.get("ConcatenationFrameOffsetNumber", 0)) != count:
                raise ViewerError("Missing or overlapping concatenation frames.")
            if (int(header.Rows), int(header.Columns)) != (height, width):
                raise ViewerError("Inconsistent structural geometry.")
            if header.PhotometricInterpretation not in {"MONOCHROME1", "MONOCHROME2"}:
                raise ViewerError(
                    "The selected checkpoint expects grayscale structural OCT."
                )
            count += int(header.NumberOfFrames)
        if any(
            int(h.get("InConcatenationTotalNumber", len(paths))) != len(paths)
            for h in all_headers
        ):
            raise ViewerError("Upload all contiguous DICOM parts of this volume.")
        paths = [path for path, _ in headers]
        shape = count, height, width
        if not all(int(n) > 0 for n in shape):
            raise ViewerError("The DICOM contains an empty volume.")
        axial, lateral = pixel_spacing(all_headers[0])
        volume = Volume(
            uuid.uuid4().hex,
            directory,
            paths,
            headers,
            tuple(shape),
            detect_family(all_headers[0]),
            axial,
            lateral,
            Path(files[0]).name,
        )
        # Fail early when a required codec is missing.
        volume.frame((shape[0] - 1) // 2)
        return volume
    except ViewerError:
        shutil.rmtree(directory, ignore_errors=True)
        raise
    except Exception as exc:
        shutil.rmtree(directory, ignore_errors=True)
        raise ViewerError("Unable to load this OCT volume: " + str(exc)) from exc


def resolve_family(volume: Volume, selected: str) -> str:
    family = volume.detected_family if selected == AUTO else selected
    if family not in FAMILIES:
        raise ViewerError(
            "The scanner could not be identified. Select its scanner family below."
        )
    return family


def boundary_choices(family: str | None) -> list[tuple[str, str]]:
    config = ENGINE.checkpoint.with_name("config.json")
    cfg = (
        json.loads(config.read_text())
        if config.is_file()
        else torch.load(ENGINE.checkpoint, map_location="cpu", weights_only=False)[
            "config"
        ]
    )
    keys = cfg["boundary_keys"].get(family, [])
    return [(NAMES.get(key, key), key) for key in keys]


def compute_choices() -> list[tuple[str, str]]:
    choices = [("Automatic", "auto"), ("CPU", "cpu")]
    if ENGINE.checkpoint.suffix == ".npz":
        return choices
    if torch.cuda.is_available():
        choices.append(("NVIDIA GPU · CUDA", "cuda"))
    if torch.backends.mps.is_available():
        choices.append(("Apple GPU · MPS", "mps"))
    return choices


def resolve_compute(selected: str) -> str:
    if selected == "auto":
        if ENGINE.checkpoint.suffix == ".npz":
            return "cpu"
        return (
            "cuda"
            if torch.cuda.is_available()
            else ("mps" if torch.backends.mps.is_available() else "cpu")
        )
    if selected not in {value for _, value in compute_choices()}:
        raise ViewerError("That compute device is not available on this machine.")
    return selected


class InferenceEngine:
    def __init__(self):
        self.lock = Lock()
        self.checkpoint = CHECKPOINT
        self.threads = 2
        self.decode_workers = 2
        self.timeout = 1800

    def predict(self, volume, family, compute, batch_size, full_volume, frame_index):
        device = resolve_compute(compute)
        frames = list(range(volume.shape[0])) if full_volume else [int(frame_index)]
        started = time.monotonic()
        volume.cancelled = False
        with (
            self.lock,
            tempfile.TemporaryDirectory(
                prefix="inference-", dir=volume.directory
            ) as directory,
        ):
            work = Path(directory)
            job = {
                "checkpoint": str(self.checkpoint),
                "data_root": str(volume.directory / "data"),
                "oct_paths": [path.name for path in volume.paths],
                "family": family,
                "device": device,
                "batch_size": batch_size,
                "full_volume": full_volume,
                "frame_index": int(frame_index),
                "threads": self.threads,
                "decode_workers": self.decode_workers,
                "output": str(work / "prediction.npz"),
                "stop": str(work / "stop"),
            }
            spec = work / "job.json"
            spec.write_text(json.dumps(job))
            log_path = work / "worker.log"
            with log_path.open("w") as log, log_path.open() as updates:
                with subprocess.Popen(
                    [sys.executable, "-m", "src.viewer", "--job", str(spec)],
                    cwd=ROOT,
                    stdout=log,
                    stderr=subprocess.STDOUT,
                    start_new_session=os.name == "posix",
                ) as process:
                    try:
                        while True:
                            if volume.cancelled:
                                Path(job["stop"]).touch()
                            for line in updates:
                                try:
                                    event = json.loads(line)
                                except json.JSONDecodeError:
                                    continue
                                if isinstance(event, dict) and "completed" in event:
                                    volume.updated = time.monotonic()
                                    yield event
                            try:
                                returncode = process.wait(timeout=0.1)
                                break
                            except subprocess.TimeoutExpired:
                                if time.monotonic() - started > self.timeout:
                                    raise ViewerError(
                                        f"Inference exceeded the {self.timeout}-second time limit."
                                    )
                        if volume.cancelled:
                            raise ViewerError("Segmentation cancelled.")
                        if returncode:
                            detail = log_path.read_text()[-6000:]
                            if "out of memory" in detail.lower():
                                raise ViewerError("GPU out of memory.")
                            raise ViewerError(
                                "Inference failed: " + detail.strip().splitlines()[-1]
                            )
                        with np.load(job["output"], allow_pickle=False) as arrays:
                            positions = arrays["positions"].copy()
                            keys = arrays["boundary_keys"].tolist()
                        if (
                            positions.shape != (len(keys), len(frames), volume.shape[2])
                            or not keys
                            or not np.isfinite(positions).all()
                            or positions.min() < 0
                            or positions.max() > volume.shape[1] - 1
                        ):
                            raise ViewerError(
                                "Inference returned invalid prediction geometry."
                            )
                        suffix = (
                            "volume"
                            if full_volume
                            else f"preview-bscan-{frames[0] + 1:04d}"
                        )
                        output = volume.directory / f"boundaries-{suffix}.npz"
                        shutil.copyfile(job["output"], output)
                        prediction = Prediction(
                            positions,
                            keys,
                            frames,
                            family,
                            device,
                            full_volume,
                            time.monotonic() - started,
                            output,
                        )
                        volume.prediction = prediction
                        yield {"result": prediction}
                    finally:
                        if process.poll() is None:
                            if os.name == "posix":
                                os.killpg(process.pid, signal.SIGKILL)
                            else:
                                process.kill()
                            process.wait()


ENGINE = InferenceEngine()


def worker(job):
    """Run the saved pipeline with batch notifications, isolated from Gradio."""
    from src.inference import apply_policy
    from src.inference.density import predict_bprob
    from src.inference.predict import (
        ordered_process_prefetch,
        predict_prepared,
        prepare_pixels,
    )
    from src.io import enforce_input_opens, resize_frames
    from src.models import make_model

    torch.set_num_threads(job["threads"])
    row = {"case_id": "volume", "device": job["family"], "oct_paths": job["oct_paths"]}
    root = Path(job["data_root"])
    started = time.monotonic()
    if Path(job["checkpoint"]).suffix == ".npz":
        from src.profile import load_checkpoint, predict_case

        models, cfg, _ = load_checkpoint(job["checkpoint"])
        enforce_input_opens(root, [row])
        positions, keys = predict_case(row, root, models, cfg, job["batch_size"])
        if not job["full_volume"]:
            positions = positions[:, job["frame_index"] : job["frame_index"] + 1]
    else:
        saved = torch.load(job["checkpoint"], map_location="cpu", weights_only=False)
        if saved["config"].get("inference_wrapper") == "device_router_v1":
            route = saved["config"]["device_routes"][job["family"]]
            saved = saved["route_checkpoints"][route]
        cfg = saved["config"]
        device = torch.device(job["device"])
        net = make_model(cfg)
        net.load_state_dict(saved["model"])
        net.to(device).eval()
        audit = enforce_input_opens(root, [row])
        selected = None
        if job["full_volume"]:
            prepared = (
                list(ordered_process_prefetch([row], root, cfg, 1, audit))[0][1]
                if job["decode_workers"]
                else prepare_pixels(row, root, cfg)
            )
        else:
            if cfg["architecture"] not in {
                "boundary_ensemble_v1",
                "boundary_unet_v1",
                "boundary_unet_axial_v1",
                "boundary_unet_spatial_v1",
            }:
                raise ViewerError("Use Full volume for this checkpoint architecture.")
            configs = [
                cfg,
                *[component["config"] for component in cfg.get("components", [])],
            ]
            radius = max(c.get("context_radius", 0) for c in configs)
            headers = [
                (root / name, structural_header(root / name))
                for name in row["oct_paths"]
            ]
            count = sum(int(header.NumberOfFrames) for _, header in headers)
            index = job["frame_index"]
            lo, hi = max(0, index - radius), min(count, index + radius + 1)
            selected = index - lo
            frames = []
            for path, header in headers:
                offset = int(header.get("ConcatenationFrameOffsetNumber", 0))
                indices = range(
                    max(0, lo - offset), min(int(header.NumberOfFrames), hi - offset)
                )
                if indices:
                    frames.extend(source_frames(path, indices=list(indices)))
            pixels = resize_frames(frames, cfg["height"], cfg["width"], "cpu").to(
                torch.float16
            )
            prepared = [(pixels, int(headers[0][1].Rows), int(headers[0][1].Columns))]
            cfg = copy.deepcopy(cfg)
            for c in [
                cfg,
                *[component["config"] for component in cfg.get("components", [])],
            ]:
                if "density_b_sigma" in c:
                    c["density_b_sigma"] = 0
                policy = c.get("prediction_policy", {})
                for settings in [policy, *policy.get("by_device", {}).values()]:
                    settings.pop("median_b", None)
                    settings.pop("gaussian_b_sigma", None)

        def report(completed, total):
            if Path(job["stop"]).exists():
                raise ViewerError("Segmentation cancelled.")
            if selected is not None:
                completed, total = int(completed > selected), 1
            print(
                json.dumps(
                    {
                        "completed": completed,
                        "total": total,
                        "device": job["device"],
                        "seconds": time.monotonic() - started,
                    }
                ),
                flush=True,
            )

        if Path(job["stop"]).exists():
            raise ViewerError("Segmentation cancelled.")
        if cfg.get("inference_wrapper") == "b_probability_smooth_v1":
            positions, keys = predict_bprob(
                row, prepared, net, cfg, device, job["batch_size"], progress=report
            )
        else:
            positions, keys = predict_prepared(
                row,
                prepared,
                net,
                cfg,
                device,
                job["batch_size"],
                cfg.get("tta_horizontal", False),
                progress=report,
            )
        positions = apply_policy(positions, job["family"], cfg.get("prediction_policy"))
        if selected is not None:
            positions = positions[:, selected : selected + 1]
    if Path(job["stop"]).exists():
        raise ViewerError("Segmentation cancelled.")
    height = int(structural_header(root / row["oct_paths"][0]).Rows)
    arrays = {
        "positions": np.clip(positions, 0, height - 1).astype("float32"),
        "boundary_keys": np.asarray(keys),
    }
    if not job["full_volume"]:
        arrays["frame_indices"] = np.asarray([job["frame_index"]], dtype=np.int32)
    np.savez_compressed(job["output"], **arrays)


def display_dimensions(volume: Volume, physical: bool) -> tuple[int, int]:
    _, h, a = volume.shape
    ratio = h / a
    if physical and volume.axial_mm and volume.lateral_mm:
        ratio *= volume.axial_mm / volume.lateral_mm
    width = 1000
    height = max(1, round(width * ratio))
    scale = min(1.0, 1400 / max(width, height))
    return max(1, round(width * scale)), max(1, round(height * scale))


def render(
    volume: Volume,
    index: int,
    selected_keys: list[str] | None = None,
    overlay: bool = True,
    opacity: float = 0.9,
    line_width: int = 2,
    labels: bool = False,
    contrast: float = 1.0,
    brightness: float = 0.0,
    physical: bool = True,
) -> tuple[np.ndarray, np.ndarray | None]:
    index = max(0, min(volume.shape[0] - 1, int(index)))
    image = np.asarray(volume.frame(index), dtype=np.float32)
    finite = image[np.isfinite(image)]
    lo, hi = np.quantile(finite, [0.01, 0.995]) if finite.size else (0, 1)
    gray = np.nan_to_num((image - lo) / max(float(hi - lo), 1e-6))
    gray = np.clip((gray - 0.5) * float(contrast) + 0.5 + float(brightness), 0, 1)
    width, height = display_dimensions(volume, physical)
    base = (
        Image.fromarray(np.uint8(np.round(gray * 255)))
        .convert("RGB")
        .resize((width, height), Image.Resampling.BILINEAR)
    )
    if volume.headers[0][1].PhotometricInterpretation == "MONOCHROME1":
        base = ImageOps.invert(base)
    raw = np.asarray(base)
    prediction = volume.prediction
    if prediction is None or index not in prediction.frames:
        return raw, None
    if not overlay:
        return raw, raw.copy()
    frame = prediction.frames.index(index)
    selected = set(prediction.keys if selected_keys is None else selected_keys)
    layer = base.convert("RGBA")
    annotation = Image.new("RGBA", base.size)
    draw = ImageDraw.Draw(annotation)
    _, native_h, native_a = volume.shape
    x = np.linspace(0, width - 1, native_a)
    for key, values in zip(prediction.keys, prediction.positions[:, frame]):
        if key not in selected:
            continue
        y = np.clip(values, 0, native_h - 1) * (height - 1) / max(1, native_h - 1)
        color = COLORS.get(key, "#ffffff")
        rgb = tuple(int(color[i : i + 2], 16) for i in (1, 3, 5))
        points = list(zip(x.tolist(), y.tolist()))
        if len(points) > 1:
            draw.line(
                points,
                fill=(*rgb, round(255 * float(opacity))),
                width=max(1, int(line_width)),
                joint="curve",
            )
        if labels:
            text = NAMES.get(key, key)
            anchor = max(
                0,
                min(
                    height - 14,
                    round(float(np.median(y[: max(1, native_a // 8)])) - 13),
                ),
            )
            box = draw.textbbox((8, anchor), text)
            draw.rectangle(
                (box[0] - 3, box[1] - 2, box[2] + 3, box[3] + 2), fill=(8, 15, 26, 210)
            )
            draw.text((8, anchor), text, fill=(*rgb, 255))
    return raw, np.asarray(Image.alpha_composite(layer, annotation).convert("RGB"))


def legend(keys: list[str]) -> str:
    if not keys:
        return "Boundary colours appear after a scan is loaded."
    items = []
    for key in keys:
        items.append(
            f'<span style="color:{COLORS.get(key, "#94a3b8")}">●</span> {html.escape(NAMES.get(key, key))}'
        )
    return "  ·  ".join(items)


def volume_info(volume: Volume, family: str | None) -> str:
    b, h, a = volume.shape
    scanner = FAMILIES.get(family, "Select scanner family")
    spacing = f" · axial {volume.axial_mm * 1000:.2f} µm" if volume.axial_mm else ""
    return (
        f"**{html.escape(scanner)}**  \n{b:,} B-scans · {h:,} × {a:,} pixels{spacing}"
    )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Isolated OCT viewer inference worker")
    parser.add_argument("--job", type=Path, required=True)
    worker(json.loads(parser.parse_args().job.read_text()))