@@ -175,19 +175,20 @@ def test_displacements_field1(
175
175
msk .to_filename ("mask.nii.gz" )
176
176
177
177
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 ),
179
179
dtype = "float32" ,
180
180
)
181
181
fieldmap [..., axis ] = - 10.0
182
-
183
182
_hdr = nii .header .copy ()
184
- if sw_tool in ("itk" ,):
185
- _hdr .set_intent ("vector" )
186
183
_hdr .set_data_dtype ("float32" )
187
184
188
- xfm_fname = "warp.nii.gz"
189
185
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 )
191
192
192
193
xfm = nitnl .load (xfm_fname , fmt = sw_tool )
193
194
@@ -197,7 +198,8 @@ def test_displacements_field1(
197
198
reference = tmp_path / "mask.nii.gz" ,
198
199
moving = tmp_path / "mask.nii.gz" ,
199
200
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 "",
201
203
)
202
204
203
205
# skip test if command is not available on host
@@ -207,14 +209,18 @@ def test_displacements_field1(
207
209
208
210
# resample mask
209
211
exit_code = check_call ([cmd ], shell = True )
210
- assert exit_code == 0
211
212
sw_moved_mask = nb .load ("resampled_brainmask.nii.gz" )
212
213
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
214
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 )
217
217
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
+ )
218
224
219
225
# Then apply the transform and cross-check with software
220
226
cmd = APPLY_NONLINEAR_CMD [sw_tool ](
@@ -236,8 +242,9 @@ def test_displacements_field1(
236
242
diff = np .asanyarray (
237
243
sw_moved .dataobj , dtype = sw_moved .get_data_dtype ()
238
244
) - 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
241
248
242
249
243
250
@pytest .mark .xfail (
0 commit comments