|
| 1 | +from algorithms.iterables import ( |
| 2 | + convolved_1d, convolved_2d |
| 3 | +) |
| 4 | + |
| 5 | +import itertools |
| 6 | +import unittest |
| 7 | + |
| 8 | + |
| 9 | +class TestConvolved1D(unittest.TestCase): |
| 10 | + """Tests copied from: |
| 11 | + - https://github.com/guillaume-chevalier/python-conv-lib/blob/master/conv/tests/test_convolved_1d.py |
| 12 | + - MIT License, Copyright (c) 2018 Guillaume Chevalier |
| 13 | + """ |
| 14 | + |
| 15 | + def test_trivial_loop(self): |
| 16 | + expected = tuple(range(7)) |
| 17 | + result = [] |
| 18 | + |
| 19 | + for kernel_hover in convolved_1d(expected, kernel_size=1, padding=0, stride=1): |
| 20 | + result.append(*kernel_hover) |
| 21 | + result = tuple(result) |
| 22 | + |
| 23 | + self.assertEqual(expected, result) |
| 24 | + |
| 25 | + def test_seven_for_with_stride_two(self): |
| 26 | + expected = tuple(range(0, 7, 2)) |
| 27 | + result = [] |
| 28 | + |
| 29 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=1, padding=0, stride=2): |
| 30 | + result.append(*kernel_hover) |
| 31 | + result = tuple(result) |
| 32 | + |
| 33 | + self.assertEqual(expected, result) |
| 34 | + |
| 35 | + def test_seven_for_with_padding_one(self): |
| 36 | + expected = tuple([42] + list(range(0, 7)) + [42]) |
| 37 | + result = [] |
| 38 | + |
| 39 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=1, padding=1, stride=1, default_value=42): |
| 40 | + result.append(*kernel_hover) |
| 41 | + result = tuple(result) |
| 42 | + |
| 43 | + self.assertEqual(expected, result) |
| 44 | + |
| 45 | + def test_seven_kernel_of_two(self): |
| 46 | + expected = tuple([a, b] for a, b in zip(list(range(0, 6)), list(range(1, 7)))) |
| 47 | + result = [] |
| 48 | + |
| 49 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=2, padding=0, stride=1): |
| 50 | + result.append(kernel_hover) |
| 51 | + result = tuple(result) |
| 52 | + |
| 53 | + self.assertEqual(expected, result) |
| 54 | + |
| 55 | + def test_seven_kernel_of_two_and_stride_two(self): |
| 56 | + expected = ([0, 1], [2, 3], [4, 5], [6, None]) |
| 57 | + result = [] |
| 58 | + |
| 59 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=2, padding=0, stride=2): |
| 60 | + result.append(kernel_hover) |
| 61 | + result = tuple(result) |
| 62 | + |
| 63 | + self.assertEqual(expected, result) |
| 64 | + |
| 65 | + def test_seven_kernel_of_two_and_stride_two_and_padding_two(self): |
| 66 | + expected = ([None, None], [0, 1], [2, 3], [4, 5], [6, None], [None, None]) |
| 67 | + result = [] |
| 68 | + |
| 69 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=2, padding=2, stride=2): |
| 70 | + result.append(kernel_hover) |
| 71 | + result = tuple(result) |
| 72 | + |
| 73 | + self.assertEqual(expected, result) |
| 74 | + |
| 75 | + def test_seven_kernel_of_two_and_stride_two_and_padding_three(self): |
| 76 | + expected = ([None, None], [None, 0], [1, 2], [3, 4], [5, 6], [None, None], [None, None]) |
| 77 | + result = [] |
| 78 | + |
| 79 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=2, padding=3, stride=2): |
| 80 | + result.append(kernel_hover) |
| 81 | + result = tuple(result) |
| 82 | + |
| 83 | + self.assertEqual(expected, result) |
| 84 | + |
| 85 | + def test_seven_kernel_of_three_and_stride_two_and_padding_two(self): |
| 86 | + expected = ([None, None, 0], [0, 1, 2], [2, 3, 4], [4, 5, 6], [6, None, None]) |
| 87 | + result = [] |
| 88 | + |
| 89 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=3, padding=2, stride=2): |
| 90 | + result.append(kernel_hover) |
| 91 | + result = tuple(result) |
| 92 | + |
| 93 | + self.assertEqual(expected, result) |
| 94 | + |
| 95 | + def test_seven_kernel_of_two_and_stride_three_and_padding_two(self): |
| 96 | + expected = ([None, None], [1, 2], [4, 5], [None, None]) |
| 97 | + result = [] |
| 98 | + |
| 99 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=2, padding=2, stride=3): |
| 100 | + result.append(kernel_hover) |
| 101 | + result = tuple(result) |
| 102 | + |
| 103 | + self.assertEqual(expected, result) |
| 104 | + |
| 105 | + def test_seven_kernel_of_three_and_stride_three_and_padding_three(self): |
| 106 | + expected = ([None, None, None], [0, 1, 2], [3, 4, 5], [6, None, None], [None, None, None]) |
| 107 | + result = [] |
| 108 | + |
| 109 | + for kernel_hover in convolved_1d(list(range(7)), kernel_size=3, padding=3, stride=3): |
| 110 | + result.append(kernel_hover) |
| 111 | + result = tuple(result) |
| 112 | + |
| 113 | + self.assertEqual(expected, result) |
| 114 | + |
| 115 | + |
| 116 | +class TestConvolved2D(unittest.TestCase): |
| 117 | + """Tests copied from: |
| 118 | + - https://github.com/guillaume-chevalier/python-conv-lib/blob/master/conv/tests/test_convolved_1d.py |
| 119 | + - MIT License, Copyright (c) 2018 Guillaume Chevalier |
| 120 | + """ |
| 121 | + |
| 122 | + def test_trivial_1x1_loop(self): |
| 123 | + base = tuple(tuple(range(i ** 2, 7 + i ** 2)) for i in range(7)) |
| 124 | + expected = tuple(([i],) for i in itertools.chain(*base)) |
| 125 | + result = [] |
| 126 | + |
| 127 | + for kernel_hover in convolved_2d(base, kernel_size=1, padding=0, stride=1): |
| 128 | + result.append(tuple(kernel_hover)) |
| 129 | + result = tuple(result) |
| 130 | + |
| 131 | + self.assertEqual(expected, result) |
| 132 | + |
| 133 | + def test_simple_2x2_loop_on_3x2(self): |
| 134 | + base = [ |
| 135 | + [1, 2], |
| 136 | + [3, 4], |
| 137 | + [5, 6] |
| 138 | + ] |
| 139 | + expected = ( |
| 140 | + ( |
| 141 | + [1, 2], |
| 142 | + [3, 4] |
| 143 | + ), ( |
| 144 | + [3, 4], |
| 145 | + [5, 6] |
| 146 | + ) |
| 147 | + ) |
| 148 | + result = [] |
| 149 | + |
| 150 | + for kernel_hover in convolved_2d(base, kernel_size=2, padding=0, stride=1): |
| 151 | + result.append(tuple(kernel_hover)) |
| 152 | + result = tuple(result) |
| 153 | + |
| 154 | + print(result) |
| 155 | + print(expected) |
| 156 | + |
| 157 | + self.assertEqual(expected, result) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + unittest.main() |
0 commit comments