Parse tracks from a JSON string, handling nested/wrapped formats.
(raw: str)
| 140 | |
| 141 | |
| 142 | def _parse_tracks(raw: str) -> list[list[dict]]: |
| 143 | """Parse tracks from a JSON string, handling nested/wrapped formats.""" |
| 144 | parsed = json.loads(raw) if isinstance(raw, str) else raw |
| 145 | |
| 146 | if isinstance(parsed, list): |
| 147 | unwrapped = [] |
| 148 | for item in parsed: |
| 149 | unwrapped.append(json.loads(item) if isinstance(item, str) else item) |
| 150 | parsed = unwrapped |
| 151 | |
| 152 | tracks: list[list[dict]] = [] |
| 153 | stack = [parsed] |
| 154 | while stack: |
| 155 | obj = stack.pop() |
| 156 | if isinstance(obj, list) and len(obj) > 0: |
| 157 | if isinstance(obj[0], dict) and "x" in obj[0] and "y" in obj[0]: |
| 158 | tracks.append(obj) |
| 159 | else: |
| 160 | stack.extend(obj) |
| 161 | return tracks |
| 162 | |
| 163 | |
| 164 | def _age_color_batch(ratios: torch.Tensor, device: torch.device) -> torch.Tensor: |