Skip to content

Commit c36bc27

Browse files
committed
RF: replace numpy deprecation decorator with ours
Replace `np.deprecate_with_doc` with `deprecate_with_version`. There are other deprecations around the code-base that could be signalled with this decorator.
1 parent 6a6aa52 commit c36bc27

File tree

8 files changed

+118
-43
lines changed

8 files changed

+118
-43
lines changed

nibabel/gifti/gifti.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ..nifti1 import data_type_codes, xform_codes, intent_codes
2424
from .util import (array_index_order_codes, gifti_encoding_codes,
2525
gifti_endian_codes, KIND2FMT)
26+
from ..deprecated import deprecate_with_version
2627

2728
# {en,de}codestring in deprecated in Python3, but
2829
# {en,de}codebytes not available in Python2.
@@ -47,7 +48,10 @@ def from_dict(klass, data_dict):
4748
meda.data.append(nv)
4849
return meda
4950

50-
@np.deprecate_with_doc("Use the metadata property instead.")
51+
@deprecate_with_version(
52+
'get_metadata method deprecated. '
53+
"Use the metadata property instead."
54+
'2.1', '4.0')
5155
def get_metadata(self):
5256
return self.metadata
5357

@@ -156,7 +160,10 @@ def __init__(self, key=0, red=None, green=None, blue=None, alpha=None):
156160
self.blue = blue
157161
self.alpha = alpha
158162

159-
@np.deprecate_with_doc("Use the rgba property instead.")
163+
@deprecate_with_version(
164+
'get_rgba method deprecated. '
165+
"Use the rgba property instead."
166+
'2.1', '4.0')
160167
def get_rgba(self):
161168
return self.rgba
162169

@@ -249,7 +256,9 @@ def print_summary(self):
249256
print('Affine Transformation Matrix: \n', self.xform)
250257

251258

252-
@np.deprecate_with_doc("This is an internal API that will be discontinued.")
259+
@deprecate_with_version(
260+
"data_tag is an internal API that will be discontinued.",
261+
'2.1', '4.0')
253262
def data_tag(dataarray, encoding, datatype, ordering):
254263
class DataTag(xml.XmlSerializable):
255264

@@ -371,16 +380,20 @@ def num_dim(self):
371380

372381
# Setter for backwards compatibility with pymvpa
373382
@num_dim.setter
383+
@deprecate_with_version(
384+
"num_dim will be read-only in future versions of nibabel",
385+
'2.1', '4.0')
374386
def num_dim(self, value):
375-
warnings.warn(
376-
"num_dim will be read-only in future versions of nibabel",
377-
DeprecationWarning, stacklevel=2)
378387
if value != len(self.dims):
379388
raise ValueError('num_dim value {0} != number of dimensions '
380389
'len(self.dims) {1}'
381390
.format(value, len(self.dims)))
382391

383392
@classmethod
393+
@deprecate_with_version(
394+
'from_array method is deprecated. '
395+
'Please use GiftiDataArray constructor instead.',
396+
'2.1', '4.0')
384397
def from_array(klass,
385398
darray,
386399
intent="NIFTI_INTENT_NONE",
@@ -419,10 +432,6 @@ def from_array(klass,
419432
-------
420433
da : instance of our own class
421434
"""
422-
warnings.warn(
423-
"Please use GiftiDataArray constructor instead of from_array "
424-
"class method",
425-
DeprecationWarning, stacklevel=2)
426435
return klass(data=darray,
427436
intent=intent,
428437
datatype=datatype,
@@ -463,7 +472,10 @@ def _to_xml_element(self):
463472

464473
return data_array
465474

466-
@np.deprecate_with_doc("Use the to_xml() function instead.")
475+
@deprecate_with_version(
476+
'to_xml_open method deprecated. '
477+
'Use the to_xml() function instead.',
478+
'2.1', '4.0')
467479
def to_xml_open(self):
468480
out = """<DataArray Intent="%s"
469481
\tDataType="%s"
@@ -487,7 +499,10 @@ def to_xml_open(self):
487499
self.ext_offset,
488500
)
489501

490-
@np.deprecate_with_doc("Use the to_xml() function instead.")
502+
@deprecate_with_version(
503+
'to_xml_close method deprecated. '
504+
'Use the to_xml() function instead.',
505+
'2.1', '4.0')
491506
def to_xml_close(self):
492507
return "</DataArray>\n"
493508

@@ -507,7 +522,10 @@ def print_summary(self):
507522
print('Coordinate System:')
508523
print(self.coordsys.print_summary())
509524

510-
@np.deprecate_with_doc("Use the metadata property instead.")
525+
@deprecate_with_version(
526+
'get_metadata method deprecated. '
527+
"Use the metadata property instead."
528+
'2.1', '4.0')
511529
def get_metadata(self):
512530
return self.meta.metadata
513531

@@ -591,11 +609,17 @@ def labeltable(self, labeltable):
591609
raise TypeError("Not a valid GiftiLabelTable instance")
592610
self._labeltable = labeltable
593611

594-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
612+
@deprecate_with_version(
613+
'set_labeltable method deprecated. '
614+
"Use the gifti_img.labeltable property instead.",
615+
'2.1', '4.0')
595616
def set_labeltable(self, labeltable):
596617
self.labeltable = labeltable
597618

598-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
619+
@deprecate_with_version(
620+
'get_labeltable method deprecated. '
621+
"Use the gifti_img.labeltable property instead.",
622+
'2.1', '4.0')
599623
def get_labeltable(self):
600624
return self.labeltable
601625

@@ -615,11 +639,17 @@ def meta(self, meta):
615639
raise TypeError("Not a valid GiftiMetaData instance")
616640
self._meta = meta
617641

618-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
642+
@deprecate_with_version(
643+
'set_meta method deprecated. '
644+
"Use the gifti_img.meta property instead.",
645+
'2.1', '4.0')
619646
def set_metadata(self, meta):
620647
self.meta = meta
621648

622-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
649+
@deprecate_with_version(
650+
'get_meta method deprecated. '
651+
"Use the gifti_img.meta property instead.",
652+
'2.1', '4.0')
623653
def get_meta(self):
624654
return self.meta
625655

@@ -651,7 +681,10 @@ def get_arrays_from_intent(self, intent):
651681
it = intent_codes.code[intent]
652682
return [x for x in self.darrays if x.intent == it]
653683

654-
@np.deprecate_with_doc("Use get_arrays_from_intent instead.")
684+
@deprecate_with_version(
685+
'getArraysFromIntent method deprecated. '
686+
"Use get_arrays_from_intent instead.",
687+
'2.1', '4.0')
655688
def getArraysFromIntent(self, intent):
656689
return self.get_arrays_from_intent(intent)
657690

nibabel/gifti/giftiio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
# Stephan Gerhard, Oktober 2010
1111
##############
1212

13-
import numpy as np
13+
from ..deprecated import deprecate_with_version
1414

1515

16-
@np.deprecate_with_doc("Use nibabel.load() instead.")
16+
@deprecate_with_version('giftiio.read function deprecated. '
17+
"Use nibabel.load() instead.",
18+
'2.1', '4.0')
1719
def read(filename):
1820
""" Load a Gifti image from a file
1921
@@ -31,7 +33,9 @@ def read(filename):
3133
return load(filename)
3234

3335

34-
@np.deprecate_with_doc("Use nibabel.save() instead.")
36+
@deprecate_with_version('giftiio.write function deprecated. '
37+
"Use nibabel.load() instead.",
38+
'2.1', '4.0')
3539
def write(image, filename):
3640
""" Save the current image to a new file
3741

nibabel/gifti/parse_gifti_fast.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
gifti_endian_codes)
2525
from ..nifti1 import data_type_codes, xform_codes, intent_codes
2626
from ..xmlutils import XmlParser
27+
from ..deprecated import deprecate_with_version
2728

2829

2930
class GiftiParseError(ExpatError):
@@ -342,7 +343,9 @@ def pending_data(self):
342343

343344
class Outputter(GiftiImageParser):
344345

345-
@np.deprecate_with_doc("Use GiftiImageParser instead.")
346+
@deprecate_with_version('Outputter class deprecated. '
347+
"Use GiftiImageParser instead.",
348+
'2.1', '4.0')
346349
def __init__(self):
347350
super(Outputter, self).__init__()
348351

@@ -351,6 +354,8 @@ def initialize(self):
351354
self.__init__()
352355

353356

354-
@np.deprecate_with_doc("Use GiftiImageParser.parse() instead.")
357+
@deprecate_with_version('parse_gifti_file deprecated. '
358+
"Use GiftiImageParser.parse() instead.",
359+
'2.1', '4.0')
355360
def parse_gifti_file(fname=None, fptr=None, buffer_size=None):
356361
GiftiImageParser(buffer_size=buffer_size).parse(fname=fname, fptr=fptr)

nibabel/gifti/tests/test_gifti.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,29 @@ def test_labeltable():
206206
img.labeltable = new_table
207207
assert_equal(len(img.labeltable.labels), 2)
208208

209+
# Test deprecations
210+
with clear_and_catch_warnings() as w:
211+
warnings.filterwarnings('always', category=DeprecationWarning)
212+
newer_table = GiftiLabelTable()
213+
newer_table.labels += ['test', 'me', 'again']
214+
img.set_labeltable(newer_table)
215+
assert_equal(len(w), 1)
216+
assert_equal(len(img.get_labeltable().labels), 3)
217+
assert_equal(len(w), 2)
218+
209219

210220
def test_metadata():
211221
nvpair = GiftiNVPairs('key', 'value')
212-
da = GiftiMetaData(nvpair=nvpair)
213-
assert_equal(da.data[0].name, 'key')
214-
assert_equal(da.data[0].value, 'value')
222+
md = GiftiMetaData(nvpair=nvpair)
223+
assert_equal(md.data[0].name, 'key')
224+
assert_equal(md.data[0].value, 'value')
215225
# Test deprecation
216226
with clear_and_catch_warnings() as w:
217227
warnings.filterwarnings('always', category=DeprecationWarning)
218-
assert_equal(len(GiftiDataArray().get_metadata()), 0)
228+
assert_equal(md.get_metadata(), dict(key='value'))
219229
assert_equal(len(w), 1)
230+
assert_equal(len(GiftiDataArray().get_metadata()), 0)
231+
assert_equal(len(w), 2)
220232

221233

222234
def test_gifti_label_rgba():

nibabel/gifti/tests/test_giftiio.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
from nose.tools import (assert_true, assert_false, assert_equal,
1313
assert_raises)
14-
from ...testing import clear_and_catch_warnings
14+
from nibabel.testing import clear_and_catch_warnings
15+
from nibabel.tmpdirs import InTemporaryDirectory
1516

1617

1718
from .test_parse_gifti_fast import (DATA_FILE1, DATA_FILE2, DATA_FILE3,
@@ -29,7 +30,10 @@ def setUp(self):
2930
def test_read_deprecated():
3031
with clear_and_catch_warnings() as w:
3132
warnings.simplefilter('always', DeprecationWarning)
32-
from nibabel.gifti.giftiio import read
33+
from nibabel.gifti.giftiio import read, write
3334

34-
read(DATA_FILE1)
35+
img = read(DATA_FILE1)
3536
assert_equal(len(w), 1)
37+
with InTemporaryDirectory():
38+
write(img, 'test.gii')
39+
assert_equal(len(w), 2)

nibabel/loadsave.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
import os.path as op
1313
import numpy as np
14-
import warnings
1514

1615
from .filename_parser import splitext_addext
1716
from .openers import ImageOpener
1817
from .filebasedimages import ImageFileError
1918
from .imageclasses import all_image_classes
2019
from .arrayproxy import is_proxy
2120
from .py3k import FileNotFoundError
21+
from .deprecated import deprecate_with_version
2222

2323

2424
def load(filename, **kwargs):
@@ -48,7 +48,9 @@ def load(filename, **kwargs):
4848
filename)
4949

5050

51-
@np.deprecate
51+
@deprecate_with_version('guessed_image_type deprecated.'
52+
'2.1',
53+
'4.0')
5254
def guessed_image_type(filename):
5355
""" Guess image type from file `filename`
5456
@@ -62,8 +64,6 @@ def guessed_image_type(filename):
6264
image_class : class
6365
Class corresponding to guessed image type
6466
"""
65-
warnings.warn('guessed_image_type is deprecated', DeprecationWarning,
66-
stacklevel=2)
6767
sniff = None
6868
for image_klass in all_image_classes:
6969
is_valid, sniff = image_klass.path_maybe_image(filename, sniff)
@@ -124,8 +124,10 @@ def save(img, filename):
124124
converted.to_filename(filename)
125125

126126

127-
@np.deprecate_with_doc('Please use ``img.dataobj.get_unscaled()`` '
128-
'instead')
127+
@deprecate_with_version('read_img_data deprecated.'
128+
'Please use ``img.dataobj.get_unscaled()`` instead.'
129+
'2.0.1',
130+
'4.0')
129131
def read_img_data(img, prefer='scaled'):
130132
""" Read data from image associated with files
131133
@@ -210,7 +212,9 @@ def read_img_data(img, prefer='scaled'):
210212
return hdr.raw_data_from_fileobj(fileobj)
211213

212214

213-
@np.deprecate
215+
@deprecate_with_version('which_analyze_type deprecated.'
216+
'2.1',
217+
'4.0')
214218
def which_analyze_type(binaryblock):
215219
""" Is `binaryblock` from NIfTI1, NIfTI2 or Analyze header?
216220
@@ -238,8 +242,6 @@ def which_analyze_type(binaryblock):
238242
* if ``sizeof_hdr`` is 348 or byteswapped 348 assume Analyze
239243
* Return None
240244
"""
241-
warnings.warn('which_analyze_type is deprecated', DeprecationWarning,
242-
stacklevel=2)
243245
from .nifti1 import header_dtype
244246
hdr_struct = np.ndarray(shape=(), dtype=header_dtype, buffer=binaryblock)
245247
bs_hdr_struct = hdr_struct.byteswap()

nibabel/orientations.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import numpy as np
1414
import numpy.linalg as npl
1515

16+
from .deprecated import deprecate_with_version
17+
1618

1719
class OrientationError(Exception):
1820
pass
@@ -228,7 +230,10 @@ def inv_ornt_aff(ornt, shape):
228230
return np.dot(undo_flip, undo_reorder)
229231

230232

231-
@np.deprecate_with_doc("Please use inv_ornt_aff instead")
233+
@deprecate_with_version('orientation_affine deprecated. '
234+
'Please use inv_ornt_aff instead'
235+
'1.3',
236+
'3.0')
232237
def orientation_affine(ornt, shape):
233238
return inv_ornt_aff(ornt, shape)
234239

0 commit comments

Comments
 (0)