code: def test_cm_with_average():
      num_classes = 5
      y_pred = torch.rand(40, num_classes)
      y = torch.randint(0, num_classes, size=(40,)).long()
      np_y_pred = y_pred.numpy().argmax(axis=1).ravel()
      np_y = y.numpy().ravel()
  
      cm = ConfusionMatrix(num_classes=num_classes, average="samples")
      cm.update((y_pred, y))
      true_res = (
          confusion_matrix(
              np_y, np_y_pred, labels=list(range(num_classes))
          )
          * 1.0
          / len(np_y)
      )
      res = cm.compute().numpy()
      np.testing.assert_almost_equal(true_res, res)
  
      cm = ConfusionMatrix(num_classes=num_classes, average="recall")
      cm.update((y_pred, y))
      true_re = recall_score(
          np_y, np_y_pred, average=None, labels=list(range(num_classes))
      )
      res = cm.compute().numpy().diagonal()
      np.testing.assert_almost_equal(true_re, res)
  
      res = cm.compute().numpy()
      true_res = confusion_matrix(np_y, np_y_pred, normalize="true")
      np.testing.assert_almost_equal(true_res, res)
  
      cm = ConfusionMatrix(num_classes=num_classes, average="precision")
      cm.update((y_pred, y))
      true_pr = precision_score(
          np_y, np_y_pred, average=None, labels=list(range(num_classes))
      )
      res = cm.compute().numpy().diagonal()
      np.testing.assert_almost_equal(true_pr, res)
  
      res = cm.compute().numpy()
      true_res = confusion_matrix(np_y, np_y_pred, normalize="pred")
      np.testing.assert_almost_equal(true_res, res)
  
imports: import os
  import numpy
  import pytest
  import torch
  from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
  import ignite.distributed
  from ignite.exceptions import NotComputableError
  from ignite.metrics import ConfusionMatrix, IoU, JaccardIndex, mIoU
  from ignite.metrics.confusion_matrix import cmAccuracy, cmPrecision, cmRecall, DiceCoefficient
label_desc: Use image node_id 16 for calling a global function with example usage: test_cm_with_average() without return types