Skip to content
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

TEST: Finalize deprecations #1117

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 7 additions & 35 deletions nibabel/freesurfer/tests/test_mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ...volumeutils import sys_is_le
from ...wrapstruct import WrapStructError
from ... import imageglobals
from ...deprecator import ExpiredDeprecationError


import pytest
Expand Down Expand Up @@ -344,42 +345,13 @@ def test_deprecated_fields():
hdr_data = MGHHeader._HeaderData(hdr.structarr)

# mrparams is the only deprecated field at the moment
# Accessing hdr_data is equivalent to accessing hdr, so double all checks
with pytest.deprecated_call(match="from version: 2.3"):
assert_array_equal(hdr['mrparams'], 0)
assert_array_equal(hdr_data['mrparams'], 0)

with pytest.deprecated_call(match="from version: 2.3"):
# Accessing hdr_data is equivalent to accessing hdr, so double all checks,
# but expect success on hdr_data['mrparams']
with pytest.raises(ExpiredDeprecationError):
hdr['mrparams']
with pytest.raises(ExpiredDeprecationError):
hdr['mrparams'] = [1, 2, 3, 4]
with pytest.deprecated_call(match="from version: 2.3"):
assert_array_almost_equal(hdr['mrparams'], [1, 2, 3, 4])
assert hdr['tr'] == 1
assert hdr['flip_angle'] == 2
assert hdr['te'] == 3
assert hdr['ti'] == 4
assert hdr['fov'] == 0
assert_array_almost_equal(hdr_data['mrparams'], [1, 2, 3, 4])
assert hdr_data['tr'] == 1
assert hdr_data['flip_angle'] == 2
assert hdr_data['te'] == 3
assert hdr_data['ti'] == 4
assert hdr_data['fov'] == 0

hdr['tr'] = 5
hdr['flip_angle'] = 6
hdr['te'] = 7
hdr['ti'] = 8
with pytest.deprecated_call(match="from version: 2.3"):
assert_array_almost_equal(hdr['mrparams'], [5, 6, 7, 8])
assert_array_almost_equal(hdr_data['mrparams'], [5, 6, 7, 8])

hdr_data['tr'] = 9
hdr_data['flip_angle'] = 10
hdr_data['te'] = 11
hdr_data['ti'] = 12
with pytest.deprecated_call(match="from version: 2.3"):
assert_array_almost_equal(hdr['mrparams'], [9, 10, 11, 12])
assert_array_almost_equal(hdr_data['mrparams'], [9, 10, 11, 12])
assert_array_equal(hdr_data['mrparams'], 0)


class TestMGHImage(tsi.TestSpatialImage, tsi.MmapImageMixin):
Expand Down
68 changes: 19 additions & 49 deletions nibabel/gifti/tests/test_gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from ..gifti import data_tag
from ...nifti1 import data_type_codes
from ...fileholders import FileHolder
from ...deprecator import ExpiredDeprecationError

from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest
from ...testing import clear_and_catch_warnings, test_data
from ...testing import test_data
from .test_parse_gifti_fast import (DATA_FILE1, DATA_FILE2, DATA_FILE3,
DATA_FILE4, DATA_FILE5, DATA_FILE6)
import itertools
Expand Down Expand Up @@ -183,46 +184,29 @@ def test_dataarray_init():


def test_dataarray_from_array():
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
da = GiftiDataArray.from_array(np.ones((3, 4)))
assert len(w) == 1
for dt_code in data_type_codes.value_set():
data_type = data_type_codes.type[dt_code]
if data_type is np.void: # not supported
continue
arr = np.zeros((10, 3), dtype=data_type)
da = GiftiDataArray.from_array(arr, 'triangle')
assert da.datatype == data_type_codes[arr.dtype]
bs_arr = arr.byteswap().newbyteorder()
da = GiftiDataArray.from_array(bs_arr, 'triangle')
assert da.datatype == data_type_codes[arr.dtype]
with pytest.raises(ExpiredDeprecationError):
GiftiDataArray.from_array(np.ones((3, 4)))


def test_to_xml_open_close_deprecations():
# Smoke test on deprecated functions
da = GiftiDataArray(np.ones((1,)), 'triangle')
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
assert isinstance(da.to_xml_open(), str)
assert len(w) == 1
with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
assert isinstance(da.to_xml_close(), str)
assert len(w) == 1
with pytest.raises(ExpiredDeprecationError):
da.to_xml_open()
with pytest.raises(ExpiredDeprecationError):
da.to_xml_close()


def test_num_dim_deprecation():
da = GiftiDataArray(np.ones((2, 3, 4)))
# num_dim is property, set automatically from len(da.dims)
assert da.num_dim == 3
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
# OK setting num_dim to correct value, but raises DeprecationWarning
# setting num_dim to correct value is deprecated
with pytest.raises(ExpiredDeprecationError):
da.num_dim = 3
assert len(w) == 1
# Any other value gives a ValueError
pytest.raises(ValueError, setattr, da, 'num_dim', 4)
# setting num_dim to incorrect value is also deprecated
with pytest.raises(ExpiredDeprecationError):
da.num_dim = 4


def test_labeltable():
Expand All @@ -235,14 +219,10 @@ def test_labeltable():
assert len(img.labeltable.labels) == 2

# Test deprecations
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
with pytest.raises(ExpiredDeprecationError):
newer_table = GiftiLabelTable()
newer_table.labels += ['test', 'me', 'again']
img.set_labeltable(newer_table)
assert len(w) == 1
assert len(img.get_labeltable().labels) == 3
assert len(w) == 2


def test_metadata():
Expand All @@ -261,14 +241,8 @@ def test_metadata():
assert md.data[0].value == 'value'
assert len(w) == 2
# Test deprecation
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
assert md.get_metadata() == dict(key='value')
assert len(w) == 1
assert md.metadata == dict(key='value')
assert len(w) == 2
assert len(GiftiDataArray().get_metadata()) == 0
assert len(w) == 3
with pytest.raises(ExpiredDeprecationError):
md.get_metadata()


def test_gifti_label_rgba():
Expand All @@ -295,10 +269,8 @@ def assign_rgba(gl, val):
pytest.raises(ValueError, assign_rgba, gl3, rgba.tolist() + rgba.tolist())

# Test deprecation
with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
assert kwargs['red'] == gl3.get_rgba()[0]
assert len(w) == 1
with pytest.raises(ExpiredDeprecationError):
gl3.get_rgba()

# Test default value
gl4 = GiftiLabel()
Expand All @@ -325,10 +297,8 @@ def test_gifti_coord():


def test_data_tag_deprecated():
with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
with pytest.raises(ExpiredDeprecationError):
data_tag(np.array([]), 'ASCII', '%i', 1)
assert len(w) == 1


def test_gifti_round_trip():
Expand Down
9 changes: 6 additions & 3 deletions nibabel/gifti/tests/test_giftiio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

from ..gifti import GiftiImage
from ..giftiio import read, write
from .test_parse_gifti_fast import DATA_FILE1
from ...deprecator import ExpiredDeprecationError

import pytest


def test_read_deprecated(tmp_path):
with pytest.deprecated_call():
img = read(DATA_FILE1)
with pytest.raises(ExpiredDeprecationError):
read(DATA_FILE1)

img = GiftiImage.from_filename(DATA_FILE1)
fname = tmp_path / 'test.gii'
with pytest.deprecated_call():
with pytest.raises(ExpiredDeprecationError):
write(img, fname)
48 changes: 16 additions & 32 deletions nibabel/gifti/tests/test_parse_gifti_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ...loadsave import load, save
from ...nifti1 import xform_codes
from ...tmpdirs import InTemporaryDirectory
from ...deprecator import ExpiredDeprecationError

from numpy.testing import assert_array_almost_equal

Expand All @@ -48,8 +49,8 @@

DATA_FILE1_darr1 = np.array(
[[-16.07201, -66.187515, 21.266994],
[-16.705893, -66.054337, 21.232786],
[-17.614349, -65.401642, 21.071466]])
[-16.705893, -66.054337, 21.232786],
[-17.614349, -65.401642, 21.071466]])
DATA_FILE1_darr2 = np.array([0, 1, 2])

DATA_FILE2_darr1 = np.array([[0.43635699],
Expand Down Expand Up @@ -189,14 +190,11 @@ def test_metadata_deprecations():
me = img.meta

# Test deprecation
with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
assert me == img.get_meta()
with pytest.raises(ExpiredDeprecationError):
img.get_meta()

with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
with pytest.raises(ExpiredDeprecationError):
img.set_metadata(me)
assert me == img.meta


def test_load_dataarray1():
Expand Down Expand Up @@ -321,12 +319,8 @@ def test_load_getbyintent():
da = img.get_arrays_from_intent("NIFTI_INTENT_POINTSET")
assert len(da) == 1

with clear_and_catch_warnings() as w:
warnings.filterwarnings('once', category=DeprecationWarning)
da = img.getArraysFromIntent("NIFTI_INTENT_POINTSET")
assert len(da) == 1
assert len(w) == 1
w[0].category == DeprecationWarning
with pytest.raises(ExpiredDeprecationError):
img.getArraysFromIntent("NIFTI_INTENT_POINTSET")

da = img.get_arrays_from_intent("NIFTI_INTENT_TRIANGLE")
assert len(da) == 1
Expand Down Expand Up @@ -360,16 +354,11 @@ def test_labeltable_deprecations():
lt = img.labeltable

# Test deprecation
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
assert lt == img.get_labeltable()
assert len(w) == 1
with pytest.raises(ExpiredDeprecationError):
img.get_labeltable()

with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
with pytest.raises(ExpiredDeprecationError):
img.set_labeltable(lt)
assert len(w) == 1
assert lt == img.labeltable


def test_parse_dataarrays():
Expand All @@ -395,16 +384,11 @@ def test_parse_dataarrays():
def test_parse_deprecated():

# Test deprecation
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
op = Outputter()
assert len(w) == 1
op.initialize() # smoke test--no error.

with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
pytest.raises(ValueError, parse_gifti_file)
assert len(w) == 1
with pytest.raises(ExpiredDeprecationError):
Outputter()

with pytest.raises(ExpiredDeprecationError):
parse_gifti_file()


def test_parse_with_buffersize():
Expand Down
5 changes: 3 additions & 2 deletions nibabel/nicom/tests/test_dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .. import dicomwrappers as didw
from .. import dicomreaders as didr
from ...volumeutils import endian_codes
from ...deprecator import ExpiredDeprecationError

import pytest
from unittest import TestCase
Expand Down Expand Up @@ -631,8 +632,8 @@ def test_affine(self):
# Make sure we find orientation/position/spacing info
dw = didw.wrapper_from_file(DATA_FILE_4D)
aff = dw.affine
with pytest.deprecated_call():
assert np.array_equal(dw.get_affine(), aff)
with pytest.raises(ExpiredDeprecationError):
dw.get_affine()

@dicom_test
def test_data_real(self):
Expand Down
3 changes: 2 additions & 1 deletion nibabel/streamlines/tests/test_array_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from numpy.testing import assert_array_equal

from ..array_sequence import ArraySequence, is_array_sequence, concatenate
from ...deprecator import ExpiredDeprecationError


SEQ_DATA = {}
Expand Down Expand Up @@ -96,7 +97,7 @@ def test_creating_arraysequence_from_list(self):

def test_deprecated_data_attribute(self):
seq = ArraySequence(SEQ_DATA['data'])
with pytest.deprecated_call(match="from version: 3.0"):
with pytest.raises(ExpiredDeprecationError):
seq.data

def test_creating_arraysequence_from_generator(self):
Expand Down
17 changes: 5 additions & 12 deletions nibabel/tests/test_ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from ..testing import data_path, suppress_warnings
from ..tmpdirs import InTemporaryDirectory
from ..deprecator import ExpiredDeprecationError

from . import test_wrapstruct as tws
from .test_fileslice import slicer_samples
Expand Down Expand Up @@ -240,9 +241,8 @@ def test_isolation(self):
assert not np.all(img.affine == aff)

def test_get_affine_deprecated(self):
with pytest.deprecated_call(match="from version: 2.1"):
aff = self.img.get_affine()
assert np.array_equal(aff, self.img.affine)
with pytest.raises(ExpiredDeprecationError):
self.img.get_affine()

def test_float_affine(self):
# Check affines get converted to float
Expand Down Expand Up @@ -275,12 +275,5 @@ def test_mlist_regression(self):


def test_from_filespec_deprecation():
# Check from_filespec raises Deprecation
with pytest.deprecated_call() as w:
# No warning for standard load
img_loaded = EcatImage.load(ecat_file)
assert len(w) == 0
# Warning for from_filespec
img_speced = EcatImage.from_filespec(ecat_file)
assert len(w) == 1
assert_array_equal(img_loaded.get_fdata(), img_speced.get_fdata())
with pytest.raises(ExpiredDeprecationError):
EcatImage.from_filespec(ecat_file)