File size: 2,035 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
"""Optional fixed surface filters in original OCT coordinates; no label inputs."""

from __future__ import annotations

import numpy as np


def apply_policy(positions, device, policy):
    if not policy:
        return positions
    allowed = {
        "median_b",
        "median_a",
        "gaussian_b_sigma",
        "integer_devices",
        "by_device",
    }
    if set(policy) - allowed:
        raise ValueError("Unknown inference policy")
    settings = {k: v for k, v in policy.items() if k != "by_device"}
    if "by_device" in policy:
        settings.update(policy["by_device"].get(device, {}))
    if set(settings) - (allowed - {"by_device"}):
        raise ValueError("Unknown device inference policy")
    result = np.asarray(positions, dtype=np.float32)
    if result.ndim != 3 or not np.isfinite(result).all():
        raise ValueError("Invalid surface filter input")
    for field, axis in [("median_b", 1), ("median_a", 2)]:
        size = settings.get(field, 1)
        if not isinstance(size, int) or size < 1 or size % 2 != 1:
            raise ValueError("Median size must be a positive odd integer")
        if size > 1:
            from scipy.ndimage import median_filter

            shape = [1, 1, 1]
            shape[axis] = size
            result = median_filter(result, size=shape, mode="nearest")
    sigma = settings.get("gaussian_b_sigma", 0.0)
    if not np.isfinite(sigma) or sigma < 0:
        raise ValueError("Invalid smoothing sigma")
    if sigma:
        from scipy.ndimage import gaussian_filter1d

        result = gaussian_filter1d(result, sigma, axis=1, mode="nearest")
    integer_devices = settings.get("integer_devices", [])
    if any(
        d not in ["topcon_maestro2", "topcon_triton", "zeiss_cirrus"]
        for d in integer_devices
    ):
        raise ValueError(
            "Integer policy is restricted to audited integer-coordinate families"
        )
    if device in integer_devices:
        result = np.rint(result)
    return result.astype(np.float32, copy=False)