0xZohar commited on
Commit
6011f4e
·
verified ·
1 Parent(s): b90e24e

Add code/cube3d/training/count_ldr_main_section.py

Browse files
code/cube3d/training/count_ldr_main_section.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ def checkEncoding(filepath):
4
+ with open(filepath, "rb") as encode_check:
5
+ encoding = encode_check.readline(3)
6
+ if encoding == b"\xfe\xff\x00":
7
+ return "utf_16_be"
8
+ elif encoding == b"\xff\xfe0":
9
+ return "utf_16_le"
10
+ else:
11
+ return "utf_8"
12
+
13
+ def readTextFile(filepath):
14
+ if os.path.exists(filepath):
15
+ file_encoding = checkEncoding(filepath)
16
+ try:
17
+ with open(filepath, "rt", encoding=file_encoding) as f_in:
18
+ return f_in.readlines()
19
+ except:
20
+ with open(filepath, "rt", encoding="latin_1") as f_in:
21
+ return f_in.readlines()
22
+ return None
23
+
24
+ def count_valid_lines(filepath):
25
+ """
26
+ 统计LDR文件main_section中以"1 "开头的有效行数
27
+ 返回值: 有效行数,如果文件不存在或读取失败则返回-1
28
+ """
29
+ if not os.path.isfile(filepath):
30
+ return -1
31
+
32
+ lines = readTextFile(filepath)
33
+ if lines is None:
34
+ return -1
35
+
36
+ # 统计main_section中以"1 "开头的有效行数
37
+ valid_line_count = 0
38
+ startLine = 0
39
+ endLine = 0
40
+ lineCount = 0
41
+ foundEnd = False
42
+ in_main_section = False
43
+
44
+ # 首先确定main_section的范围
45
+ for line in lines:
46
+ parameters = line.strip().split()
47
+ if len(parameters) > 2:
48
+ if parameters[0] == "0" and parameters[1] == "FILE":
49
+ if not foundEnd:
50
+ endLine = lineCount
51
+ if endLine > startLine:
52
+ # 标记main_section结束
53
+ foundEnd = True
54
+ break
55
+ # 标记main_section开始
56
+ startLine = lineCount
57
+ foundEnd = False
58
+ in_main_section = True
59
+
60
+ if parameters[0] == "0" and parameters[1] == "NOFILE":
61
+ endLine = lineCount
62
+ foundEnd = True
63
+ in_main_section = False
64
+ break
65
+ lineCount += 1
66
+
67
+ # 如果没找到结束标记,设置结束行为文件末尾
68
+ if not foundEnd:
69
+ endLine = len(lines)
70
+ in_main_section = True # 整个文件视为main_section
71
+
72
+ # 统计main_section范围内以"1 "开头的行
73
+ for i in range(startLine, endLine):
74
+ if lines[i].startswith("1 "):
75
+ valid_line_count += 1
76
+
77
+ return valid_line_count
78
+
79
+ def filter_ldr_by_valid_lines(input_dir):
80
+ """
81
+ 筛选目录下LDR文件,仅打印有效行数(以"1 "开头) >300或<10的文件名
82
+ """
83
+ if not os.path.isdir(input_dir):
84
+ print(f"错误:目录不存在 → {input_dir}")
85
+ return
86
+
87
+ ldr_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.ldr')]
88
+ if not ldr_files:
89
+ print(f"提示:在 {input_dir} 中未找到任何.ldr文件")
90
+ return
91
+
92
+ print(f"=== 开始筛选 {input_dir} 下的LDR文件 ===")
93
+ print(f"总计找到 {len(ldr_files)} 个LDR文件,仅显示符合条件的文件:\n")
94
+
95
+ # 分类存储符合条件的文件
96
+ less_than_10 = [] # 有效行数<10的文件
97
+ more_than_300 = [] # 有效行数>300的文件
98
+ failed_files = [] # 读取失败的文件
99
+
100
+ for ldr_file in ldr_files:
101
+ file_path = os.path.join(input_dir, ldr_file)
102
+ valid_count = count_valid_lines(file_path)
103
+
104
+ if valid_count == -1:
105
+ failed_files.append(ldr_file)
106
+ else:
107
+ if valid_count < 30:
108
+ less_than_10.append((ldr_file, valid_count))
109
+ elif valid_count > 300:
110
+ more_than_300.append((ldr_file, valid_count))
111
+
112
+ # 打印筛选结果
113
+ if less_than_10:
114
+ print("【1】有效行数(1开头)< 30 的文件:")
115
+ for file, count in less_than_10:
116
+ print(f" - {file} → 有效行数:{count}")
117
+ print()
118
+
119
+ if more_than_300:
120
+ print("【2】有效行数(1开头)> 300 的文件:")
121
+ for file, count in more_than_300:
122
+ print(f" - {file} → 有效行数:{count}")
123
+ print()
124
+
125
+ if failed_files:
126
+ print(f"【3】读取失败的文件(共{len(failed_files)}个):")
127
+ for file in failed_files:
128
+ print(f" - {file}")
129
+
130
+ print(f"\n=== 筛选完成 ===")
131
+ print(f"符合条件的文件总数:{len(less_than_10) + len(more_than_300)} 个")
132
+
133
+ if __name__ == "__main__":
134
+ # 请修改为你的LDR文件所在目录
135
+ LDR_DIRECTORY = "/public/home/wangshuo/gap/assembly/data/car_1k/subset/ldr/"
136
+
137
+ # 执行筛选
138
+ filter_ldr_by_valid_lines(LDR_DIRECTORY)