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

Method flatten

monai/transforms/compose.py:342–368  ·  view source on GitHub ↗

Return a Composition with a flattened list of transforms. Nested ``Compose`` objects that share the same ``map_items`` setting as the parent are inlined. Nested ``Compose`` objects with a *different* ``map_items`` value are kept as-is so their item-mapping behaviour is

(self)

Source from the content-addressed store, hash-verified

340 return None
341
342 def flatten(self):
343 """Return a Composition with a flattened list of transforms.
344
345 Nested ``Compose`` objects that share the same ``map_items`` setting as
346 the parent are inlined. Nested ``Compose`` objects with a *different*
347 ``map_items`` value are kept as-is so their item-mapping behaviour is
348 preserved at runtime and during inversion.
349
350 e.g., `t1 = Compose([x, x, x, x, Compose([Compose([x, x]), x, x])]).flatten()`
351 will result in the equivalent of `t1 = Compose([x, x, x, x, x, x, x, x])`.
352
353 """
354 new_transforms = []
355 for t in self.transforms:
356 if type(t) is Compose and t.map_items == self.map_items:
357 new_transforms += t.flatten().transforms
358 else:
359 new_transforms.append(t)
360
361 return Compose(
362 new_transforms,
363 map_items=self.map_items,
364 unpack_items=self.unpack_items,
365 log_stats=self.log_stats,
366 lazy=self._lazy,
367 overrides=self.overrides,
368 )
369
370 def __len__(self):
371 """Return number of transformations."""

Calls 3

ComposeClass · 0.85
flattenMethod · 0.45
appendMethod · 0.45