26
26
import warnings
27
27
from functools import partial
28
28
from itertools import product
29
+ from contextlib import nullcontext
29
30
import pathlib
30
31
31
32
import numpy as np
56
57
from .test_parrec import EXAMPLE_IMAGES as PARREC_EXAMPLE_IMAGES
57
58
from .test_brikhead import EXAMPLE_IMAGES as AFNI_EXAMPLE_IMAGES
58
59
60
+
61
+ def maybe_deprecated (meth_name ):
62
+ return pytest .deprecated_call () if meth_name == 'get_data' else nullcontext ()
63
+
64
+
59
65
class GenericImageAPI (ValidateAPI ):
60
66
""" General image validation API """
61
67
# Whether this image type can do scaling of data
@@ -221,14 +227,17 @@ def validate_data_interface(self, imaker, params):
221
227
self ._check_proxy_interface (imaker , meth_name )
222
228
else : # Array image
223
229
self ._check_array_interface (imaker , meth_name )
230
+ method = getattr (img , meth_name )
224
231
# 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
226
234
# 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
228
237
# Values to get_data caching parameter must be 'fill' or
229
238
# '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' )
232
241
# dataobj is read only
233
242
fake_data = np .zeros (img .shape ).astype (img .get_data_dtype ())
234
243
with pytest .raises (AttributeError ):
@@ -251,11 +260,13 @@ def _check_proxy_interface(self, imaker, meth_name):
251
260
assert not img .in_memory
252
261
# Load with caching='unchanged'
253
262
method = getattr (img , meth_name )
254
- data = method (caching = 'unchanged' )
263
+ with maybe_deprecated (meth_name ):
264
+ data = method (caching = 'unchanged' )
255
265
# Still not cached
256
266
assert not img .in_memory
257
267
# Default load, does caching
258
- data = method ()
268
+ with maybe_deprecated (meth_name ):
269
+ data = method ()
259
270
# Data now cached. in_memory is True if either of the get_data
260
271
# or get_fdata caches are not-None
261
272
assert img .in_memory
@@ -267,30 +278,36 @@ def _check_proxy_interface(self, imaker, meth_name):
267
278
# integers, but lets assume that's not true here.
268
279
assert_array_equal (proxy_data , data )
269
280
# 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' )
271
283
assert data is data_again
272
284
# 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' )
274
287
assert data is data_yet_again
275
288
# changing array data does not change proxy data, or reloaded
276
289
# data
277
290
data [:] = 42
278
291
assert_array_equal (proxy_data , proxy_copy )
279
292
assert_array_equal (np .asarray (img .dataobj ), proxy_copy )
280
293
# 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 )
282
296
# until we uncache
283
297
img .uncache ()
284
298
# Which unsets in_memory
285
299
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 )
287
302
# Check caching='fill' does cache data
288
303
img = imaker ()
289
304
method = getattr (img , meth_name )
290
305
assert not img .in_memory
291
- data = method (caching = 'fill' )
306
+ with maybe_deprecated (meth_name ):
307
+ data = method (caching = 'fill' )
292
308
assert img .in_memory
293
- data_again = method ()
309
+ with maybe_deprecated (meth_name ):
310
+ data_again = method ()
294
311
assert data is data_again
295
312
# Check the interaction of caching with get_data, get_fdata.
296
313
# Caching for `get_data` should have no effect on caching for
@@ -300,14 +317,17 @@ def _check_proxy_interface(self, imaker, meth_name):
300
317
# Load using the other data fetch method
301
318
other_name = set (self .meth_names ).difference ({meth_name }).pop ()
302
319
other_method = getattr (img , other_name )
303
- other_data = other_method ()
320
+ with maybe_deprecated (other_name ):
321
+ other_data = other_method ()
304
322
# We get the original data, not the modified cache
305
323
assert_array_equal (proxy_data , other_data )
306
324
assert not np .all (data == other_data )
307
325
# We can modify the other cache, without affecting the first
308
326
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 ())
311
331
if meth_name != 'get_fdata' :
312
332
return
313
333
# Check that caching refreshes for new floating point type.
@@ -353,31 +373,34 @@ def _check_array_caching(self, imaker, meth_name, caching):
353
373
partial (method , caching = caching ))
354
374
assert isinstance (img .dataobj , np .ndarray )
355
375
assert img .in_memory
356
- data = get_data_func ()
376
+ with maybe_deprecated (meth_name ):
377
+ data = get_data_func ()
357
378
# Returned data same object as underlying dataobj if using
358
379
# old ``get_data`` method, or using newer ``get_fdata``
359
380
# method, where original array was float64.
360
381
arr_dtype = img .dataobj .dtype
361
382
dataobj_is_data = arr_dtype == np .float64 or method == img .get_data
362
383
# Set something to the output array.
363
384
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' )
367
388
if dataobj_is_data :
368
389
assert data is img .dataobj
369
390
# Changing array data changes
370
391
# data
371
392
assert_array_equal (np .asarray (img .dataobj ), 42 )
372
393
# Uncache has no effect
373
394
img .uncache ()
374
- assert_array_equal (get_data_func (), 42 )
395
+ with maybe_deprecated (meth_name ):
396
+ assert_array_equal (get_data_func (), 42 )
375
397
else :
376
398
assert not data is img .dataobj
377
399
assert not np .all (np .asarray (img .dataobj ) == 42 )
378
400
# Uncache does have an effect
379
401
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 )
381
404
# in_memory is always true for array images, regardless of
382
405
# cache state.
383
406
img .uncache ()
@@ -390,7 +413,8 @@ def _check_array_caching(self, imaker, meth_name, caching):
390
413
if arr_dtype not in float_types :
391
414
return
392
415
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 )
394
418
assert (data is img .dataobj ) == (arr_dtype == float_type )
395
419
396
420
def validate_data_deprecated (self , imaker , params ):
@@ -711,7 +735,7 @@ class TestMinc1API(ImageHeaderAPI):
711
735
712
736
class TestMinc2API (TestMinc1API ):
713
737
714
- def __init__ (self ):
738
+ def setup (self ):
715
739
if not have_h5py :
716
740
raise unittest .SkipTest ('Need h5py for these tests' )
717
741
0 commit comments