forked from scikit-image/scikit-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_montage.py
102 lines (76 loc) · 2.78 KB
/
test_montage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from nose.tools import assert_equal, raises
from numpy.testing import assert_array_equal
import numpy as np
from skimage.util.montage import montage2d
def test_simple():
n_images = 3
height, width = 2, 3,
arr_in = np.arange(n_images * height * width)
arr_in = arr_in.reshape(n_images, height, width)
arr_out = montage2d(arr_in)
gt = np.array(
[[ 0. , 1. , 2. , 6. , 7. , 8. ],
[ 3. , 4. , 5. , 9. , 10. , 11. ],
[ 12. , 13. , 14. , 8.5, 8.5, 8.5],
[ 15. , 16. , 17. , 8.5, 8.5, 8.5]]
)
assert_array_equal(arr_out, gt)
def test_fill():
n_images = 3
height, width = 2, 3,
arr_in = np.arange(n_images * height * width)
arr_in = arr_in.reshape(n_images, height, width)
arr_out = montage2d(arr_in, fill=0)
gt = np.array(
[[ 0. , 1. , 2. , 6. , 7. , 8. ],
[ 3. , 4. , 5. , 9. , 10. , 11. ],
[ 12. , 13. , 14. , 0. , 0. , 0. ],
[ 15. , 16. , 17. , 0. , 0. , 0. ]]
)
assert_array_equal(arr_out, gt)
def test_shape():
n_images = 15
height, width = 11, 7
arr_in = np.arange(n_images * height * width)
arr_in = arr_in.reshape(n_images, height, width)
alpha = int(np.ceil(np.sqrt(n_images)))
arr_out = montage2d(arr_in)
assert_equal(arr_out.shape, (alpha * height, alpha * width))
def test_grid_shape():
n_images = 6
height, width = 2, 2
arr_in = np.arange(n_images * height * width, dtype=np.float32)
arr_in = arr_in.reshape(n_images, height, width)
arr_out = montage2d(arr_in, grid_shape=(3,2))
correct_arr_out = np.array(
[[ 0., 1., 4., 5.],
[ 2., 3., 6., 7.],
[ 8., 9., 12., 13.],
[ 10., 11., 14., 15.],
[ 16., 17., 20., 21.],
[ 18., 19., 22., 23.]]
)
assert_array_equal(arr_out, correct_arr_out)
def test_rescale_intensity():
n_images = 4
height, width = 3, 3
arr_in = np.arange(n_images * height * width, dtype=np.float32)
arr_in = arr_in.reshape(n_images, height, width)
arr_out = montage2d(arr_in, rescale_intensity=True)
gt = np.array(
[[ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ],
[ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625],
[ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ],
[ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ],
[ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625],
[ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ]]
)
assert_equal(arr_out.min(), 0.0)
assert_equal(arr_out.max(), 1.0)
assert_array_equal(arr_out, gt)
@raises(AssertionError)
def test_error_ndim():
arr_error = np.random.randn(1, 2, 3, 4)
montage2d(arr_error)
if __name__ == '__main__':
np.testing.run_module_suite()