AAMAS-2027-CR / tests /test_scoring_data.py
Anonymous-EmpathyAI's picture
Upload 30 files
2b5f880 verified
Raw History Blame Contribute Delete
4.02 kB
"""Protocol invariants and bundled-data integrity; no network requests."""
import hashlib
import unittest
from pathlib import Path
import numpy as np
import pandas as pd
from reframing.common import CBT_MAXIMA, ROOT, read_json
from reframing.config import ExperimentConfig, ModelConfig
from reframing.data import PrecomputedReferences, load_dataset, parse_top5
from reframing.scoring import select_incumbent, utility
class ScoringTests(unittest.TestCase):
def test_three_component_formula_including_negative_cosine(self):
situation = {"vector": np.array([1.0, 0.0])}
response = {"vector": np.array([-0.6, 0.8]), "polarity": 0.6, "subjectivity": 0.7}
references = [{"polarity": -0.2, "subjectivity": 0.3}] * 5
scores = dict(CBT_MAXIMA, evidence_sensitivity=1)
result = utility({"scores": scores}, situation, response, references)
self.assertAlmostEqual(result["cbt_score"], 8 / 9)
self.assertAlmostEqual(result["semantic_cosine"], -0.6)
self.assertAlmostEqual(result["style_score"], 0.6)
self.assertAlmostEqual(result["utility"], 0.5 * 8 / 9 - 0.3 * 0.6 + 0.2 * 0.6)
self.assertEqual(set(result), {"cbt_score", "semantic_cosine", "style_score", "utility"})
with self.assertRaises(ValueError):
utility({"scores": scores}, situation, response, references[:4])
def test_safe_selection_strict_margin_and_plateau(self):
def candidate(text, value, safe=True):
return {"response": text, "utility": value, "critic": {"candidate_safe": safe}}
cfg = ExperimentConfig()
old = candidate("old", 0.7)
result = select_incumbent([candidate("new", 0.71), candidate("unsafe", 0.99, False)], old, 1, 2, cfg)
self.assertFalse(result["accepted_revision"])
self.assertEqual(result["incumbent"]["response"], "old")
result = select_incumbent([candidate("better", 0.72)], old, 1, 3, cfg)
self.assertTrue(result["accepted_revision"])
self.assertEqual(result["incumbent_iteration"], 3)
result = select_incumbent([candidate("unsafe", 1, False)], old, 1, 2, cfg)
self.assertFalse(result["selected_eligible"])
self.assertEqual(result["incumbent"], old)
with self.assertRaises(ValueError):
select_incumbent([candidate("unsafe", 1, False)], None, None, 1, cfg)
def test_configuration_validation(self):
for kwargs in ({"loops": 4}, {"candidate_workers": 0}, {"seed": -1}, {"max_attempts": 0}):
with self.subTest(kwargs=kwargs), self.assertRaises(ValueError):
ExperimentConfig(**kwargs)
with self.assertRaises(ValueError):
ModelConfig("compatible", "test-model")
with self.assertRaises(ValueError):
ModelConfig("openai", "test-model", api_key_env="literal-key-value")
class DataTests(unittest.TestCase):
def test_bundled_checksums_counts_and_all_references(self):
frames = []
for name, entry in read_json(ROOT / "data" / "checksums.json").items():
path = ROOT / "data" / name
self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), entry["sha256"])
frame = load_dataset(path, "CR" if entry["rows"] == 300 else "PR")
self.assertEqual(len(frame), entry["rows"])
frames.append(frame)
combined = pd.concat(frames, ignore_index=True)
references = PrecomputedReferences(combined)
self.assertEqual(references.audit["n_items"], 1300)
self.assertEqual(references.audit["n_reference_entries"], 6500)
first = combined.iloc[0].to_dict()
self.assertEqual(references.for_row(first), parse_top5(first["top_5_similar"]))
def test_malformed_reference_literal_is_rejected(self):
for text in ("not a list", "[]", "[{}]", "{'references': []}"):
with self.subTest(text=text), self.assertRaises(ValueError):
parse_top5(text)
if __name__ == "__main__":
unittest.main()