DouDou commited on
Commit
60b5883
·
verified ·
1 Parent(s): 0b75f52

Upload data3/merge_datasets.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. data3/merge_datasets.py +100 -0
data3/merge_datasets.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ 合并 res2.csv 和 dataset_all.csv 数据集
4
+
5
+ res2.csv 结构: 编号, JSON列表(函数信息)
6
+ dataset_all.csv 结构: 编号(row[0]), 文件内容(row[1]), 其他内容...
7
+
8
+ 目标: 找到 res2.csv 中存在的所有编号,从 dataset_all.csv 中提取对应的记录,整合成新数据集
9
+ """
10
+
11
+ import pandas as pd
12
+ import json
13
+ from tqdm import tqdm
14
+ import os
15
+
16
+ def main():
17
+ print("开始处理数据集合并...")
18
+
19
+ # 1. 读取 res2.csv,获取所有存在的编号
20
+ print("\n步骤 1: 读取 res2.csv 获取编号列表...")
21
+ res2_path = '/home/weifengsun/tangou1/step2/res2.csv'
22
+
23
+ # 读取编号列(第一列)
24
+ res2_ids = set()
25
+ chunk_size = 100000
26
+
27
+ for chunk in tqdm(pd.read_csv(res2_path, chunksize=chunk_size, header=None, usecols=[0]),
28
+ desc="读取res2.csv"):
29
+ res2_ids.update(chunk[0].tolist())
30
+
31
+ print(f"从 res2.csv 中找到 {len(res2_ids)} 个唯一编号")
32
+
33
+ # 2. 读取 dataset_all.csv 并筛选匹配的记录
34
+ print("\n步骤 2: 从 dataset_all.csv 中筛选匹配的记录...")
35
+ dataset_all_path = '/home/weifengsun/tangou1/domain_code/src/datasets/data_merged/dataset_all.csv'
36
+ output_path = '/home/weifengsun/tangou1/step2/merged_dataset.csv'
37
+
38
+ # 统计信息
39
+ total_rows = 0
40
+ matched_rows = 0
41
+
42
+ # 分块读取和处理
43
+ first_chunk = True
44
+ for chunk in tqdm(pd.read_csv(dataset_all_path, chunksize=chunk_size, low_memory=False),
45
+ desc="处理dataset_all.csv"):
46
+ total_rows += len(chunk)
47
+
48
+ # 筛选编号在 res2_ids 中的行
49
+ # 假设第一列是编号
50
+ matched_chunk = chunk[chunk.iloc[:, 0].isin(res2_ids)]
51
+ matched_rows += len(matched_chunk)
52
+
53
+ # 写入输出文件
54
+ if len(matched_chunk) > 0:
55
+ if first_chunk:
56
+ matched_chunk.to_csv(output_path, index=True, mode='w')
57
+ first_chunk = False
58
+ else:
59
+ matched_chunk.to_csv(output_path, index=True, mode='a', header=False)
60
+
61
+ print(f"\n处理完成!")
62
+ print(f"总共处理行数: {total_rows}")
63
+ print(f"匹配的行数: {matched_rows}")
64
+ print(f"匹配率: {matched_rows/total_rows*100:.2f}%")
65
+ print(f"输出文件: {output_path}")
66
+
67
+ # 3. 可选: 创建一个包含函数信息的增强版本
68
+ print("\n步骤 3: 创建包含函数信息的增强数据集...")
69
+ enhanced_output_path = '/home/weifengsun/tangou1/step2/enhanced_dataset.csv'
70
+
71
+ # 读取res2.csv到字典
72
+ print("加载res2.csv函数信息...")
73
+ res2_dict = {}
74
+ for chunk in tqdm(pd.read_csv(res2_path, chunksize=chunk_size, header=None),
75
+ desc="加载res2"):
76
+ for idx, row in chunk.iterrows():
77
+ res2_dict[row[0]] = row[1]
78
+
79
+ # 读取刚生成的merged_dataset.csv并添加函数信息
80
+ print("合并函数信息...")
81
+ merged_df = pd.read_csv(output_path, low_memory=False)
82
+
83
+ # 添加函数信息列
84
+ merged_df['function_info'] = merged_df.iloc[:, 0].map(res2_dict)
85
+
86
+ # 保存增强数据集
87
+ merged_df.to_csv(enhanced_output_path, index=True)
88
+
89
+ print(f"\n增强数据集已保存到: {enhanced_output_path}")
90
+ print(f"增强数据集行数: {len(merged_df)}")
91
+ print(f"增强数据集列数: {len(merged_df.columns)}")
92
+
93
+ # 显示示例
94
+ print("\n数据集前5行预览:")
95
+ print(merged_df.head())
96
+ print("\n增强数据集列名:")
97
+ print(merged_df.columns.tolist())
98
+
99
+ if __name__ == "__main__":
100
+ main()