Upload change_lineage.py
Browse files- change_lineage.py +117 -0
change_lineage.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def convert_lineage_to_split_genealogy(
|
| 8 |
+
lineage_json_path: str,
|
| 9 |
+
save_pkl_path: str,
|
| 10 |
+
max_children: int = 8, # merge后每个粗点平均对应4个细点,留余量
|
| 11 |
+
):
|
| 12 |
+
"""
|
| 13 |
+
正确的转换方向:
|
| 14 |
+
|
| 15 |
+
lineage["L1"][i] = [a, b, c, d]
|
| 16 |
+
含义:L1第i个粗点 由 L0中的a,b,c,d细点 merge而来
|
| 17 |
+
反转:L1第i个粗点 → 子节点是 L0中的 a,b,c,d
|
| 18 |
+
|
| 19 |
+
genealogy[1]['children_ids'] shape (N_L1粗, max_children)
|
| 20 |
+
含义:L1第i个粗点,在L0中对应的细节点索引
|
| 21 |
+
|
| 22 |
+
使用时:
|
| 23 |
+
父节点 = L3/L2/L1 的粗粒度高斯点
|
| 24 |
+
子节点 = 更细粒度的高斯点(向原始方向展开)
|
| 25 |
+
序列:L3(粗)→L2(细)→L1(更细)→L0(原始)
|
| 26 |
+
"""
|
| 27 |
+
print(f"[convert] 加载 {lineage_json_path}")
|
| 28 |
+
with open(lineage_json_path, 'r') as f:
|
| 29 |
+
lineage = json.load(f)
|
| 30 |
+
|
| 31 |
+
genealogy = {}
|
| 32 |
+
|
| 33 |
+
# 注意方向:粗→细,所以 key 含义变了
|
| 34 |
+
# genealogy[1]: L1粗节点 → L0细节点
|
| 35 |
+
# genealogy[2]: L2粗节点 → L1细节点
|
| 36 |
+
# genealogy[3]: L3粗节点 → L2细节点
|
| 37 |
+
level_map = {
|
| 38 |
+
"L1": 1,
|
| 39 |
+
"L2": 2,
|
| 40 |
+
"L3": 3,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
for level_name, gen_key in level_map.items():
|
| 44 |
+
if level_name not in lineage:
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
# merge_list[粗点索引] = [细点索引, ...]
|
| 48 |
+
merge_list = lineage[level_name]
|
| 49 |
+
n_coarse = len(merge_list)
|
| 50 |
+
|
| 51 |
+
# 统计每个粗点有多少个细点
|
| 52 |
+
child_counts = [len(parents) for parents in merge_list]
|
| 53 |
+
max_actual = max(child_counts)
|
| 54 |
+
avg_actual = np.mean(child_counts)
|
| 55 |
+
print(f"[convert] {level_name}(粗→细方向):")
|
| 56 |
+
print(f" 粗节点数={n_coarse}")
|
| 57 |
+
print(f" 每个粗节点平均对应细节点数={avg_actual:.2f}")
|
| 58 |
+
print(f" 最多子节点数={max_actual}")
|
| 59 |
+
|
| 60 |
+
# 直接用 merge_list 构建 children_ids
|
| 61 |
+
# merge_list[i] 就是 L(k-1)_粗节点i 对应的所有 L(k-2)_细节点索引
|
| 62 |
+
actual_max = min(max_actual, max_children)
|
| 63 |
+
children_ids = np.full((n_coarse, actual_max), fill_value=-1, dtype=np.int32)
|
| 64 |
+
|
| 65 |
+
truncated = 0
|
| 66 |
+
for coarse_idx, fine_indices in enumerate(merge_list):
|
| 67 |
+
n = min(len(fine_indices), actual_max)
|
| 68 |
+
children_ids[coarse_idx, :n] = fine_indices[:n]
|
| 69 |
+
if len(fine_indices) > actual_max:
|
| 70 |
+
truncated += 1
|
| 71 |
+
|
| 72 |
+
if truncated:
|
| 73 |
+
print(f" ⚠️ {truncated} 个粗节点子节点数超过{actual_max}被截断")
|
| 74 |
+
|
| 75 |
+
genealogy[gen_key] = {
|
| 76 |
+
'children_ids': children_ids, # (N_coarse, max_children)
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
with open(save_pkl_path, 'wb') as f:
|
| 80 |
+
pickle.dump(genealogy, f, protocol=4)
|
| 81 |
+
|
| 82 |
+
size_mb = os.path.getsize(save_pkl_path) / 1024 / 1024
|
| 83 |
+
print(f"\n[convert] 已保存 → {save_pkl_path} ({size_mb:.2f} MB)")
|
| 84 |
+
return genealogy
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def verify(lineage_json_path, genealogy_pkl_path, n_sample=5):
|
| 88 |
+
with open(lineage_json_path, 'r') as f:
|
| 89 |
+
lineage = json.load(f)
|
| 90 |
+
with open(genealogy_pkl_path, 'rb') as f:
|
| 91 |
+
genealogy = pickle.load(f)
|
| 92 |
+
|
| 93 |
+
print("\n[verify] 抽查:粗节点的子节点应与 lineage 一致")
|
| 94 |
+
for level_name, gen_key in [("L1", 1), ("L2", 2), ("L3", 3)]:
|
| 95 |
+
if level_name not in lineage or gen_key not in genealogy:
|
| 96 |
+
continue
|
| 97 |
+
merge_list = lineage[level_name]
|
| 98 |
+
children_ids = genealogy[gen_key]['children_ids']
|
| 99 |
+
sample_idx = np.random.choice(len(merge_list), min(n_sample, len(merge_list)), replace=False)
|
| 100 |
+
errors = 0
|
| 101 |
+
for i in sample_idx:
|
| 102 |
+
expected = set(merge_list[i])
|
| 103 |
+
actual = set(int(x) for x in children_ids[i] if x >= 0)
|
| 104 |
+
if not expected.issubset(actual):
|
| 105 |
+
print(f" ❌ {level_name}[{i}]: expected={expected}, actual={actual}")
|
| 106 |
+
errors += 1
|
| 107 |
+
if errors == 0:
|
| 108 |
+
print(f" ✅ {level_name}:{len(sample_idx)} 个粗节点验证通过")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == '__main__':
|
| 112 |
+
genealogy = convert_lineage_to_split_genealogy(
|
| 113 |
+
lineage_json_path="outputs/lineage.json",
|
| 114 |
+
save_pkl_path="outputs/genealogy.pkl",
|
| 115 |
+
max_children=4,
|
| 116 |
+
)
|
| 117 |
+
verify("outputs/lineage.json", "outputs/genealogy.pkl", n_sample=10)
|