4747BOLD_PATH = "ds054/sub-100185/func/sub-100185_task-machinegame_run-01_bold.nii.gz"
4848
4949
50+ @pytest .mark .parametrize ("interface" , [bintfs .DerivativesDataSink , bintfs .PrepareDerivative ])
5051@pytest .mark .parametrize ("out_path_base" , [None , "fmriprep" ])
5152@pytest .mark .parametrize (
5253 "source,input_files,entities,expectation,checksum" ,
258259@pytest .mark .parametrize ("dismiss_entities" , [None , ("run" , "session" )])
259260def test_DerivativesDataSink_build_path (
260261 tmp_path ,
262+ interface ,
261263 out_path_base ,
262264 source ,
263265 input_files ,
@@ -267,6 +269,8 @@ def test_DerivativesDataSink_build_path(
267269 dismiss_entities ,
268270):
269271 """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" )
270274 ds_inputs = []
271275 for input_file in input_files :
272276 fname = tmp_path / input_file
@@ -291,24 +295,41 @@ def test_DerivativesDataSink_build_path(
291295
292296 ds_inputs .append (str (fname ))
293297
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 (
295304 in_file = ds_inputs ,
296- base_directory = str (tmp_path ),
297305 source_file = source ,
298- out_path_base = out_path_base ,
299306 dismiss_entities = dismiss_entities ,
300307 ** entities ,
308+ ** ({"out_path_base" : out_path_base } if interface == bintfs .DerivativesDataSink else {}),
301309 )
310+ if interface == bintfs .DerivativesDataSink :
311+ prep .inputs .base_directory = str (base_directory )
312+ else :
313+ save = bintfs .SaveDerivative (base_directory = str (base_directory ))
302314
303315 if isinstance (expectation , type ):
304316 with pytest .raises (expectation ):
305- dds .run ()
317+ prep .run ()
306318 return
307319
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
309329 if isinstance (expectation , str ):
310330 expectation = [expectation ]
311331 output = [output ]
332+ checksum = [checksum ]
312333
313334 if dismiss_entities :
314335 if "run" in dismiss_entities :
@@ -320,26 +341,12 @@ def test_DerivativesDataSink_build_path(
320341 for e in expectation
321342 ]
322343
323- base = out_path_base or "niworkflows"
344+ base = ( out_path_base or "niworkflows" ) if interface == bintfs . DerivativesDataSink else " "
324345 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
340347
341348 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
343350 # Regression - some images were given nan scale factors
344351 if out .endswith (".nii" ) or out .endswith (".nii.gz" ):
345352 img = nb .load (out )
0 commit comments