-
Notifications
You must be signed in to change notification settings - Fork 533
[FIX] Bugs found after #1572 and #1591 #1623
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e681974
[WIP,FIX] Bugs found after #1572 and #1591
oesteban d3f3f49
add tests
oesteban 2aeb71f
simplify code of interfaces.get_hashval()
oesteban ec7f12e
[FIX] Initialization of interfaces with from_files
oesteban ede415d
fixes a corner case of tuple with len 1 encoding
oesteban dac763b
Merge branch 'master' into fix/FromFileWorkflows
oesteban 92ce963
roll back to get() in get_hashval
oesteban 7197b6a
Merge remote-tracking branch 'upstream/master' into fix/FromFileWorkf…
oesteban e970941
replace theobject with objekt
oesteban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
nipype_version = LooseVersion(__version__) | ||
iflogger = logging.getLogger('interface') | ||
|
||
FLOAT_FORMAT = '{:.10f}'.format | ||
PY35 = sys.version_info >= (3, 5) | ||
PY3 = sys.version_info[0] > 2 | ||
|
||
|
@@ -536,6 +537,14 @@ def _clean_container(self, object, undefinedval=None, skipundefined=False): | |
out = undefinedval | ||
return out | ||
|
||
def has_metadata(self, name, metadata, value=None, recursive=True): | ||
""" | ||
Return has_metadata for the requested trait name in this | ||
interface | ||
""" | ||
return has_metadata(self.trait(name).trait_type, metadata, value, | ||
recursive) | ||
|
||
def get_hashval(self, hash_method=None): | ||
"""Return a dictionary of our items with hashes for each file. | ||
|
||
|
@@ -560,61 +569,61 @@ def get_hashval(self, hash_method=None): | |
dict_withhash = [] | ||
dict_nofilename = [] | ||
for name, val in sorted(self.get().items()): | ||
if isdefined(val): | ||
trait = self.trait(name) | ||
if has_metadata(trait.trait_type, "nohash", True): | ||
continue | ||
hash_files = (not has_metadata(trait.trait_type, "hash_files", | ||
False) and not | ||
has_metadata(trait.trait_type, "name_source")) | ||
dict_nofilename.append((name, | ||
self._get_sorteddict(val, hash_method=hash_method, | ||
hash_files=hash_files))) | ||
dict_withhash.append((name, | ||
self._get_sorteddict(val, True, hash_method=hash_method, | ||
hash_files=hash_files))) | ||
if not isdefined(val) or self.has_metadata(name, "nohash", True): | ||
# skip undefined traits and traits with nohash=True | ||
continue | ||
|
||
hash_files = (not self.has_metadata(name, "hash_files", False) and not | ||
self.has_metadata(name, "name_source")) | ||
dict_nofilename.append((name, | ||
self._get_sorteddict(val, hash_method=hash_method, | ||
hash_files=hash_files))) | ||
dict_withhash.append((name, | ||
self._get_sorteddict(val, True, hash_method=hash_method, | ||
hash_files=hash_files))) | ||
return dict_withhash, md5(encode_dict(dict_nofilename).encode()).hexdigest() | ||
|
||
def _get_sorteddict(self, object, dictwithhash=False, hash_method=None, | ||
|
||
def _get_sorteddict(self, objekt, dictwithhash=False, hash_method=None, | ||
hash_files=True): | ||
if isinstance(object, dict): | ||
if isinstance(objekt, dict): | ||
out = [] | ||
for key, val in sorted(object.items()): | ||
for key, val in sorted(objekt.items()): | ||
if isdefined(val): | ||
out.append((key, | ||
self._get_sorteddict(val, dictwithhash, | ||
hash_method=hash_method, | ||
hash_files=hash_files))) | ||
elif isinstance(object, (list, tuple)): | ||
elif isinstance(objekt, (list, tuple)): | ||
out = [] | ||
for val in object: | ||
for val in objekt: | ||
if isdefined(val): | ||
out.append(self._get_sorteddict(val, dictwithhash, | ||
hash_method=hash_method, | ||
hash_files=hash_files)) | ||
if isinstance(object, tuple): | ||
if isinstance(objekt, tuple): | ||
out = tuple(out) | ||
else: | ||
if isdefined(object): | ||
if (hash_files and isinstance(object, (str, bytes)) and | ||
os.path.isfile(object)): | ||
if isdefined(objekt): | ||
if (hash_files and isinstance(objekt, (str, bytes)) and | ||
os.path.isfile(objekt)): | ||
if hash_method is None: | ||
hash_method = config.get('execution', 'hash_method') | ||
|
||
if hash_method.lower() == 'timestamp': | ||
hash = hash_timestamp(object) | ||
hash = hash_timestamp(objekt) | ||
elif hash_method.lower() == 'content': | ||
hash = hash_infile(object) | ||
hash = hash_infile(objekt) | ||
else: | ||
raise Exception("Unknown hash method: %s" % hash_method) | ||
if dictwithhash: | ||
out = (object, hash) | ||
out = (objekt, hash) | ||
else: | ||
out = hash | ||
elif isinstance(object, float): | ||
out = '%.10f' % object | ||
elif isinstance(objekt, float): | ||
out = FLOAT_FORMAT(objekt) | ||
else: | ||
out = object | ||
out = objekt | ||
return out | ||
|
||
|
||
|
@@ -769,7 +778,10 @@ def __init__(self, from_file=None, **inputs): | |
self.num_threads = 1 | ||
|
||
if from_file is not None: | ||
self.load_inputs_from_json(from_file, overwrite=False) | ||
self.load_inputs_from_json(from_file, overwrite=True) | ||
|
||
for name, value in list(inputs.items()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This solves #1625. |
||
setattr(self.inputs, name, value) | ||
|
||
|
||
@classmethod | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,11 @@ | |
import tempfile | ||
import shutil | ||
import warnings | ||
import simplejson as json | ||
|
||
from nipype.testing import (assert_equal, assert_not_equal, assert_raises, | ||
assert_true, assert_false, with_setup, package_check, | ||
skipif) | ||
skipif, example_data) | ||
import nipype.interfaces.base as nib | ||
from nipype.utils.filemanip import split_filename | ||
from nipype.interfaces.base import Undefined, config | ||
|
@@ -497,10 +498,21 @@ def __init__(self, **inputs): | |
bif6.load_inputs_from_json(tmp_json) | ||
yield assert_equal, bif6.inputs.get_traitsfree(), inputs_dict | ||
|
||
def assert_not_raises(fn, *args, **kwargs): | ||
fn(*args, **kwargs) | ||
return True | ||
# test get hashval in a complex interface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added 3 new tests |
||
from nipype.interfaces.ants import Registration | ||
settings = example_data(example_data('smri_ants_registration_settings.json')) | ||
with open(settings) as setf: | ||
data_dict = json.load(setf) | ||
|
||
tsthash = Registration() | ||
tsthash.load_inputs_from_json(settings) | ||
yield assert_equal, {}, check_dict(data_dict, tsthash.inputs.get_traitsfree()) | ||
|
||
tsthash2 = Registration(from_file=settings) | ||
yield assert_equal, {}, check_dict(data_dict, tsthash2.inputs.get_traitsfree()) | ||
|
||
_, hashvalue = tsthash.inputs.get_hashval(hash_method='timestamp') | ||
yield assert_equal, 'ec5755e07287e04a4b409e03b77a517c', hashvalue | ||
|
||
def test_input_version(): | ||
class InputSpec(nib.TraitedSpec): | ||
|
@@ -738,3 +750,27 @@ def test_global_CommandLine_output(): | |
yield assert_equal, res.runtime.stdout, '' | ||
os.chdir(pwd) | ||
teardown_file(tmpd) | ||
|
||
def assert_not_raises(fn, *args, **kwargs): | ||
fn(*args, **kwargs) | ||
return True | ||
|
||
def check_dict(ref_dict, tst_dict): | ||
"""Compare dictionaries of inputs and and those loaded from json files""" | ||
def to_list(x): | ||
if isinstance(x, tuple): | ||
x = list(x) | ||
|
||
if isinstance(x, list): | ||
for i, xel in enumerate(x): | ||
x[i] = to_list(xel) | ||
|
||
return x | ||
|
||
failed_dict = {} | ||
for key, value in list(ref_dict.items()): | ||
newval = to_list(tst_dict[key]) | ||
if newval != value: | ||
failed_dict[key] = (value, newval) | ||
return failed_dict | ||
|
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this settings file from examples to the example data, since now I use it in tests