Skip to content

Commit 385e68b

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 385e68b

File tree

8 files changed

+117
-38
lines changed

8 files changed

+117
-38
lines changed

nibabel/gifti/gifti.py

Lines changed: 48 additions & 14 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,10 +380,10 @@ 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}'
@@ -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,18 @@ 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+
613+
@deprecate_with_version(
614+
'set_labeltable method deprecated. '
615+
"Use the gifti_img.labeltable property instead.",
616+
'2.1', '4.0')
595617
def set_labeltable(self, labeltable):
596618
self.labeltable = labeltable
597619

598-
@np.deprecate_with_doc("Use the gifti_img.labeltable property instead.")
620+
@deprecate_with_version(
621+
'get_labeltable method deprecated. '
622+
"Use the gifti_img.labeltable property instead.",
623+
'2.1', '4.0')
599624
def get_labeltable(self):
600625
return self.labeltable
601626

@@ -615,11 +640,17 @@ def meta(self, meta):
615640
raise TypeError("Not a valid GiftiMetaData instance")
616641
self._meta = meta
617642

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

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

@@ -651,7 +682,10 @@ def get_arrays_from_intent(self, intent):
651682
it = intent_codes.code[intent]
652683
return [x for x in self.darrays if x.intent == it]
653684

654-
@np.deprecate_with_doc("Use get_arrays_from_intent instead.")
685+
@deprecate_with_version(
686+
'getArraysFromIntent method deprecated. '
687+
"Use get_arrays_from_intent instead.",
688+
'2.1', '4.0')
655689
def getArraysFromIntent(self, intent):
656690
return self.get_arrays_from_intent(intent)
657691

nibabel/gifti/giftiio.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212

1313
import numpy as np
1414

15+
from ..deprecated import deprecate_with_version
1516

16-
@np.deprecate_with_doc("Use nibabel.load() instead.")
17+
18+
@deprecate_with_version('giftiio.read function deprecated. '
19+
"Use nibabel.load() instead.",
20+
'2.1', '4.0')
1721
def read(filename):
1822
""" Load a Gifti image from a file
1923
@@ -31,7 +35,9 @@ def read(filename):
3135
return load(filename)
3236

3337

34-
@np.deprecate_with_doc("Use nibabel.save() instead.")
38+
@deprecate_with_version('giftiio.write function deprecated. '
39+
"Use nibabel.load() instead.",
40+
'2.1', '4.0')
3541
def write(image, filename):
3642
""" Save the current image to a new file
3743

nibabel/gifti/parse_gifti_fast.py

Lines changed: 8 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):
@@ -340,9 +341,12 @@ def pending_data(self):
340341
return self._char_blocks is not None
341342

342343

344+
343345
class Outputter(GiftiImageParser):
344346

345-
@np.deprecate_with_doc("Use GiftiImageParser instead.")
347+
@deprecate_with_version('Outputter class deprecated. '
348+
"Use GiftiImageParser instead.",
349+
'2.1', '4.0')
346350
def __init__(self):
347351
super(Outputter, self).__init__()
348352

@@ -351,6 +355,8 @@ def initialize(self):
351355
self.__init__()
352356

353357

354-
@np.deprecate_with_doc("Use GiftiImageParser.parse() instead.")
358+
@deprecate_with_version('parse_gifti_file deprecated. '
359+
"Use GiftiImageParser.parse() instead.",
360+
'2.1', '4.0')
355361
def parse_gifti_file(fname=None, fptr=None, buffer_size=None):
356362
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

nibabel/volumeutils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .casting import (shared_range, type_info, OK_FLOATS)
2323
from .openers import Opener
24+
from .deprecated import deprecate_with_version
2425

2526
sys_is_le = sys.byteorder == 'little'
2627
native_code = sys_is_le and '<' or '>'
@@ -373,7 +374,10 @@ def make_dt_codes(codes_seqs):
373374
return Recoder(dt_codes, fields + ['dtype', 'sw_dtype'], DtypeMapper)
374375

375376

376-
@np.deprecate_with_doc('Please use arraywriter classes instead')
377+
@deprecate_with_version('can_cast deprecated. '
378+
'Please use arraywriter classes instead',
379+
'1.2',
380+
'3.0')
377381
def can_cast(in_type, out_type, has_intercept=False, has_slope=False):
378382
''' Return True if we can safely cast ``in_type`` to ``out_type``
379383
@@ -1007,7 +1011,10 @@ def working_type(in_type, slope=1.0, inter=0.0):
10071011
return val.dtype.type
10081012

10091013

1010-
@np.deprecate_with_doc('Please use arraywriter classes instead')
1014+
@deprecate_with_version('calculate_scale deprecated. '
1015+
'Please use arraywriter classes instead',
1016+
'1.2',
1017+
'3.0')
10111018
def calculate_scale(data, out_dtype, allow_intercept):
10121019
''' Calculate scaling and optional intercept for data
10131020
@@ -1052,7 +1059,10 @@ def calculate_scale(data, out_dtype, allow_intercept):
10521059
return get_slope_inter(writer) + (mn, mx)
10531060

10541061

1055-
@np.deprecate_with_doc('Please use arraywriter classes instead')
1062+
@deprecate_with_version('scale_min_max deprecated. Please use arraywriter '
1063+
'classes instead.',
1064+
'1.2',
1065+
'3.0')
10561066
def scale_min_max(mn, mx, out_type, allow_intercept):
10571067
''' Return scaling and intercept min, max of data, given output type
10581068

0 commit comments

Comments
 (0)