scikit-image

Форк
0
130 строк · 3.9 Кб
1
import numpy as np
2
from skimage.measure import block_reduce
3

4
from skimage._shared import testing
5
from skimage._shared.testing import assert_equal
6

7

8
def test_block_reduce_sum():
9
    image1 = np.arange(4 * 6).reshape(4, 6)
10
    out1 = block_reduce(image1, (2, 3))
11
    expected1 = np.array([[24, 42], [96, 114]])
12
    assert_equal(expected1, out1)
13

14
    image2 = np.arange(5 * 8).reshape(5, 8)
15
    out2 = block_reduce(image2, (3, 3))
16
    expected2 = np.array([[81, 108, 87], [174, 192, 138]])
17
    assert_equal(expected2, out2)
18

19

20
def test_block_reduce_mean():
21
    image1 = np.arange(4 * 6).reshape(4, 6)
22
    out1 = block_reduce(image1, (2, 3), func=np.mean)
23
    expected1 = np.array([[4.0, 7.0], [16.0, 19.0]])
24
    assert_equal(expected1, out1)
25

26
    image2 = np.arange(5 * 8).reshape(5, 8)
27
    out2 = block_reduce(image2, (4, 5), func=np.mean)
28
    expected2 = np.array([[14.0, 10.8], [8.5, 5.7]])
29
    assert_equal(expected2, out2)
30

31

32
def test_block_reduce_median():
33
    image1 = np.arange(4 * 6).reshape(4, 6)
34
    out1 = block_reduce(image1, (2, 3), func=np.median)
35
    expected1 = np.array([[4.0, 7.0], [16.0, 19.0]])
36
    assert_equal(expected1, out1)
37

38
    image2 = np.arange(5 * 8).reshape(5, 8)
39
    out2 = block_reduce(image2, (4, 5), func=np.median)
40
    expected2 = np.array([[14.0, 6.5], [0.0, 0.0]])
41
    assert_equal(expected2, out2)
42

43
    image3 = np.array([[1, 5, 5, 5], [5, 5, 5, 1000]])
44
    out3 = block_reduce(image3, (2, 4), func=np.median)
45
    assert_equal(5, out3)
46

47

48
def test_block_reduce_min():
49
    image1 = np.arange(4 * 6).reshape(4, 6)
50
    out1 = block_reduce(image1, (2, 3), func=np.min)
51
    expected1 = np.array([[0, 3], [12, 15]])
52
    assert_equal(expected1, out1)
53

54
    image2 = np.arange(5 * 8).reshape(5, 8)
55
    out2 = block_reduce(image2, (4, 5), func=np.min)
56
    expected2 = np.array([[0, 0], [0, 0]])
57
    assert_equal(expected2, out2)
58

59

60
def test_block_reduce_max():
61
    image1 = np.arange(4 * 6).reshape(4, 6)
62
    out1 = block_reduce(image1, (2, 3), func=np.max)
63
    expected1 = np.array([[8, 11], [20, 23]])
64
    assert_equal(expected1, out1)
65

66
    image2 = np.arange(5 * 8).reshape(5, 8)
67
    out2 = block_reduce(image2, (4, 5), func=np.max)
68
    expected2 = np.array([[28, 31], [36, 39]])
69
    assert_equal(expected2, out2)
70

71

72
def test_invalid_block_size():
73
    image = np.arange(4 * 6).reshape(4, 6)
74

75
    with testing.raises(ValueError):
76
        block_reduce(image, [1, 2, 3])
77
    with testing.raises(ValueError):
78
        block_reduce(image, [1, 0.5])
79

80

81
def test_default_block_size():
82
    image = np.arange(4 * 6).reshape(4, 6)
83
    out = block_reduce(image, func=np.min)
84
    expected = np.array([[0, 2, 4], [12, 14, 16]])
85
    assert_equal(expected, out)
86

87

88
def test_scalar_block_size():
89
    image = np.arange(6 * 6).reshape(6, 6)
90
    out = block_reduce(image, 3, func=np.min)
91
    expected1 = np.array([[0, 3], [18, 21]])
92
    assert_equal(expected1, out)
93
    expected2 = block_reduce(image, (3, 3), func=np.min)
94
    assert_equal(expected2, out)
95

96

97
def test_func_kwargs_same_dtype():
98
    image = np.array(
99
        [
100
            [97, 123, 173, 227],
101
            [217, 241, 221, 214],
102
            [211, 11, 170, 53],
103
            [214, 205, 101, 57],
104
        ],
105
        dtype=np.uint8,
106
    )
107

108
    out = block_reduce(image, (2, 2), func=np.mean, func_kwargs={'dtype': np.uint8})
109
    expected = np.array([[41, 16], [32, 31]], dtype=np.uint8)
110

111
    assert_equal(out, expected)
112
    assert out.dtype == expected.dtype
113

114

115
def test_func_kwargs_different_dtype():
116
    image = np.array(
117
        [
118
            [0.45745366, 0.67479345, 0.20949775, 0.3147348],
119
            [0.7209286, 0.88915504, 0.66153409, 0.07919526],
120
            [0.04640037, 0.54008495, 0.34664343, 0.56152301],
121
            [0.58085003, 0.80144708, 0.87844473, 0.29811511],
122
        ],
123
        dtype=np.float64,
124
    )
125

126
    out = block_reduce(image, (2, 2), func=np.mean, func_kwargs={'dtype': np.float16})
127
    expected = np.array([[0.6855, 0.3164], [0.4922, 0.521]], dtype=np.float16)
128

129
    assert_equal(out, expected)
130
    assert out.dtype == expected.dtype
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.