|
10 | 10 | from numpy.testing import TestCase, run_module_suite, assert_array_almost_equal |
11 | 11 | from numpy import fft |
12 | 12 | from numpy import pi |
| 13 | +from numpy.fft.helper import _FFTCache |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class TestFFTShift(TestCase): |
@@ -74,5 +75,91 @@ def test_not_last_axis_success(self): |
74 | 75 | fft.irfftn(a, axes=axes) |
75 | 76 |
|
76 | 77 |
|
| 78 | +class TestFFTCache(TestCase): |
| 79 | + |
| 80 | + def test_basic_behaviour(self): |
| 81 | + c = _FFTCache(max_size_in_mb=1, max_item_count=4) |
| 82 | + # Setting |
| 83 | + c[1] = [np.ones(2, dtype=np.float32)] |
| 84 | + c[2] = [np.zeros(2, dtype=np.float32)] |
| 85 | + # Getting |
| 86 | + assert_array_almost_equal(c[1][0], np.ones(2, dtype=np.float32)) |
| 87 | + assert_array_almost_equal(c[2][0], np.zeros(2, dtype=np.float32)) |
| 88 | + # Setdefault |
| 89 | + c.setdefault(1, [np.array([1, 2], dtype=np.float32)]) |
| 90 | + assert_array_almost_equal(c[1][0], np.ones(2, dtype=np.float32)) |
| 91 | + c.setdefault(3, [np.array([1, 2], dtype=np.float32)]) |
| 92 | + assert_array_almost_equal(c[3][0], np.array([1, 2], dtype=np.float32)) |
| 93 | + |
| 94 | + self.assertEqual(len(c._dict), 3) |
| 95 | + |
| 96 | + def test_automatic_pruning(self): |
| 97 | + # Thats around 2600 single precision samples. |
| 98 | + c = _FFTCache(max_size_in_mb=0.01, max_item_count=4) |
| 99 | + c[1] = [np.ones(200, dtype=np.float32)] |
| 100 | + c[2] = [np.ones(200, dtype=np.float32)] |
| 101 | + |
| 102 | + # Don't raise errors. |
| 103 | + c[1], c[2], c[1], c[2] |
| 104 | + |
| 105 | + # This is larger than the limit but should still be kept. |
| 106 | + c[3] = [np.ones(3000, dtype=np.float32)] |
| 107 | + # Should exist. |
| 108 | + c[1], c[2], c[3] |
| 109 | + # Add one more. |
| 110 | + c[4] = [np.ones(3000, dtype=np.float32)] |
| 111 | + |
| 112 | + # The other three should no longer exist. |
| 113 | + with self.assertRaises(KeyError): |
| 114 | + c[1] |
| 115 | + with self.assertRaises(KeyError): |
| 116 | + c[2] |
| 117 | + with self.assertRaises(KeyError): |
| 118 | + c[3] |
| 119 | + |
| 120 | + # Now test the max item count pruning. |
| 121 | + c = _FFTCache(max_size_in_mb=0.01, max_item_count=2) |
| 122 | + c[1] = [np.empty(2)] |
| 123 | + c[2] = [np.empty(2)] |
| 124 | + # Can still be accessed. |
| 125 | + c[2], c[1] |
| 126 | + |
| 127 | + c[3] = [np.empty(2)] |
| 128 | + |
| 129 | + # 1 and 3 can still be accessed - c[2] has been touched least recently |
| 130 | + # and is thus evicted. |
| 131 | + c[1], c[3] |
| 132 | + |
| 133 | + with self.assertRaises(KeyError): |
| 134 | + c[2] |
| 135 | + |
| 136 | + c[1], c[3] |
| 137 | + |
| 138 | + # One last test. We will add a single large item that is slightly |
| 139 | + # bigger then the cache size. Some small items can still be added. |
| 140 | + c = _FFTCache(max_size_in_mb=0.01, max_item_count=5) |
| 141 | + c[1] = [np.ones(3000, dtype=np.float32)] |
| 142 | + c[1] |
| 143 | + c[2] = [np.ones(2, dtype=np.float32)] |
| 144 | + c[3] = [np.ones(2, dtype=np.float32)] |
| 145 | + c[4] = [np.ones(2, dtype=np.float32)] |
| 146 | + c[1], c[2], c[3], c[4] |
| 147 | + |
| 148 | + # One more big item. |
| 149 | + c[5] = [np.ones(3000, dtype=np.float32)] |
| 150 | + |
| 151 | + # c[1] no longer in the cache. Rest still in the cache. |
| 152 | + c[2], c[3], c[4], c[5] |
| 153 | + with self.assertRaises(KeyError): |
| 154 | + c[1] |
| 155 | + |
| 156 | + # Another big item - should now be the only item in the cache. |
| 157 | + c[6] = [np.ones(4000, dtype=np.float32)] |
| 158 | + for _i in range(1, 6): |
| 159 | + with self.assertRaises(KeyError): |
| 160 | + c[_i] |
| 161 | + c[6] |
| 162 | + |
| 163 | + |
77 | 164 | if __name__ == "__main__": |
78 | 165 | run_module_suite() |
0 commit comments