Skip to content

Commit ec7f12e

Browse files
committed
[FIX] Initialization of interfaces with from_files
Improved the way interfaces were initialized to prevent that lists of lists of int are not set. Fixes #1625
1 parent 2aeb71f commit ec7f12e

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

nipype/interfaces/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,10 @@ def __init__(self, from_file=None, **inputs):
776776
self.num_threads = 1
777777

778778
if from_file is not None:
779-
self.load_inputs_from_json(from_file, overwrite=False)
779+
self.load_inputs_from_json(from_file, overwrite=True)
780+
781+
for name, value in list(inputs.items()):
782+
setattr(self.inputs, name, value)
780783

781784

782785
@classmethod

nipype/interfaces/tests/test_base.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,16 @@ def __init__(self, **inputs):
503503
settings = example_data(example_data('smri_ants_registration_settings.json'))
504504
with open(settings) as setf:
505505
data_dict = json.load(setf)
506-
tsthash = Registration(from_file=settings)
507-
yield assert_all_true, data_dict, tsthash.inputs.get_traitsfree()
508-
hashed_inputs, hashvalue = tsthash.inputs.get_hashval(hash_method='timestamp')
509-
# yield assert_equal, hashvalue, '9ab944cbccba61475becb9eac65052af'
510506

511-
def assert_not_raises(fn, *args, **kwargs):
512-
fn(*args, **kwargs)
513-
return True
507+
tsthash = Registration()
508+
tsthash.load_inputs_from_json(settings)
509+
yield assert_equal, {}, check_dict(data_dict, tsthash.inputs.get_traitsfree())
514510

515-
def assert_all_true(ref_dict, tst_dict):
516-
for key, value in list(ref_dict.items()):
517-
if tst_dict[key] != value:
518-
return False
519-
return True
511+
tsthash2 = Registration(from_file=settings)
512+
yield assert_equal, {}, check_dict(data_dict, tsthash2.inputs.get_traitsfree())
513+
514+
hashed_inputs, hashvalue = tsthash.inputs.get_hashval(hash_method='timestamp')
515+
yield assert_equal, hashvalue, '9ab944cbccba61475becb9eac65052af'
520516

521517
def test_input_version():
522518
class InputSpec(nib.TraitedSpec):
@@ -754,3 +750,27 @@ def test_global_CommandLine_output():
754750
yield assert_equal, res.runtime.stdout, ''
755751
os.chdir(pwd)
756752
teardown_file(tmpd)
753+
754+
def assert_not_raises(fn, *args, **kwargs):
755+
fn(*args, **kwargs)
756+
return True
757+
758+
def check_dict(ref_dict, tst_dict):
759+
"""Compare dictionaries of inputs and and those loaded from json files"""
760+
def to_list(x):
761+
if isinstance(x, tuple):
762+
x = list(x)
763+
764+
if isinstance(x, list):
765+
for i, xel in enumerate(x):
766+
x[i] = to_list(xel)
767+
768+
return x
769+
770+
failed_dict = {}
771+
for key, value in list(ref_dict.items()):
772+
newval = to_list(tst_dict[key])
773+
if newval != value:
774+
failed_dict[key] = (value, newval)
775+
return failed_dict
776+

0 commit comments

Comments
 (0)