Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NibabelIOWithReorient saver function #2478

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions nnunetv2/imageio/nibabel_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import warnings
from typing import Tuple, Union, List
import numpy as np
from nibabel import io_orientation
from nibabel.orientations import io_orientation, axcodes2ornt, ornt_transform

from nnunetv2.imageio.base_reader_writer import BaseReaderWriter
import nibabel
Expand Down Expand Up @@ -174,7 +174,11 @@ def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> Non
seg = seg.transpose((2, 1, 0)).astype(np.uint8, copy=False)

seg_nib = nibabel.Nifti1Image(seg, affine=properties['nibabel_stuff']['reoriented_affine'])
seg_nib_reoriented = seg_nib.as_reoriented(io_orientation(properties['nibabel_stuff']['original_affine']))
# Solution from https://github.com/nipy/nibabel/issues/1063#issuecomment-967124057
img_ornt = io_orientation(properties['nibabel_stuff']['original_affine'])
ras_ornt = axcodes2ornt("RAS")
from_canonical = ornt_transform(ras_ornt, img_ornt)
seg_nib_reoriented = seg_nib.as_reoriented(from_canonical)
if not np.allclose(properties['nibabel_stuff']['original_affine'], seg_nib_reoriented.affine):
print(f'WARNING: Restored affine does not match original affine. File: {output_fname}')
print(f'Original affine\n', properties['nibabel_stuff']['original_affine'])
Expand Down
Loading