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

Method get_data

monai/data/image_reader.py:1099–1141  ·  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. When loa

(self, img)

Source from the content-addressed store, hash-verified

1097 return img_ if len(filenames) > 1 else img_[0]
1098
1099 def get_data(self, img) -> tuple[np.ndarray, dict]:
1100 """
1101 Extract data array and metadata from loaded image and return them.
1102 This function returns two objects, first is numpy array of image data, second is dict of metadata.
1103 It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict.
1104 When loading a list of files, they are stacked together at a new dimension as the first dimension,
1105 and the metadata of the first image is used to present the output metadata. The returned arrays
1106 preserve the ordering in the original data, typically this is F-ordering for NIfTI files.
1107
1108 Args:
1109 img: a Nibabel image object loaded from an image file or a list of Nibabel image objects.
1110
1111 """
1112 img_array: list[NdarrayOrCupy] = []
1113 compatible_meta: dict = {}
1114
1115 for i, filename in zip(ensure_tuple(img), self.filenames):
1116 header = self._get_meta_dict(i)
1117 if MetaKeys.PIXDIM in header:
1118 header[MetaKeys.ORIGINAL_PIXDIM] = np.array(header[MetaKeys.PIXDIM], copy=True)
1119 header[MetaKeys.AFFINE] = self._get_affine(i)
1120 header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(i)
1121 header["as_closest_canonical"] = self.as_closest_canonical
1122 if self.as_closest_canonical:
1123 i = nib.as_closest_canonical(i)
1124 header[MetaKeys.AFFINE] = self._get_affine(i)
1125 header[MetaKeys.SPATIAL_SHAPE] = self._get_spatial_shape(i)
1126 header[MetaKeys.SPACE] = SpaceKeys.RAS
1127 data = self._get_array_data(i, filename)
1128 if self.squeeze_non_spatial_dims:
1129 for d in range(len(data.shape), len(header[MetaKeys.SPATIAL_SHAPE]), -1):
1130 if data.shape[d - 1] == 1:
1131 data = data.squeeze(axis=d - 1)
1132 img_array.append(data)
1133 if self.channel_dim is None: # default to "no_channel" or -1
1134 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = (
1135 float("nan") if len(data.shape) == len(header[MetaKeys.SPATIAL_SHAPE]) else -1
1136 )
1137 else:
1138 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = self.channel_dim
1139 _copy_compatible_dict(header, compatible_meta)
1140
1141 return _stack_images(img_array, compatible_meta, to_cupy=self.to_gpu), compatible_meta
1142
1143 def _get_meta_dict(self, img) -> dict:
1144 """

Calls 9

_get_meta_dictMethod · 0.95
_get_affineMethod · 0.95
_get_spatial_shapeMethod · 0.95
_get_array_dataMethod · 0.95
ensure_tupleFunction · 0.90
_copy_compatible_dictFunction · 0.85
_stack_imagesFunction · 0.85
arrayMethod · 0.80
appendMethod · 0.45