|
| 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 | + name_source="in_file") |
| 42 | + model = traits.Str(name_template="%s_model", |
| 43 | + desc='modname is the basename for the brik containing the SVM model', |
| 44 | + argstr='-model %s') |
| 45 | + alphas = traits.Str(name_template="%s_alphas", |
| 46 | + desc='output alphas file name', |
| 47 | + argstr='-alpha %s') |
| 48 | + mask = File(desc='byte-format brik file used to mask voxels in the analysis', |
| 49 | + argstr='-mask %s', |
| 50 | + position=-1, |
| 51 | + exists=True, |
| 52 | + copyfile=False) |
| 53 | + nomodelmask = traits.Bool(desc='Flag to enable the omission of a mask file', |
| 54 | + argstr='-nomodelmask') |
| 55 | + trainlabels = File(desc='.1D labels corresponding to the stimulus paradigm for the training data.', |
| 56 | + argstr='-trainlabels %s', |
| 57 | + exists=True) |
| 58 | + censor = File(desc='.1D censor file that allows the user to ignore certain samples in the training data.', |
| 59 | + argstr='-censor %s', |
| 60 | + exists=True) |
| 61 | + kernel = traits.Str(desc='string specifying type of kernel function:linear, polynomial, rbf, sigmoid', |
| 62 | + argstr='-kernel %s') |
| 63 | + max_iterations = traits.Int(desc='Specify the maximum number of iterations for the optimization.', |
| 64 | + argstr='-max_iterations %d') |
| 65 | + w_out = traits.Bool(desc='output sum of weighted linear support vectors', |
| 66 | + argstr='-wout') |
| 67 | + options = traits.Str(desc='additional options for SVM-light', argstr='%s') |
| 68 | + |
| 69 | +class SVMTrain(AFNICommand): |
| 70 | + """Temporally predictive modeling with the support vector machine |
| 71 | + SVM Train Only |
| 72 | + For complete details, see the `3dsvm Documentation. |
| 73 | + <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_ |
| 74 | +
|
| 75 | + Examples |
| 76 | + ======== |
| 77 | +
|
| 78 | + >>> from nipype.interfaces import afni as afni |
| 79 | + >>> svmTrain = afni.svmTrain() |
| 80 | + >>> svmTrain.inputs.in_file= 'run1+orig' |
| 81 | + >>> svmTrain.inputs.trainlabels= 'run1_categories.1D' |
| 82 | + >>> svmTrain.inputs.ttype = 'regression' |
| 83 | + >>> svmTrain.inputs.mask= 'mask+orig' |
| 84 | + >>> svmTrain.inputs.model= 'model_run1' |
| 85 | + >>> svmTrain.inputs.alphas= 'alphas_run1' |
| 86 | + >>> res = svmTrain.run() # doctest: +SKIP |
| 87 | +
|
| 88 | + """ |
| 89 | + |
| 90 | + _cmd = '3dsvm' |
| 91 | + input_spec = SVMTrainInputSpec |
| 92 | + output_spec = AFNICommandOutputSpec |
| 93 | + |
| 94 | +class SVMTestInputSpec(AFNICommandInputSpec): |
| 95 | + #testing options |
| 96 | + model = traits.Str(desc='modname is the basename for the brik containing the SVM model', |
| 97 | + argstr='-model %s', |
| 98 | + exists=True, |
| 99 | + mandatory=True) |
| 100 | + in_file = File(desc='A 3D or 3D+t AFNI brik dataset to be used for testing.', |
| 101 | + argstr='-testvol %s', |
| 102 | + exists=True, |
| 103 | + mandatory=True) |
| 104 | + out_file = File(name_template="%s_predictions", |
| 105 | + desc='filename for .1D prediction file(s).', |
| 106 | + argstr='-predictions %s') |
| 107 | + testlabels = File(desc='*true* class category .1D labels for the test dataset. It is used to calculate the prediction accuracy performance', |
| 108 | + exists=True, |
| 109 | + argstr='-testlabels %s') |
| 110 | + classout = traits.Bool(desc='Flag to specify that pname files should be integer-valued, corresponding to class category decisions.', |
| 111 | + argstr='-classout') |
| 112 | + nopredcensord = traits.Bool(desc='Flag to prevent writing predicted values for censored time-points', |
| 113 | + argstr='-nopredcensord') |
| 114 | + nodetrend = traits.Bool(desc='Flag to specify that pname files should not be linearly detrended', |
| 115 | + argstr='-nodetrend') |
| 116 | + multiclass = traits.Bool(desc='Specifies multiclass algorithm for classification', |
| 117 | + argstr='-multiclass %s') |
| 118 | + options = traits.Str(desc='additional options for SVM-light', argstr='%s') |
| 119 | + |
| 120 | +class SVMTest(AFNICommand): |
| 121 | + """Temporally predictive modeling with the support vector machine |
| 122 | + SVM Test Only |
| 123 | + For complete details, see the `3dsvm Documentation. |
| 124 | + <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dsvm.html>`_ |
| 125 | +
|
| 126 | + Examples |
| 127 | + ======== |
| 128 | +
|
| 129 | + >>> from nipype.interfaces import afni as afni |
| 130 | + >>> svmTest = afni.svmTest() |
| 131 | + >>> svmTest.inputs.in_file= 'run2+orig' |
| 132 | + >>> svmTest.inputs.model= 'run1+orig_model' |
| 133 | + >>> svmTest.inputs.testlabels= 'run2_categories.1D' |
| 134 | + >>> svmTest.inputs.out_file= 'pred2_model1' |
| 135 | + >>> res = svmTest.run() # doctest: +SKIP |
| 136 | +
|
| 137 | + """ |
| 138 | + _cmd = '3dsvm' |
| 139 | + input_spec = SVMTestInputSpec |
| 140 | + output_spec = AFNICommandOutputSpec |
0 commit comments