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

Class SplitDimd

monai/transforms/utility/dictionary.py:341–404  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339
340
341class SplitDimd(MapTransform, MultiSampleTrait):
342 backend = SplitDim.backend
343
344 def __init__(
345 self,
346 keys: KeysCollection,
347 output_postfixes: Sequence[str] | None = None,
348 dim: int = 0,
349 keepdim: bool = True,
350 update_meta: bool = True,
351 list_output: bool = False,
352 allow_missing_keys: bool = False,
353 ) -> None:
354 """
355 Args:
356 keys: keys of the corresponding items to be transformed.
357 See also: :py:class:`monai.transforms.compose.MapTransform`
358 output_postfixes: the postfixes to construct keys to store split data.
359 for example: if the key of input data is `pred` and split 2 classes, the output
360 data keys will be: pred_(output_postfixes[0]), pred_(output_postfixes[1])
361 if None, using the index number: `pred_0`, `pred_1`, ... `pred_N`.
362 dim: which dimension of input image is the channel, default to 0.
363 keepdim: if `True`, output will have singleton in the split dimension. If `False`, this
364 dimension will be squeezed.
365 update_meta: if `True`, copy `[key]_meta_dict` for each output and update affine to
366 reflect the cropped image
367 list_output: it `True`, the output will be a list of dictionaries with the same keys as original.
368 allow_missing_keys: don't raise exception if key is missing.
369 """
370 super().__init__(keys, allow_missing_keys)
371 self.output_postfixes = output_postfixes
372 self.splitter = SplitDim(dim, keepdim, update_meta)
373 self.list_output = list_output
374 if self.list_output is None and self.output_postfixes is not None:
375 raise ValueError("`output_postfixes` should not be provided when `list_output` is `True`.")
376
377 def __call__(
378 self, data: Mapping[Hashable, torch.Tensor]
379 ) -> dict[Hashable, torch.Tensor] | list[dict[Hashable, torch.Tensor]]:
380 d = dict(data)
381 all_keys = list(set(self.key_iterator(d)))
382
383 if self.list_output:
384 output = []
385 results = [self.splitter(d[key]) for key in all_keys]
386 for row in zip(*results):
387 new_dict = dict(zip(all_keys, row))
388 # fill in the extra keys with unmodified data
389 for k in set(d.keys()).difference(set(all_keys)):
390 new_dict[k] = deepcopy(d[k])
391 output.append(new_dict)
392 return output
393
394 for key in all_keys:
395 rets = self.splitter(d[key])
396 postfixes: Sequence = list(range(len(rets))) if self.output_postfixes is None else self.output_postfixes
397 if len(postfixes) != len(rets):
398 raise ValueError(f"count of splits must match output_postfixes, {len(postfixes)} != {len(rets)}.")

Callers 2

test_correctMethod · 0.90
test_singletonMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_correctMethod · 0.72
test_singletonMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…