Skip to content

Commit 07dcb61

Browse files
committed
Merge branch 'enh/ImproveJSONInterfaces' into enh/AntsForFMBworkflow
Conflicts: CHANGES
2 parents 8698d88 + 098f96f commit 07dcb61

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Next release
33

44
* ENH: Improved FieldMap-Based (FMB) workflow for correction of susceptibility distortions in EPI seqs.
55
(https://github.com/nipy/nipype/pull/1019)
6+
* ENH: Improve JSON interfaces: default settings when reading and consistent output creation
7+
when writing (https://github.com/nipy/nipype/pull/1047)
68
* ENH: New io interfaces for JSON files reading/writing (https://github.com/nipy/nipype/pull/1020)
79
* ENH: Enhanced openfmri script to support freesurfer linkage (https://github.com/nipy/nipype/pull/1037)
810
* BUG: matplotlib is supposed to be optional (https://github.com/nipy/nipype/pull/1003)

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': u'overrideMe', 'param3': 1.0}
1824+
>>> res = jsonSource.run()
1825+
>>> res.outputs.get()
1826+
{'param3': 1.0, 'param1': u'overrideMe'}
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+
{'param3': 1.0, 'param2': 4, 'param1': u'exampleStr'}
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

nipype/workflows/dmri/fsl/artifacts.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,8 @@ def sdc_fmb(name='fmb_correction',
554554
555555
"""
556556

557-
defaults = dict(delta_te=2.46e-3, echospacing=0.77e-3, acc_factor=3,
558-
enc_dir='AP')
559-
560-
delta_te = epi_params['echospacing'] / (1.0 * epi_params['acc_factor'])
557+
epi_defaults = {'delta_te': 2.46e-3, 'echospacing': 0.77e-3,
558+
'acc_factor': 3, 'enc_dir': u'AP'}
561559

562560
def _fix_enc_dir(enc_dir):
563561
enc_dir = enc_dir.lower()
@@ -579,7 +577,8 @@ def _fix_enc_dir(enc_dir):
579577
outputnode = pe.Node(niu.IdentityInterface(
580578
fields=['out_file', 'out_vsm', 'out_warp']), name='outputnode')
581579

582-
r_params = pe.Node(JSONFileGrabber(), name='SettingsGrabber')
580+
r_params = pe.Node(JSONFileGrabber(defaults=epi_defaults),
581+
name='SettingsGrabber')
583582
eff_echo = pe.Node(niu.Function(function=_eff_t_echo,
584583
input_names=['echospacing', 'acc_factor'],
585584
output_names=['eff_echo']), name='EffEcho')

0 commit comments

Comments
 (0)