code: def add_finite_filter_to_scorer(score_func):
      """Takes a scorer and returns a scorer that ignores NA / infinite elements in y_true.
  
      sklearn scorers (and others) don't handle arrays with 0 length. In that case, return None
  
      :param score_func: function that maps two arrays to a number. E.g. (y_true, y_pred) -> error
      :return: scorer that drops records where y_true is not finite
      """
  
      def score_func_finite(y_true, y_pred, **kwargs):
          y_true, y_pred = valid_elements_for_evaluation(
              reference_arrays=[y_true],
              arrays=[y_pred],
              reference_array_names="y_true",
              drop_leading_only=False,
              keep_inf=False,
          )
          # The Silverkite Multistage model has NANs at the beginning
          # when predicting on the training data.
          # We only drop the leading NANs/infs from ``y_pred``,
          # since they are not supposed to appear in the middle.
          y_pred, y_true = valid_elements_for_evaluation(
              reference_arrays=[y_pred],
              arrays=[y_true],
              reference_array_names="y_pred",
              drop_leading_only=True,
              keep_inf=True,
          )
          if len(y_true) == 0:  # returns None if there are no elements
              return None
          return score_func(y_true, y_pred, **kwargs)
  
      return score_func_finite
  
imports: import math
  import warnings
  from collections import namedtuple
  from enum import Enum
  from functools import partial
  from typing import List
  from typing import Optional
  from typing import Union
  import numpy
  import pandas
  from scipy.stats import pearsonr
  from sklearn.metrics import mean_absolute_error
  from sklearn.metrics import mean_squared_error
  from sklearn.metrics import median_absolute_error
  from sklearn.metrics import r2_score
  from greykite.common.constants import ACTUAL_COL
  from greykite.common.constants import COVERAGE_VS_INTENDED_DIFF
  from greykite.common.constants import LOWER_BAND_COVERAGE
  from greykite.common.constants import PREDICTED_COL
  from greykite.common.constants import PREDICTED_LOWER_COL
  from greykite.common.constants import PREDICTED_UPPER_COL
  from greykite.common.constants import PREDICTION_BAND_COVERAGE
  from greykite.common.constants import PREDICTION_BAND_WIDTH
  from greykite.common.constants import UPPER_BAND_COVERAGE
  from greykite.common.logging import LoggingLevelEnum
  from greykite.common.logging import log_message
label_desc: Use image node_id 5 for calling a global function with example usage: add_finite_filter_to_scorer(score_func) and returns: score_func_finite, score_func, None