Skip to content

Commit f0ab879

Browse files
committed
TEST: Suppress expected warnings
1 parent e65b865 commit f0ab879

17 files changed

+229
-135
lines changed

nibabel/tests/test_analyze.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,11 @@ def test_data_hdr_cache(self):
734734
assert_array_equal(np.asanyarray(img2.dataobj), data)
735735
# now check read_img_data function - here we do see the changed
736736
# header
737-
sc_data = read_img_data(img2)
737+
with pytest.deprecated_call():
738+
sc_data = read_img_data(img2)
738739
assert sc_data.shape == (3, 2, 2)
739-
us_data = read_img_data(img2, prefer='unscaled')
740+
with pytest.deprecated_call():
741+
us_data = read_img_data(img2, prefer='unscaled')
740742
assert us_data.shape == (3, 2, 2)
741743

742744
def test_affine_44(self):

nibabel/tests/test_deprecated.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33

44
import warnings
5+
import pytest
56

67
from nibabel import pkg_info
78
from nibabel.deprecated import (ModuleProxy, FutureWarningMixin,
@@ -77,6 +78,7 @@ def func():
7778
try:
7879
pkg_info.cmp_pkg_version.__defaults__ = ('2.0dev',)
7980
# No error, even though version is dev version of current
80-
assert func() == 99
81+
with pytest.deprecated_call():
82+
assert func() == 99
8183
finally:
8284
pkg_info.cmp_pkg_version.__defaults__ = ('2.0',)

nibabel/tests/test_ecat.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ def test_isolation(self):
239239
aff[0, 0] = 99
240240
assert not np.all(img.affine == aff)
241241

242+
def test_get_affine_deprecated(self):
243+
with pytest.deprecated_call():
244+
aff = self.img.get_affine()
245+
assert np.array_equal(aff, self.img.affine)
246+
242247
def test_float_affine(self):
243248
# Check affines get converted to float
244249
img_klass = self.image_class
@@ -248,9 +253,9 @@ def test_float_affine(self):
248253
self.img.get_subheaders(),
249254
self.img.get_mlist())
250255
img = img_klass(arr, aff.astype(np.float32), hdr, sub_hdr, mlist)
251-
assert img.get_affine().dtype == np.dtype(np.float64)
256+
assert img.affine.dtype == np.dtype(np.float64)
252257
img = img_klass(arr, aff.astype(np.int16), hdr, sub_hdr, mlist)
253-
assert img.get_affine().dtype == np.dtype(np.float64)
258+
assert img.affine.dtype == np.dtype(np.float64)
254259

255260
def test_data_regression(self):
256261
# Test whether data read has changed since 1.3.0

nibabel/tests/test_image_api.py

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import warnings
2727
from functools import partial
2828
from itertools import product
29+
from contextlib import nullcontext
2930
import pathlib
3031

3132
import numpy as np
@@ -56,6 +57,11 @@
5657
from .test_parrec import EXAMPLE_IMAGES as PARREC_EXAMPLE_IMAGES
5758
from .test_brikhead import EXAMPLE_IMAGES as AFNI_EXAMPLE_IMAGES
5859

60+
61+
def maybe_deprecated(meth_name):
62+
return pytest.deprecated_call() if meth_name =='get_data' else nullcontext()
63+
64+
5965
class GenericImageAPI(ValidateAPI):
6066
""" General image validation API """
6167
# Whether this image type can do scaling of data
@@ -221,14 +227,17 @@ def validate_data_interface(self, imaker, params):
221227
self._check_proxy_interface(imaker, meth_name)
222228
else: # Array image
223229
self._check_array_interface(imaker, meth_name)
230+
method = getattr(img, meth_name)
224231
# Data shape is same as image shape
225-
assert img.shape == getattr(img, meth_name)().shape
232+
with maybe_deprecated(meth_name):
233+
assert img.shape == method().shape
226234
# Data ndim is same as image ndim
227-
assert img.ndim == getattr(img, meth_name)().ndim
235+
with maybe_deprecated(meth_name):
236+
assert img.ndim == method().ndim
228237
# Values to get_data caching parameter must be 'fill' or
229238
# 'unchanged'
230-
with pytest.raises(ValueError):
231-
img.get_data(caching='something')
239+
with maybe_deprecated(meth_name), pytest.raises(ValueError):
240+
method(caching='something')
232241
# dataobj is read only
233242
fake_data = np.zeros(img.shape).astype(img.get_data_dtype())
234243
with pytest.raises(AttributeError):
@@ -251,11 +260,13 @@ def _check_proxy_interface(self, imaker, meth_name):
251260
assert not img.in_memory
252261
# Load with caching='unchanged'
253262
method = getattr(img, meth_name)
254-
data = method(caching='unchanged')
263+
with maybe_deprecated(meth_name):
264+
data = method(caching='unchanged')
255265
# Still not cached
256266
assert not img.in_memory
257267
# Default load, does caching
258-
data = method()
268+
with maybe_deprecated(meth_name):
269+
data = method()
259270
# Data now cached. in_memory is True if either of the get_data
260271
# or get_fdata caches are not-None
261272
assert img.in_memory
@@ -267,30 +278,36 @@ def _check_proxy_interface(self, imaker, meth_name):
267278
# integers, but lets assume that's not true here.
268279
assert_array_equal(proxy_data, data)
269280
# Now caching='unchanged' does nothing, returns cached version
270-
data_again = method(caching='unchanged')
281+
with maybe_deprecated(meth_name):
282+
data_again = method(caching='unchanged')
271283
assert data is data_again
272284
# caching='fill' does nothing because the cache is already full
273-
data_yet_again = method(caching='fill')
285+
with maybe_deprecated(meth_name):
286+
data_yet_again = method(caching='fill')
274287
assert data is data_yet_again
275288
# changing array data does not change proxy data, or reloaded
276289
# data
277290
data[:] = 42
278291
assert_array_equal(proxy_data, proxy_copy)
279292
assert_array_equal(np.asarray(img.dataobj), proxy_copy)
280293
# It does change the result of get_data
281-
assert_array_equal(method(), 42)
294+
with maybe_deprecated(meth_name):
295+
assert_array_equal(method(), 42)
282296
# until we uncache
283297
img.uncache()
284298
# Which unsets in_memory
285299
assert not img.in_memory
286-
assert_array_equal(method(), proxy_copy)
300+
with maybe_deprecated(meth_name):
301+
assert_array_equal(method(), proxy_copy)
287302
# Check caching='fill' does cache data
288303
img = imaker()
289304
method = getattr(img, meth_name)
290305
assert not img.in_memory
291-
data = method(caching='fill')
306+
with maybe_deprecated(meth_name):
307+
data = method(caching='fill')
292308
assert img.in_memory
293-
data_again = method()
309+
with maybe_deprecated(meth_name):
310+
data_again = method()
294311
assert data is data_again
295312
# Check the interaction of caching with get_data, get_fdata.
296313
# Caching for `get_data` should have no effect on caching for
@@ -300,14 +317,17 @@ def _check_proxy_interface(self, imaker, meth_name):
300317
# Load using the other data fetch method
301318
other_name = set(self.meth_names).difference({meth_name}).pop()
302319
other_method = getattr(img, other_name)
303-
other_data = other_method()
320+
with maybe_deprecated(other_name):
321+
other_data = other_method()
304322
# We get the original data, not the modified cache
305323
assert_array_equal(proxy_data, other_data)
306324
assert not np.all(data == other_data)
307325
# We can modify the other cache, without affecting the first
308326
other_data[:] = 44
309-
assert_array_equal(other_method(), 44)
310-
assert not np.all(method() == other_method())
327+
with maybe_deprecated(other_name):
328+
assert_array_equal(other_method(), 44)
329+
with pytest.deprecated_call():
330+
assert not np.all(method() == other_method())
311331
if meth_name != 'get_fdata':
312332
return
313333
# Check that caching refreshes for new floating point type.
@@ -353,31 +373,34 @@ def _check_array_caching(self, imaker, meth_name, caching):
353373
partial(method, caching=caching))
354374
assert isinstance(img.dataobj, np.ndarray)
355375
assert img.in_memory
356-
data = get_data_func()
376+
with maybe_deprecated(meth_name):
377+
data = get_data_func()
357378
# Returned data same object as underlying dataobj if using
358379
# old ``get_data`` method, or using newer ``get_fdata``
359380
# method, where original array was float64.
360381
arr_dtype = img.dataobj.dtype
361382
dataobj_is_data = arr_dtype == np.float64 or method == img.get_data
362383
# Set something to the output array.
363384
data[:] = 42
364-
get_result_changed = np.all(get_data_func() == 42)
365-
assert (get_result_changed ==
366-
(dataobj_is_data or caching != 'unchanged'))
385+
with maybe_deprecated(meth_name):
386+
get_result_changed = np.all(get_data_func() == 42)
387+
assert get_result_changed == (dataobj_is_data or caching != 'unchanged')
367388
if dataobj_is_data:
368389
assert data is img.dataobj
369390
# Changing array data changes
370391
# data
371392
assert_array_equal(np.asarray(img.dataobj), 42)
372393
# Uncache has no effect
373394
img.uncache()
374-
assert_array_equal(get_data_func(), 42)
395+
with maybe_deprecated(meth_name):
396+
assert_array_equal(get_data_func(), 42)
375397
else:
376398
assert not data is img.dataobj
377399
assert not np.all(np.asarray(img.dataobj) == 42)
378400
# Uncache does have an effect
379401
img.uncache()
380-
assert not np.all(get_data_func() == 42)
402+
with maybe_deprecated(meth_name):
403+
assert not np.all(get_data_func() == 42)
381404
# in_memory is always true for array images, regardless of
382405
# cache state.
383406
img.uncache()
@@ -390,7 +413,8 @@ def _check_array_caching(self, imaker, meth_name, caching):
390413
if arr_dtype not in float_types:
391414
return
392415
for float_type in float_types:
393-
data = get_data_func(dtype=float_type)
416+
with maybe_deprecated(meth_name):
417+
data = get_data_func(dtype=float_type)
394418
assert (data is img.dataobj) == (arr_dtype == float_type)
395419

396420
def validate_data_deprecated(self, imaker, params):
@@ -711,7 +735,7 @@ class TestMinc1API(ImageHeaderAPI):
711735

712736
class TestMinc2API(TestMinc1API):
713737

714-
def __init__(self):
738+
def setup(self):
715739
if not have_h5py:
716740
raise unittest.SkipTest('Need h5py for these tests')
717741

nibabel/tests/test_image_load_save.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from os.path import dirname, join as pjoin
1414
from tempfile import mkdtemp
1515
import pathlib
16+
import logging
1617

1718
import numpy as np
1819

@@ -42,7 +43,7 @@ def round_trip(img):
4243
return Nifti1Image.from_bytes(img.to_bytes())
4344

4445

45-
def test_conversion_spatialimages():
46+
def test_conversion_spatialimages(caplog):
4647
shape = (2, 4, 6)
4748
affine = np.diag([1, 2, 3, 1])
4849
klasses = [klass for klass in all_image_classes
@@ -57,7 +58,9 @@ def test_conversion_spatialimages():
5758
for w_class in klasses:
5859
if not w_class.makeable:
5960
continue
60-
img2 = w_class.from_image(img)
61+
# Suppress header field mismatch reports
62+
with caplog.at_level(logging.CRITICAL):
63+
img2 = w_class.from_image(img)
6164
assert_array_equal(img2.get_fdata(), data)
6265
assert_array_equal(img2.affine, affine)
6366

@@ -271,7 +274,8 @@ def test_analyze_detection():
271274
# Test detection of Analyze, Nifti1 and Nifti2
272275
# Algorithm is as described in loadsave:which_analyze_type
273276
def wat(hdr):
274-
return nils.which_analyze_type(hdr.binaryblock)
277+
with pytest.deprecated_call():
278+
return nils.which_analyze_type(hdr.binaryblock)
275279
n1_hdr = Nifti1Header(b'\0' * 348, check=False)
276280
assert wat(n1_hdr) is None
277281
n1_hdr['sizeof_hdr'] = 540
@@ -300,14 +304,15 @@ def wat(hdr):
300304

301305
def test_guessed_image_type():
302306
# Test whether we can guess the image type from example files
303-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'example4d.nii.gz')) == Nifti1Image
304-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'nifti1.hdr')) == Nifti1Pair
305-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'example_nifti2.nii.gz')) == Nifti2Image
306-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'nifti2.hdr')) == Nifti2Pair
307-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'tiny.mnc')) == Minc1Image
308-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'small.mnc')) == Minc2Image
309-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'test.mgz')) == MGHImage
310-
assert nils.guessed_image_type(pjoin(DATA_PATH, 'analyze.hdr')) == Spm2AnalyzeImage
307+
with pytest.deprecated_call():
308+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'example4d.nii.gz')) == Nifti1Image
309+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'nifti1.hdr')) == Nifti1Pair
310+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'example_nifti2.nii.gz')) == Nifti2Image
311+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'nifti2.hdr')) == Nifti2Pair
312+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'tiny.mnc')) == Minc1Image
313+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'small.mnc')) == Minc2Image
314+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'test.mgz')) == MGHImage
315+
assert nils.guessed_image_type(pjoin(DATA_PATH, 'analyze.hdr')) == Spm2AnalyzeImage
311316

312317

313318
def test_fail_save():

nibabel/tests/test_loadsave.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ def test_read_img_data():
4242
fpath = pathlib.Path(fpath)
4343
img = load(fpath)
4444
data = img.get_fdata()
45-
data2 = read_img_data(img)
45+
with pytest.deprecated_call():
46+
data2 = read_img_data(img)
4647
assert_array_equal(data, data2)
4748
# These examples have null scaling - assert prefer=unscaled is the same
4849
dao = img.dataobj
4950
if hasattr(dao, 'slope') and hasattr(img.header, 'raw_data_from_fileobj'):
5051
assert (dao.slope, dao.inter) == (1, 0)
51-
assert_array_equal(read_img_data(img, prefer='unscaled'), data)
52+
with pytest.deprecated_call():
53+
assert_array_equal(read_img_data(img, prefer='unscaled'), data)
5254
# Assert all caps filename works as well
5355
with TemporaryDirectory() as tmpdir:
5456
up_fpath = pjoin(tmpdir, str(fname).upper())
@@ -86,21 +88,22 @@ def test_read_img_data_nifti():
8688
img = img_class(data, np.eye(4))
8789
img.set_data_dtype(out_dtype)
8890
# No filemap => error
89-
with pytest.raises(ImageFileError):
91+
with pytest.deprecated_call(), pytest.raises(ImageFileError):
9092
read_img_data(img)
9193
# Make a filemap
9294
froot = f'an_image_{i}'
9395
img.file_map = img.filespec_to_file_map(froot)
9496
# Trying to read from this filemap will generate an error because
9597
# we are going to read from files that do not exist
96-
with pytest.raises(IOError):
98+
with pytest.deprecated_call(), pytest.raises(IOError):
9799
read_img_data(img)
98100
img.to_file_map()
99101
# Load - now the scaling and offset correctly applied
100102
img_fname = img.file_map['image'].filename
101103
img_back = load(img_fname)
102104
data_back = img_back.get_fdata()
103-
assert_array_equal(data_back, read_img_data(img_back))
105+
with pytest.deprecated_call():
106+
assert_array_equal(data_back, read_img_data(img_back))
104107
# This is the same as if we loaded the image and header separately
105108
hdr_fname = (img.file_map['header'].filename
106109
if 'header' in img.file_map else img_fname)
@@ -112,15 +115,17 @@ def test_read_img_data_nifti():
112115
# Unscaled is the same as returned from raw_data_from_fileobj
113116
with open(img_fname, 'rb') as fobj:
114117
unscaled_back = hdr_back.raw_data_from_fileobj(fobj)
115-
assert_array_equal(unscaled_back,
116-
read_img_data(img_back, prefer='unscaled'))
118+
with pytest.deprecated_call():
119+
assert_array_equal(unscaled_back, read_img_data(img_back, prefer='unscaled'))
117120
# If we futz with the scaling in the header, the result changes
118-
assert_array_equal(data_back, read_img_data(img_back))
121+
with pytest.deprecated_call():
122+
assert_array_equal(data_back, read_img_data(img_back))
119123
has_inter = hdr_back.has_data_intercept
120124
old_slope = hdr_back['scl_slope']
121125
old_inter = hdr_back['scl_inter'] if has_inter else 0
122126
est_unscaled = (data_back - old_inter) / old_slope
123-
actual_unscaled = read_img_data(img_back, prefer='unscaled')
127+
with pytest.deprecated_call():
128+
actual_unscaled = read_img_data(img_back, prefer='unscaled')
124129
assert_almost_equal(est_unscaled, actual_unscaled)
125130
img_back.header['scl_slope'] = 2.1
126131
if has_inter:
@@ -129,11 +134,13 @@ def test_read_img_data_nifti():
129134
else:
130135
new_inter = 0
131136
# scaled scaling comes from new parameters in header
132-
assert np.allclose(actual_unscaled * 2.1 + new_inter,
133-
read_img_data(img_back))
137+
with pytest.deprecated_call():
138+
assert np.allclose(actual_unscaled * 2.1 + new_inter,
139+
read_img_data(img_back))
134140
# Unscaled array didn't change
135-
assert_array_equal(actual_unscaled,
136-
read_img_data(img_back, prefer='unscaled'))
141+
with pytest.deprecated_call():
142+
assert_array_equal(actual_unscaled,
143+
read_img_data(img_back, prefer='unscaled'))
137144
# Check the offset too
138145
img.header.set_data_offset(1024)
139146
# Delete arrays still pointing to file, so Windows can re-use
@@ -144,12 +151,14 @@ def test_read_img_data_nifti():
144151
fobj.write(b'\x00\x00')
145152
img_back = load(img_fname)
146153
data_back = img_back.get_fdata()
147-
assert_array_equal(data_back, read_img_data(img_back))
154+
with pytest.deprecated_call():
155+
assert_array_equal(data_back, read_img_data(img_back))
148156
img_back.header.set_data_offset(1026)
149157
# Check we pick up new offset
150158
exp_offset = np.zeros((data.size,), data.dtype) + old_inter
151159
exp_offset[:-1] = np.ravel(data_back, order='F')[1:]
152160
exp_offset = np.reshape(exp_offset, shape, order='F')
153-
assert_array_equal(exp_offset, read_img_data(img_back))
161+
with pytest.deprecated_call():
162+
assert_array_equal(exp_offset, read_img_data(img_back))
154163
# Delete stuff that might hold onto file references
155164
del img, img_back, data_back

0 commit comments

Comments
 (0)