code: def test_macro_f1(self):
      mode = "macro"
      preds_cases = [
          [
              KIEDataSample(
                  pred_instances=InstanceData(
                      labels=torch.LongTensor([0, 1, 2])
                  ),
                  gt_instances=InstanceData(
                      labels=torch.LongTensor([0, 1, 4])
                  ),
              )
          ],
          [
              KIEDataSample(
                  gt_instances=InstanceData(
                      labels=torch.LongTensor([0, 1])
                  ),
                  pred_instances=InstanceData(
                      labels=torch.LongTensor([0, 1])
                  ),
              ),
              KIEDataSample(
                  gt_instances=InstanceData(
                      labels=torch.LongTensor([4])
                  ),
                  pred_instances=InstanceData(
                      labels=torch.LongTensor([2])
                  ),
              ),
          ],
      ]
  
      # num_classes < the maximum label index
      metric = F1Metric(num_classes=3, ignored_classes=[1])
      metric.process(None, preds_cases[0])
      with self.assertRaises(AssertionError):
          metric.evaluate(size=1)
  
      for preds in preds_cases:
          metric = F1Metric(num_classes=5, mode=mode)
          metric.process(None, preds)
          result = metric.evaluate(size=len(preds))
          self.assertAlmostEqual(result["kie/macro_f1"], 0.4)
  
          # Test ignored_classes
          metric = F1Metric(
              num_classes=5, ignored_classes=[1], mode=mode
          )
          metric.process(None, preds)
          result = metric.evaluate(size=len(preds))
          self.assertAlmostEqual(result["kie/macro_f1"], 0.25)
  
          # Test cared_classes
          metric = F1Metric(
              num_classes=5, cared_classes=[0, 2, 3, 4], mode=mode
          )
          metric.process(None, preds)
          result = metric.evaluate(size=len(preds))
          self.assertAlmostEqual(result["kie/macro_f1"], 0.25)
  
file_path: mmocr/tests/test_evaluation/test_metrics/test_f_metric.py
imports: import unittest
  import torch
  from mmengine.structures import InstanceData
  from mmocr.evaluation import F1Metric
  from mmocr.structures import KIEDataSample
label_desc: Use image node_id 2 for calling the TestF1Metric obj's underlying member method code with example usage: obj.test_macro_f1() without return types