Skip to content
Merged
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
31 changes: 31 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 distutils.version import LooseVersion
from collections import namedtuple
from ...base import traits, File, TraitedSpec, BaseInterfaceInputSpec
from ..base import (
Expand All @@ -8,7 +9,10 @@
DipyBaseInterface,
no_dipy,
get_dipy_workflows,
get_default_args,
dipy_version
)
DIPY_1_4_LESS = LooseVersion(dipy_version()) < LooseVersion("1.4")


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


@pytest.mark.skipif(no_dipy() and DIPY_1_4_LESS,
reason="DIPY is not installed")
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 +208,4 @@ def test_get_dipy_workflows():
test_convert_to_traits_type()
test_create_interface_specs()
test_dipy_to_nipype_interface()
test_get_default_args()