Skip to content

Commit dab69db

Browse files
committed
debugging task converter
1 parent 60b61cf commit dab69db

12 files changed

+395
-423
lines changed

conftest.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import os
22
from pathlib import Path
3+
import traceback
4+
import tempfile
35
import pytest
46
from click.testing import CliRunner
57

68

7-
PKG_DIR = Path(__file__).parent
9+
@pytest.fixture(scope="session")
10+
def pkg_dir():
11+
return Path(__file__).parent
812

913

1014
@pytest.fixture(scope="session")
11-
def pkg_dir():
12-
return PKG_DIR
15+
def example_specs_dir(pkg_dir):
16+
return pkg_dir / "example-specs"
17+
18+
19+
@pytest.fixture
20+
def work_dir():
21+
work_dir = tempfile.mkdtemp()
22+
return Path(work_dir)
1323

1424

1525
@pytest.fixture
@@ -42,3 +52,8 @@ def pytest_internalerror(excinfo):
4252
@pytest.fixture
4353
def catch_cli_exceptions():
4454
return CATCH_CLI_EXCEPTIONS
55+
56+
57+
def show_cli_trace(result):
58+
"Used in testing to show traceback of CLI output"
59+
return "".join(traceback.format_exception(*result.exc_info))

example-specs/ants_registration.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
task_name: Registration
2+
nipype_module: nipype.interfaces.ants.registration
3+
output_requirements:
4+
output_warped_image: ["fixed_image", "moving_image", "output_transform_prefix"]
5+
output_templates:
6+
output_warped_image: "{output_transform_prefix}warped"
7+
doctest:
8+
fixed_image: test.nii.gz
9+
moving_image: test.nii.gz
10+
cmdline: >-
11+
antsRegistration --output [ output_, output_warped_image.nii.gz ]
12+
--metric Mattes[ test.nii, test.nii, 1, 32, Random, 0.05 ]
13+
tests_inputs: []
14+
tests_outputs:
15+
- AttributeError
16+

example-specs/ants_registration_param.yml

Lines changed: 0 additions & 43 deletions
This file was deleted.

example-specs/ants_segmentation.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module: nipype.interfaces.ants.segmentation
2+
tasks:
3+
N4BiasFieldCorrection:
4+
output_requirements:
5+
output_image: []
6+
bias_image: ["save_bias"]
7+
output_templates:
8+
output_image: ""
9+
doctest:
10+
input_image: test.nii.gz
11+
cmdline:
12+
tests_inputs: []
13+
tests_outputs:
14+
- AttributeError

example-specs/ants_segmentation_param.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module: nipype.interfaces..freesurfer.preprocess
2+
tasks:
3+
ApplyVolTransform:
4+
output_requirements: []
5+
output_templates:
6+
transformed_file: "{source_file}_warped"
7+
doctest:
8+
source_file: test.nii.gz
9+
cmdline: mri_vol2vol
10+
tests_inputs: []
11+
tests_outputs: []

example-specs/freesurfer_preprocess_param.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

example-specs/smriprep.yaml

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1-
function: smriprep.workflows.base:init_single_subject_wf
2-
splits:
3-
- func_name: registration
4-
first_node: ds_surfs
5-
- func_name: segmentation
6-
first_node: lta2itk_fwd
7-
ignore_tasks:
8-
- smriprep.interfaces.DerivativesDataSink
9-
- nipype.interfaces.utility.base.IdentityInterface
10-
args:
11-
debug: false
12-
freesurfer: true
13-
fast_track: false
14-
hires: true
15-
layout:
16-
type: bids:BIDSLayout
17-
args:
18-
root: test-data/bids-data/ds000113
19-
longitudinal: false
20-
low_mem: false
21-
name: single_subject_wf
22-
omp_nthreads: 1
23-
output_dir: .
24-
skull_strip_fixed_seed: false
25-
skull_strip_mode: force
26-
skull_strip_template:
27-
type: niworkflows.utils.spaces:Reference
28-
args:
29-
space: OASIS30ANTs
30-
spaces:
31-
type: niworkflows.utils.spaces:SpatialReferences
1+
module: smriprep.workflows.base
2+
workflows:
3+
init_single_subject_wf:
324
args:
5+
debug: false
6+
freesurfer: true
7+
fast_track: false
8+
hires: true
9+
layout:
10+
type: bids:BIDSLayout
11+
args:
12+
root: test-data/bids-data/ds000113
13+
longitudinal: false
14+
low_mem: false
15+
name: single_subject_wf
16+
omp_nthreads: 1
17+
output_dir: .
18+
skull_strip_fixed_seed: false
19+
skull_strip_mode: force
20+
skull_strip_template:
21+
type: niworkflows.utils.spaces:Reference
22+
args:
23+
space: OASIS30ANTs
3324
spaces:
34-
- MNI152NLin2009cAsym
35-
- fsaverage5
36-
subject_id: test
37-
bids_filters: null
25+
type: niworkflows.utils.spaces:SpatialReferences
26+
args:
27+
spaces:
28+
- MNI152NLin2009cAsym
29+
- fsaverage5
30+
subject_id: test
31+
bids_filters: null
32+
splits:
33+
- func_name: registration
34+
first_node: ds_surfs
35+
- func_name: segmentation
36+
first_node: lta2itk_fwd
37+
ignore_tasks:
38+
- smriprep.interfaces.DerivativesDataSink
39+
- nipype.interfaces.utility.base.IdentityInterface
40+

nipype2pydra/cli.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
import click
23
import yaml
34
from nipype2pydra import __version__
@@ -15,50 +16,36 @@ def cli():
1516
@cli.command(
1617
help="""Port Nipype task interface code to Pydra
1718
18-
SPEC_FILE is a YAML file which defines the manually specified aspects of the conversion
19-
see https://github.com/nipype/nipype2pydra/tree/main/example-specs for examples
19+
YAML_SPEC is a YAML file which defines interfaces to be imported along with an
20+
manually specified aspects of the conversion see
21+
https://github.com/nipype/nipype2pydra/tree/main/example-specs for examples
2022
21-
OUT_FILE is where the converted code will be generated
23+
OUTPUT_BASE_DIR is the path of generated module file
2224
"""
2325
)
24-
@click.argument("spec-file", type=click.File())
25-
@click.argument("out-file", type=click.File(mode="w"))
26+
@click.argument("yaml-spec", type=click.File())
27+
@click.argument("output-file", type=Path)
2628
@click.option(
2729
"-c",
2830
"--callables",
2931
type=click.File(),
3032
default=None,
3133
help="a Python file containing callable functions required in the command interface",
3234
)
33-
@click.option(
34-
"-i",
35-
"--interface_name",
36-
multiple=True,
37-
default=[],
38-
help=(
39-
"name of the interfaces (name used in Nipype, e.g. BET) to be converted. If not"
40-
"provided all interfaces defined in the spec are converted"
41-
),
42-
)
43-
def task(spec_file, out_file, interface_name, callables):
44-
45-
spec = yaml.safe_load(spec_file)
35+
def task(yaml_spec, output_file, callables):
4636

47-
if interface_name:
48-
spec = {n: v for n, v in spec.items() if n in interface_name}
37+
spec = yaml.safe_load(yaml_spec)
4938

50-
converter = TaskConverter(spec, callables)
51-
code = converter.generate()
52-
out_file.write(code)
39+
converter = TaskConverter(callables_module=callables, **spec)
40+
converter.generate(output_file)
5341

5442

5543
@cli.command(help="Port Nipype workflow creation functions to Pydra")
56-
@click.argument("spec-file", type=click.File())
57-
@click.argument("out-file", type=click.File(mode="w"))
58-
def workflow(spec_file, out_file):
44+
@click.argument("yaml-spec", type=click.File())
45+
@click.argument("output-module-file", type=click.File(mode="w"))
46+
def workflow(yaml_spec, output_module_file):
5947

60-
spec = yaml.safe_load(spec_file)
48+
spec = yaml.safe_load(yaml_spec)
6149

6250
converter = WorkflowConverter(spec)
63-
code = converter.generate()
64-
out_file.write(code)
51+
converter.generate(output_module_file)

0 commit comments

Comments
 (0)