Skip to content

Commit a80d2be

Browse files
committed
RF - move fieldname recoding to header class
The field_recoders is a bit specific to headers; move to the header classes, leave wrapstruct with generic stub interface to get_value_label
1 parent 0e843c0 commit a80d2be

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

nibabel/analyze.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,20 @@ def get_value_label(self, fieldname):
357357
label : str
358358
label for code value in header field `fieldname`
359359
360+
Raises
361+
------
362+
ValueError : if field is not coded
363+
360364
Examples
361365
--------
362366
>>> hdr = AnalyzeHeader()
363367
>>> hdr.get_value_label('datatype')
364368
'float32'
365369
'''
366-
return super(AnalyzeHeader, self).get_value_label(fieldname)
370+
if not fieldname in self._field_recoders:
371+
raise ValueError('%s not a coded field' % fieldname)
372+
code = int(self._structarr[fieldname])
373+
return self._field_recoders[fieldname].label[code]
367374

368375
@classmethod
369376
def from_header(klass, header=None, check=True):

nibabel/tests/test_wrapstruct.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646

4747
class MyWrapStruct(WrapStruct):
48-
""" An example binary header class """
49-
_field_recoders = {}
48+
""" An example wrapped struct class """
49+
_field_recoders = {} # for recoding values for str
5050
template_dtype = np.dtype([('an_integer', 'i2'), ('a_str', 'S10')])
5151

5252
@classmethod
@@ -68,6 +68,12 @@ def _get_checks(klass):
6868
return (klass._chk_integer,
6969
klass._chk_string)
7070

71+
def get_value_label(self, fieldname):
72+
if not fieldname in self._field_recoders:
73+
raise ValueError('%s not a coded field' % fieldname)
74+
code = int(self._structarr[fieldname])
75+
return self._field_recoders[fieldname].label[code]
76+
7177
''' Check functions in format expected by BatteryRunner class '''
7278
@staticmethod
7379
def _chk_integer(hdr, fix=False):

nibabel/wrapstruct.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
.__str__
5151
.__eq__
5252
.__ne__
53+
.get_value_label(name)
5354
5455
Class methods::
5556
@@ -116,7 +117,6 @@ class WrapStructError(Exception):
116117

117118

118119
class WrapStruct(object):
119-
_field_recoders = {}
120120
# placeholder datatype
121121
template_dtype = np.dtype([('integer', 'i2')])
122122

@@ -427,11 +427,12 @@ def get_value_label(self, fieldname):
427427
-------
428428
label : str
429429
label for code value in field `fieldname`
430+
431+
Raises
432+
------
433+
ValueError : if fieldname not coded
430434
'''
431-
if not fieldname in self._field_recoders:
432-
raise ValueError('%s not a coded field' % fieldname)
433-
code = int(self._structarr[fieldname])
434-
return self._field_recoders[fieldname].label[code]
435+
raise ValueError('%s not a coded field' % fieldname)
435436

436437
def as_byteswapped(self, endianness=None):
437438
''' return new byteswapped object with given ``endianness``

0 commit comments

Comments
 (0)