forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rule_based_chatbot

Clone
★ 0

0 watching0 forksPython1.2 MiBa039b60Ahmet Kaan Gümüş2 months ago64 commits

test_metrics.py57 lines, 1.7 KiBHistoryRawDownload
1"""Aşama 6/10 birim testleri: çekirdek sınıflandırma metrikleri."""
2
3import unittest
4
5from src.evaluation.metrics import (
6 accuracy,
7 confusion_counts,
8 macro_f1,
9 per_class_scores,
10)
11
12
13# Küçük, elle doğrulanabilir örnek.
14PAIRS = [("a", "a"), ("a", "b"), ("b", "b"), ("c", "c")]
15LABELS = {"a", "b", "c"}
16
17
18class ConfusionTests(unittest.TestCase):
19 def test_counts(self) -> None:
20 counts = confusion_counts(PAIRS, LABELS)
21 self.assertEqual(counts["a"], (1, 0, 1)) # tp, fp, fn
22 self.assertEqual(counts["b"], (1, 1, 0))
23 self.assertEqual(counts["c"], (1, 0, 0))
24
25
26class AccuracyTests(unittest.TestCase):
27 def test_accuracy(self) -> None:
28 self.assertAlmostEqual(accuracy(PAIRS), 0.75)
29
30 def test_empty(self) -> None:
31 self.assertEqual(accuracy([]), 0.0)
32
33
34class MacroF1Tests(unittest.TestCase):
35 def test_macro_f1(self) -> None:
36 # a: f1=0.667, b: f1=0.667, c: f1=1.0 -> ort ~0.778
37 self.assertAlmostEqual(macro_f1(PAIRS, LABELS), (2 / 3 + 2 / 3 + 1.0) / 3, places=6)
38
39 def test_perfect(self) -> None:
40 perfect = [("a", "a"), ("b", "b")]
41 self.assertAlmostEqual(macro_f1(perfect, {"a", "b"}), 1.0)
42
43 def test_empty_labels(self) -> None:
44 self.assertEqual(macro_f1(PAIRS, set()), 0.0)
45
46
47class PerClassTests(unittest.TestCase):
48 def test_scores_and_support(self) -> None:
49 scores = {s.label: s for s in per_class_scores(PAIRS, LABELS)}
50 self.assertAlmostEqual(scores["a"].precision, 1.0)
51 self.assertAlmostEqual(scores["a"].recall, 0.5)
52 self.assertEqual(scores["a"].support, 2)
53 self.assertAlmostEqual(scores["c"].f1, 1.0)
54
55
56if __name__ == "__main__":
57 unittest.main()