forked from Dipraise1/Engram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ground_truth.py
More file actions
145 lines (107 loc) · 4.91 KB
/
Copy pathtest_ground_truth.py
File metadata and controls
145 lines (107 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Tests for engram/validator/ground_truth.py — GroundTruthManager."""
import json
import numpy as np
import pytest
from pathlib import Path
from engram.validator.ground_truth import GroundTruthEntry, GroundTruthManager
def _make_entry(text: str = "hello", cid: str = "v1::" + "a" * 64) -> GroundTruthEntry:
return GroundTruthEntry(
text=text,
embedding=np.random.rand(4).astype(np.float32),
cid=cid,
top_k_cids=[cid],
)
def _write_jsonl(path: Path, entries: list[dict]) -> None:
path.write_text("\n".join(json.dumps(e) for e in entries))
# ── Load ──────────────────────────────────────────────────────────────────────
def test_load_valid_entries(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
_write_jsonl(path, [
{"text": "hello", "embedding": [0.1, 0.2, 0.3], "cid": "v1::" + "a" * 64, "top_k_cids": ["v1::" + "a" * 64]},
{"text": "world", "embedding": [0.4, 0.5, 0.6], "cid": "v1::" + "b" * 64, "top_k_cids": []},
])
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 2
def test_load_skips_missing_text(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
_write_jsonl(path, [
{"embedding": [0.1, 0.2], "cid": "v1::" + "a" * 64},
])
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 0
def test_load_skips_missing_embedding(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
_write_jsonl(path, [
{"text": "hi", "cid": "v1::" + "a" * 64},
])
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 0
def test_load_skips_empty_embedding(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
_write_jsonl(path, [
{"text": "hi", "embedding": [], "cid": "v1::" + "a" * 64},
])
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 0
def test_load_skips_malformed_json(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
path.write_text('{"text": "ok", "embedding": [1.0], "cid": "v1::' + "a" * 64 + '"}\nnot json\n')
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 1
def test_load_skips_blank_lines(tmp_path: Path) -> None:
path = tmp_path / "gt.jsonl"
path.write_text('\n\n{"text": "ok", "embedding": [1.0], "cid": "v1::' + "a" * 64 + '"}\n\n')
mgr = GroundTruthManager(path=str(path))
assert len(mgr) == 1
def test_nonexistent_path_returns_empty() -> None:
mgr = GroundTruthManager(path="/nonexistent/path/gt.jsonl")
assert len(mgr) == 0
# ── Add / all_cids ────────────────────────────────────────────────────────────
def test_add_and_count() -> None:
mgr = GroundTruthManager()
mgr.add(_make_entry(cid="v1::" + "a" * 64))
mgr.add(_make_entry(cid="v1::" + "b" * 64))
assert len(mgr) == 2
def test_all_cids() -> None:
mgr = GroundTruthManager()
cid_a = "v1::" + "a" * 64
cid_b = "v1::" + "b" * 64
mgr.add(_make_entry(cid=cid_a))
mgr.add(_make_entry(cid=cid_b))
assert set(mgr.all_cids()) == {cid_a, cid_b}
# ── Sample ────────────────────────────────────────────────────────────────────
def test_sample_returns_correct_count() -> None:
mgr = GroundTruthManager()
for i in range(10):
mgr.add(_make_entry(cid="v1::" + chr(ord("a") + i) * 64))
sample = mgr.sample(n=5)
assert len(sample) == 5
def test_sample_clamps_to_available() -> None:
mgr = GroundTruthManager()
mgr.add(_make_entry())
sample = mgr.sample(n=100)
assert len(sample) == 1
def test_sample_empty_manager_returns_empty() -> None:
mgr = GroundTruthManager()
assert mgr.sample(n=5) == []
def test_sample_returns_valid_entries() -> None:
mgr = GroundTruthManager()
mgr.add(_make_entry(text="alpha"))
result = mgr.sample(n=1)
assert result[0].text == "alpha"
# ── Save / reload ─────────────────────────────────────────────────────────────
def test_save_and_reload(tmp_path: Path) -> None:
path = str(tmp_path / "gt.jsonl")
mgr = GroundTruthManager()
mgr.add(GroundTruthEntry(
text="test text",
embedding=np.array([0.1, 0.2, 0.3], dtype=np.float32),
cid="v1::" + "c" * 64,
top_k_cids=["v1::" + "c" * 64],
))
mgr.save(path)
mgr2 = GroundTruthManager(path=path)
assert len(mgr2) == 1
assert mgr2._entries[0].text == "test text"
assert mgr2._entries[0].cid == "v1::" + "c" * 64
np.testing.assert_allclose(mgr2._entries[0].embedding, [0.1, 0.2, 0.3], atol=1e-6)