Skip to content

Commit cf30734

Browse files
committed
adding more tests in docstring for seg_maths and seg_stats
1 parent 3389a28 commit cf30734

File tree

3 files changed

+186
-46
lines changed

3 files changed

+186
-46
lines changed

nipype/interfaces/niftyseg/maths.py

Lines changed: 140 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,41 @@ class UnaryMaths(MathsCommand):
139139
140140
Examples
141141
--------
142+
>>> import copy
142143
>>> from nipype.interfaces import niftyseg
143-
>>> node = niftyseg.UnaryMaths()
144-
>>> node.inputs.in_file = 'im1.nii'
145-
>>> node.inputs.operation = 'sqrt'
146-
>>> node.inputs.output_datatype = 'float'
147-
>>> node.cmdline # doctest: +ALLOW_UNICODE
144+
>>> unary = niftyseg.UnaryMaths()
145+
>>> unary.inputs.output_datatype = 'float'
146+
>>> unary.inputs.in_file = 'im1.nii'
147+
>>> # Test sqrt operation
148+
>>> unary_sqrt = copy.deepcopy(unary)
149+
>>> unary_sqrt.inputs.operation = 'sqrt'
150+
>>> unary_sqrt.cmdline # doctest: +ALLOW_UNICODE
148151
'seg_maths im1.nii -sqrt -odt float im1_sqrt.nii'
152+
>>> unary_sqrt.run() # doctest: +SKIP
153+
>>> # Test sqrt operation
154+
>>> unary_abs = copy.deepcopy(unary)
155+
>>> unary_abs.inputs.operation = 'abs'
156+
>>> unary_abs.cmdline # doctest: +ALLOW_UNICODE
157+
'seg_maths im1.nii -abs -odt float im1_abs.nii'
158+
>>> unary_abs.run() # doctest: +SKIP
159+
>>> # Test bin operation
160+
>>> unary_bin = copy.deepcopy(unary)
161+
>>> unary_bin.inputs.operation = 'bin'
162+
>>> unary_bin.cmdline # doctest: +ALLOW_UNICODE
163+
'seg_maths im1.nii -bin -odt float im1_bin.nii'
164+
>>> unary_bin.run() # doctest: +SKIP
165+
>>> # Test otsu operation
166+
>>> unary_otsu = copy.deepcopy(unary)
167+
>>> unary_otsu.inputs.operation = 'otsu'
168+
>>> unary_otsu.cmdline # doctest: +ALLOW_UNICODE
169+
'seg_maths im1.nii -otsu -odt float im1_otsu.nii'
170+
>>> unary_otsu.run() # doctest: +SKIP
171+
>>> # Test isnan operation
172+
>>> unary_isnan = copy.deepcopy(unary)
173+
>>> unary_isnan.inputs.operation = 'isnan'
174+
>>> unary_isnan.cmdline # doctest: +ALLOW_UNICODE
175+
'seg_maths im1.nii -isnan -odt float im1_isnan.nii'
176+
>>> unary_isnan.run() # doctest: +SKIP
149177
150178
"""
151179
input_spec = UnaryMathsInput
@@ -226,28 +254,71 @@ class BinaryMaths(MathsCommand):
226254
227255
Examples
228256
--------
257+
>>> import copy
229258
>>> from nipype.interfaces import niftyseg
230-
>>> node = niftyseg.BinaryMaths()
231-
>>> node.inputs.in_file = 'im1.nii'
232-
>>> node.inputs.operation = 'sub'
233-
>>> node.inputs.operand_file = 'im2.nii'
234-
>>> node.inputs.output_datatype = 'float'
235-
>>> node.cmdline # doctest: +ALLOW_UNICODE
259+
>>> binary = niftyseg.BinaryMaths()
260+
>>> binary.inputs.in_file = 'im1.nii'
261+
>>> binary.inputs.output_datatype = 'float'
262+
>>> # Test sub operation
263+
>>> binary_sub = copy.deepcopy(binary)
264+
>>> binary_sub.inputs.operation = 'sub'
265+
>>> binary_sub.inputs.operand_file = 'im2.nii'
266+
>>> binary_sub.cmdline # doctest: +ALLOW_UNICODE
236267
'seg_maths im1.nii -sub im2.nii -odt float im1_sub.nii'
268+
>>> binary_sub.run() # doctest: +SKIP
269+
>>> # Test mul operation
270+
>>> binary_mul = copy.deepcopy(binary)
271+
>>> binary_mul.inputs.operation = 'mul'
272+
>>> binary_mul.inputs.operand_value = 2.0
273+
>>> binary_mul.cmdline # doctest: +ALLOW_UNICODE
274+
'seg_maths im1.nii -mul 2.0 -odt float im1_mul.nii'
275+
>>> binary_mul.run() # doctest: +SKIP
276+
>>> # Test llsnorm operation
277+
>>> binary_llsnorm = copy.deepcopy(binary)
278+
>>> binary_llsnorm.inputs.operation = 'llsnorm'
279+
>>> binary_llsnorm.inputs.operand_file = 'im2.nii'
280+
>>> binary_llsnorm.cmdline # doctest: +ALLOW_UNICODE
281+
'seg_maths im1.nii -llsnorm im2.nii -odt float im1_llsnorm.nii'
282+
>>> binary_llsnorm.run() # doctest: +SKIP
283+
>>> # Test splitinter operation
284+
>>> binary_splitinter = copy.deepcopy(binary)
285+
>>> binary_splitinter.inputs.operation = 'llsnorm'
286+
>>> binary_splitinter.inputs.operand_str = 'z'
287+
>>> binary_splitinter.cmdline # doctest: +ALLOW_UNICODE
288+
'seg_maths im1.nii -splitinter z -odt float im1_splitinter.nii'
289+
>>> binary_splitinter.run() # doctest: +SKIP
237290
238291
"""
239292
input_spec = BinaryMathsInput
240293

241294
def _format_arg(self, opt, spec, val):
242295
"""Convert input to appropriate format for seg_maths."""
243-
if opt == 'operand_value' and float(val) == 0.0:
244-
return '0'
245-
246296
if opt == 'operand_str' and self.inputs.operation != 'splitinter':
247297
err = 'operand_str set but with an operation different than \
248298
"splitinter"'
249299
raise NipypeInterfaceError(err)
250300

301+
if opt == 'operation':
302+
# Only float
303+
if val in ['pow', 'thr', 'uthr', 'smo', 'edge', 'sobel3', 'sobel5',
304+
'smol']:
305+
if not isdefined(self.inputs.operand_value):
306+
err = 'operand_value not set for {0}.'.format(val)
307+
raise NipypeInterfaceError(err)
308+
# only files
309+
elif val in ['min', 'llsnorm', 'masknan', 'hdr_copy']:
310+
if not isdefined(self.inputs.operand_file):
311+
err = 'operand_file not set for {0}.'.format(val)
312+
raise NipypeInterfaceError(err)
313+
# splitinter:
314+
elif val == 'splitinter':
315+
if not isdefined(self.inputs.operand_str):
316+
err = 'operand_str not set for splitinter.'
317+
raise NipypeInterfaceError(err)
318+
319+
if opt == 'operand_value' and float(val) == 0.0:
320+
return '0'
321+
251322
return super(BinaryMaths, self)._format_arg(opt, spec, val)
252323

253324
def _overload_extension(self, value, name=None):
@@ -293,22 +364,40 @@ class BinaryMathsInteger(MathsCommand):
293364
294365
Examples
295366
--------
367+
>>> import copy
296368
>>> from nipype.interfaces.niftyseg import BinaryMathsInteger
297-
>>> node = BinaryMathsInteger()
298-
>>> node.inputs.in_file = 'im1.nii'
299-
>>> node.inputs.operation = 'dil'
300-
>>> node.inputs.operand_value = 2
301-
>>> node.inputs.output_datatype = 'float'
302-
>>> node.cmdline # doctest: +ALLOW_UNICODE
369+
>>> binaryi = BinaryMathsInteger()
370+
>>> binaryi.inputs.in_file = 'im1.nii'
371+
>>> binaryi.inputs.output_datatype = 'float'
372+
>>> # Test dil operation
373+
>>> binaryi_dil = copy.deepcopy(binaryi)
374+
>>> binaryi_dil.inputs.operation = 'dil'
375+
>>> binaryi_dil.inputs.operand_value = 2
376+
>>> binaryi_dil.cmdline # doctest: +ALLOW_UNICODE
303377
'seg_maths im1.nii -dil 2 -odt float im1_dil.nii'
378+
>>> binaryi_dil.run() # doctest: +SKIP
379+
>>> # Test dil operation
380+
>>> binaryi_ero = copy.deepcopy(binaryi)
381+
>>> binaryi_ero.inputs.operation = 'ero'
382+
>>> binaryi_ero.inputs.operand_value = 1
383+
>>> binaryi_ero.cmdline # doctest: +ALLOW_UNICODE
384+
'seg_maths im1.nii -ero 1 -odt float im1_ero.nii'
385+
>>> binaryi_ero.run() # doctest: +SKIP
386+
>>> # Test pad operation
387+
>>> binaryi_pad = copy.deepcopy(binaryi)
388+
>>> binaryi_pad.inputs.operation = 'pad'
389+
>>> binaryi_pad.inputs.operand_value = 4
390+
>>> binaryi_pad.cmdline # doctest: +ALLOW_UNICODE
391+
'seg_maths im1.nii -pad 4 -odt float im1_pad.nii'
392+
>>> binaryi_pad.run() # doctest: +SKIP
304393
305394
"""
306395
input_spec = BinaryMathsInputInteger
307396

308397

309398
class TupleMathsInput(MathsInput):
310399
"""Input Spec for seg_maths Tuple operations."""
311-
operation = traits.Enum('lncc', 'lssd', 'lltsnorm', 'qlsnorm',
400+
operation = traits.Enum('lncc', 'lssd', 'lltsnorm',
312401
mandatory=True,
313402
argstr='-%s',
314403
position=4,
@@ -354,25 +443,45 @@ class TupleMaths(MathsCommand):
354443
on a kernel with <std>
355444
-lltsnorm <file_norm> <float> Linear LTS normalisation assuming
356445
<float> percent outliers
357-
-qlsnorm <order> <file_norm> LS normalisation of <order>
358-
between current and <file_norm>
359446
360447
For source code, see http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg
361448
For Documentation, see:
362449
http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg_documentation
363450
364451
Examples
365452
--------
453+
>>> import copy
366454
>>> from nipype.interfaces import niftyseg
367-
>>> node = niftyseg.TupleMaths()
368-
>>> node.inputs.in_file = 'im1.nii'
369-
>>> node.inputs.operation = 'lncc'
370-
>>> node.inputs.operand_file1 = 'im2.nii'
371-
>>> node.inputs.operand_value2 = 2.0
372-
>>> node.inputs.output_datatype = 'float'
373-
>>> node.cmdline # doctest: +ALLOW_UNICODE
455+
>>> tuple = niftyseg.TupleMaths()
456+
>>> tuple.inputs.in_file = 'im1.nii'
457+
>>> tuple.inputs.output_datatype = 'float'
458+
459+
>>> # Test lncc operation
460+
>>> tuple_lncc = copy.deepcopy(binary)
461+
>>> tuple_lncc.inputs.operation = 'lncc'
462+
>>> tuple_lncc.inputs.operand_file1 = 'im2.nii'
463+
>>> tuple_lncc.inputs.operand_value2 = 2.0
464+
>>> tuple_lncc.cmdline # doctest: +ALLOW_UNICODE
374465
'seg_maths im1.nii -lncc im2.nii 2.00000000 -odt float im1_lncc.nii'
375-
466+
>>> tuple_lncc.run() # doctest: +SKIP
467+
468+
>>> # Test lssd operation
469+
>>> tuple_lssd = copy.deepcopy(binary)
470+
>>> tuple_lssd.inputs.operation = 'lssd'
471+
>>> tuple_lssd.inputs.operand_file1 = 'im2.nii'
472+
>>> tuple_lssd.inputs.operand_value2 = 1.0
473+
>>> tuple_lssd.cmdline # doctest: +ALLOW_UNICODE
474+
'seg_maths im1.nii -lssd im2.nii 1.00000000 -odt float im1_lssd.nii'
475+
>>> tuple_lssd.run() # doctest: +SKIP
476+
477+
>>> # Test lltsnorm operation
478+
>>> tuple_lltsnorm = copy.deepcopy(binary)
479+
>>> tuple_lltsnorm.inputs.operation = 'lltsnorm'
480+
>>> tuple_lltsnorm.inputs.operand_file1 = 'im2.nii'
481+
>>> tuple_lltsnorm.inputs.operand_value2 = 0.01
482+
>>> tuple_lltsnorm.cmdline # doctest: +ALLOW_UNICODE
483+
'seg_maths im1.nii -lltsnorm im2.nii 0.01 -odt float im1_lltsnorm.nii'
484+
>>> tuple_lltsnorm.run() # doctest: +SKIP
376485
"""
377486
input_spec = TupleMathsInput
378487

nipype/interfaces/niftyseg/stats.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,34 @@ class UnaryStats(StatsCommand):
130130
-xdim | Output the voxel dimention in the x direction.
131131
| Replace x with y/z for other directions.
132132
133-
Note: All NaN or Inf are ignored for all stats.
134-
The -m and -t options can be used in conjusction.
135-
136133
For source code, see http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg
137134
For Documentation, see:
138135
http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg_documentation
139136
140137
Examples
141138
--------
139+
>>> import copy
142140
>>> from nipype.interfaces import niftyseg
143-
>>> node = niftyseg.UnaryStats()
144-
>>> node.inputs.in_file = 'im1.nii'
145-
>>> node.inputs.operation = 'v'
146-
>>> node.cmdline # doctest: +ALLOW_UNICODE
141+
>>> unary = niftyseg.UnaryStats()
142+
>>> unary.inputs.in_file = 'im1.nii'
143+
>>> # Test v operation
144+
>>> unary_v = copy.deepcopy(unary)
145+
>>> unary_v.inputs.operation = 'v'
146+
>>> unary_v.cmdline # doctest: +ALLOW_UNICODE
147147
'seg_stats im1.nii -v'
148+
>>> unary_v.run() # doctest: +SKIP
149+
>>> # Test vl operation
150+
>>> unary_vl = copy.deepcopy(unary)
151+
>>> unary_vl.inputs.operation = 'vl'
152+
>>> unary_vl.cmdline # doctest: +ALLOW_UNICODE
153+
'seg_stats im1.nii -vl'
154+
>>> unary_vl.run() # doctest: +SKIP
155+
>>> # Test x operation
156+
>>> unary_x = copy.deepcopy(unary)
157+
>>> unary_x.inputs.operation = 'x'
158+
>>> unary_x.cmdline # doctest: +ALLOW_UNICODE
159+
'seg_stats im1.nii -x'
160+
>>> unary_x.run() # doctest: +SKIP
148161
149162
"""
150163
input_spec = UnaryStatsInput
@@ -193,22 +206,40 @@ class BinaryStats(StatsCommand):
193206
-Vl <csv> | Volume of each integer label <in>. Save to <csv>file.
194207
-Nl <csv> | Count of each label <in>. Save to <csv> file.
195208
196-
Note: All NaN or Inf are ignored for all stats.
197-
The -m and -t options can be used in conjusction.
198-
199209
For source code, see http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg
200210
For Documentation, see:
201211
http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg_documentation
202212
203213
Examples
204214
--------
215+
>>> import copy
205216
>>> from nipype.interfaces import niftyseg
206-
>>> node = niftyseg.BinaryStats()
207-
>>> node.inputs.in_file = 'im1.nii'
208-
>>> node.inputs.operation = 'sa'
209-
>>> node.inputs.operand_value = 2.0
210-
>>> node.cmdline # doctest: +ALLOW_UNICODE
217+
>>> binary = niftyseg.BinaryStats()
218+
>>> binary.inputs.in_file = 'im1.nii'
219+
>>> # Test sa operation
220+
>>> binary_sa = copy.deepcopy(binary)
221+
>>> binary_sa = niftyseg.BinaryStats()
222+
>>> binary_sa.inputs.operation = 'sa'
223+
>>> binary_sa.inputs.operand_value = 2.0
224+
>>> binary_sa.cmdline # doctest: +ALLOW_UNICODE
211225
'seg_stats im1.nii -sa 2.00000000'
226+
>>> binary_sa.run() # doctest: +SKIP
227+
>>> # Test ncc operation
228+
>>> binary_ncc = copy.deepcopy(binary)
229+
>>> binary_ncc = niftyseg.BinaryStats()
230+
>>> binary_ncc.inputs.operation = 'ncc'
231+
>>> binary_ncc.inputs.operand_file = 'im2.nii'
232+
>>> binary_ncc.cmdline # doctest: +ALLOW_UNICODE
233+
'seg_stats im1.nii -ncc im2.nii'
234+
>>> binary_ncc.run() # doctest: +SKIP
235+
>>> # Test Nl operation
236+
>>> binary_nl = copy.deepcopy(binary)
237+
>>> binary_nl = niftyseg.BinaryStats()
238+
>>> binary_nl.inputs.operation = 'Nl'
239+
>>> binary_nl.inputs.operand_file = 'output.csv'
240+
>>> binary_nl.cmdline # doctest: +ALLOW_UNICODE
241+
'seg_stats im1.nii -Nl output.csv'
242+
>>> binary_nl.run() # doctest: +SKIP
212243
213244
"""
214245
input_spec = BinaryStatsInput

nipype/testing/data/output.csv

Whitespace-only changes.

0 commit comments

Comments
 (0)