code: def get_scoring_and_refit(
      score_func=EvaluationMetricEnum.MeanAbsolutePercentError.name,
      score_func_greater_is_better=False,
      cv_report_metrics=None,
      agg_periods=None,
      agg_func=None,
      relative_error_tolerance=None,
  ):
      """Provides ``scoring`` and ``refit`` parameters for
      `~sklearn.model_selection.RandomizedSearchCV`.
  
      Together, ``scoring`` and ``refit`` specify how what metrics to evaluate and how
      to evaluate the predictions on the test set to identify the optimal model.
  
      Notes
      -----
      Sets ``greater_is_better=True`` in `scoring` for all metrics to report them with their
      original sign, and properly accounts for this in ``refit`` to extract the best index.
  
      Pass both `scoring` and `refit` to `~sklearn.model_selection.RandomizedSearchCV`
  
      Parameters
      ----------
      score_func : `str` or callable, default ``EvaluationMetricEnum.MeanAbsolutePercentError.name``
          Score function used to select optimal model in CV.
          If a callable, takes arrays ``y_true``, ``y_pred`` and returns a float.
          If a string, must be either a
          `~greykite.common.evaluation.EvaluationMetricEnum` member name
          or `~greykite.common.constants.FRACTION_OUTSIDE_TOLERANCE`.
      score_func_greater_is_better : `bool`, default False
          True if ``score_func`` is a score function, meaning higher is better,
          and False if it is a loss function, meaning lower is better.
          Must be provided if ``score_func`` is a callable (custom function).
          Ignored if ``score_func`` is a string, because the direction is known.
      cv_report_metrics : `~greykite.common.constants.CV_REPORT_METRICS_ALL`, or `list` [`str`], or None, default None  # noqa: E501
          Additional metrics to compute during CV, besides the one specified by ``score_func``.
  
              - If the string constant `greykite.common.constants.CV_REPORT_METRICS_ALL`,
                computes all metrics in ``EvaluationMetricEnum``. Also computes
                ``FRACTION_OUTSIDE_TOLERANCE`` if ``relative_error_tolerance`` is not None.
                The results are reported by the short name (``.get_metric_name()``) for ``EvaluationMetricEnum``
                members and ``FRACTION_OUTSIDE_TOLERANCE_NAME`` for ``FRACTION_OUTSIDE_TOLERANCE``.
              - If a list of strings, each of the listed metrics is computed. Valid strings are
                `greykite.common.evaluation.EvaluationMetricEnum` member names
                and `~greykite.common.constants.FRACTION_OUTSIDE_TOLERANCE`.
  
                For example::
  
                  ["MeanSquaredError", "MeanAbsoluteError", "MeanAbsolutePercentError", "MedianAbsolutePercentError", "FractionOutsideTolerance2"]
  
              - If None, no additional metrics are computed.
      agg_periods : `int` or None, default None
          Number of periods to aggregate before evaluation.
          Model is fit at original frequency, and forecast is
          aggregated according to ``agg_periods``
          E.g. fit model on hourly data, and evaluate performance at daily level
          If None, does not apply aggregation
      agg_func : callable or None, default None
          Takes an array and returns a number, e.g. np.max, np.sum
          Used to aggregate data prior to evaluation (applied to actual and predicted)
          Ignored if ``agg_periods`` is None
      relative_error_tolerance : `float` or None, default None
          Threshold to compute the
          `~greykite.common.constants.FRACTION_OUTSIDE_TOLERANCE`
          metric, defined as the fraction of forecasted values whose relative
          error is strictly greater than ``relative_error_tolerance``.
          For example, 0.05 allows for 5% relative error.
          If `None`, the metric is not computed.
  
      Returns
      -------
      scoring : `dict`
          A dictionary of metrics to evaluate for each CV split.
          The key is the metric name, the value is an instance
          of `~greykite.common.evaluation_PredictScorerDF` generated
          by :func:`~greykite.common.evaluation.make_scorer_df`.
  
          The value has a score method that takes actual and predicted values
          and returns a single number.
  
          There is one item in the dictionary for ``score_func``
          and an additional item for each additional element in
          ``cv_report_metrics``.
  
              - The key for ``score_func`` if it is a callable is
                `~greykite.common.constants.CUSTOM_SCORE_FUNC_NAME`.
              - The key for ``EvaluationMetricEnum`` member name is the short name
                from ``.get_metric_name()``.
              - The key for `~greykite.common.constants.FRACTION_OUTSIDE_TOLERANCE`
                is `~greykite.common.constants.FRACTION_OUTSIDE_TOLERANCE_NAME`.
  
          See `~sklearn.model_selection.RandomizedSearchCV`.
  
      refit : callable
          Callable that takes ``cv_results_`` from grid search
          and returns the best index.
  
          See `~sklearn.model_selection.RandomizedSearchCV`.
      """
      if cv_report_metrics is None:
          cv_report_metrics = []
      elif cv_report_metrics == CV_REPORT_METRICS_ALL:
          cv_report_metrics = EvaluationMetricEnum.__dict__[
              "_member_names_"
          ].copy()
          # Computes `FRACTION_OUTSIDE_TOLERANCE` if `relative_error_tolerance` is specified
          if relative_error_tolerance is not None:
              cv_report_metrics.append(FRACTION_OUTSIDE_TOLERANCE)
      else:
          cv_report_metrics = cv_report_metrics.copy()
  
      # Defines scoring metrics to evaluate on each CV split.
      # The results in .cv_results_ attribute are reported as
      # ``f"mean_test_{name}"``, ``f"mean_train_{name}"``, etc.,
      # where `name` is a key in the `scoring` dictionary.
      scoring = {}
      # Adds all recognized metrics in `cv_report_metrics` to `scoring`
      for metric_name in cv_report_metrics:
          # Adds aggregation to the metric function and gets the short name
          func, _, short_name = get_score_func_with_aggregation(
              score_func=metric_name,
              agg_periods=agg_periods,
              agg_func=agg_func,
              relative_error_tolerance=relative_error_tolerance,
          )
          # Strings/functions typically accepted by
          #   `sklearn.model_selection.RandomizedSearchCV`
          #   are not compatible with the predictions returned by
          #   `~greykite.sklearn.estimator.base_forecast_estimator.BaseForecastEstimator`
          #   because predictions are returned as a dataframe.
          #   Thus, instead of ``"neg_median_absolute_error"``,
          #   use ``make_scorer_df(median_absolute_error, greater_is_better=False)``
          #   See `~greykite.common.evaluation.make_scorer_df` for details.
          # Uses `greater_is_better=True` to avoid flipping the metric sign in CV results.
          scoring[short_name] = make_scorer_df(
              score_func=func, greater_is_better=True
          )
  
      # Adds `score_func` to `scoring`
      (
          func,
          greater_is_better,
          short_name,
      ) = get_score_func_with_aggregation(
          score_func=score_func,  # string or callable
          greater_is_better=score_func_greater_is_better,
          agg_periods=agg_periods,
          agg_func=agg_func,
          relative_error_tolerance=relative_error_tolerance,
      )
      scoring[short_name] = make_scorer_df(
          score_func=func,
          # Uses `greater_is_better=True` to avoid flipping the metric sign in CV results.
          # The `refit` parameter defined below should be passed to grid search to ensure proper
          # extraction of the optimal result, accounting for `greater_is_better` of the metric.
          greater_is_better=True,
      )
      # Defines `refit` function with the true `greater_is_better` to pick the best result from CV.
      refit = partial(
          get_best_index,
          metric=short_name,
          greater_is_better=greater_is_better,
      )
      return scoring, refit
  
imports: import math
  from functools import partial
  import pandas
  from sklearn.base import clone
  from sklearn.model_selection import ParameterGrid
  from sklearn.model_selection import RandomizedSearchCV
  from sklearn.pipeline import Pipeline
  from greykite.common import constants
  from greykite.common.constants import FRACTION_OUTSIDE_TOLERANCE
  from greykite.common.constants import TIME_COL
  from greykite.common.constants import VALUE_COL
  from greykite.common.evaluation import EvaluationMetricEnum
  from greykite.common.evaluation import add_finite_filter_to_scorer
  from greykite.common.evaluation import add_preaggregation_to_scorer
  from greykite.common.evaluation import fraction_outside_tolerance
  from greykite.common.logging import LoggingLevelEnum
  from greykite.common.logging import log_message
  from greykite.common.python_utils import get_integer
  from greykite.common.python_utils import unique_elements_in_list
  from greykite.common.time_properties_forecast import get_default_horizon_from_period
  from greykite.framework.constants import CUSTOM_SCORE_FUNC_NAME
  from greykite.framework.constants import CV_REPORT_METRICS_ALL
  from greykite.framework.constants import FRACTION_OUTSIDE_TOLERANCE_NAME
  from greykite.framework.output.univariate_forecast import UnivariateForecast
  from greykite.sklearn.estimator.simple_silverkite_estimator import SimpleSilverkiteEstimator
  from greykite.sklearn.sklearn_scorer import make_scorer_df
  from greykite.sklearn.transform.column_selector import ColumnSelector
  from greykite.sklearn.transform.difference_based_outlier_transformer import DifferenceBasedOutlierTransformer
  from greykite.sklearn.transform.drop_degenerate_transformer import DropDegenerateTransformer
  from greykite.sklearn.transform.dtype_column_selector import DtypeColumnSelector
  from greykite.sklearn.transform.normalize_transformer import NormalizeTransformer
  from greykite.sklearn.transform.null_transformer import NullTransformer
  from greykite.sklearn.transform.pandas_feature_union import PandasFeatureUnion
label_desc: Use image node_id 5 for calling a global function with example usage: get_scoring_and_refit(score_func, score_func_greater_is_better, cv_report_metrics, agg_periods, agg_func, relative_error_tolerance) and returns: scoring,  refit