You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python ripser获取1维持续性图对应点云顶点的实现方法

你需要在调用ripser时开启cocycle记录参数,即可关联1维持久图点与对应的原始点云顶点,具体实现如下:

代码修改步骤

1. 开启ripser的cocycle返回

将原有代码中ripser的调用行替换为以下内容,开启同调类生成元记录:

res = ripser.ripser(P, do_cocycles=True)
diagrams = res["dgms"]
# cocycles[dim] 对应对应维度的同调类生成元,与持久图点顺序一一对应
cocycles = res["cocycles"]

2. 筛选存活期最长的2个1维持久点

dgm1 = diagrams[1]
# 计算每个1维点的存活期(死亡值-出生值)
lifetimes = dgm1[:, 1] - dgm1[:, 0]
# 按存活期降序排序,取前2个的索引
top2_idx = np.argsort(lifetimes)[::-1][:2]

3. 提取对应原始点云顶点索引

top2_vertices = []
for idx in top2_idx:
    # 取出对应同调类的cocycle,提取所有出现过的顶点索引并去重
    cocycle = cocycles[1][idx]
    verts = np.unique(cocycle[:, :2].flatten())
    top2_vertices.append(verts)

# 输出结果
for i, verts in enumerate(top2_vertices):
    print(f"第{i+1}长存活期的1维同调类对应顶点数:{len(verts)}")
    print(f"对应点坐标示例:\n{P[verts[:5]]}\n")

可选:可视化验证

你可以直接高亮这两个顶点集合,验证是否对应两个环形点云:

plt.figure(figsize=(10, 5))
plt.scatter(P[:, 0], P[:, 1], c="lightgray", s=10, label="背景点")
plt.scatter(P[top2_vertices[0], 0], P[top2_vertices[0], 1], c="red", s=15, label="左环顶点")
plt.scatter(P[top2_vertices[1], 0], P[top2_vertices[1], 1], c="blue", s=15, label="右环顶点")
plt.legend()
plt.axis("equal")
plt.show()

内容的提问来源于stack exchange,提问作者adam.hendry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 02:45:04