Skip to content

added support for boolean flags in nipype_cmd #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions nipype/utils/nipype_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import argparse
import inspect
import sys
from nipype.interfaces.base import Interface, InputMultiPath
from nipype.interfaces.base import Interface, InputMultiPath, traits
from nipype.utils.misc import str2bool

def listClasses(module=None):
Expand All @@ -24,6 +24,9 @@ def add_options(parser=None, module=None, function=None):
for name, spec in sorted(interface.inputs.traits(transient=None).items()):
desc = "\n".join(interface._get_trait_desc(inputs, name, spec))[len(name)+2:]
args = {}

if spec.is_trait_type(traits.Bool):
args["action"] = 'store_true'

if hasattr(spec, "mandatory") and spec.mandatory:
if spec.is_trait_type(InputMultiPath):
Expand All @@ -43,16 +46,17 @@ def run_instance(interface, options):
for input_name, _ in interface.inputs.items():
if getattr(options, input_name) != None:
value = getattr(options, input_name)
#traits cannot cast from string to float or int
try:
value = float(value)
except:
pass
#try to cast string input to boolean
try:
value = str2bool(value)
except:
pass
if not isinstance(value, bool):
#traits cannot cast from string to float or int
try:
value = float(value)
except:
pass
#try to cast string input to boolean
try:
value = str2bool(value)
except:
pass
try:
setattr(interface.inputs, input_name,
value)
Expand Down
4 changes: 2 additions & 2 deletions nipype/utils/tests/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_run_4d_realign_without_arguments(self):
self.assertEqual(stderr.getvalue(),
"""usage: nipype_cmd nipype.interfaces.nipy FmriRealign4d [-h]
[--between_loops [BETWEEN_LOOPS [BETWEEN_LOOPS ...]]]
[--ignore_exception IGNORE_EXCEPTION]
[--ignore_exception]
[--loops [LOOPS [LOOPS ...]]]
[--slice_order SLICE_ORDER]
[--speedup [SPEEDUP [SPEEDUP ...]]]
Expand All @@ -113,4 +113,4 @@ def test_run_4d_realign_help(self):
self.assertTrue("Run FmriRealign4d" in stdout.getvalue())

if __name__ == '__main__':
unittest.main()
unittest.main()