docstr: Test "sign test" algorithm with a threshold of 20%
docstr_tok: ["Test","``","sign","test","''","algorithm","with","a","threshold","of","20","%"]
code: def test_sign_test_algorithm(self):
      """
      Test "sign test" algorithm with a threshold of 20%
      """
      bs = dict()
      bs.update((t, 1) for t in range(1, 100))
      ts = dict(bs)
  
      # test missing parameters
      self.assertRaises(
          exceptions.RequiredParametersNotPassed,
          lambda: AnomalyDetector(
              self.s1,
              baseline_time_series=self.s2,
              algorithm_name="sign_test",
          ),
      )
      # test over specified
      algorithm_params = {
          "percent_threshold_upper": 20,
          "percent_threshold_lower": -20,
          "scan_window": 24,
          "confidence": 0.01,
      }
  
      self.assertRaises(
          exceptions.RequiredParametersNotPassed,
          lambda: AnomalyDetector(
              self.s1,
              baseline_time_series=self.s2,
              algorithm_name="sign_test",
          ),
      )
      # Simple tests
      algorithm_params = {
          "percent_threshold_upper": 20,
          "scan_window": 24,
      }
  
      # first no anomalies
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
  
      self.assertTrue(anomalies is not None)
      self.assertEqual(len(anomalies), 0)
  
      # Next one anomaly exactly equal to scan window
      ts.update((t, 1.200001) for t in range(10, 34))
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
  
      self.assertTrue(anomalies is not None)
      self.assertEqual(len(anomalies), 1)
      anomaly = anomalies[0]
      s, e = anomaly.get_time_window()
  
      # note the anomaly is larger than scan window
      self.assertEqual(s, 4)
      self.assertEqual(e, 39)
  
      # score should be roughly 98.5
      self.assertGreater(anomaly.anomaly_score, 98)
      self.assertLess(anomaly.anomaly_score, 99)
  
      # anomaly below baseline
      algorithm_params = {
          "percent_threshold_lower": -20,
          "scan_window": 24,
          "confidence": 0.01,
      }
      ts.update((t, 0.799999) for t in range(10, 34))
  
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
      self.assertEqual(len(anomalies), 1)
      anomaly = anomalies[0]
      s, e = anomaly.get_time_window()
      self.assertEqual(s, 4)
      self.assertEqual(e, 39)
  
      # score should be roughly 98.5
      self.assertGreater(anomaly.anomaly_score, 98)
      self.assertLess(anomaly.anomaly_score, 99)
  
      # anomalies separated by big gap
      ts.update(bs)
      ts.update((t, 0.799999) for t in range(1, 25))
      ts.update((t, 0.799999) for t in range(60, 84))
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
      self.assertEqual(len(anomalies), 2)
      anomaly = anomalies[0]
      s, e = anomaly.get_time_window()
      self.assertEqual(s, 1)
      self.assertEqual(e, 30)
  
      # score ~ 99.9
      self.assertGreater(anomaly.anomaly_score, 99)
  
      anomaly = anomalies[1]
      s, e = anomaly.get_time_window()
      self.assertEqual(s, 54)
      self.assertEqual(e, 89)
  
      # score should be roughly 98.5
      self.assertGreater(anomaly.anomaly_score, 98)
      self.assertLess(anomaly.anomaly_score, 99)
  
      # anomalies separated by small gap
      algorithm_params = {
          "percent_threshold_upper": 20,
          "scan_window": 24,
      }
      ts.update(bs)
      ts.update((t, 1.21) for t in range(1, 25))
      ts.update((t, 1.21) for t in range(30, 40))
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
  
      # just one
      self.assertEqual(len(anomalies), 1)
      anomaly = anomalies[0]
      s, e = anomaly.get_time_window()
      self.assertEqual(s, 1)
      self.assertEqual(e, 40)
  
      # score ~ 99.9
      self.assertGreater(anomaly.anomaly_score, 99)
  
      # try noisy data
      ts.update(bs)
      ts.update((t, 1.21) for t in range(1, 25))
      ts.update((t, 1.19) for t in range(1, 25, 6))
      algorithm_params = {
          "percent_threshold_upper": 20,
          "scan_window": 24,
          "confidence": 0.01,
      }
  
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
      self.assertEqual(len(anomalies), 1)
  
      # now decrease sensitivity
      algorithm_params = {
          "percent_threshold_upper": 20,
          "scan_window": 24,
          "confidence": 0.0001,
      }
  
      detector = AnomalyDetector(
          ts,
          baseline_time_series=bs,
          algorithm_name="sign_test",
          algorithm_params=algorithm_params,
      )
      anomalies = detector.get_anomalies()
      self.assertEqual(len(anomalies), 0)
  
imports: import os
  import sys
  import unittest
  from luminol import exceptions
  from luminol.anomaly_detector import AnomalyDetector
  from luminol.modules.time_series import TimeSeries
  from luminol.algorithms.anomaly_detector_algorithms import AnomalyDetectorAlgorithm
label_desc: Use image node_id 4 for calling the TestAnomalyDetector obj's underlying member method code with example usage: obj.test_sign_test_algorithm() without return types