@@ -149,10 +149,6 @@ def test_apply_linear_transform(
149
149
assert np .sqrt ((diff [brainmask ] ** 2 ).mean ()) < RMSE_TOL_LINEAR
150
150
151
151
152
- @pytest .mark .xfail (
153
- reason = "Disable while #266 is developed." ,
154
- strict = False ,
155
- )
156
152
@pytest .mark .parametrize ("image_orientation" , ["RAS" , "LAS" , "LPS" , "oblique" ])
157
153
@pytest .mark .parametrize ("sw_tool" , ["itk" , "afni" ])
158
154
@pytest .mark .parametrize ("axis" , [0 , 1 , 2 , (0 , 1 ), (1 , 2 ), (0 , 1 , 2 )])
@@ -174,29 +170,24 @@ def test_displacements_field1(
174
170
nii .to_filename ("reference.nii.gz" )
175
171
msk .to_filename ("mask.nii.gz" )
176
172
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" )
181
174
fieldmap [..., axis ] = - 10.0
182
175
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
188
177
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 )
193
183
184
+ tool_output = tmp_path / f"{ sw_tool } _brainmask.nii.gz"
194
185
# Then apply the transform and cross-check with software
195
186
cmd = APPLY_NONLINEAR_CMD [sw_tool ](
196
187
transform = os .path .abspath (xfm_fname ),
197
188
reference = tmp_path / "mask.nii.gz" ,
198
189
moving = tmp_path / "mask.nii.gz" ,
199
- output = tmp_path / "resampled_brainmask.nii.gz" ,
190
+ output = tool_output ,
200
191
extra = "--output-data-type uchar" if sw_tool == "itk" else "" ,
201
192
)
202
193
@@ -208,26 +199,28 @@ def test_displacements_field1(
208
199
# resample mask
209
200
exit_code = check_call ([cmd ], shell = True )
210
201
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 )
212
203
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" )
217
205
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
+ )
218
211
219
212
# Then apply the transform and cross-check with software
220
213
cmd = APPLY_NONLINEAR_CMD [sw_tool ](
221
214
transform = os .path .abspath (xfm_fname ),
222
215
reference = tmp_path / "reference.nii.gz" ,
223
216
moving = tmp_path / "reference.nii.gz" ,
224
- output = tmp_path / "resampled .nii.gz" ,
217
+ output = tmp_path / f" { sw_tool } _resampled .nii.gz" ,
225
218
extra = "--output-data-type uchar" if sw_tool == "itk" else "" ,
226
219
)
227
220
228
221
exit_code = check_call ([cmd ], shell = True )
229
222
assert exit_code == 0
230
- sw_moved = nb .load ("resampled .nii.gz" )
223
+ sw_moved = nb .load (f" { sw_tool } _resampled .nii.gz" )
231
224
232
225
nt_moved = apply (xfm , nii , order = 0 )
233
226
nt_moved .set_data_dtype (nii .get_data_dtype ())
@@ -240,10 +233,6 @@ def test_displacements_field1(
240
233
assert np .sqrt ((diff [brainmask ] ** 2 ).mean ()) < RMSE_TOL_LINEAR
241
234
242
235
243
- @pytest .mark .xfail (
244
- reason = "Disable while #266 is developed." ,
245
- strict = False ,
246
- )
247
236
@pytest .mark .parametrize ("sw_tool" , ["itk" , "afni" ])
248
237
def test_displacements_field2 (tmp_path , testdata_path , sw_tool ):
249
238
"""Check a translation-only field on one or more axes, different image orientations."""
0 commit comments