Skip to content

Commit 8207421

Browse files
committed
wip: add to_boutiques command
1 parent b2c9867 commit 8207421

File tree

3 files changed

+67
-54
lines changed

3 files changed

+67
-54
lines changed

nipype/scripts/cli.py

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
ExistingDirPath,
1010
ExistingFilePath,
1111
RegularExpression,
12-
PythonModule)
12+
PythonModule,
13+
grouper)
1314

1415

1516
# declare the CLI group
@@ -111,7 +112,7 @@ def run(ctx, module, interface, list, help):
111112
if not module_given:
112113
click.echo(ctx.command.get_help(ctx))
113114

114-
# print the list available interfaces for the given module
115+
# print the list of available interfaces for the given module
115116
elif (module_given and list) or (module_given and not interface):
116117
iface_names = list_interfaces(module)
117118
click.echo('Available Interfaces:')
@@ -120,11 +121,11 @@ def run(ctx, module, interface, list, help):
120121

121122
# check the interface
122123
elif (module_given and interface):
124+
# create the argument parser
123125
description = "Run {}".format(interface)
124126
prog = " ".join([ctx.command_path,
125127
module.__name__,
126-
interface] +
127-
ctx.args)
128+
interface] + ctx.args)
128129
iface_parser = argparse.ArgumentParser(description=description,
129130
prog=prog)
130131

@@ -141,54 +142,54 @@ def run(ctx, module, interface, list, help):
141142
run_instance(node, args)
142143

143144

144-
#
145-
# @cli.command(context_settings=CONTEXT_SETTINGS.update(dict(ignore_unknown_options=True,)))
146-
# @click.argument('-f', '--format', type=click.Choice(['boutiques']))
145+
# @cli.command(context_settings=UNKNOWN_OPTIONS)
146+
# @click.option('-f', '--format', type=click.Choice(['boutiques']),
147+
# help='Output format type.')
147148
# @click.argument('format_args', nargs=-1, type=click.UNPROCESSED)
148149
# @click.pass_context
149-
# def convert(ctx, format):
150+
# def convert(ctx, format, format_args):
150151
# """Export nipype interfaces to other formats."""
151152
# if format == 'boutiques':
153+
# ctx.params.pop('format')
154+
# ctx.params = dict(grouper(ctx.params['format_args'], 2))
152155
# ctx.forward(to_boutiques)
153-
# import pdb; pdb.set_trace()
154-
# ctx.invoke(to_boutiques, **format_args)
155-
#
156-
#
157-
# @cli.command(context_settings=CONTEXT_SETTINGS)
158-
# @click.option("-i", "--interface", type=str, required=True,
159-
# help="Name of the Nipype interface to export.")
160-
# @click.option("-m", "--module", type=PythonModule(), required=True,
161-
# help="Module where the interface is defined.")
162-
# @click.option("-o", "--output", type=str, required=True,
163-
# help="JSON file name where the Boutiques descriptor will be written.")
164-
# @click.option("-t", "--ignored-template-inputs", type=str, multiple=True,
165-
# help="Interface inputs ignored in path template creations.")
166-
# @click.option("-d", "--docker-image", type=str,
167-
# help="Name of the Docker image where the Nipype interface is available.")
168-
# @click.option("-r", "--docker-index", type=str,
169-
# help="Docker index where the Docker image is stored (e.g. http://index.docker.io).")
170-
# @click.option("-n", "--ignore-template-numbers", is_flag=True, flag_value=True,
171-
# help="Ignore all numbers in path template creations.")
172-
# @click.option("-v", "--verbose", is_flag=True, flag_value=True,
173-
# help="Enable verbose output.")
174-
# def to_boutiques(interface, module, output, ignored_template_inputs,
175-
# docker_image, docker_index, ignore_template_numbers,
176-
# verbose):
177-
# """Nipype Boutiques exporter.
178-
#
179-
# See Boutiques specification at https://github.com/boutiques/schema.
180-
# """
181-
# from nipype.utils.nipype2boutiques import generate_boutiques_descriptor
182-
#
183-
# # Generates JSON string
184-
# json_string = generate_boutiques_descriptor(module,
185-
# interface,
186-
# ignored_template_inputs,
187-
# docker_image,
188-
# docker_index,
189-
# verbose,
190-
# ignore_template_numbers)
191-
#
192-
# # Writes JSON string to file
193-
# with open(output, 'w') as f:
194-
# f.write(json_string)
156+
157+
158+
@cli.command(context_settings=CONTEXT_SETTINGS)
159+
@click.option("-i", "--interface", type=str, required=True,
160+
help="Name of the Nipype interface to export.")
161+
@click.option("-m", "--module", type=PythonModule(), required=True,
162+
help="Module where the interface is defined.")
163+
@click.option("-o", "--output", type=str, required=True,
164+
help="JSON file name where the Boutiques descriptor will be written.")
165+
@click.option("-t", "--ignored-template-inputs", type=str, multiple=True,
166+
help="Interface inputs ignored in path template creations.")
167+
@click.option("-d", "--docker-image", type=str,
168+
help="Name of the Docker image where the Nipype interface is available.")
169+
@click.option("-r", "--docker-index", type=str,
170+
help="Docker index where the Docker image is stored (e.g. http://index.docker.io).")
171+
@click.option("-n", "--ignore-template-numbers", is_flag=True, flag_value=True,
172+
help="Ignore all numbers in path template creations.")
173+
@click.option("-v", "--verbose", is_flag=True, flag_value=True,
174+
help="Enable verbose output.")
175+
def to_boutiques(interface, module, output, ignored_template_inputs,
176+
docker_image, docker_index, ignore_template_numbers,
177+
verbose):
178+
"""Nipype Boutiques exporter.
179+
180+
See Boutiques specification at https://github.com/boutiques/schema.
181+
"""
182+
from nipype.utils.nipype2boutiques import generate_boutiques_descriptor
183+
184+
# Generates JSON string
185+
json_string = generate_boutiques_descriptor(module,
186+
interface,
187+
ignored_template_inputs,
188+
docker_image,
189+
docker_index,
190+
verbose,
191+
ignore_template_numbers)
192+
193+
# Writes JSON string to file
194+
with open(output, 'w') as f:
195+
f.write(json_string)

nipype/scripts/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
from __future__ import print_function, division, unicode_literals, absolute_import
66
import re
7+
from itertools import zip_longest
78

89
import click
910

@@ -67,3 +68,10 @@ def add_args_options(arg_parser, interface):
6768
arg_parser.add_argument("--%s" % name, dest=name,
6869
help=desc, **args)
6970
return arg_parser
71+
72+
73+
def grouper(iterable, n, fillvalue=None):
74+
"Collect data into fixed-length chunks or blocks"
75+
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
76+
args = [iter(iterable)] * n
77+
return zip_longest(fillvalue=fillvalue, *args)

nipype/utils/nipype2boutiques.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ def generate_boutiques_descriptor(module, interface_name, ignored_template_input
3535
raise Exception("Undefined module.")
3636

3737
# Retrieves Nipype interface
38-
__import__(module)
39-
interface = getattr(sys.modules[module], interface_name)()
38+
if isinstance(module, str):
39+
__import__(module)
40+
module_name = str(module)
41+
module = sys.modules[module]
42+
43+
interface = getattr(module, interface_name)()
4044
inputs = interface.input_spec()
4145
outputs = interface.output_spec()
4246

4347
# Tool description
4448
tool_desc = {}
4549
tool_desc['name'] = interface_name
46-
tool_desc['command-line'] = "nipype_cmd " + str(module) + " " + interface_name + " "
47-
tool_desc['description'] = interface_name + ", as implemented in Nipype (module: " + str(module) + ", interface: " + interface_name + ")."
50+
tool_desc['command-line'] = "nipype_cmd " + module_name + " " + interface_name + " "
51+
tool_desc['description'] = interface_name + ", as implemented in Nipype (module: " + module_name + ", interface: " + interface_name + ")."
4852
tool_desc['inputs'] = []
4953
tool_desc['outputs'] = []
5054
tool_desc['tool-version'] = interface.version

0 commit comments

Comments
 (0)