code: def test_ThresholdDetector(self):
      # Parameters
      # Need to have at least one parameter (low, high) not None
      with pytest.raises(ValueError):
          ThresholdDetector()
      with pytest.raises(ValueError):
          ThresholdDetector(low_threshold=None, high_threshold=None)
  
      # if high and low are both sequences of length>1, they must be of the same size
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[0.2, 0.1], high_threshold=[0.95, 0.8, 0.9]
          )
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[0.2, 0.1, 0.7], high_threshold=[0.95, 0.8]
          )
  
      # Parameter high must be higher or equal than parameter low
      with pytest.raises(ValueError):
          ThresholdDetector(low_threshold=0.7, high_threshold=0.2)
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[0.2, 0.9], high_threshold=[0.95, 0.1]
          )
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=0.2, high_threshold=[0.95, 0.1]
          )
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[0.2, 0.9], high_threshold=0.8
          )
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[0.2, 0.9, None], high_threshold=0.8
          )
  
      # Parameter high/low cannot be sequence of only None
      with pytest.raises(ValueError):
          ThresholdDetector(low_threshold=[None, None, None])
      with pytest.raises(ValueError):
          ThresholdDetector(high_threshold=[None, None, None])
      with pytest.raises(ValueError):
          ThresholdDetector(
              low_threshold=[None], high_threshold=[None, None, None]
          )
  
      # widths of series used for scoring must match the number of values given for high or/and low,
      # if high and low have a length higher than 1
  
      detector = ThresholdDetector(
          low_threshold=0.1, high_threshold=[0.8, 0.7]
      )
      with pytest.raises(ValueError):
          detector.detect(self.train)
      with pytest.raises(ValueError):
          detector.detect([self.train, self.mts_train])
  
      detector = ThresholdDetector(
          low_threshold=[0.1, 0.2], high_threshold=[0.8, 0.9]
      )
      with pytest.raises(ValueError):
          detector.detect(self.train)
      with pytest.raises(ValueError):
          detector.detect([self.train, self.mts_train])
  
      detector = ThresholdDetector(
          low_threshold=[0.1, 0.2], high_threshold=0.8
      )
      with pytest.raises(ValueError):
          detector.detect(self.train)
      with pytest.raises(ValueError):
          detector.detect([self.train, self.mts_train])
  
      detector = ThresholdDetector(low_threshold=[0.1, 0.2])
      with pytest.raises(ValueError):
          detector.detect(self.train)
      with pytest.raises(ValueError):
          detector.detect([self.train, self.mts_train])
  
      detector = ThresholdDetector(high_threshold=[0.1, 0.2])
      with pytest.raises(ValueError):
          detector.detect(self.train)
      with pytest.raises(ValueError):
          detector.detect([self.train, self.mts_train])
  
      detector = ThresholdDetector(
          low_threshold=9.5, high_threshold=10.5
      )
      binary_detection = detector.detect(self.test)
  
      # Return of .detect() must be binary
      np.testing.assert_array_equal(
          binary_detection.values(copy=False),
          binary_detection.values(copy=False).astype(bool),
      )
  
      # Return of .detect() must be same len as input
      assert len(binary_detection) == len(self.test)
  
      # univariate test
      # detector must found 58 anomalies in the series 'test'
      assert (
          binary_detection.sum(axis=0).all_values().flatten()[0] == 58
      )
      # detector must have an accuracy of 0.41 for the series 'test'
      assert (
          abs(
              detector.eval_accuracy(
                  self.anomalies, self.test, metric="accuracy"
              )
              - 0.41
          )
          < 1e-05
      )
      # detector must have an recall of 0.4 for the series 'test'
      assert (
          abs(
              detector.eval_accuracy(
                  self.anomalies, self.test, metric="recall"
              )
              - 0.4
          )
          < 1e-05
      )
      # detector must have an f1 of 0.06349 for the series 'test'
      assert (
          abs(
              detector.eval_accuracy(
                  self.anomalies, self.test, metric="f1"
              )
              - 0.06349
          )
          < 1e-05
      )
      # detector must have an precision of 0.03448 for the series 'test'
      assert (
          abs(
              detector.eval_accuracy(
                  self.anomalies, self.test, metric="precision"
              )
              - 0.03448
          )
          < 1e-05
      )
  
      # multivariate test
      detector = ThresholdDetector(
          low_threshold=4.8, high_threshold=10.5
      )
      binary_detection = detector.detect(self.mts_test)
  
      # if two values are given for low and high, and a series of width 2 is given,
      # then the results must be the same as a detector that was given only one value
      # for low and high. (will duplicate the value for each width)
      detector_2param = ThresholdDetector(
          low_threshold=[4.8, 4.8], high_threshold=[10.5, 10.5]
      )
      binary_detection_2param = detector_2param.detect(self.mts_test)
      assert binary_detection == binary_detection_2param
  
      # width of output must be equal to 2 (same dimension as input)
      assert binary_detection.width == 2
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="accuracy"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="recall"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="f1"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="precision"
              )
          )
          == 2
      )
  
      # detector must found 28 anomalies on the first width of the series 'test'
      assert (
          binary_detection["0"].sum(axis=0).all_values().flatten()[0]
          == 28
      )
      # detector must found 52 anomalies on the second width of the series 'test'
      assert (
          binary_detection["1"].sum(axis=0).all_values().flatten()[0]
          == 52
      )
  
      acc = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="accuracy"
      )
      # detector must have an accuracy of 0.71 on the first width of the series 'mts_test'
      assert abs(acc[0] - 0.71) < 1e-05
      # detector must have an accuracy of 0.48 on the second width of the series 'mts_test'
      assert abs(acc[1] - 0.48) < 1e-05
  
      precision = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="precision"
      )
      # detector must have an precision of 0.17857 on the first width of the series 'mts_test'
      assert abs(precision[0] - 0.17857) < 1e-05
      # detector must have an precision of 0.09615 on the second width of the series 'mts_test'
      assert abs(precision[1] - 0.09615) < 1e-05
  
      recall = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="recall"
      )
      # detector must have an recall of 0.45454 on the first width of the series 'mts_test'
      assert abs(recall[0] - 0.45454) < 1e-05
      # detector must have an recall of 0.5 on the second width of the series 'mts_test'
      assert abs(recall[1] - 0.5) < 1e-05
  
      f1 = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="f1"
      )
      # detector must have an f1 of 0.25641 on the first width of the series 'mts_test'
      assert abs(f1[0] - 0.25641) < 1e-05
      # detector must have an f1 of 0.16129 on the second width of the series 'mts_test'
      assert abs(f1[1] - 0.16129) < 1e-05
  
      # exemple multivariate with Nones
      detector = ThresholdDetector(
          low_threshold=[10, None], high_threshold=[None, 5]
      )
      binary_detection = detector.detect(self.mts_test)
  
      # width of output must be equal to 2 (same dimension as input)
      assert binary_detection.width == 2
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="accuracy"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="recall"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="f1"
              )
          )
          == 2
      )
      assert (
          len(
              detector.eval_accuracy(
                  self.mts_anomalies, self.mts_test, metric="precision"
              )
          )
          == 2
      )
  
      # detector must found 48 anomalies on the first width of the series 'test'
      assert (
          binary_detection["0"].sum(axis=0).all_values().flatten()[0]
          == 48
      )
      # detector must found 43 anomalies on the second width of the series 'test'
      assert (
          binary_detection["1"].sum(axis=0).all_values().flatten()[0]
          == 43
      )
  
      acc = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="accuracy"
      )
      # detector must have an accuracy of 0.51 on the first width of the series 'mts_test'
      assert abs(acc[0] - 0.51) < 1e-05
      # detector must have an accuracy of 0.57 on the second width of the series 'mts_test'
      assert abs(acc[1] - 0.57) < 1e-05
  
      precision = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="precision"
      )
      # detector must have an precision of 0.10416 and  on the first width of the series 'mts_test'
      assert abs(precision[0] - 0.10416) < 1e-05
      # detector must have an precision of 0.11627 on the second width of the series 'mts_test'
      assert abs(precision[1] - 0.11627) < 1e-05
  
      recall = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="recall"
      )
      # detector must have an recall of 0.45454 on the first width of the series 'mts_test'
      assert abs(recall[0] - 0.45454) < 1e-05
      # detector must have an recall of 0.5 on the second width of the series 'mts_test'
      assert abs(recall[1] - 0.5) < 1e-05
  
      f1 = detector.eval_accuracy(
          self.mts_anomalies, self.mts_test, metric="f1"
      )
      # detector must have an f1 of 0.16949 on the first width of the series 'mts_test'
      assert abs(f1[0] - 0.16949) < 1e-05
      # detector must have an f1 of 0.18867 on the second width of the series 'mts_test'
      assert abs(f1[1] - 0.18867) < 1e-05
  
file_path: darts/darts/tests/ad/test_detectors.py
imports: from typing import Sequence
  import numpy
  import pytest
  from darts import TimeSeries
  from darts.ad.detectors.quantile_detector import QuantileDetector
  from darts.ad.detectors.threshold_detector import ThresholdDetector
label_desc: Use image node_id 6 for calling the TestADDetectors obj's underlying member method code with example usage: obj.test_ThresholdDetector() without return types