Skip to content

Commit e766401

Browse files
authored
3881 using sform/qform code 1 for the writers (#3882)
Fixes #3881 Follow-up of Project-MONAI/tutorials#481 ### Description following https://github.com/InsightSoftwareConsortium/ITK/blob/15aa8d58a01a1279e1fafa2214c82edd80b17e92/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L2174-L2176 ### Status **Ready** ### 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). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 775935d commit e766401

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

monai/data/image_writer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ def write(self, filename: PathLike, verbose: bool = False, **obj_kwargs):
601601
self.data_obj = self.create_backend_obj(
602602
self.data_obj, affine=self.affine, dtype=self.output_dtype, **obj_kwargs # type: ignore
603603
)
604+
if self.affine is None:
605+
self.affine = np.eye(4)
606+
# ITK v5.2.1/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L2175-L2176
607+
_affine = to_affine_nd(r=3, affine=convert_data_type(self.affine, np.ndarray)[0])
608+
self.data_obj.set_sform(_affine, code=1)
609+
self.data_obj.set_qform(_affine, code=1)
604610
nib.save(self.data_obj, filename)
605611

606612
@classmethod

tests/test_image_rw.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def nifti_rw(self, test_data, reader, writer, dtype, resample=True):
6565
_test_data = test_data[0]
6666
if resample:
6767
_test_data = moveaxis(_test_data, 0, 1)
68+
assert_allclose(meta["qform_code"], 1, type_test=False)
69+
assert_allclose(meta["sform_code"], 1, type_test=False)
6870
assert_allclose(data, torch.as_tensor(_test_data))
6971

7072
@parameterized.expand(itertools.product([NibabelReader, ITKReader], [NibabelWriter, "ITKWriter"]))

tests/testing_data/integration_answers.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,64 @@
433433
"infer_metric": 0.9326590299606323,
434434
},
435435
},
436+
{ # test answers for PyTorch 1.13
437+
"integration_workflows": {
438+
"output_sums_2": [
439+
0.14264830205979873,
440+
0.15264129328718357,
441+
0.1519652511118344,
442+
0.14003114557361543,
443+
0.18870416611118465,
444+
0.1699260498246968,
445+
0.14727475398203582,
446+
0.16870874483246967,
447+
0.15757932277023196,
448+
0.1797779694564011,
449+
0.16310501082450635,
450+
0.16850569170136015,
451+
0.14472958359864832,
452+
0.11402527744419455,
453+
0.16217657428257873,
454+
0.20135486560244975,
455+
0.17627557567092866,
456+
0.09802074024435596,
457+
0.19418729084978026,
458+
0.20339278025379662,
459+
0.1966174446916041,
460+
0.20872528599049203,
461+
0.16246183433492764,
462+
0.1323750751202327,
463+
0.14830347036335728,
464+
0.14300732028781024,
465+
0.23163101813922762,
466+
0.1612925258625139,
467+
0.1489573676973957,
468+
0.10299491921717041,
469+
0.11921404797064328,
470+
0.1300212751422368,
471+
0.11437829790254125,
472+
0.1524755276727056,
473+
0.16350584736767904,
474+
0.19424317961257148,
475+
0.2229762916892286,
476+
0.18121074825540173,
477+
0.19064286213535897,
478+
0.0747544243069024,
479+
]
480+
},
481+
"integration_segmentation_3d": { # for the mixed readers
482+
"losses": [
483+
0.5451162219047546,
484+
0.4709601759910583,
485+
0.45201429128646853,
486+
0.4443251401185989,
487+
0.4341257899999619,
488+
0.4350819975137711,
489+
],
490+
"best_metric": 0.9316844940185547,
491+
"infer_metric": 0.9316383600234985,
492+
},
493+
},
436494
{ # test answers for PyTorch 21.10
437495
"integration_classification_2d": {
438496
"losses": [0.7806222991199251, 0.16259610306495315, 0.07529311385124353, 0.04640352608529246],

0 commit comments

Comments
 (0)