MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / fall_back_tuple

Function fall_back_tuple

monai/utils/misc.py:260–303  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

258
259
260def 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
306def is_scalar_tensor(val: Any) -> bool:

Callers 15

__call__Method · 0.90
__call__Method · 0.90
zoom_affineFunction · 0.90
resize_centerFunction · 0.90
weighted_patch_samplesFunction · 0.90
correct_crop_centersFunction · 0.90
randomizeMethod · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90
spatial_resampleFunction · 0.90

Calls 1

ensure_tuple_repFunction · 0.85

Tested by 3

test_valueMethod · 0.72
test_spacingMethod · 0.72
test_valueMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…