This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_docma.py
172 lines (146 loc) · 5.81 KB
/
test_docma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
"""
import os
from copy import deepcopy
import numpy as np
import nibabel as nib
from bids.layout import BIDSLayout
import nipype.pipeline.engine as pe # pypeline engine
from nipype.interfaces import utility as niu
from nipype.interfaces.utility import Function
import nipype.interfaces.io as nio
from niflow.nipype1.workflows.dmri.fsl.utils import siemens2rads, rads2radsec
from sdcflows.workflows.docma import init_docma_wf
from utils import collect_data
def pick_first_two(func):
"""
Use to grab first two echoes for multi-echo data
"""
return func[:2]
def pick_first(func):
"""
Used to grab first echo for multi-echo data
"""
if isinstance(func, list):
return func[0]
else:
return func
def pick_second(func):
"""
Used to grab second echo for multi-echo data
"""
if isinstance(func, list):
return func[1]
else:
return func
def copy_files(*, output_dir, **kwargs):
"""
Requires 3.6+
"""
from os import mkdir
import os.path as op
from shutil import copyfile
out_files = []
for in_file in kwargs.values():
fn = op.basename(in_file)
out_file = op.join(output_dir, fn)
if not op.isdir(output_dir):
mkdir(output_dir)
copyfile(in_file, out_file)
out_files.append(out_file)
return out_files
def init_preproc_workflow(bids_dir, output_dir, work_dir, subject_list,
session_label, task_label, run_label):
"""
A workflow for preprocessing complex-valued multi-echo fMRI data with
single-band reference images and available T1s.
"""
# setup workflow
participant_wf = pe.Workflow(name='participant_wf')
participant_wf.base_dir = os.path.join(work_dir, 'complex_preprocessing')
os.makedirs(participant_wf.base_dir, exist_ok=True)
# Read in dataset, but don't validate because phase isn't supported yet
layout = BIDSLayout(bids_dir, validate=False)
for subject_label in subject_list:
# collect the necessary inputs
subject_data = collect_data(
layout, subject_label, task=task_label, run=run_label, ses=session_label
)
single_subject_wf = init_single_subject_wf(
name='single_subject_' + subject_label + '_wf',
output_dir=output_dir,
layout=layout,
bold_files=subject_data['bold_mag_files'],
bold_metadata=subject_data['bold_mag_metadata'],
phase_files=subject_data['bold_phase_files'],
phase_metadata=subject_data['bold_phase_metadata'],
)
single_subject_wf.config['execution']['crashdump_dir'] = os.path.join(
output_dir, 'sub-' + subject_label, 'log'
)
for node in single_subject_wf._get_all_nodes():
node.config = deepcopy(single_subject_wf.config)
participant_wf.add_nodes([single_subject_wf])
return participant_wf
def init_single_subject_wf(name, output_dir, layout, bold_files, bold_metadata,
phase_files, phase_metadata):
"""
Single-subject workflow
"""
workflow = pe.Workflow(name=name)
# name the nodes
inputnode = pe.Node(
niu.IdentityInterface(
fields=[
'bold_files',
'bold_metadata',
'phase_files',
'phase_metadata',
]
),
name='inputnode',
iterables=[
('bold_files', bold_files),
('bold_metadata', bold_metadata),
('phase_files', phase_files),
('phase_metadata', phase_metadata),
],
synchronize=True)
outputnode = pe.Node(
niu.IdentityInterface(
fields=['fmap', 'fmap_ref', 'fmap_mask', 'reference', 'reference_brain', 'warp', 'mask']
),
name='outputnode')
# Generate single-band reference image-based field maps
docma_wf = init_docma_wf(name='docma_wf', num_trs=20, omp_nthreads=1)
# Output BOLD mag files
derivativesnode = pe.Node(
interface=Function(
['fmap', 'fmap_ref', 'fmap_mask', 'reference', 'reference_brain', 'warp', 'mask', 'output_dir'],
['fmap', 'fmap_ref', 'fmap_mask', 'reference', 'reference_brain', 'warp', 'mask'],
copy_files),
name='derivativesnode')
derivativesnode.inputs.output_dir = output_dir
workflow.connect([
(inputnode, docma_wf, [(('bold_files', pick_first), 'inputnode.magnitude1'),
(('bold_files', pick_second), 'inputnode.magnitude2'),
(('phase_files', pick_first), 'inputnode.phase1'),
(('phase_files', pick_second), 'inputnode.phase2'),
(('phase_metadata', pick_first), 'inputnode.phase1_metadata'),
(('phase_metadata', pick_second), 'inputnode.phase2_metadata')]),
(docma_wf, outputnode, [('outputnode.fmap', 'fmap'),
('outputnode.fmap_mask', 'fmap_mask'),
('outputnode.fmap_ref', 'fmap_ref'),
('outputnode.reference', 'reference'),
('outputnode.reference_brain', 'reference_brain'),
('outputnode.warp', 'warp'),
('outputnode.mask', 'mask')]),
(outputnode, derivativesnode, [('fmap', 'fmap'),
('fmap_mask', 'fmap_mask'),
('fmap_ref', 'fmap_ref'),
('reference', 'reference'),
('reference_brain', 'reference_brain'),
('warp', 'warp'),
('mask', 'mask')]),
])
return workflow