Skip to content

Commit 6fb4ea0

Browse files
committed
Merge pull request #355 from bcipolli/gifti-addtests
TST: Add GIFTI tests RF: Raise TypeErrors on mistyped arguments
2 parents 8040f6d + 287eae8 commit 6fb4ea0

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

nibabel/gifti/gifti.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def rgba(self, rgba):
155155
raise ValueError('rgba must be length 4.')
156156
self.red, self.green, self.blue, self.alpha = rgba
157157

158+
158159
def _arr2txt(arr, elem_fmt):
159160
arr = np.asarray(arr)
160161
assert arr.dtype.names is None
@@ -404,7 +405,7 @@ def labeltable(self, labeltable):
404405
405406
"""
406407
if not isinstance(labeltable, GiftiLabelTable):
407-
raise ValueError("Not a valid GiftiLabelTable instance")
408+
raise TypeError("Not a valid GiftiLabelTable instance")
408409
self._labeltable = labeltable
409410

410411
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
@@ -432,7 +433,7 @@ def meta(self, meta):
432433
None
433434
"""
434435
if not isinstance(meta, GiftiMetaData):
435-
raise ValueError("Not a valid GiftiMetaData instance")
436+
raise TypeError("Not a valid GiftiMetaData instance")
436437
self._meta = meta
437438

438439
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
@@ -450,10 +451,9 @@ def add_gifti_data_array(self, dataarr):
450451
----------
451452
dataarr : GiftiDataArray
452453
"""
453-
if isinstance(dataarr, GiftiDataArray):
454-
self.darrays.append(dataarr)
455-
else:
456-
print("dataarr paramater must be of tzpe GiftiDataArray")
454+
if not isinstance(dataarr, GiftiDataArray):
455+
raise TypeError("Not a valid GiftiDataArray instance")
456+
self.darrays.append(dataarr)
457457

458458
def remove_gifti_data_array(self, ith):
459459
""" Removes the ith data array element from the GiftiImage """

nibabel/gifti/tests/test_gifti.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
import numpy as np
66

7+
from nibabel.gifti import giftiio
8+
9+
from .test_giftiio import (DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4,
10+
DATA_FILE5, DATA_FILE6)
711
from ..gifti import (GiftiImage, GiftiDataArray, GiftiLabel, GiftiLabelTable,
812
GiftiMetaData)
913
from ...nifti1 import data_type_codes, intent_codes
10-
14+
from ...testing import clear_and_catch_warnings
1115
from numpy.testing import (assert_array_almost_equal,
1216
assert_array_equal)
1317
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
14-
from ...testing import clear_and_catch_warnings
1518

1619

1720
def test_gifti_image():
@@ -77,11 +80,6 @@ def test_labeltable():
7780
img.labeltable = new_table
7881
assert_equal(len(img.labeltable.labels), 2)
7982

80-
# Try to set to non-table
81-
def assign_labeltable(val):
82-
img.labeltable = val
83-
assert_raises(ValueError, assign_labeltable, 'not-a-table')
84-
8583

8684
def test_metadata():
8785
# Test deprecation
@@ -127,3 +125,41 @@ def assign_rgba(gl, val):
127125
gl4 = GiftiLabel()
128126
assert_equal(len(gl4.rgba), 4)
129127
assert_true(np.all([elem is None for elem in gl4.rgba]))
128+
129+
130+
def test_print_summary():
131+
for fil in [DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4,
132+
DATA_FILE5, DATA_FILE6]:
133+
gimg = giftiio.read(fil)
134+
gimg.print_summary()
135+
136+
137+
def test_gifti_coord():
138+
from ..gifti import GiftiCoordSystem
139+
gcs = GiftiCoordSystem()
140+
assert_true(gcs.xform is not None)
141+
142+
# Smoke test
143+
gcs.xform = None
144+
gcs.print_summary()
145+
gcs.to_xml()
146+
147+
148+
def test_gifti_image():
149+
img = GiftiImage()
150+
assert_true(img.darrays is not None)
151+
assert_true(img.meta is not None)
152+
assert_true(img.labeltable is not None)
153+
154+
# Try to set a non-data-array
155+
assert_raises(TypeError, img.add_gifti_data_array, 'not-a-data-array')
156+
157+
# Try to set to non-table
158+
def assign_labeltable(val):
159+
img.labeltable = val
160+
assert_raises(TypeError, assign_labeltable, 'not-a-table')
161+
162+
# Try to set to non-table
163+
def assign_metadata(val):
164+
img.meta = val
165+
assert_raises(TypeError, assign_metadata, 'not-a-meta')

0 commit comments

Comments
 (0)