Skip to content
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

handle unavailable traits due to version differences #3273

Merged
merged 6 commits into from Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions nipype/interfaces/base/core.py
Expand Up @@ -31,7 +31,7 @@

from ...external.due import due

from .traits_extension import traits, isdefined
from .traits_extension import traits, isdefined, Undefined
from .specs import (
BaseInterfaceInputSpec,
CommandLineInputSpec,
Expand Down Expand Up @@ -180,7 +180,14 @@ def __init__(
if not self.input_spec:
raise Exception("No input_spec in class: %s" % self.__class__.__name__)

self.inputs = self.input_spec(**inputs)
# Create input spec, disable any defaults that are unavailable due to
# version, and then apply the inputs that were passed.
self.inputs = self.input_spec()
unavailable_traits = self._check_version_requirements(self.inputs, raise_exception=False)
if unavailable_traits:
self.inputs.trait_set(**{k: Undefined for k in unavailable_traits})
self.inputs.trait_set(**inputs)

self.ignore_exception = ignore_exception

if resource_monitor is not None:
Expand Down
66 changes: 56 additions & 10 deletions nipype/interfaces/base/tests/test_core.py
Expand Up @@ -245,15 +245,17 @@ class input_spec(nib.TraitedSpec):

_version = "misparsed-garbage"

obj = DerivedInterface()
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
obj = DerivedInterface()
assert len(caplog.records) == 2
obj.inputs.foo = 1
obj.inputs.bar = 1
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
obj._check_version_requirements(obj.inputs)
assert len(caplog.records) == 2
assert len(caplog.records) == 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed the expected warnings... I'm not sure if we want them at init?

Copy link
Member Author

@satra satra Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could suppress them in init.



def test_input_version_missing_error():
def test_input_version_missing_error(caplog):
from nipype import config

class DerivedInterface(nib.BaseInterface):
Expand All @@ -263,13 +265,57 @@ class input_spec(nib.TraitedSpec):

_version = "misparsed-garbage"

with mock.patch.object(config, "getboolean", return_value=True):
obj = DerivedInterface(foo=1)
with pytest.raises(ValueError):
obj._check_version_requirements(obj.inputs)
obj = DerivedInterface(bar=1)
with pytest.raises(ValueError):
obj._check_version_requirements(obj.inputs)
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
obj1 = DerivedInterface(foo=1)
obj2 = DerivedInterface(bar=1)
assert len(caplog.records) == 4
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
with mock.patch.object(config, "getboolean", return_value=True):
with pytest.raises(ValueError):
obj1._check_version_requirements(obj1.inputs)
with pytest.raises(ValueError):
obj2._check_version_requirements(obj2.inputs)
assert len(caplog.records) == 6


def test_unavailable_input():
class WithInput(nib.BaseInterface):
class input_spec(nib.TraitedSpec):
foo = nib.traits.Int(3, usedefault=True, max_ver="0.5")

_version = "0.4"

def _run_interface(self, runtime):
return runtime

class WithoutInput(WithInput):
_version = "0.6"

has = WithInput()
hasnt = WithoutInput()
trying_anyway = WithoutInput(foo=3)
assert has.inputs.foo == 3
assert not nib.isdefined(hasnt.inputs.foo)
assert trying_anyway.inputs.foo == 3

has.run()
hasnt.run()
with pytest.raises(Exception):
trying_anyway.run()

# Still settable
has.inputs.foo = 4
hasnt.inputs.foo = 4
trying_anyway.inputs.foo = 4
assert has.inputs.foo == 4
assert hasnt.inputs.foo == 4
assert trying_anyway.inputs.foo == 4

has.run()
with pytest.raises(Exception):
hasnt.run()
with pytest.raises(Exception):
trying_anyway.run()


def test_output_version():
Expand Down