inire/scripts/export_advanced_examples.py

52 lines
2.3 KiB
Python

"""Export the advanced example inputs from the canonical benchmark fixtures."""
from argparse import ArgumentParser
from json import dumps
from pathlib import Path
from examples.advanced_layouts import COMPLEX_EXAMPLE, COMPLEX_EXAMPLE_POINTS, INPUTS, build_problem
from inire.tests.routing_corpus import CASE_GROUPS, profile_options
from inire.tests.stress_routing_benchmarks import STRESS_BENCHMARK_CASES
def main() -> None:
parser = ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, default=INPUTS)
args = parser.parse_args()
cases = [(case.name, case) for case in STRESS_BENCHMARK_CASES]
for name in (COMPLEX_EXAMPLE, COMPLEX_EXAMPLE_POINTS):
(case,) = CASE_GROUPS[name]
cases.append((name, case))
entries = []
for name, case in cases:
problem = case.problem
options = profile_options(case, "public-defaults")
data = {
"bounds": list(problem.bounds),
"clearance": problem.clearance,
"bend_radii": list(options.search.bend_radii),
"sbend_radii": list(options.search.sbend_radii),
"infer_terminal_headings": options.topology.infer_terminal_headings,
"keepouts": [polygon.wkt for polygon in problem.static_obstacles],
"nets": [(net.net_id, net.start.as_tuple(), net.target.as_tuple(), net.width) for net in problem.nets],
}
rebuilt, settings = build_problem(data)
if problem != rebuilt or options != settings:
raise ValueError(f"The advanced example no longer reproduces fixture {name}")
# Keep ports and each net on one line so the input file is easy to scan.
lines = [f" {dumps(name)}: {{"]
for field in ("bounds", "clearance", "bend_radii", "sbend_radii", "infer_terminal_headings"):
lines.append(f" {dumps(field)}: {dumps(data[field])},")
for field in ("keepouts", "nets"):
values = ",\n".join(f" {dumps(value)}" for value in data[field])
comma = "," if field == "keepouts" else ""
lines.append(f" {dumps(field)}: [\n{values}\n ]{comma}")
entries.append("\n".join((*lines, " }")))
args.output.write_text("{\n" + ",\n".join(entries) + "\n}\n", encoding="utf-8")
if __name__ == "__main__":
main()