File size: 3,691 Bytes
a863f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import importlib.util
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent


def load_module():
    spec = importlib.util.spec_from_file_location("synth_report", HERE / "synth_report.py")
    module = importlib.util.module_from_spec(spec)
    sys.modules["synth_report"] = module
    spec.loader.exec_module(module)
    return module


def build(prompts, answers):
    prompts_map = {}
    for (scene, state, cond), truth in prompts.items():
        prompts_map[(scene, state, cond)] = {"truth": truth}
    preds = {}
    for key, answer in answers.items():
        preds[key] = {"answer": answer}
    return prompts_map, preds


def test_matrix_identity():
    module = load_module()
    scene = "left_right_0001"
    prompts, answers = {}, {}
    truths = {"a": "yes", "b": "no"}
    for state, truth in truths.items():
        for cond in ("T0", "Tpos", "Tneg", "Tabs", "Tunk"):
            prompts[(scene, state, cond)] = truth
    # 完全跟随视觉:断言不影响答案
    for cond in ("T0", "Tpos", "Tneg", "Tabs", "Tunk"):
        answers[(scene, "a", cond)] = "yes"
        answers[(scene, "b", cond)] = "no"
    prompts_map, preds = build(prompts, answers)
    out = module.matrix_metrics(prompts_map, [(scene, "a"), (scene, "b")], preds)
    assert out["n_scene_pairs"] == 1, out
    assert abs(out["v"] - 1.0) < 1e-9, out
    assert abs(out["t"] - 0.0) < 1e-9, out
    assert abs(out["k"] - 0.0) < 1e-9, out
    assert abs(out["pfc_Tpos"] - 1.0) < 1e-9, out
    assert abs(out["pfc_Tneg"] - 1.0) < 1e-9, out


def test_matrix_follows_text():
    module = load_module()
    scene = "above_below_0002"
    prompts, answers = {}, {}
    for state, truth in (("a", "yes"), ("b", "no")):
        for cond in ("T0", "Tpos", "Tneg", "Tabs", "Tunk"):
            prompts[(scene, state, cond)] = truth
    # 完全服从文本:Tpos 一律 yes,Tneg 一律 no
    for state in ("a", "b"):
        answers[(scene, state, "T0")] = "yes"
        answers[(scene, state, "Tpos")] = "yes"
        answers[(scene, state, "Tneg")] = "no"
        answers[(scene, state, "Tabs")] = "yes"
        answers[(scene, state, "Tunk")] = "yes"
    prompts_map, preds = build(prompts, answers)
    out = module.matrix_metrics(prompts_map, [(scene, "a"), (scene, "b")], preds)
    assert abs(out["v"] - 0.0) < 1e-9, out
    assert abs(out["t"] - 1.0) < 1e-9, out
    assert abs(out["k"] - 0.0) < 1e-9, out
    assert abs(out["pfc_Tpos"] - 0.0) < 1e-9, out


def test_matrix_interaction():
    module = load_module()
    scene = "on_not_on_0003"
    prompts, answers = {}, {}
    for state, truth in (("a", "yes"), ("b", "no")):
        for cond in ("T0", "Tpos", "Tneg", "Tabs", "Tunk"):
            prompts[(scene, state, cond)] = truth
    # 视觉为真时跟随文本,视觉为假时忽略文本
    answers[(scene, "a", "T0")] = "yes"
    answers[(scene, "a", "Tpos")] = "yes"
    answers[(scene, "a", "Tneg")] = "no"
    answers[(scene, "a", "Tabs")] = "yes"
    answers[(scene, "a", "Tunk")] = "yes"
    answers[(scene, "b", "T0")] = "no"
    answers[(scene, "b", "Tpos")] = "no"
    answers[(scene, "b", "Tneg")] = "no"
    answers[(scene, "b", "Tabs")] = "no"
    answers[(scene, "b", "Tunk")] = "no"
    prompts_map, preds = build(prompts, answers)
    out = module.matrix_metrics(prompts_map, [(scene, "a"), (scene, "b")], preds)
    assert abs(out["t"] - 0.5) < 1e-9, out
    assert abs(out["k"] - 1.0) < 1e-9, out
    assert abs(out["v"] - 0.5) < 1e-9, out


if __name__ == "__main__":
    for name, fn in list(globals().items()):
        if name.startswith("test_") and callable(fn):
            fn()
            print("ok", name)
    print("all tests passed")