from pallet.annotation import (AnnotationSet,Box,FrameAnnotation,Review,record_review,
                               export_coco,export_yolo,save_jsonl,load_jsonl,is_resolved)
from pallet.experiments.evaluate import _count_matches,_coco_ap


def test_partial_ignore_is_bound_roundtripped_and_never_yolo_background(tmp_path):
    frame=FrameAnnotation('f','g','x.png',100,80,source_anchor_hash='anchor',split='test')
    box=Box(10,10,20,20);region=Box(60,10,20,40)
    record_review(frame,Review('a','approve'),status='pos',boxes=[box],ignore_regions=[region])
    record_review(frame,Review('b','approve'),ignore_regions=[region])
    assert is_resolved(frame,require_blind=True)
    path=tmp_path/'labels.jsonl';save_jsonl(AnnotationSet([frame]),path)
    restored=load_jsonl(path)
    assert restored.frames['f'].ignore_regions==[region]
    coco=export_coco(restored)
    assert [r['iscrowd'] for r in coco['annotations']]==[0,1]
    assert export_yolo(restored,tmp_path/'yolo')==[]
    detections=[([10,10,30,30],.9),([61,11,79,49],.8)]
    assert _count_matches(detections,[box],.25,[region])==(1,0,0)
    report=_coco_ap([{'reference_boxes':[[10,10,20,20]],'ignore_regions':[[60,10,20,40]],
                     'predictions':[{'xyxy':b,'score':s} for b,s in detections]}])
    assert report['AP50']>.99
    restored.frames['f'].ignore_regions=[]
    assert not is_resolved(restored.frames['f'])
