Skip to content

[FIX] Import error for nipype.interfaces.dipy.base #3414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nipype/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def get_nipype_gitversion():
EXTRA_REQUIRES = {
"data": ["datalad"],
"doc": [
"dipy!=1.4.1",
"dipy",
"ipython",
"matplotlib",
"nbsphinx",
Expand All @@ -172,7 +172,7 @@ def get_nipype_gitversion():
"sphinxcontrib-napoleon",
],
"duecredit": ["duecredit"],
"nipy": ["nitime", "nilearn", "dipy!=1.4.1", "nipy", "matplotlib"],
"nipy": ["nitime", "nilearn", "dipy", "nipy", "matplotlib"],
"profiler": ["psutil>=5.0"],
"pybids": ["pybids>=0.7.0"],
"specs": ["black"],
Expand Down
32 changes: 23 additions & 9 deletions nipype/interfaces/dipy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,21 @@


def no_dipy():
"""Check if dipy is available"""
"""Check if dipy is available."""
global HAVE_DIPY
return not HAVE_DIPY


def dipy_version():
"""Check dipy version"""
"""Check dipy version."""
if no_dipy():
return None

return dipy.__version__


class DipyBaseInterface(LibraryBaseInterface):
"""
A base interface for py:mod:`dipy` computations
"""
"""A base interface for py:mod:`dipy` computations."""

_pkg = "dipy"

Expand All @@ -57,9 +55,7 @@ class DipyBaseInterfaceInputSpec(BaseInterfaceInputSpec):


class DipyDiffusionInterface(DipyBaseInterface):
"""
A base interface for py:mod:`dipy` computations
"""
"""A base interface for py:mod:`dipy` computations."""

input_spec = DipyBaseInterfaceInputSpec

Expand Down Expand Up @@ -90,6 +86,24 @@ def _gen_filename(self, name, ext=None):
return out_prefix + "_" + name + ext


def get_default_args(func):
"""Return optional arguments of a function.

Parameters
----------
func: callable

Returns
-------
dict

"""
signature = inspect.signature(func)
return {k: v.default for k, v in signature.parameters.items()
if v.default is not inspect.Parameter.empty
}


def convert_to_traits_type(dipy_type, is_file=False):
"""Convert DIPY type to Traits type."""
dipy_type = dipy_type.lower()
Expand Down Expand Up @@ -189,7 +203,7 @@ def dipy_to_nipype_interface(cls_name, dipy_flow, BaseClass=DipyBaseInterface):
parser = IntrospectiveArgumentParser()
flow = dipy_flow()
parser.add_workflow(flow)
default_values = inspect.getfullargspec(flow.run).defaults
default_values = list(get_default_args(flow.run).values())
optional_params = [
args + (val,) for args, val in zip(parser.optional_parameters, default_values)
]
Expand Down
30 changes: 30 additions & 0 deletions nipype/interfaces/dipy/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from packaging.version import Version
from collections import namedtuple
from ...base import traits, File, TraitedSpec, BaseInterfaceInputSpec
from ..base import (
Expand All @@ -8,6 +9,8 @@
DipyBaseInterface,
no_dipy,
get_dipy_workflows,
get_default_args,
dipy_version
)


Expand Down Expand Up @@ -112,6 +115,32 @@ def test_create_interface_specs():
assert "out_params" in current_params.keys()


@pytest.mark.skipif(no_dipy() or Version(dipy_version()) < Version("1.4"),
reason="DIPY >=1.4 required")
def test_get_default_args():
from dipy.utils.deprecator import deprecated_params

def test(dummy=11, x=3):
return dummy, x

@deprecated_params('x', None, '0.3', '0.5', alternative='test2.y')
def test2(dummy=11, x=3):
return dummy, x

@deprecated_params(['dummy', 'x'], None, '0.3', alternative='test2.y')
def test3(dummy=11, x=3):
return dummy, x

@deprecated_params(['dummy', 'x'], None, '0.3', '0.5',
alternative='test2.y')
def test4(dummy=11, x=3):
return dummy, x

expected_res = {'dummy': 11, 'x': 3}
for func in [test, test2, test3, test4]:
assert get_default_args(func) == expected_res


@pytest.mark.skipif(no_dipy(), reason="DIPY is not installed")
def test_dipy_to_nipype_interface():
from dipy.workflows.workflow import Workflow
Expand Down Expand Up @@ -178,3 +207,4 @@ def test_get_dipy_workflows():
test_convert_to_traits_type()
test_create_interface_specs()
test_dipy_to_nipype_interface()
test_get_default_args()