File size: 4,015 Bytes
6169243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
from collections import Counter
from pathlib import Path
from typing import Any


GENERIC_DATATYPES = {"text", "image", "json", "csv", "report_table"}
TRANSITION_TYPES = {"follows", "typed_flow", "adjacent_stage"}
ANNOTATION_TYPES = {"consumes", "produces", "implements", "implements_operation", "belongs_to_stage"}


def load_json(path: Path) -> Any:
    return json.loads(path.read_text(encoding="utf-8"))


def load_graph(graph_dir: Path) -> tuple[list[dict], list[dict]]:
    return load_json(graph_dir / "graph_nodes.json"), load_json(graph_dir / "graph_edges.json")


def generic_only_transition(edge: dict) -> bool:
    if edge.get("type") not in {"follows", "typed_flow"}:
        return False
    shared = set(edge.get("shared_datatypes") or [])
    return bool(shared) and shared <= GENERIC_DATATYPES


def summarize(graph_dir: Path) -> dict:
    nodes, edges = load_graph(graph_dir)
    edge_type_counts = Counter(edge.get("type") for edge in edges)
    transition_edges = [edge for edge in edges if edge.get("type") in TRANSITION_TYPES]
    generic_only = [edge for edge in edges if generic_only_transition(edge)]
    annotated_edges = [edge for edge in edges if edge.get("type") in ANNOTATION_TYPES]
    with_evidence = [edge for edge in annotated_edges if edge.get("annotation_evidence")]
    with_confidence = [edge for edge in annotated_edges if "annotation_confidence" in edge]
    unknown_operation_edges = [
        edge
        for edge in edges
        if edge.get("type") == "implements_operation" and str(edge.get("target", "")).endswith(":unknown")
    ]
    soft_prior_nodes = [node for node in nodes if node.get("soft_priors")]
    soft_prior_count = sum(len(node.get("soft_priors") or []) for node in soft_prior_nodes)
    proxy_noise_edges = len(generic_only) + edge_type_counts.get("adjacent_stage", 0)
    return {
        "graph_dir": str(graph_dir),
        "node_count": len(nodes),
        "edge_count": len(edges),
        "edge_type_counts": dict(edge_type_counts),
        "transition_edge_count": len(transition_edges),
        "generic_only_transition_count": len(generic_only),
        "adjacent_stage_hard_edge_count": edge_type_counts.get("adjacent_stage", 0),
        "soft_adjacent_stage_prior_count": soft_prior_count,
        "annotation_edges": len(annotated_edges),
        "annotation_edges_with_evidence": len(with_evidence),
        "annotation_edges_with_confidence": len(with_confidence),
        "unknown_operation_edges": len(unknown_operation_edges),
        "proxy_structural_noise_count": proxy_noise_edges,
        "proxy_structural_noise_rate": round(proxy_noise_edges / max(1, len(transition_edges)), 6),
    }


def compare(old_graph: Path, new_graph: Path) -> dict:
    old = summarize(old_graph)
    new = summarize(new_graph)
    keys = [
        "edge_count",
        "transition_edge_count",
        "generic_only_transition_count",
        "adjacent_stage_hard_edge_count",
        "soft_adjacent_stage_prior_count",
        "annotation_edges_with_evidence",
        "annotation_edges_with_confidence",
        "proxy_structural_noise_count",
        "proxy_structural_noise_rate",
    ]
    delta = {key: new.get(key, 0) - old.get(key, 0) for key in keys}
    return {"old": old, "new": new, "delta_new_minus_old": delta}


def main() -> None:
    parser = argparse.ArgumentParser(description="Validate clean typed graph reconstruction.")
    parser.add_argument("--old-graph", type=Path, required=True)
    parser.add_argument("--new-graph", type=Path, required=True)
    parser.add_argument("--out", type=Path, default=None)
    args = parser.parse_args()

    report = compare(args.old_graph, args.new_graph)
    text = json.dumps(report, indent=2, sort_keys=True)
    if args.out:
        args.out.parent.mkdir(parents=True, exist_ok=True)
        args.out.write_text(text + "\n", encoding="utf-8")
    print(text)


if __name__ == "__main__":
    main()