Skip to content

Commit ba6f4ae

Browse files
committed
RF+TST: restore GiftiDataArray num_dim setter
We removed the `GiftiDataArray.num_dim` attribute in favor of a property, but pymvpa was expecting to be able to set the value. Add a setter that raises a ValueError if the value is incorrect, and gives a deprecation warning regardless.
1 parent 82a261c commit ba6f4ae

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)