Skip to content

Commit 32c5f2e

Browse files
committed
RF: rewrite return of array / proxy test images.
Make dtypes explicit for return, in order to allow sub-classes to override the storable dtypes.
1 parent 825e047 commit 32c5f2e

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

nibabel/tests/test_image_api.py

+34-24
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import warnings
2929
from functools import partial
30+
from itertools import product
3031
from six import string_types
3132

3233
import numpy as np
@@ -481,40 +482,49 @@ class MakeImageAPI(LoadImageAPI):
481482
header_maker = None
482483
# Example shapes for created images
483484
example_shapes = ((2,), (2, 3), (2, 3, 4), (2, 3, 4, 5))
485+
# Supported dtypes for storing to disk
486+
storable_dtypes = (np.uint8, np.int16, np.float32)
484487

485488
def obj_params(self):
486489
# Return any obj_params from superclass
487490
for func, params in super(MakeImageAPI, self).obj_params():
488491
yield func, params
489-
# Create a new images
492+
# Create new images
490493
aff = np.diag([1, 2, 3, 1])
491494

492495
def make_imaker(arr, aff, header=None):
493496
return lambda: self.image_maker(arr, aff, header)
494-
for shape in self.example_shapes:
495-
for dtype in (np.uint8, np.int16, np.float32):
496-
arr = np.arange(np.prod(shape), dtype=np.float32).reshape(shape)
497-
hdr = self.header_maker()
498-
hdr.set_data_dtype(dtype)
499-
func = make_imaker(arr.copy(), aff, hdr)
500-
params = dict(
501-
dtype=dtype,
502-
affine=aff,
503-
data=arr,
504-
shape=shape,
505-
is_proxy=False)
506-
yield func, params
507-
if not self.can_save:
508-
return
509-
# Add a proxy image
510-
# We assume that loading from a fileobj creates a proxy image
511-
params['is_proxy'] = True
512497

513-
def prox_imaker():
514-
img = self.image_maker(arr, aff, hdr)
515-
rt_img = bytesio_round_trip(img)
516-
return self.image_maker(rt_img.dataobj, aff, rt_img.header)
517-
yield prox_imaker, params
498+
def make_prox_imaker(arr, aff, hdr):
499+
500+
def prox_imaker():
501+
img = self.image_maker(arr, aff, hdr)
502+
rt_img = bytesio_round_trip(img)
503+
return self.image_maker(rt_img.dataobj, aff, rt_img.header)
504+
505+
return prox_imaker
506+
507+
for shape, stored_dtype in product(self.example_shapes,
508+
self.storable_dtypes):
509+
# To make sure we do not trigger scaling, always use the
510+
# stored_dtype for the input array.
511+
arr = np.arange(np.prod(shape), dtype=stored_dtype).reshape(shape)
512+
hdr = self.header_maker()
513+
hdr.set_data_dtype(stored_dtype)
514+
func = make_imaker(arr.copy(), aff, hdr)
515+
params = dict(
516+
dtype=stored_dtype,
517+
affine=aff,
518+
data=arr,
519+
shape=shape,
520+
is_proxy=False)
521+
yield make_imaker(arr.copy(), aff, hdr), params
522+
if not self.can_save:
523+
continue
524+
# Create proxy images from these array images, by storing via BytesIO.
525+
# We assume that loading from a fileobj creates a proxy image.
526+
params['is_proxy'] = True
527+
yield make_prox_imaker(arr.copy(), aff, hdr), params
518528

519529

520530
class ImageHeaderAPI(MakeImageAPI):

0 commit comments

Comments
 (0)