Skip to content

Commit 7c98066

Browse files
committed
Add defaults input to JSONFileGrabber
1 parent 2410411 commit 7c98066

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

nipype/interfaces/io.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,8 +1804,9 @@ def _get_ssh_client(self):
18041804

18051805

18061806
class JSONFileGrabberInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
1807-
in_file = File(exists=True, mandatory=True,
1808-
desc='JSON source file')
1807+
in_file = File(exists=True, desc='JSON source file')
1808+
defaults = traits.Dict(desc=('JSON dictionary that sets default output'
1809+
'values, overridden by values found in in_file'))
18091810

18101811

18111812
class JSONFileGrabber(IOBase):
@@ -1819,12 +1820,15 @@ class JSONFileGrabber(IOBase):
18191820
18201821
>>> from nipype.interfaces.io import JSONFileGrabber
18211822
>>> jsonSource = JSONFileGrabber()
1823+
>>> jsonSource.inputs.defaults = {'param1': 'overrideMe', 'param3': 1.0}
1824+
>>> res = jsonSource.run()
1825+
>>> res.outputs.get()
1826+
{'param1': 'overrideMe', 'param3': 1.0 }
18221827
>>> jsonSource.inputs.in_file = 'jsongrabber.txt'
18231828
>>> res = jsonSource.run()
1824-
>>> print res.outputs.param1
1825-
exampleStr
1826-
>>> print res.outputs.param2
1827-
4
1829+
>>> res.outputs.get()
1830+
{'param1': 'exampleStr', 'param2': 4, 'param3': 1.0}
1831+
18281832
18291833
"""
18301834
input_spec = JSONFileGrabberInputSpec
@@ -1834,15 +1838,22 @@ class JSONFileGrabber(IOBase):
18341838
def _list_outputs(self):
18351839
import json
18361840

1837-
with open(self.inputs.in_file, 'r') as f:
1838-
data = json.load(f)
1841+
outputs = {}
1842+
if isdefined(self.inputs.in_file):
1843+
with open(self.inputs.in_file, 'r') as f:
1844+
data = json.load(f)
18391845

1840-
if not isinstance(data, dict):
1841-
raise RuntimeError('JSON input has no dictionary structure')
1846+
if not isinstance(data, dict):
1847+
raise RuntimeError('JSON input has no dictionary structure')
18421848

1843-
outputs = {}
1844-
for key, value in data.iteritems():
1845-
outputs[key] = value
1849+
for key, value in data.iteritems():
1850+
outputs[key] = value
1851+
1852+
if isdefined(self.inputs.defaults):
1853+
defaults = self.inputs.defaults
1854+
for key, value in defaults.iteritems():
1855+
if key not in outputs.keys():
1856+
outputs[key] = value
18461857

18471858
return outputs
18481859

nipype/interfaces/tests/test_auto_JSONFileGrabber.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from nipype.interfaces.io import JSONFileGrabber
44

55
def test_JSONFileGrabber_inputs():
6-
input_map = dict(ignore_exception=dict(nohash=True,
6+
input_map = dict(defaults=dict(),
7+
ignore_exception=dict(nohash=True,
78
usedefault=True,
89
),
9-
in_file=dict(mandatory=True,
10-
),
10+
in_file=dict(),
1111
)
1212
inputs = JSONFileGrabber.input_spec()
1313

0 commit comments

Comments
 (0)