| 70 | |
| 71 | |
| 72 | def _normalize_array( |
| 73 | x: np.ndarray, |
| 74 | *, |
| 75 | mean: Optional[np.ndarray] = None, |
| 76 | std: Optional[np.ndarray] = None, |
| 77 | min_val: Optional[np.ndarray] = None, |
| 78 | max_val: Optional[np.ndarray] = None, |
| 79 | q_low: Optional[np.ndarray] = None, |
| 80 | q_high: Optional[np.ndarray] = None, |
| 81 | mode: str = "mean_std", |
| 82 | ) -> np.ndarray: |
| 83 | eps = 1e-6 |
| 84 | if mode == "none": |
| 85 | return x |
| 86 | if mode == "mean_std": |
| 87 | assert mean is not None and std is not None |
| 88 | return (x - mean) / np.maximum(std, eps) |
| 89 | if mode == "min_max": |
| 90 | assert min_val is not None and max_val is not None |
| 91 | denom = np.maximum(max_val - min_val, eps) |
| 92 | return 2.0 * (x - min_val) / denom - 1.0 |
| 93 | if mode == "q01_q99": |
| 94 | assert q_low is not None and q_high is not None |
| 95 | denom = np.maximum(q_high - q_low, eps) |
| 96 | return 2.0 * (x - q_low) / denom - 1.0 |
| 97 | if mode == "q10_q90": |
| 98 | assert q_low is not None and q_high is not None |
| 99 | denom = np.maximum(q_high - q_low, eps) |
| 100 | return 2.0 * (x - q_low) / denom - 1.0 |
| 101 | return x |
| 102 | |
| 103 | |
| 104 | def _unnormalize_array( |