1- """ Testing module for fmriprep.workflows.bold.util """
1+ """Testing module for fmriprep.workflows.bold.util. """
22import pytest
33import os
44from pathlib import Path
55
66import numpy as np
77from nipype .pipeline import engine as pe
8- from nipype .utils .filemanip import fname_presuffix , copyfile
8+ from nipype .utils .filemanip import fname_presuffix , copyfile , ensure_list
99from nilearn .image import load_img
1010
1111from niworkflows .interfaces .masks import ROIsPlot
1212
1313from ..util import init_bold_reference_wf
1414
15+ # Multi-echo datasets
16+ bold_datasets = ["""\
17+ ds000210/sub-06_task-rest_run-01_echo-1_bold.nii.gz
18+ ds000210/sub-06_task-rest_run-01_echo-2_bold.nii.gz
19+ ds000210/sub-06_task-rest_run-01_echo-3_bold.nii.gz\
20+ """ .splitlines (), """\
21+ ds000216/sub-03_task-rest_echo-1_bold.nii.gz
22+ ds000216/sub-03_task-rest_echo-2_bold.nii.gz
23+ ds000216/sub-03_task-rest_echo-3_bold.nii.gz
24+ ds000216/sub-03_task-rest_echo-4_bold.nii.gz""" .splitlines ()]
25+
26+ # Single-echo datasets
27+ bold_datasets += """\
28+ ds000116/sub-12_task-visualoddballwithbuttonresponsetotargetstimuli_run-02_bold.nii.gz
29+ ds000133/sub-06_ses-post_task-rest_run-01_bold.nii.gz
30+ ds000140/sub-32_task-heatpainwithregulationandratings_run-02_bold.nii.gz
31+ ds000157/sub-23_task-passiveimageviewing_bold.nii.gz
32+ ds000237/sub-03_task-MemorySpan_acq-multiband_run-01_bold.nii.gz
33+ ds000237/sub-06_task-MemorySpan_acq-multiband_run-01_bold.nii.gz
34+ ds001240/sub-26_task-localizerimagination_bold.nii.gz
35+ ds001240/sub-26_task-localizerviewing_bold.nii.gz
36+ ds001240/sub-26_task-molencoding_run-01_bold.nii.gz
37+ ds001240/sub-26_task-molencoding_run-02_bold.nii.gz
38+ ds001240/sub-26_task-molretrieval_run-01_bold.nii.gz
39+ ds001240/sub-26_task-molretrieval_run-02_bold.nii.gz
40+ ds001240/sub-26_task-rest_bold.nii.gz
41+ ds001362/sub-01_task-taskname_run-01_bold.nii.gz""" .splitlines ()
42+
43+ bold_datasets = [ensure_list (d ) for d in bold_datasets ]
44+
1545
1646def symmetric_overlap (img1 , img2 ):
1747 mask1 = load_img (img1 ).get_fdata () > 0
@@ -32,60 +62,42 @@ def symmetric_overlap(img1, img2):
3262 "input_fname,expected_fname" ,
3363 [
3464 (
35- os .path .join (os .getenv ("FMRIPREP_REGRESSION_SOURCE" , "" ), base_fname ),
65+ [os .path .join (os .getenv ("FMRIPREP_REGRESSION_SOURCE" , "" ), bf )
66+ for bf in base_fname ],
3667 fname_presuffix (
37- base_fname ,
68+ base_fname [ 0 ]. replace ( "_echo-1" , "" ) ,
3869 suffix = "_mask" ,
3970 use_ext = True ,
4071 newpath = os .path .join (
4172 os .getenv ("FMRIPREP_REGRESSION_TARGETS" , "" ),
42- os .path .dirname (base_fname ),
73+ os .path .dirname (base_fname [ 0 ] ),
4374 ),
4475 ),
4576 )
46- for base_fname in """\
47- ds000116/sub-12_task-visualoddballwithbuttonresponsetotargetstimuli_run-02_bold.nii.gz
48- ds000133/sub-06_ses-post_task-rest_run-01_bold.nii.gz
49- ds000140/sub-32_task-heatpainwithregulationandratings_run-02_bold.nii.gz
50- ds000157/sub-23_task-passiveimageviewing_bold.nii.gz
51- ds000210/sub-06_task-rest_run-01_echo-1_bold.nii.gz
52- ds000210/sub-06_task-rest_run-01_echo-2_bold.nii.gz
53- ds000210/sub-06_task-rest_run-01_echo-3_bold.nii.gz
54- ds000216/sub-03_task-rest_echo-1_bold.nii.gz
55- ds000216/sub-03_task-rest_echo-2_bold.nii.gz
56- ds000216/sub-03_task-rest_echo-3_bold.nii.gz
57- ds000216/sub-03_task-rest_echo-4_bold.nii.gz
58- ds000237/sub-03_task-MemorySpan_acq-multiband_run-01_bold.nii.gz
59- ds000237/sub-06_task-MemorySpan_acq-multiband_run-01_bold.nii.gz
60- ds001240/sub-26_task-localizerimagination_bold.nii.gz
61- ds001240/sub-26_task-localizerviewing_bold.nii.gz
62- ds001240/sub-26_task-molencoding_run-01_bold.nii.gz
63- ds001240/sub-26_task-molencoding_run-02_bold.nii.gz
64- ds001240/sub-26_task-molretrieval_run-01_bold.nii.gz
65- ds001240/sub-26_task-molretrieval_run-02_bold.nii.gz
66- ds001240/sub-26_task-rest_bold.nii.gz
67- ds001362/sub-01_task-taskname_run-01_bold.nii.gz""" .splitlines ()
77+ for base_fname in bold_datasets
6878 ],
6979)
7080def test_masking (input_fname , expected_fname ):
71- basename = Path (input_fname ).name
81+ basename = Path (input_fname [ 0 ] ).name
7282 dsname = Path (expected_fname ).parent .name
7383
7484 # Reconstruct base_fname from above
7585 reports_dir = Path (os .getenv ("FMRIPREP_REGRESSION_REPORTS" , "" ))
7686 newpath = reports_dir / dsname
7787
7888 name = basename .rstrip ("_bold.nii.gz" ).replace ("-" , "_" )
79- bold_reference_wf = init_bold_reference_wf (omp_nthreads = 1 , name = name )
80- bold_reference_wf .inputs .inputnode .bold_file = input_fname
89+ bold_reference_wf = init_bold_reference_wf (omp_nthreads = 1 , name = name ,
90+ multiecho = len (input_fname ) > 1 )
91+ bold_reference_wf .inputs .inputnode .bold_file = input_fname [0 ] if len (input_fname ) == 1 \
92+ else input_fname
8193 base_dir = os .getenv ("CACHED_WORK_DIRECTORY" )
8294 if base_dir :
8395 base_dir = Path (base_dir ) / dsname
8496 base_dir .mkdir (parents = True , exist_ok = True )
8597 bold_reference_wf .base_dir = str (base_dir )
8698
8799 out_fname = fname_presuffix (
88- basename , suffix = "_mask .svg" , use_ext = False , newpath = str (newpath )
100+ Path ( expected_fname ). name , suffix = ".svg" , use_ext = False , newpath = str (newpath )
89101 )
90102 newpath .mkdir (parents = True , exist_ok = True )
91103
@@ -117,7 +129,7 @@ def test_masking(input_fname, expected_fname):
117129 mask_dir .mkdir (parents = True , exist_ok = True )
118130 copyfile (
119131 combine_masks .result .outputs .out_file ,
120- fname_presuffix ( basename , suffix = "_mask" , use_ext = True , newpath = str ( mask_dir ) ),
132+ str ( mask_dir / Path ( expected_fname ). name ),
121133 copy = True ,
122134 )
123135
0 commit comments