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

Function apply_transform

monai/transforms/transform.py:101–183  ·  view source on GitHub ↗

Transform `data` with `transform`. If `data` is a list or tuple and `map_data` is True, each item of `data` will be transformed and this method returns a list of outcomes. otherwise transform will be applied once with `data` as the argument. Args: transform: a callable

(
    transform: Callable[..., ReturnType],
    data: Any,
    map_items: bool | int = True,
    unpack_items: bool = False,
    log_stats: bool | str = False,
    lazy: bool | None = None,
    overrides: dict | None = None,
)

Source from the content-addressed store, hash-verified

99
100
101def apply_transform(
102 transform: Callable[..., ReturnType],
103 data: Any,
104 map_items: bool | int = True,
105 unpack_items: bool = False,
106 log_stats: bool | str = False,
107 lazy: bool | None = None,
108 overrides: dict | None = None,
109) -> list[Any] | ReturnType:
110 """
111 Transform `data` with `transform`.
112
113 If `data` is a list or tuple and `map_data` is True, each item of `data` will be transformed
114 and this method returns a list of outcomes.
115 otherwise transform will be applied once with `data` as the argument.
116
117 Args:
118 transform: a callable to be used to transform `data`.
119 data: an object to be transformed.
120 map_items: controls whether to apply a transformation to each item in `data`. If `data` is a list or tuple,
121 it can behave as follows:
122 - Defaults to True, which is equivalent to `map_items=1`, meaning the transformation will be applied
123 to the first level of items in `data`.
124 - If an integer is provided, it specifies the maximum level of nesting to which the transformation
125 should be recursively applied. This allows treating multi-sample transforms applied after another
126 multi-sample transform while controlling how deep the mapping goes.
127 unpack_items: whether to unpack parameters using `*`. Defaults to False.
128 log_stats: log errors when they occur in the processing pipeline. By default, this is set to False, which
129 disables the logger for processing pipeline errors. Setting it to None or True will enable logging to the
130 default logger name. Setting it to a string specifies the logger to which errors should be logged.
131 lazy: whether to execute in lazy mode or not. See the :ref:`Lazy Resampling topic<lazy_resampling> for more
132 information about lazy resampling. Defaults to None.
133 overrides: optional overrides to apply to transform parameters. This parameter is ignored unless transforms
134 are being executed lazily. See the :ref:`Lazy Resampling topic<lazy_resampling> for more details and
135 examples of its usage.
136
137 Raises:
138 Exception: When ``transform`` raises an exception.
139
140 Returns:
141 Union[List[ReturnType], ReturnType]: The return type of `transform` or a list thereof.
142 """
143 try:
144 map_items_ = int(map_items) if isinstance(map_items, bool) else map_items
145 if isinstance(data, (list, tuple)) and map_items_ > 0 and not isinstance(transform, ReduceTrait):
146 # If the transform is a Compose with its own map_items, let it handle list/tuple
147 # expansion internally so that nested Compose map_items settings are respected.
148 if not isinstance(transform, transforms.compose.Compose):
149 return [
150 apply_transform(transform, item, map_items_ - 1, unpack_items, log_stats, lazy, overrides)
151 for item in data
152 ]
153 return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
154 except Exception as e:
155 # if in debug mode, don't swallow exception so that the breakpoint
156 # appears where the exception was raised.
157 if MONAIEnvVars.debug():
158 raise

Callers 8

_transformMethod · 0.90
__getitem__Method · 0.90
__iter__Method · 0.90
__iter__Method · 0.90
_inverse_oneFunction · 0.90
execute_composeFunction · 0.90
engine_apply_transformFunction · 0.90

Calls 3

_apply_transformFunction · 0.85
_log_statsFunction · 0.85
debugMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…