Skip to content

Commit 5d08d91

Browse files
authored
Merge pull request #647 from HippocampusGirl/derivativesdatasink-list-source-file
FIX: DerivativesDataSink warning when it has multiple source files [backport #573]
2 parents a4620fd + 0b084db commit 5d08d91

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

niworkflows/interfaces/bids.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ def _run_interface(self, runtime):
616616

617617
if data_dtype == "source": # match source dtype
618618
try:
619-
data_dtype = nb.load(self.inputs.source_file).get_data_dtype()
619+
data_dtype = nb.load(self.inputs.source_file[0]).get_data_dtype()
620620
except Exception:
621621
LOGGER.warning(
622-
f"Could not get data type of file {self.inputs.source_file}"
622+
f"Could not get data type of file {self.inputs.source_file[0]}"
623623
)
624624
data_dtype = None
625625

niworkflows/interfaces/tests/test_bids.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,51 @@ def test_DerivativesDataSink_values(tmp_path, dtype):
502502
assert sha1(out_file.read_bytes()).hexdigest() == checksum
503503

504504

505+
@pytest.mark.parametrize(
506+
"source_file",
507+
[
508+
BOLD_PATH,
509+
[BOLD_PATH],
510+
[BOLD_PATH, "ds054/sub-100185/func/sub-100185_task-machinegame_run-02_bold.nii.gz"]
511+
]
512+
)
513+
@pytest.mark.parametrize("source_dtype", ["<i4", "<f4"])
514+
@pytest.mark.parametrize("in_dtype", ["<i4", "<f4"])
515+
def test_DerivativesDataSink_data_dtype_source(
516+
tmp_path, source_file, source_dtype, in_dtype
517+
):
518+
519+
def make_empty_nii_with_dtype(fname, dtype):
520+
Path(fname).parent.mkdir(exist_ok=True, parents=True)
521+
522+
size = (2, 3, 4, 5)
523+
524+
nb.Nifti1Image(np.zeros(size, dtype=dtype), np.eye(4)).to_filename(fname)
525+
526+
in_file = str(tmp_path / "in.nii")
527+
make_empty_nii_with_dtype(in_file, in_dtype)
528+
529+
if isinstance(source_file, str):
530+
source_file = str(tmp_path / source_file)
531+
make_empty_nii_with_dtype(source_file, source_dtype)
532+
533+
elif isinstance(source_file, list):
534+
source_file = [str(tmp_path / s) for s in source_file]
535+
for s in source_file:
536+
make_empty_nii_with_dtype(s, source_dtype)
537+
538+
dds = bintfs.DerivativesDataSink(
539+
base_directory=str(tmp_path),
540+
data_dtype="source",
541+
desc="preproc",
542+
source_file=source_file,
543+
in_file=in_file,
544+
).run()
545+
546+
nii = nb.load(dds.outputs.out_file)
547+
assert nii.get_data_dtype() == np.dtype(source_dtype)
548+
549+
505550
@pytest.mark.parametrize("field", ["RepetitionTime", "UndefinedField"])
506551
def test_ReadSidecarJSON_connection(testdata_dir, field):
507552
"""

0 commit comments

Comments
 (0)