mcy98's picture
Upload folder using huggingface_hub
2d13eb2 verified
Raw History Blame Contribute Delete
2.59 kB
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()