@@ -1804,8 +1804,9 @@ def _get_ssh_client(self):
1804
1804
1805
1805
1806
1806
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' ))
1809
1810
1810
1811
1811
1812
class JSONFileGrabber (IOBase ):
@@ -1819,12 +1820,15 @@ class JSONFileGrabber(IOBase):
1819
1820
1820
1821
>>> from nipype.interfaces.io import JSONFileGrabber
1821
1822
>>> 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 }
1822
1827
>>> jsonSource.inputs.in_file = 'jsongrabber.txt'
1823
1828
>>> 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
+
1828
1832
1829
1833
"""
1830
1834
input_spec = JSONFileGrabberInputSpec
@@ -1834,15 +1838,22 @@ class JSONFileGrabber(IOBase):
1834
1838
def _list_outputs (self ):
1835
1839
import json
1836
1840
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 )
1839
1845
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' )
1842
1848
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
1846
1857
1847
1858
return outputs
1848
1859
0 commit comments