|
27 | 27 |
|
28 | 28 | import warnings |
29 | 29 | from functools import partial |
| 30 | +from itertools import product |
30 | 31 | from six import string_types |
31 | 32 |
|
32 | 33 | import numpy as np |
@@ -481,40 +482,49 @@ class MakeImageAPI(LoadImageAPI): |
481 | 482 | header_maker = None |
482 | 483 | # Example shapes for created images |
483 | 484 | 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) |
484 | 487 |
|
485 | 488 | def obj_params(self): |
486 | 489 | # Return any obj_params from superclass |
487 | 490 | for func, params in super(MakeImageAPI, self).obj_params(): |
488 | 491 | yield func, params |
489 | | - # Create a new images |
| 492 | + # Create new images |
490 | 493 | aff = np.diag([1, 2, 3, 1]) |
491 | 494 |
|
492 | 495 | def make_imaker(arr, aff, header=None): |
493 | 496 | 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 |
512 | 497 |
|
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 |
518 | 528 |
|
519 | 529 |
|
520 | 530 | class ImageHeaderAPI(MakeImageAPI): |
|
0 commit comments