Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Use a more reliable test of NIfTIness #784

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions niworkflows/data/nipreps.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}][_desc-{desc}]_{suffix<T1w|T2w|T1rho|T1map|T2map|T2starmap|FLAIR|FLASH|PDmap|PD|PDT2|dseg|inplaneT[12]|angio|T2starw|MTw|TSE>}{extension<.nii|.nii.gz|.json>|.nii.gz}",
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}]_from-{from}_to-{to}_mode-{mode<image|points>|image}_{suffix<xfm>|xfm}{extension<.txt|.h5>}",
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}]_hemi-{hemi<L|R>}[_space-{space}][_cohort-{cohort}][_den-{density}]_{suffix<wm|smoothwm|pial|midthickness|inflated|vinflated|sphere|flat|sulc|curv|thickness>}{extension<.surf.gii|.shape.gii>}",
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_den-{density}]_{suffix<sulc|curv|thickness>}{extension<.dscalar.nii|.json>}",
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}]_desc-{desc}_{suffix<mask>|mask}{extension<.nii|.nii.gz|.json>|.nii.gz}",
"sub-{subject}[/ses-{session}]/{datatype<anat>|anat}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}]_label-{label}[_desc-{desc}]_{suffix<probseg>|probseg}{extension<.nii|.nii.gz|.json>|.nii.gz}",
"sub-{subject}[/ses-{session}]/{datatype<func>|func}/sub-{subject}[_ses-{session}]_task-{task}[_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_dir-{direction}][_run-{run}][_echo-{echo}][_part-{part}][_space-{space}][_cohort-{cohort}][_res-{resolution}][_desc-{desc}]_{suffix<bold|cbv|sbref|boldref|dseg>}{extension<.nii|.nii.gz|.json>|.nii.gz}",
Expand Down
8 changes: 5 additions & 3 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#
"""Interfaces for handling BIDS-like neuroimaging structures."""
from collections import defaultdict
from contextlib import suppress
from json import dumps, loads
from pathlib import Path
import shutil
Expand Down Expand Up @@ -657,9 +658,10 @@ def _run_interface(self, runtime):
# still None when it's time to write, just copy.
new_data, new_header = None, None

is_nifti = out_file.name.endswith(
(".nii", ".nii.gz")
) and not out_file.name.endswith((".dtseries.nii", ".dtseries.nii.gz"))
is_nifti = False
with suppress(nb.filebasedimages.ImageFileError):
is_nifti = isinstance(nb.load(orig_file), nb.Nifti1Image)

data_dtype = self.inputs.data_dtype or self._default_dtypes[self.inputs.suffix]
if is_nifti and any((self.inputs.check_hdr, data_dtype)):
nii = nb.load(orig_file)
Expand Down
28 changes: 21 additions & 7 deletions niworkflows/interfaces/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# https://www.nipreps.org/community/licensing/
#
"""Tests on BIDS compliance."""
import sys
import os
from pathlib import Path
import json
Expand Down Expand Up @@ -181,7 +182,7 @@
{"space": "fsLR", "density": "91k"},
"sub-100185/func/sub-100185_task-machinegame_run-01_"
"space-fsLR_den-91k_bold.dtseries.nii",
"53d9b486d08fec5a952f68fcbcddb38a72818d4c",
"f7b8755c6ad0d8dcdb60676331b52a23ce288b61",
),
(
BOLD_PATH,
Expand Down Expand Up @@ -269,7 +270,15 @@ def test_DerivativesDataSink_build_path(
ds_inputs = []
for input_file in input_files:
fname = tmp_path / input_file
if fname.name.rstrip(".gz").endswith(".nii"):
if fname.name.endswith(".dtseries.nii"):
axes = (nb.cifti2.SeriesAxis(start=0, step=2, size=20),
nb.cifti2.BrainModelAxis.from_mask(np.ones((5, 5, 5))))
hdr = nb.cifti2.cifti2_axes.to_header(axes)
cifti = nb.Cifti2Image(np.zeros(hdr.matrix.get_data_shape(), dtype=np.float32),
header=hdr)
cifti.nifti_header.set_intent("ConnDenseSeries")
cifti.to_filename(fname)
elif fname.name.rstrip(".gz").endswith(".nii"):
hdr = nb.Nifti1Header()
hdr.set_qform(np.eye(4), code=2)
hdr.set_sform(np.eye(4), code=2)
Expand All @@ -278,7 +287,7 @@ def test_DerivativesDataSink_build_path(
hdr.set_xyzt_units(*units)
nb.Nifti1Image(np.zeros(size), np.eye(4), hdr).to_filename(fname)
else:
(tmp_path / input_file).write_text("")
fname.write_text("")

ds_inputs.append(str(fname))

Expand Down Expand Up @@ -334,11 +343,16 @@ def test_DerivativesDataSink_build_path(
# Regression - some images were given nan scale factors
if out.endswith(".nii") or out.endswith(".nii.gz"):
img = nb.load(out)
with nb.openers.ImageOpener(out) as fobj:
hdr = img.header.from_fileobj(fobj)
assert not np.isnan(hdr["scl_slope"])
assert not np.isnan(hdr["scl_inter"])
if isinstance(img, nb.Nifti1Image):
with nb.openers.ImageOpener(out) as fobj:
hdr = img.header.from_fileobj(fobj)
assert not np.isnan(hdr["scl_slope"])
assert not np.isnan(hdr["scl_inter"])
for out, chksum in zip(output, checksum):
if chksum == "f7b8755c6ad0d8dcdb60676331b52a23ce288b61" and sys.version_info < (3, 8):
# Python 3.8 began preserving insertion order of attributes in XML
# Therefore we get a different checksum before/after
chksum = "a37ffb1188dd9a7b708de5b8daef46dac56ef8d4"
assert sha1(Path(out).read_bytes()).hexdigest() == chksum


Expand Down