28
28
from ..nifti2 import Nifti2Image
29
29
30
30
31
+ def _float_01 (val ):
32
+ out = float (val )
33
+ if out < 0 or out > 1 :
34
+ raise ValueError ('Float must be between 0 and 1 inclusive' )
35
+ return out
36
+
37
+
31
38
class CIFTI2HeaderError (Exception ):
32
39
""" Error in CIFTI2 header
33
40
"""
@@ -171,26 +178,14 @@ def __setitem__(self, key, value):
171
178
if key != value .key :
172
179
raise ValueError ("The key and the label's key must agree" )
173
180
self ._labels [key ] = value
174
- else :
175
- try :
176
- key = int (key )
177
- v = (str (value [0 ]),) + tuple (float (v ) for v in value [1 :] if 0 <= float (v ) <= 1 )
178
- if len (v ) != 5 :
179
- raise ValueError
180
- except :
181
- raise ValueError (
182
- 'Key must be integer and value a string and 4-tuple of floats between 0 and 1'
183
- )
184
-
185
- label = Cifti2Label (
186
- key = key ,
187
- label = v [0 ],
188
- red = v [1 ],
189
- green = v [2 ],
190
- blue = v [3 ],
191
- alpha = v [4 ]
192
- )
193
- self ._labels [key ] = label
181
+ return
182
+ if len (value ) != 5 :
183
+ raise ValueError ('Value should be length 5' )
184
+ try :
185
+ self ._labels [key ] = Cifti2Label (* ([key ] + list (value )))
186
+ except ValueError :
187
+ raise ValueError ('Key should be int, value should be sequence '
188
+ 'of str and 4 floats between 0 and 1' )
194
189
195
190
def __delitem__ (self , key ):
196
191
del self ._labels [key ]
@@ -215,27 +210,26 @@ class Cifti2Label(xml.XmlSerializable):
215
210
216
211
Attributes
217
212
----------
218
- key : int
213
+ key : int, optional
219
214
Integer, data value which is assigned this name and color.
220
- label : str
215
+ label : str, optional
221
216
Name of the label.
222
- red : None or float
223
- Red color component for label.
224
- green : None or float
225
- Green color component for label.
226
- blue : None or float
227
- Blue color component for label.
228
- alpha : None or float
229
- Alpha color component for label.
217
+ red : float, optional
218
+ Red color component for label (between 0 and 1) .
219
+ green : float, optional
220
+ Green color component for label (between 0 and 1) .
221
+ blue : float, optional
222
+ Blue color component for label (between 0 and 1) .
223
+ alpha : float, optional
224
+ Alpha color component for label (between 0 and 1) .
230
225
"""
231
- def __init__ (self , key = 0 , label = '' , red = 0 , green = 0 , blue = 0 ,
232
- alpha = 0 ):
233
- self .key = key
234
- self .label = label
235
- self .red = red
236
- self .green = green
237
- self .blue = blue
238
- self .alpha = alpha
226
+ def __init__ (self , key = 0 , label = '' , red = 0. , green = 0. , blue = 0. , alpha = 0. ):
227
+ self .key = int (key )
228
+ self .label = str (label )
229
+ self .red = _float_01 (red )
230
+ self .green = _float_01 (green )
231
+ self .blue = _float_01 (blue )
232
+ self .alpha = _float_01 (alpha )
239
233
240
234
@property
241
235
def rgba (self ):
@@ -251,21 +245,20 @@ def _to_xml_element(self):
251
245
raise CIFTI2HeaderError ('The key must be an integer' )
252
246
for c_ in ('red' , 'blue' , 'green' , 'alpha' ):
253
247
try :
254
- v = float (getattr (self , c_ ))
255
- if not (0 <= v <= 1 ):
256
- raise ValueError
248
+ v = _float_01 (getattr (self , c_ ))
257
249
except ValueError :
258
250
raise CIFTI2HeaderError (
259
- 'Label invalid %s needs to be a float between 0 and 1. and it is %s' %
260
- (c_ , v )
251
+ 'Label invalid %s needs to be a float between 0 and 1. '
252
+ 'and it is %s' % (c_ , v )
261
253
)
262
254
263
255
lab = xml .Element ('Label' )
264
256
lab .attrib ['Key' ] = str (self .key )
265
257
lab .text = str (self .label )
266
258
267
259
for name in ('red' , 'green' , 'blue' , 'alpha' ):
268
- attr = str (getattr (self , name ))
260
+ val = getattr (self , name )
261
+ attr = '0' if val == 0 else '1' if val == 1 else str (val )
269
262
lab .attrib [name .capitalize ()] = attr
270
263
return lab
271
264
0 commit comments