Skip to content

Commit a3b3f41

Browse files
author
Ben Cipollini
committed
RF: make meta, numDA, and labeltable into properties.
1 parent 593dce2 commit a3b3f41

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

nibabel/gifti/gifti.py

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -344,63 +344,48 @@ def get_metadata(self):
344344

345345

346346
class GiftiImage(object):
347-
348-
numDA = int
349-
version = str
350-
filename = str
351-
352347
def __init__(self, meta=None, labeltable=None, darrays=None,
353348
version="1.0"):
354349
if darrays is None:
355350
darrays = []
356-
self.darrays = darrays
357351
if meta is None:
358-
self.meta = GiftiMetaData()
359-
else:
360-
self.meta = meta
352+
meta = GiftiMetaData()
361353
if labeltable is None:
362-
self.labeltable = GiftiLabelTable()
363-
else:
364-
self.labeltable = labeltable
365-
self.numDA = len(self.darrays)
354+
labeltable = GiftiLabelTable()
355+
356+
self._labeltable = labeltable
357+
self._meta = meta
358+
359+
self.darrays = darrays
366360
self.version = version
367361

368-
# @classmethod
369-
# def from_array(cls):
370-
# pass
371-
#def GiftiImage_fromarray(data, intent = GiftiIntentCode.NIFTI_INTENT_NONE, encoding=GiftiEncoding.GIFTI_ENCODING_B64GZ, endian = GiftiEndian.GIFTI_ENDIAN_LITTLE):
372-
# """ Returns a GiftiImage from a Numpy array with a given intent code and
373-
# encoding """
374-
375-
# @classmethod
376-
# def from_vertices_and_triangles(cls):
377-
# pass
378-
# def from_vertices_and_triangles(cls, vertices, triangles, coordsys = None, \
379-
# encoding = GiftiEncoding.GIFTI_ENCODING_B64GZ,\
380-
# endian = GiftiEndian.GIFTI_ENDIAN_LITTLE):
381-
# """ Returns a GiftiImage from two numpy arrays representing the vertices
382-
# and the triangles. Additionally defining the coordinate system and encoding """
383-
384-
def get_labeltable(self):
385-
return self.labeltable
386-
387-
def set_labeltable(self, labeltable):
362+
@property
363+
def numDA(self):
364+
return len(self.darrays)
365+
366+
@property
367+
def labeltable(self):
368+
return self._labeltable
369+
370+
@labeltable.setter
371+
def labeltable(self, labeltable):
388372
""" Set the labeltable for this GiftiImage
389373
390374
Parameters
391375
----------
392376
labeltable : GiftiLabelTable
393377
394378
"""
395-
if isinstance(labeltable, GiftiLabelTable):
396-
self.labeltable = labeltable
397-
else:
398-
print("Not a valid GiftiLabelTable instance")
379+
if not isinstance(labeltable, GiftiLabelTable):
380+
raise ValueError("Not a valid GiftiLabelTable instance")
381+
self._labeltable = labeltable
399382

400-
def get_metadata(self):
401-
return self.meta
383+
@property
384+
def meta(self):
385+
return self._meta
402386

403-
def set_metadata(self, meta):
387+
@meta.setter
388+
def meta(self, meta):
404389
""" Set the metadata for this GiftiImage
405390
406391
Parameters
@@ -411,13 +396,10 @@ def set_metadata(self, meta):
411396
-------
412397
None
413398
"""
414-
if isinstance(meta, GiftiMetaData):
415-
self.meta = meta
416-
print("New Metadata set. Be aware of changing "
417-
"coordinate transformation!")
418-
else:
419-
print("Not a valid GiftiMetaData instance")
420-
399+
if not isinstance(meta, GiftiMetaData):
400+
raise ValueError("Not a valid GiftiMetaData instance")
401+
self._meta = meta
402+
421403
def add_gifti_data_array(self, dataarr):
422404
""" Adds a data array to the GiftiImage
423405
@@ -427,22 +409,19 @@ def add_gifti_data_array(self, dataarr):
427409
"""
428410
if isinstance(dataarr, GiftiDataArray):
429411
self.darrays.append(dataarr)
430-
self.numDA += 1
431412
else:
432413
print("dataarr paramater must be of tzpe GiftiDataArray")
433414

434415
def remove_gifti_data_array(self, ith):
435416
""" Removes the ith data array element from the GiftiImage """
436417
self.darrays.pop(ith)
437-
self.numDA -= 1
438418

439419
def remove_gifti_data_array_by_intent(self, intent):
440420
""" Removes all the data arrays with the given intent type """
441421
intent2remove = intent_codes.code[intent]
442422
for dele in self.darrays:
443423
if dele.intent == intent2remove:
444424
self.darrays.remove(dele)
445-
self.numDA -= 1
446425

447426
def get_arrays_from_intent(self, intent):
448427
""" Returns a a list of GiftiDataArray elements matching

nibabel/gifti/parse_gifti_fast.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ def StartElementHandler(self, name, attrs):
108108
self.img = GiftiImage()
109109
if 'Version' in attrs:
110110
self.img.version = attrs['Version']
111-
if 'NumberOfDataArrays' in attrs:
112-
self.img.numDA = int(attrs['NumberOfDataArrays'])
113-
self.count_da = False
114111

115112
self.fsm_state.append('GIFTI')
116113
elif name == 'MetaData':
@@ -234,8 +231,6 @@ def EndElementHandler(self, name):
234231
self.img.labeltable = self.lata
235232
self.lata = None
236233
elif name == 'DataArray':
237-
if self.count_da:
238-
self.img.numDA += 1
239234
self.fsm_state.pop()
240235
elif name == 'CoordinateSystemTransformMatrix':
241236
self.fsm_state.pop()

nibabel/gifti/tests/test_giftiio.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def test_read_ordering():
112112
def test_metadata():
113113
for i, dat in enumerate(datafiles):
114114
img = gi.read(dat)
115-
me = img.get_metadata()
116-
medat = me.get_metadata()
115+
me = img.meta
117116
assert_equal(numda[i], img.numDA)
118117
assert_equal(img.version,'1.0')
119118

@@ -217,11 +216,11 @@ def test_newmetadata():
217216
img = gi.GiftiImage()
218217
attr = gi.GiftiNVPairs(name = 'mykey', value = 'val1')
219218
newmeta = gi.GiftiMetaData(attr)
220-
img.set_metadata(newmeta)
219+
img.meta = newmeta
221220
myme = img.meta.get_metadata()
222221
assert_true('mykey' in myme)
223222
newmeta = gi.GiftiMetaData.from_dict( {'mykey1' : 'val2'} )
224-
img.set_metadata(newmeta)
223+
img.meta = newmeta
225224
myme = img.meta.get_metadata()
226225
assert_true('mykey1' in myme)
227226
assert_false('mykey' in myme)

0 commit comments

Comments
 (0)