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

Improve error messages in ReduceASLFiles #379

Merged
merged 6 commits into from
Dec 20, 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
13 changes: 11 additions & 2 deletions aslprep/interfaces/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class ReduceASLFiles(SimpleInterface):
def _run_interface(self, runtime):
aslcontext = pd.read_table(self.inputs.aslcontext)
asl_img = nb.load(self.inputs.asl_file)
assert asl_img.shape[3] == aslcontext.shape[0]
if asl_img.shape[3] != aslcontext.shape[0]:
raise ValueError(
f"Number of volumes in {self.inputs.asl_file} ({asl_img.shape[3]}) doesn't equal "
f"number of rows in {self.inputs.aslcontext} ({aslcontext.shape[0]})."
)

if self.inputs.processing_target == "control":
files_to_keep = ["control", "label", "m0scan"]
Expand All @@ -49,9 +53,14 @@ def _run_interface(self, runtime):
else:
files_to_keep = ["cbf", "m0scan"]

n_volumes = aslcontext.shape[0]
asl_idx = aslcontext.loc[aslcontext["volume_type"].isin(files_to_keep)].index.values
asl_idx = asl_idx.astype(int)
self._results["metadata"] = reduce_metadata_lists(self.inputs.metadata, asl_idx)
self._results["metadata"] = reduce_metadata_lists(
metadata=self.inputs.metadata,
n_volumes=n_volumes,
keep_idx=asl_idx,
)

asl_img = image.index_img(asl_img, asl_idx)

Expand Down
1 change: 0 additions & 1 deletion aslprep/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ def test_test_001(data_dir, output_dir, working_dir):
"T1w",
"MNI152NLin2009cAsym",
"--scorescrub",
"--use-syn-sdc",
"--force-no-ge",
"--fs-no-reconall",
f"--fs-subjects-dir={os.path.join(data_dir, 'anatomical/freesurfer')}",
Expand Down
9 changes: 7 additions & 2 deletions aslprep/utils/asl.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_bolus_duration(metadata: "dict[str, Any]", is_casl: bool) -> float:
return metadata["BolusCutOffDelayTime"]


def reduce_metadata_lists(metadata, metadata_idx):
def reduce_metadata_lists(metadata, n_volumes, keep_idx):
"""Reduce any volume-wise metadata fields to only contain values for selected volumes."""
# A hardcoded list of fields that may have one value for each volume.
VOLUME_WISE_FIELDS = [
Expand All @@ -182,7 +182,12 @@ def reduce_metadata_lists(metadata, metadata_idx):

value = metadata[field]
if isinstance(value, list):
if len(value) != n_volumes:
raise ValueError(
f"Number of elements in list-type metadata field {field} ({len(value)}) "
f"doesn't equal the number of volumes in the ASL file ({n_volumes})."
)
# Reduce to only the selected volumes
metadata[field] = [value[i] for i in metadata_idx]
metadata[field] = [value[i] for i in keep_idx]

return metadata
4 changes: 4 additions & 0 deletions aslprep/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from copy import deepcopy

import bids
import nibabel as nb
from nipype.interfaces import utility as niu
from nipype.pipeline import engine as pe
from niworkflows.utils.connections import listify
Expand Down Expand Up @@ -562,6 +563,9 @@ def init_single_subject_wf(subject_id: str):
)
syn_preprocessing_wf.inputs.inputnode.in_epis = sources
syn_preprocessing_wf.inputs.inputnode.in_meta = source_meta
# Use all volumes of each run.
run_lengths = [nb.load(f).shape[3] for f in subject_data["asl"]]
syn_preprocessing_wf.inputs.inputnode.t_masks = [[True] * rl for rl in run_lengths]

workflow.connect([
(anat_fit_wf, syn_preprocessing_wf, [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"psutil >= 5.4",
"pybids ~= 0.16.4",
"requests",
"sdcflows",
"sdcflows ~= 2.6.0",
"sentry-sdk >= 0.6.9",
"smriprep ~= 0.13.1",
"templateflow >= 23.0.0",
Expand Down