Skip to content

Commit 3f5e185

Browse files
authored
Merge pull request #2387 from djarecka/interfaces_base_2320
changes in Interfaces base (closes #2320)
2 parents 0ba35f1 + c1ab001 commit 3f5e185

File tree

7 files changed

+44
-166
lines changed

7 files changed

+44
-166
lines changed

nipype/interfaces/base/core.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,12 @@ def _check_version_requirements(self, trait_object, raise_exception=True):
412412
raise Exception(
413413
'Trait %s (%s) (version %s < required %s)' %
414414
(name, self.__class__.__name__, version, min_ver))
415-
check = dict(max_ver=lambda t: t is not None)
416-
names = trait_object.trait_names(**check)
415+
416+
# check maximum version
417+
check = dict(max_ver=lambda t: t is not None)
418+
names = trait_object.trait_names(**check)
419+
if names and self.version:
420+
version = LooseVersion(str(self.version))
417421
for name in names:
418422
max_ver = LooseVersion(
419423
str(trait_object.traits()[name].max_ver))

nipype/interfaces/base/specs.py

-37
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,6 @@ def _xor_warn(self, obj, name, old, new):
119119
'which is already set') % (name, trait_name)
120120
raise IOError(msg)
121121

122-
def _requires_warn(self, obj, name, old, new):
123-
"""Part of the xor behavior
124-
"""
125-
if isdefined(new):
126-
trait_spec = self.traits()[name]
127-
msg = None
128-
for trait_name in trait_spec.requires:
129-
if not isdefined(getattr(self, trait_name)):
130-
if not msg:
131-
msg = 'Input %s requires inputs: %s' \
132-
% (name, ', '.join(trait_spec.requires))
133-
if msg: # only one requires warning at a time.
134-
warn(msg)
135-
136122
def _deprecated_warn(self, obj, name, old, new):
137123
"""Checks if a user assigns a value to a deprecated trait
138124
"""
@@ -165,29 +151,6 @@ def _deprecated_warn(self, obj, name, old, new):
165151
'%s' % trait_spec.new_name: new
166152
})
167153

168-
def _hash_infile(self, adict, key):
169-
""" Inject file hashes into adict[key]"""
170-
stuff = adict[key]
171-
if not is_container(stuff):
172-
stuff = [stuff]
173-
file_list = []
174-
for afile in stuff:
175-
if is_container(afile):
176-
hashlist = self._hash_infile({'infiles': afile}, 'infiles')
177-
hash = [val[1] for val in hashlist]
178-
else:
179-
if config.get('execution',
180-
'hash_method').lower() == 'timestamp':
181-
hash = hash_timestamp(afile)
182-
elif config.get('execution',
183-
'hash_method').lower() == 'content':
184-
hash = hash_infile(afile)
185-
else:
186-
raise Exception("Unknown hash method: %s" % config.get(
187-
'execution', 'hash_method'))
188-
file_list.append((afile, hash))
189-
return file_list
190-
191154
def get(self, **kwargs):
192155
""" Returns traited class as a dict
193156

nipype/interfaces/base/support.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,8 @@ def _get_bunch_hash(self):
168168
sorted_dict = to_str(sorted(dict_nofilename.items()))
169169
return dict_withhash, md5(sorted_dict.encode()).hexdigest()
170170

171-
def __pretty__(self, p, cycle):
172-
"""Support for the pretty module
173-
174-
pretty is included in ipython.externals for ipython > 0.10"""
171+
def _repr_pretty_(self, p, cycle):
172+
"""Support for the pretty module from ipython.externals"""
175173
if cycle:
176174
p.text('Bunch(...)')
177175
else:

nipype/interfaces/base/tests/test_core.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -177,74 +177,76 @@ def __init__(self, **inputs):
177177
assert 'ec5755e07287e04a4b409e03b77a517c' == hashvalue
178178

179179

180-
def test_input_version():
181-
class InputSpec(nib.TraitedSpec):
182-
foo = nib.traits.Int(desc='a random int', min_ver='0.9')
180+
class MinVerInputSpec(nib.TraitedSpec):
181+
foo = nib.traits.Int(desc='a random int', min_ver='0.9')
182+
183+
class MaxVerInputSpec(nib.TraitedSpec):
184+
foo = nib.traits.Int(desc='a random int', max_ver='0.7')
183185

186+
187+
def test_input_version_1():
184188
class DerivedInterface1(nib.BaseInterface):
185-
input_spec = InputSpec
189+
input_spec = MinVerInputSpec
186190

187191
obj = DerivedInterface1()
188192
obj._check_version_requirements(obj.inputs)
189193

190194
config.set('execution', 'stop_on_unknown_version', True)
191195

192-
with pytest.raises(Exception):
196+
with pytest.raises(ValueError) as excinfo:
193197
obj._check_version_requirements(obj.inputs)
198+
assert "no version information" in str(excinfo.value)
194199

195200
config.set_default_config()
196201

197-
class InputSpec(nib.TraitedSpec):
198-
foo = nib.traits.Int(desc='a random int', min_ver='0.9')
199202

203+
def test_input_version_2():
200204
class DerivedInterface1(nib.BaseInterface):
201-
input_spec = InputSpec
205+
input_spec = MinVerInputSpec
202206
_version = '0.8'
203207

204208
obj = DerivedInterface1()
205209
obj.inputs.foo = 1
206-
with pytest.raises(Exception):
207-
obj._check_version_requirements()
210+
with pytest.raises(Exception) as excinfo:
211+
obj._check_version_requirements(obj.inputs)
212+
assert "version 0.8 < required 0.9" in str(excinfo.value)
208213

209-
class InputSpec(nib.TraitedSpec):
210-
foo = nib.traits.Int(desc='a random int', min_ver='0.9')
211214

215+
def test_input_version_3():
212216
class DerivedInterface1(nib.BaseInterface):
213-
input_spec = InputSpec
217+
input_spec = MinVerInputSpec
214218
_version = '0.10'
215219

216220
obj = DerivedInterface1()
217221
obj._check_version_requirements(obj.inputs)
218222

219-
class InputSpec(nib.TraitedSpec):
220-
foo = nib.traits.Int(desc='a random int', min_ver='0.9')
221223

224+
def test_input_version_4():
222225
class DerivedInterface1(nib.BaseInterface):
223-
input_spec = InputSpec
226+
input_spec = MinVerInputSpec
224227
_version = '0.9'
225228

226229
obj = DerivedInterface1()
227230
obj.inputs.foo = 1
228231
obj._check_version_requirements(obj.inputs)
229232

230-
class InputSpec(nib.TraitedSpec):
231-
foo = nib.traits.Int(desc='a random int', max_ver='0.7')
232233

234+
def test_input_version_5():
233235
class DerivedInterface2(nib.BaseInterface):
234-
input_spec = InputSpec
236+
input_spec = MaxVerInputSpec
235237
_version = '0.8'
236238

237239
obj = DerivedInterface2()
238240
obj.inputs.foo = 1
239-
with pytest.raises(Exception):
240-
obj._check_version_requirements()
241+
with pytest.raises(Exception) as excinfo:
242+
obj._check_version_requirements(obj.inputs)
243+
assert "version 0.8 > required 0.7" in str(excinfo.value)
241244

242-
class InputSpec(nib.TraitedSpec):
243-
foo = nib.traits.Int(desc='a random int', max_ver='0.9')
244245

246+
def test_input_version_6():
245247
class DerivedInterface1(nib.BaseInterface):
246-
input_spec = InputSpec
247-
_version = '0.9'
248+
input_spec = MaxVerInputSpec
249+
_version = '0.7'
248250

249251
obj = DerivedInterface1()
250252
obj.inputs.foo = 1

nipype/interfaces/base/traits_extension.py

+9-94
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Str(Unicode):
5454
traits.DictStrStr = DictStrStr
5555

5656

57-
class BaseFile(BaseUnicode):
57+
class File(BaseUnicode):
5858
""" Defines a trait whose value must be the name of a file.
5959
"""
6060

@@ -96,14 +96,11 @@ def __init__(self,
9696
if exists:
9797
self.info_text = 'an existing file name'
9898

99-
super(BaseFile, self).__init__(value, **metadata)
99+
super(File, self).__init__(value, **metadata)
100100

101101
def validate(self, object, name, value):
102-
""" Validates that a specified value is valid for this trait.
103-
104-
Note: The 'fast validator' version performs this check in C.
105-
"""
106-
validated_value = super(BaseFile, self).validate(object, name, value)
102+
""" Validates that a specified value is valid for this trait."""
103+
validated_value = super(File, self).validate(object, name, value)
107104
if not self.exists:
108105
return validated_value
109106
elif os.path.isfile(value):
@@ -117,53 +114,12 @@ def validate(self, object, name, value):
117114
self.error(object, name, value)
118115

119116

120-
class File(BaseFile):
121-
"""
122-
Defines a trait whose value must be the name of a file.
123-
Disables the default C-level fast validator.
124-
"""
125-
126-
def __init__(self,
127-
value='',
128-
filter=None,
129-
auto_set=False,
130-
entries=0,
131-
exists=False,
132-
**metadata):
133-
""" Creates a File trait.
134-
135-
Parameters
136-
----------
137-
value : string
138-
The default value for the trait
139-
filter : string
140-
A wildcard string to filter filenames in the file dialog box used by
141-
the attribute trait editor.
142-
auto_set : boolean
143-
Indicates whether the file editor updates the trait value after
144-
every key stroke.
145-
exists : boolean
146-
Indicates whether the trait value must be an existing file or
147-
not.
148-
149-
Default Value
150-
-------------
151-
*value* or ''
152-
"""
153-
# if not exists:
154-
# # Define the C-level fast validator to use:
155-
# fast_validate = (11, str)
156-
157-
super(File, self).__init__(value, filter, auto_set, entries, exists,
158-
**metadata)
159-
160-
161117
# -------------------------------------------------------------------------------
162-
# 'BaseDirectory' and 'Directory' traits:
118+
# 'Directory' trait
163119
# -------------------------------------------------------------------------------
164120

165121

166-
class BaseDirectory(BaseUnicode):
122+
class Directory(BaseUnicode):
167123
"""
168124
Defines a trait whose value must be the name of a directory.
169125
"""
@@ -177,7 +133,7 @@ def __init__(self,
177133
entries=0,
178134
exists=False,
179135
**metadata):
180-
""" Creates a BaseDirectory trait.
136+
""" Creates a Directory trait.
181137
182138
Parameters
183139
----------
@@ -201,13 +157,10 @@ def __init__(self,
201157
if exists:
202158
self.info_text = 'an existing directory name'
203159

204-
super(BaseDirectory, self).__init__(value, **metadata)
160+
super(Directory, self).__init__(value, **metadata)
205161

206162
def validate(self, object, name, value):
207-
""" Validates that a specified value is valid for this trait.
208-
209-
Note: The 'fast validator' version performs this check in C.
210-
"""
163+
""" Validates that a specified value is valid for this trait."""
211164
if isinstance(value, (str, bytes)):
212165
if not self.exists:
213166
return value
@@ -222,44 +175,6 @@ def validate(self, object, name, value):
222175
self.error(object, name, value)
223176

224177

225-
class Directory(BaseDirectory):
226-
"""
227-
Defines a trait whose value must be the name of a directory.
228-
Disables the default C-level fast validator.
229-
"""
230-
231-
def __init__(self,
232-
value='',
233-
auto_set=False,
234-
entries=0,
235-
exists=False,
236-
**metadata):
237-
""" Creates a Directory trait.
238-
239-
Parameters
240-
----------
241-
value : string
242-
The default value for the trait
243-
auto_set : boolean
244-
Indicates whether the directory editor updates the trait value
245-
after every key stroke.
246-
exists : boolean
247-
Indicates whether the trait value must be an existing directory or
248-
not.
249-
250-
Default Value
251-
-------------
252-
*value* or ''
253-
"""
254-
# Define the C-level fast validator to use if the directory existence
255-
# test is not required:
256-
# if not exists:
257-
# self.fast_validate = (11, str)
258-
259-
super(Directory, self).__init__(value, auto_set, entries, exists,
260-
**metadata)
261-
262-
263178
# lists of tuples
264179
# each element consists of :
265180
# - uncompressed (tuple[0]) extension

nipype/pipeline/engine/utils.py

-3
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,6 @@ def _standardize_iterables(node):
11581158
iterables = node.iterables
11591159
# The candidate iterable fields
11601160
fields = set(node.inputs.copyable_trait_names())
1161-
# Flag indicating whether the iterables are in the alternate
1162-
# synchronize form and are not converted to a standard format.
1163-
# synchronize = False # OE: commented out since it is not used
11641161
# A synchronize iterables node without an itersource can be in
11651162
# [fields, value tuples] format rather than
11661163
# [(field, value list), (field, value list), ...]

nipype/utils/misc.py

-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ def dict_diff(dold, dnew, indent=0):
285285
# Values in common keys would differ quite often,
286286
# so we need to join the messages together
287287
for k in new_keys.intersection(old_keys):
288-
same = False
289288
try:
290289
new, old = dnew[k], dold[k]
291290
same = new == old

0 commit comments

Comments
 (0)