Skip to content

TST: Add GIFTI tests #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def rgba(self, rgba):
raise ValueError('rgba must be length 4.')
self.red, self.green, self.blue, self.alpha = rgba


def _arr2txt(arr, elem_fmt):
arr = np.asarray(arr)
assert arr.dtype.names is None
Expand Down Expand Up @@ -404,7 +405,7 @@ def labeltable(self, labeltable):

"""
if not isinstance(labeltable, GiftiLabelTable):
raise ValueError("Not a valid GiftiLabelTable instance")
raise TypeError("Not a valid GiftiLabelTable instance")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do a test for this exception, while we're at it.

self._labeltable = labeltable

@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
Expand Down Expand Up @@ -432,7 +433,7 @@ def meta(self, meta):
None
"""
if not isinstance(meta, GiftiMetaData):
raise ValueError("Not a valid GiftiMetaData instance")
raise TypeError("Not a valid GiftiMetaData instance")
self._meta = meta

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, while I'm critiquing, my preference on this idiom would be:

if not isinstance(...):
    raise TypeError(...)
self.darrays.append(dataarr)

Just associates the check more closely with the guarded case, and avoids visually demoting the useful code by putting it in an else block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on the style and error type. Fixed, and scrubbed the code for other ValueErrors that should be TypeErrors. Tests running.

def remove_gifti_data_array(self, ith):
""" Removes the ith data array element from the GiftiImage """
Expand Down
50 changes: 43 additions & 7 deletions nibabel/gifti/tests/test_gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

import numpy as np

from nibabel.gifti import giftiio

from .test_giftiio import (DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4,
DATA_FILE5, DATA_FILE6)
from ..gifti import (GiftiImage, GiftiDataArray, GiftiLabel, GiftiLabelTable,
GiftiMetaData)
from ...nifti1 import data_type_codes, intent_codes

from ...testing import clear_and_catch_warnings
from numpy.testing import (assert_array_almost_equal,
assert_array_equal)
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
from ...testing import clear_and_catch_warnings


def test_gifti_image():
Expand Down Expand Up @@ -77,11 +80,6 @@ def test_labeltable():
img.labeltable = new_table
assert_equal(len(img.labeltable.labels), 2)

# Try to set to non-table
def assign_labeltable(val):
img.labeltable = val
assert_raises(ValueError, assign_labeltable, 'not-a-table')


def test_metadata():
# Test deprecation
Expand Down Expand Up @@ -127,3 +125,41 @@ def assign_rgba(gl, val):
gl4 = GiftiLabel()
assert_equal(len(gl4.rgba), 4)
assert_true(np.all([elem is None for elem in gl4.rgba]))


def test_print_summary():
for fil in [DATA_FILE1, DATA_FILE2, DATA_FILE3, DATA_FILE4,
DATA_FILE5, DATA_FILE6]:
gimg = giftiio.read(fil)
gimg.print_summary()


def test_gifti_coord():
from ..gifti import GiftiCoordSystem
gcs = GiftiCoordSystem()
assert_true(gcs.xform is not None)

# Smoke test
gcs.xform = None
gcs.print_summary()
gcs.to_xml()


def test_gifti_image():
img = GiftiImage()
assert_true(img.darrays is not None)
assert_true(img.meta is not None)
assert_true(img.labeltable is not None)

# Try to set a non-data-array
assert_raises(TypeError, img.add_gifti_data_array, 'not-a-data-array')

# Try to set to non-table
def assign_labeltable(val):
img.labeltable = val
assert_raises(TypeError, assign_labeltable, 'not-a-table')

# Try to set to non-table
def assign_metadata(val):
img.meta = val
assert_raises(TypeError, assign_metadata, 'not-a-meta')