Spaces:
Paused
Paused
Add code/cube3d/render/ldr_freq.py
Browse files
code/cube3d/render/ldr_freq.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
|
| 5 |
+
# 输入主文件夹路径,包含多个子文件夹
|
| 6 |
+
main_folder = "/public/home/wangshuo/gap/assembly/data/car_1k/ldr" # 替换为你的主文件夹路径
|
| 7 |
+
output_file = "part_count_with_files.txt" # 输出统计结果文件(可选)
|
| 8 |
+
|
| 9 |
+
# 统计零件的种类、频率和它们出现在多少个文件中的统计
|
| 10 |
+
part_count = defaultdict(int) # 统计零件出现的次数
|
| 11 |
+
part_file_count = defaultdict(set) # 统计零件出现在多少个不同的文件中
|
| 12 |
+
total_valid_lines = 0 # 统计有效的行数
|
| 13 |
+
|
| 14 |
+
# 遍历主文件夹中的所有子文件夹
|
| 15 |
+
for subdir in os.listdir(main_folder):
|
| 16 |
+
subdir_path = os.path.join(main_folder, subdir)
|
| 17 |
+
|
| 18 |
+
if os.path.isdir(subdir_path):
|
| 19 |
+
# 假设每个子文件夹中都有一个 LDR 文件
|
| 20 |
+
ldr_files = [f for f in os.listdir(subdir_path) if f.endswith('.ldr')]
|
| 21 |
+
|
| 22 |
+
for ldr_file in ldr_files:
|
| 23 |
+
ldr_path = os.path.join(subdir_path, ldr_file)
|
| 24 |
+
|
| 25 |
+
# 打开并读取每个 LDR 文件
|
| 26 |
+
with open(ldr_path, "r") as file:
|
| 27 |
+
for line in file:
|
| 28 |
+
# 只统计以 '1' 开头的行,代表零件
|
| 29 |
+
if line.startswith("1"):
|
| 30 |
+
total_valid_lines += 1 # 有效行数加 1
|
| 31 |
+
columns = line.split()
|
| 32 |
+
|
| 33 |
+
if len(columns) >= 15:
|
| 34 |
+
# 零件名称从第15列开始
|
| 35 |
+
part_name = " ".join(columns[14:]) # 合并第15列及后面的列
|
| 36 |
+
|
| 37 |
+
# 统计零件出现的次数
|
| 38 |
+
part_count[part_name] += 1
|
| 39 |
+
|
| 40 |
+
# 统计零件出现在多少个不同的 LDR 文件中
|
| 41 |
+
part_file_count[part_name].add(ldr_file)
|
| 42 |
+
|
| 43 |
+
# 输出零件种类、使用频率和出现在多少个文件中的统计
|
| 44 |
+
with open(output_file, "w") as f:
|
| 45 |
+
f.write("零件名称, 使用频率, 出现的 LDR 文件数\n")
|
| 46 |
+
for part_name, count in sorted(part_count.items(), key=lambda x: x[1], reverse=True):
|
| 47 |
+
file_count = len(part_file_count[part_name]) # 获取该零件出现在多少个不同文件
|
| 48 |
+
f.write(f"{part_name}, {count}, {file_count}\n")
|
| 49 |
+
|
| 50 |
+
# 输出统计信息
|
| 51 |
+
print(f"有效行数:{total_valid_lines}")
|
| 52 |
+
print(f"统计完成,结果已保存到 {output_file}")
|