Skip to content

Commit

Permalink
TEST: Test singular and decomposed interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 29, 2024
1 parent 91ea4b9 commit 23e1bbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
51 changes: 29 additions & 22 deletions niworkflows/interfaces/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
BOLD_PATH = "ds054/sub-100185/func/sub-100185_task-machinegame_run-01_bold.nii.gz"


@pytest.mark.parametrize("interface", [bintfs.DerivativesDataSink, bintfs.PrepareDerivative])
@pytest.mark.parametrize("out_path_base", [None, "fmriprep"])
@pytest.mark.parametrize(
"source,input_files,entities,expectation,checksum",
Expand Down Expand Up @@ -258,6 +259,7 @@
@pytest.mark.parametrize("dismiss_entities", [None, ("run", "session")])
def test_DerivativesDataSink_build_path(
tmp_path,
interface,
out_path_base,
source,
input_files,
Expand All @@ -267,6 +269,8 @@ def test_DerivativesDataSink_build_path(
dismiss_entities,
):
"""Check a few common derivatives generated by NiPreps."""
if interface is bintfs.PrepareDerivative and out_path_base is not None:
pytest.skip("PrepareDerivative does not support out_path_base")
ds_inputs = []
for input_file in input_files:
fname = tmp_path / input_file
Expand All @@ -291,24 +295,41 @@ def test_DerivativesDataSink_build_path(

ds_inputs.append(str(fname))

dds = bintfs.DerivativesDataSink(
base_directory = tmp_path / "output"
work_dir = tmp_path / "work"
base_directory.mkdir()
work_dir.mkdir()

prep = save = interface(
in_file=ds_inputs,
base_directory=str(tmp_path),
source_file=source,
out_path_base=out_path_base,
dismiss_entities=dismiss_entities,
**entities,
**({"out_path_base": out_path_base} if interface == bintfs.DerivativesDataSink else {}),
)
if interface == bintfs.DerivativesDataSink:
prep.inputs.base_directory = str(base_directory)
else:
save = bintfs.SaveDerivative(base_directory=str(base_directory))

if isinstance(expectation, type):
with pytest.raises(expectation):
dds.run()
prep.run()
return

output = dds.run().outputs.out_file
prep_outputs = save_outputs = prep.run().outputs

if save is not prep:
save.inputs.in_file = prep_outputs.out_file
save.inputs.relative_path = prep_outputs.out_path
save.inputs.metadata = prep_outputs.out_meta
save_outputs = save.run().outputs

output = save_outputs.out_file
if isinstance(expectation, str):
expectation = [expectation]
output = [output]
checksum = [checksum]

if dismiss_entities:
if "run" in dismiss_entities:
Expand All @@ -320,26 +341,12 @@ def test_DerivativesDataSink_build_path(
for e in expectation
]

base = out_path_base or "niworkflows"
base = (out_path_base or "niworkflows") if interface == bintfs.DerivativesDataSink else ""
for out, exp in zip(output, expectation):
assert Path(out).relative_to(tmp_path) == Path(base) / exp

os.chdir(str(tmp_path)) # Exercise without setting base_directory
dds = bintfs.DerivativesDataSink(
in_file=ds_inputs,
dismiss_entities=dismiss_entities,
source_file=source,
out_path_base=out_path_base,
**entities,
)

output = dds.run().outputs.out_file
if isinstance(output, str):
output = [output]
checksum = [checksum]
assert Path(out).relative_to(base_directory) == Path(base) / exp

for out, exp in zip(output, expectation):
assert Path(out).relative_to(tmp_path) == Path(base) / exp
assert Path(out).relative_to(base_directory) == Path(base) / exp
# Regression - some images were given nan scale factors
if out.endswith(".nii") or out.endswith(".nii.gz"):
img = nb.load(out)
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ def _copy_any(src, dst):
from shutil import copyfileobj
from nipype.utils.filemanip import copyfile

src_isgz = src.endswith(".gz")
dst_isgz = dst.endswith(".gz")
src_isgz = os.fspath(src).endswith(".gz")
dst_isgz = os.fspath(dst).endswith(".gz")
if not src_isgz and not dst_isgz:
copyfile(src, dst, copy=True, use_hardlink=True)
return False # Make sure we do not reuse the hardlink later
Expand Down

0 comments on commit 23e1bbc

Please sign in to comment.