Orienter / tests /test_evaluation_tools.py
stereoid's picture
Add files using upload-large-folder tool
3f3265f verified
Raw
History Blame Contribute Delete
4.84 kB
import json
import sys
import tempfile
import unittest
from itertools import chain, repeat
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from evaluation import context_eval_baselines
from evaluation.tools import to_pred
class ToPredTests(unittest.TestCase):
def test_task_is_required(self):
parser = to_pred.build_parser()
with self.assertRaises(SystemExit):
parser.parse_args(["--input", "pred.json", "--output", "out.json"])
def test_convert_old_predictions_to_semantics_fields(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
pred_path = tmp / "pred.json"
question_path = tmp / "questions.jsonl"
output_path = tmp / "out.json"
pred_path.write_text(
json.dumps(
{
"q1": {
"oovd_result": {
"button": [
{
"bbox_pixels": [1, 2, 3, 4],
"probability": 0.7,
}
]
}
}
}
)
)
question_path.write_text(
json.dumps({"question_id": "q1", "image": "1026760_11.jpg"}) + "\n"
)
to_pred.main(
[
"--task",
"semantics",
"--input",
str(pred_path),
"--questions",
str(question_path),
"--output",
str(output_path),
]
)
converted = json.loads(output_path.read_text())
self.assertEqual(
converted,
[
{
"image_id": 1026760011,
"category_id": "button",
"bbox": [1, 2, 3, 4],
"score": 0.7,
}
],
)
def test_all_outputs_do_not_overwrite(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
pred_path = tmp / "pred.json"
output_path = tmp / "converted.json"
pred_path.write_text(
json.dumps(
[
{
"image_id": 1,
"category_id": "trigger",
"bbox": [1, 2, 3, 4],
"score": 0.5,
}
]
)
)
to_pred.main(
[
"--task",
"all",
"--input",
str(pred_path),
"--output",
str(output_path),
]
)
self.assertFalse(output_path.exists())
for task in to_pred.TASKS:
self.assertTrue((tmp / f"converted_{task}.json").exists())
interactable = json.loads((tmp / "converted_interactable.json").read_text())
interaction = json.loads((tmp / "converted_interaction.json").read_text())
semantics = json.loads((tmp / "converted_semantics.json").read_text())
self.assertEqual(interactable[0]["category_id"], 1)
self.assertEqual(interaction[0]["category_id"], "trigger")
self.assertEqual(semantics[0]["category_id"], "trigger")
class ContextEvalBaselineTests(unittest.TestCase):
def test_validate_unique_methods_rejects_duplicates(self):
with self.assertRaises(ValueError):
context_eval_baselines.validate_unique_methods(["Seed-E2E", "Seed-E2E"])
def test_main_uses_subprocess_check_true(self):
args = context_eval_baselines.build_parser().parse_args([])
methods = ["CenterNet2"]
with mock.patch.object(
context_eval_baselines, "METHODS", methods
), mock.patch.object(
context_eval_baselines, "LLM_METHODS", []
), mock.patch.object(
context_eval_baselines.os.path,
"exists",
side_effect=chain([True, False], repeat(False)),
), mock.patch.object(
context_eval_baselines.subprocess, "run"
) as run:
context_eval_baselines.main(args)
self.assertTrue(run.called)
self.assertTrue(run.call_args.kwargs["check"])
self.assertIsInstance(run.call_args.args[0], list)
if __name__ == "__main__":
unittest.main()