Skip to content

Commit 935079b

Browse files
authored
Merge pull request #755 from effigies/mnt/deprecations
MAINT: Effect FutureWarnings, including deprecations
2 parents 980f678 + b643ccd commit 935079b

10 files changed

+110
-122
lines changed

nibabel/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def setup_test():
7777
flip_axis, OrientationError,
7878
apply_orientation, aff2axcodes)
7979
from .imageclasses import class_map, ext_map, all_image_classes
80-
from . import trackvis
80+
trackvis = _ModuleProxy('nibabel.trackvis')
8181
from . import mriutils
8282
from . import streamlines
8383
from . import viewers

nibabel/checkwarns.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,20 @@
1313
import warnings
1414

1515
from .testing import (error_warnings, suppress_warnings)
16+
from .deprecated import deprecate_with_version
1617

1718

1819
warnings.warn('The checkwarns module is deprecated and will be removed '
19-
'in nibabel v3.0', FutureWarning)
20+
'in nibabel v3.0', DeprecationWarning)
2021

2122

23+
@deprecate_with_version('ErrorWarnings is deprecated; use nibabel.testing.error_warnings.',
24+
since='2.1.0', until='3.0.0')
2225
class ErrorWarnings(error_warnings):
23-
24-
def __init__(self, *args, **kwargs):
25-
warnings.warn('ErrorWarnings is deprecated and will be removed in '
26-
'nibabel v3.0; use nibabel.testing.error_warnings.',
27-
FutureWarning)
28-
super(ErrorWarnings, self).__init__(*args, **kwargs)
26+
pass
2927

3028

29+
@deprecate_with_version('IgnoreWarnings is deprecated; use nibabel.testing.suppress_warnings.',
30+
since='2.1.0', until='3.0.0')
3131
class IgnoreWarnings(suppress_warnings):
32-
33-
def __init__(self, *args, **kwargs):
34-
warnings.warn('IgnoreWarnings is deprecated and will be removed in '
35-
'nibabel v3.0; use nibabel.testing.suppress_warnings.',
36-
FutureWarning)
37-
super(IgnoreWarnings, self).__init__(*args, **kwargs)
32+
pass

nibabel/minc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import warnings
44

5-
warnings.warn("We will remove this module from nibabel soon; "
5+
warnings.warn("We will remove this module from nibabel 3.0; "
66
"Please use the 'minc1' module instead",
7-
FutureWarning,
7+
DeprecationWarning,
88
stacklevel=2)
99

1010
from .minc1 import * # noqa

nibabel/minc1.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .fileslice import canonical_slicers
1919

2020
from .keywordonly import kw_only_meth
21-
from .deprecated import FutureWarningMixin
21+
from .deprecated import deprecate_with_version
2222

2323
_dt_dict = {
2424
('b', 'unsigned'): np.uint8,
@@ -331,13 +331,13 @@ def from_file_map(klass, file_map, mmap=True, keep_file_open=None):
331331

332332

333333
# Backwards compatibility
334-
class MincFile(FutureWarningMixin, Minc1File):
335-
""" Deprecated alternative name for Minc1File
336-
"""
337-
warn_message = 'MincFile is deprecated; please use Minc1File instead'
334+
@deprecate_with_version('MincFile is deprecated; please use Minc1File instead',
335+
since='2.0.0', until='3.0.0', warn_class=FutureWarning)
336+
class MincFile(Minc1File):
337+
pass
338338

339339

340-
class MincImage(FutureWarningMixin, Minc1Image):
341-
""" Deprecated alternative name for Minc1Image
342-
"""
343-
warn_message = 'MincImage is deprecated; please use Minc1Image instead'
340+
@deprecate_with_version('MincImage is deprecated; please use Minc1Image instead',
341+
since='2.0.0', until='3.0.0', warn_class=FutureWarning)
342+
class MincImage(Minc1Image):
343+
pass

nibabel/testing/__init__.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
from os.path import dirname, abspath, join as pjoin
1717

1818
import numpy as np
19-
from numpy.testing import assert_array_equal
19+
from numpy.testing import assert_array_equal, assert_warns
2020
from numpy.testing import dec
2121
skipif = dec.skipif
2222
slow = dec.slow
2323

24+
from ..deprecated import deprecate_with_version as _deprecate_with_version
25+
2426
# Allow failed import of nose if not now running tests
2527
try:
2628
from nose.tools import (assert_equal, assert_not_equal,
@@ -187,12 +189,11 @@ class suppress_warnings(error_warnings):
187189
filter = 'ignore'
188190

189191

192+
@_deprecate_with_version('catch_warn_reset is deprecated; use '
193+
'nibabel.testing.clear_and_catch_warnings.',
194+
since='2.1.0', until='3.0.0')
190195
class catch_warn_reset(clear_and_catch_warnings):
191-
192-
def __init__(self, *args, **kwargs):
193-
warnings.warn('catch_warn_reset is deprecated and will be removed in '
194-
'nibabel v3.0; use nibabel.testing.clear_and_catch_warnings.',
195-
FutureWarning)
196+
pass
196197

197198

198199
EXTRA_SET = os.environ.get('NIPY_EXTRA_TESTS', '').split(',')

nibabel/tests/test_checkwarns.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
""" Tests for warnings context managers
22
"""
3-
from __future__ import division, print_function, absolute_import
4-
5-
from nose.tools import assert_equal
6-
from ..testing import clear_and_catch_warnings, suppress_warnings
3+
from ..testing import assert_equal, assert_warns, suppress_warnings
74

85

96
def test_ignore_and_error_warnings():
107
with suppress_warnings():
118
from .. import checkwarns
129

13-
with clear_and_catch_warnings() as w:
14-
checkwarns.IgnoreWarnings()
15-
assert_equal(len(w), 1)
16-
assert_equal(w[0].category, FutureWarning)
17-
18-
with clear_and_catch_warnings() as w:
19-
checkwarns.ErrorWarnings()
20-
assert_equal(len(w), 1)
21-
assert_equal(w[0].category, FutureWarning)
10+
assert_warns(DeprecationWarning, checkwarns.IgnoreWarnings)
11+
assert_warns(DeprecationWarning, checkwarns.ErrorWarnings)

nibabel/tests/test_minc1.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
from .. import minc1
2525
from ..minc1 import Minc1File, Minc1Image, MincHeader
2626

27-
from nose.tools import (assert_true, assert_equal, assert_false, assert_raises)
28-
from numpy.testing import assert_array_equal
2927
from ..tmpdirs import InTemporaryDirectory
30-
from ..testing import data_path
28+
from ..testing import (assert_true, assert_equal, assert_false, assert_raises, assert_warns,
29+
assert_array_equal, data_path, clear_and_catch_warnings)
3130

3231
from . import test_spatialimages as tsi
3332
from .test_fileslice import slicer_samples
@@ -106,7 +105,8 @@ def test_old_namespace():
106105
# Check warnings raised
107106
arr = np.arange(24).reshape((2, 3, 4))
108107
aff = np.diag([2, 3, 4, 1])
109-
with warnings.catch_warnings(record=True) as warns:
108+
with clear_and_catch_warnings() as warns:
109+
warnings.simplefilter('always', DeprecationWarning)
110110
# Top level import.
111111
# This import does not trigger an import of the minc.py module, because
112112
# it's the proxy object.
@@ -122,7 +122,9 @@ def test_old_namespace():
122122
# depending on whether the minc.py module is already imported in this
123123
# test run.
124124
if not previous_import:
125-
assert_equal(warns.pop(0).category, FutureWarning)
125+
assert_equal(warns.pop(0).category, DeprecationWarning)
126+
127+
with clear_and_catch_warnings() as warns:
126128
from .. import Minc1Image, MincImage
127129
assert_equal(warns, [])
128130
# The import from old module is the same as that from new
@@ -132,17 +134,17 @@ def test_old_namespace():
132134
assert_equal(warns, [])
133135
# Create object using old name
134136
mimg = MincImage(arr, aff)
135-
assert_array_equal(mimg.get_data(), arr)
136137
# Call to create object created warning
137138
assert_equal(warns.pop(0).category, FutureWarning)
139+
assert_array_equal(mimg.get_data(), arr)
138140
# Another old name
139141
from ..minc1 import MincFile, Minc1File
140142
assert_false(MincFile is Minc1File)
141143
assert_equal(warns, [])
142144
mf = MincFile(netcdf_file(EG_FNAME))
143-
assert_equal(mf.get_data_shape(), (10, 20, 20))
144145
# Call to create object created warning
145146
assert_equal(warns.pop(0).category, FutureWarning)
147+
assert_equal(mf.get_data_shape(), (10, 20, 20))
146148

147149

148150
class _TestMincFile(object):

nibabel/tests/test_removalschedule.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from ..info import cmp_pkg_version
2+
from ..testing import assert_raises, assert_false
3+
4+
MODULE_SCHEDULE = [
5+
('4.0.0', ['nibabel.trackvis']),
6+
('3.0.0', ['nibabel.minc', 'nibabel.checkwarns']),
7+
# Verify that the test will be quiet if the schedule outlives the modules
8+
('1.0.0', ['nibabel.neverexisted']),
9+
]
10+
11+
OBJECT_SCHEDULE = [
12+
('3.0.0', [('nibabel.testing', 'catch_warn_reset')]),
13+
# Verify that the test will be quiet if the schedule outlives the modules
14+
('1.0.0', [('nibabel', 'neverexisted')]),
15+
]
16+
17+
18+
def test_module_removal():
19+
for version, to_remove in MODULE_SCHEDULE:
20+
if cmp_pkg_version(version) < 1:
21+
for module in to_remove:
22+
with assert_raises(ImportError, msg="Time to remove " + module):
23+
__import__(module)
24+
25+
26+
def test_object_removal():
27+
for version, to_remove in OBJECT_SCHEDULE:
28+
if cmp_pkg_version(version) < 1:
29+
for module_name, obj in to_remove:
30+
try:
31+
module = __import__(module_name)
32+
except ImportError:
33+
continue
34+
assert_false(hasattr(module, obj), msg="Time to remove %s.%s" % (module_name, obj))

nibabel/tests/test_trackvis.py

+9-31
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from ..orientations import aff2axcodes
1111
from ..volumeutils import native_code, swapped_code
1212

13-
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
14-
from numpy.testing import assert_array_equal, assert_array_almost_equal
15-
from ..testing import error_warnings, suppress_warnings
13+
from numpy.testing import assert_array_almost_equal
14+
from ..testing import (assert_true, assert_false, assert_equal, assert_raises, assert_warns,
15+
assert_array_equal, suppress_warnings)
1616

1717

1818
def test_write():
@@ -217,8 +217,7 @@ def _rt(streams, hdr, points_space):
217217
assert_raises(tv.HeaderError, tv.read, out_f, False, 'voxel')
218218
# There's a warning for any voxel sizes == 0
219219
hdr = {'voxel_size': [2, 3, 0]}
220-
with error_warnings():
221-
assert_raises(UserWarning, _rt, vx_streams, hdr, 'voxel')
220+
assert_warns(UserWarning, _rt, vx_streams, hdr, 'voxel')
222221
# This should be OK
223222
hdr = {'voxel_size': [2, 3, 4]}
224223
(raw_streams, hdr), (proc_streams, _) = _rt(vx_streams, hdr, 'voxel')
@@ -305,9 +304,8 @@ def test__check_hdr_points_space():
305304
tv._check_hdr_points_space, hdr, 'voxel')
306305
# Warning here only
307306
hdr['voxel_size'] = [2, 3, 0]
308-
with error_warnings():
309-
assert_raises(UserWarning,
310-
tv._check_hdr_points_space, hdr, 'voxel')
307+
assert_warns(UserWarning,
308+
tv._check_hdr_points_space, hdr, 'voxel')
311309
# This is OK
312310
hdr['voxel_size'] = [2, 3, 4]
313311
assert_equal(tv._check_hdr_points_space(hdr, 'voxel'), None)
@@ -370,10 +368,6 @@ def test_empty_header():
370368
def test_get_affine():
371369
# Test get affine behavior, including pending deprecation
372370
hdr = tv.empty_header()
373-
# Using version 1 affine is not a good idea because is fragile and not
374-
# very useful. The default atleast_v2=None mode raises a FutureWarning
375-
with error_warnings():
376-
assert_raises(FutureWarning, tv.aff_from_hdr, hdr)
377371
# testing the old behavior
378372
old_afh = partial(tv.aff_from_hdr, atleast_v2=False)
379373
# default header gives useless affine
@@ -421,9 +415,8 @@ def test_get_affine():
421415
assert_equal(hdr['voxel_order'], o_codes)
422416
# Check it came back the way we wanted
423417
assert_array_equal(old_afh(hdr), in_aff)
424-
# Check that the default case matches atleast_v2=False case
425-
with suppress_warnings():
426-
assert_array_equal(tv.aff_from_hdr(hdr), flipped_aff)
418+
# Check that v1 header raises error
419+
assert_raises(tv.HeaderError, tv.aff_from_hdr, hdr)
427420
# now use the easier vox_to_ras field
428421
hdr = tv.empty_header()
429422
aff = np.eye(4)
@@ -455,15 +448,7 @@ def test_aff_to_hdr():
455448
# Historically we flip the first axis if there is a negative determinant
456449
assert_array_almost_equal(hdr['voxel_size'], [-1, 2, 3])
457450
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), aff2)
458-
# Test that default mode raises DeprecationWarning
459-
with error_warnings():
460-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr)
461-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, None)
462-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, False, None)
463-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, False)
464-
# And has same effect as above
465-
with suppress_warnings():
466-
tv.aff_to_hdr(affine, hdr)
451+
tv.aff_to_hdr(affine, hdr, pos_vox=False, set_order=False)
467452
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), affine)
468453
# Check pos_vox and order flags
469454
for hdr in ({}, {'version': 2}, {'version': 1}):
@@ -515,13 +500,6 @@ def test_tv_class():
515500
affine = np.diag([1, 2, 3, 1])
516501
affine[:3, 3] = [10, 11, 12]
517502
# affine methods will raise same warnings and errors as function
518-
with error_warnings():
519-
assert_raises(FutureWarning, tvf.set_affine, affine)
520-
assert_raises(FutureWarning, tvf.set_affine, affine, None, None)
521-
assert_raises(FutureWarning, tvf.set_affine, affine, False, None)
522-
assert_raises(FutureWarning, tvf.set_affine, affine, None, False)
523-
assert_raises(FutureWarning, tvf.get_affine)
524-
assert_raises(FutureWarning, tvf.get_affine, None)
525503
tvf.set_affine(affine, pos_vox=True, set_order=True)
526504
aff = tvf.get_affine(atleast_v2=True)
527505
assert_array_almost_equal(aff, affine)

0 commit comments

Comments
 (0)