-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumb_3_d_plot.py
More file actions
217 lines (176 loc) · 7.11 KB
/
umb_3_d_plot.py
File metadata and controls
217 lines (176 loc) · 7.11 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
209
210
211
212
213
214
215
216
217
"""
UMB 3‑D visualisation – Sapphire (v20 — final polish)
====================================================
* **Verbose flag truly works** – every major step prints when `--verbose` is
present.
* **Solid node circles** (opaque pastel) with thin black edge.
* **Label centred in each circle.**
* **Connector line‑width ∝ edge weight** (range 1 – 4 px).
* **Guaranteed `main()` completeness** – script runs end‑to‑end.
"""
from __future__ import annotations
import argparse, json, math, random, re, sys
from collections import Counter, OrderedDict
from pathlib import Path
from typing import Dict, List, Tuple
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms.community import girvan_newman
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 – activate 3‑D backend
# ---------------- constants ----------------
MEMORY_FILE = Path("./memory/emergence_UMB.json")
TOP_K = 15
SEED = 42
DEPTH_MULT = 4.0
MARGIN_X = 1.0
DATA_ROT_DEG = 25
EDGE_THRESH_START = 0.35
EDGE_THRESH_STEP = 0.05
EDGE_THRESH_MAX = 0.85
STOPWORDS = {
"a","an","and","are","as","at","be","but","by","for","from","has","have","he","her","his","i","in","is","it","its","me","my","of","on","or","our","s","she","that","the","their","them","they","this","to","was","we","were","what","with","you","your","am","do","who","so"
}
random.seed(SEED)
# ---------------- helpers ----------------
def _text(m: Dict[str, str]) -> str:
return f"{m.get('input','')} {m.get('output','')}".lower()
def dedup(mem: List[dict], v=False):
uniq = OrderedDict()
for m in mem:
uniq.setdefault(_text(m), m)
if v:
print(f"[dedup] {len(mem)} → {len(uniq)} unique memories")
return list(uniq.values())
def top_keywords(mem: List[dict], v=False) -> List[str]:
cnt = Counter()
for m in mem:
cnt.update([t for t in re.findall(r"[a-zA-Z']{2,}", _text(m)) if t not in STOPWORDS])
top = cnt.most_common(TOP_K)
if v:
print("[keywords]")
for w, c in top:
print(f" {w:>15} : {c}")
return [w for w, _ in top]
def build_graph(words: List[str], mem: List[dict]):
contain: Dict[str, set] = {w: set() for w in words}
for idx, m in enumerate(mem):
txt = _text(m)
for w in words:
if re.search(fr"\b{re.escape(w)}\b", txt):
contain[w].add(idx)
G = nx.Graph()
for w in words:
G.add_node(w, salience=len(contain[w]))
for i, wi in enumerate(words):
for wj in words[i+1:]:
w = len(contain[wi] & contain[wj])
if w:
G.add_edge(wi, wj, weight=w)
return G
# ------------- clustering -------------
def communities(G):
return list(nx.algorithms.community.greedy_modularity_communities(G, weight="weight"))
def find_threshold(words, mem, min_clusters, v=False):
th = EDGE_THRESH_START
while th <= EDGE_THRESH_MAX:
G_full = build_graph(words, mem)
G = nx.Graph()
for u, v, d in G_full.edges(data=True):
if d["weight"] >= th:
G.add_edge(u, v, weight=d["weight"])
G.add_nodes_from(G_full.nodes(data=True))
comms = communities(G)
if v:
print(f"[threshold] {th:.2f} → {len(comms)} clusters")
if len(comms) >= min_clusters:
return th, G, comms
th += EDGE_THRESH_STEP
return th, G, comms
def split_gn(G_sub):
part_a, part_b = next(girvan_newman(G_sub))
return [list(part_a), list(part_b)]
def recursive_split(G_full, initial_comms, target, v=False):
comms = [set(c) for c in initial_comms]
while len(comms) < target:
largest = max(comms, key=len)
if len(largest) <= 2:
if v:
print("[split] cannot split further (≤2)")
break
split = split_gn(G_full.subgraph(largest))
if v:
sizes = [len(s) for s in split]
print(f"[split] {len(largest)} → {sizes[0]} | {sizes[1]}")
comms.remove(largest)
comms.extend([set(s) for s in split])
comm_lists = [list(c) for c in comms]
comm_lists.sort(key=len, reverse=True)
return comm_lists[:target]
# ------------- layout -------------
def cluster_radius(n):
return max(0.6, 1.3*math.sqrt(n))
def layout(clusters, v=False):
clusters_sorted = sorted(clusters, key=len)
radii = [cluster_radius(len(c)) for c in clusters_sorted]
step = DEPTH_MULT * max(radii) if radii else 1
pos = {}
for i, (c, r) in enumerate(zip(clusters_sorted, radii)):
x = MARGIN_X + i*step
for k, node in enumerate(c):
ang = 2*math.pi*k/len(c)
rho = r*math.sqrt(random.random())
y,z = rho*math.cos(ang), rho*math.sin(ang)
pos[node]=(x,y,z)
if v:
sample = list(pos.items())[:5]
print("[layout] sample positions:", sample)
cx = MARGIN_X + step*(len(clusters_sorted)-1)
return pos, cx
def rotate_y(pos, deg):
if deg == 0: return
t = math.radians(deg); c,s = math.cos(t), math.sin(t)
for k, (x,y,z) in pos.items():
pos[k]=(x, z*s + y*c, z*c - y*s)
# ------------- plot -------------
def plot(G, pos, cx, verbose=False):
xs,ys,zs = zip(*pos.values())
y_lim = max(map(abs,ys))*1.2 or 1
z_lim = max(map(abs,zs))*1.2 or 1
fig = plt.figure(figsize=(8,6))
ax: Axes3D = fig.add_subplot(111, projection='3d')
ax.dist = 1
ax.set_axis_off();
ax.set_xlim(0,cx); ax.set_ylim(-y_lim,y_lim); ax.set_zlim(-z_lim,z_lim)
ax.plot([0, cx], [0, 0], [0, 0], color='black', linewidth=1) # X
ax.plot([0, 0], [-y_lim, y_lim], [0, 0], color='black', linewidth=1) # Y
ax.plot([0, 0], [0, 0], [-z_lim, z_lim], color='black', linewidth=1) # Z
w_max = max((d['weight'] for _,_,d in G.edges(data=True)), default=1.0)
for u,v,d in G.edges(data=True):
lw = 6 * d['weight'] / (w_max*3)
(xu,yu,zu),(xv,yv,zv)=pos[u],pos[v]
ax.plot([xu, xv], [yu, yv], [zu, zv], color='gray', alpha=0.8, linewidth=lw)
sal_vals=[G.nodes[n]['salience'] for n in G.nodes]
lo,hi=min(sal_vals),max(sal_vals)
sizes = [60 + (s - lo) / (hi - lo + 1e-9) * 680 for s in sal_vals]
ax.scatter(xs, ys, zs, s=sizes, c='lightskyblue', edgecolors='black', linewidths=0.4, alpha=1.0)
for n,(x,y,z) in pos.items():
ax.text(x,y,z,n,fontsize=5,ha='center',va='center')
plt.tight_layout(); plt.savefig('umb_3d_view.png',dpi=400)
if verbose: print('[plot] saved umb_3d_view.png')
# ------------- main -------------
def main():
ap=argparse.ArgumentParser()
ap.add_argument('--target_clusters',type=int,default=3)
ap.add_argument('--datarot',type=float,default=DATA_ROT_DEG)
ap.add_argument('--verbose',action='store_true')
ap.add_argument('memory',nargs='?',default=str(MEMORY_FILE))
args=ap.parse_args(); v=args.verbose
mem = dedup(json.loads(Path(args.memory).read_text()), v)
words= top_keywords(mem, v)
_, G, initial = find_threshold(words, mem, 1, v)
clusters = recursive_split(G, initial, args.target_clusters, v)
pos,cx = layout(clusters, v)
rotate_y(pos,args.datarot)
plot(G,pos,cx,v)
if __name__=='__main__':
main()