Skip to content

Commit 754ee52

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 754ee52

File tree

8 files changed

+118
-44
lines changed

8 files changed

+118
-44
lines changed

nibabel/gifti/gifti.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from __future__ import division, print_function, absolute_import
1515

1616
import sys
17-
import warnings
1817

1918
import numpy as np
2019

@@ -23,6 +22,7 @@
2322
from ..nifti1 import data_type_codes, xform_codes, intent_codes
2423
from .util import (array_index_order_codes, gifti_encoding_codes,
2524
gifti_endian_codes, KIND2FMT)
25+
from ..deprecated import deprecate_with_version
2626

2727
# {en,de}codestring in deprecated in Python3, but
2828
# {en,de}codebytes not available in Python2.
@@ -47,7 +47,10 @@ def from_dict(klass, data_dict):
4747
meda.data.append(nv)
4848
return meda
4949

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

@@ -156,7 +159,10 @@ def __init__(self, key=0, red=None, green=None, blue=None, alpha=None):
156159
self.blue = blue
157160
self.alpha = alpha
158161

159-
@np.deprecate_with_doc("Use the rgba property instead.")
162+
@deprecate_with_version(
163+
'get_rgba method deprecated. '
164+
"Use the rgba property instead."
165+
'2.1', '4.0')
160166
def get_rgba(self):
161167
return self.rgba
162168

@@ -249,7 +255,9 @@ def print_summary(self):
249255
print('Affine Transformation Matrix: \n', self.xform)
250256

251257

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

@@ -371,16 +379,20 @@ def num_dim(self):
371379

372380
# Setter for backwards compatibility with pymvpa
373381
@num_dim.setter
382+
@deprecate_with_version(
383+
"num_dim will be read-only in future versions of nibabel",
384+
'2.1', '4.0')
374385
def num_dim(self, value):
375-
warnings.warn(
376-
"num_dim will be read-only in future versions of nibabel",
377-
DeprecationWarning, stacklevel=2)
378386
if value != len(self.dims):
379387
raise ValueError('num_dim value {0} != number of dimensions '
380388
'len(self.dims) {1}'
381389
.format(value, len(self.dims)))
382390

383391
@classmethod
392+
@deprecate_with_version(
393+
'from_array method is deprecated. '
394+
'Please use GiftiDataArray constructor instead.',
395+
'2.1', '4.0')
384396
def from_array(klass,
385397
darray,
386398
intent="NIFTI_INTENT_NONE",
@@ -419,10 +431,6 @@ def from_array(klass,
419431
-------
420432
da : instance of our own class
421433
"""
422-
warnings.warn(
423-
"Please use GiftiDataArray constructor instead of from_array "
424-
"class method",
425-
DeprecationWarning, stacklevel=2)
426434
return klass(data=darray,
427435
intent=intent,
428436
datatype=datatype,
@@ -463,7 +471,10 @@ def _to_xml_element(self):
463471

464472
return data_array
465473

466-
@np.deprecate_with_doc("Use the to_xml() function instead.")
474+
@deprecate_with_version(
475+
'to_xml_open method deprecated. '
476+
'Use the to_xml() function instead.',
477+
'2.1', '4.0')
467478
def to_xml_open(self):
468479
out = """<DataArray Intent="%s"
469480
\tDataType="%s"
@@ -487,7 +498,10 @@ def to_xml_open(self):
487498
self.ext_offset,
488499
)
489500

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

@@ -507,7 +521,10 @@ def print_summary(self):
507521
print('Coordinate System:')
508522
print(self.coordsys.print_summary())
509523

510-
@np.deprecate_with_doc("Use the metadata property instead.")
524+
@deprecate_with_version(
525+
'get_metadata method deprecated. '
526+
"Use the metadata property instead."
527+
'2.1', '4.0')
511528
def get_metadata(self):
512529
return self.meta.metadata
513530

@@ -591,11 +608,17 @@ def labeltable(self, labeltable):
591608
raise TypeError("Not a valid GiftiLabelTable instance")
592609
self._labeltable = labeltable
593610

594-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
611+
@deprecate_with_version(
612+
'set_labeltable method deprecated. '
613+
"Use the gifti_img.labeltable property instead.",
614+
'2.1', '4.0')
595615
def set_labeltable(self, labeltable):
596616
self.labeltable = labeltable
597617

598-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
618+
@deprecate_with_version(
619+
'get_labeltable method deprecated. '
620+
"Use the gifti_img.labeltable property instead.",
621+
'2.1', '4.0')
599622
def get_labeltable(self):
600623
return self.labeltable
601624

@@ -615,11 +638,17 @@ def meta(self, meta):
615638
raise TypeError("Not a valid GiftiMetaData instance")
616639
self._meta = meta
617640

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

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

@@ -651,7 +680,10 @@ def get_arrays_from_intent(self, intent):
651680
it = intent_codes.code[intent]
652681
return [x for x in self.darrays if x.intent == it]
653682

654-
@np.deprecate_with_doc("Use get_arrays_from_intent instead.")
683+
@deprecate_with_version(
684+
'getArraysFromIntent method deprecated. '
685+
"Use get_arrays_from_intent instead.",
686+
'2.1', '4.0')
655687
def getArraysFromIntent(self, intent):
656688
return self.get_arrays_from_intent(intent)
657689

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)