Add files using upload-large-folder tool
Browse files
derived/Source/scripts/build_circuit_dataset.py
CHANGED
|
@@ -44,7 +44,10 @@ GRAPH_WORD_COUNT_MAX = 50
|
|
| 44 |
GRAPH_TEXT_FRACTION_MAX = 0.18
|
| 45 |
GRAPH_RENDER_MAE_MAX = 3.0
|
| 46 |
GRAPH_RENDER_PHASH_MAX = 8
|
| 47 |
-
GRAPH_DECORATIVE_MAX =
|
|
|
|
|
|
|
|
|
|
| 48 |
NO_TEXT_PS_OVERRIDE = (
|
| 49 |
"\n"
|
| 50 |
"/show { pop } bind def\n"
|
|
@@ -167,21 +170,274 @@ ELECTRICAL_FAMILIES = {
|
|
| 167 |
MIN_CONNECTED_PINS = {
|
| 168 |
"ac_source": 2,
|
| 169 |
"capacitor": 2,
|
|
|
|
| 170 |
"diode": 2,
|
| 171 |
"fuse": 2,
|
| 172 |
"ground": 1,
|
| 173 |
"integrated_circuit": 2,
|
| 174 |
"inductor": 2,
|
|
|
|
| 175 |
"junction": 1,
|
| 176 |
"led": 2,
|
| 177 |
"logic_gate": 2,
|
| 178 |
"op_amp": 3,
|
| 179 |
"potentiometer": 3,
|
|
|
|
| 180 |
"resistor": 2,
|
|
|
|
| 181 |
"terminal": 1,
|
|
|
|
| 182 |
"transistor": 3,
|
|
|
|
| 183 |
"voltage_source": 2,
|
| 184 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
STRIP_INVOCATION_PROCEDURES = {"person", "person_shocked"}
|
| 186 |
NON_CIRCUIT_SOURCE_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
|
| 187 |
(
|
|
@@ -552,7 +808,11 @@ def parse_eps_metadata(eps_path: Path) -> dict[str, Any]:
|
|
| 552 |
creator_match = XCIRCUIT_CREATOR_RE.search(text)
|
| 553 |
creator = normalize_space(creator_match.group(1)) if creator_match else None
|
| 554 |
procedures = sorted(set(EPS_PROC_RE.findall(text)))
|
| 555 |
-
circuit_tokens = sorted(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
is_xcircuit = bool(creator and "circuit" in creator.lower())
|
| 557 |
return {
|
| 558 |
"creator": creator,
|
|
@@ -902,6 +1162,36 @@ def component_family_kind(name: str) -> tuple[str, str]:
|
|
| 902 |
if name in PROCEDURE_KIND_MAP:
|
| 903 |
return PROCEDURE_KIND_MAP[name]
|
| 904 |
lower = name.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
if lower in {"clip", "label", "marks"} or lower.startswith("die_") or lower.startswith("truth_"):
|
| 906 |
return "decorative", lower
|
| 907 |
if "arrow" in lower:
|
|
@@ -910,6 +1200,8 @@ def component_family_kind(name: str) -> tuple[str, str]:
|
|
| 910 |
return "active", "op_amp"
|
| 911 |
if name.endswith("_gate") or "gate" in lower or lower in {"nor", "buffer"} or "flipflop" in lower or lower.startswith("dip_"):
|
| 912 |
return "logic", "logic_gate"
|
|
|
|
|
|
|
| 913 |
if "resistor" in lower or lower == "strain_gauge":
|
| 914 |
return "passive", "resistor"
|
| 915 |
if "capacitor" in lower or "polarized" in lower:
|
|
@@ -922,11 +1214,30 @@ def component_family_kind(name: str) -> tuple[str, str]:
|
|
| 922 |
return "source", "voltage_source"
|
| 923 |
if "isource" in lower or "current" in lower:
|
| 924 |
return "source", "current_source"
|
|
|
|
|
|
|
| 925 |
if "switch" in lower or "toggle" in lower or "pushbutton" in lower or "contact" in lower:
|
| 926 |
return "switch", "switch"
|
| 927 |
-
if
|
| 928 |
-
|
| 929 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 930 |
return "indicator", "indicator"
|
| 931 |
if "connector" in lower or "terminal" in lower or lower in {"stud", "tap", "powerpole"}:
|
| 932 |
return "io", "terminal"
|
|
@@ -944,7 +1255,7 @@ def classify_component(component: dict[str, Any]) -> str:
|
|
| 944 |
return "terminal"
|
| 945 |
if family in {"active", "passive", "source", "semiconductor", "switch", "indicator", "protection", "logic"}:
|
| 946 |
return "electrical"
|
| 947 |
-
if family
|
| 948 |
return "decorative"
|
| 949 |
if family == "unknown":
|
| 950 |
return "unknown_bad"
|
|
@@ -1025,10 +1336,47 @@ def point_inside_bbox(
|
|
| 1025 |
return left - margin <= x <= right + margin and bottom - margin <= y <= top + margin
|
| 1026 |
|
| 1027 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1028 |
def snap_key(point: tuple[float, float]) -> tuple[int, int]:
|
| 1029 |
return (round(point[0] / COORD_SNAP), round(point[1] / COORD_SNAP))
|
| 1030 |
|
| 1031 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1032 |
def _projection_fraction(
|
| 1033 |
point: tuple[float, float],
|
| 1034 |
start: tuple[float, float],
|
|
@@ -1098,6 +1446,109 @@ def sort_points_on_segment(
|
|
| 1098 |
return sorted(points, key=lambda point: _projection_fraction(point, start, end))
|
| 1099 |
|
| 1100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1101 |
def count_words(text: str) -> int:
|
| 1102 |
return len(re.findall(r"[A-Za-z0-9_]+", text))
|
| 1103 |
|
|
@@ -1298,7 +1749,7 @@ def build_eps_ir(source_code: str) -> dict[str, Any]:
|
|
| 1298 |
)
|
| 1299 |
|
| 1300 |
local_pins = proc_pin_catalog.get(proc_name, [])
|
| 1301 |
-
if not local_pins and family in {"passive", "source", "indicator", "protection"} and local_bbox:
|
| 1302 |
bx, by, bw, bh = local_bbox
|
| 1303 |
if abs(bh) >= abs(bw):
|
| 1304 |
local_pins = [
|
|
@@ -1315,9 +1766,6 @@ def build_eps_ir(source_code: str) -> dict[str, Any]:
|
|
| 1315 |
{"label": kind, "kind": "terminal", "x": 0.0, "y": 0.0, "raw": "inferred"}
|
| 1316 |
]
|
| 1317 |
|
| 1318 |
-
if not local_pins and family != "decorative":
|
| 1319 |
-
warnings_list.append(f"missing_pins:{proc_name}:{component_id}")
|
| 1320 |
-
|
| 1321 |
for pin_position, pin_info in enumerate(local_pins, start=1):
|
| 1322 |
global_point = transform_component_point(
|
| 1323 |
float(pin_info["x"]),
|
|
@@ -1427,6 +1875,14 @@ def build_eps_ir(source_code: str) -> dict[str, Any]:
|
|
| 1427 |
}
|
| 1428 |
)
|
| 1429 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1430 |
wire_polylines.extend(synthesize_component_spans(components, pins))
|
| 1431 |
|
| 1432 |
node_points: dict[tuple[int, int], tuple[float, float]] = {}
|
|
@@ -1709,25 +2165,47 @@ def graph_metrics_for_row(row: dict[str, Any], overlay: dict[str, Any], original
|
|
| 1709 |
or int(original_render.get("phash_distance", 999)) <= GRAPH_RENDER_PHASH_MAX
|
| 1710 |
)
|
| 1711 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1712 |
graph_core_reasons: list[str] = []
|
| 1713 |
if row.get("parse_status") == "failed":
|
| 1714 |
graph_core_reasons.append("parse_not_ok")
|
| 1715 |
if len(active_component_ids) == 0:
|
| 1716 |
graph_core_reasons.append("electrical_component_count_zero")
|
| 1717 |
-
if
|
| 1718 |
-
graph_core_reasons.append("electrical_graph_components_gt_1")
|
| 1719 |
-
if underconnected_components:
|
| 1720 |
graph_core_reasons.append("electrical_component_underconnected")
|
| 1721 |
-
if
|
| 1722 |
-
graph_core_reasons.append("
|
| 1723 |
-
if
|
| 1724 |
graph_core_reasons.append("unrecognized_component_present")
|
| 1725 |
-
if int(component_class_counts.get("decorative", 0)) > GRAPH_DECORATIVE_MAX:
|
| 1726 |
-
graph_core_reasons.append("decorative_component_count_gt_1")
|
| 1727 |
if overlapping_active_components:
|
| 1728 |
graph_core_reasons.append("active_component_span_overlap")
|
| 1729 |
|
| 1730 |
pretty_reasons = list(graph_core_reasons)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1731 |
if int(overlay.get("word_count", 0)) > GRAPH_WORD_COUNT_MAX:
|
| 1732 |
pretty_reasons.append("word_count_gt_50")
|
| 1733 |
if float(overlay.get("text_mask_fraction", 0.0)) > GRAPH_TEXT_FRACTION_MAX:
|
|
@@ -1757,6 +2235,11 @@ def graph_metrics_for_row(row: dict[str, Any], overlay: dict[str, Any], original
|
|
| 1757 |
"overlapping_active_component_group_count": len(overlapping_active_components),
|
| 1758 |
"one_pin_active_net_count": one_pin_active_net_count,
|
| 1759 |
"one_pin_terminal_only_net_count": one_pin_terminal_only_net_count,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1760 |
"component_class_counts": dict(component_class_counts),
|
| 1761 |
"word_count": int(overlay.get("word_count", 0)),
|
| 1762 |
"text_mask_fraction": float(overlay.get("text_mask_fraction", 0.0)),
|
|
@@ -1971,6 +2454,8 @@ def derive_crop_bbox(
|
|
| 1971 |
source_code: str,
|
| 1972 |
analysis_png_path: Path,
|
| 1973 |
) -> tuple[int, int, int, int] | None:
|
|
|
|
|
|
|
| 1974 |
with Image.open(analysis_png_path) as image:
|
| 1975 |
gray = np.array(image.convert("L"))
|
| 1976 |
|
|
@@ -2080,7 +2565,7 @@ def prepare_figure_artifact(
|
|
| 2080 |
|
| 2081 |
crop_bbox_px = None if removed_procedures else derive_crop_bbox(sanitized_code, analysis_png)
|
| 2082 |
crop_bbox_eps = None
|
| 2083 |
-
if crop_bbox_px:
|
| 2084 |
with Image.open(analysis_png) as analysis_image:
|
| 2085 |
crop_bbox_eps = crop_pixels_to_eps_points(
|
| 2086 |
crop_bbox_px,
|
|
@@ -2118,19 +2603,20 @@ def write_no_text_eps(source_code: str, output_path: Path) -> None:
|
|
| 2118 |
|
| 2119 |
def render_eps_to_png(eps_path: Path, png_path: Path, *, device: str) -> None:
|
| 2120 |
png_path.parent.mkdir(parents=True, exist_ok=True)
|
| 2121 |
-
|
| 2122 |
-
|
| 2123 |
-
|
| 2124 |
-
|
| 2125 |
-
|
| 2126 |
-
|
| 2127 |
-
|
| 2128 |
-
|
| 2129 |
-
|
| 2130 |
-
|
| 2131 |
-
|
| 2132 |
-
|
| 2133 |
-
)
|
|
|
|
| 2134 |
|
| 2135 |
|
| 2136 |
def images_equal_rgba(path_a: Path, path_b: Path) -> bool:
|
|
@@ -2200,13 +2686,23 @@ def render_task(task: dict[str, Any]) -> dict[str, Any]:
|
|
| 2200 |
temp_no_text_eps.parent.mkdir(parents=True, exist_ok=True)
|
| 2201 |
write_no_text_eps(source_code, temp_no_text_eps)
|
| 2202 |
render_eps_to_png(temp_no_text_eps, no_text_png, device=render_device)
|
| 2203 |
-
|
| 2204 |
-
|
| 2205 |
-
|
| 2206 |
-
|
| 2207 |
-
|
| 2208 |
-
|
| 2209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2210 |
overlay_check = overlay_metrics_from_pngs(output_png, no_text_png, word_count)
|
| 2211 |
original_render_check = compare_image_paths(output_png, reference_compare_path if reference_compare_path and reference_compare_path.exists() else None)
|
| 2212 |
render_check = dict(original_render_check)
|
|
@@ -2799,3 +3295,7 @@ def main() -> None:
|
|
| 2799 |
|
| 2800 |
if __name__ == "__main__":
|
| 2801 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
GRAPH_TEXT_FRACTION_MAX = 0.18
|
| 45 |
GRAPH_RENDER_MAE_MAX = 3.0
|
| 46 |
GRAPH_RENDER_PHASH_MAX = 8
|
| 47 |
+
GRAPH_DECORATIVE_MAX = 12
|
| 48 |
+
PIN_INFER_BOUNDARY_MARGIN = 10.0
|
| 49 |
+
PIN_INFER_EXPAND_MARGIN = 12.0
|
| 50 |
+
MAX_INFERRED_COMPONENT_PINS = 12
|
| 51 |
NO_TEXT_PS_OVERRIDE = (
|
| 52 |
"\n"
|
| 53 |
"/show { pop } bind def\n"
|
|
|
|
| 170 |
MIN_CONNECTED_PINS = {
|
| 171 |
"ac_source": 2,
|
| 172 |
"capacitor": 2,
|
| 173 |
+
"current_source": 2,
|
| 174 |
"diode": 2,
|
| 175 |
"fuse": 2,
|
| 176 |
"ground": 1,
|
| 177 |
"integrated_circuit": 2,
|
| 178 |
"inductor": 2,
|
| 179 |
+
"instrument": 2,
|
| 180 |
"junction": 1,
|
| 181 |
"led": 2,
|
| 182 |
"logic_gate": 2,
|
| 183 |
"op_amp": 3,
|
| 184 |
"potentiometer": 3,
|
| 185 |
+
"relay": 2,
|
| 186 |
"resistor": 2,
|
| 187 |
+
"switch": 2,
|
| 188 |
"terminal": 1,
|
| 189 |
+
"transformer": 2,
|
| 190 |
"transistor": 3,
|
| 191 |
+
"vacuum_tube": 2,
|
| 192 |
"voltage_source": 2,
|
| 193 |
}
|
| 194 |
+
DECORATIVE_PROCEDURES = {
|
| 195 |
+
"approx",
|
| 196 |
+
"car2",
|
| 197 |
+
"cart",
|
| 198 |
+
"candle",
|
| 199 |
+
"d_dx",
|
| 200 |
+
"elipse",
|
| 201 |
+
"flywheel",
|
| 202 |
+
"g2h",
|
| 203 |
+
"g2v",
|
| 204 |
+
"greenlight",
|
| 205 |
+
"infinity",
|
| 206 |
+
"jet",
|
| 207 |
+
"kmap_3",
|
| 208 |
+
"kmap_4",
|
| 209 |
+
"left_hand",
|
| 210 |
+
"left_parenthesis",
|
| 211 |
+
"log_grid",
|
| 212 |
+
"omega",
|
| 213 |
+
"pointer",
|
| 214 |
+
"radicand1",
|
| 215 |
+
"radicand2",
|
| 216 |
+
"right_parenthesis",
|
| 217 |
+
"spring",
|
| 218 |
+
"washer",
|
| 219 |
+
"wave",
|
| 220 |
+
"bird",
|
| 221 |
+
"degree",
|
| 222 |
+
"polygon",
|
| 223 |
+
"sadfsaf",
|
| 224 |
+
"signal_air",
|
| 225 |
+
"squarewave",
|
| 226 |
+
"sinewave",
|
| 227 |
+
"sinewave1",
|
| 228 |
+
"sinewave2",
|
| 229 |
+
"sinewave3",
|
| 230 |
+
"sinewave4",
|
| 231 |
+
"tree",
|
| 232 |
+
}
|
| 233 |
+
BLOCK_PROCEDURES = {
|
| 234 |
+
"actuator_junk",
|
| 235 |
+
"analog_input",
|
| 236 |
+
"analog_output",
|
| 237 |
+
"air_compressor",
|
| 238 |
+
"aux_behind",
|
| 239 |
+
"aux_computer_front",
|
| 240 |
+
"aux_computer_behind",
|
| 241 |
+
"aux_front",
|
| 242 |
+
"aux_shared_behind",
|
| 243 |
+
"bar2",
|
| 244 |
+
"box",
|
| 245 |
+
"breadboard_large",
|
| 246 |
+
"blower",
|
| 247 |
+
"body_nut",
|
| 248 |
+
"brick",
|
| 249 |
+
"burner_1",
|
| 250 |
+
"control_selector",
|
| 251 |
+
"controller_pid",
|
| 252 |
+
"dndevice",
|
| 253 |
+
"fault_analysis_box",
|
| 254 |
+
"fcv",
|
| 255 |
+
"field",
|
| 256 |
+
"fieldbus_brick",
|
| 257 |
+
"fieldbus_terminator",
|
| 258 |
+
"flange",
|
| 259 |
+
"keyboard",
|
| 260 |
+
"link_data",
|
| 261 |
+
"link_dcs",
|
| 262 |
+
"main_behind",
|
| 263 |
+
"main_computer_front",
|
| 264 |
+
"main_front",
|
| 265 |
+
"measuring",
|
| 266 |
+
"packing",
|
| 267 |
+
"pid_controller",
|
| 268 |
+
"pipe_elbow",
|
| 269 |
+
"pipe_female",
|
| 270 |
+
"pipe_male",
|
| 271 |
+
"pipe_tee",
|
| 272 |
+
"proportional",
|
| 273 |
+
"remote_seal",
|
| 274 |
+
"signal_electric_binary",
|
| 275 |
+
"spectrum_display",
|
| 276 |
+
"tube_tee",
|
| 277 |
+
"transfer",
|
| 278 |
+
"valve_3",
|
| 279 |
+
"valve_5",
|
| 280 |
+
"valve_6",
|
| 281 |
+
"valve_7",
|
| 282 |
+
"valve_solenoid_2way",
|
| 283 |
+
"valve_control1",
|
| 284 |
+
"valve_control2",
|
| 285 |
+
"valve_globe_air",
|
| 286 |
+
"valve_globe_hand",
|
| 287 |
+
"valve_small",
|
| 288 |
+
"wedge",
|
| 289 |
+
}
|
| 290 |
+
TERMINAL_PROCEDURES = {
|
| 291 |
+
"audioplug",
|
| 292 |
+
"binding_post_black",
|
| 293 |
+
"binding_post_red",
|
| 294 |
+
"bnc_end",
|
| 295 |
+
"cabletip",
|
| 296 |
+
"connect",
|
| 297 |
+
"ladder_start",
|
| 298 |
+
"ladder_start2",
|
| 299 |
+
"probe",
|
| 300 |
+
"term_block_1",
|
| 301 |
+
}
|
| 302 |
+
SOURCE_PROCEDURES = {
|
| 303 |
+
"ac_supply",
|
| 304 |
+
"acsource",
|
| 305 |
+
"acsource2",
|
| 306 |
+
"alternator1",
|
| 307 |
+
"cell",
|
| 308 |
+
"dcgen1",
|
| 309 |
+
"dcgen2",
|
| 310 |
+
"function_generator",
|
| 311 |
+
"pulse_voltage_source",
|
| 312 |
+
"real_batt2",
|
| 313 |
+
"real_batt3",
|
| 314 |
+
"squarewave_source",
|
| 315 |
+
"variable_voltage",
|
| 316 |
+
"voltage_source_ac_dependent",
|
| 317 |
+
}
|
| 318 |
+
CURRENT_SOURCE_PROCEDURES = {"ac_dependent_i_source"}
|
| 319 |
+
SWITCH_PROCEDURES = {
|
| 320 |
+
"dpdt_toggle",
|
| 321 |
+
"nctc",
|
| 322 |
+
"ncto",
|
| 323 |
+
"nc_contact",
|
| 324 |
+
"nc_electronic",
|
| 325 |
+
"nc_flow",
|
| 326 |
+
"nc_pressure",
|
| 327 |
+
"nc_pushbutton",
|
| 328 |
+
"no_electronic",
|
| 329 |
+
"no_flow",
|
| 330 |
+
"no_level",
|
| 331 |
+
"no_limit",
|
| 332 |
+
"no_pressure",
|
| 333 |
+
"no_pushbutton",
|
| 334 |
+
"notc",
|
| 335 |
+
"noto",
|
| 336 |
+
"relay_coil",
|
| 337 |
+
"selector",
|
| 338 |
+
"solenoid",
|
| 339 |
+
"spdt_toggle",
|
| 340 |
+
"switch1",
|
| 341 |
+
"switch_closed",
|
| 342 |
+
"toggle_nc",
|
| 343 |
+
}
|
| 344 |
+
LOGIC_PROCEDURES = {
|
| 345 |
+
"compare_eq",
|
| 346 |
+
"compare_ge",
|
| 347 |
+
"compare_gt",
|
| 348 |
+
"compare_le",
|
| 349 |
+
"compare_lt",
|
| 350 |
+
"compare_ne",
|
| 351 |
+
"counter_down",
|
| 352 |
+
"counter_up",
|
| 353 |
+
"counter_up_down",
|
| 354 |
+
"d_flipflop",
|
| 355 |
+
"d_latch",
|
| 356 |
+
"halfadder",
|
| 357 |
+
"invert2",
|
| 358 |
+
"jk_flipflop",
|
| 359 |
+
"jk_flipflop2",
|
| 360 |
+
"neg_and",
|
| 361 |
+
"neg_or",
|
| 362 |
+
"schmitt",
|
| 363 |
+
"sr_enabled_latch",
|
| 364 |
+
"timer_on_en",
|
| 365 |
+
"tristate_1",
|
| 366 |
+
"xnor",
|
| 367 |
+
}
|
| 368 |
+
INDICATOR_PROCEDURES = {
|
| 369 |
+
"dc_voltmeter",
|
| 370 |
+
"led2",
|
| 371 |
+
"dp_cell",
|
| 372 |
+
"fancy_scope",
|
| 373 |
+
"gauge",
|
| 374 |
+
"headphones",
|
| 375 |
+
"lamp_socket",
|
| 376 |
+
"meter_movement",
|
| 377 |
+
"multimeter",
|
| 378 |
+
"multimeter2",
|
| 379 |
+
"multimeter_tiny",
|
| 380 |
+
"nullmeter",
|
| 381 |
+
"scopemeter",
|
| 382 |
+
"speaker",
|
| 383 |
+
"speaker2",
|
| 384 |
+
"synchro",
|
| 385 |
+
}
|
| 386 |
+
PASSIVE_RESISTIVE_PROCEDURES = {
|
| 387 |
+
"cds_cell",
|
| 388 |
+
"heater_element",
|
| 389 |
+
"load_delta",
|
| 390 |
+
"load_y",
|
| 391 |
+
"resistor_delta",
|
| 392 |
+
}
|
| 393 |
+
PASSIVE_MAGNETIC_PROCEDURES = {
|
| 394 |
+
"coil_out",
|
| 395 |
+
"inductor_large",
|
| 396 |
+
"inductor_medium",
|
| 397 |
+
"saturable_reactor",
|
| 398 |
+
"variac",
|
| 399 |
+
}
|
| 400 |
+
SEMICONDUCTOR_DIODE_PROCEDURES = {
|
| 401 |
+
"diac",
|
| 402 |
+
"lascr",
|
| 403 |
+
"constant_i_diode",
|
| 404 |
+
"diode1",
|
| 405 |
+
"opto_triac",
|
| 406 |
+
"photodiode",
|
| 407 |
+
"shockley_diode",
|
| 408 |
+
"zener_diode",
|
| 409 |
+
}
|
| 410 |
+
SEMICONDUCTOR_TRANSISTOR_PROCEDURES = {
|
| 411 |
+
"gcs",
|
| 412 |
+
"gto",
|
| 413 |
+
"igbt_nchannel",
|
| 414 |
+
"igbt_npn",
|
| 415 |
+
"scr",
|
| 416 |
+
"scs",
|
| 417 |
+
"triac",
|
| 418 |
+
"ujt",
|
| 419 |
+
"bjt_bias",
|
| 420 |
+
"nmos",
|
| 421 |
+
"npnn",
|
| 422 |
+
"pnpp",
|
| 423 |
+
"transistor1",
|
| 424 |
+
"transistor3",
|
| 425 |
+
"transistor4",
|
| 426 |
+
"tr4",
|
| 427 |
+
}
|
| 428 |
+
ACTIVE_TUBE_PROCEDURES = {
|
| 429 |
+
"beam_tetrode",
|
| 430 |
+
"glow_tube",
|
| 431 |
+
"ignitron",
|
| 432 |
+
"phototube",
|
| 433 |
+
"reflex_klystron",
|
| 434 |
+
"tube_halftriode",
|
| 435 |
+
"tube_pentode_1",
|
| 436 |
+
"tube_pentode_2",
|
| 437 |
+
"tube_tetrode",
|
| 438 |
+
"tube_thyratron",
|
| 439 |
+
"tube_triode",
|
| 440 |
+
}
|
| 441 |
STRIP_INVOCATION_PROCEDURES = {"person", "person_shocked"}
|
| 442 |
NON_CIRCUIT_SOURCE_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
|
| 443 |
(
|
|
|
|
| 808 |
creator_match = XCIRCUIT_CREATOR_RE.search(text)
|
| 809 |
creator = normalize_space(creator_match.group(1)) if creator_match else None
|
| 810 |
procedures = sorted(set(EPS_PROC_RE.findall(text)))
|
| 811 |
+
circuit_tokens = sorted(
|
| 812 |
+
procedure
|
| 813 |
+
for procedure in procedures
|
| 814 |
+
if component_family_kind(procedure)[0] in ELECTRICAL_FAMILIES
|
| 815 |
+
)
|
| 816 |
is_xcircuit = bool(creator and "circuit" in creator.lower())
|
| 817 |
return {
|
| 818 |
"creator": creator,
|
|
|
|
| 1162 |
if name in PROCEDURE_KIND_MAP:
|
| 1163 |
return PROCEDURE_KIND_MAP[name]
|
| 1164 |
lower = name.lower()
|
| 1165 |
+
if lower in DECORATIVE_PROCEDURES or lower.startswith("truth_") or lower.startswith("die_"):
|
| 1166 |
+
return "decorative", lower
|
| 1167 |
+
if lower in BLOCK_PROCEDURES:
|
| 1168 |
+
return "block", lower
|
| 1169 |
+
if lower in TERMINAL_PROCEDURES or lower.startswith("binding_post"):
|
| 1170 |
+
return "io", "terminal"
|
| 1171 |
+
if lower in SOURCE_PROCEDURES or lower.startswith("real_batt") or lower.endswith("_source"):
|
| 1172 |
+
return "source", "ac_source" if "ac" in lower else "voltage_source"
|
| 1173 |
+
if lower in CURRENT_SOURCE_PROCEDURES:
|
| 1174 |
+
return "source", "current_source"
|
| 1175 |
+
if lower in SWITCH_PROCEDURES or lower.startswith("no_") or lower.startswith("nc_"):
|
| 1176 |
+
return "switch", "relay" if "relay" in lower else "switch"
|
| 1177 |
+
if lower in LOGIC_PROCEDURES or "flipflop" in lower or "latch" in lower or "adder" in lower:
|
| 1178 |
+
return "logic", "integrated_circuit"
|
| 1179 |
+
if lower in INDICATOR_PROCEDURES or "scope" in lower:
|
| 1180 |
+
return "indicator", "instrument"
|
| 1181 |
+
if lower in PASSIVE_RESISTIVE_PROCEDURES or lower == "strain_gauge":
|
| 1182 |
+
return "passive", "resistor"
|
| 1183 |
+
if lower in PASSIVE_MAGNETIC_PROCEDURES:
|
| 1184 |
+
return "passive", "inductor" if "inductor" in lower or "coil" in lower else "transformer"
|
| 1185 |
+
if lower in SEMICONDUCTOR_DIODE_PROCEDURES:
|
| 1186 |
+
return "semiconductor", "diode"
|
| 1187 |
+
if lower in SEMICONDUCTOR_TRANSISTOR_PROCEDURES:
|
| 1188 |
+
return "semiconductor", "transistor"
|
| 1189 |
+
if lower in ACTIVE_TUBE_PROCEDURES or (lower.startswith("tube_") and lower != "tube_tee"):
|
| 1190 |
+
return "active", "vacuum_tube"
|
| 1191 |
+
if lower in {"plc_powersupply"} or "powersupply" in lower:
|
| 1192 |
+
return "source", "voltage_source"
|
| 1193 |
+
if lower in {"plc_processor"} or "processor" in lower:
|
| 1194 |
+
return "logic", "integrated_circuit"
|
| 1195 |
if lower in {"clip", "label", "marks"} or lower.startswith("die_") or lower.startswith("truth_"):
|
| 1196 |
return "decorative", lower
|
| 1197 |
if "arrow" in lower:
|
|
|
|
| 1200 |
return "active", "op_amp"
|
| 1201 |
if name.endswith("_gate") or "gate" in lower or lower in {"nor", "buffer"} or "flipflop" in lower or lower.startswith("dip_"):
|
| 1202 |
return "logic", "logic_gate"
|
| 1203 |
+
if "flipflop" in lower or "latch" in lower or lower in {"xnor", "xor", "xnor", "schmitt"}:
|
| 1204 |
+
return "logic", "integrated_circuit"
|
| 1205 |
if "resistor" in lower or lower == "strain_gauge":
|
| 1206 |
return "passive", "resistor"
|
| 1207 |
if "capacitor" in lower or "polarized" in lower:
|
|
|
|
| 1214 |
return "source", "voltage_source"
|
| 1215 |
if "isource" in lower or "current" in lower:
|
| 1216 |
return "source", "current_source"
|
| 1217 |
+
if "source" in lower:
|
| 1218 |
+
return "source", "ac_source" if "ac" in lower else "voltage_source"
|
| 1219 |
if "switch" in lower or "toggle" in lower or "pushbutton" in lower or "contact" in lower:
|
| 1220 |
return "switch", "switch"
|
| 1221 |
+
if (
|
| 1222 |
+
"diode" in lower
|
| 1223 |
+
or "mosfet" in lower
|
| 1224 |
+
or "jfet" in lower
|
| 1225 |
+
or lower in {"scr", "diode", "triac", "diac", "gto", "gcs", "scs", "ujt"}
|
| 1226 |
+
or "fet" in lower
|
| 1227 |
+
or lower.startswith("npn")
|
| 1228 |
+
or lower.startswith("pnp")
|
| 1229 |
+
or lower.startswith("transistor")
|
| 1230 |
+
or lower == "nmos"
|
| 1231 |
+
):
|
| 1232 |
+
transistor_like = (
|
| 1233 |
+
"fet" in lower
|
| 1234 |
+
or lower in {"scr", "triac", "gto", "gcs", "scs", "ujt", "nmos"}
|
| 1235 |
+
or lower.startswith("npn")
|
| 1236 |
+
or lower.startswith("pnp")
|
| 1237 |
+
or lower.startswith("transistor")
|
| 1238 |
+
)
|
| 1239 |
+
return "semiconductor", "transistor" if transistor_like else "diode"
|
| 1240 |
+
if "lamp" in lower or "meter" in lower or "motor" in lower or "detector" in lower or "speaker" in lower:
|
| 1241 |
return "indicator", "indicator"
|
| 1242 |
if "connector" in lower or "terminal" in lower or lower in {"stud", "tap", "powerpole"}:
|
| 1243 |
return "io", "terminal"
|
|
|
|
| 1255 |
return "terminal"
|
| 1256 |
if family in {"active", "passive", "source", "semiconductor", "switch", "indicator", "protection", "logic"}:
|
| 1257 |
return "electrical"
|
| 1258 |
+
if family in {"block", "decorative"}:
|
| 1259 |
return "decorative"
|
| 1260 |
if family == "unknown":
|
| 1261 |
return "unknown_bad"
|
|
|
|
| 1336 |
return left - margin <= x <= right + margin and bottom - margin <= y <= top + margin
|
| 1337 |
|
| 1338 |
|
| 1339 |
+
def distance_to_bbox_boundary(
|
| 1340 |
+
point: tuple[float, float],
|
| 1341 |
+
bbox: tuple[float, float, float, float],
|
| 1342 |
+
) -> float:
|
| 1343 |
+
x, y = point
|
| 1344 |
+
left, bottom, right, top = bbox
|
| 1345 |
+
if left <= x <= right and bottom <= y <= top:
|
| 1346 |
+
return min(x - left, right - x, y - bottom, top - y)
|
| 1347 |
+
dx = 0.0 if left <= x <= right else min(abs(x - left), abs(x - right))
|
| 1348 |
+
dy = 0.0 if bottom <= y <= top else min(abs(y - bottom), abs(y - top))
|
| 1349 |
+
if dx == 0.0:
|
| 1350 |
+
return dy
|
| 1351 |
+
if dy == 0.0:
|
| 1352 |
+
return dx
|
| 1353 |
+
return math.hypot(dx, dy)
|
| 1354 |
+
|
| 1355 |
+
|
| 1356 |
+
def bbox_segments(
|
| 1357 |
+
bbox: tuple[float, float, float, float]
|
| 1358 |
+
) -> list[tuple[tuple[float, float], tuple[float, float]]]:
|
| 1359 |
+
left, bottom, right, top = bbox
|
| 1360 |
+
return [
|
| 1361 |
+
((left, bottom), (right, bottom)),
|
| 1362 |
+
((right, bottom), (right, top)),
|
| 1363 |
+
((right, top), (left, top)),
|
| 1364 |
+
((left, top), (left, bottom)),
|
| 1365 |
+
]
|
| 1366 |
+
|
| 1367 |
+
|
| 1368 |
def snap_key(point: tuple[float, float]) -> tuple[int, int]:
|
| 1369 |
return (round(point[0] / COORD_SNAP), round(point[1] / COORD_SNAP))
|
| 1370 |
|
| 1371 |
|
| 1372 |
+
def dedupe_points(points: list[tuple[float, float]]) -> list[tuple[float, float]]:
|
| 1373 |
+
unique: dict[tuple[int, int], tuple[float, float]] = {}
|
| 1374 |
+
for point in points:
|
| 1375 |
+
key = snap_key(point)
|
| 1376 |
+
unique.setdefault(key, (round(point[0], 3), round(point[1], 3)))
|
| 1377 |
+
return list(unique.values())
|
| 1378 |
+
|
| 1379 |
+
|
| 1380 |
def _projection_fraction(
|
| 1381 |
point: tuple[float, float],
|
| 1382 |
start: tuple[float, float],
|
|
|
|
| 1446 |
return sorted(points, key=lambda point: _projection_fraction(point, start, end))
|
| 1447 |
|
| 1448 |
|
| 1449 |
+
def fallback_bbox_pin_points(component: dict[str, Any]) -> list[tuple[float, float]]:
|
| 1450 |
+
bbox_values = component.get("bbox")
|
| 1451 |
+
if not bbox_values:
|
| 1452 |
+
origin = component.get("point")
|
| 1453 |
+
if origin:
|
| 1454 |
+
return [(float(origin["x"]), float(origin["y"]))]
|
| 1455 |
+
return []
|
| 1456 |
+
|
| 1457 |
+
bbox = tuple(float(value) for value in bbox_values)
|
| 1458 |
+
left, bottom, right, top = bbox
|
| 1459 |
+
center_x = (left + right) / 2.0
|
| 1460 |
+
center_y = (bottom + top) / 2.0
|
| 1461 |
+
family = component.get("family", "unknown")
|
| 1462 |
+
expected = min_connected_pins(component)
|
| 1463 |
+
|
| 1464 |
+
if family in {"ground", "io", "junction"} or expected <= 1:
|
| 1465 |
+
return [(center_x, center_y)]
|
| 1466 |
+
if expected != 2:
|
| 1467 |
+
return []
|
| 1468 |
+
if (right - left) >= (top - bottom):
|
| 1469 |
+
return [(left, center_y), (right, center_y)]
|
| 1470 |
+
return [(center_x, bottom), (center_x, top)]
|
| 1471 |
+
|
| 1472 |
+
|
| 1473 |
+
def infer_missing_component_pins(
|
| 1474 |
+
components: list[dict[str, Any]],
|
| 1475 |
+
pins: list[dict[str, Any]],
|
| 1476 |
+
wire_polylines: list[dict[str, Any]],
|
| 1477 |
+
*,
|
| 1478 |
+
start_index: int,
|
| 1479 |
+
) -> tuple[list[dict[str, Any]], int, list[str]]:
|
| 1480 |
+
pins_by_component: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
| 1481 |
+
for pin in pins:
|
| 1482 |
+
pins_by_component[pin["component_id"]].append(pin)
|
| 1483 |
+
|
| 1484 |
+
conductive_segments: list[tuple[tuple[float, float], tuple[float, float]]] = []
|
| 1485 |
+
for wire in wire_polylines:
|
| 1486 |
+
if not wire.get("conductive"):
|
| 1487 |
+
continue
|
| 1488 |
+
points = [(float(point["x"]), float(point["y"])) for point in wire["points"]]
|
| 1489 |
+
for start, end in zip(points, points[1:]):
|
| 1490 |
+
conductive_segments.append((start, end))
|
| 1491 |
+
|
| 1492 |
+
inferred_pins: list[dict[str, Any]] = []
|
| 1493 |
+
missing_warnings: list[str] = []
|
| 1494 |
+
pin_index = start_index
|
| 1495 |
+
|
| 1496 |
+
for component in components:
|
| 1497 |
+
if pins_by_component.get(component["id"]):
|
| 1498 |
+
continue
|
| 1499 |
+
family = component.get("family", "unknown")
|
| 1500 |
+
if family in {"decorative", "block"}:
|
| 1501 |
+
continue
|
| 1502 |
+
|
| 1503 |
+
bbox_values = component.get("bbox")
|
| 1504 |
+
candidate_points: list[tuple[float, float]] = []
|
| 1505 |
+
if bbox_values:
|
| 1506 |
+
bbox = tuple(float(value) for value in bbox_values)
|
| 1507 |
+
for start, end in conductive_segments:
|
| 1508 |
+
seg_bbox = (
|
| 1509 |
+
min(start[0], end[0]),
|
| 1510 |
+
min(start[1], end[1]),
|
| 1511 |
+
max(start[0], end[0]),
|
| 1512 |
+
max(start[1], end[1]),
|
| 1513 |
+
)
|
| 1514 |
+
if not bbox_intersects(bbox, seg_bbox, margin=PIN_INFER_EXPAND_MARGIN):
|
| 1515 |
+
continue
|
| 1516 |
+
for point in (start, end):
|
| 1517 |
+
if point_inside_bbox(point, bbox, margin=PIN_INFER_EXPAND_MARGIN) and distance_to_bbox_boundary(point, bbox) <= PIN_INFER_BOUNDARY_MARGIN:
|
| 1518 |
+
candidate_points.append(point)
|
| 1519 |
+
for edge_start, edge_end in bbox_segments(bbox):
|
| 1520 |
+
intersection = segment_intersection(start, end, edge_start, edge_end, tolerance=PIN_INFER_EXPAND_MARGIN)
|
| 1521 |
+
if intersection is not None:
|
| 1522 |
+
candidate_points.append(intersection)
|
| 1523 |
+
|
| 1524 |
+
candidate_points = sorted(
|
| 1525 |
+
dedupe_points(candidate_points),
|
| 1526 |
+
key=lambda point: (round(point[0], 3), round(point[1], 3)),
|
| 1527 |
+
)
|
| 1528 |
+
if not candidate_points:
|
| 1529 |
+
candidate_points = fallback_bbox_pin_points(component)
|
| 1530 |
+
if not candidate_points:
|
| 1531 |
+
missing_warnings.append(f"missing_pins:{component['raw_kind']}:{component['id']}")
|
| 1532 |
+
continue
|
| 1533 |
+
|
| 1534 |
+
for pin_position, point in enumerate(candidate_points[:MAX_INFERRED_COMPONENT_PINS], start=1):
|
| 1535 |
+
pin_id = f"{component['id']}.pin_{pin_index:04d}"
|
| 1536 |
+
pin_index += 1
|
| 1537 |
+
inferred_pins.append(
|
| 1538 |
+
{
|
| 1539 |
+
"id": pin_id,
|
| 1540 |
+
"component_id": component["id"],
|
| 1541 |
+
"name": f"inferred_{pin_position}",
|
| 1542 |
+
"kind": "terminal",
|
| 1543 |
+
"point": point_payload(*point),
|
| 1544 |
+
"point_ref": point_to_ref(*point),
|
| 1545 |
+
"raw": "inferred_from_bbox_wire",
|
| 1546 |
+
}
|
| 1547 |
+
)
|
| 1548 |
+
|
| 1549 |
+
return inferred_pins, pin_index, missing_warnings
|
| 1550 |
+
|
| 1551 |
+
|
| 1552 |
def count_words(text: str) -> int:
|
| 1553 |
return len(re.findall(r"[A-Za-z0-9_]+", text))
|
| 1554 |
|
|
|
|
| 1749 |
)
|
| 1750 |
|
| 1751 |
local_pins = proc_pin_catalog.get(proc_name, [])
|
| 1752 |
+
if not local_pins and family in {"passive", "source", "indicator", "protection", "switch"} and local_bbox:
|
| 1753 |
bx, by, bw, bh = local_bbox
|
| 1754 |
if abs(bh) >= abs(bw):
|
| 1755 |
local_pins = [
|
|
|
|
| 1766 |
{"label": kind, "kind": "terminal", "x": 0.0, "y": 0.0, "raw": "inferred"}
|
| 1767 |
]
|
| 1768 |
|
|
|
|
|
|
|
|
|
|
| 1769 |
for pin_position, pin_info in enumerate(local_pins, start=1):
|
| 1770 |
global_point = transform_component_point(
|
| 1771 |
float(pin_info["x"]),
|
|
|
|
| 1875 |
}
|
| 1876 |
)
|
| 1877 |
|
| 1878 |
+
inferred_pins, pin_index, missing_pin_warnings = infer_missing_component_pins(
|
| 1879 |
+
components,
|
| 1880 |
+
pins,
|
| 1881 |
+
wire_polylines,
|
| 1882 |
+
start_index=pin_index,
|
| 1883 |
+
)
|
| 1884 |
+
pins.extend(inferred_pins)
|
| 1885 |
+
warnings_list.extend(missing_pin_warnings)
|
| 1886 |
wire_polylines.extend(synthesize_component_spans(components, pins))
|
| 1887 |
|
| 1888 |
node_points: dict[tuple[int, int], tuple[float, float]] = {}
|
|
|
|
| 2165 |
or int(original_render.get("phash_distance", 999)) <= GRAPH_RENDER_PHASH_MAX
|
| 2166 |
)
|
| 2167 |
|
| 2168 |
+
unknown_component_count = int(
|
| 2169 |
+
component_class_counts.get("unknown_bad", 0) + component_class_counts.get("other_bad", 0)
|
| 2170 |
+
)
|
| 2171 |
+
decorative_component_count = int(component_class_counts.get("decorative", 0))
|
| 2172 |
+
severe_underconnected = len(underconnected_components) >= max(
|
| 2173 |
+
2,
|
| 2174 |
+
math.ceil(len(active_component_ids) * 0.5),
|
| 2175 |
+
)
|
| 2176 |
+
symbol_plate_like = bool(
|
| 2177 |
+
active_component_ids
|
| 2178 |
+
and one_pin_active_net_count >= max(2, len(active_component_ids))
|
| 2179 |
+
and electrical_connected_components >= max(1, len(active_component_ids) // 2)
|
| 2180 |
+
)
|
| 2181 |
+
heavily_fragmented = electrical_connected_components >= max(
|
| 2182 |
+
3,
|
| 2183 |
+
math.ceil(len(active_component_ids) * 0.5),
|
| 2184 |
+
)
|
| 2185 |
+
|
| 2186 |
graph_core_reasons: list[str] = []
|
| 2187 |
if row.get("parse_status") == "failed":
|
| 2188 |
graph_core_reasons.append("parse_not_ok")
|
| 2189 |
if len(active_component_ids) == 0:
|
| 2190 |
graph_core_reasons.append("electrical_component_count_zero")
|
| 2191 |
+
if severe_underconnected and not symbol_plate_like:
|
|
|
|
|
|
|
| 2192 |
graph_core_reasons.append("electrical_component_underconnected")
|
| 2193 |
+
if heavily_fragmented and severe_underconnected and not symbol_plate_like:
|
| 2194 |
+
graph_core_reasons.append("electrical_graph_fragmented")
|
| 2195 |
+
if unknown_component_count > 0:
|
| 2196 |
graph_core_reasons.append("unrecognized_component_present")
|
|
|
|
|
|
|
| 2197 |
if overlapping_active_components:
|
| 2198 |
graph_core_reasons.append("active_component_span_overlap")
|
| 2199 |
|
| 2200 |
pretty_reasons = list(graph_core_reasons)
|
| 2201 |
+
if electrical_connected_components > 1:
|
| 2202 |
+
pretty_reasons.append("electrical_graph_components_gt_1")
|
| 2203 |
+
if underconnected_components and "electrical_component_underconnected" not in pretty_reasons:
|
| 2204 |
+
pretty_reasons.append("electrical_component_underconnected")
|
| 2205 |
+
if one_pin_active_net_count > 0:
|
| 2206 |
+
pretty_reasons.append("one_pin_active_net_present")
|
| 2207 |
+
if decorative_component_count > GRAPH_DECORATIVE_MAX:
|
| 2208 |
+
pretty_reasons.append("decorative_component_count_gt_12")
|
| 2209 |
if int(overlay.get("word_count", 0)) > GRAPH_WORD_COUNT_MAX:
|
| 2210 |
pretty_reasons.append("word_count_gt_50")
|
| 2211 |
if float(overlay.get("text_mask_fraction", 0.0)) > GRAPH_TEXT_FRACTION_MAX:
|
|
|
|
| 2235 |
"overlapping_active_component_group_count": len(overlapping_active_components),
|
| 2236 |
"one_pin_active_net_count": one_pin_active_net_count,
|
| 2237 |
"one_pin_terminal_only_net_count": one_pin_terminal_only_net_count,
|
| 2238 |
+
"unknown_component_count": unknown_component_count,
|
| 2239 |
+
"decorative_component_count": decorative_component_count,
|
| 2240 |
+
"severe_underconnected": severe_underconnected,
|
| 2241 |
+
"heavily_fragmented": heavily_fragmented,
|
| 2242 |
+
"symbol_plate_like": symbol_plate_like,
|
| 2243 |
"component_class_counts": dict(component_class_counts),
|
| 2244 |
"word_count": int(overlay.get("word_count", 0)),
|
| 2245 |
"text_mask_fraction": float(overlay.get("text_mask_fraction", 0.0)),
|
|
|
|
| 2454 |
source_code: str,
|
| 2455 |
analysis_png_path: Path,
|
| 2456 |
) -> tuple[int, int, int, int] | None:
|
| 2457 |
+
if not analysis_png_path.exists():
|
| 2458 |
+
return None
|
| 2459 |
with Image.open(analysis_png_path) as image:
|
| 2460 |
gray = np.array(image.convert("L"))
|
| 2461 |
|
|
|
|
| 2565 |
|
| 2566 |
crop_bbox_px = None if removed_procedures else derive_crop_bbox(sanitized_code, analysis_png)
|
| 2567 |
crop_bbox_eps = None
|
| 2568 |
+
if crop_bbox_px and analysis_png.exists():
|
| 2569 |
with Image.open(analysis_png) as analysis_image:
|
| 2570 |
crop_bbox_eps = crop_pixels_to_eps_points(
|
| 2571 |
crop_bbox_px,
|
|
|
|
| 2603 |
|
| 2604 |
def render_eps_to_png(eps_path: Path, png_path: Path, *, device: str) -> None:
|
| 2605 |
png_path.parent.mkdir(parents=True, exist_ok=True)
|
| 2606 |
+
cmd = [
|
| 2607 |
+
"gs",
|
| 2608 |
+
"-dSAFER",
|
| 2609 |
+
"-dBATCH",
|
| 2610 |
+
"-dNOPAUSE",
|
| 2611 |
+
"-dEPSCrop",
|
| 2612 |
+
f"-sDEVICE={device}",
|
| 2613 |
+
f"-r{PNG_DPI}",
|
| 2614 |
+
f"-sOutputFile={png_path}",
|
| 2615 |
+
str(eps_path),
|
| 2616 |
+
]
|
| 2617 |
+
run(cmd)
|
| 2618 |
+
if not png_path.exists():
|
| 2619 |
+
run(cmd)
|
| 2620 |
|
| 2621 |
|
| 2622 |
def images_equal_rgba(path_a: Path, path_b: Path) -> bool:
|
|
|
|
| 2686 |
temp_no_text_eps.parent.mkdir(parents=True, exist_ok=True)
|
| 2687 |
write_no_text_eps(source_code, temp_no_text_eps)
|
| 2688 |
render_eps_to_png(temp_no_text_eps, no_text_png, device=render_device)
|
| 2689 |
+
if not no_text_png.exists() and output_png.exists():
|
| 2690 |
+
shutil.copy2(output_png, no_text_png)
|
| 2691 |
+
no_text_status = "fallback_original_copy"
|
| 2692 |
+
no_label_render = {
|
| 2693 |
+
"status": "fallback_original_copy",
|
| 2694 |
+
"compiler": "ghostscript",
|
| 2695 |
+
"word_count": word_count,
|
| 2696 |
+
"label_count": label_count,
|
| 2697 |
+
}
|
| 2698 |
+
else:
|
| 2699 |
+
no_text_status = "rendered"
|
| 2700 |
+
no_label_render = {
|
| 2701 |
+
"status": "ok",
|
| 2702 |
+
"compiler": "ghostscript",
|
| 2703 |
+
"word_count": word_count,
|
| 2704 |
+
"label_count": label_count,
|
| 2705 |
+
}
|
| 2706 |
overlay_check = overlay_metrics_from_pngs(output_png, no_text_png, word_count)
|
| 2707 |
original_render_check = compare_image_paths(output_png, reference_compare_path if reference_compare_path and reference_compare_path.exists() else None)
|
| 2708 |
render_check = dict(original_render_check)
|
|
|
|
| 3295 |
|
| 3296 |
if __name__ == "__main__":
|
| 3297 |
main()
|
| 3298 |
+
"block",
|
| 3299 |
+
"butterfly",
|
| 3300 |
+
"crt_elec",
|
| 3301 |
+
"displacer",
|