Refine `user_provided` according to the `default`, and returns as a validated tuple. The validation is done for each element in `user_provided` using `func`. If `func(user_provided[idx])` returns False, the corresponding `default[idx]` will be used as the fallback. Typically u
(
user_provided: Any, default: Sequence | NdarrayTensor, func: Callable = lambda x: x and x > 0
)
| 258 | |
| 259 | |
| 260 | def fall_back_tuple( |
| 261 | user_provided: Any, default: Sequence | NdarrayTensor, func: Callable = lambda x: x and x > 0 |
| 262 | ) -> tuple[Any, ...]: |
| 263 | """ |
| 264 | Refine `user_provided` according to the `default`, and returns as a validated tuple. |
| 265 | |
| 266 | The validation is done for each element in `user_provided` using `func`. |
| 267 | If `func(user_provided[idx])` returns False, the corresponding `default[idx]` will be used |
| 268 | as the fallback. |
| 269 | |
| 270 | Typically used when `user_provided` is a tuple of window size provided by the user, |
| 271 | `default` is defined by data, this function returns an updated `user_provided` with its non-positive |
| 272 | components replaced by the corresponding components from `default`. |
| 273 | |
| 274 | Args: |
| 275 | user_provided: item to be validated. |
| 276 | default: a sequence used to provided the fallbacks. |
| 277 | func: a Callable to validate every components of `user_provided`. |
| 278 | |
| 279 | Examples:: |
| 280 | |
| 281 | >>> fall_back_tuple((1, 2), (32, 32)) |
| 282 | (1, 2) |
| 283 | >>> fall_back_tuple(None, (32, 32)) |
| 284 | (32, 32) |
| 285 | >>> fall_back_tuple((-1, 10), (32, 32)) |
| 286 | (32, 10) |
| 287 | >>> fall_back_tuple((-1, None), (32, 32)) |
| 288 | (32, 32) |
| 289 | >>> fall_back_tuple((1, None), (32, 32)) |
| 290 | (1, 32) |
| 291 | >>> fall_back_tuple(0, (32, 32)) |
| 292 | (32, 32) |
| 293 | >>> fall_back_tuple(range(3), (32, 64, 48)) |
| 294 | (32, 1, 2) |
| 295 | >>> fall_back_tuple([0], (32, 32)) |
| 296 | ValueError: Sequence must have length 2, got length 1. |
| 297 | |
| 298 | """ |
| 299 | ndim = len(default) |
| 300 | user = ensure_tuple_rep(user_provided, ndim) |
| 301 | return tuple( # use the default values if user provided is not valid |
| 302 | user_c if func(user_c) else default_c for default_c, user_c in zip(default, user) |
| 303 | ) |
| 304 | |
| 305 | |
| 306 | def is_scalar_tensor(val: Any) -> bool: |
searching dependent graphs…