Skip to content

Commit 7c30385

Browse files
committed
Merge branch 'pr/801'
2 parents 4c32e1a + 04a51ce commit 7c30385

File tree

13 files changed

+511
-4
lines changed

13 files changed

+511
-4
lines changed

nipype/interfaces/afni/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
Fourier, Allineate, Maskave, SkullStrip, TCat, Fim,
1313
BlurInMask, Autobox, TCorrMap, Bandpass, Retroicor,
1414
TCorrelate, TCorr1D, BrickStat, ROIStats, AutoTcorrelate,
15-
AFNItoNIFTI)
15+
AFNItoNIFTI, Eval, Means)
16+
from .svm import (SVMTest, SVMTrain)

nipype/interfaces/afni/preprocess.py

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ def _format_arg(self, name, trait_spec, value):
11111111
def _list_outputs(self):
11121112
outputs = self.output_spec().get()
11131113
if not isdefined(self.inputs.out_file):
1114-
outputs['out_file'] = self._gen_fname(self.inputs.in_file,
1114+
outputs['out_file'] = self._gen_filename(self.inputs.in_file,
11151115
suffix=self.inputs.suffix)
11161116
else:
11171117
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
@@ -1209,7 +1209,7 @@ class TCatInputSpec(AFNICommandInputSpec):
12091209
mandatory=True,
12101210
copyfile=False)
12111211
out_file = File(name_template="%s_tcat", desc='output image file name',
1212-
argstr='-prefix %s', name_source="in_file")
1212+
argstr='-prefix %s', name_source="in_files")
12131213
rlt = traits.Str(desc='options', argstr='-rlt%s', position=1)
12141214

12151215

@@ -1935,5 +1935,107 @@ def _overload_extension(self, value):
19351935

19361936
def _gen_filename(self, name):
19371937
return os.path.abspath(super(AFNItoNIFTI, self)._gen_filename(name))
1938+
1939+
class EvalInputSpec(AFNICommandInputSpec):
1940+
in_file_a = File(desc='input file to 1deval',
1941+
argstr='-a %s', position=0, mandatory=True, exists=True)
1942+
in_file_b = File(desc='operand file to 1deval',
1943+
argstr=' -b %s', position=1, exists=True)
1944+
in_file_c = File(desc='operand file to 1deval',
1945+
argstr=' -c %s', position=2, exists=True)
1946+
out_file = File(name_template="%s_calc", desc='output image file name',
1947+
argstr='-prefix %s', name_source="in_file_a")
1948+
out1D = traits.Bool(desc="output in 1D",
1949+
argstr='-1D')
1950+
expr = traits.Str(desc='expr', argstr='-expr "%s"', position=3,
1951+
mandatory=True)
1952+
start_idx = traits.Int(desc='start index for in_file_a',
1953+
requires=['stop_idx'])
1954+
stop_idx = traits.Int(desc='stop index for in_file_a',
1955+
requires=['start_idx'])
1956+
single_idx = traits.Int(desc='volume index for in_file_a')
1957+
other = File(desc='other options', argstr='')
1958+
1959+
class Eval(AFNICommand):
1960+
"""Evaluates an expression that may include columns of data from one or more text files
1961+
1962+
see AFNI Documentation: <http://afni.nimh.nih.gov/pub/dist/doc/program_help/1deval.html>
1963+
1964+
Examples
1965+
========
1966+
1967+
>>> from nipype.interfaces import afni as afni
1968+
>>> eval = afni.Eval()
1969+
>>> eval.inputs.in_file_a = 'seed.1D'
1970+
>>> eval.inputs.in_file_b = 'resp.1D'
1971+
>>> eval.inputs.expr='a*b'
1972+
>>> eval.inputs.out1D = True
1973+
>>> eval.inputs.out_file = 'data_calc.1D'
1974+
>>> calc.cmdline #doctest: +SKIP
1975+
'3deval -a timeseries1.1D -b timeseries2.1D -expr "a*b" -1D -prefix data_calc.1D'
1976+
1977+
"""
1978+
1979+
_cmd = '1deval'
1980+
input_spec = EvalInputSpec
1981+
output_spec = AFNICommandOutputSpec
1982+
1983+
def _format_arg(self, name, trait_spec, value):
1984+
if name == 'in_file_a':
1985+
arg = trait_spec.argstr % value
1986+
if isdefined(self.inputs.start_idx):
1987+
arg += '[%d..%d]' % (self.inputs.start_idx,
1988+
self.inputs.stop_idx)
1989+
if isdefined(self.inputs.single_idx):
1990+
arg += '[%d]' % (self.inputs.single_idx)
1991+
return arg
1992+
return super(Eval, self)._format_arg(name, trait_spec, value)
1993+
1994+
def _parse_inputs(self, skip=None):
1995+
"""Skip the arguments without argstr metadata
1996+
"""
1997+
return super(Eval, self)._parse_inputs(
1998+
skip=('start_idx', 'stop_idx', 'out1D', 'other'))
1999+
2000+
class MeansInputSpec(AFNICommandInputSpec):
2001+
in_file_a = File(desc='input file to 3dMean',
2002+
argstr='%s',
2003+
position=0,
2004+
mandatory=True,
2005+
exists=True)
2006+
in_file_b = File(desc='another input file to 3dMean',
2007+
argstr='%s',
2008+
position=1,
2009+
exists=True)
2010+
out_file = File(name_template="%s_mean", desc='output image file name',
2011+
argstr='-prefix %s', name_source="in_file_a")
2012+
scale = traits.Str(desc='scaling of output', argstr='-%sscale')
2013+
non_zero = traits.Bool(desc='use only non-zero values', argstr='-non_zero')
2014+
std_dev = traits.Bool(desc='calculate std dev', argstr='-stdev')
2015+
sqr = traits.Bool(desc='mean square instead of value', argstr='-sqr')
2016+
summ = traits.Bool(desc='take sum, (not average)', argstr='-sum')
2017+
count = traits.Bool(desc='compute count of non-zero voxels', argstr='-count')
2018+
mask_inter = traits.Bool(desc='create intersection mask', argstr='-mask_inter')
2019+
mask_union = traits.Bool(desc='create union mask', argstr='-mask_union')
19382020

2021+
class Means(AFNICommand):
2022+
"""Takes the voxel-by-voxel mean of all input datasets using 3dMean
19392023
2024+
see AFNI Documentation: <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dMean.html>
2025+
2026+
Examples
2027+
========
2028+
2029+
>>> from nipype.interfaces import afni as afni
2030+
>>> means = afni.Means()
2031+
>>> means.inputs.in_file_a = 'im1.nii'
2032+
>>> means.inputs.in_file_b = 'im2.nii'
2033+
>>> means.inputs.out_file = 'output.nii'
2034+
>>> means.cmdline
2035+
'3dMean im1.nii im2.nii -prefix output.nii'
2036+
2037+
"""
2038+
2039+
_cmd = '3dMean'
2040+
input_spec = MeansInputSpec
2041+
output_spec = AFNICommandOutputSpec

nipype/interfaces/afni/svm.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft = python sts = 4 ts = 4 sw = 4 et:
3+
"""Afni svm interfaces
4+
5+
Change directory to provide relative paths for doctests
6+
>>> import os
7+
>>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
8+
>>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data'))
9+
>>> os.chdir(datadir)
10+
"""
11+
import warnings
12+
13+
import os
14+
import re
15+
16+
from ..base import (Directory, TraitedSpec,
17+
traits, isdefined, File, InputMultiPath, Undefined)
18+
from ...utils.filemanip import (load_json, save_json, split_filename)
19+
from nipype.utils.filemanip import fname_presuffix
20+
from .base import AFNICommand, AFNICommandInputSpec,\
21+
AFNICommandOutputSpec
22+
from nipype.interfaces.base import CommandLineInputSpec, CommandLine,\
23+
OutputMultiPath
24+
25+
warn = warnings.warn
26+
warnings.filterwarnings('always', category=UserWarning)
27+
28+
class SVMTrainInputSpec(AFNICommandInputSpec):
29+
#training options
30+
ttype = traits.Str(desc='tname: classification or regression',
31+
argstr='-type %s',
32+
mandatory=True)
33+
in_file = File(desc='A 3D+t AFNI brik dataset to be used for training.',
34+
argstr='-trainvol %s',
35+
mandatory=True,
36+
exists=True,
37+
copyfile=False)
38+
out_file = File(name_template="%s_vectors",
39+
desc='output sum of weighted linear support vectors file name',
40+
argstr='-bucket %s',
41+
suffix='_bucket',
42+
name_source="in_file")
43+
model = File(name_template="%s_model",
44+
desc='basename for the brik containing the SVM model',
45+
argstr='-model %s',
46+
suffix='_model',
47+
name_source="in_file")
48+
alphas = File(name_template="%s_alphas",
49+
desc='output alphas file name',
50+
argstr='-alpha %s',
51+
suffix='_alphas',
52+
name_source="in_file")
53+
mask = File(desc='byte-format brik file used to mask voxels in the analysis',
54+
argstr='-mask %s',
55+
position=-1,
56+
exists=True,
57+
copyfile=False)
58+
nomodelmask = traits.Bool(desc='Flag to enable the omission of a mask file',
59+
argstr='-nomodelmask')
60+
trainlabels = File(desc='.1D labels corresponding to the stimulus paradigm for the training data.',
61+
argstr='-trainlabels %s',
62+
exists=True)
63+
censor = File(desc='.1D censor file that allows the user to ignore certain samples in the training data.',
64+
argstr='-censor %s',
65+
exists=True)
66+
kernel = traits.Str(desc='string specifying type of kernel function:linear, polynomial, rbf, sigmoid',
67+
argstr='-kernel %s')
68+
max_iterations = traits.Int(desc='Specify the maximum number of iterations for the optimization.',
69+
argstr='-max_iterations %d')
70+
w_out = traits.Bool(desc='output sum of weighted linear support vectors',
71+
argstr='-wout')
72+
options = traits.Str(desc='additional options for SVM-light', argstr='%s')
73+
74+
class SVMTrainOutputSpec(TraitedSpec):
75+
out_file = File(desc='sum of weighted linear support vectors file name')
76+
model = File(desc='brik containing the SVM model file name')
77+
alphas = File(desc='output alphas file name')
78+
79+
class SVMTrain(AFNICommand):
80+
"""Temporally predictive modeling with the support vector machine
81+
SVM Train Only
82+
For complete details, see the `3dsvm Documentation.
83+
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_
84+
85+
Examples
86+
========
87+
88+
>>> from nipype.interfaces import afni as afni
89+
>>> svmTrain = afni.SVMTrain()
90+
>>> svmTrain.inputs.in_file = 'run1+orig'
91+
>>> svmTrain.inputs.trainlabels = 'run1_categories.1D'
92+
>>> svmTrain.inputs.ttype = 'regression'
93+
>>> svmTrain.inputs.mask = 'mask.nii'
94+
>>> svmTrain.inputs.model = 'model_run1'
95+
>>> svmTrain.inputs.alphas = 'alphas_run1'
96+
>>> res = svmTrain.run() # doctest: +SKIP
97+
98+
"""
99+
100+
_cmd = '3dsvm'
101+
input_spec = SVMTrainInputSpec
102+
output_spec = SVMTrainOutputSpec
103+
_additional_metadata = ['suffix']
104+
105+
def _format_arg(self, name, trait_spec, value):
106+
return super(SVMTrain, self)._format_arg(name, trait_spec, value)
107+
108+
class SVMTestInputSpec(AFNICommandInputSpec):
109+
#testing options
110+
model = traits.Str(desc='modname is the basename for the brik containing the SVM model',
111+
argstr='-model %s',
112+
exists=True,
113+
mandatory=True)
114+
in_file = File(desc='A 3D or 3D+t AFNI brik dataset to be used for testing.',
115+
argstr='-testvol %s',
116+
exists=True,
117+
mandatory=True)
118+
out_file = File(name_template="%s_predictions",
119+
desc='filename for .1D prediction file(s).',
120+
argstr='-predictions %s')
121+
testlabels = File(desc='*true* class category .1D labels for the test dataset. It is used to calculate the prediction accuracy performance',
122+
exists=True,
123+
argstr='-testlabels %s')
124+
classout = traits.Bool(desc='Flag to specify that pname files should be integer-valued, corresponding to class category decisions.',
125+
argstr='-classout')
126+
nopredcensord = traits.Bool(desc='Flag to prevent writing predicted values for censored time-points',
127+
argstr='-nopredcensord')
128+
nodetrend = traits.Bool(desc='Flag to specify that pname files should not be linearly detrended',
129+
argstr='-nodetrend')
130+
multiclass = traits.Bool(desc='Specifies multiclass algorithm for classification',
131+
argstr='-multiclass %s')
132+
options = traits.Str(desc='additional options for SVM-light', argstr='%s')
133+
134+
class SVMTest(AFNICommand):
135+
"""Temporally predictive modeling with the support vector machine
136+
SVM Test Only
137+
For complete details, see the `3dsvm Documentation.
138+
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_
139+
140+
Examples
141+
========
142+
143+
>>> from nipype.interfaces import afni as afni
144+
>>> svmTest = afni.SVMTest()
145+
>>> svmTest.inputs.in_file= 'run2+orig'
146+
>>> svmTest.inputs.model= 'run1+orig_model'
147+
>>> svmTest.inputs.testlabels= 'run2_categories.1D'
148+
>>> svmTest.inputs.out_file= 'pred2_model1'
149+
>>> res = svmTest.run() # doctest: +SKIP
150+
151+
"""
152+
_cmd = '3dsvm'
153+
input_spec = SVMTestInputSpec
154+
output_spec = AFNICommandOutputSpec
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.afni.preprocess import Eval
4+
5+
def test_Eval_inputs():
6+
input_map = dict(args=dict(argstr='%s',
7+
),
8+
environ=dict(nohash=True,
9+
usedefault=True,
10+
),
11+
expr=dict(argstr='-expr "%s"',
12+
mandatory=True,
13+
position=3,
14+
),
15+
ignore_exception=dict(nohash=True,
16+
usedefault=True,
17+
),
18+
in_file_a=dict(argstr='-a %s',
19+
mandatory=True,
20+
position=0,
21+
),
22+
in_file_b=dict(argstr=' -b %s',
23+
position=1,
24+
),
25+
in_file_c=dict(argstr=' -c %s',
26+
position=2,
27+
),
28+
other=dict(argstr='',
29+
),
30+
out1D=dict(argstr='-1D',
31+
),
32+
out_file=dict(argstr='-prefix %s',
33+
name_source='in_file_a',
34+
name_template='%s_calc',
35+
),
36+
outputtype=dict(),
37+
single_idx=dict(),
38+
start_idx=dict(requires=['stop_idx'],
39+
),
40+
stop_idx=dict(requires=['start_idx'],
41+
),
42+
terminal_output=dict(mandatory=True,
43+
nohash=True,
44+
),
45+
)
46+
inputs = Eval.input_spec()
47+
48+
for key, metadata in input_map.items():
49+
for metakey, value in metadata.items():
50+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
51+
52+
def test_Eval_outputs():
53+
output_map = dict(out_file=dict(),
54+
)
55+
outputs = Eval.output_spec()
56+
57+
for key, metadata in output_map.items():
58+
for metakey, value in metadata.items():
59+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
60+

0 commit comments

Comments
 (0)