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")