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

Method get_data

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

1306 return img_ if len(img_) > 1 else img_[0]
1307
1308 def get_data(self, img) -> tuple[np.ndarray, dict]:
1309 """
1310 Extract data array and metadata from loaded image and return them.
1311 This function returns two objects, first is numpy array of image data, second is dict of metadata.
1312 It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict.
1313 When loading a list of files, they are stacked together at a new dimension as the first dimension,
1314 and the metadata of the first image is used to represent the output metadata.
1315
1316 Args:
1317 img: a Numpy array loaded from a file or a list of Numpy arrays.
1318
1319 """
1320 img_array: list[np.ndarray] = []
1321 compatible_meta: dict = {}
1322 if isinstance(img, np.ndarray):
1323 img = (img,)
1324
1325 for i in ensure_tuple(img):
1326 header: dict[MetaKeys, Any] = {}
1327 if isinstance(i, np.ndarray):
1328 # if `channel_dim` is None, can not detect the channel dim, use all the dims as spatial_shape
1329 spatial_shape = np.asarray(i.shape)
1330 if isinstance(self.channel_dim, int):
1331 spatial_shape = np.delete(spatial_shape, self.channel_dim)
1332 header[MetaKeys.SPATIAL_SHAPE] = spatial_shape
1333 header[MetaKeys.SPACE] = SpaceKeys.RAS
1334 img_array.append(i)
1335 header[MetaKeys.ORIGINAL_CHANNEL_DIM] = (
1336 self.channel_dim if isinstance(self.channel_dim, int) else float("nan")
1337 )
1338 _copy_compatible_dict(header, compatible_meta)
1339
1340 return _stack_images(img_array, compatible_meta), compatible_meta
1341
1342
1343@require_pkg(pkg_name="PIL")

Callers 7

test_npyMethod · 0.95
test_npz1Method · 0.95
test_npz2Method · 0.95
test_npz3Method · 0.95
test_npy_pickleMethod · 0.95
test_kwargsMethod · 0.95
test_channel_dimMethod · 0.95

Calls 4

ensure_tupleFunction · 0.90
_copy_compatible_dictFunction · 0.85
_stack_imagesFunction · 0.85
appendMethod · 0.45

Tested by 7

test_npyMethod · 0.76
test_npz1Method · 0.76
test_npz2Method · 0.76
test_npz3Method · 0.76
test_npy_pickleMethod · 0.76
test_kwargsMethod · 0.76
test_channel_dimMethod · 0.76