|
7 | 7 | #
|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
9 | 9 | ''' Test for image funcs '''
|
| 10 | +from __future__ import with_statement |
10 | 11 |
|
11 |
| -import os |
12 |
| -import tempfile |
13 | 12 | import numpy as np
|
14 | 13 |
|
15 | 14 | from ..funcs import concat_images, as_closest_canonical, OrientationError
|
16 | 15 | from ..nifti1 import Nifti1Image
|
17 | 16 | from ..loadsave import save
|
18 | 17 |
|
| 18 | +from ..tmpdirs import InTemporaryDirectory |
| 19 | + |
19 | 20 | from numpy.testing import assert_array_equal
|
20 | 21 | from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
|
21 | 22 |
|
22 |
| -def test_concat(): |
23 |
| - shape = (1,2,5) |
24 |
| - data0 = np.arange(10).reshape(shape) |
25 |
| - affine = np.eye(4) |
26 |
| - img0 = Nifti1Image(data0, affine) |
27 |
| - data1 = data0 - 10 |
28 |
| - img1 = Nifti1Image(data1, affine) |
29 |
| - all_imgs = concat_images([img0, img1]) |
30 |
| - all_data = np.concatenate( |
31 |
| - [data0[:,:,:,np.newaxis],data1[:,:,:,np.newaxis]],3) |
32 |
| - assert_array_equal(all_imgs.get_data(), all_data) |
33 |
| - assert_array_equal(all_imgs.get_affine(), affine) |
34 |
| - # check that not-matching affines raise error |
35 |
| - img2 = Nifti1Image(data1, affine+1) |
36 |
| - assert_raises(ValueError, concat_images, [img0, img2]) |
37 |
| - img2 = Nifti1Image(data1.T, affine) |
38 |
| - assert_raises(ValueError, concat_images, [img0, img2]) |
39 |
| - # except if check_affines is False |
40 |
| - all_imgs = concat_images([img0, img1]) |
41 |
| - assert_array_equal(all_imgs.get_data(), all_data) |
42 |
| - assert_array_equal(all_imgs.get_affine(), affine) |
| 23 | +_counter = 0 |
| 24 | +def _as_fname(img): |
| 25 | + global _counter |
| 26 | + fname = 'img%3d.nii' % _counter |
| 27 | + _counter = _counter + 1 |
| 28 | + save(img, fname) |
| 29 | + return fname |
43 | 30 |
|
44 | 31 |
|
45 |
| -def test_concat_with_filenames(): |
| 32 | +def test_concat(): |
46 | 33 | shape = (1,2,5)
|
47 | 34 | data0 = np.arange(10).reshape(shape)
|
48 | 35 | affine = np.eye(4)
|
49 |
| - img0 = Nifti1Image(data0, affine) |
50 |
| - img0_path = tempfile.mkstemp(suffix='.nii')[1] |
51 |
| - save(img0, img0_path) |
| 36 | + img0_mem = Nifti1Image(data0, affine) |
52 | 37 | data1 = data0 - 10
|
53 |
| - img1 = Nifti1Image(data1, affine) |
54 |
| - img1_path = tempfile.mkstemp(suffix='.nii')[1] |
55 |
| - save(img1, img1_path) |
56 |
| - all_imgs = concat_images([img0_path, img1_path]) |
| 38 | + img1_mem = Nifti1Image(data1, affine) |
| 39 | + img2_mem = Nifti1Image(data1, affine+1) |
| 40 | + img3_mem = Nifti1Image(data1.T, affine) |
57 | 41 | all_data = np.concatenate(
|
58 | 42 | [data0[:,:,:,np.newaxis],data1[:,:,:,np.newaxis]],3)
|
59 |
| - assert_array_equal(all_imgs.get_data(), all_data) |
60 |
| - assert_array_equal(all_imgs.get_affine(), affine) |
61 |
| - # check that not-matching affines raise error |
62 |
| - img2 = Nifti1Image(data1, affine+1) |
63 |
| - assert_raises(ValueError, concat_images, [img0, img2]) |
64 |
| - img2 = Nifti1Image(data1.T, affine) |
65 |
| - assert_raises(ValueError, concat_images, [img0, img2]) |
66 |
| - # except if check_affines is False |
67 |
| - all_imgs = concat_images([img0, img1]) |
68 |
| - assert_array_equal(all_imgs.get_data(), all_data) |
69 |
| - assert_array_equal(all_imgs.get_affine(), affine) |
70 |
| - os.remove(img0_path) |
71 |
| - os.remove(img1_path) |
| 43 | + # Check filenames and in-memory images work |
| 44 | + with InTemporaryDirectory(): |
| 45 | + imgs = [img0_mem, img1_mem, img2_mem, img3_mem] |
| 46 | + img_files = [_as_fname(img) for img in imgs] |
| 47 | + for img0, img1, img2, img3 in (imgs, img_files): |
| 48 | + all_imgs = concat_images([img0, img1]) |
| 49 | + assert_array_equal(all_imgs.get_data(), all_data) |
| 50 | + assert_array_equal(all_imgs.get_affine(), affine) |
| 51 | + # check that not-matching affines raise error |
| 52 | + assert_raises(ValueError, concat_images, [img0, img2]) |
| 53 | + assert_raises(ValueError, concat_images, [img0, img3]) |
| 54 | + # except if check_affines is False |
| 55 | + all_imgs = concat_images([img0, img1]) |
| 56 | + assert_array_equal(all_imgs.get_data(), all_data) |
| 57 | + assert_array_equal(all_imgs.get_affine(), affine) |
| 58 | + # Delete images as prophylaxis for windows access errors |
| 59 | + for img in imgs: |
| 60 | + del(img) |
72 | 61 |
|
73 | 62 |
|
74 | 63 | def test_closest_canonical():
|
|
0 commit comments