| import json
|
| import pickle
|
| import numpy as np
|
| import os
|
|
|
|
|
| def convert_lineage_to_split_genealogy(
|
| lineage_json_path: str,
|
| save_pkl_path: str,
|
| max_children: int = 8,
|
| ):
|
| """
|
| 正确的转换方向:
|
|
|
| lineage["L1"][i] = [a, b, c, d]
|
| 含义:L1第i个粗点 由 L0中的a,b,c,d细点 merge而来
|
| 反转:L1第i个粗点 → 子节点是 L0中的 a,b,c,d
|
|
|
| genealogy[1]['children_ids'] shape (N_L1粗, max_children)
|
| 含义:L1第i个粗点,在L0中对应的细节点索引
|
|
|
| 使用时:
|
| 父节点 = L3/L2/L1 的粗粒度高斯点
|
| 子节点 = 更细粒度的高斯点(向原始方向展开)
|
| 序列:L3(粗)→L2(细)→L1(更细)→L0(原始)
|
| """
|
| print(f"[convert] 加载 {lineage_json_path}")
|
| with open(lineage_json_path, 'r') as f:
|
| lineage = json.load(f)
|
|
|
| genealogy = {}
|
|
|
|
|
|
|
|
|
|
|
| level_map = {
|
| "L1": 1,
|
| "L2": 2,
|
| "L3": 3,
|
| }
|
|
|
| for level_name, gen_key in level_map.items():
|
| if level_name not in lineage:
|
| continue
|
|
|
|
|
| merge_list = lineage[level_name]
|
| n_coarse = len(merge_list)
|
|
|
|
|
| child_counts = [len(parents) for parents in merge_list]
|
| max_actual = max(child_counts)
|
| avg_actual = np.mean(child_counts)
|
| print(f"[convert] {level_name}(粗→细方向):")
|
| print(f" 粗节点数={n_coarse}")
|
| print(f" 每个粗节点平均对应细节点数={avg_actual:.2f}")
|
| print(f" 最多子节点数={max_actual}")
|
|
|
|
|
|
|
| actual_max = min(max_actual, max_children)
|
| children_ids = np.full((n_coarse, actual_max), fill_value=-1, dtype=np.int32)
|
|
|
| truncated = 0
|
| for coarse_idx, fine_indices in enumerate(merge_list):
|
| n = min(len(fine_indices), actual_max)
|
| children_ids[coarse_idx, :n] = fine_indices[:n]
|
| if len(fine_indices) > actual_max:
|
| truncated += 1
|
|
|
| if truncated:
|
| print(f" ⚠️ {truncated} 个粗节点子节点数超过{actual_max}被截断")
|
|
|
| genealogy[gen_key] = {
|
| 'children_ids': children_ids,
|
| }
|
|
|
| with open(save_pkl_path, 'wb') as f:
|
| pickle.dump(genealogy, f, protocol=4)
|
|
|
| size_mb = os.path.getsize(save_pkl_path) / 1024 / 1024
|
| print(f"\n[convert] 已保存 → {save_pkl_path} ({size_mb:.2f} MB)")
|
| return genealogy
|
|
|
|
|
| def verify(lineage_json_path, genealogy_pkl_path, n_sample=5):
|
| with open(lineage_json_path, 'r') as f:
|
| lineage = json.load(f)
|
| with open(genealogy_pkl_path, 'rb') as f:
|
| genealogy = pickle.load(f)
|
|
|
| print("\n[verify] 抽查:粗节点的子节点应与 lineage 一致")
|
| for level_name, gen_key in [("L1", 1), ("L2", 2), ("L3", 3)]:
|
| if level_name not in lineage or gen_key not in genealogy:
|
| continue
|
| merge_list = lineage[level_name]
|
| children_ids = genealogy[gen_key]['children_ids']
|
| sample_idx = np.random.choice(len(merge_list), min(n_sample, len(merge_list)), replace=False)
|
| errors = 0
|
| for i in sample_idx:
|
| expected = set(merge_list[i])
|
| actual = set(int(x) for x in children_ids[i] if x >= 0)
|
| if not expected.issubset(actual):
|
| print(f" ❌ {level_name}[{i}]: expected={expected}, actual={actual}")
|
| errors += 1
|
| if errors == 0:
|
| print(f" ✅ {level_name}:{len(sample_idx)} 个粗节点验证通过")
|
|
|
|
|
| if __name__ == '__main__':
|
| genealogy = convert_lineage_to_split_genealogy(
|
| lineage_json_path="outputs/lineage.json",
|
| save_pkl_path="outputs/genealogy.pkl",
|
| max_children=4,
|
| )
|
| verify("outputs/lineage.json", "outputs/genealogy.pkl", n_sample=10) |