Added multilayer hypergraph - #75
Open
922397935 wants to merge 2 commits into
Open
Conversation
yifanfeng97
requested changes
Aug 2, 2026
yifanfeng97
left a comment
Member
There was a problem hiding this comment.
感谢你的贡献!多层超图是社区非常需要的功能(#69, #71 都在讨论),非常欢迎这个 PR。但在合入前,有几个问题需要修复:
🔴 Bug — 需要修复
1. node_sum_before 列表累积污染
self.node_sum_before 在 construct_multi_layer_hypergraph()、_inter_layer_connect()、node_layer_list() 三个方法中都被 append。如果对同一个实例调用多个方法,列表会不断增长,导致偏移量计算错误。
建议:不要把 node_sum_before 作为可变实例状态,改为每次计算时使用局部变量:
def _compute_offsets(self):
layer_num_v = [layer.num_v for layer in self.layers_list]
return [sum(layer_num_v[:i]) for i in range(self.num_layers)]2. construct_multi_layer_hypergraph 返回新的 MultilayerHypergraph
返回的新实例本身又需要调用 construct_multi_layer_hypergraph(),形成递归调用。建议:直接在当前实例上构建,或返回一个普通的 Hypergraph。
3. temp 列表推导式逻辑可能有误
temp = [self.node_layer[i][j] for i in range(num_layers) for j in range(num_layers) if i == j]这只取了对角线元素 [node_layer[0][0], node_layer[1][1], ...]。如果意图是获取每层选中的节点列表,请确认逻辑是否正确。
4. load_interlayer_mapping 的 save 逻辑
在 isinstance(path, list) 分支中,连续两次 open(save_path, 'w') —— 第二次会覆盖第一次的内容。此外 gene_info 只在首次遇到 gene 时赋值,后续不更新。
5. 测试文件无法通过
test_construct_multilayer_hypergraph把 fixtureg1/g2当函数调用(g1())→ 会抛TypeError,fixture 不需要加括号test_draw引用了不存在的 fixturemhgtest_save和test_adjacency_matrix测的是普通Hypergraph,与多层超图无关,疑似从其他测试文件复制
🟡 代码质量 — 建议改进
| # | 问题 | 建议 |
|---|---|---|
| 6 | import matplotlib.pyplot as plt |
核心结构类不应依赖 matplotlib,移除或改为延迟导入 |
| 7 | from tqdm import tqdm |
同上,print + tqdm 应改用 logging 或去掉 |
| 8 | docstring 混用中英文 | 项目约定用英文(Google/NumPy style) |
| 9 | from dhg import Hypergraph |
有循环导入风险,改为 from .hypergraph import Hypergraph |
| 10 | 文件末尾缺换行符 | 加一个 trailing newline |
| 11 | select_nodes 类型标注 num_v: int |
实际接收 range,应改为正确的类型标注 |
| 12 | notebook from data import MultilayerHypergraph |
导入路径不对,应为 from dhg.structure import MultilayerHypergraph |
| 13 | notebook 含 870KB+ base64 图片 | 提交前请清除所有 cell output(jupyter nbconvert --clear-output) |
整体来说功能方向很好,期待修改后的版本。如果需要帮助实现其中某些部分,欢迎讨论。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
我简单写了一个多层超图应用的notebook,不知道是否不太规范,如果您觉得写的还行,我可以帮忙再加点儿文档的使用说明