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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Native Gradio controls for the retained OCT model and B-scan viewer."""

from __future__ import annotations

import html
import os
from pathlib import Path

os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")

import gradio as gr
from PIL import Image

from src.viewer import (
    AUTO,
    ENGINE,
    FAMILIES,
    STORE,
    boundary_choices,
    compute_choices,
    legend,
    load_upload,
    render,
    resolve_family,
    volume_info,
)

DEFAULT_SCANNER = "heidelberg_spectralis"
EMPTY_MESSAGE = "Upload a structural OCT DICOM to begin."


def download_result(volume, artifact, png=None):
    if artifact not in {"predictions", "overlay"}:
        raise gr.Error("Choose a supported export format.")
    label = "Download NPZ" if artifact == "predictions" else "Download PNG"
    path = None
    if volume and volume.prediction:
        path = volume.prediction.export if artifact == "predictions" else png
    ready = path is not None and Path(path).is_file()
    return gr.update(value=str(path) if ready else None, label=label, interactive=ready)


def display_outputs(
    identity,
    frame,
    keys,
    overlay,
    opacity,
    width,
    labels,
    contrast,
    brightness,
    physical,
    artifact="predictions",
):
    if not identity:
        return (
            None,
            None,
            "Upload a scan to browse B-scans.",
            download_result(None, artifact),
        )
    volume = STORE.get(identity)
    index = max(0, min(volume.shape[0] - 1, int(frame) - 1))
    volume.view_frame = index
    volume.view_settings = (
        keys,
        overlay,
        opacity,
        width,
        labels,
        contrast,
        brightness,
        physical,
        artifact,
    )
    raw, result = render(
        volume,
        index,
        keys,
        overlay,
        opacity,
        width,
        labels,
        contrast,
        brightness,
        physical,
    )
    caption = f"B-scan {index + 1:,} / {volume.shape[0]:,}"
    png = None
    if result is not None:
        png = volume.directory / f"overlay-bscan-{index + 1:04d}.png"
        Image.fromarray(result).save(png)
        caption += " · " + (
            "full-volume prediction"
            if volume.prediction.complete_volume
            else "single-slice preview"
        )
    elif volume.prediction:
        caption += " · this slice has not been segmented"
    return raw, result, caption, download_result(volume, artifact, png)


def on_upload(
    files,
    previous,
    scanner,
    overlay,
    opacity,
    width,
    labels,
    contrast,
    brightness,
    physical,
    artifact,
):
    volume = None
    try:
        volume = load_upload(files)
        identity = STORE.add(volume)
        frame = (volume.shape[0] - 1) // 2 + 1
        family = volume.detected_family if scanner == AUTO else scanner
        choices = boundary_choices(family)
        keys = [value for _, value in choices]
        raw, result, caption, download = display_outputs(
            identity,
            frame,
            keys,
            overlay,
            opacity,
            width,
            labels,
            contrast,
            brightness,
            physical,
            artifact,
        )
        if previous:
            STORE.remove(previous)
        message = "Scan loaded. Check the scanner family, then run inference."
        if family is None:
            message = "Scan loaded. Select its scanner family, then run inference."
        return (
            identity,
            raw,
            result,
            gr.update(
                minimum=1,
                maximum=max(2, volume.shape[0]),
                value=frame,
                interactive=volume.shape[0] > 1,
            ),
            gr.update(choices=choices, value=keys),
            volume_info(volume, family),
            legend(keys),
            caption,
            download,
            message,
            gr.update(interactive=True),
        )
    except Exception as exc:
        if volume is not None:
            STORE.remove(volume.identity)
        raise gr.Error(str(exc)) from exc


def clear_upload(identity, artifact="predictions"):
    STORE.remove(identity)
    return (
        None,
        None,
        None,
        gr.update(minimum=1, maximum=2, value=1, interactive=False),
        gr.update(choices=[], value=[]),
        "No scan loaded.",
        legend([]),
        "Upload a scan to browse B-scans.",
        download_result(None, artifact),
        EMPTY_MESSAGE,
        gr.update(interactive=False),
    )


def clear_volume(identity):
    return (
        *clear_upload(identity),
        gr.update(value=None),
        DEFAULT_SCANNER,
        "predictions",
    )


def scanner_changed(
    identity,
    scanner,
    frame,
    overlay,
    opacity,
    width,
    labels,
    contrast,
    brightness,
    physical,
    artifact,
):
    if not identity:
        return (gr.skip(),) * 8
    volume = STORE.get(identity)
    family = volume.detected_family if scanner == AUTO else scanner
    if volume.prediction is not None and volume.prediction.family != family:
        volume.prediction = None
    choices = boundary_choices(family)
    keys = [value for _, value in choices]
    outputs = display_outputs(
        identity,
        frame,
        keys,
        overlay,
        opacity,
        width,
        labels,
        contrast,
        brightness,
        physical,
        artifact,
    )
    return (
        gr.update(choices=choices, value=keys),
        volume_info(volume, family),
        legend(keys),
        *outputs,
        "Scanner selection updated. Run inference to apply it.",
    )


def move_frame(delta):
    def move(identity, frame, *settings):
        if not identity:
            return (gr.skip(),) * 5
        volume = STORE.get(identity)
        next_frame = max(1, min(volume.shape[0], int(frame) + delta))
        return gr.update(value=next_frame), *display_outputs(
            identity, next_frame, *settings
        )

    return move


def update_display(identity, frame, *settings):
    outputs = display_outputs(identity, frame, *settings)
    return *outputs, legend(settings[0]) if identity else legend([])


def segment(
    identity,
    frame,
    scanner,
    compute,
    scope,
    batch_size,
    *settings,
    progress=gr.Progress(),
):
    try:
        volume = STORE.get(identity)
        family = resolve_family(volume, scanner)
        if batch_size is None or not 1 <= int(batch_size) <= 16:
            raise ValueError("Enter a batch size from 1 to 16.")
        progress(0, desc="Loading the saved model")
        yield (
            gr.skip(),
            None,
            "Loading model…",
            download_result(None, settings[-1]),
            "Loading the saved model. First use takes a little longer.",
            gr.update(interactive=False),
        )
        for event in ENGINE.predict(
            volume, family, compute, int(batch_size), scope == "volume", int(frame) - 1
        ):
            if "result" not in event:
                progress(
                    event["completed"] / event["total"],
                    desc=f"Segmenting B-scans · {event['completed']:,}/{event['total']:,}",
                )
                message = (
                    f"Segmenting {event['completed']:,} / {event['total']:,} B-scans · "
                    f"{event['device'].upper()} · {event['seconds']:.0f} s elapsed"
                )
                yield (
                    gr.skip(),
                    gr.skip(),
                    gr.skip(),
                    gr.skip(),
                    message,
                    gr.update(interactive=False),
                )
            else:
                result = event["result"]
                outputs = display_outputs(
                    identity, volume.view_frame + 1, *(volume.view_settings or settings)
                )
                message = (
                    f"**Completed** · {FAMILIES[family]} · {result.compute.upper()} · "
                    f"{len(result.frames):,} B-scans · {len(result.keys)} boundaries · "
                    f"{result.seconds:.1f} s"
                )
                if not result.complete_volume:
                    message += "  \nSlice preview omits smoothing between B-scans. Use Full volume for the saved pipeline."
                yield *outputs, message, gr.update(interactive=True)
    except Exception as exc:
        message = html.escape(str(exc))
        if "out of memory" in str(exc).lower():
            message += " Try batch size 1, or select CPU."
        yield (
            gr.skip(),
            gr.skip(),
            gr.skip(),
            gr.skip(),
            f"**Error:** {message}",
            gr.update(interactive=True),
        )


def cancel(identity):
    if identity:
        STORE.get(identity).cancelled = True
    return (
        "Stop requested. Inference stops after the current model batch.",
        gr.update(interactive=True),
    )


def build_app():
    zoom_js = Path(__file__).with_name("viewer.js").read_text()
    with gr.Blocks(
        title="MYXOS · OCT Viewer", analytics_enabled=False, delete_cache=(7200, 7200)
    ) as demo:
        identity = gr.State(value=None, time_to_live=7200, delete_callback=STORE.remove)
        gr.Markdown("# OCT Boundary Explorer")
        with gr.Row():
            with gr.Column(scale=1, min_width=300):
                gr.Markdown("### Configuration")
                upload = gr.File(
                    label="OCT volume",
                    file_types=[".dcm", ".dicom"],
                    file_count="multiple",
                    type="filepath",
                    height=135,
                )
                scanner = gr.Dropdown(
                    choices=[
                        *[(name, key) for key, name in FAMILIES.items()],
                        ("Detect from DICOM", AUTO),
                    ],
                    value=DEFAULT_SCANNER,
                    label="Scanner family",
                )
                compute = gr.Dropdown(
                    choices=compute_choices(),
                    value="auto",
                    label="Processor",
                    info="Auto uses an available GPU, otherwise CPU.",
                )
                scope = gr.Radio(
                    choices=[
                        ("Full volume", "volume"),
                        ("Selected B-scan preview", "preview"),
                    ],
                    value="volume",
                    label="Inference scope",
                )
                with gr.Accordion("Performance settings", open=False):
                    batch = gr.Number(
                        value=1,
                        minimum=1,
                        maximum=16,
                        step=1,
                        precision=0,
                        label="Batch size",
                        info="Lower this if GPU memory is limited.",
                    )
                with gr.Row():
                    run = gr.Button(
                        "Run inference",
                        variant="primary",
                        interactive=False,
                        scale=3,
                        min_width=135,
                    )
                    stop = gr.Button("Stop", variant="stop", scale=1, min_width=70)
                clear = gr.Button("Clear volume", size="sm")
                scan_info = gr.Markdown("No scan loaded.")
                gr.Markdown(
                    "Upload one volume, or its contiguous DICOM parts together."
                )
            with gr.Column(scale=3, min_width=480):
                gr.Markdown("### Boundary viewer")
                messages = gr.Markdown(EMPTY_MESSAGE)
                with gr.Tabs():
                    viewers = []
                    for tab, label, viewer_id in [
                        ("Boundary overlay", "Predicted boundaries", "overlay-viewer"),
                        ("Original OCT", "OCT B-scan", "source-viewer"),
                    ]:
                        with gr.Tab(tab):
                            buttons = []
                            for text, action in [
                                ("Zoom in", "in"),
                                ("Zoom out", "out"),
                                ("Fit", "fit"),
                            ]:
                                button = gr.Button(text, size="sm", render=False)
                                button.click(
                                    fn=None,
                                    js=f"() => ({zoom_js})('{viewer_id}', '{action}')",
                                    queue=False,
                                    api_name=False,
                                )
                                buttons.append(button)
                            viewers.append(
                                gr.Image(
                                    label=label,
                                    type="numpy",
                                    format="png",
                                    interactive=False,
                                    height=480,
                                    buttons=[*buttons, "fullscreen"],
                                    elem_id=viewer_id,
                                )
                            )
                segmented, original = viewers
                gr.Markdown(
                    "Use Zoom in / Zoom out, then drag to pan. Fit resets the view."
                )
                with gr.Row():
                    previous = gr.Button("Previous", scale=0, min_width=85)
                    frame = gr.Slider(
                        1,
                        2,
                        value=1,
                        step=1,
                        label="B-scan",
                        scale=6,
                        interactive=False,
                    )
                    following = gr.Button("Next", scale=0, min_width=85)
                caption = gr.Markdown("Upload a scan to browse B-scans.")
                colors = gr.Markdown(legend([]))
                with gr.Accordion("Visible boundaries", open=False):
                    boundaries = gr.CheckboxGroup(
                        choices=[], value=[], label="Overlay boundaries"
                    )
                with gr.Accordion("Display settings", open=False):
                    with gr.Row():
                        show_overlay = gr.Checkbox(value=True, label="Show boundaries")
                        show_labels = gr.Checkbox(value=False, label="Label curves")
                        physical = gr.Checkbox(
                            value=True, label="Use physical pixel aspect"
                        )
                    with gr.Row():
                        opacity = gr.Slider(
                            0.1, 1.0, value=0.9, step=0.05, label="Overlay opacity"
                        )
                        width = gr.Slider(1, 5, value=2, step=1, label="Line width")
                    with gr.Row():
                        contrast = gr.Slider(
                            0.5, 3.0, value=1.0, step=0.05, label="Display contrast"
                        )
                        brightness = gr.Slider(
                            -0.4, 0.4, value=0.0, step=0.02, label="Display brightness"
                        )
                with gr.Row():
                    artifact = gr.Dropdown(
                        choices=[
                            ("Boundary predictions · NPZ", "predictions"),
                            ("Current B-scan overlay · PNG", "overlay"),
                        ],
                        value="predictions",
                        label="Export format",
                        scale=2,
                    )
                    download = gr.DownloadButton(
                        "Download selected output",
                        interactive=False,
                        scale=1,
                        min_width=175,
                    )
                gr.Markdown(
                    "Display settings change the view only. Predictions retain original OCT coordinates."
                )

        display_settings = [
            boundaries,
            show_overlay,
            opacity,
            width,
            show_labels,
            contrast,
            brightness,
            physical,
            artifact,
        ]
        settings_without_keys = display_settings[1:]
        display_widgets = [original, segmented, caption, download]
        upload_outputs = [
            identity,
            original,
            segmented,
            frame,
            boundaries,
            scan_info,
            colors,
            caption,
            download,
            messages,
            run,
        ]
        upload.upload(
            on_upload,
            [upload, identity, scanner, *settings_without_keys],
            upload_outputs,
            api_name="load_oct",
            concurrency_id="viewer",
            concurrency_limit=1,
        )
        upload.clear(
            clear_upload,
            [identity, artifact],
            upload_outputs,
            concurrency_id="viewer",
            concurrency_limit=1,
        )
        clear.click(
            clear_volume,
            [identity],
            upload_outputs + [upload, scanner, artifact],
            api_name="clear_volume",
            concurrency_id="viewer",
            concurrency_limit=1,
        )
        scanner.change(
            scanner_changed,
            [identity, scanner, frame, *settings_without_keys],
            [boundaries, scan_info, colors, *display_widgets, messages],
            concurrency_id="viewer",
            concurrency_limit=1,
        )
        frame.release(
            display_outputs,
            [identity, frame, *display_settings],
            display_widgets,
            queue=False,
            api_name="view_bscan",
        )
        for button, delta in [(previous, -1), (following, 1)]:
            button.click(
                move_frame(delta),
                [identity, frame, *display_settings],
                [frame, *display_widgets],
                queue=False,
                api_name=False,
            )
        for control in (show_overlay, show_labels, physical, boundaries):
            control.input(
                update_display,
                [identity, frame, *display_settings],
                [*display_widgets, colors],
                queue=False,
                api_name=False,
            )
        for control in (opacity, width, contrast, brightness):
            control.release(
                display_outputs,
                [identity, frame, *display_settings],
                display_widgets,
                queue=False,
                api_name=False,
            )
        artifact.input(
            display_outputs,
            [identity, frame, *display_settings],
            display_widgets,
            queue=False,
            api_name="select_export",
        )
        inference = run.click(
            segment,
            [identity, frame, scanner, compute, scope, batch, *display_settings],
            [*display_widgets, messages, run],
            api_name="segment_oct",
            show_progress="full",
            show_progress_on=[segmented, original],
            concurrency_id="viewer",
            concurrency_limit=1,
        )
        stop.click(
            cancel,
            [identity],
            [messages, run],
            cancels=[inference],
            queue=False,
            api_name="stop_segmentation",
        )
    return demo