From 527719a1b03c16e74834ff12396078dbaf8b4fb2 Mon Sep 17 00:00:00 2001 From: Fabian Isensee Date: Tue, 30 Apr 2024 16:55:28 +0200 Subject: [PATCH] print warning if affines do not match --- nnunetv2/imageio/nibabel_reader_writer.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nnunetv2/imageio/nibabel_reader_writer.py b/nnunetv2/imageio/nibabel_reader_writer.py index a7d288fc4..db90824e0 100644 --- a/nnunetv2/imageio/nibabel_reader_writer.py +++ b/nnunetv2/imageio/nibabel_reader_writer.py @@ -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 @@ -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)