scikit-image
130 строк · 3.9 Кб
1import numpy as np2from skimage.measure import block_reduce3
4from skimage._shared import testing5from skimage._shared.testing import assert_equal6
7
8def test_block_reduce_sum():9image1 = np.arange(4 * 6).reshape(4, 6)10out1 = block_reduce(image1, (2, 3))11expected1 = np.array([[24, 42], [96, 114]])12assert_equal(expected1, out1)13
14image2 = np.arange(5 * 8).reshape(5, 8)15out2 = block_reduce(image2, (3, 3))16expected2 = np.array([[81, 108, 87], [174, 192, 138]])17assert_equal(expected2, out2)18
19
20def test_block_reduce_mean():21image1 = np.arange(4 * 6).reshape(4, 6)22out1 = block_reduce(image1, (2, 3), func=np.mean)23expected1 = np.array([[4.0, 7.0], [16.0, 19.0]])24assert_equal(expected1, out1)25
26image2 = np.arange(5 * 8).reshape(5, 8)27out2 = block_reduce(image2, (4, 5), func=np.mean)28expected2 = np.array([[14.0, 10.8], [8.5, 5.7]])29assert_equal(expected2, out2)30
31
32def test_block_reduce_median():33image1 = np.arange(4 * 6).reshape(4, 6)34out1 = block_reduce(image1, (2, 3), func=np.median)35expected1 = np.array([[4.0, 7.0], [16.0, 19.0]])36assert_equal(expected1, out1)37
38image2 = np.arange(5 * 8).reshape(5, 8)39out2 = block_reduce(image2, (4, 5), func=np.median)40expected2 = np.array([[14.0, 6.5], [0.0, 0.0]])41assert_equal(expected2, out2)42
43image3 = np.array([[1, 5, 5, 5], [5, 5, 5, 1000]])44out3 = block_reduce(image3, (2, 4), func=np.median)45assert_equal(5, out3)46
47
48def test_block_reduce_min():49image1 = np.arange(4 * 6).reshape(4, 6)50out1 = block_reduce(image1, (2, 3), func=np.min)51expected1 = np.array([[0, 3], [12, 15]])52assert_equal(expected1, out1)53
54image2 = np.arange(5 * 8).reshape(5, 8)55out2 = block_reduce(image2, (4, 5), func=np.min)56expected2 = np.array([[0, 0], [0, 0]])57assert_equal(expected2, out2)58
59
60def test_block_reduce_max():61image1 = np.arange(4 * 6).reshape(4, 6)62out1 = block_reduce(image1, (2, 3), func=np.max)63expected1 = np.array([[8, 11], [20, 23]])64assert_equal(expected1, out1)65
66image2 = np.arange(5 * 8).reshape(5, 8)67out2 = block_reduce(image2, (4, 5), func=np.max)68expected2 = np.array([[28, 31], [36, 39]])69assert_equal(expected2, out2)70
71
72def test_invalid_block_size():73image = np.arange(4 * 6).reshape(4, 6)74
75with testing.raises(ValueError):76block_reduce(image, [1, 2, 3])77with testing.raises(ValueError):78block_reduce(image, [1, 0.5])79
80
81def test_default_block_size():82image = np.arange(4 * 6).reshape(4, 6)83out = block_reduce(image, func=np.min)84expected = np.array([[0, 2, 4], [12, 14, 16]])85assert_equal(expected, out)86
87
88def test_scalar_block_size():89image = np.arange(6 * 6).reshape(6, 6)90out = block_reduce(image, 3, func=np.min)91expected1 = np.array([[0, 3], [18, 21]])92assert_equal(expected1, out)93expected2 = block_reduce(image, (3, 3), func=np.min)94assert_equal(expected2, out)95
96
97def test_func_kwargs_same_dtype():98image = np.array(99[100[97, 123, 173, 227],101[217, 241, 221, 214],102[211, 11, 170, 53],103[214, 205, 101, 57],104],105dtype=np.uint8,106)107
108out = block_reduce(image, (2, 2), func=np.mean, func_kwargs={'dtype': np.uint8})109expected = np.array([[41, 16], [32, 31]], dtype=np.uint8)110
111assert_equal(out, expected)112assert out.dtype == expected.dtype113
114
115def test_func_kwargs_different_dtype():116image = 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],123dtype=np.float64,124)125
126out = block_reduce(image, (2, 2), func=np.mean, func_kwargs={'dtype': np.float16})127expected = np.array([[0.6855, 0.3164], [0.4922, 0.521]], dtype=np.float16)128
129assert_equal(out, expected)130assert out.dtype == expected.dtype131