Skip to content

Commit 1f5ae1c

Browse files
committed
using all as a default option for interface_name in cli; checking name of modules and available interfaces
1 parent 2a5c7f0 commit 1f5ae1c

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed
File renamed without changes.

tools/converter.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import typing as ty
99
import inspect
1010
import click
11-
import pytest
12-
11+
import warnings
1312

1413

1514
class FSLConverter:
@@ -32,8 +31,7 @@ class FSLConverter:
3231
]
3332

3433

35-
def __init__(self, interface_name,
36-
interface_spec_file=Path(os.path.dirname(__file__)) / "../specs/fsl_conv_param.yml"):
34+
def __init__(self, interface_name, interface_spec_file):
3735
self.interface_name = interface_name
3836
with interface_spec_file.open() as f:
3937
self.interface_spec = yaml.safe_load(f)[self.interface_name]
@@ -414,16 +412,53 @@ def string_formats(self, argstr, name):
414412
return argstr_new
415413

416414

415+
FSL_MODULES = ['aroma', 'dti', 'epi', 'fix', 'maths', 'model', 'possum', 'preprocess', 'utils']
417416

418417
@click.command()
419-
@click.option("-i", "--interface_name", required=True,
420-
help="name of the interface (name used in Nipype, e.g. BET)")
421-
@click.option("-m", "--module_name", required=True, help="name of the module (e.g. preprocess)")
418+
@click.option("-i", "--interface_name", required=True, default="all",
419+
help="name of the interface (name used in Nipype, e.g. BET) or all (default)"
420+
"if all is used all interfaces from the spec file will be created")
421+
@click.option("-m", "--module_name", required=True, help=f"name of the module from the list {FSL_MODULES}")
422422
def create_pydra_spec(interface_name, module_name):
423-
converter = FSLConverter(interface_name=interface_name)
423+
if module_name not in FSL_MODULES:
424+
raise Exception(f"module name {module_name} not available;"
425+
f"should be from the list {FSL_MODULES}")
426+
427+
spec_file = Path(os.path.dirname(__file__)) / f"../specs/fsl_{module_name}_param.yml"
428+
if not spec_file.exists():
429+
raise Exception(f"the specification file doesn't exist for the module {module_name},"
430+
f"create the specification file in {spec_file.parent}")
431+
432+
def all_interfaces(module):
433+
nipype_module = getattr(fsl, module)
434+
all_specs = [el for el in dir(nipype_module) if "InputSpec" in el]
435+
all_interf = [el.replace("InputSpec", "") for el in all_specs]
436+
437+
# interfaces in the spec file
438+
with open(spec_file) as f:
439+
spec_interf = yaml.safe_load(f).keys()
440+
441+
if set(all_interf) - set(spec_interf):
442+
warnings.warn(f"some interfaces are not in the spec file: "
443+
f"{set(all_interf) - set(spec_interf)}, "
444+
f"and pydra interfaces will not be created for them")
445+
return spec_interf
446+
447+
if interface_name == "all":
448+
interface_list = all_interfaces(module_name)
449+
elif interface_name in all_interfaces(module_name):
450+
interface_list = [interface_name]
451+
else:
452+
raise Exception(f"interface_name has to be 'all' "
453+
f"or a name from the list {all_interfaces(module_name)}")
454+
424455
dirname_interf = Path(__file__).parent.parent / f"pydra/tasks/fsl/{module_name}"
425456
dirname_interf.mkdir(exist_ok=True)
426-
converter.pydra_specs(write=True, dirname=dirname_interf)
457+
458+
for interface_el in interface_list:
459+
converter = FSLConverter(interface_name=interface_el,
460+
interface_spec_file=Path(os.path.dirname(__file__)) / "../specs/fsl_preprocess_param.yml")
461+
converter.pydra_specs(write=True, dirname=dirname_interf)
427462

428463
if __name__ == '__main__':
429464
create_pydra_spec()

0 commit comments

Comments
 (0)