import unittest from lib.SlidingWindow import SlidingWindow class SlidingWindowTests(unittest.TestCase): def test_model_only_init(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=0, future_values=0) for i in range(1, 5): self.assertFalse(sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3})) for i in range(5, 7): self.assertTrue(sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3})) def test_model_only_values(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=0, future_values=0) for i in range(1, 5): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertListEqual( sliding_window.get_model_values(), [2, 4, 6, 3, 6, 9] ) def test_model_only_values_next(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=0, future_values=0) for i in range(1, 6): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertListEqual( sliding_window.get_model_values(), [2, 4, 6, 3, 6, 9] ) def test_model_target_values(self): sliding_window = SlidingWindow(model_past_values=2, past_values=0, future_values=0) sliding_window.add_observation({'time': 1, 'a': 2, 'temp_in': 3}) sliding_window.add_observation({'time': 2, 'a': 4, 'temp_in': 6}) sliding_window.add_observation({'time': 3, 'a': 6, 'temp_in': 9}) self.assertTrue(sliding_window.add_observation({'time': 4, 'a': 8, 'temp_in': 12})) self.assertEqual(sliding_window.get_model_target(), 9) def test_all_init(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, future_values=2) for i in range(1, 6): self.assertFalse(sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3})) for i in range(6, 7): self.assertTrue(sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3})) def test_all_values(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), future_values=2, future_fields=('b',)) for i in range(1, 7): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertListEqual( sliding_window.get_model_values(), [2, 4, 6, 3, 6, 9] ) self.assertListEqual( sliding_window._get_past_values(), [6] ) self.assertListEqual( sliding_window._get_future_values(), [12, 15] ) def test_all_values_next(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), future_values=2, future_fields=('b',)) for i in range(1, 8): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertListEqual( sliding_window.get_model_values(), [4, 6, 8, 6, 9, 12] ) self.assertEqual(sliding_window.get_previous_value('a'), 6) self.assertEqual(sliding_window.get_current_value('a'), 8) self.assertEqual(sliding_window.get_next_value('a'), 10) self.assertEqual(sliding_window.get_previous_value('b'), 9) self.assertEqual(sliding_window.get_current_value('b'), 12) self.assertEqual(sliding_window.get_next_value('b'), 15) self.assertListEqual( sliding_window._get_past_values(), [8] ) self.assertListEqual( sliding_window._get_future_values(), [15, 18] ) def test_replace_value_current(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), future_values=2, future_fields=('b',)) for i in range(1, 7): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) sliding_window.set_current_value('a', -1) self.assertListEqual( sliding_window.get_model_values(), [2, 4, -1, 3, 6, 9] ) self.assertListEqual( sliding_window._get_past_values(), [-1] ) self.assertListEqual( sliding_window._get_future_values(), [12, 15] ) def test_replace_value_next(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), future_values=2, future_fields=('b',)) for i in range(1, 7): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) sliding_window.set_next_value('b', -1) self.assertListEqual( sliding_window.get_model_values(), [2, 4, 6, 3, 6, 9] ) self.assertListEqual( sliding_window._get_past_values(), [6] ) self.assertListEqual( sliding_window._get_future_values(), [-1, 15] ) def test_replace_value_next_next(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), future_values=2, future_fields=('b',)) for i in range(1, 7): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) sliding_window.set_current_value('a', -1) sliding_window.set_next_value('b', -2) for i in range(7, 8): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertEqual(sliding_window.get_previous_value('a'), -1) self.assertEqual(sliding_window.get_current_value('b'), -2) def test_next(self): sliding_window = SlidingWindow(model_past_values=3, model_past_fields=('a', 'b'), past_values=1, past_fields=('a',), model_future_values=1, model_future_fields=('b',), future_values=2, future_fields=('b',)) for i in range(1, 7): sliding_window.add_observation({'time': i, 'a': i * 2, 'b': i * 3}) self.assertListEqual( sliding_window.get_model_values(), [2, 4, 6, 3, 6, 9, 12] ) sliding_window.next() self.assertListEqual( sliding_window.get_model_values(), [4, 6, 8, 6, 9, 12, 15] )