code: def detect_anomaly(
      y,
      yhat=None,
      pattern_th=math.inf,
      trend_th=(-math.inf, math.inf),
      dist_measure=EuclideanDistance(),
  ):
      """
      Detect anomalies. Each sample can have 1 or more dimensions.
  
      :param y: the values to detect. shape could be 1-D (num_samples,)
          or 2-D array (num_samples, features)
      :param yhat: the predicted values, a tensor with same shape as y,
          default to be None
      :param pattern_th: a single value, specify absolute distance threshold
                         between y and yhat
      :param trend_th: a tuple composed of min_threshold and max_threshold,
                       specify min and max threshold for y
      :param dist_measure: measure of distance
      :return: dict, the anomaly values indexes in the samples, including pattern and trend type
      """
      pattern_anomaly_indexes, trend_anomaly_indexes = [], []
      pattern_anomaly_scores, trend_anomaly_scores = np.zeros_like(
          y
      ), np.zeros_like(y)
      # Detect pattern anomaly
      if yhat is not None:
          invalidInputError(
              isinstance(pattern_th, int)
              or isinstance(pattern_th, float),
              f"Pattern threshold format {type(pattern_th)} is not supported, "
              "please specify int or float value.",
          )
          pattern_anomaly_indexes = detect_pattern_anomaly(
              y, yhat, pattern_th, dist_measure
          )
  
      # Detect trend anomaly
      invalidInputError(
          isinstance(trend_th, tuple) and len(trend_th) == 2,
          "Trend threshold is supposed to be a tuple of two elements.",
      )
      if (
          isinstance(trend_th[0], int) or isinstance(trend_th[0], float)
      ) and (
          isinstance(trend_th[1], int) or isinstance(trend_th[1], float)
      ):
          # min / max values are scalars
          invalidInputError(
              trend_th[0] <= trend_th[1],
              "Trend threshold is composed of (min, max), max should not be smaller.",
          )
          trend_anomaly_indexes = detect_trend_anomaly(y, trend_th)
      elif (
          trend_th[0].shape == y.shape and trend_th[1].shape == y.shape
      ):
          # min max values are arrays
          invalidInputError(
              np.all((trend_th[1] - trend_th[0]) >= 0),
              "In trend threshold (min, max), each data point in max tensor"
              " should not be smaller.",
          )
          trend_anomaly_indexes = detect_trend_anomaly_arr(y, trend_th)
      else:
          invalidInputError(
              False,
              f"Threshold format ${str(trend_th)} is not supported",
          )
  
      pattern_anomaly_scores[pattern_anomaly_indexes] = 1
      trend_anomaly_scores[trend_anomaly_indexes] = 1
  
      anomaly_indexes = list(
          set(pattern_anomaly_indexes + trend_anomaly_indexes)
      )
      anomaly_scores = np.zeros_like(y)
      anomaly_scores[anomaly_indexes] = 1
  
      index_dict = {
          "pattern anomaly index": pattern_anomaly_indexes,
          "trend anomaly index": trend_anomaly_indexes,
          "anomaly index": anomaly_indexes,
      }
      score_dict = {
          "pattern anomaly score": pattern_anomaly_scores,
          "trend anomaly score": trend_anomaly_scores,
          "anomaly score": anomaly_scores,
      }
  
      return index_dict, score_dict
  
imports: import math
  import numpy
  from bigdl.chronos.detector.anomaly.abstract import AnomalyDetector
  from bigdl.nano.utils.common import invalidInputError
  from abc import ABC, abstractmethod
label_desc: Use image node_id 6 for calling a global function with example usage: detect_anomaly(y, yhat, pattern_th, trend_th, dist_measure) and returns: index_dict,  score_dict