47
47
BOLD_PATH = "ds054/sub-100185/func/sub-100185_task-machinegame_run-01_bold.nii.gz"
48
48
49
49
50
+ @pytest .mark .parametrize ("interface" , [bintfs .DerivativesDataSink , bintfs .PrepareDerivative ])
50
51
@pytest .mark .parametrize ("out_path_base" , [None , "fmriprep" ])
51
52
@pytest .mark .parametrize (
52
53
"source,input_files,entities,expectation,checksum" ,
258
259
@pytest .mark .parametrize ("dismiss_entities" , [None , ("run" , "session" )])
259
260
def test_DerivativesDataSink_build_path (
260
261
tmp_path ,
262
+ interface ,
261
263
out_path_base ,
262
264
source ,
263
265
input_files ,
@@ -267,6 +269,8 @@ def test_DerivativesDataSink_build_path(
267
269
dismiss_entities ,
268
270
):
269
271
"""Check a few common derivatives generated by NiPreps."""
272
+ if interface is bintfs .PrepareDerivative and out_path_base is not None :
273
+ pytest .skip ("PrepareDerivative does not support out_path_base" )
270
274
ds_inputs = []
271
275
for input_file in input_files :
272
276
fname = tmp_path / input_file
@@ -291,24 +295,41 @@ def test_DerivativesDataSink_build_path(
291
295
292
296
ds_inputs .append (str (fname ))
293
297
294
- dds = bintfs .DerivativesDataSink (
298
+ base_directory = tmp_path / "output"
299
+ work_dir = tmp_path / "work"
300
+ base_directory .mkdir ()
301
+ work_dir .mkdir ()
302
+
303
+ prep = save = interface (
295
304
in_file = ds_inputs ,
296
- base_directory = str (tmp_path ),
297
305
source_file = source ,
298
- out_path_base = out_path_base ,
299
306
dismiss_entities = dismiss_entities ,
300
307
** entities ,
308
+ ** ({"out_path_base" : out_path_base } if interface == bintfs .DerivativesDataSink else {}),
301
309
)
310
+ if interface == bintfs .DerivativesDataSink :
311
+ prep .inputs .base_directory = str (base_directory )
312
+ else :
313
+ save = bintfs .SaveDerivative (base_directory = str (base_directory ))
302
314
303
315
if isinstance (expectation , type ):
304
316
with pytest .raises (expectation ):
305
- dds .run ()
317
+ prep .run ()
306
318
return
307
319
308
- output = dds .run ().outputs .out_file
320
+ prep_outputs = save_outputs = prep .run ().outputs
321
+
322
+ if save is not prep :
323
+ save .inputs .in_file = prep_outputs .out_file
324
+ save .inputs .relative_path = prep_outputs .out_path
325
+ save .inputs .metadata = prep_outputs .out_meta
326
+ save_outputs = save .run ().outputs
327
+
328
+ output = save_outputs .out_file
309
329
if isinstance (expectation , str ):
310
330
expectation = [expectation ]
311
331
output = [output ]
332
+ checksum = [checksum ]
312
333
313
334
if dismiss_entities :
314
335
if "run" in dismiss_entities :
@@ -320,26 +341,12 @@ def test_DerivativesDataSink_build_path(
320
341
for e in expectation
321
342
]
322
343
323
- base = out_path_base or "niworkflows"
344
+ base = ( out_path_base or "niworkflows" ) if interface == bintfs . DerivativesDataSink else " "
324
345
for out , exp in zip (output , expectation ):
325
- assert Path (out ).relative_to (tmp_path ) == Path (base ) / exp
326
-
327
- os .chdir (str (tmp_path )) # Exercise without setting base_directory
328
- dds = bintfs .DerivativesDataSink (
329
- in_file = ds_inputs ,
330
- dismiss_entities = dismiss_entities ,
331
- source_file = source ,
332
- out_path_base = out_path_base ,
333
- ** entities ,
334
- )
335
-
336
- output = dds .run ().outputs .out_file
337
- if isinstance (output , str ):
338
- output = [output ]
339
- checksum = [checksum ]
346
+ assert Path (out ).relative_to (base_directory ) == Path (base ) / exp
340
347
341
348
for out , exp in zip (output , expectation ):
342
- assert Path (out ).relative_to (tmp_path ) == Path (base ) / exp
349
+ assert Path (out ).relative_to (base_directory ) == Path (base ) / exp
343
350
# Regression - some images were given nan scale factors
344
351
if out .endswith (".nii" ) or out .endswith (".nii.gz" ):
345
352
img = nb .load (out )
0 commit comments