Skip to content

Commit b2de969

Browse files
committed
Added JSON File interface
1 parent e815741 commit b2de969

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

nipype/interfaces/io.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,3 +1803,45 @@ def _get_ssh_client(self):
18031803
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
18041804
client.connect(host['hostname'], username=host['user'], sock=proxy)
18051805
return client
1806+
1807+
class JSONFileGrabberInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
1808+
in_file = File(exists=True, mandatory=True,
1809+
desc='JSON source file')
1810+
1811+
class JSONFileGrabber(IOBase):
1812+
"""
1813+
Datagrabber module that loads a json file and generates an output for every
1814+
first-level object
1815+
1816+
Example
1817+
-------
1818+
1819+
>>> from nipype.interfaces.io import JSONFileGrabber
1820+
>>> jsonSource = JSONFileGrabber()
1821+
>>> jsonSource.inputs.in_file = 'jsongrabber.txt'
1822+
>>> res = jsonSource.run() # doctest: +SKIP
1823+
>>> print res.outputs.param1
1824+
'exampleStr'
1825+
>>> print res.outputs.param2
1826+
4
1827+
1828+
"""
1829+
input_spec = JSONFileGrabberInputSpec
1830+
output_spec = DynamicTraitedSpec
1831+
_always_run = True
1832+
1833+
1834+
def _list_outputs(self):
1835+
import json
1836+
1837+
with open(self.inputs.in_file, 'r') as f:
1838+
data = json.load(f)
1839+
1840+
if not isinstance(data, dict):
1841+
raise RuntimeError('JSON input has no dictionary structure')
1842+
1843+
outputs = {}
1844+
for key, value in data.iteritems():
1845+
outputs[key] = value
1846+
1847+
return outputs
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.io import JSONFileGrabber
4+
5+
def test_JSONFileGrabber_inputs():
6+
input_map = dict(ignore_exception=dict(nohash=True,
7+
usedefault=True,
8+
),
9+
in_file=dict(mandatory=True,
10+
),
11+
)
12+
inputs = JSONFileGrabber.input_spec()
13+
14+
for key, metadata in input_map.items():
15+
for metakey, value in metadata.items():
16+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
17+
18+
def test_JSONFileGrabber_outputs():
19+
output_map = dict()
20+
outputs = JSONFileGrabber.output_spec()
21+
22+
for key, metadata in output_map.items():
23+
for metakey, value in metadata.items():
24+
yield assert_equal, getattr(outputs.traits()[key], metakey), value
25+

nipype/testing/data/jsongrabber.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"param2": 4, "param1": "exampleStr"}

0 commit comments

Comments
 (0)