61 lines
3.3 KiB
Python
61 lines
3.3 KiB
Python
"""Check benchmark scoring and independent fixture annotations (no PDF render)."""
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SPEC = importlib.util.spec_from_file_location('check_margins', ROOT / 'cropping-test/check_margins.py')
|
|
benchmark = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(benchmark)
|
|
|
|
|
|
class MarginBenchmarkTests(unittest.TestCase):
|
|
def test_label_must_fit_both_strip_edges_even_when_start_is_acceptable(self):
|
|
expected = {'start_mm_range': [0, 20], 'labels': [
|
|
{'box_px': [20, 100, 50, 140], 'label': 'left heading'},
|
|
{'box_px': [590, 200, 630, 240], 'label': 'right heading'},
|
|
]}
|
|
result = benchmark.score_page({'left_px': 30, 'right_px': 620}, expected)
|
|
self.assertTrue(result['start_in_range'])
|
|
self.assertFalse(result['pass'])
|
|
self.assertEqual(len(result['missed_labels']), 2)
|
|
self.assertTrue(benchmark.score_page({'left_px': 20, 'right_px': 630}, expected)['pass'])
|
|
|
|
def test_unannotated_page_still_checks_reviewed_start_range(self):
|
|
expected = {'start_mm_range': [0, 3], 'labels': []}
|
|
self.assertFalse(benchmark.score_page({'left_px': 116, 'right_px': 722}, expected)['pass'])
|
|
self.assertTrue(benchmark.score_page({'left_px': 0, 'right_px': 606}, expected)['pass'])
|
|
|
|
def test_fixture_provenance_whole_copy_order_and_suggested_strip_containment(self):
|
|
manifest = json.loads((benchmark.ROOT / 'manifest.json').read_text())
|
|
self.assertEqual(manifest['render_dpi'], 200)
|
|
self.assertEqual({c['id'] for c in manifest['cases']},
|
|
{f'DS02VA-Copie{n:02}' for n in (1, 2, 3, 5, 8, 10, 16, 24, 25, 32)})
|
|
for case in manifest['cases']:
|
|
with self.subTest(case=case['id']):
|
|
self.assertEqual(hashlib.sha256((benchmark.ROOT / case['source']).read_bytes()).hexdigest(),
|
|
case['sha256'])
|
|
self.assertEqual([p['page'] for p in case['pages']], list(range(1, case['page_count'] + 1)))
|
|
self.assertTrue(case['explanation'])
|
|
name_box = case['name_localization']['box_px']
|
|
self.assertEqual(case['name_localization']['page_height_px'],
|
|
case['recorded_schema']['page_crops'][0]['height_px'])
|
|
self.assertAlmostEqual(case['name_bottom_height_fraction'],
|
|
name_box[3] / case['name_localization']['page_height_px'])
|
|
self.assertTrue(any(p['labels'] for p in case['pages']))
|
|
for expected, recorded in zip(case['pages'], case['recorded_schema']['page_crops'], strict=True):
|
|
start = round(expected['suggested_start_mm'] * 200 / 25.4)
|
|
crop = {'left_px': start, 'right_px': min(recorded['width_px'],
|
|
start + recorded['right_px'] - recorded['left_px'])}
|
|
self.assertTrue(benchmark.score_page(crop, expected)['pass'])
|
|
for label in expected['labels']:
|
|
x0, y0, x1, y1 = label['box_px']
|
|
self.assertTrue(0 <= x0 < x1 <= recorded['width_px'])
|
|
self.assertTrue(0 <= y0 < y1 <= recorded['height_px'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|