Datasets:
Add optional target physical-range mapping: scripts/target_mapping.py
Browse files- scripts/target_mapping.py +50 -0
scripts/target_mapping.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Utilities for mapping normalized target modalities to physical ranges."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
TARGET_CHANNELS = ["D", "Delta", "eta", "theta", "psi", "R"]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def normalized_modalities_to_physical(target, channel_axis=0, clip=False):
|
| 12 |
+
"""Map normalized grayscale modalities to nominal physical ranges.
|
| 13 |
+
|
| 14 |
+
Use this only for targets encoded as
|
| 15 |
+
``png_uint8_normalized_to_float32_0_1``. If a split already stores physical
|
| 16 |
+
Lu-Chipman values, do not apply this conversion again.
|
| 17 |
+
"""
|
| 18 |
+
target = np.asarray(target, dtype=np.float32)
|
| 19 |
+
values = np.moveaxis(target, channel_axis, 0)
|
| 20 |
+
if values.shape[0] != 6:
|
| 21 |
+
raise ValueError(f"Expected 6 target channels, got shape {target.shape}")
|
| 22 |
+
|
| 23 |
+
g = np.clip(values, 0.0, 1.0) if clip else values
|
| 24 |
+
physical = np.empty_like(g, dtype=np.float32)
|
| 25 |
+
physical[0] = g[0]
|
| 26 |
+
physical[1] = g[1]
|
| 27 |
+
physical[2] = np.pi * g[2]
|
| 28 |
+
physical[3] = np.pi * (g[3] - 0.5)
|
| 29 |
+
physical[4] = np.pi * (g[4] - 0.5)
|
| 30 |
+
physical[5] = np.pi * g[5]
|
| 31 |
+
return np.moveaxis(physical, 0, channel_axis)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def physical_modalities_to_normalized(target, channel_axis=0, clip=False):
|
| 35 |
+
"""Map physical target modalities to normalized grayscale ranges."""
|
| 36 |
+
target = np.asarray(target, dtype=np.float32)
|
| 37 |
+
values = np.moveaxis(target, channel_axis, 0)
|
| 38 |
+
if values.shape[0] != 6:
|
| 39 |
+
raise ValueError(f"Expected 6 target channels, got shape {target.shape}")
|
| 40 |
+
|
| 41 |
+
normalized = np.empty_like(values, dtype=np.float32)
|
| 42 |
+
normalized[0] = values[0]
|
| 43 |
+
normalized[1] = values[1]
|
| 44 |
+
normalized[2] = values[2] / np.pi
|
| 45 |
+
normalized[3] = values[3] / np.pi + 0.5
|
| 46 |
+
normalized[4] = values[4] / np.pi + 0.5
|
| 47 |
+
normalized[5] = values[5] / np.pi
|
| 48 |
+
if clip:
|
| 49 |
+
normalized = np.clip(normalized, 0.0, 1.0)
|
| 50 |
+
return np.moveaxis(normalized, 0, channel_axis)
|