code: def prepare_anomaly_ranges(
      pointwise_anomalies, range_based: bool = True
  ):
      """Convert a list of pointwise anomalies into a list of anomaly ranges
  
      Parameters
      ----------
      pointwise_anmalies : array-like, 1-D
          List of pointwise anomalies
      range_based : `bool`
          If False, each pointwise anomaly is treated as an anomaly range
          If True, adjacent pointwise anomalies will be merged into a single anomaly interval
  
      Returns
      -------
      anomaly_ranges: array-like, 2D
          2D-array representing anomaly ranges, with each element indicating the
          start and end indexes for an anomaly period.
      """
  
      if range_based:
          pointwise_anomalies = np.argwhere(
              pointwise_anomalies == 1
          ).ravel()
          anomaly_ranges_shift_forward = shift(
              pointwise_anomalies, 1, fill_value=pointwise_anomalies[0]
          )
          anomaly_ranges_shift_backward = shift(
              pointwise_anomalies,
              -1,
              fill_value=pointwise_anomalies[-1],
          )
          anomaly_ranges_start = np.argwhere(
              (anomaly_ranges_shift_forward - pointwise_anomalies) != -1
          ).ravel()
          anomaly_ranges_end = np.argwhere(
              (pointwise_anomalies - anomaly_ranges_shift_backward)
              != -1
          ).ravel()
          anomaly_ranges = np.hstack(
              [
                  pointwise_anomalies[anomaly_ranges_start].reshape(
                      -1, 1
                  ),
                  pointwise_anomalies[anomaly_ranges_end].reshape(
                      -1, 1
                  ),
              ]
          )
      else:
          anomaly_ranges = np.argwhere(pointwise_anomalies == 1).repeat(
              2, axis=1
          )
  
      return anomaly_ranges
  
file_path: greykite/greykite/detection/common/ad_evaluation_utils.py
imports: from typing import List
  from typing import Optional
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: prepare_anomaly_ranges(pointwise_anomalies, range_based) and returns: anomaly_ranges