Skip to content

MAINT: Effect FutureWarnings, including deprecations #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 22, 2019
2 changes: 1 addition & 1 deletion nibabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def setup_test():
flip_axis, OrientationError,
apply_orientation, aff2axcodes)
from .imageclasses import class_map, ext_map, all_image_classes
from . import trackvis
trackvis = _ModuleProxy('nibabel.trackvis')
from . import mriutils
from . import streamlines
from . import viewers
Expand Down
21 changes: 8 additions & 13 deletions nibabel/checkwarns.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@
import warnings

from .testing import (error_warnings, suppress_warnings)
from .deprecated import deprecate_with_version


warnings.warn('The checkwarns module is deprecated and will be removed '
'in nibabel v3.0', FutureWarning)
'in nibabel v3.0', DeprecationWarning)


@deprecate_with_version('ErrorWarnings is deprecated; use nibabel.testing.error_warnings.',
since='2.1.0', until='3.0.0')
class ErrorWarnings(error_warnings):

def __init__(self, *args, **kwargs):
warnings.warn('ErrorWarnings is deprecated and will be removed in '
'nibabel v3.0; use nibabel.testing.error_warnings.',
FutureWarning)
super(ErrorWarnings, self).__init__(*args, **kwargs)
pass


@deprecate_with_version('IgnoreWarnings is deprecated; use nibabel.testing.suppress_warnings.',
since='2.1.0', until='3.0.0')
class IgnoreWarnings(suppress_warnings):

def __init__(self, *args, **kwargs):
warnings.warn('IgnoreWarnings is deprecated and will be removed in '
'nibabel v3.0; use nibabel.testing.suppress_warnings.',
FutureWarning)
super(IgnoreWarnings, self).__init__(*args, **kwargs)
pass
4 changes: 2 additions & 2 deletions nibabel/minc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import warnings

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

from .minc1 import * # noqa
18 changes: 9 additions & 9 deletions nibabel/minc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .fileslice import canonical_slicers

from .keywordonly import kw_only_meth
from .deprecated import FutureWarningMixin
from .deprecated import deprecate_with_version

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


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


class MincImage(FutureWarningMixin, Minc1Image):
""" Deprecated alternative name for Minc1Image
"""
warn_message = 'MincImage is deprecated; please use Minc1Image instead'
@deprecate_with_version('MincImage is deprecated; please use Minc1Image instead',
since='2.0.0', until='3.0.0', warn_class=FutureWarning)
class MincImage(Minc1Image):
pass
13 changes: 7 additions & 6 deletions nibabel/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
from os.path import dirname, abspath, join as pjoin

import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_array_equal, assert_warns
from numpy.testing import dec
skipif = dec.skipif
slow = dec.slow

from ..deprecated import deprecate_with_version as _deprecate_with_version

# Allow failed import of nose if not now running tests
try:
from nose.tools import (assert_equal, assert_not_equal,
Expand Down Expand Up @@ -187,12 +189,11 @@ class suppress_warnings(error_warnings):
filter = 'ignore'


@_deprecate_with_version('catch_warn_reset is deprecated; use '
'nibabel.testing.clear_and_catch_warnings.',
since='2.1.0', until='3.0.0')
class catch_warn_reset(clear_and_catch_warnings):

def __init__(self, *args, **kwargs):
warnings.warn('catch_warn_reset is deprecated and will be removed in '
'nibabel v3.0; use nibabel.testing.clear_and_catch_warnings.',
FutureWarning)
pass


EXTRA_SET = os.environ.get('NIPY_EXTRA_TESTS', '').split(',')
Expand Down
16 changes: 3 additions & 13 deletions nibabel/tests/test_checkwarns.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
""" Tests for warnings context managers
"""
from __future__ import division, print_function, absolute_import

from nose.tools import assert_equal
from ..testing import clear_and_catch_warnings, suppress_warnings
from ..testing import assert_equal, assert_warns, suppress_warnings


def test_ignore_and_error_warnings():
with suppress_warnings():
from .. import checkwarns

with clear_and_catch_warnings() as w:
checkwarns.IgnoreWarnings()
assert_equal(len(w), 1)
assert_equal(w[0].category, FutureWarning)

with clear_and_catch_warnings() as w:
checkwarns.ErrorWarnings()
assert_equal(len(w), 1)
assert_equal(w[0].category, FutureWarning)
assert_warns(DeprecationWarning, checkwarns.IgnoreWarnings)
assert_warns(DeprecationWarning, checkwarns.ErrorWarnings)
16 changes: 9 additions & 7 deletions nibabel/tests/test_minc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
from .. import minc1
from ..minc1 import Minc1File, Minc1Image, MincHeader

from nose.tools import (assert_true, assert_equal, assert_false, assert_raises)
from numpy.testing import assert_array_equal
from ..tmpdirs import InTemporaryDirectory
from ..testing import data_path
from ..testing import (assert_true, assert_equal, assert_false, assert_raises, assert_warns,
assert_array_equal, data_path, clear_and_catch_warnings)

from . import test_spatialimages as tsi
from .test_fileslice import slicer_samples
Expand Down Expand Up @@ -106,7 +105,8 @@ def test_old_namespace():
# Check warnings raised
arr = np.arange(24).reshape((2, 3, 4))
aff = np.diag([2, 3, 4, 1])
with warnings.catch_warnings(record=True) as warns:
with clear_and_catch_warnings() as warns:
warnings.simplefilter('always', DeprecationWarning)
# Top level import.
# This import does not trigger an import of the minc.py module, because
# it's the proxy object.
Expand All @@ -122,7 +122,9 @@ def test_old_namespace():
# depending on whether the minc.py module is already imported in this
# test run.
if not previous_import:
assert_equal(warns.pop(0).category, FutureWarning)
assert_equal(warns.pop(0).category, DeprecationWarning)

with clear_and_catch_warnings() as warns:
from .. import Minc1Image, MincImage
assert_equal(warns, [])
# The import from old module is the same as that from new
Expand All @@ -132,17 +134,17 @@ def test_old_namespace():
assert_equal(warns, [])
# Create object using old name
mimg = MincImage(arr, aff)
assert_array_equal(mimg.get_data(), arr)
# Call to create object created warning
assert_equal(warns.pop(0).category, FutureWarning)
assert_array_equal(mimg.get_data(), arr)
# Another old name
from ..minc1 import MincFile, Minc1File
assert_false(MincFile is Minc1File)
assert_equal(warns, [])
mf = MincFile(netcdf_file(EG_FNAME))
assert_equal(mf.get_data_shape(), (10, 20, 20))
# Call to create object created warning
assert_equal(warns.pop(0).category, FutureWarning)
assert_equal(mf.get_data_shape(), (10, 20, 20))


class _TestMincFile(object):
Expand Down
34 changes: 34 additions & 0 deletions nibabel/tests/test_removalschedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from ..info import cmp_pkg_version
from ..testing import assert_raises, assert_false

MODULE_SCHEDULE = [
('4.0.0', ['nibabel.trackvis']),
('3.0.0', ['nibabel.minc', 'nibabel.checkwarns']),
# Verify that the test will be quiet if the schedule outlives the modules
('1.0.0', ['nibabel.neverexisted']),
]

OBJECT_SCHEDULE = [
('3.0.0', [('nibabel.testing', 'catch_warn_reset')]),
# Verify that the test will be quiet if the schedule outlives the modules
('1.0.0', [('nibabel', 'neverexisted')]),
]


def test_module_removal():
for version, to_remove in MODULE_SCHEDULE:
if cmp_pkg_version(version) < 1:
for module in to_remove:
with assert_raises(ImportError, msg="Time to remove " + module):
__import__(module)


def test_object_removal():
for version, to_remove in OBJECT_SCHEDULE:
if cmp_pkg_version(version) < 1:
for module_name, obj in to_remove:
try:
module = __import__(module_name)
except ImportError:
continue
assert_false(hasattr(module, obj), msg="Time to remove %s.%s" % (module_name, obj))
40 changes: 9 additions & 31 deletions nibabel/tests/test_trackvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from ..orientations import aff2axcodes
from ..volumeutils import native_code, swapped_code

from nose.tools import assert_true, assert_false, assert_equal, assert_raises
from numpy.testing import assert_array_equal, assert_array_almost_equal
from ..testing import error_warnings, suppress_warnings
from numpy.testing import assert_array_almost_equal
from ..testing import (assert_true, assert_false, assert_equal, assert_raises, assert_warns,
assert_array_equal, suppress_warnings)


def test_write():
Expand Down Expand Up @@ -217,8 +217,7 @@ def _rt(streams, hdr, points_space):
assert_raises(tv.HeaderError, tv.read, out_f, False, 'voxel')
# There's a warning for any voxel sizes == 0
hdr = {'voxel_size': [2, 3, 0]}
with error_warnings():
assert_raises(UserWarning, _rt, vx_streams, hdr, 'voxel')
assert_warns(UserWarning, _rt, vx_streams, hdr, 'voxel')
# This should be OK
hdr = {'voxel_size': [2, 3, 4]}
(raw_streams, hdr), (proc_streams, _) = _rt(vx_streams, hdr, 'voxel')
Expand Down Expand Up @@ -305,9 +304,8 @@ def test__check_hdr_points_space():
tv._check_hdr_points_space, hdr, 'voxel')
# Warning here only
hdr['voxel_size'] = [2, 3, 0]
with error_warnings():
assert_raises(UserWarning,
tv._check_hdr_points_space, hdr, 'voxel')
assert_warns(UserWarning,
tv._check_hdr_points_space, hdr, 'voxel')
# This is OK
hdr['voxel_size'] = [2, 3, 4]
assert_equal(tv._check_hdr_points_space(hdr, 'voxel'), None)
Expand Down Expand Up @@ -370,10 +368,6 @@ def test_empty_header():
def test_get_affine():
# Test get affine behavior, including pending deprecation
hdr = tv.empty_header()
# Using version 1 affine is not a good idea because is fragile and not
# very useful. The default atleast_v2=None mode raises a FutureWarning
with error_warnings():
assert_raises(FutureWarning, tv.aff_from_hdr, hdr)
# testing the old behavior
old_afh = partial(tv.aff_from_hdr, atleast_v2=False)
# default header gives useless affine
Expand Down Expand Up @@ -421,9 +415,8 @@ def test_get_affine():
assert_equal(hdr['voxel_order'], o_codes)
# Check it came back the way we wanted
assert_array_equal(old_afh(hdr), in_aff)
# Check that the default case matches atleast_v2=False case
with suppress_warnings():
assert_array_equal(tv.aff_from_hdr(hdr), flipped_aff)
# Check that v1 header raises error
assert_raises(tv.HeaderError, tv.aff_from_hdr, hdr)
# now use the easier vox_to_ras field
hdr = tv.empty_header()
aff = np.eye(4)
Expand Down Expand Up @@ -455,15 +448,7 @@ def test_aff_to_hdr():
# Historically we flip the first axis if there is a negative determinant
assert_array_almost_equal(hdr['voxel_size'], [-1, 2, 3])
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), aff2)
# Test that default mode raises DeprecationWarning
with error_warnings():
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr)
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, None)
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, False, None)
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, False)
# And has same effect as above
with suppress_warnings():
tv.aff_to_hdr(affine, hdr)
tv.aff_to_hdr(affine, hdr, pos_vox=False, set_order=False)
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), affine)
# Check pos_vox and order flags
for hdr in ({}, {'version': 2}, {'version': 1}):
Expand Down Expand Up @@ -515,13 +500,6 @@ def test_tv_class():
affine = np.diag([1, 2, 3, 1])
affine[:3, 3] = [10, 11, 12]
# affine methods will raise same warnings and errors as function
with error_warnings():
assert_raises(FutureWarning, tvf.set_affine, affine)
assert_raises(FutureWarning, tvf.set_affine, affine, None, None)
assert_raises(FutureWarning, tvf.set_affine, affine, False, None)
assert_raises(FutureWarning, tvf.set_affine, affine, None, False)
assert_raises(FutureWarning, tvf.get_affine)
assert_raises(FutureWarning, tvf.get_affine, None)
tvf.set_affine(affine, pos_vox=True, set_order=True)
aff = tvf.get_affine(atleast_v2=True)
assert_array_almost_equal(aff, affine)
Expand Down
Loading