Sample a float from a provided range description.
(value: Union[float, int, Sequence, Mapping],
device: Optional[torch.device] = None,
generator: Optional[torch.Generator] = None)
| 20 | return f"{numel}" |
| 21 | |
| 22 | def sample_from_range(value: Union[float, int, Sequence, Mapping], |
| 23 | device: Optional[torch.device] = None, |
| 24 | generator: Optional[torch.Generator] = None): |
| 25 | """Sample a float from a provided range description.""" |
| 26 | if value is None: |
| 27 | return value |
| 28 | |
| 29 | if isinstance(value, (float, int)): |
| 30 | return float(value) |
| 31 | |
| 32 | low, high = None, None |
| 33 | if isinstance(value, Sequence) and not isinstance(value, (str, bytes)): |
| 34 | if len(value) != 2: |
| 35 | raise ValueError(f"Range specification must have length 2, got {value}.") |
| 36 | low, high = value |
| 37 | elif isinstance(value, Mapping): |
| 38 | if 'range' in value: |
| 39 | return sample_from_range(value['range'], device=device, generator=generator) |
| 40 | if 'min' in value and 'max' in value: |
| 41 | low, high = value['min'], value['max'] |
| 42 | elif 'low' in value and 'high' in value: |
| 43 | low, high = value['low'], value['high'] |
| 44 | else: |
| 45 | raise ValueError( |
| 46 | "Range dictionary must include either 'min'/'max', 'low'/'high', or 'range'." |
| 47 | ) |
| 48 | else: |
| 49 | raise TypeError(f"Unsupported range specification type: {type(value)}") |
| 50 | |
| 51 | low = float(low) |
| 52 | high = float(high) |
| 53 | if low > high: |
| 54 | low, high = high, low |
| 55 | if math.isclose(low, high, rel_tol=1e-8, abs_tol=1e-8): |
| 56 | return low |
| 57 | |
| 58 | rand_val = torch.rand((), device=device, generator=generator).item() |
| 59 | return low + (high - low) * rand_val |
| 60 | |
| 61 | |
| 62 | def create_ref_motion(ref_motion, |
no outgoing calls
no test coverage detected