@@ -175,19 +175,20 @@ def test_displacements_field1(
175175 msk .to_filename ("mask.nii.gz" )
176176
177177 fieldmap = np .zeros (
178- (* nii .shape [:3 ], 1 , 3 ) if sw_tool != "fsl " else (* nii .shape [:3 ], 3 ),
178+ (* nii .shape [:3 ], 1 , 3 ) if sw_tool == "afni " else (* nii .shape [:3 ], 3 ),
179179 dtype = "float32" ,
180180 )
181181 fieldmap [..., axis ] = - 10.0
182-
183182 _hdr = nii .header .copy ()
184- if sw_tool in ("itk" ,):
185- _hdr .set_intent ("vector" )
186183 _hdr .set_data_dtype ("float32" )
187184
188- xfm_fname = "warp.nii.gz"
189185 field = nb .Nifti1Image (fieldmap , nii .affine , _hdr )
190- field .to_filename (xfm_fname )
186+
187+ xfm_fname = "warp.nii.gz"
188+ if sw_tool == "itk" :
189+ io .itk .ITKDisplacementsField .to_filename (field , xfm_fname )
190+ else :
191+ field .to_filename (xfm_fname )
191192
192193 xfm = nitnl .load (xfm_fname , fmt = sw_tool )
193194
@@ -197,7 +198,8 @@ def test_displacements_field1(
197198 reference = tmp_path / "mask.nii.gz" ,
198199 moving = tmp_path / "mask.nii.gz" ,
199200 output = tmp_path / "resampled_brainmask.nii.gz" ,
200- extra = "--output-data-type uchar" if sw_tool == "itk" else "" ,
201+ extra = "" ,
202+ # extra="--output-data-type uchar" if sw_tool == "itk" else "",
201203 )
202204
203205 # skip test if command is not available on host
@@ -207,14 +209,18 @@ def test_displacements_field1(
207209
208210 # resample mask
209211 exit_code = check_call ([cmd ], shell = True )
210- assert exit_code == 0
211212 sw_moved_mask = nb .load ("resampled_brainmask.nii.gz" )
212213 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 )
215214
216- assert np .sqrt ((diff ** 2 ).mean ()) < RMSE_TOL_LINEAR
215+ # Calculate xor between both:
216+ sw_mask = np .asanyarray (sw_moved_mask .dataobj , dtype = bool )
217217 brainmask = np .asanyarray (nt_moved_mask .dataobj , dtype = bool )
218+ percent_diff = (sw_mask != brainmask )[5 :- 5 , 5 :- 5 , 5 :- 5 ].sum () / brainmask .size
219+
220+ assert exit_code == 0
221+ assert percent_diff < 1e-8 , (
222+ f"Resampled masks differed by { percent_diff * 100 :0.2f} %."
223+ )
218224
219225 # Then apply the transform and cross-check with software
220226 cmd = APPLY_NONLINEAR_CMD [sw_tool ](
@@ -236,8 +242,9 @@ def test_displacements_field1(
236242 diff = np .asanyarray (
237243 sw_moved .dataobj , dtype = sw_moved .get_data_dtype ()
238244 ) - np .asanyarray (nt_moved .dataobj , dtype = nt_moved .get_data_dtype ())
239- # A certain tolerance is necessary because of resampling at borders
240- assert np .sqrt ((diff [brainmask ] ** 2 ).mean ()) < RMSE_TOL_LINEAR
245+
246+ # Drop samples close to the border of the image
247+ assert np .sqrt ((diff [5 :- 5 , 5 :- 5 , 5 :- 5 ] ** 2 ).mean ()) < 1e-6
241248
242249
243250@pytest .mark .xfail (
0 commit comments