Skip to content

Commit 43b2406

Browse files
authored
Merge pull request #2190 from oliver-contier/mc_ref_mod
create_featreg_preproc: reference run modification
2 parents 6a8318f + 64fe757 commit 43b2406

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

nipype/workflows/fmri/fsl/preprocess.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ def getthreshop(thresh):
1818
return ['-thr %.10f -Tmin -bin' % (0.1 * val[1]) for val in thresh]
1919

2020

21+
def pickrun(files, whichrun):
22+
"""pick file from list of files"""
23+
24+
filemap = {'first': 0, 'last': -1, 'middle': len(files) // 2}
25+
26+
if isinstance(files, list):
27+
28+
# whichrun is given as integer
29+
if isinstance(whichrun, int):
30+
return files[whichrun]
31+
# whichrun is given as string
32+
elif isinstance(whichrun, str):
33+
if whichrun not in filemap.keys():
34+
raise(KeyError, 'Sorry, whichrun must be either integer index'
35+
'or string in form of "first", "last" or "middle')
36+
else:
37+
return files[filemap[whichrun]]
38+
else:
39+
# in case single file name is given
40+
return files
41+
42+
2143
def pickfirst(files):
2244
if isinstance(files, list):
2345
return files[0]
@@ -401,7 +423,7 @@ def create_parallelfeat_preproc(name='featpreproc', highpass=True):
401423
return featpreproc
402424

403425

404-
def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle'):
426+
def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle', whichrun=0):
405427
"""Create a FEAT preprocessing workflow with registration to one volume of the first run
406428
407429
Parameters
@@ -412,6 +434,7 @@ def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle')
412434
name : name of workflow (default: featpreproc)
413435
highpass : boolean (default: True)
414436
whichvol : which volume of the first run to register to ('first', 'middle', 'last', 'mean')
437+
whichrun : which run to draw reference volume from (integer index or 'first', 'middle', 'last')
415438
416439
Inputs::
417440
@@ -511,8 +534,8 @@ def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle')
511534
if whichvol != 'mean':
512535
extract_ref = pe.Node(interface=fsl.ExtractROI(t_size=1),
513536
iterfield=['in_file'],
514-
name='extractref')
515-
featpreproc.connect(img2float, ('out_file', pickfirst), extract_ref, 'in_file')
537+
name = 'extractref')
538+
featpreproc.connect(img2float, ('out_file', pickrun, whichrun), extract_ref, 'in_file')
516539
featpreproc.connect(img2float, ('out_file', pickvol, 0, whichvol), extract_ref, 't_min')
517540
featpreproc.connect(extract_ref, 'roi_file', outputnode, 'reference')
518541

@@ -530,7 +553,7 @@ def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle')
530553
featpreproc.connect(extract_ref, 'roi_file', motion_correct, 'ref_file')
531554
else:
532555
motion_correct.inputs.mean_vol = True
533-
featpreproc.connect(motion_correct, ('mean_img', pickfirst), outputnode, 'reference')
556+
featpreproc.connect(motion_correct, ('mean_img', pickrun, whichrun), outputnode, 'reference')
534557

535558
featpreproc.connect(motion_correct, 'par_file', outputnode, 'motion_parameters')
536559
featpreproc.connect(motion_correct, 'out_file', outputnode, 'realigned_files')
@@ -550,10 +573,9 @@ def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle')
550573
Extract the mean volume of the first functional run
551574
"""
552575

553-
meanfunc = pe.Node(interface=fsl.ImageMaths(op_string='-Tmean',
554-
suffix='_mean'),
576+
meanfunc = pe.Node(interface=fsl.ImageMaths(op_string = '-Tmean', suffix='_mean'),
555577
name='meanfunc')
556-
featpreproc.connect(motion_correct, ('out_file', pickfirst), meanfunc, 'in_file')
578+
featpreproc.connect(motion_correct, ('out_file', pickrun, whichrun), meanfunc, 'in_file')
557579

558580
"""
559581
Strip the skull from the mean functional to generate a mask
@@ -699,7 +721,7 @@ def create_featreg_preproc(name='featpreproc', highpass=True, whichvol='middle')
699721
iterfield=['in_file'],
700722
name='meanfunc3')
701723

702-
featpreproc.connect(meanscale, ('out_file', pickfirst), meanfunc3, 'in_file')
724+
featpreproc.connect(meanscale, ('out_file', pickrun, whichrun), meanfunc3, 'in_file')
703725
featpreproc.connect(meanfunc3, 'out_file', outputnode, 'mean')
704726

705727
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
__author__ = 'oliver'
2+
3+
from ..preprocess import create_featreg_preproc, pickrun
4+
5+
6+
def test_pickrun():
7+
files = ['1', '2', '3', '4']
8+
assert pickrun(files, 0) == '1'
9+
assert pickrun(files, 'first') == '1'
10+
assert pickrun(files, -1) == '4'
11+
assert pickrun(files, 'last') == '4'
12+
assert pickrun(files, 'middle') == '3'
13+
14+
15+
def test_create_featreg_preproc():
16+
"""smoke test"""
17+
wf = create_featreg_preproc(whichrun=0)
18+
19+
# test type
20+
import nipype
21+
assert type(wf) == nipype.pipeline.engine.Workflow
22+
23+
# test methods
24+
assert wf.get_node('extractref')
25+
assert wf._get_dot()

0 commit comments

Comments
 (0)