code: def test_group_holidays(daily_df, holiday_df):
      """Tests `get_holiday_scores` and `group_holidays` functions."""
      default_get_suffix_func = "wd_we"
      default_baseline_offsets = (-7, 7)
      default_use_relative_score = True
  
      # Initializes the holiday grouper.
      hg = HolidayGrouper(
          df=daily_df,
          time_col=TIME_COL,
          value_col=VALUE_COL,
          holiday_df=holiday_df,
          holiday_date_col=EVENT_DF_DATE_COL,
          holiday_name_col=EVENT_DF_LABEL_COL,
          holiday_impact_pre_num_days=0,
          holiday_impact_post_num_days=0,
          holiday_impact_dict=HOLIDAY_IMPACT_DICT,
          get_suffix_func=default_get_suffix_func,
      )
      assert hg.get_suffix_func == default_get_suffix_func
      assert hg.baseline_offsets is None
      assert hg.use_relative_score is None
  
      # Runs the holiday grouper with KDE-based clustering.
      min_n_days = 2
      min_same_sign_ratio = 0.66
      min_abs_avg_score = 0.02
      bandwidth_multiplier = 0.2
  
      hg.group_holidays(
          min_n_days=min_n_days,
          min_same_sign_ratio=min_same_sign_ratio,
          min_abs_avg_score=min_abs_avg_score,
          clustering_method="kde",
          bandwidth_multiplier=bandwidth_multiplier,
      )
  
      # Checks the attributes are overriden.
      assert hg.baseline_offsets == default_baseline_offsets
      assert hg.use_relative_score == default_use_relative_score
      assert hg.clustering_method == "kde"
      assert hg.result_dict is not None
      assert hg.bandwidth is not None
      assert hg.bandwidth_multiplier == bandwidth_multiplier
      assert hg.kde is not None
  
      # Checks correctness of the grouping results.
      result_dict = hg.result_dict.copy()
  
      expected_keys = [
          "holiday_inferrer",
          "score_result_original",
          "score_result_avg_original",
          "score_result",
          "score_result_avg",
          "daily_event_df_dict_with_score",
          "daily_event_df_dict",
          "kde_cutoffs",
          "kde_res",
          "kde_plot",
      ]
      for key in expected_keys:
          assert result_dict[key] is not None
  
      assert len(result_dict["score_result_original"]) == len(
          result_dict["score_result_avg_original"]
      )
      assert len(result_dict["score_result"]) == len(
          result_dict["score_result_avg"]
      )
  
      # Checks if the pruning works as expected.
      for key, value in result_dict["score_result_avg"].items():
          assert (
              abs(result_dict["score_result_avg"][key])
              >= min_abs_avg_score
          )
          assert len(result_dict["score_result"][key]) >= min_n_days
  
      # Checks the grouped holidays output.
      for event_df in result_dict["daily_event_df_dict"].values():
          assert event_df.shape[1] == 2
          assert EVENT_DF_DATE_COL in event_df.columns
          assert EVENT_DF_LABEL_COL in event_df.columns
  
      for event_df in result_dict[
          "daily_event_df_dict_with_score"
      ].values():
          assert event_df.shape[1] == 4
          assert EVENT_DF_DATE_COL in event_df.columns
          assert EVENT_DF_LABEL_COL in event_df.columns
  
      assert len(result_dict["daily_event_df_dict"]) == 4
  
      # Runs again with a bigger bandwidth for clustering.
      new_bandwidth_multiplier = 1
  
      hg.group_holidays(
          min_n_days=min_n_days,
          min_same_sign_ratio=min_same_sign_ratio,
          min_abs_avg_score=min_abs_avg_score,
          clustering_method="kde",
          bandwidth_multiplier=new_bandwidth_multiplier,
      )
      assert hg.bandwidth_multiplier == new_bandwidth_multiplier
  
      # Checks results.
      new_result_dict = hg.result_dict.copy()
  
      # New grouping has fewer groups due to a relaxed bandwidth.
      assert len(new_result_dict["daily_event_df_dict"]) == 3
  
      # Scoring results have not changed.
      for key in ["score_result", "score_result_avg"]:
          assert new_result_dict[key] == result_dict[key]
  
      # Grouping results changed.
      for key in [
          "daily_event_df_dict_with_score",
          "daily_event_df_dict",
          "kde_cutoffs",
          "kde_res",
          "kde_plot",
      ]:
          with pytest.raises(AssertionError):
              assert_equal(new_result_dict[key], result_dict[key])
  
      # Runs holiday grouper with k-means clustering.
      n_clusters = 5
      hg.group_holidays(
          min_n_days=min_n_days,
          min_same_sign_ratio=min_same_sign_ratio,
          min_abs_avg_score=min_abs_avg_score,
          clustering_method="kmeans",
          n_clusters=n_clusters,
          include_diagnostics=True,
      )
  
      # Checks the attributes are overriden.
      assert hg.baseline_offsets == default_baseline_offsets
      assert hg.use_relative_score == default_use_relative_score
      assert hg.clustering_method == "kmeans"
      assert hg.result_dict is not None
      assert hg.n_clusters == n_clusters
      assert hg.kmeans is not None
  
      # Checks results.
      new_result_dict = hg.result_dict.copy()
      expected_keys = [
          "holiday_inferrer",
          "score_result_original",
          "score_result_avg_original",
          "score_result",
          "score_result_avg",
          "daily_event_df_dict_with_score",
          "daily_event_df_dict",
          "kmeans_diagnostics",
          "kmeans_plot",
      ]
      for key in expected_keys:
          assert new_result_dict[key] is not None
      # Checks the number of groups matches the input.
      assert len(new_result_dict["daily_event_df_dict"]) == n_clusters
  
      # Checks invalid clustering method.
      # Tests unknown `get_suffix_func`.
      with pytest.raises(NotImplementedError, match="is not supported"):
          hg.group_holidays(
              min_n_days=min_n_days,
              min_same_sign_ratio=min_same_sign_ratio,
              min_abs_avg_score=min_abs_avg_score,
              clustering_method="unknown",
              bandwidth_multiplier=new_bandwidth_multiplier,
          )
  
file_path: greykite/greykite/tests/algo/common/test_holiday_grouper.py
imports: import pandas
  import pytest
  from greykite.algo.common.holiday_grouper import HolidayGrouper
  from greykite.algo.common.holiday_utils import HOLIDAY_NAME_COL
  from greykite.algo.common.holiday_utils import get_weekday_weekend_suffix
  from greykite.common.constants import EVENT_DF_DATE_COL
  from greykite.common.constants import EVENT_DF_LABEL_COL
  from greykite.common.constants import TIME_COL
  from greykite.common.constants import VALUE_COL
  from greykite.common.data_loader import DataLoader
  from greykite.common.features.timeseries_features import get_holidays
  from greykite.common.python_utils import assert_equal
label_desc: Use image node_id 5 for calling a global function with example usage: test_group_holidays(daily_df, holiday_df) without return types