Skip to content

Commit 0df5209

Browse files
committed
a fix for internal variable name and more tests
1 parent a4a6c8e commit 0df5209

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

nibabel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from .nifti2 import Nifti2Header, Nifti2Image, Nifti2Pair
5252
from .minc1 import Minc1Image
5353
from .minc2 import Minc2Image
54+
from .cifti2 import Cifti2Header, Cifti2Image
5455
# Deprecated backwards compatiblity for MINC1
5556
from .deprecated import ModuleProxy as _ModuleProxy
5657
minc = _ModuleProxy('nibabel.minc')

nibabel/cifti2/cifti2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,12 @@ class Cifti2Parcel(xml.XmlSerializable):
603603
"""
604604
def __init__(self, name=None, voxel_indices_ijk=None, vertices=None):
605605
self.name = name
606-
self.voxel_indices_ijk = voxel_indices_ijk
606+
self._voxel_indices_ijk = voxel_indices_ijk
607607
self.vertices = vertices if vertices is not None else []
608+
for val in self.vertices:
609+
if not isinstance(val, Cifti2Vertices):
610+
raise ValueError(('Cifti2Parcel vertices must be instances of '
611+
'Cifti2Vertices'))
608612

609613
@property
610614
def voxel_indices_ijk(self):

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from nibabel import cifti2 as ci
99
from nibabel.nifti2 import Nifti2Header
10-
from nibabel.cifti2.cifti2 import _float_01
10+
from nibabel.cifti2.cifti2 import _float_01, _value_if_klass, Cifti2HeaderError
1111

1212
from nose.tools import assert_true, assert_equal, assert_raises, assert_is_none
1313

@@ -26,6 +26,12 @@ def compare_xml_leaf(str1, str2):
2626
return test
2727

2828

29+
def test_value_if_klass():
30+
assert_equal(_value_if_klass(None, list), None)
31+
assert_equal(_value_if_klass([1], list), [1])
32+
assert_raises(ValueError, _value_if_klass, 1, list)
33+
34+
2935
def test_cifti2_metadata():
3036
md = ci.Cifti2MetaData(metadata={'a': 'aval'})
3137
assert_equal(len(md), 1)
@@ -83,6 +89,7 @@ def test_cifti2_labeltable():
8389
lt = ci.Cifti2LabelTable()
8490
assert_equal(len(lt), 0)
8591
assert_raises(ci.Cifti2HeaderError, lt.to_xml)
92+
assert_raises(ci.Cifti2HeaderError, lt._to_xml_element)
8693

8794
label = ci.Cifti2Label(label='Test', key=0)
8895
lt[0] = label
@@ -146,6 +153,17 @@ def test_cifti2_parcel():
146153
assert_raises(ci.Cifti2HeaderError, pl.to_xml)
147154
assert_raises(TypeError, pl.append_cifti_vertices, None)
148155

156+
assert_raises(ValueError, ci.Cifti2Parcel, **{'vertices': [1, 2, 3]})
157+
pl = ci.Cifti2Parcel(name='region',
158+
voxel_indices_ijk=ci.Cifti2VoxelIndicesIJK([[1, 2, 3]]),
159+
vertices=[ci.Cifti2Vertices([0, 1, 2])])
160+
pl.pop_cifti2_vertices(0)
161+
assert_equal(len(pl.vertices), 0)
162+
assert_equal(
163+
pl.to_xml().decode('utf-8'),
164+
'<Parcel Name="region"><VoxelIndicesIJK>1 2 3</VoxelIndicesIJK></Parcel>'
165+
)
166+
149167

150168
def test_cifti2_vertices():
151169
vs = ci.Cifti2Vertices()
@@ -158,7 +176,8 @@ def test_cifti2_vertices():
158176
assert_equal(len(vs), 0)
159177
vs.extend(np.array([0, 1, 2]))
160178
assert_equal(len(vs), 3)
161-
179+
assert_raises(ValueError, vs.__setitem__, 1, 'a')
180+
assert_raises(ValueError, vs.insert, 1, 'a')
162181
assert_equal(
163182
vs.to_xml().decode('utf-8'),
164183
'<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER">0 1 2</Vertices>'
@@ -167,6 +186,8 @@ def test_cifti2_vertices():
167186
vs[0] = 10
168187
assert_equal(vs[0], 10)
169188
assert_equal(len(vs), 3)
189+
vs = ci.Cifti2Vertices(vertices=[0, 1, 2])
190+
assert_equal(len(vs), 3)
170191

171192

172193
def test_cifti2_transformationmatrixvoxelindicesijktoxyz():
@@ -220,6 +241,8 @@ def test_cifti2_voxelindicesijk():
220241
assert_equal(vi[1], [3, 4, 5])
221242
vi[1] = [3, 4, 6]
222243
assert_equal(vi[1], [3, 4, 6])
244+
assert_raises(ValueError, vi.__setitem__, 'a', [1, 2, 3])
245+
assert_raises(TypeError, vi.__setitem__, [1, 2], [1, 2, 3])
223246
assert_raises(ValueError, vi.__setitem__, 1, [2, 3])
224247
assert_equal(vi[1, 1], 4)
225248
assert_raises(ValueError, vi.__setitem__, [1, 1], 'a')
@@ -239,6 +262,9 @@ def test_cifti2_voxelindicesijk():
239262
vi.to_xml().decode('utf-8'),
240263
'<VoxelIndicesIJK>0 1 2\n3 4 6</VoxelIndicesIJK>'
241264
)
265+
assert_raises(TypeError, ci.Cifti2VoxelIndicesIJK, [0, 1])
266+
vi = ci.Cifti2VoxelIndicesIJK([[1, 2, 3]])
267+
assert_equal(len(vi), 1)
242268

243269

244270
def test_matrixindicesmap():

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def main(**extra_args):
108108
pjoin('externals', 'tests', 'data', '*'),
109109
pjoin('nicom', 'tests', 'data', '*'),
110110
pjoin('gifti', 'tests', 'data', '*'),
111-
pjoin('cifti2', 'tests', 'data', '*'),
112111
pjoin('streamlines', 'tests', 'data', '*'),
113112
]},
114113
scripts = [pjoin('bin', 'parrec2nii'),

0 commit comments

Comments
 (0)