Skip to content

Commit 271f49e

Browse files
committed
RF: deprecate GiftiDataArray.from_array
Deprecate GiftiDataArray.from_array in favor of GiftiDataArray constructor.
1 parent c2a8f52 commit 271f49e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

nibabel/gifti/gifti.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import division, print_function, absolute_import
1515

1616
import sys
17+
import warnings
1718

1819
import numpy as np
1920

@@ -407,6 +408,10 @@ def from_array(klass,
407408
-------
408409
da : instance of our own class
409410
"""
411+
warnings.warn(
412+
"Please use GiftiDataArray constructor instead of from_array "
413+
"class method",
414+
DeprecationWarning, stacklevel=2)
410415
return klass(data=darray,
411416
intent=intent,
412417
datatype=datatype,

nibabel/gifti/tests/test_gifti.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_gifti_image():
4040

4141
# Test from numpy numeric array
4242
data = np.random.random((5,))
43-
da = GiftiDataArray.from_array(data)
43+
da = GiftiDataArray(data)
4444
gi.add_gifti_data_array(da)
4545
assert_equal(gi.numDA, 1)
4646
assert_array_equal(gi.darrays[0].data, data)
@@ -56,7 +56,7 @@ def test_gifti_image():
5656

5757
# Remove one
5858
gi = GiftiImage()
59-
da = GiftiDataArray.from_array(np.zeros((5,)), intent=0)
59+
da = GiftiDataArray(np.zeros((5,)), intent=0)
6060
gi.add_gifti_data_array(da)
6161

6262
gi.remove_gifti_data_array_by_intent(3)
@@ -153,19 +153,25 @@ def test_dataarray_init():
153153

154154

155155
def test_dataarray_from_array():
156-
for dt_code in data_type_codes.value_set():
157-
data_type = data_type_codes.type[dt_code]
158-
if data_type is np.void: # not supported
159-
continue
160-
arr = np.zeros((10, 3), dtype=data_type)
161-
da = GiftiDataArray.from_array(arr, 'triangle')
162-
assert_equal(da.datatype, data_type_codes[arr.dtype])
163-
bs_arr = arr.byteswap().newbyteorder()
164-
da = GiftiDataArray.from_array(bs_arr, 'triangle')
165-
assert_equal(da.datatype, data_type_codes[arr.dtype])
166-
156+
with clear_and_catch_warnings() as w:
157+
warnings.filterwarnings('always', category=DeprecationWarning)
158+
da = GiftiDataArray.from_array(np.ones((3, 4)))
159+
assert_equal(len(w), 1)
160+
for dt_code in data_type_codes.value_set():
161+
data_type = data_type_codes.type[dt_code]
162+
if data_type is np.void: # not supported
163+
continue
164+
arr = np.zeros((10, 3), dtype=data_type)
165+
da = GiftiDataArray.from_array(arr, 'triangle')
166+
assert_equal(da.datatype, data_type_codes[arr.dtype])
167+
bs_arr = arr.byteswap().newbyteorder()
168+
da = GiftiDataArray.from_array(bs_arr, 'triangle')
169+
assert_equal(da.datatype, data_type_codes[arr.dtype])
170+
171+
172+
def test_to_xml_open_close_deprecations():
167173
# Smoke test on deprecated functions
168-
da = GiftiDataArray.from_array(np.ones((1,)), 'triangle')
174+
da = GiftiDataArray(np.ones((1,)), 'triangle')
169175
with clear_and_catch_warnings() as w:
170176
warnings.filterwarnings('always', category=DeprecationWarning)
171177
assert_true(isinstance(da.to_xml_open(), string_types))

0 commit comments

Comments
 (0)