Skip to content

Commit

Permalink
Add affine_lps_to_ras argument to NrrdReader (Project-MONAI#6074)
Browse files Browse the repository at this point in the history
Fixes Project-MONAI#6073

### Description

Add a affine_lps_to_ras argument to NrrdReader controlling whether to
convert the affine matrix from "LPS" to "RAS" or not. The naming and
functionality is identical to the argument used by PydicomReader and
ITKReader

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [x] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Johannes Ulén <[email protected]>
  • Loading branch information
johannesu authored Feb 27, 2023
1 parent 26f100a commit 6d19992
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,9 @@ class NrrdReader(ImageReader):
dtype: dtype of the data array when loading image.
index_order: Specify whether the returned data array should be in C-order (‘C’) or Fortran-order (‘F’).
Numpy is usually in C-order, but default on the NRRD header is F
affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``True``.
Set to ``True`` to be consistent with ``NibabelReader``, otherwise the affine matrix is unmodified.
kwargs: additional args for `nrrd.read` API. more details about available args:
https://github.com/mhe/pynrrd/blob/master/nrrd/reader.py
Expand All @@ -1532,11 +1535,13 @@ def __init__(
channel_dim: int | None = None,
dtype: np.dtype | type | str | None = np.float32,
index_order: str = "F",
affine_lps_to_ras: bool = True,
**kwargs,
):
self.channel_dim = channel_dim
self.dtype = dtype
self.index_order = index_order
self.affine_lps_to_ras = affine_lps_to_ras
self.kwargs = kwargs

def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool:
Expand Down Expand Up @@ -1590,7 +1595,10 @@ def get_data(self, img: NrrdImage | list[NrrdImage]) -> tuple[np.ndarray, dict]:
if self.index_order == "C":
header = self._convert_f_to_c_order(header)
header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(i)
header = self._switch_lps_ras(header)

if self.affine_lps_to_ras:
header = self._switch_lps_ras(header)

header[MetaKeys.AFFINE] = header[MetaKeys.ORIGINAL_AFFINE].copy()
header[MetaKeys.SPATIAL_SHAPE] = header["sizes"]
[header.pop(k) for k in ("sizes", "space origin", "space directions")] # rm duplicated data in header
Expand Down

0 comments on commit 6d19992

Please sign in to comment.