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

Method get_data

monai/data/image_reader.py:279–310  ·  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

277 return img_ if len(filenames) > 1 else img_[0]
278
279 def get_data(self, img) -> tuple[np.ndarray, dict]:
280 """
281 Extract data array and metadata from loaded image and return them.
282 This function returns two objects, first is numpy array of image data, second is dict of metadata.
283 It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict.
284 When loading a list of files, they are stacked together at a new dimension as the first dimension,
285 and the metadata of the first image is used to represent the output metadata.
286
287 Args:
288 img: an ITK image object loaded from an image file or a list of ITK image objects.
289
290 """
291 img_array: list[np.ndarray] = []
292 compatible_meta: dict = {}
293
294 for i in ensure_tuple(img):
295 data = self._get_array_data(i)
296 img_array.append(data)
297 header = self._get_meta_dict(i)
298 header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(i, self.affine_lps_to_ras)
299 header[MetaKeys.SPACE] = SpaceKeys.RAS if self.affine_lps_to_ras else SpaceKeys.LPS
300 header[MetaKeys.AFFINE] = header[MetaKeys.ORIGINAL_AFFINE].copy()
301 header[MetaKeys.SPATIAL_SHAPE] = self._get_spatial_shape(i)
302 if self.channel_dim is None: # default to "no_channel" or -1
303 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = (
304 float("nan") if len(data.shape) == len(header[MetaKeys.SPATIAL_SHAPE]) else -1
305 )
306 else:
307 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = self.channel_dim
308 _copy_compatible_dict(header, compatible_meta)
309
310 return _stack_images(img_array, compatible_meta), compatible_meta
311
312 def _get_meta_dict(self, img) -> dict:
313 """

Callers 6

itk_image_to_metatensorFunction · 0.95
test_kwargsMethod · 0.95
_get_dataMethod · 0.45
__call__Method · 0.45
_get_patchMethod · 0.45

Calls 8

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

Tested by 1

test_kwargsMethod · 0.76