Skip to content

Commit

Permalink
print warning if affines do not match
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianIsensee committed Apr 30, 2024
1 parent d1706a3 commit 527719a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions nnunetv2/imageio/nibabel_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import warnings
from typing import Tuple, Union, List
import numpy as np
from nibabel import io_orientation
Expand Down Expand Up @@ -179,8 +179,10 @@ def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> Non

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']))
assert np.allclose(properties['nibabel_stuff']['original_affine'], seg_nib_reoriented.affine), \
'restored affine does not match original affine'
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'])
print(f'Restored affine\n', seg_nib_reoriented.affine)
nibabel.save(seg_nib_reoriented, output_fname)


Expand Down

0 comments on commit 527719a

Please sign in to comment.