-
Notifications
You must be signed in to change notification settings - Fork 0
/
doit_spm.py
108 lines (80 loc) · 3.25 KB
/
doit_spm.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
from nipype.interfaces.spm.base import SPMCommand, SPMCommandInputSpec
from nipype.interfaces.base import (BaseInterface, TraitedSpec, traits, File,
OutputMultiPath, BaseInterfaceInputSpec,
isdefined, InputMultiPath)
import nipype.pipeline.engine as pe
import nipype.interfaces.io as nio
from nipype.interfaces.utility import IdentityInterface
import os
#from utils import doit_workflow
class DoitInputSpec(SPMCommandInputSpec):
data_ref = traits.List(File(exists=True), field="doit.data_ref")
bin_thresh = traits.Any(0.5, field="doit.bin_thresh", usedefault=True) #default is 0.5
class DoitOutputSpec(TraitedSpec):
csv_file = OutputMultiPath(File(exists=True))
class Doit(SPMCommand):
input_spec = DoitInputSpec
output_spec = DoitOutputSpec
_jobtype = 'tools'
_jobname = 'LST'
def _make_matlab_command(self, contents, postscript=None):
len_ref = len(self.inputs.data_ref)
contents = """
%% Generated by nipype.interfaces.spm
if isempty(which('spm')),
throw(MException('SPMCheck:NotFound', 'SPM not in matlab path'));
end
[name, version] = spm('ver');
fprintf('SPM version: %s Release: %s',name, version);
fprintf('SPM path: %s', which('spm'));
spm('Defaults','fMRI');
if strcmp(name, 'SPM8') || strcmp(name(1:5), 'SPM12'),
spm_jobman('initcfg');
spm_get_defaults('cmdline', 1);
end
"""
contents += """
jobs{1}.spm.tools.LST.doit.bin_thresh = %f;
""" % self.inputs.bin_thresh
for i, ref in enumerate(self.inputs.data_ref):
contents += """
jobs{1}.spm.tools.LST.doit.data_ref{%d, 1} = '%s';
""" % (i+1,
ref)
contents += """
spm_jobman('run', jobs);
"""
return contents
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm
"""
# import numpy as np
# from nipype.utils.filemanip import copyfiles
# if opt in ['t1_files', 'flair_files']:
# val2 = copyfiles(val, os.path.abspath("."))
# return np.array(val2, dtype=object)
print("_format_arg opt is: ", opt)
print("_format_arg opt is: ", spec)
print("_format_arg opt is: ", val)
# TODO change the format of data_ref
return super(Doit, self)._format_arg(opt, spec, val)
def _list_outputs(self):
#from nipype.utils.filemanip import fname_presuffix
from glob import glob
from os.path import join
outputs = self._outputs().get()
print("Listing outputs!")
print(os.path.abspath('.'))
outputs["csv_file"] = glob(join(os.path.abspath('.'), "LST_doit_*.csv"))
print(outputs)
return outputs
if __name__ == '__main__':
from glob import glob
foo = Doit()
print("The default value is: ", foo.inputs.bin_thresh)
foo.inputs.data_ref = glob("/data/henry1/tristan/LST/FLAIR-MPRAGE/*/*_bin_lesion_map.nii")
print("data_ref: ", foo.inputs.data_ref)
foo.inputs.bin_thresh = 0.5
print("The threshold is: ", foo.inputs.bin_thresh)
foo.run()
## Make a workflow