0xZohar commited on
Commit
764fa39
·
verified ·
1 Parent(s): a3d60c3

Add code/cube3d/training/dat_freq_sort.py

Browse files
code/cube3d/training/dat_freq_sort.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ from collections import defaultdict
4
+
5
+ def count_dat_usage(folder_path):
6
+ """
7
+ 统计文件夹中所有LDR文件里DAT文件的使用频率
8
+
9
+ 参数:
10
+ folder_path: LDR文件所在的文件夹路径
11
+
12
+ 返回:
13
+ 按使用频率排序的DAT文件字典列表
14
+ """
15
+ # 用于存储每个DAT文件的使用次数
16
+ dat_counts = defaultdict(int)
17
+
18
+ # 正则表达式模式,用于匹配LDR文件中的DAT引用
19
+ # LDR格式中,零件引用通常类似: 1 0 0 0 1 0 0 0 1 0 part.dat
20
+ dat_pattern = re.compile(r'\b(\w+\.dat)\b', re.IGNORECASE)
21
+
22
+ # 遍历文件夹中的所有文件
23
+ for root, dirs, files in os.walk(folder_path):
24
+ for file in files:
25
+ # 只处理LDR文件
26
+ if file.lower().endswith('.ldr'):
27
+ file_path = os.path.join(root, file)
28
+
29
+ try:
30
+ # 打开并读取LDR文件
31
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
32
+ line_number = 0
33
+ for line in f:
34
+ line_number += 1
35
+ # 跳过注释行和空行(LDR中以0开头的通常是注释或元数据)
36
+ stripped_line = line.strip()
37
+ if not stripped_line or stripped_line.startswith('0 '):
38
+ continue
39
+
40
+ # 在有效行中查找DAT文件引用
41
+ matches = dat_pattern.findall(line)
42
+ for dat_file in matches:
43
+ # 统一转为小写,避免大小写导致的重复计数
44
+ dat_file_lower = dat_file.lower()
45
+ dat_counts[dat_file_lower] += 1
46
+
47
+ except Exception as e:
48
+ print(f"处理文件 {file_path} 时出错: {str(e)}")
49
+
50
+ # 按使用频率从高到低排序
51
+ sorted_dats = sorted(dat_counts.items(), key=lambda x: x[1], reverse=True)
52
+
53
+ return sorted_dats
54
+
55
+ def main():
56
+ import sys
57
+
58
+ # # 获取用户输入的文件夹路径
59
+ # if len(sys.argv) > 1:
60
+ # folder_path = sys.argv[1]
61
+ # else:
62
+ # folder_path = input("请输入LDR文件所在的文件夹路径: ")
63
+
64
+ folder_path = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom_flip"
65
+
66
+ # 验证文件夹路径是否有效
67
+ if not os.path.isdir(folder_path):
68
+ print(f"错误: {folder_path} 不是有效的文件夹路径")
69
+ return
70
+
71
+ print(f"正在统计 {folder_path} 中LDR文件的DAT使用频率...")
72
+ dat_usage = count_dat_usage(folder_path)
73
+
74
+ print("\nDAT文件使用频率统计结果(按使用次数排序):")
75
+ print("==========================================")
76
+ for i, (dat_file, count) in enumerate(dat_usage, 1):
77
+ print(f"{i}. {dat_file}: {count} 次")
78
+
79
+ # 也可以将结果保存到文件
80
+ output_file = "dat_usage统计结果.txt"
81
+ with open(output_file, 'w', encoding='utf-8') as f:
82
+ f.write("DAT文件使用频率统计结果(按使用次数排序):\n")
83
+ f.write("==========================================\n")
84
+ for i, (dat_file, count) in enumerate(dat_usage, 1):
85
+ f.write(f"{i}. {dat_file}: {count} 次\n")
86
+
87
+ print(f"\n统计结果已保存到 {output_file}")
88
+
89
+ if __name__ == "__main__":
90
+ main()