Skip to content

Commit e2ec11c

Browse files
committed
Merge pull request #1360 from oesteban/enh/AddAfniHist
[ENH] Addition of 3dHist from AFNI
2 parents bfb918f + 286fd7c commit e2ec11c

File tree

4 files changed

+138
-5
lines changed

4 files changed

+138
-5
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Next release
22
============
33

4+
* ENH: Added interfaces of AFNI (https://github.com/nipy/nipype/pull/1360)
45
* ENH: Added support for PETPVC (https://github.com/nipy/nipype/pull/1335)
56
* ENH: Merge S3DataSink into DataSink, added AWS documentation (https://github.com/nipy/nipype/pull/1316)
67
* TST: Cache APT in CircleCI (https://github.com/nipy/nipype/pull/1333)

nipype/interfaces/afni/__init__.py

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

nipype/interfaces/afni/preprocess.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
"""
1111

1212
import os
13+
import os.path as op
1314
import re
1415
from warnings import warn
1516

16-
from .base import AFNICommand, AFNICommandInputSpec, AFNICommandOutputSpec
17+
from .base import AFNICommand, AFNICommandInputSpec, AFNICommandOutputSpec, Info, no_afni
1718
from ..base import CommandLineInputSpec, CommandLine, OutputMultiPath
1819
from ..base import (Directory, TraitedSpec,
1920
traits, isdefined, File, InputMultiPath, Undefined)
2021
from ...utils.filemanip import (load_json, save_json, split_filename)
2122
from ...utils.filemanip import fname_presuffix
2223

23-
2424
class To3DInputSpec(AFNICommandInputSpec):
2525
out_file = File(name_template="%s", desc='output image file name',
2626
argstr='-prefix %s', name_source=["in_folder"])
@@ -1234,9 +1234,7 @@ class SkullStrip(AFNICommand):
12341234
output_spec = AFNICommandOutputSpec
12351235

12361236
def __init__(self, **inputs):
1237-
from .base import Info, no_afni
12381237
super(SkullStrip, self).__init__(**inputs)
1239-
12401238
if not no_afni():
12411239
v = Info.version()
12421240

@@ -2090,3 +2088,75 @@ class Means(AFNICommand):
20902088
_cmd = '3dMean'
20912089
input_spec = MeansInputSpec
20922090
output_spec = AFNICommandOutputSpec
2091+
2092+
2093+
class HistInputSpec(CommandLineInputSpec):
2094+
in_file = File(
2095+
desc='input file to 3dHist', argstr='-input %s', position=1, mandatory=True,
2096+
exists=True, copyfile=False)
2097+
out_file = File(
2098+
desc='Write histogram to niml file with this prefix', name_template='%s_hist',
2099+
keep_extension=False, argstr='-prefix %s', name_source=['in_file'])
2100+
showhist = traits.Bool(False, usedefault=True, desc='write a text visual histogram',
2101+
argstr='-showhist')
2102+
out_show = File(
2103+
name_template="%s_hist.out", desc='output image file name', keep_extension=False,
2104+
argstr="> %s", name_source="in_file", position=-1)
2105+
mask = File(desc='matrix to align input file', argstr='-mask %s', exists=True)
2106+
nbin = traits.Int(desc='number of bins', argstr='-nbin %d')
2107+
max_value = traits.Float(argstr='-max %f', desc='maximum intensity value')
2108+
min_value = traits.Float(argstr='-min %f', desc='minimum intensity value')
2109+
bin_width = traits.Float(argstr='-binwidth %f', desc='bin width')
2110+
2111+
class HistOutputSpec(TraitedSpec):
2112+
out_file = File(desc='output file', exists=True)
2113+
out_show = File(desc='output visual histogram')
2114+
2115+
2116+
class Hist(CommandLine):
2117+
"""Computes average of all voxels in the input dataset
2118+
which satisfy the criterion in the options list
2119+
2120+
For complete details, see the `3dHist Documentation.
2121+
<http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dHist.html>`_
2122+
2123+
Examples
2124+
========
2125+
2126+
>>> from nipype.interfaces import afni as afni
2127+
>>> hist = afni.Hist()
2128+
>>> hist.inputs.in_file = 'functional.nii'
2129+
>>> hist.cmdline
2130+
'3dHist -input functional.nii -prefix functional_hist'
2131+
>>> res = hist.run() # doctest: +SKIP
2132+
2133+
"""
2134+
2135+
_cmd = '3dHist'
2136+
input_spec = HistInputSpec
2137+
output_spec = HistOutputSpec
2138+
_redirect_x = True
2139+
2140+
def __init__(self, **inputs):
2141+
super(Hist, self).__init__(**inputs)
2142+
if not no_afni():
2143+
version = Info.version()
2144+
2145+
# As of AFNI 16.0.00, redirect_x is not needed
2146+
if isinstance(version[0], int) and version[0] > 15:
2147+
self._redirect_x = False
2148+
2149+
def _parse_inputs(self, skip=None):
2150+
if not self.inputs.showhist:
2151+
if skip is None:
2152+
skip = []
2153+
skip += ['out_show']
2154+
return super(Hist, self)._parse_inputs(skip=skip)
2155+
2156+
2157+
def _list_outputs(self):
2158+
outputs = super(Hist, self)._list_outputs()
2159+
outputs['out_file'] += '.niml.hist'
2160+
if not self.inputs.showhist:
2161+
outputs['out_show'] = Undefined
2162+
return outputs
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from ....testing import assert_equal
3+
from ..preprocess import Hist
4+
5+
6+
def test_Hist_inputs():
7+
input_map = dict(args=dict(argstr='%s',
8+
),
9+
bin_width=dict(argstr='-binwidth %f',
10+
),
11+
environ=dict(nohash=True,
12+
usedefault=True,
13+
),
14+
ignore_exception=dict(nohash=True,
15+
usedefault=True,
16+
),
17+
in_file=dict(argstr='-input %s',
18+
copyfile=False,
19+
mandatory=True,
20+
position=1,
21+
),
22+
mask=dict(argstr='-mask %s',
23+
),
24+
max_value=dict(argstr='-max %f',
25+
),
26+
min_value=dict(argstr='-min %f',
27+
),
28+
nbin=dict(argstr='-nbin %d',
29+
),
30+
out_file=dict(argstr='-prefix %s',
31+
keep_extension=False,
32+
name_source=['in_file'],
33+
name_template='%s_hist',
34+
),
35+
out_show=dict(argstr='> %s',
36+
keep_extension=False,
37+
name_source='in_file',
38+
name_template='%s_hist.out',
39+
position=-1,
40+
),
41+
showhist=dict(argstr='-showhist',
42+
usedefault=True,
43+
),
44+
terminal_output=dict(nohash=True,
45+
),
46+
)
47+
inputs = Hist.input_spec()
48+
49+
for key, metadata in list(input_map.items()):
50+
for metakey, value in list(metadata.items()):
51+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
52+
53+
54+
def test_Hist_outputs():
55+
output_map = dict(out_file=dict(),
56+
out_show=dict(),
57+
)
58+
outputs = Hist.output_spec()
59+
60+
for key, metadata in list(output_map.items()):
61+
for metakey, value in list(metadata.items()):
62+
yield assert_equal, getattr(outputs.traits()[key], metakey), value

0 commit comments

Comments
 (0)