Skip to content

Commit 77c5baa

Browse files
authored
Merge pull request #739 from MichielCottaar/bug_single_vertex
BF: allow for BrainModels or Parcels to contain a single vertex
2 parents de09d3a + 3a1a94b commit 77c5baa

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

nibabel/cifti2/parse_cifti2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def flush_chardata(self):
517517
# conversion to numpy array
518518
c = BytesIO(data.strip().encode('utf-8'))
519519
vertices = self.struct_state[-1]
520-
vertices.extend(np.loadtxt(c, dtype=np.int))
520+
vertices.extend(np.loadtxt(c, dtype=np.int, ndmin=1))
521521
c.close()
522522

523523
elif self.write_to == 'VoxelIndices':
@@ -531,7 +531,7 @@ def flush_chardata(self):
531531
# conversion to numpy array
532532
c = BytesIO(data.strip().encode('utf-8'))
533533
index = self.struct_state[-1]
534-
index.extend(np.loadtxt(c, dtype=np.int))
534+
index.extend(np.loadtxt(c, dtype=np.int, ndmin=1))
535535
c.close()
536536

537537
elif self.write_to == 'TransformMatrix':

nibabel/cifti2/tests/test_new_cifti2.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
[61, 59, 60],
2727
[61, 60, 59],
2828
[80, 90, 92]]),
29-
('CIFTI_STRUCTURE_CORTEX_LEFT', [0, 1000, 1301, 19972, 27312])]
29+
('CIFTI_STRUCTURE_CORTEX_LEFT', [0, 1000, 1301, 19972, 27312]),
30+
('CIFTI_STRUCTURE_CORTEX_RIGHT', [207])
31+
]
3032

3133

3234
def create_geometry_map(applies_to_matrix_dimension):
@@ -35,25 +37,34 @@ def create_geometry_map(applies_to_matrix_dimension):
3537
model_type='CIFTI_MODEL_TYPE_VOXELS',
3638
brain_structure=brain_models[0][0],
3739
voxel_indices_ijk=voxels)
40+
3841
vertices = ci.Cifti2VertexIndices(np.array(brain_models[1][1]))
3942
left_cortex = ci.Cifti2BrainModel(index_offset=4, index_count=5,
4043
model_type='CIFTI_MODEL_TYPE_SURFACE',
4144
brain_structure=brain_models[1][0],
4245
vertex_indices=vertices)
4346
left_cortex.surface_number_of_vertices = number_of_vertices
47+
48+
vertices = ci.Cifti2VertexIndices(np.array(brain_models[2][1]))
49+
right_cortex = ci.Cifti2BrainModel(index_offset=9, index_count=1,
50+
model_type='CIFTI_MODEL_TYPE_SURFACE',
51+
brain_structure=brain_models[2][0],
52+
vertex_indices=vertices)
53+
right_cortex.surface_number_of_vertices = number_of_vertices
54+
4455
volume = ci.Cifti2Volume(dimensions,
4556
ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ(-3,
4657
affine))
4758
return ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension,
4859
'CIFTI_INDEX_TYPE_BRAIN_MODELS',
49-
maps=[left_thalamus, left_cortex, volume])
60+
maps=[left_thalamus, left_cortex, right_cortex, volume])
5061

5162

5263
def check_geometry_map(mapping):
5364
assert_equal(mapping.indices_map_to_data_type,
5465
'CIFTI_INDEX_TYPE_BRAIN_MODELS')
55-
assert_equal(len(list(mapping.brain_models)), 2)
56-
left_thalamus, left_cortex = mapping.brain_models
66+
assert_equal(len(list(mapping.brain_models)), 3)
67+
left_thalamus, left_cortex, right_cortex = mapping.brain_models
5768

5869
assert_equal(left_thalamus.index_offset, 0)
5970
assert_equal(left_thalamus.index_count, 4)
@@ -71,9 +82,18 @@ def check_geometry_map(mapping):
7182
assert_equal(left_cortex.vertex_indices._indices, brain_models[1][1])
7283
assert_equal(left_cortex.surface_number_of_vertices, number_of_vertices)
7384

85+
assert_equal(right_cortex.index_offset, 9)
86+
assert_equal(right_cortex.index_count, 1)
87+
assert_equal(right_cortex.model_type, 'CIFTI_MODEL_TYPE_SURFACE')
88+
assert_equal(right_cortex.brain_structure, brain_models[2][0])
89+
assert_equal(right_cortex.voxel_indices_ijk, None)
90+
assert_equal(right_cortex.vertex_indices._indices, brain_models[2][1])
91+
assert_equal(right_cortex.surface_number_of_vertices, number_of_vertices)
92+
7493
assert_equal(mapping.volume.volume_dimensions, dimensions)
7594
assert_true((mapping.volume.transformation_matrix_voxel_indices_ijk_to_xyz.matrix == affine).all())
7695

96+
7797
parcels = [('volume_parcel', ([[60, 60, 60],
7898
[61, 59, 60],
7999
[61, 60, 59],
@@ -84,7 +104,10 @@ def check_geometry_map(mapping):
84104
[0, 100, 381]))),
85105
('mixed_parcel', ([[71, 81, 39],
86106
[53, 21, 91]],
87-
('CIFTI_STRUCTURE_CORTEX_LEFT', [71, 88, 999])))]
107+
('CIFTI_STRUCTURE_CORTEX_LEFT', [71, 88, 999]))),
108+
('single_element', ([[71, 81, 39]],
109+
('CIFTI_STRUCTURE_CORTEX_LEFT', [40]))),
110+
]
88111

89112

90113
def create_parcel_map(applies_to_matrix_dimension):
@@ -109,7 +132,7 @@ def create_parcel_map(applies_to_matrix_dimension):
109132

110133
def check_parcel_map(mapping):
111134
assert_equal(mapping.indices_map_to_data_type, 'CIFTI_INDEX_TYPE_PARCELS')
112-
assert_equal(len(list(mapping.parcels)), 3)
135+
assert_equal(len(list(mapping.parcels)), len(parcels))
113136
for (name, elements), parcel in zip(parcels, mapping.parcels):
114137
assert_equal(parcel.name, name)
115138
idx_surface = 0

0 commit comments

Comments
 (0)