Extract data array and metadata from loaded image and return them. This function must return two objects, the first is a numpy array of image data, the second is a dictionary of metadata. Args: img: a `NrrdImage` loaded from an image file or a list of im
(self, img: NrrdImage | list[NrrdImage])
| 1520 | return img_ if len(filenames) > 1 else img_[0] |
| 1521 | |
| 1522 | def get_data(self, img: NrrdImage | list[NrrdImage]) -> tuple[np.ndarray, dict]: |
| 1523 | """ |
| 1524 | Extract data array and metadata from loaded image and return them. |
| 1525 | This function must return two objects, the first is a numpy array of image data, |
| 1526 | the second is a dictionary of metadata. |
| 1527 | |
| 1528 | Args: |
| 1529 | img: a `NrrdImage` loaded from an image file or a list of image objects. |
| 1530 | |
| 1531 | """ |
| 1532 | img_array: list[np.ndarray] = [] |
| 1533 | compatible_meta: dict = {} |
| 1534 | |
| 1535 | for i in ensure_tuple(img): |
| 1536 | data = i.array.astype(self.dtype) |
| 1537 | img_array.append(data) |
| 1538 | header = dict(i.header) |
| 1539 | if self.index_order == "C": |
| 1540 | header = self._convert_f_to_c_order(header) |
| 1541 | header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(header) |
| 1542 | |
| 1543 | if self.affine_lps_to_ras: |
| 1544 | header = self._switch_lps_ras(header) |
| 1545 | if header.get(MetaKeys.SPACE, "left-posterior-superior") == "left-posterior-superior": |
| 1546 | header[MetaKeys.SPACE] = SpaceKeys.LPS # assuming LPS if not specified |
| 1547 | |
| 1548 | header[MetaKeys.AFFINE] = header[MetaKeys.ORIGINAL_AFFINE].copy() |
| 1549 | header[MetaKeys.SPATIAL_SHAPE] = header["sizes"].copy() |
| 1550 | [header.pop(k) for k in ("sizes", "space origin", "space directions")] # rm duplicated data in header |
| 1551 | |
| 1552 | if self.channel_dim is None: # default to "no_channel" or -1 |
| 1553 | header[MetaKeys.ORIGINAL_CHANNEL_DIM] = ( |
| 1554 | float("nan") if len(data.shape) == len(header[MetaKeys.SPATIAL_SHAPE]) else 0 |
| 1555 | ) |
| 1556 | else: |
| 1557 | header[MetaKeys.ORIGINAL_CHANNEL_DIM] = self.channel_dim |
| 1558 | _copy_compatible_dict(header, compatible_meta) |
| 1559 | |
| 1560 | return _stack_images(img_array, compatible_meta), compatible_meta |
| 1561 | |
| 1562 | def _get_affine(self, header: dict) -> np.ndarray: |
| 1563 | """ |