Download CODE/evaluation/tests/test_portable.py from QLU-LLM/CAD: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/datasets/QLU-LLM/CAD/resolve/main/CODE/evaluation/tests/test_portable.py
- Command line
-
hf download hf://datasets/QLU-LLM/CAD/CODE/evaluation/tests/test_portable.py
-
curl -L -o test_portable.py https://huggingface.co/datasets/QLU-LLM/CAD/resolve/main/CODE/evaluation/tests/test_portable.py
3.16 kB
| """Portable reporting, population and frozen-source regression tests.""" | |
| import copy | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import sys | |
| import unittest | |
| ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(ROOT)) | |
| from evaluate import load_population | |
| from reporting import COMPONENTS, THRESHOLDS, aggregate, make_record | |
| def contract(eligible=True): | |
| return {'full_gt': {'annotation_eligible': eligible}, | |
| 'specs': [{'id': 'dimension0', 'gt_dimension_index': 0}]} | |
| def perfect(): | |
| result = {'metric_version': 'v5.2-rc5k-final', | |
| 'components': {key: {'score': 1., 'eligible': True} for key in COMPONENTS.values()}, | |
| 'benchmark': {'thresholds': dict(THRESHOLDS), 'view_pass': True, 'layout_pass': True}} | |
| result['components']['annotation']['details'] = { | |
| 'scope': 'full_gt', 'n_selected_target': 1, 'n_matched_predicted_native': 1, | |
| 'n_total_gt_dimension_targets': 1, | |
| 'items': [{'id': 'dimension0', 'gt_dimension_index': 0, 'presence': 1., 'pred_dimension_index': 0}], | |
| } | |
| return result | |
| class PortableTests(unittest.TestCase): | |
| def test_failed_output_keeps_denominators(self): | |
| good = make_record('a', contract(), perfect()) | |
| failed = make_record('b', contract(), status='execution_error') | |
| summary = aggregate([good, failed]) | |
| self.assertEqual(summary['sample_count'], 2) | |
| self.assertEqual(summary['camr_denominator'], 2) | |
| self.assertTrue(all(v == 50. for v in summary['metrics'].values())) | |
| def test_unknown_annotation_is_not_complete(self): | |
| raw = perfect() | |
| raw['components']['annotation'].pop('details') | |
| summary = aggregate([make_record('a', contract(), raw)]) | |
| self.assertEqual(summary['annotation_unknown_count'], 1) | |
| self.assertEqual(summary['camr_denominator'], 1) | |
| self.assertEqual(summary['metrics']['CAMR'], 0.) | |
| def test_gt_inapplicable_annotation(self): | |
| raw = perfect() | |
| raw['components']['annotation'].update(score=None, eligible=False) | |
| summary = aggregate([make_record('a', contract(False), raw)]) | |
| self.assertEqual(summary['metrics']['AQ'], 0.) | |
| self.assertIsNone(summary['metrics']['CAMR']) | |
| self.assertEqual(summary['annotation_unknown_count'], 0) | |
| def test_duplicate_ids_rejected(self): | |
| item = make_record('a', contract(), perfect()) | |
| with self.assertRaises(ValueError): | |
| aggregate([item, copy.deepcopy(item)]) | |
| def test_population_contracts(self): | |
| for name, n in [('test1045', 1045), ('paper1041', 1041), ('validation254', 254)]: | |
| ids, rows = load_population(name) | |
| self.assertEqual(len(ids), n) | |
| self.assertEqual(len(set(ids)), n) | |
| self.assertTrue(set(ids) <= set(rows)) | |
| def test_frozen_source_hashes(self): | |
| provenance = json.loads((ROOT / 'PROVENANCE.json').read_text()) | |
| for name, expected in provenance['source_files_sha256'].items(): | |
| self.assertEqual(hashlib.sha256((ROOT / name).read_bytes()).hexdigest(), expected, name) | |
| if __name__ == '__main__': | |
| unittest.main() | |