code: def test_label_anomalies_multi_metric():
      """Tests ``label_anomalies_multi_metric``"""
      anomaly_df = pd.DataFrame(
          {
              "start_time": [
                  "2020-01-01",
                  "2020-02-01",
                  "2020-01-02",
                  "2020-02-02",
                  "2020-02-05",
              ],
              "end_time": [
                  "2020-01-03",
                  "2020-02-04",
                  "2020-01-05",
                  "2020-02-06",
                  "2020-02-08",
              ],
              "metric": [
                  "impressions",
                  "impressions",
                  "clicks",
                  "clicks",
                  "bookings",
              ],
          }
      )
  
      ts = pd.date_range(start="2019-12-01", end="2020-03-01", freq="D")
  
      np.random.seed(1317)
      df = pd.DataFrame({"ts": ts})
      size = len(df)
      value_cols = ["impressions", "clicks", "bookings"]
      df["impressions"] = np.random.normal(
          loc=0.0, scale=1.0, size=size
      )
      df["clicks"] = np.random.normal(loc=1.0, scale=1.0, size=size)
      df["bookings"] = np.random.normal(loc=2.0, scale=1.0, size=size)
  
      res = label_anomalies_multi_metric(
          df=df,
          time_col="ts",
          value_cols=value_cols,
          anomaly_df=anomaly_df,
          anomaly_df_grouping_col="metric",
          start_time_col="start_time",
          end_time_col="end_time",
      )
  
      augmented_df = res["augmented_df"]
      is_anomaly_cols = res["is_anomaly_cols"]
      anomaly_value_cols = res["anomaly_value_cols"]
      normal_value_cols = res["normal_value_cols"]
  
      assert len(augmented_df) == len(df)
  
      assert is_anomaly_cols == [
          "impressions_is_anomaly",
          "clicks_is_anomaly",
          "bookings_is_anomaly",
      ]
  
      assert anomaly_value_cols == [
          "impressions_anomaly_value",
          "clicks_anomaly_value",
          "bookings_anomaly_value",
      ]
  
      assert normal_value_cols == [
          "impressions_normal_value",
          "clicks_normal_value",
          "bookings_normal_value",
      ]
  
      assert set(augmented_df.columns) == set(
          ["ts"]
          + value_cols
          + is_anomaly_cols
          + anomaly_value_cols
          + normal_value_cols
      )
  
      # From the above data its clear this is the range for bookings anomalies and non-anomalies
      bookings_anomaly_time_ind = (df["ts"] >= "2020-02-05") & (
          df["ts"] <= "2020-02-08"
      )
      bookings_normal_time_ind = (df["ts"] < "2020-02-05") | (
          df["ts"] > "2020-02-08"
      )
  
      # We expect this to only consist of missing values
      x = augmented_df.loc[bookings_anomaly_time_ind][
          "bookings_normal_value"
      ]
      assert x.isnull().all()
  
      # We expect this to only consist of missing values
      x = augmented_df.loc[
          bookings_normal_time_ind, "bookings_anomaly_value"
      ]
      assert x.isnull().all()
  
      # Checks for anomaly values
      x = augmented_df.loc[
          bookings_anomaly_time_ind, "bookings_anomaly_value"
      ]
      y = df.loc[bookings_anomaly_time_ind, "bookings"]
      assert (x == y).all()
  
      # Checks for normal values
      x = augmented_df.loc[
          bookings_normal_time_ind, "bookings_normal_value"
      ]
      y = df.loc[bookings_normal_time_ind, "bookings"]
      assert (x == y).all()
  
      # Tests for raising ``ValueError`` due to non existing columns
      expected_match = "time_col:"
      with pytest.raises(ValueError, match=expected_match):
          label_anomalies_multi_metric(
              df=df,
              time_col="timestamp",  # non-existing column name
              value_cols=value_cols,
              anomaly_df=anomaly_df,
              anomaly_df_grouping_col="metric",
              start_time_col="start_time",
              end_time_col="end_time",
          )
  
      expected_match = "value_col:"
      with pytest.raises(ValueError, match=expected_match):
          label_anomalies_multi_metric(
              df=df,
              time_col="ts",
              value_cols=[
                  "abandons",
                  "impressions",
              ],  # non-existing column name
              anomaly_df=anomaly_df,
              anomaly_df_grouping_col="metric",
              start_time_col="start_time",
              end_time_col="end_time",
          )
  
      expected_match = "start_time_col:"
      with pytest.raises(ValueError, match=expected_match):
          label_anomalies_multi_metric(
              df=df,
              time_col="ts",
              value_cols=["impressions"],
              anomaly_df=anomaly_df,
              anomaly_df_grouping_col="metric",
              start_time_col="start_ts",  # non-existing column name
              end_time_col="end_time",
          )
  
      expected_match = "end_time_col:"
      with pytest.raises(ValueError, match=expected_match):
          label_anomalies_multi_metric(
              df=df,
              time_col="ts",
              value_cols=["impressions"],
              anomaly_df=anomaly_df,
              anomaly_df_grouping_col="metric",
              start_time_col="start_time",
              end_time_col="end_ts",
          )
  
file_path: greykite/greykite/tests/common/features/test_adjust_anomalous_data.py
imports: import numpy
  import pandas
  import pytest
  from greykite.common.constants import ADJUSTMENT_DELTA_COL
  from greykite.common.constants import END_TIME_COL
  from greykite.common.constants import METRIC_COL
  from greykite.common.constants import START_TIME_COL
  from greykite.common.constants import TIME_COL
  from greykite.common.features.adjust_anomalous_data import adjust_anomalous_data
  from greykite.common.features.adjust_anomalous_data import label_anomalies_multi_metric
  from greykite.common.testing_utils import generate_anomalous_data
  from greykite.common.testing_utils import generic_test_adjust_anomalous_data
label_desc: Use image node_id 3 for calling a global function with example usage: test_label_anomalies_multi_metric() without return types