Skip to content

Commit ac59a94

Browse files
committed
Merge branch 'enh/JSONFileSink' into enh/JSONFileInterface
2 parents 4c8a9fe + f59c467 commit ac59a94

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

nipype/interfaces/io.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,3 +1843,57 @@ def _list_outputs(self):
18431843
outputs[key] = value
18441844

18451845
return outputs
1846+
1847+
1848+
class JSONFileSinkInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
1849+
out_file = File(desc='JSON sink file')
1850+
1851+
1852+
class JSONFileSinkOutputSpec(TraitedSpec):
1853+
out_file = File(desc='JSON sink file')
1854+
1855+
1856+
class JSONFileSink(IOBase):
1857+
""" Very simple frontend for storing values into a JSON file.
1858+
1859+
.. warning::
1860+
1861+
This is not a thread-safe node because it can write to a common
1862+
shared location. It will not complain when it overwrites a file.
1863+
1864+
Examples
1865+
--------
1866+
1867+
>>> jsonsink = JSONFileSink(input_names=['subject_id', 'some_measurement'])
1868+
>>> jsonsink.inputs.subject_id = 's1'
1869+
>>> jsonsink.inputs.some_measurement = 11.4
1870+
>>> jsonsink.run() # doctest: +SKIP
1871+
1872+
"""
1873+
input_spec = JSONFileSinkInputSpec
1874+
output_spec = JSONFileSinkOutputSpec
1875+
1876+
def __init__(self, input_names, **inputs):
1877+
super(JSONFileSink, self).__init__(**inputs)
1878+
self._input_names = filename_to_list(input_names)
1879+
add_traits(self.inputs, [name for name in self._input_names])
1880+
1881+
def _list_outputs(self):
1882+
import json
1883+
import os.path as op
1884+
if not isdefined(self.inputs.out_file):
1885+
out_file = op.abspath('datasink.json')
1886+
else:
1887+
out_file = self.inputs.out_file
1888+
1889+
out_dict = dict()
1890+
for name in self._input_names:
1891+
val = getattr(self.inputs, name)
1892+
val = val if isdefined(val) else 'undefined'
1893+
out_dict[name] = val
1894+
1895+
with open(out_file, 'w') as f:
1896+
json.dump(out_dict, f)
1897+
outputs = self.output_spec().get()
1898+
outputs['out_file'] = out_file
1899+
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 JSONFileSink
4+
5+
def test_JSONFileSink_inputs():
6+
input_map = dict(ignore_exception=dict(nohash=True,
7+
usedefault=True,
8+
),
9+
out_file=dict(),
10+
)
11+
inputs = JSONFileSink.input_spec()
12+
13+
for key, metadata in input_map.items():
14+
for metakey, value in metadata.items():
15+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
16+
17+
def test_JSONFileSink_outputs():
18+
output_map = dict(out_file=dict(),
19+
)
20+
outputs = JSONFileSink.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+

0 commit comments

Comments
 (0)