code: def _eval_accuracy_from_data(
      s_anomalies: TimeSeries,
      s_data: TimeSeries,
      window: int,
      metric_fn,
      metric_name: str,
  ) -> Union[float, Sequence[float]]:
      """Internal function for:
          - ``eval_accuracy_from_binary_prediction()``
          - ``eval_accuracy_from_scores()``
  
      Score the results against true anomalies.
  
      Parameters
      ----------
      actual_anomalies
          The ground truth of the anomalies (1 if it is an anomaly and 0 if not)
      s_data
          series prediction
      window
          Integer value indicating the number of past samples each point represents
          in the anomaly_score. The parameter will be used by the function
          ``_window_adjustment_anomalies()`` to transform s_anomalies.
      metric_fn
          Function to use. Can be "average_precision_score", "roc_auc_score", "accuracy_score",
          "f1_score", "precision_score" and "recall_score".
      metric_name
          Name str of the function to use. Can be "AUC_PR", "AUC_ROC", "accuracy",
          "f1", "precision" and "recall".
  
      Returns
      -------
      Union[float, Sequence[float]]
          Score of the anomalies prediction
              - float -> if `s_data` is a univariate series (dimension=1).
              - Sequence[float] -> if `s_data` is a multivariate series (dimension>1),
              returns one value per dimension.
      """
  
      _assert_timeseries(s_data, "Prediction series input")
      _assert_timeseries(s_anomalies, "actual_anomalies input")
  
      # if window > 1, the anomalies will be adjusted so that it can be compared timewise with s_data
      s_anomalies = _max_pooling(s_anomalies, window)
  
      _sanity_check_two_series(s_data, s_anomalies)
  
      s_data, s_anomalies = _intersect(s_data, s_anomalies)
  
      if metric_name == "AUC_ROC" or metric_name == "AUC_PR":
          nr_anomalies_per_component = (
              s_anomalies.sum(axis=0).values(copy=False).flatten()
          )
  
          raise_if(
              nr_anomalies_per_component.min() == 0,
              f"`actual_anomalies` does not contain anomalies. {metric_name} cannot be computed.",
          )
  
          raise_if(
              nr_anomalies_per_component.max() == len(s_anomalies),
              f"`actual_anomalies` only contains anomalies. {metric_name} cannot be computed."
              + [
                  "",
                  f" Consider decreasing the window size (window={window})",
              ][window > 1],
          )
  
      # TODO: could we vectorize this?
      metrics = []
      for component_idx in range(s_data.width):
          metrics.append(
              metric_fn(
                  s_anomalies.all_values(copy=False)[:, component_idx],
                  s_data.all_values(copy=False)[:, component_idx],
              )
          )
  
      if len(metrics) == 1:
          return metrics[0]
      else:
          return metrics
  
file_path: darts/darts/ad/utils.py
imports: from typing import Sequence, Tuple, Union
  import matplotlib.pyplot
  import numpy
  from sklearn.metrics import accuracy_score, average_precision_score, f1_score, precision_score, recall_score, roc_auc_score
  from darts import TimeSeries
  from darts.logging import get_logger, raise_if, raise_if_not
label_desc: Use image node_id 4 for calling a global function with example usage: _eval_accuracy_from_data(s_anomalies, s_data, window, metric_fn, metric_name) and returns: metrics, metrics