code: def testAPMeter(self):
      mtr = meter.APMeter()
  
      target = torch.Tensor([0, 1, 0, 1])
      output = torch.Tensor([0.1, 0.2, 0.3, 4])
      weight = torch.Tensor([0.5, 1.0, 2.0, 0.1])
      mtr.add(output, target, weight)
  
      ap = mtr.value()
      val = (
          1 * 0.1 / 0.1 + 0 * 2.0 / 2.1 + 1.1 * 1 / 3.1 + 0 * 1 / 4
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test1 failed"
      )
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      val = (
          1 * 1.0 / 1.0 + 0 * 1.0 / 2.0 + 2 * 1.0 / 3.0 + 0 * 1.0 / 4.0
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test2 failed"
      )
  
      target = torch.Tensor([0, 1, 0, 1])
      output = torch.Tensor([4, 3, 2, 1])
      weight = torch.Tensor([1, 2, 3, 4])
  
      mtr.reset()
      mtr.add(output, target, weight)
      ap = mtr.value()
      val = (
          0 * 1.0 / 1.0
          + 1.0 * 2.0 / 3.0
          + 2.0 * 0 / 6.0
          + 6.0 * 1.0 / 10.0
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test3 failed"
      )
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      val = (
          0 * 1.0 + 1 * 1.0 / 2.0 + 0 * 1.0 / 3.0 + 2 * 1.0 / 4.0
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test4 failed"
      )
  
      target = torch.Tensor([0, 1, 0, 1])
      output = torch.Tensor([1, 4, 2, 3])
      weight = torch.Tensor([1, 2, 3, 4])
      mtr.reset()
      mtr.add(output, target, weight)
      ap = mtr.value()
      val = (
          4 * 1.0 / 4.0 + 6 * 1.0 / 6.0 + 0 * 6.0 / 9.0 + 0 * 6.0 / 10.0
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test5 failed"
      )
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      val = (
          1 * 1.0 + 2 * 1.0 / 2.0 + 0 * 1.0 / 3.0 + 0 * 1.0 / 4.0
      ) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test6 failed"
      )
  
      target = torch.Tensor([0, 0, 0, 0])
      output = torch.Tensor([1, 4, 2, 3])
      weight = torch.Tensor([1.0, 0.1, 0.0, 0.5])
      mtr.reset()
      mtr.add(output, target, weight)
  
      ap = mtr.value()
      self.assertEqual(ap[0], 0.0)
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      self.assertEqual(ap[0], 0.0)
  
      target = torch.Tensor([1, 1, 0])
      output = torch.Tensor([3, 1, 2])
      weight = torch.Tensor([1, 0.1, 3])
      mtr.reset()
      mtr.add(output, target, weight)
      ap = mtr.value()
      val = (1 * 1.0 / 1.0 + 1 * 0.0 / 4.0 + 1.1 / 4.1) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test7 failed"
      )
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      val = (1 * 1.0 + 0 * 1.0 / 2.0 + 2 * 1.0 / 3.0) / 2.0
      self.assertTrue(
          math.fabs(ap[0] - val) < 0.01, msg="ap test8 failed"
      )
  
      # Test multiple K's
      target = torch.Tensor([[0, 1, 0, 1], [0, 1, 0, 1]]).transpose(
          0, 1
      )
      output = torch.Tensor(
          [[0.1, 0.2, 0.3, 4], [4, 3, 2, 1]]
      ).transpose(0, 1)
      weight = torch.Tensor([[1.0, 0.5, 2.0, 3.0]]).transpose(0, 1)
      mtr.reset()
      mtr.add(output, target, weight)
      ap = mtr.value()
      self.assertTrue(
          math.fabs(
              ap.sum()
              - torch.Tensor(
                  [
                      (
                          1 * 3.0 / 3.0
                          + 0 * 3.0 / 5.0
                          + 3.5 * 1 / 5.5
                          + 0 * 3.5 / 6.5
                      )
                      / 2.0,
                      (
                          0 * 1.0 / 1.0
                          + 1 * 0.5 / 1.5
                          + 0 * 0.5 / 3.5
                          + 1 * 3.5 / 6.5
                      )
                      / 2.0,
                  ]
              ).sum()
          )
          < 0.01,
          msg="ap test9 failed",
      )
  
      mtr.reset()
      mtr.add(output, target)
      ap = mtr.value()
      self.assertTrue(
          math.fabs(
              ap.sum()
              - torch.Tensor(
                  [
                      (
                          1 * 1.0
                          + 0 * 1.0 / 2.0
                          + 2 * 1.0 / 3
                          + 0 * 1.0 / 4.0
                      )
                      / 2.0,
                      (
                          0 * 1.0
                          + 1 * 1.0 / 2.0
                          + 0 * 1.0 / 3.0
                          + 2.0 * 1.0 / 4.0
                      )
                      / 2.0,
                  ]
              ).sum()
          )
          < 0.01,
          msg="ap test10 failed",
      )
  
      mtr.reset()
      output = torch.Tensor(5, 4).fill_(0.25)
      target = torch.ones(5, 4)
      mtr.add(output, target)
      output = torch.Tensor(1, 4).fill_(0.25)
      target = torch.ones(1, 4)
      mtr.add(output, target)
      self.assertEqual(mtr.value().size(0), 4, msg="ap test11 failed")
  
file_path: pywick/tests/test_meters.py
imports: import unittest
  import math
  import torch
  import pywick.meters
  import numpy
label_desc: Use image node_id 7 for calling the TestMeters obj's underlying member method code with example usage: obj.testAPMeter() without return types