8
8
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9
9
''' Test for image funcs '''
10
10
11
+ import os
12
+ import tempfile
11
13
import numpy as np
12
14
13
15
from ..funcs import concat_images , as_closest_canonical , OrientationError
14
16
from ..nifti1 import Nifti1Image
17
+ from ..loadsave import save
15
18
16
19
from numpy .testing import assert_array_equal
17
20
from nose .tools import (assert_true , assert_false , assert_equal , assert_raises )
18
21
19
-
20
22
def test_concat ():
21
23
shape = (1 ,2 ,5 )
22
24
data0 = np .arange (10 ).reshape (shape )
@@ -40,6 +42,35 @@ def test_concat():
40
42
assert_array_equal (all_imgs .get_affine (), affine )
41
43
42
44
45
+ def test_concat_with_filenames ():
46
+ shape = (1 ,2 ,5 )
47
+ data0 = np .arange (10 ).reshape (shape )
48
+ affine = np .eye (4 )
49
+ img0 = Nifti1Image (data0 , affine )
50
+ img0_path = tempfile .mkstemp (suffix = '.nii' )[1 ]
51
+ save (img0 , img0_path )
52
+ 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 ])
57
+ all_data = np .concatenate (
58
+ [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 )
72
+
73
+
43
74
def test_closest_canonical ():
44
75
arr = np .arange (24 ).reshape ((2 ,3 ,4 ,1 ))
45
76
# no funky stuff, returns same thing
0 commit comments