code: def ranking_eval(
      model,
      metrics,
      train_set,
      test_set,
      repetition_eval=False,
      exploration_eval=False,
      exclude_unknowns=True,
      verbose=False,
  ):
      """Evaluate model on provided ranking metrics.
  
      Parameters
      ----------
      model: :obj:`cornac.models.BasketRecommender`, required
          BasketRecommender model to be evaluated.
  
      metrics: :obj:`iterable`, required
          List of rating metrics :obj:`cornac.metrics.RankingMetric`.
  
      train_set: :obj:`cornac.data.BasketDataset`, required
          BasketDataset to be used for model training. This will be used to exclude
          observations already appeared during training.
  
      test_set: :obj:`cornac.data.BasketDataset`, required
          BasketDataset to be used for evaluation.
  
      repetition_eval: boolean, optional,
          Evaluation on repetition items, appeared in history baskets.
  
      exploration_eval: boolean, optional,
          Evaluation on exploration items, not appeared in history baskets.
  
      exclude_unknowns: bool, optional, default: True
          Ignore unknown users and items during evaluation.
  
      verbose: bool, optional, default: False
          Output evaluation progress.
  
      Returns
      -------
      res: (List, List)
          Tuple of two lists:
           - average result for each of the metrics
           - average result per user for each of the metrics
  
      """
  
      if len(metrics) == 0:
          return [], []
  
      avg_results = {
          "conventional": [],
          "repetition": [],
          "exploration": [],
      }
      user_results = {
          "conventional": [{} for _ in enumerate(metrics)],
          "repetition": [{} for _ in enumerate(metrics)],
          "exploration": [{} for _ in enumerate(metrics)],
      }
  
      def pos_items(baskets):
          return [item_idx for basket in baskets for item_idx in basket]
  
      def get_gt_items(
          train_set, test_set, test_pos_items, exclude_unknowns
      ):
          # binary mask for ground-truth positive items
          u_gt_pos_mask = np.zeros(test_set.num_items, dtype="int")
          u_gt_pos_mask[test_pos_items] = 1
  
          # binary mask for ground-truth negative items, removing all positive items
          u_gt_neg_mask = np.ones(test_set.num_items, dtype="int")
          u_gt_neg_mask[test_pos_items] = 0
  
          # filter items being considered for evaluation
          if exclude_unknowns:
              u_gt_pos_mask = u_gt_pos_mask[: train_set.num_items]
              u_gt_neg_mask = u_gt_neg_mask[: train_set.num_items]
  
          u_gt_pos_items = np.nonzero(u_gt_pos_mask)[0]
          u_gt_neg_items = np.nonzero(u_gt_neg_mask)[0]
          item_indices = np.nonzero(u_gt_pos_mask + u_gt_neg_mask)[0]
          return item_indices, u_gt_pos_items, u_gt_neg_items
  
      (test_user_indices, *_) = test_set.uir_tuple
      for [user_idx], [bids], [(*history_baskets, gt_basket)] in tqdm(
          test_set.ubi_iter(batch_size=1, shuffle=False),
          total=len(set(test_user_indices)),
          desc="Ranking",
          disable=not verbose,
          miniters=100,
      ):
          test_pos_items = pos_items([gt_basket])
          if len(test_pos_items) == 0:
              continue
  
          item_indices, u_gt_pos_items, u_gt_neg_items = get_gt_items(
              train_set, test_set, test_pos_items, exclude_unknowns
          )
  
          item_rank, item_scores = model.rank(
              user_idx,
              item_indices,
              history_baskets=history_baskets,
              history_bids=bids[:-1],
              uir_tuple=test_set.uir_tuple,
              baskets=test_set.baskets,
              basket_indices=test_set.basket_indices,
              extra_data=test_set.extra_data,
          )
  
          for i, mt in enumerate(metrics):
              mt_score = mt.compute(
                  gt_pos=u_gt_pos_items,
                  gt_neg=u_gt_neg_items,
                  pd_rank=item_rank,
                  pd_scores=item_scores,
                  item_indices=item_indices,
              )
              user_results["conventional"][i][user_idx] = mt_score
  
          history_items = set(
              item_idx
              for basket in history_baskets
              for item_idx in basket
          )
          if repetition_eval:
              test_repetition_pos_items = pos_items(
                  [[iid for iid in gt_basket if iid in history_items]]
              )
              if len(test_repetition_pos_items) > 0:
                  _, u_gt_pos_items, u_gt_neg_items = get_gt_items(
                      train_set,
                      test_set,
                      test_repetition_pos_items,
                      exclude_unknowns,
                  )
                  for i, mt in enumerate(metrics):
                      mt_score = mt.compute(
                          gt_pos=u_gt_pos_items,
                          gt_neg=u_gt_neg_items,
                          pd_rank=item_rank,
                          pd_scores=item_scores,
                          item_indices=item_indices,
                      )
                      user_results["repetition"][i][user_idx] = mt_score
  
          if exploration_eval:
              test_exploration_pos_items = pos_items(
                  [
                      [
                          iid
                          for iid in gt_basket
                          if iid not in history_items
                      ]
                  ]
              )
              if len(test_exploration_pos_items) > 0:
                  _, u_gt_pos_items, u_gt_neg_items = get_gt_items(
                      train_set,
                      test_set,
                      test_exploration_pos_items,
                      exclude_unknowns,
                  )
                  for i, mt in enumerate(metrics):
                      mt_score = mt.compute(
                          gt_pos=u_gt_pos_items,
                          gt_neg=u_gt_neg_items,
                          pd_rank=item_rank,
                          pd_scores=item_scores,
                          item_indices=item_indices,
                      )
                      user_results["exploration"][i][
                          user_idx
                      ] = mt_score
      # avg results of ranking metrics
      for i, mt in enumerate(metrics):
          avg_results["conventional"].append(
              np.mean(list(user_results["conventional"][i].values()))
              if len(user_results["conventional"][i]) > 0
              else 0
          )
          if repetition_eval:
              avg_results["repetition"].append(
                  np.mean(list(user_results["repetition"][i].values()))
                  if len(user_results["repetition"][i]) > 0
                  else 0
              )
          if exploration_eval:
              avg_results["exploration"].append(
                  np.mean(list(user_results["exploration"][i].values()))
                  if len(user_results["repetition"][i]) > 0
                  else 0
              )
  
      return avg_results, user_results
  
file_path: cornac/cornac/eval_methods/next_basket_evaluation.py
imports: from collections import OrderedDict
  import numpy
  from tqdm.auto import tqdm
  from .None import RatioSplit
  from ..data import BasketDataset
  from ..experiment.result import Result
  from ..utils.common import safe_indexing
label_desc: Use image node_id 1 for calling a global function with example usage: ranking_eval(model, metrics, train_set, test_set, repetition_eval, exploration_eval, exclude_unknowns, verbose) and returns: avg_results,  user_results, list,  list, unknown, item_indices,  u_gt_pos_items,  u_gt_neg_items