code: def merge_ranking_true_pred(
      rating_true,
      rating_pred,
      col_user,
      col_item,
      col_prediction,
      relevancy_method,
      k=DEFAULT_K,
      threshold=DEFAULT_THRESHOLD,
      **_,
  ):
      """Filter truth and prediction data frames on common users
  
      Args:
          rating_true (pandas.DataFrame): True DataFrame
          rating_pred (pandas.DataFrame): Predicted DataFrame
          col_user (str): column name for user
          col_item (str): column name for item
          col_prediction (str): column name for prediction
          relevancy_method (str): method for determining relevancy ['top_k', 'by_threshold', None]. None means that the
              top k items are directly provided, so there is no need to compute the relevancy operation.
          k (int): number of top k items per user (optional)
          threshold (float): threshold of top items per user (optional)
  
      Returns:
          pandas.DataFrame, pandas.DataFrame, int: DataFrame of recommendation hits, sorted by `col_user` and `rank`
          DataFrame of hit counts vs actual relevant items per user number of unique user ids
      """
  
      # Make sure the prediction and true data frames have the same set of users
      common_users = set(rating_true[col_user]).intersection(
          set(rating_pred[col_user])
      )
      rating_true_common = rating_true[
          rating_true[col_user].isin(common_users)
      ]
      rating_pred_common = rating_pred[
          rating_pred[col_user].isin(common_users)
      ]
      n_users = len(common_users)
  
      # Return hit items in prediction data frame with ranking information. This is used for calculating NDCG and MAP.
      # Use first to generate unique ranking values for each item. This is to align with the implementation in
      # Spark evaluation metrics, where index of each recommended items (the indices are unique to items) is used
      # to calculate penalized precision of the ordered items.
      if relevancy_method == "top_k":
          top_k = k
      elif relevancy_method == "by_threshold":
          top_k = threshold
      elif relevancy_method is None:
          top_k = None
      else:
          raise NotImplementedError("Invalid relevancy_method")
      df_hit = get_top_k_items(
          dataframe=rating_pred_common,
          col_user=col_user,
          col_rating=col_prediction,
          k=top_k,
      )
      df_hit = pd.merge(
          df_hit, rating_true_common, on=[col_user, col_item]
      )[[col_user, col_item, "rank"]]
  
      # count the number of hits vs actual relevant items per user
      df_hit_count = pd.merge(
          df_hit.groupby(col_user, as_index=False)[col_user].agg(
              {"hit": "count"}
          ),
          rating_true_common.groupby(col_user, as_index=False)[
              col_user
          ].agg({"actual": "count"}),
          on=col_user,
      )
  
      return df_hit, df_hit_count, n_users
  
imports: import numpy
  import pandas
  from functools import wraps
  from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, explained_variance_score, roc_auc_score, log_loss
  from recommenders.utils.constants import DEFAULT_USER_COL, DEFAULT_ITEM_COL, DEFAULT_RATING_COL, DEFAULT_PREDICTION_COL, DEFAULT_RELEVANCE_COL, DEFAULT_SIMILARITY_COL, DEFAULT_ITEM_FEATURES_COL, DEFAULT_ITEM_SIM_MEASURE, DEFAULT_K, DEFAULT_THRESHOLD
  from recommenders.datasets.pandas_df_utils import has_columns, has_same_base_dtype, lru_cache_df
label_desc: Use image node_id 9 for calling a global function with example usage: merge_ranking_true_pred(rating_true, rating_pred, col_user, col_item, col_prediction, relevancy_method, k, threshold) and returns: df_hit,  df_hit_count,  n_users