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

Method get_data

monai/data/image_reader.py:1401–1428  ·  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 computes `spatial_shape` and stores it in meta dict. When loading a list of files, they are stacke

(self, img)

Source from the content-addressed store, hash-verified

1399 return img_ if len(filenames) > 1 else img_[0]
1400
1401 def get_data(self, img) -> tuple[np.ndarray, dict]:
1402 """
1403 Extract data array and metadata from loaded image and return them.
1404 This function returns two objects, first is numpy array of image data, second is dict of metadata.
1405 It computes `spatial_shape` and stores it in meta dict.
1406 When loading a list of files, they are stacked together at a new dimension as the first dimension,
1407 and the metadata of the first image is used to represent the output metadata.
1408 Note that by default `self.reverse_indexing` is set to ``True``, which swaps axis 0 and 1 after loading
1409 the array because the spatial axes definition in PIL is different from other common medical packages.
1410
1411 Args:
1412 img: a PIL Image object loaded from a file or a list of PIL Image objects.
1413
1414 """
1415 img_array: list[np.ndarray] = []
1416 compatible_meta: dict = {}
1417
1418 for i in ensure_tuple(img):
1419 header = self._get_meta_dict(i)
1420 header[MetaKeys.SPATIAL_SHAPE] = self._get_spatial_shape(i)
1421 data = np.moveaxis(np.asarray(i), 0, 1) if self.reverse_indexing else np.asarray(i)
1422 img_array.append(data)
1423 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = (
1424 float("nan") if len(data.shape) == len(header[MetaKeys.SPATIAL_SHAPE]) else -1
1425 )
1426 _copy_compatible_dict(header, compatible_meta)
1427
1428 return _stack_images(img_array, compatible_meta), compatible_meta
1429
1430 def _get_meta_dict(self, img) -> dict:
1431 """

Callers 2

test_shape_valueMethod · 0.95
test_converterMethod · 0.95

Calls 6

_get_meta_dictMethod · 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 2

test_shape_valueMethod · 0.76
test_converterMethod · 0.76