Skip to content

Commit e787f88

Browse files
committed
ENH: Add new boldref module
1 parent d5f0b45 commit e787f88

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

nibabies/workflows/bold/boldref.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import nipype.interfaces.utility as niu
2+
import nipype.pipeline.engine as pe
3+
4+
5+
def init_infant_epi_reference_wf(
6+
omp_nthreads: int,
7+
is_sbref: bool = False,
8+
start_frame: int = 17,
9+
name: str = 'infant_epi_reference_wf',
10+
) -> pe.Workflow:
11+
"""
12+
Workflow to generate a reference map from one or more infant EPI images.
13+
14+
If any single-band references are provided, the reference map will be calculated from those.
15+
16+
If no single-band references are provided, the BOLD files are used.
17+
To account for potential increased motion on the start of image acquisition, this
18+
workflow discards a bigger chunk of the initial frames.
19+
20+
Parameters
21+
----------
22+
omp_nthreads
23+
Maximum number of threads an individual process may use
24+
has_sbref
25+
A single-band reference is provided.
26+
start_frame
27+
BOLD frame to start creating the reference map from. Any earlier frames are discarded.
28+
29+
Inputs
30+
------
31+
bold_file
32+
BOLD EPI file
33+
sbref_file
34+
single-band reference EPI
35+
36+
Outputs
37+
-------
38+
boldref_file
39+
The generated reference map
40+
boldref_mask
41+
Binary brain mask of the ``boldref_file``
42+
boldref_xfm
43+
Rigid-body transforms in LTA format
44+
45+
"""
46+
from niworkflows.workflows.epi.refmap import init_epi_reference_wf
47+
from sdcflows.interfaces.brainmask import BrainExtraction
48+
49+
wf = pe.Workflow(name=name)
50+
51+
inputnode = pe.Node(
52+
niu.IdentityInterface(fields=['epi_file']),
53+
name='inputnode',
54+
)
55+
outputnode = pe.Node(
56+
niu.IdentityInterface(fields=['boldref_file', 'boldref_mask']),
57+
name='outputnode',
58+
)
59+
60+
epi_reference_wf = init_epi_reference_wf(omp_nthreads)
61+
62+
boldref_mask = pe.Node(BrainExtraction(), name='boldref_mask')
63+
64+
# fmt:off
65+
wf.connect([
66+
(inputnode, epi_reference_wf, [('epi_file', 'in_files')]),
67+
(epi_reference_wf, boldref_mask, [('epi_ref_file', 'in_file')]),
68+
(epi_reference_wf, outputnode, [('epi_ref_file', 'boldref_file')]),
69+
(boldref_mask, outputnode, [('out_mask', 'boldref_mask')]),
70+
])
71+
# fmt:on
72+
if not is_sbref:
73+
select_frames = pe.Node(
74+
niu.Function(function=_select_frames, output_names=['t_mask']),
75+
name='select_frames',
76+
)
77+
select_frames.inputs.start_frame = start_frame
78+
# fmt:off
79+
wf.connect([
80+
(inputnode, select_frames, [('epi_file', 'in_file')]),
81+
(select_frames, epi_reference_wf, [('t_mask', 't_mask')]),
82+
])
83+
# fmt:on
84+
return wf
85+
86+
87+
def _select_frames(in_file: str, start_frame: int) -> list:
88+
import nibabel as nb
89+
import numpy as np
90+
91+
img = nb.load(in_file)
92+
img_len = img.shape[3]
93+
if start_frame >= img_len:
94+
start_frame = img_len - 1
95+
t_mask = np.array([False] * img_len, dtype=bool)
96+
t_mask[start_frame:] = True
97+
return list(t_mask)

0 commit comments

Comments
 (0)