@@ -149,10 +149,6 @@ def test_apply_linear_transform(
149149 assert np .sqrt ((diff [brainmask ] ** 2 ).mean ()) < RMSE_TOL_LINEAR
150150
151151
152- @pytest .mark .xfail (
153- reason = "Disable while #266 is developed." ,
154- strict = False ,
155- )
156152@pytest .mark .parametrize ("image_orientation" , ["RAS" , "LAS" , "LPS" , "oblique" ])
157153@pytest .mark .parametrize ("sw_tool" , ["itk" , "afni" ])
158154@pytest .mark .parametrize ("axis" , [0 , 1 , 2 , (0 , 1 ), (1 , 2 ), (0 , 1 , 2 )])
@@ -174,29 +170,24 @@ def test_displacements_field1(
174170 nii .to_filename ("reference.nii.gz" )
175171 msk .to_filename ("mask.nii.gz" )
176172
177- fieldmap = np .zeros (
178- (* nii .shape [:3 ], 1 , 3 ) if sw_tool != "fsl" else (* nii .shape [:3 ], 3 ),
179- dtype = "float32" ,
180- )
173+ fieldmap = np .zeros ((* nii .shape [:3 ], 3 ), dtype = "float32" )
181174 fieldmap [..., axis ] = - 10.0
182175
183- _hdr = nii .header .copy ()
184- if sw_tool in ("itk" ,):
185- _hdr .set_intent ("vector" )
186- _hdr .set_data_dtype ("float32" )
187-
176+ # Generate a transform file for the particular software
188177 xfm_fname = "warp.nii.gz"
189- field = nb .Nifti1Image (fieldmap , nii .affine , _hdr )
190- field .to_filename (xfm_fname )
191-
192- xfm = nitnl .load (xfm_fname , fmt = sw_tool )
178+ xfm = nitnl .DenseFieldTransform (
179+ fieldmap ,
180+ reference = nii ,
181+ )
182+ xfm .to_filename (xfm_fname , fmt = sw_tool )
193183
184+ tool_output = tmp_path / f"{ sw_tool } _brainmask.nii.gz"
194185 # Then apply the transform and cross-check with software
195186 cmd = APPLY_NONLINEAR_CMD [sw_tool ](
196187 transform = os .path .abspath (xfm_fname ),
197188 reference = tmp_path / "mask.nii.gz" ,
198189 moving = tmp_path / "mask.nii.gz" ,
199- output = tmp_path / "resampled_brainmask.nii.gz" ,
190+ output = tool_output ,
200191 extra = "--output-data-type uchar" if sw_tool == "itk" else "" ,
201192 )
202193
@@ -208,26 +199,28 @@ def test_displacements_field1(
208199 # resample mask
209200 exit_code = check_call ([cmd ], shell = True )
210201 assert exit_code == 0
211- sw_moved_mask = nb .load ("resampled_brainmask.nii.gz" )
202+ sw_moved_mask = np . asanyarray ( nb .load (tool_output ). dataobj , dtype = bool )
212203 nt_moved_mask = apply (xfm , msk , order = 0 )
213- nt_moved_mask .set_data_dtype (msk .get_data_dtype ())
214- diff = np .asanyarray (sw_moved_mask .dataobj ) - np .asanyarray (nt_moved_mask .dataobj )
215-
216- assert np .sqrt ((diff ** 2 ).mean ()) < RMSE_TOL_LINEAR
204+ nt_moved_mask .to_filename (tmp_path / "nit_brainmask.nii.gz" )
217205 brainmask = np .asanyarray (nt_moved_mask .dataobj , dtype = bool )
206+ percent_diff = (sw_moved_mask != brainmask )[5 :- 5 , 5 :- 5 , 5 :- 5 ].sum () / brainmask .size
207+
208+ assert percent_diff < 1e-8 , (
209+ f"Resampled masks differed by { percent_diff * 100 :0.2f} %."
210+ )
218211
219212 # Then apply the transform and cross-check with software
220213 cmd = APPLY_NONLINEAR_CMD [sw_tool ](
221214 transform = os .path .abspath (xfm_fname ),
222215 reference = tmp_path / "reference.nii.gz" ,
223216 moving = tmp_path / "reference.nii.gz" ,
224- output = tmp_path / "resampled .nii.gz" ,
217+ output = tmp_path / f" { sw_tool } _resampled .nii.gz" ,
225218 extra = "--output-data-type uchar" if sw_tool == "itk" else "" ,
226219 )
227220
228221 exit_code = check_call ([cmd ], shell = True )
229222 assert exit_code == 0
230- sw_moved = nb .load ("resampled .nii.gz" )
223+ sw_moved = nb .load (f" { sw_tool } _resampled .nii.gz" )
231224
232225 nt_moved = apply (xfm , nii , order = 0 )
233226 nt_moved .set_data_dtype (nii .get_data_dtype ())
@@ -240,10 +233,6 @@ def test_displacements_field1(
240233 assert np .sqrt ((diff [brainmask ] ** 2 ).mean ()) < RMSE_TOL_LINEAR
241234
242235
243- @pytest .mark .xfail (
244- reason = "Disable while #266 is developed." ,
245- strict = False ,
246- )
247236@pytest .mark .parametrize ("sw_tool" , ["itk" , "afni" ])
248237def test_displacements_field2 (tmp_path , testdata_path , sw_tool ):
249238 """Check a translation-only field on one or more axes, different image orientations."""
0 commit comments