-
Notifications
You must be signed in to change notification settings - Fork 521
/
svm.py
176 lines (156 loc) · 5.65 KB
/
svm.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
173
174
175
176
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""AFNI's svm interfaces."""
from ..base import TraitedSpec, traits, File
from .base import AFNICommand, AFNICommandInputSpec, AFNICommandOutputSpec
class SVMTrainInputSpec(AFNICommandInputSpec):
# training options
ttype = traits.Str(
desc="tname: classification or regression", argstr="-type %s", mandatory=True
)
in_file = File(
desc="A 3D+t AFNI brik dataset to be used for training.",
argstr="-trainvol %s",
mandatory=True,
exists=True,
copyfile=False,
)
out_file = File(
name_template="%s_vectors",
desc="output sum of weighted linear support vectors file name",
argstr="-bucket %s",
suffix="_bucket",
name_source="in_file",
)
model = File(
name_template="%s_model",
desc="basename for the brik containing the SVM model",
argstr="-model %s",
suffix="_model",
name_source="in_file",
)
alphas = File(
name_template="%s_alphas",
desc="output alphas file name",
argstr="-alpha %s",
suffix="_alphas",
name_source="in_file",
)
mask = File(
desc="byte-format brik file used to mask voxels in the analysis",
argstr="-mask %s",
position=-1,
exists=True,
copyfile=False,
)
nomodelmask = traits.Bool(
desc="Flag to enable the omission of a mask file", argstr="-nomodelmask"
)
trainlabels = File(
desc=".1D labels corresponding to the stimulus paradigm for the training data.",
argstr="-trainlabels %s",
exists=True,
)
censor = File(
desc=".1D censor file that allows the user to ignore certain samples in the training data.",
argstr="-censor %s",
exists=True,
)
kernel = traits.Str(
desc="string specifying type of kernel function:linear, polynomial, rbf, sigmoid",
argstr="-kernel %s",
)
max_iterations = traits.Int(
desc="Specify the maximum number of iterations for the optimization.",
argstr="-max_iterations %d",
)
w_out = traits.Bool(
desc="output sum of weighted linear support vectors", argstr="-wout"
)
options = traits.Str(desc="additional options for SVM-light", argstr="%s")
class SVMTrainOutputSpec(TraitedSpec):
out_file = File(desc="sum of weighted linear support vectors file name")
model = File(desc="brik containing the SVM model file name")
alphas = File(desc="output alphas file name")
class SVMTrain(AFNICommand):
"""Temporally predictive modeling with the support vector machine
SVM Train Only
For complete details, see the `3dsvm Documentation.
<https://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_
Examples
========
>>> from nipype.interfaces import afni as afni
>>> svmTrain = afni.SVMTrain()
>>> svmTrain.inputs.in_file = 'run1+orig'
>>> svmTrain.inputs.trainlabels = 'run1_categories.1D'
>>> svmTrain.inputs.ttype = 'regression'
>>> svmTrain.inputs.mask = 'mask.nii'
>>> svmTrain.inputs.model = 'model_run1'
>>> svmTrain.inputs.alphas = 'alphas_run1'
>>> res = svmTrain.run() # doctest: +SKIP
"""
_cmd = "3dsvm"
input_spec = SVMTrainInputSpec
output_spec = SVMTrainOutputSpec
_additional_metadata = ["suffix"]
def _format_arg(self, name, trait_spec, value):
return super(SVMTrain, self)._format_arg(name, trait_spec, value)
class SVMTestInputSpec(AFNICommandInputSpec):
# testing options
model = traits.Str(
desc="modname is the basename for the brik containing the SVM model",
argstr="-model %s",
mandatory=True,
)
in_file = File(
desc="A 3D or 3D+t AFNI brik dataset to be used for testing.",
argstr="-testvol %s",
exists=True,
mandatory=True,
)
out_file = File(
name_template="%s_predictions",
desc="filename for .1D prediction file(s).",
argstr="-predictions %s",
)
testlabels = File(
desc="*true* class category .1D labels for the test dataset. It is used to calculate the prediction accuracy performance",
exists=True,
argstr="-testlabels %s",
)
classout = traits.Bool(
desc="Flag to specify that pname files should be integer-valued, corresponding to class category decisions.",
argstr="-classout",
)
nopredcensord = traits.Bool(
desc="Flag to prevent writing predicted values for censored time-points",
argstr="-nopredcensord",
)
nodetrend = traits.Bool(
desc="Flag to specify that pname files should not be linearly detrended",
argstr="-nodetrend",
)
multiclass = traits.Bool(
desc="Specifies multiclass algorithm for classification",
argstr="-multiclass %s",
)
options = traits.Str(desc="additional options for SVM-light", argstr="%s")
class SVMTest(AFNICommand):
"""Temporally predictive modeling with the support vector machine
SVM Test Only
For complete details, see the `3dsvm Documentation.
<https://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_
Examples
========
>>> from nipype.interfaces import afni as afni
>>> svmTest = afni.SVMTest()
>>> svmTest.inputs.in_file= 'run2+orig'
>>> svmTest.inputs.model= 'run1+orig_model'
>>> svmTest.inputs.testlabels= 'run2_categories.1D'
>>> svmTest.inputs.out_file= 'pred2_model1'
>>> res = svmTest.run() # doctest: +SKIP
"""
_cmd = "3dsvm"
input_spec = SVMTestInputSpec
output_spec = AFNICommandOutputSpec