|
3 | 3 | import nipype.pipeline.engine as pe |
4 | 4 | from nipype.interfaces import spm |
5 | 5 | from nipype.interfaces import fsl |
| 6 | +from nipype.interfaces import utility as niu |
| 7 | +from nipype.interfaces import io as nio |
6 | 8 | from nipype.algorithms.misc import Gunzip |
7 | | -import os |
| 9 | +import os.path as op |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +def test_spm(name='test_spm_3d'): |
| 14 | + """ |
| 15 | + A simple workflow to test SPM's installation. By default will split the 4D volume in |
| 16 | + time-steps. |
| 17 | + """ |
| 18 | + workflow = pe.Workflow(name=name) |
| 19 | + |
| 20 | + inputnode = pe.Node(niu.IdentityInterface(fields=['in_data']), name='inputnode') |
| 21 | + dgr = pe.Node(nio.DataGrabber(template="feeds/data/fmri.nii.gz", outfields=['out_file']), |
| 22 | + name='datasource') |
| 23 | + |
| 24 | + stc = pe.Node(spm.SliceTiming( |
| 25 | + num_slices=21, time_repetition=1.0, time_acquisition=2. - 2. / 32, |
| 26 | + slice_order=list(range(21, 0, -1)), ref_slice=10), name='stc') |
| 27 | + realign_estimate = pe.Node(spm.Realign(jobtype='estimate'), name='realign_estimate') |
| 28 | + realign_write = pe.Node(spm.Realign(jobtype='write'), name='realign_write') |
| 29 | + realign_estwrite = pe.Node(spm.Realign(jobtype='estwrite'), name='realign_estwrite') |
| 30 | + smooth = pe.Node(spm.Smooth(fwhm=[6, 6, 6]), name='smooth') |
| 31 | + |
| 32 | + |
| 33 | + if name == 'test_spm_3d': |
| 34 | + split = pe.Node(fsl.Split(dimension="t", output_type="NIFTI"), name="split") |
| 35 | + workflow.connect([ |
| 36 | + (dgr, split, [('out_file', 'in_file')]), |
| 37 | + (split, stc, [("out_files", "in_files")])]) |
| 38 | + elif name == 'test_spm_4d': |
| 39 | + gunzip = pe.Node(Gunzip(), name="gunzip") |
| 40 | + workflow.connect([ |
| 41 | + (dgr, gunzip, [('out_file', 'in_file')]), |
| 42 | + (gunzip, stc, [("out_file", "in_files")]) |
| 43 | + ]) |
| 44 | + else: |
| 45 | + raise NotImplementedError('No implementation of the test workflow \'{}\' was found'.format( |
| 46 | + name)) |
| 47 | + |
| 48 | + workflow.connect([ |
| 49 | + (inputnode, dgr, [('in_data', 'base_directory')]), |
| 50 | + (stc, realign_estimate, [('timecorrected_files', 'in_files')]), |
| 51 | + (realign_estimate, realign_write, [('modified_in_files', 'in_files')]), |
| 52 | + (stc, realign_estwrite, [('timecorrected_files', 'in_files')]), |
| 53 | + (realign_write, smooth, [('realigned_files', 'in_files')]) |
| 54 | + ]) |
| 55 | + return workflow |
| 56 | + |
| 57 | +workflow3d = test_spm() |
| 58 | +workflow3d.inputs.inputnode.in_data = '/root/examples' |
| 59 | +workflow4d = test_spm(name='test_spm_4d') |
| 60 | +workflow4d.inputs.inputnode.in_data = '/root/examples' |
8 | 61 |
|
9 | | -in_file = "feeds/data/fmri.nii.gz" |
10 | | - |
11 | | -split = pe.Node(fsl.Split(dimension="t", output_type="NIFTI"), name="split") |
12 | | -split.inputs.in_file = os.path.abspath(in_file) |
13 | | - |
14 | | -stc = pe.Node(interface=spm.SliceTiming(), name='stc') |
15 | | -stc.inputs.num_slices = 21 |
16 | | -stc.inputs.time_repetition = 1.0 |
17 | | -stc.inputs.time_acquisition = 2. - 2. / 32 |
18 | | -stc.inputs.slice_order = list(range(21, 0, -1)) |
19 | | -stc.inputs.ref_slice = 10 |
20 | | - |
21 | | -realign_estimate = pe.Node(interface=spm.Realign(), name='realign_estimate') |
22 | | -realign_estimate.inputs.jobtype = "estimate" |
23 | | - |
24 | | -realign_write = pe.Node(interface=spm.Realign(), name='realign_write') |
25 | | -realign_write.inputs.jobtype = "write" |
26 | | - |
27 | | -realign_estwrite = pe.Node(interface=spm.Realign(), name='realign_estwrite') |
28 | | -realign_estwrite.inputs.jobtype = "estwrite" |
29 | | -realign_estwrite.inputs.register_to_mean = True |
30 | | - |
31 | | -smooth = pe.Node(interface=spm.Smooth(), name='smooth') |
32 | | -smooth.inputs.fwhm = [6, 6, 6] |
33 | | - |
34 | | -workflow3d = pe.Workflow(name='test_3d') |
35 | | -workflow3d.base_dir = "/tmp" |
36 | | - |
37 | | -workflow3d.connect([(split, stc, [("out_files", "in_files")]), |
38 | | - (stc, realign_estimate, [('timecorrected_files', 'in_files')]), |
39 | | - (realign_estimate, realign_write, [('modified_in_files', 'in_files')]), |
40 | | - (stc, realign_estwrite, [('timecorrected_files', 'in_files')]), |
41 | | - (realign_write, smooth, [('realigned_files', 'in_files')])]) |
42 | | - |
43 | | -workflow3d.run() |
44 | | - |
45 | | - |
46 | | -gunzip = pe.Node(Gunzip(), name="gunzip") |
47 | | -gunzip.inputs.in_file = os.path.abspath(in_file) |
48 | | - |
49 | | -stc = pe.Node(interface=spm.SliceTiming(), name='stc') |
50 | | -stc.inputs.num_slices = 21 |
51 | | -stc.inputs.time_repetition = 1.0 |
52 | | -stc.inputs.time_acquisition = 2. - 2. / 32 |
53 | | -stc.inputs.slice_order = list(range(21, 0, -1)) |
54 | | -stc.inputs.ref_slice = 10 |
55 | | - |
56 | | -realign_estimate = pe.Node(interface=spm.Realign(), name='realign_estimate') |
57 | | -realign_estimate.inputs.jobtype = "estimate" |
58 | | - |
59 | | -realign_write = pe.Node(interface=spm.Realign(), name='realign_write') |
60 | | -realign_write.inputs.jobtype = "write" |
61 | | - |
62 | | -realign_estwrite = pe.Node(interface=spm.Realign(), name='realign_estwrite') |
63 | | -realign_estwrite.inputs.jobtype = "estwrite" |
64 | | - |
65 | | -smooth = pe.Node(interface=spm.Smooth(), name='smooth') |
66 | | -smooth.inputs.fwhm = [6, 6, 6] |
67 | | - |
68 | | -workflow4d = pe.Workflow(name='test_4d') |
69 | | -workflow4d.base_dir = "/tmp" |
70 | | - |
71 | | -workflow4d.connect([(gunzip, stc, [("out_file", "in_files")]), |
72 | | - (stc, realign_estimate, [('timecorrected_files', 'in_files')]), |
73 | | - (realign_estimate, realign_write, [('modified_in_files', 'in_files')]), |
74 | | - (stc, realign_estwrite, [('timecorrected_files', 'in_files')]), |
75 | | - (realign_write, smooth, [('realigned_files', 'in_files')])]) |
76 | | - |
77 | | -workflow4d.run() |
0 commit comments