Spaces:
Sleeping
Sleeping
Download src/inference/__init__.py from farrell236/OCT_Segmentation_a2: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/spaces/farrell236/OCT_Segmentation_a2/resolve/main/src/inference/__init__.py
- Command line
-
hf download hf://spaces/farrell236/OCT_Segmentation_a2/src/inference/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/farrell236/OCT_Segmentation_a2/resolve/main/src/inference/__init__.py
2.04 kB
| """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) | |