|
1 | 1 | import unittest |
| 2 | +import warnings |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 |
|
5 | | -from qupulse.utils.performance import (_time_windows_to_samples_numba, _time_windows_to_samples_numpy, |
6 | | - _average_windows_numba, _average_windows_numpy, average_windows) |
| 6 | +from qupulse.utils.performance import ( |
| 7 | + _time_windows_to_samples_numba, _time_windows_to_samples_numpy, |
| 8 | + _average_windows_numba, _average_windows_numpy, average_windows, |
| 9 | + shrink_overlapping_windows, WindowOverlapWarning) |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class TimeWindowsToSamplesTest(unittest.TestCase): |
@@ -55,3 +58,53 @@ def test_single_channel(self): |
55 | 58 |
|
56 | 59 | def test_dual_channel(self): |
57 | 60 | self.assert_implementations_equal(self.time, self.values, self.begins, self.ends) |
| 61 | + |
| 62 | + |
| 63 | +class TestOverlappingWindowReduction(unittest.TestCase): |
| 64 | + def setUp(self): |
| 65 | + self.shrank = np.array([1, 4, 8], dtype=np.uint64), np.array([3, 4, 4], dtype=np.uint64) |
| 66 | + self.to_shrink = np.array([1, 4, 7], dtype=np.uint64), np.array([3, 4, 5], dtype=np.uint64) |
| 67 | + |
| 68 | + def assert_noop(self, shrink_fn): |
| 69 | + begins = np.array([1, 3, 5], dtype=np.uint64) |
| 70 | + lengths = np.array([2, 1, 6], dtype=np.uint64) |
| 71 | + result = shrink_fn(begins, lengths) |
| 72 | + np.testing.assert_equal((begins, lengths), result) |
| 73 | + |
| 74 | + begins = (np.arange(100) * 176.5).astype(dtype=np.uint64) |
| 75 | + lengths = (np.ones(100) * 10 * np.pi).astype(dtype=np.uint64) |
| 76 | + result = shrink_fn(begins, lengths) |
| 77 | + np.testing.assert_equal((begins, lengths), result) |
| 78 | + |
| 79 | + begins = np.arange(15, dtype=np.uint64)*16 |
| 80 | + lengths = 1+np.arange(15, dtype=np.uint64) |
| 81 | + result = shrink_fn(begins, lengths) |
| 82 | + np.testing.assert_equal((begins, lengths), result) |
| 83 | + |
| 84 | + def assert_shrinks(self, shrink_fn): |
| 85 | + with warnings.catch_warnings(): |
| 86 | + warnings.simplefilter("always", WindowOverlapWarning) |
| 87 | + with self.assertWarns(WindowOverlapWarning): |
| 88 | + shrank = shrink_fn(*self.to_shrink) |
| 89 | + np.testing.assert_equal(self.shrank, shrank) |
| 90 | + |
| 91 | + def assert_empty_window_error(self, shrink_fn): |
| 92 | + invalid = np.array([1, 2], dtype=np.uint64), np.array([5, 1], dtype=np.uint64) |
| 93 | + with self.assertRaisesRegex(ValueError, "Overlap is bigger than measurement window"): |
| 94 | + shrink_fn(*invalid) |
| 95 | + |
| 96 | + def test_shrink_overlapping_windows_numba(self): |
| 97 | + def shrink_fn(begins, lengths): |
| 98 | + return shrink_overlapping_windows(begins, lengths, use_numba=True) |
| 99 | + |
| 100 | + self.assert_noop(shrink_fn) |
| 101 | + self.assert_shrinks(shrink_fn) |
| 102 | + self.assert_empty_window_error(shrink_fn) |
| 103 | + |
| 104 | + def test_shrink_overlapping_windows_numpy(self): |
| 105 | + def shrink_fn(begins, lengths): |
| 106 | + return shrink_overlapping_windows(begins, lengths, use_numba=False) |
| 107 | + |
| 108 | + self.assert_noop(shrink_fn) |
| 109 | + self.assert_shrinks(shrink_fn) |
| 110 | + self.assert_empty_window_error(shrink_fn) |
0 commit comments