Skip to content

Commit c525a1b

Browse files
committed
add dict input to JSONFileSink #1020
1 parent fe6a054 commit c525a1b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

nipype/interfaces/io.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,7 @@ def _list_outputs(self):
18491849

18501850
class JSONFileSinkInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
18511851
out_file = File(desc='JSON sink file')
1852+
in_dict = traits.Dict(desc='input JSON dictionary')
18521853

18531854

18541855
class JSONFileSinkOutputSpec(TraitedSpec):
@@ -1873,11 +1874,18 @@ class JSONFileSink(IOBase):
18731874
>>> jsonsink.inputs.some_measurement = 11.4
18741875
>>> jsonsink.run() # doctest: +SKIP
18751876
1877+
Using a dictionary as input:
1878+
1879+
>>> dictsink = JSONFileSink()
1880+
>>> dictsink.inputs.in_dict = {'subject_id': 's1',
1881+
... 'some_measurement': 11.4}
1882+
>>> dictsink.run() # doctest: +SKIP
1883+
18761884
"""
18771885
input_spec = JSONFileSinkInputSpec
18781886
output_spec = JSONFileSinkOutputSpec
18791887

1880-
def __init__(self, input_names, **inputs):
1888+
def __init__(self, input_names=[], **inputs):
18811889
super(JSONFileSink, self).__init__(**inputs)
18821890
self._input_names = filename_to_list(input_names)
18831891
add_traits(self.inputs, [name for name in self._input_names])
@@ -1891,14 +1899,20 @@ def _list_outputs(self):
18911899
out_file = self.inputs.out_file
18921900

18931901
out_dict = dict()
1894-
for name in self._input_names:
1895-
val = getattr(self.inputs, name)
1896-
val = val if isdefined(val) else 'undefined'
1897-
out_dict[name] = val
1902+
1903+
if isdefined(self.inputs.in_dict):
1904+
if isinstance(self.inputs.in_dict, dict):
1905+
out_dict = self.inputs.in_dict
1906+
else:
1907+
for name in self._input_names:
1908+
val = getattr(self.inputs, name)
1909+
val = val if isdefined(val) else 'undefined'
1910+
out_dict[name] = val
18981911

18991912
with open(out_file, 'w') as f:
19001913
json.dump(out_dict, f)
19011914
outputs = self.output_spec().get()
19021915
outputs['out_file'] = out_file
19031916
return outputs
19041917

1918+

nipype/interfaces/tests/test_auto_JSONFileSink.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def test_JSONFileSink_inputs():
66
input_map = dict(ignore_exception=dict(nohash=True,
77
usedefault=True,
88
),
9+
in_dict=dict(),
910
out_file=dict(),
1011
)
1112
inputs = JSONFileSink.input_spec()

0 commit comments

Comments
 (0)