code: def mapk(
      targets: csr_matrix,
      user_ids: Union[np.array, torch.tensor],
      preds: Union[np.array, torch.tensor],
      k: int = 10,
  ) -> float:
      """
      Calculate the mean average precision at K (MAP@K) score for each user.
  
      Parameters
      ----------
      targets: scipy.sparse.csr_matrix
          Interaction matrix containing user and item IDs
      user_ids: np.array or torch.tensor
          Users corresponding to the recommendations in the top k predictions
      preds: torch.tensor
          Tensor of shape (n_users x n_items) with each user's scores for each item
      k: int
          Number of recommendations to consider per user
  
      Returns
      -------
      mapk_score: float
  
      """
      device = preds.device
      n_users = preds.shape[0]
  
      try:
          predicted_items = preds.topk(k, dim=1).indices
      except RuntimeError as e:
          raise ValueError(
              f"Ensure ``k`` ({k}) is less than the number of items ({preds.shape[1]}):",
              str(e),
          )
  
      topk_labeled = _get_labels(
          targets, user_ids, predicted_items, device
      )
      accuracy = topk_labeled.int()
  
      weights = (
          1.0
          / torch.arange(
              start=1,
              end=k + 1,
              dtype=torch.float64,
              requires_grad=False,
              device=device,
          )
      ).repeat(n_users, 1)
  
      denominator = torch.min(
          torch.tensor(k, device=device, dtype=torch.int).repeat(
              len(user_ids)
          ),
          torch.tensor(targets[user_ids].getnnz(axis=1), device=device),
      )
  
      res = (
          (accuracy * accuracy.cumsum(axis=1) * weights).sum(axis=1)
      ) / denominator
      res[torch.isnan(res)] = 0
  
      return res.mean().item()
  
imports: from typing import Any, Callable, Iterable, List, Optional, Tuple, Union
  import warnings
  import numpy
  from scipy.sparse import csr_matrix
  import torch
  from torchmetrics import Metric
  from torchmetrics.functional import auroc
  from tqdm.auto import tqdm
  import collie
  from collie.interactions import ExplicitInteractions, Interactions, InteractionsDataLoader
  from collie.model import BasePipeline
label_desc: Use image node_id 4 for calling a global function with example usage: mapk(targets, user_ids, preds, k) and returns: res