Download real_robot/source/retime/scoring/residual_cache.py from mmmmmcyaaa/ReTime: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/real_robot/source/retime/scoring/residual_cache.py
- Command line
-
hf download hf://mmmmmcyaaa/ReTime/real_robot/source/retime/scoring/residual_cache.py
-
curl -L -o residual_cache.py https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/real_robot/source/retime/scoring/residual_cache.py
2.82 kB
| """Deterministic grid scoring and interpolation for residual proposals.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| from scipy.stats import rankdata | |
| def quantile_anchor_positions(anchor_count: int, grid_size: int) -> np.ndarray: | |
| """Select fixed, unique, endpoint-covering positions within one episode.""" | |
| if anchor_count <= 0: | |
| raise ValueError("anchor_count must be positive") | |
| if grid_size <= 0: | |
| raise ValueError("grid_size must be positive") | |
| selected_count = min(int(anchor_count), int(grid_size)) | |
| positions = np.linspace( | |
| 0, | |
| anchor_count - 1, | |
| selected_count, | |
| dtype=np.int64, | |
| ) | |
| if len(np.unique(positions)) != selected_count: | |
| raise RuntimeError("quantile anchor selection produced duplicates") | |
| return positions | |
| def interpolate_grid_scores( | |
| anchor_count: int, | |
| grid_positions: np.ndarray, | |
| grid_scores: np.ndarray, | |
| ) -> np.ndarray: | |
| """Linearly interpolate frozen grid scores over all episode anchors.""" | |
| if anchor_count <= 0: | |
| raise ValueError("anchor_count must be positive") | |
| positions = np.asarray(grid_positions, dtype=np.int64) | |
| scores = np.asarray(grid_scores, dtype=np.float64) | |
| if ( | |
| positions.ndim != 1 | |
| or scores.ndim != 1 | |
| or positions.shape != scores.shape | |
| or positions.size == 0 | |
| ): | |
| raise ValueError("grid positions and scores must be aligned non-empty vectors") | |
| if not np.isfinite(scores).all(): | |
| raise ValueError("grid scores must be finite") | |
| if np.any(positions < 0) or np.any(positions >= anchor_count): | |
| raise ValueError("grid positions are outside the episode") | |
| if np.any(np.diff(positions) <= 0): | |
| raise ValueError("grid positions must be strictly increasing") | |
| if positions[0] != 0 or positions[-1] != anchor_count - 1: | |
| raise ValueError("grid positions must cover both episode endpoints") | |
| if anchor_count == 1: | |
| return scores.copy() | |
| return np.interp( | |
| np.arange(anchor_count, dtype=np.float64), | |
| positions.astype(np.float64), | |
| scores, | |
| ) | |
| def percentile_intensity_masses( | |
| scores: np.ndarray, | |
| *, | |
| epsilon: float, | |
| ) -> np.ndarray: | |
| """Convert episode-local scores into epsilon-floored percentile masses.""" | |
| values = np.asarray(scores, dtype=np.float64) | |
| if values.ndim != 1 or values.size == 0 or not np.isfinite(values).all(): | |
| raise ValueError("scores must be a finite non-empty vector") | |
| if epsilon <= 0.0: | |
| raise ValueError("epsilon must be positive") | |
| if values.size == 1: | |
| percentiles = np.zeros(1, dtype=np.float64) | |
| else: | |
| percentiles = (rankdata(values, method="average") - 1.0) / ( | |
| values.size - 1.0 | |
| ) | |
| return float(epsilon) + percentiles | |