Skip to content

Commit 23e1bbc

Browse files
committed
TEST: Test singular and decomposed interfaces
1 parent 91ea4b9 commit 23e1bbc

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

niworkflows/interfaces/tests/test_bids.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
BOLD_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",
@@ -258,6 +259,7 @@
258259
@pytest.mark.parametrize("dismiss_entities", [None, ("run", "session")])
259260
def 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)

niworkflows/utils/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ def _copy_any(src, dst):
282282
from shutil import copyfileobj
283283
from nipype.utils.filemanip import copyfile
284284

285-
src_isgz = src.endswith(".gz")
286-
dst_isgz = dst.endswith(".gz")
285+
src_isgz = os.fspath(src).endswith(".gz")
286+
dst_isgz = os.fspath(dst).endswith(".gz")
287287
if not src_isgz and not dst_isgz:
288288
copyfile(src, dst, copy=True, use_hardlink=True)
289289
return False # Make sure we do not reuse the hardlink later

0 commit comments

Comments
 (0)