code: def _sklearn_stat_scores_multilabel(
      preds, target, ignore_index, multidim_average, average
  ):
      preds = preds.numpy()
      target = target.numpy()
      if np.issubdtype(preds.dtype, np.floating):
          if not ((preds > 0) & (preds < 1)).all():
              preds = sigmoid(preds)
          preds = (preds >= THRESHOLD).astype(np.uint8)
      preds = preds.reshape(*preds.shape[:2], -1)
      target = target.reshape(*target.shape[:2], -1)
      if multidim_average == "global":
          stat_scores = []
          for i in range(preds.shape[1]):
              pred, true = preds[:, i].flatten(), target[:, i].flatten()
              true, pred = remove_ignore_index(true, pred, ignore_index)
              tn, fp, fn, tp = sk_confusion_matrix(
                  true, pred, labels=[0, 1]
              ).ravel()
              stat_scores.append(np.array([tp, fp, tn, fn, tp + fn]))
          res = np.stack(stat_scores, axis=0)
  
          if average == "micro":
              return res.sum(0)
          if average == "macro":
              return res.mean(0)
          if average == "weighted":
              w = res[:, 0] + res[:, 3]
              return (res * (w / w.sum()).reshape(-1, 1)).sum(0)
          if average is None or average == "none":
              return res
          return None
  
      stat_scores = []
      for i in range(preds.shape[0]):
          scores = []
          for j in range(preds.shape[1]):
              pred, true = preds[i, j], target[i, j]
              true, pred = remove_ignore_index(true, pred, ignore_index)
              tn, fp, fn, tp = sk_confusion_matrix(
                  true, pred, labels=[0, 1]
              ).ravel()
              scores.append(np.array([tp, fp, tn, fn, tp + fn]))
          stat_scores.append(np.stack(scores, 1))
      res = np.stack(stat_scores, 0)
      if average == "micro":
          return res.sum(-1)
      if average == "macro":
          return res.mean(-1)
      if average == "weighted":
          w = res[:, 0, :] + res[:, 3, :]
          return (res * (w / w.sum())[:, np.newaxis]).sum(-1)
      if average is None or average == "none":
          return np.moveaxis(res, 1, -1)
      return None
  
imports: from functools import partial
  import numpy
  import pytest
  import torch
  from scipy.special import expit
  from sklearn.metrics import confusion_matrix
  from torchmetrics.classification.stat_scores import BinaryStatScores, MulticlassStatScores, MultilabelStatScores, StatScores
  from torchmetrics.functional.classification.stat_scores import binary_stat_scores, multiclass_stat_scores, multilabel_stat_scores
  from torchmetrics.metric import Metric
  from unittests import NUM_CLASSES, THRESHOLD
  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 8 for calling a global function with example usage: _sklearn_stat_scores_multilabel(preds, target, ignore_index, multidim_average, average) and returns: None, None, res, res, unknown, np, res, res, unknown, res