Skip to content

Commit 665c459

Browse files
committed
改进了可视化脚本
1
1 parent cb5145c commit 665c459

File tree

3 files changed

+23
-71
lines changed

3 files changed

+23
-71
lines changed

run_memory_vis.bat

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ if errorlevel 1 (
88
exit /b 1
99
)
1010
echo Conda 环境 "%CONDA_ENV%" 激活成功
11-
python src/plugins/memory_system/memory_manual_build.py
11+
12+
set /p OPTION="请选择运行选项 (1: 运行全部绘制, 2: 运行简单绘制): "
13+
if "%OPTION%"=="1" (
14+
python src/plugins/memory_system/memory_manual_build.py
15+
) else if "%OPTION%"=="2" (
16+
python src/plugins/memory_system/draw_memory.py
17+
) else (
18+
echo 无效的选项
19+
pause
20+
exit /b 1
21+
)
22+
1223
if errorlevel 1 (
1324
echo 命令执行失败,错误代码 %errorlevel%
1425
pause

src/plugins/memory_system/draw_memory.py

+10-69
Original file line numberDiff line numberDiff line change
@@ -201,67 +201,6 @@ def topic_what(text, topic):
201201
prompt = f'这是一段文字:{text}。我想知道这记忆里有什么关于{topic}的话题,帮我总结成一句自然的话,可以包含时间和人物。只输出这句话就好'
202202
return prompt
203203

204-
def visualize_graph(memory_graph: Memory_graph, color_by_memory: bool = False):
205-
# 设置中文字体
206-
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
207-
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
208-
209-
G = memory_graph.G
210-
211-
# 保存图到本地
212-
nx.write_gml(G, "memory_graph.gml") # 保存为 GML 格式
213-
214-
# 根据连接条数或记忆数量设置节点颜色
215-
node_colors = []
216-
nodes = list(G.nodes()) # 获取图中实际的节点列表
217-
218-
if color_by_memory:
219-
# 计算每个节点的记忆数量
220-
memory_counts = []
221-
for node in nodes:
222-
memory_items = G.nodes[node].get('memory_items', [])
223-
if isinstance(memory_items, list):
224-
count = len(memory_items)
225-
else:
226-
count = 1 if memory_items else 0
227-
memory_counts.append(count)
228-
max_memories = max(memory_counts) if memory_counts else 1
229-
230-
for count in memory_counts:
231-
# 使用不同的颜色方案:红色表示记忆多,蓝色表示记忆少
232-
if max_memories > 0:
233-
intensity = min(1.0, count / max_memories)
234-
color = (intensity, 0, 1.0 - intensity) # 从蓝色渐变到红色
235-
else:
236-
color = (0, 0, 1) # 如果没有记忆,则为蓝色
237-
node_colors.append(color)
238-
else:
239-
# 使用原来的连接数量着色方案
240-
max_degree = max(G.degree(), key=lambda x: x[1])[1] if G.degree() else 1
241-
for node in nodes:
242-
degree = G.degree(node)
243-
if max_degree > 0:
244-
red = min(1.0, degree / max_degree)
245-
blue = 1.0 - red
246-
color = (red, 0, blue)
247-
else:
248-
color = (0, 0, 1)
249-
node_colors.append(color)
250-
251-
# 绘制图形
252-
plt.figure(figsize=(12, 8))
253-
pos = nx.spring_layout(G, k=1, iterations=50)
254-
nx.draw(G, pos,
255-
with_labels=True,
256-
node_color=node_colors,
257-
node_size=200,
258-
font_size=10,
259-
font_family='SimHei',
260-
font_weight='bold')
261-
262-
title = '记忆图谱可视化 - ' + ('按记忆数量着色' if color_by_memory else '按连接数量着色')
263-
plt.title(title, fontsize=16, fontfamily='SimHei')
264-
plt.show()
265204

266205

267206
def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = False):
@@ -280,7 +219,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
280219
memory_items = H.nodes[node].get('memory_items', [])
281220
memory_count = len(memory_items) if isinstance(memory_items, list) else (1 if memory_items else 0)
282221
degree = H.degree(node)
283-
if memory_count < 5 or degree < 2: # 改为小于2而不是小于等于2
222+
if memory_count < 3 or degree < 2: # 改为小于2而不是小于等于2
284223
nodes_to_remove.append(node)
285224

286225
H.remove_nodes_from(nodes_to_remove)
@@ -291,7 +230,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
291230
return
292231

293232
# 保存图到本地
294-
nx.write_gml(H, "memory_graph.gml") # 保存为 GML 格式
233+
# nx.write_gml(H, "memory_graph.gml") # 保存为 GML 格式
295234

296235
# 计算节点大小和颜色
297236
node_colors = []
@@ -315,21 +254,23 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
315254
memory_count = len(memory_items) if isinstance(memory_items, list) else (1 if memory_items else 0)
316255
# 使用指数函数使变化更明显
317256
ratio = memory_count / max_memories
318-
size = 500 + 5000 * (ratio ** 2) # 使用平方函数使差异更明显
257+
size = 500 + 5000 * (ratio ) # 使用1.5次方函数使差异不那么明显
319258
node_sizes.append(size)
320259

321260
# 计算节点颜色(基于连接数)
322261
degree = H.degree(node)
323262
# 红色分量随着度数增加而增加
324-
red = min(1.0, degree / max_degree)
263+
r = (degree / max_degree) ** 0.3
264+
red = min(1.0, r)
325265
# 蓝色分量随着度数减少而增加
326-
blue = 1.0 - red
327-
color = (red, 0, blue)
266+
blue = max(0.0, 1 - red)
267+
# blue = 1
268+
color = (red, 0.1, blue)
328269
node_colors.append(color)
329270

330271
# 绘制图形
331272
plt.figure(figsize=(12, 8))
332-
pos = nx.spring_layout(H, k=1.5, iterations=50) # 增加k值使节点分布更开
273+
pos = nx.spring_layout(H, k=1, iterations=50) # 增加k值使节点分布更开
333274
nx.draw(H, pos,
334275
with_labels=True,
335276
node_color=node_colors,
@@ -339,7 +280,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
339280
font_weight='bold',
340281
edge_color='gray',
341282
width=0.5,
342-
alpha=0.7)
283+
alpha=0.9)
343284

344285
title = '记忆图谱可视化 - 节点大小表示记忆数量,颜色表示连接数'
345286
plt.title(title, fontsize=16, fontfamily='SimHei')

src/plugins/memory_system/memory_manual_build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ async def main():
944944
db = Database.get_instance()
945945
start_time = time.time()
946946

947-
test_pare = {'do_build_memory':True,'do_forget_topic':False,'do_visualize_graph':True,'do_query':False,'do_merge_memory':False}
947+
test_pare = {'do_build_memory':False,'do_forget_topic':False,'do_visualize_graph':True,'do_query':False,'do_merge_memory':False}
948948

949949
# 创建记忆图
950950
memory_graph = Memory_graph()

0 commit comments

Comments
 (0)