Repository navigation
feat(backend): net tracing v2 with pin assignment and schema 2 exporter - #27
Merged
Merged
Conversation
…er, pin assignment and schema 2 exporter Replace the Hough segment extractor and the left/right-of-box gate heuristics with a classical net tracer: background-normalised ink, symbol erasure with size-relative margins, skeleton graph with junction-dot vs crossover rules, pin assignment from detector keypoints or a geometry fallback with an orientation estimate, global supply nets, and OCR of port and supply labels. The graph is now an undirected component/net multigraph and the exporter emits schema 2 (pins, nets, ReactFlow edges, library cells) while keeping the legacy gate-level connections for the current importer. bench_tracing.py drives the tracer from perturbed T2 ground truth and scores it with the T6 evaluator, which now also reads T2 pin-map netlists and net objects. Scores: on 300 held-out T2 samples (seed 1) the tracer reaches net F1 0.9995 and exact match 99.3% on clean renders, net F1 0.9989 and exact match 98.7% on hand-drawn-style renders, truth-table equivalence 99.0%, p95 runtime 0.29 s per image. These scores come from perturbed ground-truth detections (box and keypoint jitter, dropped junction dots): no trained detector for the new classes exists yet, so the numbers bound the tracer, not the end-to-end photo pipeline. The T3 round-trip truth-table criterion waits for T3.
…rough repair on partial keypoints
✅ Deploy Preview for digisim ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
DigiSim is a neglected project. Pull up the latest changes in Linux. It is high time to start training the model for it. I have not been able to provide the data. Figure out, get the data, train a model, make improvements and fix that. Do not come to me until you absolutely need me.
Context: the scout report at ~/linux-fleet-home/data/digisim-ml-scout/report.md found that the only trained detector uses 12 gate-era classes and detects nothing on the transistor-first schematics the app now uses, that the transistor-level training data does not exist yet, and that the wire/net tracing assumes left-to-right gates. It split the work into tasks T1-T11 (section 5); this task is one of them. T1 (class schema ml/classes.yaml), T2 (synthetic generator v2 with netlist ground truth) and T6 (evaluation harness v2 with end-to-end net and truth-table metrics) are already merged on the default branch.
This task is the report's T8 - Net tracing v2 and exporter. Its substance: the wire/net step (backend/pipeline/graph_builder.py, wire_extractor.py) assumes left-to-right gates with an output on the right and inputs on the left, but transistors have D/G/S/B terminals and no direction, so the graph builder and exporter must be rewritten as a net-based classical CV stage (still no neural net): binarise and mask out detected symbol bodies while keeping lead ends attached; skeletonise and build a pixel graph whose connected components are candidate nets; at a degree-4 skeleton node, connect only if a JUNCTION detection sits on it, otherwise split it into two straight-through wires (crossover); pin assignment where each symbol's lead endpoints at its box boundary become pins, with an orientation head (or keypoints) deciding which MOSFET pin is D, G, S (B optional) and a single pin for VDD/GND/ports; emit nets (sets of pins) rather than directed gate edges, with an exporter that turns nets into connections between pins using the app's t:/s: handles, supply symbols becoming vsource/ground nodes, and types nmos pmos vsource ground input output plus library cells for gate symbols; attach OCR text labels to the nearest port/net for naming; make the pixel-absolute thresholds (min_line_length, max_line_gap, min_component_diag, NET_JOIN_DISTANCE) scale with image and symbol size; remove the gate-direction heuristics (graph_builder.py right-half-is-output / left-half-is-input and top/bottom-half port choice). Acceptance in the report: on 300 T2 samples with ground-truth nets, net F1 >= 0.90 and netlist exact-match >= 70% on clean renders and >= 50% on hand-drawn-style renders; on the T3 round-trip set the reconstructed circuit gives the same truth table as the source in >= 80% of cases; tests cover crossover with/without dot, branching supply rail, rotated/mirrored transistor, 3 vs 4 terminal symbols; runtime < 2 s per image on this CPU. The frontend import integration (updating importCircuit for transistor handles) is the separate T9 task.
What Changed
wire_extractor.py, directionalgraph_builder.py) with a classical-CV net tracer inbackend/pipeline/(ink.py,skeleton_graph.py,net_tracer.py,pin_assignment.py,label_attach.py,trace_types.py). It binarises ink, erases symbol bodies, skeletonises wires into a pixel graph, and treats a degree-4 node as a connection only when a JUNCTION detection sits on it (otherwise a crossover). Thresholds scale with stroke width and median symbol size. Pins come from detector keypoints or a geometry fallback with orientation estimation and MOSFET D/G/S resolution. OCR text is attached to ports and nets.GraphBuildernow builds an undirected component/net graph, and/detect_circuitinapp.pyuses the newNetTracer.CircuitExporternow emitsschema_version2 (per-component pins, orientation, library cell, nets and ReactFlow edges witht:/s:handles) alongside the legacyconnectionslist.class_map.pyandml/class_schema.pygain a generatedCLASS_TERMINALSmap.frontend/src/types/api.tsadds the matching optional types, andscikit-imageis added to the backend dependencies.backend/test_net_tracer.pyandbackend/bench_tracing.py(scores traced nets against T2 synthetic ground truth).ml/eval_netlist.pynow also accepts the T2 pin-map netlist shape and object-form nets, with test updates. Docs (README.md,agent_docs/ml-pipeline.md,.claude/agents/model.md,testing.md) are updated.Risk Assessment
✅ Low: The two round-2 fixes (downscaled paper-background estimate in ink_mask, and skipping pass-through repair when a symbol has partial keypoints) match the recorded instructions and behave correctly. A timing probe of the full tracer on scenes up to 2560 px wide stayed at 0.23 s, so no performance concern remains.
Testing
I generated 300 synthetic circuits (seed 1) and ran the bench with truth-table checking in keypoint and geometry modes. Keypoint mode gave net F1 0.9995 clean and 0.9989 hand-drawn, with exact match 99.3% and 98.7% and p95 runtime about 0.16 s. Geometry fallback gave exact match 88.7% clean and 87.2% hand-drawn, with truth-table match about 92%. The bench scores come from perturbed ground-truth detections, so they bound the tracer rather than the end-to-end photo pipeline. I then ran the real Flask app over HTTP with a stand-in detector. Three transistor circuits returned schema 2 output whose net sizes and component types matched ground truth, in 0.07 to 0.18 s. A 4000x3000 upload traced correctly in about 0.8 s, which covers the large-image fix. Dropping keypoints did not merge drain and source nets. Missing image gave 400, undecodable image gave 400, and no auth cookie gave 401. The targeted unit tests for crossover with and without a dot, branching rails, rotated and mirrored transistors, and 3 versus 4 terminal symbols passed, but they ran the tracer directly on rendered images and were not driven through the running service, so that scenario is recorded as untested. The T3 round-trip criterion is deferred, as the recorded decision says. The new class set has no trained detector yet, so nothing here exercised real detections or the frontend. No UI changed, so there are no screenshots. The
frontend/node_modulesinstall and all temp data were cleaned or are gitignored, and the working tree is clean.Evidence: Bench with truth tables (300 T2 samples, both pin modes)
Evidence: Bench without truth tables
Evidence: /detect_circuit runs vs ground truth, including 4000x3000 timing
Evidence: /detect_circuit with partial or no keypoints and error cases
Evidence: Full /detect_circuit JSON response for sample 000000
Evidence: test_net_tracer.py verbose results
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 1 issue found → auto-fixed (2) ✅
agent_docs/ml-pipeline.md:30- The intent's acceptance list requires that on the T3 round-trip set the reconstructed circuit gives the same truth table as the source in >= 80% of cases. The change does not implement or measure this. agent_docs/ml-pipeline.md says "Not yet: the T3 screenshot round-trip check", and bench_tracing.py only scores the T2 synthetic samples (net F1, exact match, and an optional --truth-table on T2 ground truth). No bench results for net F1 >= 0.90, exact match >= 70% clean / >= 50% hand-drawn, or runtime < 2 s are recorded in the diff either. Whether this criterion is deferred (for example because the T3 set is not merged yet) is the author's call.🔧 Fix applied.
2 warnings still open:
backend/pipeline/ink.py:63- ink_mask() estimates the paper background with a morphological CLOSE using an elliptical kernel that is 3% of the longer image side, on the full-resolution float32 image. A 4000x3000 camera photo took 10.6 s in ink_mask alone (measured: 1024x768 takes 0.06 s). /detect_circuit passes the uploaded image at native size, and the frontend comment at App.tsx:~840 notes camera photos can be 4k wide, so the stated acceptance of < 2 s per image is lost on the real endpoint. The bench only covers T2 render sizes, so this goes unmeasured. Skeletonisation and the symbol-scaled closing kernel add more cost at that size. Siblings that scale with resolution in the same way: net_tracer.py:211-212 (closing kernel grows with symbol size, full-res), net_tracer.py:220 and skeleton_graph.py:107 (skeletonize on the full mask), app.py:154-156 (no downscale before NetTracer). Smallest fix: compute the background at a reduced resolution (or cap the working long side) and upsample it, and bound the tracer's working size.backend/pipeline/net_tracer.py:252- _repair_pass_through unions any two collinear, opposite leftover contacts of a symbol, assuming a wire runs past the symbol. In keypoint mode, terminals whose keypoints were dropped (visibility < KEYPOINT_MIN_VISIBILITY, detector.py) also leave their contacts unused. Trace: a vertical NMOS with only the 'g' keypoint kept has its drain and source wires as unused contacts, collinear and opposite. They are unioned, so the drain net and source net merge. Every other component's pins on those two nets then land on one wrong net, with no error or warning. The same gate applies to any symbol with a partial keypoint set. Fix: skip the repair for symbols whose class has declared terminals that went unassigned, or first fall back to assign_by_geometry for those terminals.🔧 Fix applied.
✅ Re-checked - no issues remain.
agent_docs/ml-pipeline.md:40- The geometry-mode hand-drawn bench numbers in agent_docs/ml-pipeline.md (net F1 0.9799, exact match 86.6%) differ slightly from this run (0.9816, 87.2%). Every other figure matches. The small drift is probably from the later ink-background downscale.uv run pytest test_net_tracer.py test_class_schema.pyin backend (56 passed)uv run pytest tests/test_eval_netlist.pyin ml (11 passed, 2 skipped)uv run python data_gen/generate_v2.py --count 300 --seed 1, thenuv run python bench_tracing.py --count 300 --mode both --truth-tableReal Flask/detect_circuitover HTTP with an auth cookie: 3 transistor circuits, a 4000x3000 upload, partial or no keypoints, and 400/401 error cases✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.