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

Method get_data

monai/data/image_reader.py:626–701  ·  view source on GitHub ↗

Extract data array and metadata from loaded image and return them. This function returns two objects, first is numpy array of image data, second is dict of metadata. It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict. For dico

(self, data)

Source from the content-addressed store, hash-verified

624 return stack_array, stack_metadata
625
626 def get_data(self, data) -> tuple[np.ndarray, dict]:
627 """
628 Extract data array and metadata from loaded image and return them.
629 This function returns two objects, first is numpy array of image data, second is dict of metadata.
630 It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict.
631 For dicom series within the input, all slices will be stacked first,
632 When loading a list of files (dicom file, or stacked dicom series), they are stacked together at a new
633 dimension as the first dimension, and the metadata of the first image is used to represent the output metadata.
634
635 To use this function, all pydicom dataset objects (if not segmentation data) should contain:
636 `pixel_array`, `PixelSpacing`, `ImagePositionPatient` and `ImageOrientationPatient`.
637
638 For segmentation data, we assume that the input is not a dicom series, and the object should contain
639 `SegmentSequence` in order to identify it.
640 In addition, tags (5200, 9229) and (5200, 9230) are required to achieve
641 `PixelSpacing`, `ImageOrientationPatient` and `ImagePositionPatient`.
642
643 Args:
644 data: a pydicom dataset object, or a list of pydicom dataset objects, or a list of list of
645 pydicom dataset objects.
646
647 """
648
649 dicom_data = []
650 # combine dicom series if exists
651 if self.has_series is True:
652 # a list, all objects within a list belong to one dicom series
653 if not isinstance(data[0], list):
654 # input is a dir, self.filenames is a list of list of filenames
655 dicom_data.append(self._combine_dicom_series(data, self.filenames[0])) # type: ignore
656 # a list of list, each inner list represents a dicom series
657 else:
658 for i, series in enumerate(data):
659 dicom_data.append(self._combine_dicom_series(series, self.filenames[i])) # type: ignore
660 else:
661 # a single pydicom dataset object
662 if not isinstance(data, list):
663 data = [data]
664 for i, d in enumerate(data):
665 if hasattr(d, "SegmentSequence"):
666 data_array, metadata = self._get_seg_data(d, self.filenames[i])
667 else:
668 data_array = self._get_array_data(d, self.filenames[i])
669 metadata = self._get_meta_dict(d)
670 metadata[MetaKeys.SPATIAL_SHAPE] = data_array.shape
671 dicom_data.append((data_array, metadata))
672
673 img_array: list[NdarrayOrCupy] = []
674 compatible_meta: dict = {}
675
676 for data_array, metadata in ensure_tuple(dicom_data):
677 if self.swap_ij:
678 data_array = cp.swapaxes(data_array, 0, 1) if self.to_gpu else np.swapaxes(data_array, 0, 1)
679 img_array.append(cp.ascontiguousarray(data_array) if self.to_gpu else np.ascontiguousarray(data_array))
680 affine = self._get_affine(metadata, self.affine_lps_to_ras)
681 metadata[MetaKeys.SPACE] = SpaceKeys.RAS if self.affine_lps_to_ras else SpaceKeys.LPS
682 if self.swap_ij:
683 affine = affine @ np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])

Callers

nothing calls this directly

Calls 11

_combine_dicom_seriesMethod · 0.95
_get_seg_dataMethod · 0.95
_get_array_dataMethod · 0.95
_get_meta_dictMethod · 0.95
_get_affineMethod · 0.95
ensure_tupleFunction · 0.90
affine_to_spacingFunction · 0.90
_copy_compatible_dictFunction · 0.85
_stack_imagesFunction · 0.85
arrayMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected