Skip to content

Commit c2dbd16

Browse files
demianweffigies
authored andcommitted
Icreased testing coverage. Cleaned up code
1 parent 0ba811c commit c2dbd16

File tree

5 files changed

+79
-54
lines changed

5 files changed

+79
-54
lines changed

nibabel/cifti2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
Cifti2MatrixIndicesMap, Cifti2NamedMap, Cifti2Parcel,
2424
Cifti2Surface, Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ,
2525
Cifti2Vertices, Cifti2Volume, CIFTI_BrainStructures, CIFTI2HeaderError,
26-
Cifti2DenseDataSeries, CIFTI_MODEL_TYPES, load, save)
26+
CIFTI_MODEL_TYPES, load, save)

nibabel/cifti2/cifti2.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def __setitem__(self, index, value):
404404
value = [int(v) for v in value]
405405
if len(value) != 3:
406406
raise ValueError('rows are triples of ints')
407-
self._indices[index[0]] = value
407+
self._indices[index] = value
408408
except ValueError:
409409
raise ValueError('value must be a triple of ints')
410410
elif len(index) == 2:
@@ -521,8 +521,8 @@ def voxel_indices_ijk(self):
521521
def voxel_indices_ijk(self, value):
522522
self._voxel_indices_ijk = _value_if_klass(value, Cifti2VoxelIndicesIJK)
523523

524-
def add_cifti_vertices(self, vertices):
525-
""" Adds a vertices to the Cifti2Parcel
524+
def append_cifti_vertices(self, vertices):
525+
""" Appends a Cifti2Vertices element to the Cifti2Parcel
526526
527527
Parameters
528528
----------
@@ -532,8 +532,8 @@ def add_cifti_vertices(self, vertices):
532532
raise TypeError("Not a valid Cifti2Vertices instance")
533533
self.vertices.append(vertices)
534534

535-
def remove_cifti2_vertices(self, ith):
536-
""" Removes the ith vertices element from the Cifti2Parcel """
535+
def pop_cifti2_vertices(self, ith):
536+
""" Pops the ith vertices element from the Cifti2Parcel """
537537
self.vertices.pop(ith)
538538

539539
def _to_xml_element(self):
@@ -871,10 +871,10 @@ def metadata(self, meta):
871871
-------
872872
None
873873
"""
874-
self._meta = _value_if_klass(meta, Cifti2MetaData, False)
874+
self._meta = _value_if_klass(meta, Cifti2MetaData, True)
875875

876876
def __setitem__(self, key, value):
877-
if not isinstance(mim, Cifti2MatrixIndicesMap):
877+
if not isinstance(value, Cifti2MatrixIndicesMap):
878878
raise TypeError("Not a valid Cifti2MatrixIndicesMap instance")
879879
self._mims[key] = value
880880

@@ -1055,35 +1055,35 @@ def to_file_map(self, file_map=None):
10551055
img.to_file_map(file_map or self.file_map)
10561056

10571057

1058-
class Cifti2DenseDataSeriesHeader(Cifti2Header):
1059-
@classmethod
1060-
def may_contain_header(klass, binaryblock):
1061-
from .parse_cifti2_fast import _Cifti2DenseDataSeriesNiftiHeader
1062-
return _Cifti2DenseDataSeriesNiftiHeader.may_contain_header(binaryblock)
1063-
1064-
1065-
class Cifti2DenseDataSeries(Cifti2Image):
1066-
"""Class to handle Dense Data Series
1058+
# class Cifti2DenseDataSeriesHeader(Cifti2Header):
1059+
# @classmethod
1060+
# def may_contain_header(klass, binaryblock):
1061+
# from .parse_cifti2_fast import _Cifti2DenseDataSeriesNiftiHeader
1062+
# return _Cifti2DenseDataSeriesNiftiHeader.may_contain_header(binaryblock)
10671063

1068-
Dense Data Series
1069-
-----------------
10701064

1071-
Intent_code: 3002, NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES
1072-
Intent_name: ConnDenseSeries
1073-
File extension: .dtseries.nii
1074-
AppliesToMatrixDimension 0: series
1075-
AppliesToMatrixDimension 1: brain models
1076-
1077-
This file type represents data points in a series for every vertex and voxel
1078-
in the mapping. A row is a complete data series,for a single vertex or
1079-
voxel in the mapping that applies along the second dimension. A data series
1080-
is often a timeseries, but it can also represent other data types such as a
1081-
series of sampling depths along the surface normal from the white to pial
1082-
surface. It retains the 't' in dtseries from CIFTI-1 naming conventions.
1083-
"""
1084-
header_class = Cifti2DenseDataSeriesHeader
1085-
valid_exts = ('.dtseries.nii',)
1086-
files_types = (('image', '.dtseries.nii'),)
1065+
# class Cifti2DenseDataSeries(Cifti2Image):
1066+
# """Class to handle Dense Data Series
1067+
#
1068+
# Dense Data Series
1069+
# -----------------
1070+
#
1071+
# Intent_code: 3002, NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES
1072+
# Intent_name: ConnDenseSeries
1073+
# File extension: .dtseries.nii
1074+
# AppliesToMatrixDimension 0: series
1075+
# AppliesToMatrixDimension 1: brain models
1076+
#
1077+
# This file type represents data points in a series for every vertex and voxel
1078+
# in the mapping. A row is a complete data series,for a single vertex or
1079+
# voxel in the mapping that applies along the second dimension. A data series
1080+
# is often a timeseries, but it can also represent other data types such as a
1081+
# series of sampling depths along the surface normal from the white to pial
1082+
# surface. It retains the 't' in dtseries from CIFTI-1 naming conventions.
1083+
# """
1084+
# header_class = Cifti2DenseDataSeriesHeader
1085+
# valid_exts = ('.dtseries.nii',)
1086+
# files_types = (('image', '.dtseries.nii'),)
10871087

10881088

10891089
def load(filename):

nibabel/cifti2/parse_cifti2_fast.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def StartElementHandler(self, name, attrs):
327327
raise CIFTI2HeaderError(
328328
'BrainStructure for this Vertices element is not valid'
329329
)
330-
parcel.add_cifti_vertices(vertices)
330+
parcel.append_cifti_vertices(vertices)
331331
self.fsm_state.append('Vertices')
332332
self.struct_state.append(vertices)
333333
self.write_to = 'Vertices'
@@ -576,10 +576,10 @@ def pending_data(self):
576576
return self._char_blocks is not None
577577

578578

579-
class _Cifti2DenseDataSeriesNiftiHeader(_Cifti2AsNiftiHeader):
580-
581-
@classmethod
582-
def _valid_intent_code(klass, intent_code):
583-
""" Return True if `intent_code` matches our class `klass`
584-
"""
585-
return intent_code == 3002
579+
# class _Cifti2DenseDataSeriesNiftiHeader(_Cifti2AsNiftiHeader):
580+
#
581+
# @classmethod
582+
# def _valid_intent_code(klass, intent_code):
583+
# """ Return True if `intent_code` matches our class `klass`
584+
# """
585+
# return intent_code == 3002

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def compare_xml_leaf(str1, str2):
2222
if len(x1) > 0 or len(x2) > 0:
2323
raise ValueError
2424

25-
return (x1.tag == x2.tag) and (x1.attrib and x2.attrib)
25+
test = (x1.tag == x2.tag) and (x1.attrib == x2.attrib) and (x1.text == x2.text)
26+
print((x1.tag, x1.attrib, x1.text))
27+
print((x2.tag, x2.attrib, x2.text))
28+
return test
2629

2730
def test_cifti2_metadata():
2831
md = ci.Cifti2MetaData()
@@ -120,11 +123,7 @@ def test_cifti2_label():
120123
def test_cifti2_parcel():
121124
pl = ci.Cifti2Parcel()
122125
assert_raises(ci.CIFTI2HeaderError, pl.to_xml)
123-
assert_raises(TypeError, pl.add_cifti_vertices, None)
124-
125-
def test_cifti2_voxelindicesijk():
126-
vi = ci.Cifti2VoxelIndicesIJK()
127-
assert_raises(ci.CIFTI2HeaderError, vi.to_xml)
126+
assert_raises(TypeError, pl.append_cifti_vertices, None)
128127

129128
def test_cifti2_vertices():
130129
vs = ci.Cifti2Vertices()
@@ -143,10 +142,18 @@ def test_cifti2_vertices():
143142
'<Vertices BrainStructure="CIFTI_STRUCTURE_OTHER">0 1 2</Vertices>'
144143
)
145144

145+
vs[0] = 10
146+
assert_equal(vs[0], 10)
147+
assert_equal(len(vs), 3)
148+
146149
def test_cifti2_transformationmatrixvoxelindicesijktoxyz():
147150
tr = ci.Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ()
148151
assert_raises(ci.CIFTI2HeaderError, tr.to_xml)
149152

153+
def test_cifti2_surface():
154+
s = ci.Cifti2Surface()
155+
assert_raises(ci.CIFTI2HeaderError, s.to_xml)
156+
150157
def test_cifti2_volume():
151158
vo = ci.Cifti2Volume()
152159
assert_raises(ci.CIFTI2HeaderError, vo.to_xml)
@@ -161,8 +168,16 @@ def test_cifti2_vertexindices():
161168
vi.to_xml().decode('utf-8'),
162169
'<VertexIndices>0 1 2</VertexIndices>'
163170
)
171+
assert_raises(ValueError, vi.__setitem__, 0, 'a')
172+
vi[0] = 10
173+
assert_equal(vi[0], 10)
174+
assert_equal(len(vi), 3)
175+
176+
177+
def test_cifti2_voxelindicesijk():
178+
vi = ci.Cifti2VoxelIndicesIJK()
179+
assert_raises(ci.CIFTI2HeaderError, vi.to_xml)
164180

165-
def test_cifti2_cifti2voxelindicesijk():
166181
vi = ci.Cifti2VoxelIndicesIJK()
167182
assert_equal(len(vi), 0)
168183
assert_raises(ci.CIFTI2HeaderError, vi.to_xml)
@@ -177,7 +192,11 @@ def test_cifti2_cifti2voxelindicesijk():
177192
assert_equal(len(vi), 2)
178193

179194
assert_equal(vi[1], [3, 4, 5])
195+
vi[1] = [3, 4, 6]
196+
assert_equal(vi[1], [3, 4, 6])
197+
assert_raises(ValueError, vi.__setitem__, 1, [2, 3])
180198
assert_equal(vi[1, 1], 4)
199+
assert_raises(ValueError, vi.__setitem__, [1, 1], 'a')
181200
assert_equal(vi[0, 1:], [1, 2])
182201
vi[0, 1] = 10
183202
assert_equal(vi[0, 1], 10)
@@ -190,10 +209,9 @@ def test_cifti2_cifti2voxelindicesijk():
190209
assert_raises(ValueError, vi.__getitem__, (0, 0, 0))
191210
assert_raises(ValueError, vi.__setitem__, (0, 0, 0), 0)
192211

193-
194212
assert_equal(
195213
vi.to_xml().decode('utf-8'),
196-
'<VoxelIndicesIJK>0 1 2\n3 4 5</VoxelIndicesIJK>'
214+
'<VoxelIndicesIJK>0 1 2\n3 4 6</VoxelIndicesIJK>'
197215
)
198216

199217
def test_matrixindicesmap():
@@ -225,6 +243,13 @@ def test_matrixindicesmap():
225243

226244
assert_raises(ValueError, setattr, mim, 'volume', parcel)
227245

246+
def test_matrix():
247+
m = ci.Cifti2Matrix()
248+
assert_raises(TypeError, m, setattr, 'metadata', ci.Cifti2Parcel())
249+
assert_raises(TypeError, m.__setitem__, 0, ci.Cifti2Parcel())
250+
assert_raises(TypeError, m.insert, 0, ci.Cifti2Parcel())
251+
252+
228253
def test_underscoring():
229254
# Pairs taken from inflection tests
230255
# https://github.com/jpvanhal/inflection/blob/663982e/test_inflection.py#L113-L125

nibabel/imageclasses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
''' Define supported image classes and names '''
1010

1111
from .analyze import AnalyzeImage
12-
from .cifti2 import Cifti2Image, Cifti2DenseDataSeries
12+
from .cifti2 import Cifti2Image
1313
from .freesurfer import MGHImage
1414
from .gifti import GiftiImage
1515
from .minc1 import Minc1Image
@@ -28,7 +28,7 @@
2828

2929
# Ordered by the load/save priority.
3030
all_image_classes = [Nifti1Pair, Nifti1Image, Nifti2Pair,
31-
Cifti2DenseDataSeries, Cifti2Image, Nifti2Image, # Cifti2 before Nifti2
31+
Cifti2Image, Nifti2Image, # Cifti2 before Nifti2
3232
Spm2AnalyzeImage, Spm99AnalyzeImage, AnalyzeImage,
3333
Minc1Image, Minc2Image, MGHImage,
3434
PARRECImage, GiftiImage]

0 commit comments

Comments
 (0)