Skip to content

Commit 3bac385

Browse files
demianweffigies
authored andcommitted
Changed Cifti2Vertices such that it behaves like a sequence. Added corresponding tests
1 parent 45cc854 commit 3bac385

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,35 +439,60 @@ def _to_xml_element(self):
439439
return vox_ind
440440

441441

442-
class Cifti2Vertices(xml.XmlSerializable):
442+
class Cifti2Vertices(xml.XmlSerializable, collections.MutableSequence):
443443
"""Cifti2 vertices - association of brain structure and a list of vertices
444444
445445
"Contains a BrainStructure type and a list of vertex indices within a
446446
Parcel."
447447
448448
Attribute descriptions are from the CIFTI-2 spec dated 2014-03-01.
449+
The class behaves like a list of Vertex indices
450+
(which are independent for each surface, and zero-based)
449451
450452
Attributes
451453
----------
452454
brain_structure : str
453455
A string from the BrainStructure list to identify what surface this
454456
vertex list is from (usually left cortex, right cortex, or cerebellum).
455-
vertices : ndarray shape (N,)
456-
Vertex indices (which are independent for each surface, and zero-based)
457457
"""
458458
def __init__(self, brain_structure=None, vertices=None):
459-
self.vertices = vertices
459+
self._vertices = []
460+
if vertices is not None:
461+
self.extend(vertices)
462+
460463
self.brain_structure = brain_structure
461464

465+
def __len__(self):
466+
return len(self._vertices)
467+
468+
def __delitem__(self, index):
469+
del self._vertices[index]
470+
471+
def __getitem__(self, index):
472+
return self._vertices[index]
473+
474+
def __setitem__(self, index, value):
475+
try:
476+
value = int(value)
477+
self._vertices[index] = value
478+
except ValueError:
479+
raise ValueError('value must be an int')
480+
481+
def insert(self, index, value):
482+
try:
483+
value = int(value)
484+
self._vertices.insert(index, value)
485+
except ValueError:
486+
raise ValueError('value must be an int')
487+
462488
def _to_xml_element(self):
463489
if self.brain_structure is None:
464490
raise CIFTI2HeaderError('Vertices element require a BrainStructure')
465491

466492
vertices = xml.Element('Vertices')
467493
vertices.attrib['BrainStructure'] = str(self.brain_structure)
468494

469-
if self.vertices is not None:
470-
vertices.text = ' '.join(self.vertices.astype(str))
495+
vertices.text = ' '.join([str(i) for i in self])
471496
return vertices
472497

473498

nibabel/cifti2/parse_cifti2_fast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def flush_chardata(self):
538538
# conversion to numpy array
539539
c = BytesIO(data.strip().encode('utf-8'))
540540
vertices = self.struct_state[-1]
541-
vertices.vertices = np.genfromtxt(c, dtype=np.int)
541+
vertices.extend(np.genfromtxt(c, dtype=np.int))
542542
c.close()
543543

544544
elif self.write_to == 'VoxelIndices':

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ def test_cifti2_vertices():
134134
vs.to_xml().decode('utf-8'),
135135
'<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER" />'
136136
)
137-
vs.vertices = np.array([0, 1, 2])
137+
assert_equal(len(vs), 0)
138+
vs.extend(np.array([0, 1, 2]))
139+
assert_equal(len(vs), 3)
140+
138141
assert_equal(
139142
vs.to_xml().decode('utf-8'),
140143
'<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER">0 1 2</Vertices>'
@@ -159,7 +162,6 @@ def test_cifti2_vertexindices():
159162
'<VertexIndices>0 1 2</VertexIndices>'
160163
)
161164

162-
163165
def test_cifti2_cifti2voxelindicesijk():
164166
vi = ci.Cifti2VoxelIndicesIJK()
165167
assert_equal(len(vi), 0)

0 commit comments

Comments
 (0)