code: def _auroc_at_k(
      target: np.ndarray,
      preds: np.ndarray,
      top_k: Optional[int] = None,
      max_fpr: Optional[float] = None,
  ):
      """Reference implementation using sklearn."""
      assert target.shape == preds.shape
      assert (
          len(target.shape) == 1
      )  # works only with single dimension inputs
  
      if top_k is None or top_k > len(preds):
          top_k = len(preds)
      idx = np.argsort(preds)[::-1][:top_k]
      preds = preds[idx]
      target = target[idx]
      if (0 not in target) or (1 not in target):
          return 0.0
      return roc_auc_score(target, preds, max_fpr=max_fpr)
  
file_path: torchmetrics/tests/unittests/retrieval/test_auroc.py
imports: from typing import Callable, Optional, Union
  import numpy
  import pytest
  from sklearn.metrics import roc_auc_score
  from torch import Tensor
  from torchmetrics.functional.retrieval.auroc import retrieval_auroc
  from torchmetrics.retrieval.auroc import RetrievalAUROC
  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_k, _errors_test_class_metric_parameters_no_pos_target, _errors_test_functional_metric_parameters_default, _errors_test_functional_metric_parameters_k
label_desc: Use image node_id 1 for calling a global function with example usage: _auroc_at_k(target, preds, top_k, max_fpr) and returns: roc_auc_score, int