File size: 2,589 Bytes
2d13eb2 | 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 | import unittest
import numpy as np
import copy
# Import canonical and generated implementations
from canonical import get_r_score as canonical_get_r_score
from canonical import compute_threshold as canonical_compute_threshold
## for generation
from calibrate_thresh import get_r_score as generated_get_r_score
from calibrate_thresh import compute_threshold as generated_compute_threshold
import random
class TestConformalThresholdEquivalence(unittest.TestCase):
def setUp(self):
random.seed(42)
np.random.seed(42)
self.confidence_method = "gpt"
self.factuality_fraction = 0.7
self.alpha = 0.1
# Generate 150 calibration entries
self.sample_data = []
for _ in range(150):
num_claims = random.randint(3, 6)
claims = []
for _ in range(num_claims):
score = round(random.uniform(0.0, 1.0), 3)
noise = round(random.uniform(0.0, 0.05), 3)
annotation = random.choices(["Y", "S", "N"], weights=[0.4, 0.3, 0.3])[0]
claims.append({
f"{self.confidence_method}-score": score,
"noise": noise,
"annotation": annotation
})
self.sample_data.append({"claims": claims})
def test_get_r_score_equivalence(self):
for entry in self.sample_data:
entry_copy1 = copy.deepcopy(entry)
entry_copy2 = copy.deepcopy(entry)
canon_score = canonical_get_r_score(
entry_copy1, self.confidence_method, self.factuality_fraction
)
gen_score = generated_get_r_score(
entry_copy2, self.confidence_method, self.factuality_fraction
)
self.assertAlmostEqual(
canon_score, gen_score, places=8,
msg=f"Mismatch in get_r_score: canonical={canon_score}, generated={gen_score}"
)
def test_compute_threshold_equivalence(self):
canon_thresh = canonical_compute_threshold(
self.alpha, copy.deepcopy(self.sample_data), self.factuality_fraction, self.confidence_method
)
gen_thresh = generated_compute_threshold(
self.alpha, copy.deepcopy(self.sample_data), self.factuality_fraction, self.confidence_method
)
self.assertAlmostEqual(
canon_thresh, gen_thresh, places=8,
msg=f"Mismatch in compute_threshold: canonical={canon_thresh}, generated={gen_thresh}"
)
if __name__ == "__main__":
unittest.main()
|