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

Class OneOf

monai/transforms/compose.py:424–564  ·  view source on GitHub ↗

``OneOf`` provides the ability to randomly choose one transform out of a list of callables with pre-defined probabilities for each. Args: transforms: sequence of callables. weights: probabilities corresponding to each callable in transforms. Probabilities ar

Source from the content-addressed store, hash-verified

422
423
424class OneOf(Compose):
425 """
426 ``OneOf`` provides the ability to randomly choose one transform out of a
427 list of callables with pre-defined probabilities for each.
428
429 Args:
430 transforms: sequence of callables.
431 weights: probabilities corresponding to each callable in transforms.
432 Probabilities are normalized to sum to one.
433 map_items: controls whether to apply a transformation to each item in `data`. If `data` is a list or tuple,
434 it can behave as follows:
435
436 - Defaults to True, which is equivalent to `map_items=1`, meaning the transformation will be applied
437 to the first level of items in `data`.
438 - If an integer is provided, it specifies the maximum level of nesting to which the transformation
439 should be recursively applied. This allows treating multi-sample transforms applied after another
440 multi-sample transform while controlling how deep the mapping goes.
441 unpack_items: whether to unpack input `data` with `*` as parameters for the callable function of transform.
442 defaults to `False`.
443 log_stats: this optional parameter allows you to specify a logger by name for logging of pipeline execution.
444 Setting this to False disables logging. Setting it to True enables logging to the default loggers.
445 Setting a string overrides the logger name to which logging is performed.
446 lazy: whether to enable :ref:`Lazy Resampling<lazy_resampling>` for lazy transforms. If False, transforms will
447 be carried out on a transform by transform basis. If True, all lazy transforms will be executed by
448 accumulating changes and resampling as few times as possible. If lazy is None, `Compose` will
449 perform lazy execution on lazy transforms that have their `lazy` property set to True.
450 overrides: this optional parameter allows you to specify a dictionary of parameters that should be overridden
451 when executing a pipeline. These each parameter that is compatible with a given transform is then applied
452 to that transform before it is executed. Note that overrides are currently only applied when
453 :ref:`Lazy Resampling<lazy_resampling>` is enabled for the pipeline or a given transform. If lazy is False
454 they are ignored. Currently supported args are:
455 {``"mode"``, ``"padding_mode"``, ``"dtype"``, ``"align_corners"``, ``"resample_mode"``, ``device``}.
456 """
457
458 def __init__(
459 self,
460 transforms: Sequence[Callable] | Callable | None = None,
461 weights: Sequence[float] | float | None = None,
462 map_items: bool | int = True,
463 unpack_items: bool = False,
464 log_stats: bool | str = False,
465 lazy: bool | None = False,
466 overrides: dict | None = None,
467 ) -> None:
468 super().__init__(transforms, map_items, unpack_items, log_stats, lazy, overrides)
469 if len(self.transforms) == 0:
470 weights = []
471 elif weights is None or isinstance(weights, float):
472 weights = [1.0 / len(self.transforms)] * len(self.transforms)
473 if len(weights) != len(self.transforms):
474 raise ValueError(
475 "transforms and weights should be same size if both specified as sequences, "
476 f"got {len(weights)} and {len(self.transforms)}."
477 )
478 self.weights = ensure_tuple(self._normalize_probabilities(weights))
479 self.log_stats = log_stats
480
481 def _normalize_probabilities(self, weights):

Callers 12

test_one_of.pyFile · 0.90
test_no_weights_argMethod · 0.90
test_len_and_flattenMethod · 0.90
test_inverse_composeMethod · 0.90
test_one_ofMethod · 0.90
test_raisesMethod · 0.90
flattenMethod · 0.85

Calls

no outgoing calls

Tested by 9

test_no_weights_argMethod · 0.72
test_len_and_flattenMethod · 0.72
test_inverse_composeMethod · 0.72
test_one_ofMethod · 0.72
test_raisesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…