Skip to content

Commit fa2b5c9

Browse files
authored
Revert "fix: python 2 Function interfaces recompatibility"
1 parent cf9e64e commit fa2b5c9

File tree

8 files changed

+71
-98
lines changed

8 files changed

+71
-98
lines changed

doc/users/saving_workflows.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ This will create a file "outputtestsave.py" with the following content:
5555
from nipype.pipeline.engine import Workflow, Node, MapNode
5656
from nipype.interfaces.utility import IdentityInterface
5757
from nipype.interfaces.utility import Function
58-
from nipype.utils.functions import getsource
58+
from nipype.utils.misc import getsource
5959
from nipype.interfaces.fsl.preprocess import BET
6060
from nipype.interfaces.fsl.utils import ImageMaths
6161
# Functions

nipype/interfaces/utility/wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
BaseInterfaceInputSpec, get_max_resources_used)
2525
from ..io import IOBase, add_traits
2626
from ...utils.filemanip import filename_to_list
27-
from ...utils.functions import getsource, create_function_from_source
27+
from ...utils.misc import getsource, create_function_from_source
2828

2929
logger = logging.getLogger('interface')
3030
if runtime_profile:

nipype/pipeline/engine/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131

3232
from ...utils.filemanip import (fname_presuffix, FileNotFoundError, to_str,
3333
filename_to_list, get_related_files)
34-
from ...utils.misc import str2bool
35-
from ...utils.functions import create_function_from_source
34+
from ...utils.misc import create_function_from_source, str2bool
3635
from ...interfaces.base import (CommandLine, isdefined, Undefined,
3736
InterfaceResult)
3837
from ...interfaces.utility import IdentityInterface
@@ -101,7 +100,7 @@ def _write_inputs(node):
101100
lines[-1] = lines[-1].replace(' %s(' % funcname,
102101
' %s_1(' % funcname)
103102
funcname = '%s_1' % funcname
104-
lines.append('from nipype.utils.functions import getsource')
103+
lines.append('from nipype.utils.misc import getsource')
105104
lines.append("%s.inputs.%s = getsource(%s)" % (nodename,
106105
key,
107106
funcname))

nipype/pipeline/engine/workflows.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737

3838
from ... import config, logging
39-
from ...utils.misc import (unflatten, package_check, str2bool)
40-
from ...utils.functions import (getsource, create_function_from_source)
39+
from ...utils.misc import (unflatten, package_check, str2bool,
40+
getsource, create_function_from_source)
4141
from ...interfaces.base import (traits, InputMultiPath, CommandLine,
4242
Undefined, TraitedSpec, DynamicTraitedSpec,
4343
Bunch, InterfaceResult, md5, Interface,

nipype/utils/functions.py

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

nipype/utils/misc.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
"""Miscellaneous utility functions
55
"""
6-
from __future__ import print_function, unicode_literals, division, absolute_import
6+
from __future__ import print_function, division, unicode_literals, absolute_import
77
from future import standard_library
88
standard_library.install_aliases()
99
from builtins import next, str
@@ -66,6 +66,47 @@ def trim(docstring, marker=None):
6666
return '\n'.join(trimmed)
6767

6868

69+
def getsource(function):
70+
"""Returns the source code of a function"""
71+
src = dedent(inspect.getsource(function))
72+
return src
73+
74+
75+
def create_function_from_source(function_source, imports=None):
76+
"""Return a function object from a function source
77+
78+
Parameters
79+
----------
80+
function_source : pickled string
81+
string in pickled form defining a function
82+
imports : list of strings
83+
list of import statements in string form that allow the function
84+
to be executed in an otherwise empty namespace
85+
"""
86+
ns = {}
87+
import_keys = []
88+
try:
89+
if imports is not None:
90+
for statement in imports:
91+
exec(statement, ns)
92+
import_keys = list(ns.keys())
93+
exec(function_source, ns)
94+
95+
except Exception as e:
96+
msg = '\nError executing function:\n %s\n' % function_source
97+
msg += '\n'.join(["Functions in connection strings have to be standalone.",
98+
"They cannot be declared either interactively or inside",
99+
"another function or inline in the connect string. Any",
100+
"imports should be done inside the function"
101+
])
102+
raise_from(RuntimeError(msg), e)
103+
ns_funcs = list(set(ns) - set(import_keys + ['__builtins__']))
104+
assert len(ns_funcs) == 1, "Function or inputs are ill-defined"
105+
funcname = ns_funcs[0]
106+
func = ns[funcname]
107+
return func
108+
109+
69110
def find_indices(condition):
70111
"Return the indices where ravel(condition) is true"
71112
res, = np.nonzero(np.ravel(condition))

nipype/utils/tests/test_functions.py

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

nipype/utils/tests/test_misc.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import pytest
1010

11-
from nipype.utils.misc import (container_to_string, str2bool,
12-
flatten, unflatten)
11+
from nipype.utils.misc import (container_to_string, getsource,
12+
create_function_from_source, str2bool, flatten,
13+
unflatten)
1314

1415

1516
def test_cont_to_str():
@@ -34,6 +35,26 @@ def test_cont_to_str():
3435
assert (container_to_string(123) == '123')
3536

3637

38+
def _func1(x):
39+
return x**3
40+
41+
42+
def test_func_to_str():
43+
44+
def func1(x):
45+
return x**2
46+
47+
# Should be ok with both functions!
48+
for f in _func1, func1:
49+
f_src = getsource(f)
50+
f_recreated = create_function_from_source(f_src)
51+
assert f(2.3) == f_recreated(2.3)
52+
53+
def test_func_to_str_err():
54+
bad_src = "obbledygobbledygook"
55+
with pytest.raises(RuntimeError): create_function_from_source(bad_src)
56+
57+
3758
@pytest.mark.parametrize("string, expected", [
3859
("yes", True), ("true", True), ("t", True), ("1", True),
3960
("no", False), ("false", False), ("n", False), ("f", False), ("0", False)

0 commit comments

Comments
 (0)