Skip to content

Commit 9fdd7a5

Browse files
committed
add two new interfaces for AFNI
1 parent a7876c5 commit 9fdd7a5

File tree

4 files changed

+283
-1
lines changed

4 files changed

+283
-1
lines changed

nipype/interfaces/afni/__init__.py

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

nipype/interfaces/afni/preprocess.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,3 +2372,145 @@ def _list_outputs(self):
23722372

23732373
outputs['fwhm'] = tuple(sout)
23742374
return outputs
2375+
2376+
2377+
class OutlierCountInputSpec(AFNICommandInputSpec):
2378+
in_file = File(argstr='%s', mandatory=True, exists=True, position=-2, desc='input dataset')
2379+
mask = File(exists=True, argstr='-mask %s', xor=['autoclip', 'automask'],
2380+
desc='only count voxels within the given mask')
2381+
qthr = traits.Range(value=1e-3, low=0.0, high=1.0, argstr='-qthr %.5f',
2382+
desc='indicate a value for q to compute alpha')
2383+
2384+
autoclip = traits.Bool(False, usedefault=True, argstr='-autoclip', xor=['in_file'],
2385+
desc='clip off small voxels')
2386+
automask = traits.Bool(False, usedefault=True, argstr='-automask', xor=['in_file'],
2387+
desc='clip off small voxels')
2388+
2389+
fraction = traits.Bool(False, usedefault=True, argstr='-fraction',
2390+
desc='write out the fraction of masked voxels'
2391+
' which are outliers at each timepoint')
2392+
interval = traits.Bool(False, usedefault=True, argstr='-range',
2393+
desc='write out the median + 3.5 MAD of outlier'
2394+
' count with each timepoint')
2395+
save_outliers = traits.Bool(False, usedefault=True, desc='enables out_file option')
2396+
outliers_file = File(name_template="%s_outliers", argstr='-save %s', name_source=["in_file"],
2397+
keep_extension=True, desc='output image file name')
2398+
2399+
polort = traits.Int(argstr='-polort %d',
2400+
desc='detrend each voxel timeseries with polynomials')
2401+
legendre = traits.Bool(False, usedefault=True, argstr='-legendre',
2402+
desc='use Legendre polynomials')
2403+
out_file = File(name_template='%s_outliers', name_source=['in_file'], argstr='> %s',
2404+
keep_extension=False, position=-1, desc='capture standard output')
2405+
2406+
2407+
class OutlierCountOutputSpec(AFNICommandOutputSpec):
2408+
outliers_file = File(exists=True, desc='output image file name')
2409+
outliers = traits.List(traits.Float,
2410+
desc='parse standard output to get the count of outliers')
2411+
2412+
2413+
class OutlierCount(AFNICommand):
2414+
"""Create a 3D dataset from 2D image files using AFNI to3d command
2415+
2416+
For complete details, see the `to3d Documentation
2417+
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/to3d.html>`_
2418+
2419+
Examples
2420+
========
2421+
2422+
>>> from nipype.interfaces import afni
2423+
>>> toutcount = afni.OutlierCount()
2424+
>>> toutcount.inputs.in_file = 'functional.nii'
2425+
>>> toutcount.cmdline #doctest: +ELLIPSIS
2426+
'3dToutcount functional.nii > functional_outliers'
2427+
>>> res = toutcount.run() #doctest: +SKIP
2428+
2429+
"""
2430+
2431+
_cmd = '3dToutcount'
2432+
input_spec = OutlierCountInputSpec
2433+
output_spec = OutlierCountOutputSpec
2434+
2435+
def _parse_inputs(self, skip=None):
2436+
if skip is None:
2437+
skip = []
2438+
2439+
if not self.inputs.save_outliers:
2440+
skip += ['outliers_file']
2441+
return super(OutlierCount, self)._parse_inputs(skip)
2442+
2443+
def _list_outputs(self):
2444+
outputs = self.output_spec().get()
2445+
outputs['outliers_file'] = (Undefined if not self.inputs.save_outliers
2446+
else self.inputs.outliers_file)
2447+
2448+
2449+
with open(self.inputs.out_file, 'r') as fout:
2450+
lines = fout.readlines()
2451+
# remove general information and warnings
2452+
outputs['outliers'] = [float(l)
2453+
for l in lines if re.match("[0-9]+$", l.strip())]
2454+
return outputs
2455+
2456+
2457+
class QualityIndexInputSpec(AFNICommandInputSpec):
2458+
in_file = File(argstr='%s', mandatory=True, exists=True, position=-2, desc='input dataset')
2459+
mask = File(exists=True, argstr='-mask %s',
2460+
desc='compute correlation only across masked voxels')
2461+
spearman = traits.Bool(False, usedefault=True, argstr='-spearman',
2462+
desc='Quality index is 1 minus the Spearman (rank) '
2463+
'correlation coefficient of each sub-brick '
2464+
'with the median sub-brick. (default)')
2465+
quadrant = traits.Bool(False, usedefault=True, argstr='-quadrant',
2466+
desc='Similar to -spearman, but using 1 minus the '
2467+
'quadrant correlation coefficient as the '
2468+
'quality index.')
2469+
autoclip = traits.Bool(False, usedefault=True, argstr='-autoclip',
2470+
desc='clip off small voxels')
2471+
automask = traits.Bool(False, usedefault=True, argstr='-automask',
2472+
desc='clip off small voxels')
2473+
clip = traits.Float(argstr='-clip %f', desc='clip off values below')
2474+
2475+
interval = traits.Bool(False, usedefault=True, argstr='-range',
2476+
desc='write out the median + 3.5 MAD of outlier'
2477+
' count with each timepoint')
2478+
out_file = File(name_template='%s_tqual', name_source=['in_file'], argstr='> %s',
2479+
keep_extension=False, position=-1, desc='capture standard output')
2480+
2481+
2482+
class QualityIndexOutputSpec(AFNICommandOutputSpec):
2483+
qi_value = traits.List(traits.Float, desc='output quality index')
2484+
2485+
2486+
class QualityIndex(AFNICommand):
2487+
"""Create a 3D dataset from 2D image files using AFNI to3d command
2488+
2489+
For complete details, see the `to3d Documentation
2490+
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/to3d.html>`_
2491+
2492+
Examples
2493+
========
2494+
2495+
>>> from nipype.interfaces import afni
2496+
>>> tqual = afni.QualityIndex()
2497+
>>> tqual.inputs.in_file = 'functional.nii'
2498+
>>> tqual.cmdline #doctest: +ELLIPSIS
2499+
'3dTqual functional.nii > functional_tqual'
2500+
>>> res = tqual.run() #doctest: +SKIP
2501+
2502+
"""
2503+
2504+
_cmd = '3dTqual'
2505+
input_spec = QualityIndexInputSpec
2506+
output_spec = QualityIndexOutputSpec
2507+
2508+
def _list_outputs(self):
2509+
outputs = self.output_spec().get()
2510+
with open(self.inputs.out_file, 'r') as fout:
2511+
lines = fout.readlines()
2512+
# remove general information
2513+
lines = [l for l in lines if l[:2] != "++"]
2514+
# remove general information and warnings
2515+
outputs['qi_value'] = [float(l.strip()) for l in lines]
2516+
return outputs
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from ....testing import assert_equal
3+
from ..preprocess import OutlierCount
4+
5+
6+
def test_OutlierCount_inputs():
7+
input_map = dict(args=dict(argstr='%s',
8+
),
9+
autoclip=dict(argstr='-autoclip',
10+
usedefault=True,
11+
xor=['in_file'],
12+
),
13+
automask=dict(argstr='-automask',
14+
usedefault=True,
15+
xor=['in_file'],
16+
),
17+
environ=dict(nohash=True,
18+
usedefault=True,
19+
),
20+
fraction=dict(argstr='-fraction',
21+
usedefault=True,
22+
),
23+
ignore_exception=dict(nohash=True,
24+
usedefault=True,
25+
),
26+
in_file=dict(argstr='%s',
27+
mandatory=True,
28+
position=-2,
29+
),
30+
interval=dict(argstr='-range',
31+
usedefault=True,
32+
),
33+
legendre=dict(argstr='-legendre',
34+
usedefault=True,
35+
),
36+
mask=dict(argstr='-mask %s',
37+
xor=['autoclip', 'automask'],
38+
),
39+
out_file=dict(argstr='> %s',
40+
keep_extension=False,
41+
name_source=['in_file'],
42+
name_template='%s_outliers',
43+
position=-1,
44+
),
45+
outliers_file=dict(argstr='-save %s',
46+
keep_extension=True,
47+
name_source=['in_file'],
48+
name_template='%s_outliers',
49+
),
50+
outputtype=dict(),
51+
polort=dict(argstr='-polort %d',
52+
),
53+
qthr=dict(argstr='-qthr %.5f',
54+
),
55+
save_outliers=dict(usedefault=True,
56+
),
57+
terminal_output=dict(nohash=True,
58+
),
59+
)
60+
inputs = OutlierCount.input_spec()
61+
62+
for key, metadata in list(input_map.items()):
63+
for metakey, value in list(metadata.items()):
64+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
65+
66+
67+
def test_OutlierCount_outputs():
68+
output_map = dict(out_file=dict(),
69+
outliers=dict(),
70+
outliers_file=dict(),
71+
)
72+
outputs = OutlierCount.output_spec()
73+
74+
for key, metadata in list(output_map.items()):
75+
for metakey, value in list(metadata.items()):
76+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from ....testing import assert_equal
3+
from ..preprocess import QualityIndex
4+
5+
6+
def test_QualityIndex_inputs():
7+
input_map = dict(args=dict(argstr='%s',
8+
),
9+
autoclip=dict(argstr='-autoclip',
10+
usedefault=True,
11+
),
12+
automask=dict(argstr='-automask',
13+
usedefault=True,
14+
),
15+
clip=dict(argstr='-clip %f',
16+
),
17+
environ=dict(nohash=True,
18+
usedefault=True,
19+
),
20+
ignore_exception=dict(nohash=True,
21+
usedefault=True,
22+
),
23+
in_file=dict(argstr='%s',
24+
mandatory=True,
25+
position=-2,
26+
),
27+
interval=dict(argstr='-range',
28+
usedefault=True,
29+
),
30+
mask=dict(argstr='-mask %s',
31+
),
32+
out_file=dict(argstr='> %s',
33+
keep_extension=False,
34+
name_source=['in_file'],
35+
name_template='%s_tqual',
36+
position=-1,
37+
),
38+
outputtype=dict(),
39+
quadrant=dict(argstr='-quadrant',
40+
usedefault=True,
41+
),
42+
spearman=dict(argstr='-spearman',
43+
usedefault=True,
44+
),
45+
terminal_output=dict(nohash=True,
46+
),
47+
)
48+
inputs = QualityIndex.input_spec()
49+
50+
for key, metadata in list(input_map.items()):
51+
for metakey, value in list(metadata.items()):
52+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
53+
54+
55+
def test_QualityIndex_outputs():
56+
output_map = dict(out_file=dict(),
57+
qi_value=dict(),
58+
)
59+
outputs = QualityIndex.output_spec()
60+
61+
for key, metadata in list(output_map.items()):
62+
for metakey, value in list(metadata.items()):
63+
yield assert_equal, getattr(outputs.traits()[key], metakey), value

0 commit comments

Comments
 (0)