code: def _average_precision_at_k(
      target: np.ndarray, preds: np.ndarray, top_k: Optional[int] = None
  ):
      """Wrap reference metric to account for top_k argument."""
      assert target.shape == preds.shape
      assert len(target.shape) == 1
      top_k = top_k or len(preds)
      idx = np.argsort(preds, axis=0)[::-1][:top_k]
      target, preds = target[idx], preds[idx]
      return sk_average_precision_score(target, preds)
  
file_path: torchmetrics/tests/unittests/retrieval/test_map.py
imports: from typing import Callable, Optional, Union
  import numpy
  import pytest
  from sklearn.metrics import average_precision_score
  from torch import Tensor
  from torchmetrics.functional.retrieval.average_precision import retrieval_average_precision
  from torchmetrics.retrieval.average_precision import RetrievalMAP
  from typing_extensions import Literal
  from unittests.helpers import seed_all
  from unittests.retrieval.helpers import RetrievalMetricTester, _concat_tests, _custom_aggregate_fn, _default_metric_class_input_arguments, _default_metric_class_input_arguments_ignore_index, _default_metric_functional_input_arguments, _errors_test_class_metric_parameters_default, _errors_test_class_metric_parameters_no_pos_target, _errors_test_functional_metric_parameters_default
label_desc: Use image node_id 1 for calling a global function with example usage: _average_precision_at_k(target, preds, top_k) and returns: sk_average_precision_score