Skip to content

Commit 4d35109

Browse files
committed
RF: Listify and sort BOLD series by EchoTime once [no fasttrack]
1 parent d2bb045 commit 4d35109

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

fmriprep/workflows/base.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ def init_single_subject_wf(subject_id: str):
216216
)
217217
)
218218

219+
bold_runs = [
220+
sorted(
221+
listify(run),
222+
key=lambda file: config.execution.layout.get_metadata(file).get('EchoTime', 0),
223+
)
224+
for run in subject_data['bold']
225+
]
226+
219227
if subject_data['roi']:
220228
warnings.warn(
221229
f"Lesion mask {subject_data['roi']} found. "
@@ -393,7 +401,7 @@ def init_single_subject_wf(subject_id: str):
393401
fmap_estimators, estimator_map = map_fieldmap_estimation(
394402
layout=config.execution.layout,
395403
subject_id=subject_id,
396-
bold_data=subject_data['bold'],
404+
bold_data=bold_runs,
397405
ignore_fieldmaps="fieldmaps" in config.workflow.ignore,
398406
use_syn=config.workflow.use_syn_sdc,
399407
force_syn=config.workflow.force_syn,
@@ -509,11 +517,10 @@ def init_single_subject_wf(subject_id: str):
509517
: For each of the {num_bold} BOLD runs found per subject (across all
510518
tasks and sessions), the following preprocessing was performed.
511519
""".format(
512-
num_bold=len(subject_data['bold'])
520+
num_bold=len(bold_runs)
513521
)
514522

515-
for bold_series in subject_data['bold']:
516-
bold_series = sorted(listify(bold_series))
523+
for bold_series in bold_runs:
517524
bold_file = bold_series[0]
518525
fieldmap_id = estimator_map.get(bold_file)
519526

@@ -610,7 +617,7 @@ def init_single_subject_wf(subject_id: str):
610617
def map_fieldmap_estimation(
611618
layout: bids.BIDSLayout,
612619
subject_id: str,
613-
bold_data: list,
620+
bold_data: list[list[str]],
614621
ignore_fieldmaps: bool,
615622
use_syn: bool | str,
616623
force_syn: bool,
@@ -656,7 +663,7 @@ def map_fieldmap_estimation(
656663
# Pare down estimators to those that are actually used
657664
# If fmap_estimators == [], all loops/comprehensions terminate immediately
658665
all_ids = {fmap.bids_id for fmap in fmap_estimators}
659-
bold_files = (sorted(listify(bold_file))[0] for bold_file in bold_data)
666+
bold_files = (bold_series[0] for bold_series in bold_data)
660667

661668
all_estimators = {
662669
bold_file: [fmap_id for fmap_id in get_estimator(layout, bold_file) if fmap_id in all_ids]

fmriprep/workflows/bold/fit.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def init_bold_fit_wf(
118118
Parameters
119119
----------
120120
bold_series
121-
List of paths to NIfTI files.
121+
List of paths to NIfTI files, sorted by echo time.
122122
precomputed
123123
Dictionary containing precomputed derivatives to reuse, if possible.
124124
fieldmap_id
@@ -204,27 +204,26 @@ def init_bold_fit_wf(
204204

205205
layout = config.execution.layout
206206

207-
# Collect bold and sbref files, sorted by EchoTime
208-
bold_files = sorted(bold_series, key=lambda fname: layout.get_metadata(fname).get("EchoTime"))
207+
# Collect sbref files, sorted by EchoTime
209208
sbref_files = get_sbrefs(
210-
bold_files,
209+
bold_series,
211210
entity_overrides=config.execution.get().get('bids_filters', {}).get('sbref', {}),
212211
layout=layout,
213212
)
214213

215214
# Fitting operates on the shortest echo
216215
# This could become more complicated in the future
217-
bold_file = bold_files[0]
216+
bold_file = bold_series[0]
218217

219218
# Get metadata from BOLD file(s)
220-
entities = extract_entities(bold_files)
219+
entities = extract_entities(bold_series)
221220
metadata = layout.get_metadata(bold_file)
222221
orientation = "".join(nb.aff2axcodes(nb.load(bold_file).affine))
223222

224223
bold_tlen, mem_gb = estimate_bold_mem_usage(bold_file)
225224

226225
# Boolean used to update workflow self-descriptions
227-
multiecho = len(bold_files) > 1
226+
multiecho = len(bold_series) > 1
228227

229228
have_hmcref = "hmc_boldref" in precomputed
230229
have_coregref = "coreg_boldref" in precomputed
@@ -699,27 +698,20 @@ def init_bold_native_wf(
699698
layout = config.execution.layout
700699

701700
# Shortest echo first
702-
all_metadata, bold_files, echo_times = zip(
703-
*sorted(
704-
(
705-
(md := layout.get_metadata(bold_file), bold_file, md.get("EchoTime"))
706-
for bold_file in listify(bold_series)
707-
),
708-
key=lambda x: x[2],
709-
)
710-
)
711-
multiecho = len(bold_files) > 1
701+
all_metadata = [layout.get_metadata(bold_file) for bold_file in bold_series]
702+
echo_times = [md.get("EchoTime") for md in all_metadata]
703+
multiecho = len(bold_series) > 1
712704

713-
bold_file = bold_files[0]
705+
bold_file = bold_series[0]
714706
metadata = all_metadata[0]
715707

716708
bold_tlen, mem_gb = estimate_bold_mem_usage(bold_file)
717709

718710
if multiecho:
719-
shapes = [nb.load(echo).shape for echo in bold_files]
711+
shapes = [nb.load(echo).shape for echo in bold_series]
720712
if len(set(shapes)) != 1:
721713
diagnostic = "\n".join(
722-
f"{os.path.basename(echo)}: {shape}" for echo, shape in zip(bold_files, shapes)
714+
f"{os.path.basename(echo)}: {shape}" for echo, shape in zip(bold_series, shapes)
723715
)
724716
raise RuntimeError(f"Multi-echo images found with mismatching shapes\n{diagnostic}")
725717
if len(shapes) == 2:
@@ -774,12 +766,12 @@ def init_bold_native_wf(
774766
# almost identically
775767
echo_index = pe.Node(niu.IdentityInterface(fields=["echoidx"]), name="echo_index")
776768
if multiecho:
777-
echo_index.iterables = [("echoidx", range(len(bold_files)))]
769+
echo_index.iterables = [("echoidx", range(len(bold_series)))]
778770
else:
779771
echo_index.inputs.echoidx = 0
780772

781773
# BOLD source: track original BOLD file(s)
782-
bold_source = pe.Node(niu.Select(inlist=bold_files), name="bold_source")
774+
bold_source = pe.Node(niu.Select(inlist=bold_series), name="bold_source")
783775
validate_bold = pe.Node(ValidateImage(), name="validate_bold")
784776
workflow.connect([
785777
(echo_index, bold_source, [("echoidx", "index")]),

0 commit comments

Comments
 (0)