code: def test_that_precision_metric_calculates_multiclass_correctly(
      y_ypred,
  ):
      predicted, true = y_ypred
      predicted = (
          predicted.softmax(-1) + torch.randn_like(predicted) / 100
      )
      # test adding a batch:
      precision = Precision(num_classes=predicted.size(1), average=True)
      precision.update(predicted, true)
      true_positive = (
          (
              torch.zeros_like(predicted).scatter(
                  1, predicted.argmax(-1).unsqueeze(-1), 1
              )
              > 0
          )
          & (
              torch.zeros_like(predicted).scatter(
                  1, true.unsqueeze(-1), 1
              )
              > 0
          )
      ).sum(dim=0)
      all_positive = (
          torch.zeros_like(predicted).scatter(1, true.unsqueeze(-1), 1)
          > 0
      ).sum(dim=0)
      manual_precision = true_positive.float() / all_positive.float()
      manual_precision[torch.isnan(manual_precision)] = 0
      # breakpoint()
      assert np.allclose(precision.value, manual_precision.mean())
      # test adding all elements in a batch separately
      new_precision = Precision(
          num_classes=predicted.size(1), average=True
      )
      for x, y in zip(predicted, true):
          x, y = x.unsqueeze(0), y.unsqueeze(0)
          new_precision.update(x, y)
      assert np.allclose(new_precision.value, precision.value)
  
imports: import os
  from pathlib import Path
  import numpy
  import pytest
  import shutil
  import torch
  from hypothesis import given
  from torch_hypothesis import classification_logits_and_labels
  from baal.utils.metrics import Loss, Accuracy, Precision, ECE, ClassificationReport, PRAuC, ECE_PerCLs
label_desc: Use image node_id 5 for calling a global function with example usage: test_that_precision_metric_calculates_multiclass_correctly(y_ypred) without return types