code: def _sklearn_avg_precision_multiclass(
      preds, target, average="macro", ignore_index=None
  ):
      preds = np.moveaxis(preds.numpy(), 1, -1).reshape(
          (-1, preds.shape[1])
      )
      target = target.numpy().flatten()
      if not ((preds > 0) & (preds < 1)).all():
          preds = softmax(preds, 1)
      target, preds = remove_ignore_index(target, preds, ignore_index)
  
      res = []
      for i in range(NUM_CLASSES):
          y_true_temp = np.zeros_like(target)
          y_true_temp[target == i] = 1
          res.append(
              sk_average_precision_score(y_true_temp, preds[:, i])
          )
      if average == "macro":
          return np.array(res)[~np.isnan(res)].mean()
      if average == "weighted":
          weights = np.bincount(target)
          weights = weights / sum(weights)
          return (np.array(res) * weights)[~np.isnan(res)].sum()
      return res
  
imports: from functools import partial
  import numpy
  import pytest
  import torch
  from scipy.special import expit
  from scipy.special import softmax
  from sklearn.metrics import average_precision_score
  from torchmetrics.classification.average_precision import AveragePrecision, BinaryAveragePrecision, MulticlassAveragePrecision, MultilabelAveragePrecision
  from torchmetrics.functional.classification.average_precision import binary_average_precision, multiclass_average_precision, multilabel_average_precision
  from torchmetrics.functional.classification.precision_recall_curve import binary_precision_recall_curve
  from torchmetrics.metric import Metric
  from unittests import NUM_CLASSES
  from unittests.classification.inputs import _binary_cases, _multiclass_cases, _multilabel_cases
  from unittests.helpers import seed_all
  from unittests.helpers.testers import MetricTester, inject_ignore_index, remove_ignore_index
label_desc: Use image node_id 2 for calling a global function with example usage: _sklearn_avg_precision_multiclass(preds, target, average, ignore_index) and returns: res, np, unknown