File size: 2,185 Bytes
9126e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Contract checks for empty targets, anchor leakage and safe fallback."""
import unittest
import numpy as np
from repair import query_plan,repaired_maps,score_mask

class Contracts(unittest.TestCase):
    def fixture(self,query):
        q,texts=query_plan(query);maps={t:np.zeros((100,100),np.float32) for t in texts}
        target=np.zeros((100,100),np.float32)
        for x in [15,45,75]:target[35:45,x:x+10]=.9
        maps[q.original]=target.copy();maps[q.target]=target.copy()
        if q.anchor:maps[q.anchor][60:70,30:40]=.9;maps[q.opposite]=target.copy()
        return q,maps
    def test_oracle_cannot_change_primary(self):
        q,m=self.fixture('show all red circles to the left of the blue square')
        a,_=repaired_maps(q.original,m,.5)
        fake=np.zeros((100,100),bool);fake[70:80,85:95]=1
        b,_=repaired_maps(q.original,m,.5,oracle_anchor=fake)
        self.assertTrue(np.array_equal(a['cacp'],b['cacp']))
        self.assertEqual(a['cacp'].sum(),100)
    def test_plural_relation_retains_two(self):
        q,m=self.fixture('select all red circles on the right of the blue square')
        p,_=repaired_maps(q.original,m,.5)
        self.assertEqual(p['cacp'].sum(),200)
    def test_ambiguous_anchor_falls_back(self):
        q,m=self.fixture('show all red circles to the left of the blue square')
        m[q.anchor][60:70,80:90]=.9
        p,f=repaired_maps(q.original,m,.5)
        self.assertTrue(np.array_equal(p['cacp'],p['frozen']))
        self.assertEqual(f['reason'],'anchor_ambiguous')
    def test_unsupported_clause_preserves_output(self):
        q,m=self.fixture('the person wearing a hat who is holding a bag')
        p,_=repaired_maps(q.original,m,.5)
        self.assertTrue(np.array_equal(p['cacp'],p['frozen']))
    def test_explicit_action_and_empty_scores(self):
        q,m=self.fixture('do not highlight all red circles')
        p,_=repaired_maps(q.original,m,.5);empty=np.zeros((100,100),bool)
        self.assertFalse(p['cacp'].any())
        self.assertEqual(score_mask(empty,empty)['iou'],1)
        self.assertEqual(score_mask(empty,np.ones_like(empty))['iou'],0)
if __name__=='__main__':unittest.main(verbosity=2)