Skip to content

Commit 955e6c8

Browse files
committed
debugged workflows and spatial normalisation monster interface
1 parent 0cac4cd commit 955e6c8

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

nipype2pydra/interface/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
parse_imports,
4646
ExplicitImport,
4747
from_list_to_imports,
48+
make_imports_absolute,
4849
)
4950
from fileformats.generic import File
5051
import nipype2pydra.package
@@ -1319,6 +1320,11 @@ def process_method_body(
13191320
)
13201321
method_body = output_re.sub(r"\1", method_body)
13211322
method_body = self.unwrap_nested_methods(method_body)
1323+
method_body = make_imports_absolute(
1324+
method_body,
1325+
super_base.__module__,
1326+
translations=self.package.all_import_translations,
1327+
)
13221328
# method_body = self._misc_cleanups(method_body)
13231329
return method_body
13241330

nipype2pydra/interface/function.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
@attrs.define(slots=False)
1717
class FunctionInterfaceConverter(BaseInterfaceConverter):
1818

19+
converter_type = "function"
20+
1921
@property
2022
def included_methods(self) -> ty.Tuple[str, ...]:
2123
return ("__init__", "_run_interface", "_list_outputs")
@@ -65,7 +67,7 @@ def types_to_names(spec_fields):
6567
used = UsedSymbols.find(
6668
self.nipype_module,
6769
self.referenced_local_functions,
68-
omit_classes=self.package.omit_classes, # + [BaseInterface, TraitedSpec],
70+
omit_classes=self.package.omit_classes,
6971
omit_modules=self.package.omit_modules,
7072
omit_functions=self.package.omit_functions,
7173
omit_constants=self.package.omit_constants,
@@ -81,7 +83,7 @@ def types_to_names(spec_fields):
8183
method_used = UsedSymbols.find(
8284
method_module,
8385
[ref_method],
84-
omit_classes=self.package.omit_classes, # + [BaseInterface, TraitedSpec],
86+
omit_classes=self.package.omit_classes,
8587
omit_modules=self.package.omit_modules,
8688
omit_functions=self.package.omit_functions,
8789
omit_constants=self.package.omit_constants,
@@ -153,7 +155,7 @@ def types_to_names(spec_fields):
153155
run_interface_used = UsedSymbols.find(
154156
run_interface_class.__module__,
155157
[run_interface_code],
156-
omit_classes=self.package.omit_classes, # + [BaseInterface, TraitedSpec],
158+
omit_classes=self.package.omit_classes,
157159
omit_modules=self.package.omit_modules,
158160
omit_functions=self.package.omit_functions,
159161
omit_constants=self.package.omit_constants,
@@ -187,7 +189,7 @@ def types_to_names(spec_fields):
187189
list_outputs_used = UsedSymbols.find(
188190
list_outputs_class.__module__,
189191
[list_outputs_code],
190-
omit_classes=self.package.omit_classes, # + [BaseInterface, TraitedSpec],
192+
omit_classes=self.package.omit_classes,
191193
omit_modules=self.package.omit_modules,
192194
omit_functions=self.package.omit_functions,
193195
omit_constants=self.package.omit_constants,
@@ -231,7 +233,14 @@ def types_to_names(spec_fields):
231233
spec_str += "\n\n# Nipype methods converted into functions\n\n"
232234

233235
for m in sorted(self.referenced_methods, key=attrgetter("__name__")):
234-
spec_str += "\n\n" + self.process_method(m, input_names, output_names)
236+
spec_str += "\n\n" + self.process_method(
237+
m,
238+
input_names,
239+
output_names,
240+
super_base=find_super_method(
241+
self.nipype_interface, m.__name__, include_class=True
242+
)[1],
243+
)
235244

236245
# Replace runtime attributes
237246
additional_imports = set()

nipype2pydra/statements/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
GENERIC_PYDRA_IMPORTS,
66
ExplicitImport,
77
from_list_to_imports,
8+
make_imports_absolute,
89
)
910
from .workflow_build import ( # noqa: F401
1011
AddNestedWorkflowStatement,

nipype2pydra/statements/imports.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,39 @@ def collate(
465465
)
466466

467467

468+
def translate(
469+
module_name: str, translations: ty.Sequence[ty.Tuple[str, str]]
470+
) -> ty.Optional[str]:
471+
for from_pkg, to_pkg in translations:
472+
if re.match(from_pkg, module_name):
473+
return re.sub(
474+
from_pkg,
475+
to_pkg,
476+
module_name,
477+
count=1,
478+
flags=re.MULTILINE | re.DOTALL,
479+
)
480+
return None
481+
482+
483+
def make_imports_absolute(
484+
src: str, modulepath: str, translations: ty.Sequence[ty.Tuple[str, str]] = ()
485+
) -> str:
486+
parts = modulepath.split(".")
487+
488+
def replacer(match) -> str:
489+
levels = len(match.group(2))
490+
assert levels < len(parts)
491+
abs_modulepath = ".".join(parts[:-levels]) + "." + match.group(3)
492+
if translations:
493+
newpath = translate(abs_modulepath, translations)
494+
if newpath:
495+
abs_modulepath = newpath
496+
return f"{match.group(1)}from {abs_modulepath}"
497+
498+
return re.sub(r"(\s*)from\s+(\.+)(\w+)", replacer, src)
499+
500+
468501
def parse_imports(
469502
stmts: ty.Union[str, ty.Sequence[str]],
470503
relative_to: ty.Union[str, ModuleType, None] = None,
@@ -497,18 +530,6 @@ def parse_imports(
497530
".__init__" if relative_to.__file__.endswith("__init__.py") else ""
498531
)
499532

500-
def translate(module_name: str) -> ty.Optional[str]:
501-
for from_pkg, to_pkg in translations:
502-
if re.match(from_pkg, module_name):
503-
return re.sub(
504-
from_pkg,
505-
to_pkg,
506-
module_name,
507-
count=1,
508-
flags=re.MULTILINE | re.DOTALL,
509-
)
510-
return None
511-
512533
parsed = []
513534
for stmt in stmts:
514535
if isinstance(stmt, ImportStatement):
@@ -543,7 +564,7 @@ def translate(module_name: str) -> ty.Optional[str]:
543564
)
544565
if absolute:
545566
import_stmt = import_stmt.absolute()
546-
import_stmt.translation = translate(import_stmt.module_name)
567+
import_stmt.translation = translate(import_stmt.module_name, translations)
547568
parsed.append(import_stmt)
548569

549570
else:
@@ -554,7 +575,7 @@ def translate(module_name: str) -> ty.Optional[str]:
554575
ImportStatement(
555576
indent=match.group(1),
556577
imported={imp.local_name: imp},
557-
translation=translate(imp.name),
578+
translation=translate(imp.name, translations),
558579
)
559580
)
560581
return parsed

0 commit comments

Comments
 (0)