def test_anom_without_breakout(self): # when the breakout just happened, reports it as anomaly, which is correct. ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret) # after the time windows moved forward, still reports the last data is anomaly, which is bad. ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret) ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret)
def test_anom_without_breakout(self): # when the breakout just happened, reports it as anomaly, which is correct. ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret) # after the time windows moved forward, still reports the last data is anomaly, which is bad. ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret) ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1) self.assertEqual([29], ret)
def test_anom_with_breakout(self): breakout_kwargs = {'min_size': 7, 'method': 'multi', 'beta': 0.008} # when the breakout just happened, reports it as anomaly, which is correct. ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([29], ret) # after the time window moved forward, detects the breakout and stops reporting the last point as anomaly. ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([], ret) ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([], ret)
def test_twitter_data_threshold_p95(self): """ Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. Set the threshold=p95 """ x = read_twitter_raw_data('tests/raw_data.txt') expected_index, expected_e_values = _read_twitter_test_result('tests/expected_threshold_p95.txt') index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', threshold='p95', e_value=True) self.assertListEqual(expected_index, index) self.assertListEqual(expected_e_values, e_values)
def test_seasonal_data(self): """ An example from real Indeed data. Numbers are the click count of one of Indeed pages. The last number is an anomaly caused by a holiday. """ x = [534592, 854369, 868702, 852728, 773757, 618216, 423549, 497898, 836237, 883591, 888337, 818443, 660449, 482778, 477392, 904671, 943225, 918105, 843145, 685644, 511239, 558484, 894195, 927928, 919406, 852359, 658974, 473478, 458006, 587811] anoms_index = detect_anoms(x, 7) self.assertEqual([29], anoms_index)
def test_twitter_data_longterm_onlylast(self): """ Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. Set the longterm_period to 1440 * 7 """ x = read_twitter_raw_data('tests/raw_data.txt') expected_index, expected_e_values = _read_twitter_test_result('tests/expected_longterm_onlylast.txt') index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', longterm_period=1440 * 7, only_last=1440, e_value=True) self.assertListEqual(expected_index, index) self.assertListEqual(expected_e_values, e_values)
def test_seasonal_data(self): """ An example from real Indeed data. Numbers are the click count of one of Indeed pages. The last number is an anomaly caused by a holiday. """ x = [ 534592, 854369, 868702, 852728, 773757, 618216, 423549, 497898, 836237, 883591, 888337, 818443, 660449, 482778, 477392, 904671, 943225, 918105, 843145, 685644, 511239, 558484, 894195, 927928, 919406, 852359, 658974, 473478, 458006, 587811 ] anoms_index = detect_anoms(x, 7) self.assertEqual([29], anoms_index)
def test_anom_with_breakout(self): breakout_kwargs = {'min_size': 7, 'method': 'multi', 'beta': 0.008} # when the breakout just happened, reports it as anomaly, which is correct. ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([29], ret) # after the time window moved forward, detects the breakout and stops reporting the last point as anomaly. ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([], ret) ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) self.assertEqual([], ret)
def test_twitter_data_neg(self): """ Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. Set the direction=neg """ x = read_twitter_raw_data('tests/raw_data.txt') expected_index, expected_e_values = _read_twitter_test_result( 'tests/expected_neg.txt') index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='neg', e_value=True) self.assertListEqual(expected_index, index) self.assertListEqual(expected_e_values, list(e_values))
def test_twitter_data_longterm(self): """ Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. Set the longterm_period to 1440 * 7 """ x = read_twitter_raw_data('tests/raw_data.txt') expected_index, expected_e_values = _read_twitter_test_result( 'tests/expected_longterm.txt') index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', longterm_period=1440 * 7, e_value=True) self.assertListEqual(expected_index, index) self.assertListEqual(expected_e_values, list(e_values))
def test_twitter_data_onlylast(self): """ Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. Set the only_last=True """ x = read_twitter_raw_data('tests/raw_data.txt') expected_index, expected_e_values = _read_twitter_test_result( 'tests/expected_onlylast.txt') index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', only_last=1440, e_value=True) self.assertListEqual(expected_index, index) self.assertListEqual(expected_e_values, e_values)
def test_constants(self): ret = detect_anoms([1] * 1000, 14, direction='both') self.assertEqual([], ret)
def test_constants(self): ret = detect_anoms([1] * 1000, 14, direction='both') self.assertEqual([], ret)