-
Notifications
You must be signed in to change notification settings - Fork 262
RF: Use python properties, rather than set/get methods #353
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
Changes from all commits
a3b3f41
1effc55
bc5f38f
6699a43
bc5f2d1
017f7e5
f542f36
2fc386c
9d67e3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
import base64 | ||
import sys | ||
import warnings | ||
import zlib | ||
from ..externals.six import StringIO | ||
from xml.parsers.expat import ParserCreate, ExpatError | ||
|
@@ -109,8 +110,7 @@ def StartElementHandler(self, name, attrs): | |
if 'Version' in attrs: | ||
self.img.version = attrs['Version'] | ||
if 'NumberOfDataArrays' in attrs: | ||
self.img.numDA = int(attrs['NumberOfDataArrays']) | ||
self.count_da = False | ||
self.expected_numDA = int(attrs['NumberOfDataArrays']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
And then check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I will also add a test for this. In fact, we lack malformed GIFTI file tests; see #357 |
||
|
||
self.fsm_state.append('GIFTI') | ||
elif name == 'MetaData': | ||
|
@@ -207,6 +207,10 @@ def EndElementHandler(self, name): | |
if DEBUG_PRINT: | ||
print('End element:\n\t', repr(name)) | ||
if name == 'GIFTI': | ||
if hasattr(self, 'expected_numDA') and self.expected_numDA != self.img.numDA: | ||
warnings.warn("Actual # of data arrays does not match " | ||
"# expected: %d != %d." % (self.expected_numDA, | ||
self.img.numDA)) | ||
# remove last element of the list | ||
self.fsm_state.pop() | ||
# assert len(self.fsm_state) == 0 | ||
|
@@ -234,8 +238,6 @@ def EndElementHandler(self, name): | |
self.img.labeltable = self.lata | ||
self.lata = None | ||
elif name == 'DataArray': | ||
if self.count_da: | ||
self.img.numDA += 1 | ||
self.fsm_state.pop() | ||
elif name == 'CoordinateSystemTransformMatrix': | ||
self.fsm_state.pop() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
""" Testing gifti objects | ||
""" | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
from ..gifti import (GiftiImage, GiftiDataArray, GiftiLabel, GiftiLabelTable, | ||
GiftiMetaData) | ||
from ...nifti1 import data_type_codes, intent_codes | ||
|
||
from ..gifti import GiftiImage, GiftiDataArray | ||
|
||
from numpy.testing import (assert_array_almost_equal, | ||
assert_array_equal) | ||
|
||
from nose.tools import assert_true, assert_equal, assert_raises | ||
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises) | ||
from ...testing import clear_and_catch_warnings | ||
|
||
|
||
def test_gifti_image(): | ||
|
@@ -24,6 +25,35 @@ def test_gifti_image(): | |
gi = GiftiImage() | ||
assert_equal(gi.darrays, []) | ||
|
||
# Test darrays / numDA | ||
gi = GiftiImage() | ||
assert_equal(gi.numDA, 0) | ||
|
||
da = GiftiDataArray(data='data') | ||
gi.add_gifti_data_array(da) | ||
assert_equal(gi.numDA, 1) | ||
assert_equal(gi.darrays[0].data, 'data') | ||
|
||
gi.remove_gifti_data_array(0) | ||
assert_equal(gi.numDA, 0) | ||
|
||
# Remove from empty | ||
gi = GiftiImage() | ||
gi.remove_gifti_data_array_by_intent(0) | ||
assert_equal(gi.numDA, 0) | ||
|
||
# Remove one | ||
gi = GiftiImage() | ||
da = GiftiDataArray(data='data') | ||
gi.add_gifti_data_array(da) | ||
|
||
gi.remove_gifti_data_array_by_intent(0) | ||
assert_equal(gi.numDA, 1) | ||
|
||
gi.darrays[0].intent = 0 | ||
gi.remove_gifti_data_array_by_intent(0) | ||
assert_equal(gi.numDA, 0) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a check that trying to remove a data array when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that test is here: #353 (diff) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow. Yup. Now I'm questioning my ability to read. Should probably have another pass through in a bit. |
||
|
||
def test_dataarray(): | ||
for dt_code in data_type_codes.value_set(): | ||
|
@@ -36,3 +66,64 @@ def test_dataarray(): | |
bs_arr = arr.byteswap().newbyteorder() | ||
da = GiftiDataArray.from_array(bs_arr, 'triangle') | ||
assert_equal(da.datatype, data_type_codes[arr.dtype]) | ||
|
||
|
||
def test_labeltable(): | ||
img = GiftiImage() | ||
assert_equal(len(img.labeltable.labels), 0) | ||
|
||
new_table = GiftiLabelTable() | ||
new_table.labels += ['test', 'me'] | ||
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 | ||
with clear_and_catch_warnings() as w: | ||
warnings.filterwarnings('once', category=DeprecationWarning) | ||
assert_equal(len(GiftiDataArray().get_metadata()), 0) | ||
|
||
# Test deprecation | ||
with clear_and_catch_warnings() as w: | ||
warnings.filterwarnings('once', category=DeprecationWarning) | ||
assert_equal(len(GiftiMetaData().get_metadata()), 0) | ||
|
||
|
||
def test_gifti_label_rgba(): | ||
rgba = np.random.rand(4) | ||
kwargs = dict(zip(['red', 'green', 'blue', 'alpha'], rgba)) | ||
|
||
gl1 = GiftiLabel(**kwargs) | ||
assert_array_equal(rgba, gl1.rgba) | ||
|
||
gl1.red = 2 * gl1.red | ||
assert_false(np.allclose(rgba, gl1.rgba)) # don't just store the list! | ||
|
||
gl2 = GiftiLabel() | ||
gl2.rgba = rgba | ||
assert_array_equal(rgba, gl2.rgba) | ||
|
||
gl2.blue = 2 * gl2.blue | ||
assert_false(np.allclose(rgba, gl2.rgba)) # don't just store the list! | ||
|
||
def assign_rgba(gl, val): | ||
gl.rgba = val | ||
gl3 = GiftiLabel(**kwargs) | ||
assert_raises(ValueError, assign_rgba, gl3, rgba[:2]) | ||
assert_raises(ValueError, assign_rgba, gl3, rgba.tolist() + rgba.tolist()) | ||
|
||
# Test deprecation | ||
with clear_and_catch_warnings() as w: | ||
warnings.filterwarnings('once', category=DeprecationWarning) | ||
assert_equal(kwargs['red'], gl3.get_rgba()[0]) | ||
|
||
# Test default value | ||
gl4 = GiftiLabel() | ||
assert_equal(len(gl4.rgba), 4) | ||
assert_true(np.all([elem is None for elem in gl4.rgba])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the change from
print
toValueError
here.