def test_metrics_pocessor_calculation(self):
        metrics_group_lv11 = MetricsGroup('lvl').add(SimpleMetric())
        metrics_group_lv21 = MetricsGroup('lv2').add(SimpleMetric())
        metrics_group_lv11.add(metrics_group_lv21)
        metrics_processor = MetricsProcessor()
        metrics_group_lv12 = MetricsGroup('lvl').add(SimpleMetric())
        metrics_group_lv22 = MetricsGroup('lv2').add(SimpleMetric())
        metrics_group_lv12.add(metrics_group_lv22)
        metrics_processor.add_metrics_group(metrics_group_lv11)
        metrics_processor.add_metrics_group(metrics_group_lv12)
        m1, m2 = SimpleMetric(), SimpleMetric()
        metrics_processor.add_metric(m1)
        metrics_processor.add_metric(m2)

        values = []
        for i in range(10):
            output, target = torch.rand(1, 3), torch.rand(1, 3)
            metrics_processor.calc_metrics(output, target)
            values.append(np.linalg.norm(output.numpy() - target.numpy()))

        for metrics_group in [metrics_group_lv11, metrics_group_lv21, metrics_group_lv12, metrics_group_lv22]:
            for m in metrics_group.metrics():
                for v1, v2 in zip(values, m.get_values()):
                    self.assertAlmostEqual(v1, v2, delta=1e-5)
        for m in [m1, m2]:
            for v1, v2 in zip(values, m.get_values()):
                self.assertAlmostEqual(v1, v2, delta=1e-5)

        metrics_processor.reset_metrics()
        self.assertEqual(metrics_group_lv11.metrics()[0].get_values().size, 0)
        self.assertEqual(metrics_group_lv21.metrics()[0].get_values().size, 0)
        self.assertEqual(metrics_group_lv12.metrics()[0].get_values().size, 0)
        self.assertEqual(metrics_group_lv22.metrics()[0].get_values().size, 0)
        self.assertEqual(m1.get_values().size, 0)
        self.assertEqual(m2.get_values().size, 0)
    def test_metrics_group_nested(self):
        metrics_group_lv1 = MetricsGroup('lvl')
        metrics_group_lv2 = MetricsGroup('lv2')
        metrics_group_lv1.add(metrics_group_lv2)
        self.assertTrue(metrics_group_lv1.have_groups())
        self.assertRaises(MetricsGroup.MGException, lambda: metrics_group_lv2.add(MetricsGroup('lv3')))

        metrics_group_lv1 = MetricsGroup('lvl')
        metrics_group_lv2 = MetricsGroup('lv2')
        metrics_group_lv3 = MetricsGroup('lv2')
        metrics_group_lv2.add(metrics_group_lv3)
        self.assertRaises(MetricsGroup.MGException, lambda: metrics_group_lv1.add(metrics_group_lv2))
    def test_metrics_group_calculation(self):
        metrics_group_lv1 = MetricsGroup('lvl').add(SimpleMetric())
        metrics_group_lv2 = MetricsGroup('lv2').add(SimpleMetric())
        metrics_group_lv1.add(metrics_group_lv2)

        values = []
        for i in range(10):
            output, target = torch.rand(1, 3), torch.rand(1, 3)
            metrics_group_lv1.calc(output, target)
            values.append(np.linalg.norm(output.numpy() - target.numpy()))

        for metrics_group in [metrics_group_lv1, metrics_group_lv2]:
            for m in metrics_group.metrics():
                for v1, v2 in zip(values, m.get_values()):
                    self.assertAlmostEqual(v1, v2, delta=1e-5)

        metrics_group_lv1.reset()
        self.assertEqual(metrics_group_lv1.metrics()[0].get_values().size, 0)
        self.assertEqual(metrics_group_lv2.metrics()[0].get_values().size, 0)
    def test_metrics_and_groups_collection(self):
        m1 = SimpleMetric()
        name = 'lv1'
        metrics_group_lv1 = MetricsGroup(name)
        self.assertEqual(metrics_group_lv1.metrics(), [])
        metrics_group_lv1.add(m1)
        self.assertEqual(metrics_group_lv1.groups(), [])
        self.assertEqual(metrics_group_lv1.metrics(), [m1])

        metrics_group_lv2 = MetricsGroup('lv2').add(SimpleMetric())
        metrics_group_lv1.add(metrics_group_lv2)
        self.assertEqual(metrics_group_lv1.groups(), [metrics_group_lv2])
        self.assertEqual(metrics_group_lv1.metrics(), [m1])

        metrics_group_lv22 = MetricsGroup('lv2').add(SimpleMetric())
        metrics_group_lv1.add(metrics_group_lv22)
        self.assertEqual(metrics_group_lv1.groups(), [metrics_group_lv2, metrics_group_lv22])
        self.assertEqual(metrics_group_lv1.metrics(), [m1])

        self.assertEqual(metrics_group_lv1.name(), name)