-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
70 lines (56 loc) · 2.82 KB
/
Copy pathcli.py
File metadata and controls
70 lines (56 loc) · 2.82 KB
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
"""Command-line entry point: analyse a path and write a curvature review.
curvecheck path.json [-o out_dir] [--r-min R]
Writes report.json, report.md, and curvature.png into the output directory.
"""
from __future__ import annotations
import argparse
import os
import pathio
import report
def run(source, out_dir=".", r_min=None, **loss_kwargs):
"""Analyse a path (JSON path, points, or a gdsfactory Path) and write the review artefacts."""
os.makedirs(out_dir, exist_ok=True)
points = pathio.centerline(source)
rep = report.analyze(points, r_min=r_min, **loss_kwargs)
json_path = os.path.join(out_dir, "report.json")
md_path = os.path.join(out_dir, "report.md")
fig_path = os.path.join(out_dir, "curvature.png")
with open(json_path, "w", encoding="utf-8") as f:
f.write(report.to_json(rep))
with open(md_path, "w", encoding="utf-8") as f:
f.write(report.to_markdown(rep))
report.figure(points, fig_path, r_min=r_min)
return {"json": json_path, "markdown": md_path, "figure": fig_path}
def _validate():
import tempfile
import numpy as np
import mincurv
path = mincurv.build_path([(0.0, 30), (0.1, np.pi / 2 * 10), (0.0, 30)])
with tempfile.TemporaryDirectory() as d:
src = os.path.join(d, "path.json")
pathio.save_json(path, src)
out = run(src, os.path.join(d, "out"), r_min=30.0)
ok_md = os.path.exists(out["markdown"]) and os.path.getsize(out["markdown"]) > 0
ok_json = os.path.exists(out["json"]) and os.path.getsize(out["json"]) > 0
ok_fig = os.path.exists(out["figure"]) and os.path.getsize(out["figure"]) > 1000
md = open(out["markdown"], encoding="utf-8").read()
ok_content = "minimum radius" in md and "violation" in md
for label, cond in [("report.md written", ok_md), ("report.json written", ok_json),
("curvature figure written", ok_fig), ("review content present", ok_content)]:
print(f"[{label}] {'PASS' if cond else 'FAIL'}")
print("RESULT:", "PASS" if all([ok_md, ok_json, ok_fig, ok_content]) else "FAIL")
def main():
ap = argparse.ArgumentParser(description="curvecheck - curvature analysis for photonic paths")
ap.add_argument("path", nargs="?", help="path JSON file (a list of [x, y] points)")
ap.add_argument("-o", "--out", default=".", help="output directory")
ap.add_argument("--r-min", type=float, default=None, help="minimum radius for the curvature DRC (um)")
ap.add_argument("--validate", action="store_true")
args = ap.parse_args()
if args.validate:
_validate(); return
if not args.path:
ap.error("provide a path JSON file, or --validate")
out = run(args.path, args.out, r_min=args.r_min)
print(f"wrote {out['markdown']}, {out['json']}, and {out['figure']}")
if __name__ == "__main__":
main()