-
Notifications
You must be signed in to change notification settings - Fork 15
/
utility.py
196 lines (160 loc) · 5.91 KB
/
utility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Interfaces under evaluation before upstreaming to nipype.interfaces.utility."""
from nipype.interfaces.io import add_traits
from nipype.interfaces.base import (
InputMultiObject,
Str,
DynamicTraitedSpec,
BaseInterface,
isdefined,
)
class _KeySelectInputSpec(DynamicTraitedSpec):
key = Str(mandatory=True, desc="selective key")
keys = InputMultiObject(Str, mandatory=True, min=1, desc="index of keys")
class _KeySelectOutputSpec(DynamicTraitedSpec):
key = Str(desc="propagates selected key")
class KeySelect(BaseInterface):
"""
An interface that operates similarly to an OrderedDict.
>>> ks = KeySelect(keys=['MNI152NLin6Asym', 'MNI152Lin', 'fsaverage'],
... fields=['field1', 'field2', 'field3'])
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'MNI152Lin'
>>> ks.run().outputs
<BLANKLINE>
field1 = mni
field2 = volume
field3 = False
key = MNI152Lin
<BLANKLINE>
>>> ks = KeySelect(fields=['field1', 'field2', 'field3'])
>>> ks.inputs.keys=['MNI152NLin6Asym', 'MNI152Lin', 'fsaverage']
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'MNI152Lin'
>>> ks.run().outputs
<BLANKLINE>
field1 = mni
field2 = volume
field3 = False
key = MNI152Lin
<BLANKLINE>
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'fsaverage'
>>> ks.run().outputs
<BLANKLINE>
field1 = freesurfer
field2 = surface
field3 = False
key = fsaverage
<BLANKLINE>
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False] # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
ValueError: Trying to set an invalid value
>>> ks.inputs.key = 'MNINLin2009cAsym'
Traceback (most recent call last):
ValueError: Selected key "MNINLin2009cAsym" not found in the index
>>> ks = KeySelect(fields=['field1', 'field2', 'field3'])
>>> ks.inputs.keys=['MNI152NLin6Asym']
>>> ks.inputs.field1 = ['fsl']
>>> ks.inputs.field2 = ['volume']
>>> ks.inputs.field3 = [True]
>>> ks.inputs.key = 'MNI152NLin6Asym'
>>> ks.run().outputs
<BLANKLINE>
field1 = fsl
field2 = volume
field3 = True
key = MNI152NLin6Asym
<BLANKLINE>
"""
input_spec = _KeySelectInputSpec
output_spec = _KeySelectOutputSpec
def __init__(self, keys=None, fields=None, **inputs):
"""
Instantiate a KeySelect utility interface.
Examples
--------
>>> ks = KeySelect(fields='field1')
>>> ks.inputs
<BLANKLINE>
field1 = <undefined>
key = <undefined>
keys = <undefined>
<BLANKLINE>
>>> ks = KeySelect(fields='field1', field1=['a', 'b'])
>>> ks.inputs
<BLANKLINE>
field1 = ['a', 'b']
key = <undefined>
keys = <undefined>
<BLANKLINE>
>>> ks = KeySelect()
Traceback (most recent call last):
ValueError: A list or multiplexed...
>>> ks = KeySelect(fields='key')
Traceback (most recent call last):
ValueError: Some fields are invalid...
"""
# Call constructor
super(KeySelect, self).__init__(**inputs)
# Handle and initiate fields
if not fields:
raise ValueError(
"A list or multiplexed fields must be provided at "
"instantiation time."
)
if isinstance(fields, str):
fields = [fields]
_invalid = set(self.input_spec.class_editable_traits()).intersection(fields)
if _invalid:
raise ValueError("Some fields are invalid (%s)." % ", ".join(_invalid))
self._fields = fields
# Attach events
self.inputs.on_trait_change(self._check_len)
if keys:
self.inputs.keys = keys
# Add fields in self._fields
add_traits(self.inputs, self._fields)
for in_field in set(self._fields).intersection(inputs.keys()):
setattr(self.inputs, in_field, inputs[in_field])
def _check_len(self, name, new):
if name == "keys":
nitems = len(new)
if len(set(new)) != nitems:
raise ValueError(
"Found duplicated entries in the index of ordered keys"
)
if not isdefined(self.inputs.keys):
return
if name == "key" and new not in self.inputs.keys:
raise ValueError('Selected key "%s" not found in the index' % new)
if name in self._fields:
if isinstance(new, str) or len(new) < 1:
raise ValueError(
'Trying to set an invalid value (%s) for input "%s"' % (new, name)
)
if len(new) != len(self.inputs.keys):
raise ValueError(
'Length of value (%s) for input field "%s" does not match '
"the length of the indexing list." % (new, name)
)
def _run_interface(self, runtime):
return runtime
def _list_outputs(self):
index = self.inputs.keys.index(self.inputs.key)
outputs = {k: getattr(self.inputs, k)[index] for k in self._fields}
outputs["key"] = self.inputs.key
return outputs
def _outputs(self):
base = super(KeySelect, self)._outputs()
base = add_traits(base, self._fields)
return base