Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4724b7

Browse files
authoredAug 1, 2016
Merge pull request #468 from matthew-brett/back-compat-gifti
MRG: restore GiftiDataArray num_dim setter Add back-compatibility setter for attribute moved to property.
2 parents 82a261c + ba6f4ae commit a4724b7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎nibabel/gifti/gifti.py

+11
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ def __init__(self,
369369
def num_dim(self):
370370
return len(self.dims)
371371

372+
# Setter for backwards compatibility with pymvpa
373+
@num_dim.setter
374+
def num_dim(self, value):
375+
warnings.warn(
376+
"num_dim will be read-only in future versions of nibabel",
377+
DeprecationWarning, stacklevel=2)
378+
if value != len(self.dims):
379+
raise ValueError('num_dim value {0} != number of dimensions '
380+
'len(self.dims) {1}'
381+
.format(value, len(self.dims)))
382+
372383
@classmethod
373384
def from_array(klass,
374385
darray,

‎nibabel/gifti/tests/test_gifti.py

+13
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ def test_to_xml_open_close_deprecations():
182182
assert_equal(len(w), 1)
183183

184184

185+
def test_num_dim_deprecation():
186+
da = GiftiDataArray(np.ones((2, 3, 4)))
187+
# num_dim is property, set automatically from len(da.dims)
188+
assert_equal(da.num_dim, 3)
189+
with clear_and_catch_warnings() as w:
190+
warnings.filterwarnings('always', category=DeprecationWarning)
191+
# OK setting num_dim to correct value, but raises DeprecationWarning
192+
da.num_dim = 3
193+
assert_equal(len(w), 1)
194+
# Any other value gives a ValueError
195+
assert_raises(ValueError, setattr, da, 'num_dim', 4)
196+
197+
185198
def test_labeltable():
186199
img = GiftiImage()
187200
assert_equal(len(img.labeltable.labels), 0)

0 commit comments

Comments
 (0)
Please sign in to comment.