"""Utilities for mapping normalized target modalities to physical ranges.""" from __future__ import annotations import numpy as np TARGET_CHANNELS = ["D", "Delta", "eta", "theta", "psi", "R"] def normalized_modalities_to_physical(target, channel_axis=0, clip=False): """Map normalized grayscale modalities to nominal physical ranges. Use this only for targets encoded as ``png_uint8_normalized_to_float32_0_1``. If a split already stores physical Lu-Chipman values, do not apply this conversion again. """ target = np.asarray(target, dtype=np.float32) values = np.moveaxis(target, channel_axis, 0) if values.shape[0] != 6: raise ValueError(f"Expected 6 target channels, got shape {target.shape}") g = np.clip(values, 0.0, 1.0) if clip else values physical = np.empty_like(g, dtype=np.float32) physical[0] = g[0] physical[1] = g[1] physical[2] = np.pi * g[2] physical[3] = np.pi * (g[3] - 0.5) physical[4] = np.pi * (g[4] - 0.5) physical[5] = np.pi * g[5] return np.moveaxis(physical, 0, channel_axis) def physical_modalities_to_normalized(target, channel_axis=0, clip=False): """Map physical target modalities to normalized grayscale ranges.""" target = np.asarray(target, dtype=np.float32) values = np.moveaxis(target, channel_axis, 0) if values.shape[0] != 6: raise ValueError(f"Expected 6 target channels, got shape {target.shape}") normalized = np.empty_like(values, dtype=np.float32) normalized[0] = values[0] normalized[1] = values[1] normalized[2] = values[2] / np.pi normalized[3] = values[3] / np.pi + 0.5 normalized[4] = values[4] / np.pi + 0.5 normalized[5] = values[5] / np.pi if clip: normalized = np.clip(normalized, 0.0, 1.0) return np.moveaxis(normalized, 0, channel_axis)