-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_relation_graph.py
More file actions
208 lines (188 loc) · 10 KB
/
Copy pathrun_relation_graph.py
File metadata and controls
208 lines (188 loc) · 10 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Neo4j-style interactive visualization of the Phase-2 supply-chain graph.
Reads the pilot edges (reports/relations_pilot_edges.csv) and emits a self-contained,
draggable / zoomable force-directed HTML graph (vis-network from CDN):
* nodes = companies, colored by kind (analyzed filer / tradeable universe counterparty /
external foreign-private), sized by degree (hubs like AAPL pop out)
* edges = relationships, colored by type (customer/supplier/partner/competitor), directed,
with hover tooltips showing the % concentration + the verbatim evidence quote
* a checkbox toggles the 313 noisy competitor edges so the dependency graph stands out
Output: reports/relations_graph.html (open in a browser)
Run: py -3 run_relation_graph.py
"""
import re
import json
import math
import pandas as pd
from pathlib import Path
CSV = Path("reports/relations_pilot_edges.csv")
OUT = Path("reports/relations_graph.html")
REL_COLOR = {"customer": "#0f9d6e", "supplier": "#2563eb", "partner": "#8b5cf6", "competitor": "#d1d5db"}
KIND_COLOR = {"filer": "#6366f1", "universe": "#10b981", "external": "#94a3b8"}
KIND_LABEL = {"filer": "已分析公司(filer)", "universe": "可交易对手方(universe)", "external": "外资/私企(不可交易)"}
_NONAL = re.compile(r"[^a-z0-9]+")
def norm(s: str) -> str:
return _NONAL.sub(" ", (s or "").lower()).strip()
def build():
df = pd.read_csv(CSV).fillna({"dst": "", "name": "", "relation": "", "evidence": "", "pct": 0})
src_set = set(df["src"])
nodes, edges = {}, []
def node(nid, label, kind):
if nid not in nodes:
nodes[nid] = {"id": nid, "label": label, "kind": kind, "deg": 0}
# filer kind wins over universe/external if a counterparty is also an analyzed filer
if kind == "filer":
nodes[nid]["kind"] = "filer"
nodes[nid]["deg"] += 1
for _, r in df.iterrows():
rel = r["relation"]
if rel not in REL_COLOR:
continue
sid = r["src"]
if r["in_universe"] and r["dst"]:
did, dlabel = r["dst"], r["dst"]
dkind = "filer" if r["dst"] in src_set else "universe"
else:
did, dlabel = "ext::" + norm(r["name"]), (r["name"] or "?")[:24]
dkind = "external"
if not norm(did):
continue
node(sid, sid, "filer")
node(did, dlabel, dkind)
pct = f" {int(r['pct'])}%" if r["pct"] else ""
ev = (str(r["evidence"]) or "").replace('"', "'")[:140]
edges.append({
"from": sid, "to": did, "relation": rel,
"color": {"color": REL_COLOR[rel], "opacity": 0.35 if rel == "competitor" else 0.9},
"width": 1 if rel == "competitor" else 2.5,
"dashes": rel == "competitor",
"arrows": "to",
"title": f"{sid} —{rel}{pct}→ {dlabel}\n{ev}",
})
vnodes = []
for n in nodes.values():
size = 12 + math.sqrt(n["deg"]) * 7
vnodes.append({
"id": n["id"], "label": n["label"], "value": n["deg"],
"color": {"background": KIND_COLOR[n["kind"]], "border": "#0f172a"},
"size": size, "font": {"color": "#0f172a", "size": 14, "face": "Segoe UI, Arial"},
"shape": "dot", "kind": n["kind"],
"title": f"{n['label']} · {KIND_LABEL[n['kind']]} · 连接 {n['deg']}",
})
n_uni = sum(1 for n in nodes.values() if n["kind"] in ("filer", "universe"))
rel_counts = df[df["relation"].isin(REL_COLOR)]["relation"].value_counts().to_dict()
stats = (f"{len(vnodes)} 节点 · {len(edges)} 关系边 · "
f"可交易节点 {n_uni}/{len(vnodes)} · "
+ " ".join(f"{k}:{v}" for k, v in rel_counts.items()))
return vnodes, edges, stats
HTML = """<!doctype html><html lang="zh"><head><meta charset="utf-8">
<title>Phase-2 供应链关系图</title>
<script src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<style>
html,body{{margin:0;height:100%;font-family:'Segoe UI',Arial,sans-serif;background:#0b1220;color:#e5e7eb}}
#bar{{padding:10px 16px;background:#111827;border-bottom:1px solid #1f2937}}
#bar h1{{margin:0;font-size:17px}} #bar .s{{color:#9ca3af;font-size:12px;margin-top:3px}}
#legend{{margin-top:8px;font-size:12px;display:flex;gap:14px;flex-wrap:wrap;align-items:center}}
.chip{{display:inline-flex;align-items:center;gap:5px}}
.dot{{width:11px;height:11px;border-radius:50%;display:inline-block;border:1px solid #0f172a}}
.ln{{width:18px;height:0;border-top:3px solid;display:inline-block}}
#net{{width:100%;height:calc(100% - 96px)}}
label{{cursor:pointer;user-select:none}}
</style></head><body>
<div id="bar">
<h1>Phase-2 供应链 / 业务关系图 <span style="color:#9ca3af;font-weight:400;font-size:13px">(10-K 抽取 · 试点 52 家)</span></h1>
<div class="s">{stats} · 拖动节点 / 滚轮缩放 / 悬停看证据引文</div>
<div id="legend">
<span class="chip"><span class="dot" style="background:#6366f1"></span>已分析公司</span>
<span class="chip"><span class="dot" style="background:#10b981"></span>可交易对手方</span>
<span class="chip"><span class="dot" style="background:#94a3b8"></span>外资/私企(不可交易)</span>
<span style="color:#374151">|</span>
<span class="chip"><span class="ln" style="border-color:#0f9d6e"></span>客户</span>
<span class="chip"><span class="ln" style="border-color:#2563eb"></span>供应商</span>
<span class="chip"><span class="ln" style="border-color:#8b5cf6"></span>合作</span>
<span class="chip"><span class="ln" style="border-color:#9ca3af;border-top-style:dashed"></span>竞争对手</span>
<span style="color:#374151">|</span>
<label><input type="checkbox" id="togComp" checked> 显示竞争对手边</label>
</div>
</div>
<div id="net"></div>
<script>
const ALL = {edges};
const nodes = new vis.DataSet({nodes});
const edges = new vis.DataSet(ALL);
const net = new vis.Network(document.getElementById('net'), {{nodes, edges}}, {{
physics:{{solver:'barnesHut', barnesHut:{{gravitationalConstant:-9000, springLength:120, springConstant:0.03, avoidOverlap:0.2}}, stabilization:{{iterations:220}}}},
interaction:{{hover:true, tooltipDelay:80, navigationButtons:false}},
nodes:{{scaling:{{min:10,max:46}}}},
edges:{{smooth:{{type:'continuous'}}}}
}});
document.getElementById('togComp').addEventListener('change', e=>{{
edges.clear();
edges.add(e.target.checked ? ALL : ALL.filter(x=>x.relation!=='competitor'));
}});
</script></body></html>"""
def static_png():
"""Readable static preview of the DEPENDENCY subgraph (customer/supplier/partner;
the 313 competitor edges are omitted — they hairball a static layout)."""
try:
import networkx as nx
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import font_manager
zh = "C:/Windows/Fonts/simhei.ttf"
if Path(zh).exists():
font_manager.fontManager.addfont(zh); plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
except Exception as e:
print("static png skipped:", e); return
df = pd.read_csv(CSV).fillna({"dst": "", "name": "", "relation": ""})
# readable core: the TRADEABLE (in-universe) dependency edges only — externals/competitors
# are kept in the interactive HTML but omitted here so the structure is legible.
dep = df[(df["relation"].isin(["customer", "supplier", "partner"])) & (df["in_universe"]) & (df["dst"] != "")]
src_set = set(df["src"])
G = nx.DiGraph()
for _, r in dep.iterrows():
s, d = r["src"], r["dst"]
dk = "filer" if d in src_set else "universe"
G.add_node(s, kind="filer")
if d not in G:
G.add_node(d, kind=dk)
G.add_edge(s, d, relation=r["relation"])
if G.number_of_edges() == 0:
return
deg = dict(G.degree())
pos = nx.spring_layout(G, k=1.6, seed=42, iterations=300)
fig, ax = plt.subplots(figsize=(15, 11))
for rel, col in [("customer", "#0f9d6e"), ("supplier", "#2563eb"), ("partner", "#8b5cf6")]:
el = [(u, v) for u, v, d in G.edges(data=True) if d["relation"] == rel]
nx.draw_networkx_edges(G, pos, edgelist=el, edge_color=col, width=2.0, alpha=0.8,
arrows=True, arrowsize=13, connectionstyle="arc3,rad=0.06", ax=ax)
for kind, col in KIND_COLOR.items():
nl = [n for n in G if G.nodes[n].get("kind") == kind]
nx.draw_networkx_nodes(G, pos, nodelist=nl, node_color=col, node_size=[160 + deg[n] * 140 for n in nl],
edgecolors="#0f172a", linewidths=1.0, ax=ax)
labels = {n: n for n in G if deg[n] >= 2 or G.nodes[n].get("kind") in ("filer", "universe")}
nx.draw_networkx_labels(G, pos, labels=labels, font_size=9, font_weight="bold", ax=ax)
import matplotlib.patches as mp
leg = [mp.Patch(color=KIND_COLOR["filer"], label="已分析公司(filer)"),
mp.Patch(color=KIND_COLOR["universe"], label="可交易对手方(枢纽)"),
mp.Patch(color="#0f9d6e", label="客户边 (filer→买家)"), mp.Patch(color="#2563eb", label="供应商边"),
mp.Patch(color="#8b5cf6", label="合作边")]
ax.legend(handles=leg, loc="upper left", fontsize=10, frameon=True)
ax.set_title("Phase-2 可交易供应链核心(仅 in-universe 客户/供应商边 ≈20 条;外资/竞争边见交互 HTML)",
fontsize=14, fontweight="bold")
ax.axis("off")
fig.savefig("reports/relations_graph.png", dpi=130, bbox_inches="tight", facecolor="white")
plt.close(fig)
print("static png -> reports/relations_graph.png")
def main():
vnodes, edges, stats = build()
html = HTML.format(nodes=json.dumps(vnodes, ensure_ascii=False),
edges=json.dumps(edges, ensure_ascii=False), stats=stats)
OUT.write_text(html, encoding="utf-8")
n_uni = sum(1 for n in vnodes if n["kind"] in ("filer", "universe"))
print(f"graph -> {OUT}")
print(f" nodes={len(vnodes)} edges={len(edges)} tradeable_nodes={n_uni}")
static_png()
if __name__ == "__main__":
main()