File size: 7,985 Bytes
4ac1fc5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | # # import json
# # import matplotlib.pyplot as plt
# # from collections import Counter
# # # 读取数据
# # file_path = '/share/project/sunshuang/deep_search/data_syn/data/mixed_data/splits/tagged_domain_keypoints/merged_tagged_domain_keypoints_keywords_count_hop.json'
# # with open(file_path, 'r') as f:
# # data = json.load(f)
# # # 提取所需字段
# # domains = []
# # totals = []
# # special_totals = []
# # hops = []
# # for entry in data:
# # domain_info = entry.get('domain_keypoints', {})
# # # print(domain_info)
# # # print(f"idx: {entry['idx']}")
# # keywords_info = entry.get('keywords_count', {})
# # domains.append(domain_info.get('domain', 'Unknown'))
# # totals.append(keywords_info.get('total', 0))
# # special_totals.append(keywords_info.get('special_total', 0))
# # hops.append(entry.get('hop', 0))
# # # 统计domain分布并绘制饼图
# # domain_counts = Counter(domains)
# # domain_labels = [f'{dom} ({cnt})' for dom, cnt in domain_counts.items()]
# # domain_sizes = list(domain_counts.values())
# # plt.figure(figsize=(12, 7))
# # plt.pie(domain_sizes, labels=domain_labels, autopct='%1.1f%%', startangle=140)
# # plt.title('Domain Distribution (Count in Parentheses)')
# # plt.savefig('domain_distribution_pie.png')
# # plt.close()
# # # 辅助函数:绘制带数值标签的直方图
# # def plot_histogram(data, title, xlabel, output_filename):
# # counts = Counter(data)
# # sorted_items = sorted(counts.items())
# # labels, values = zip(*sorted_items)
# # plt.figure(figsize=(10, 6))
# # bars = plt.bar(labels, values)
# # plt.title(title)
# # plt.xlabel(xlabel)
# # plt.ylabel('Count')
# # for bar in bars:
# # height = bar.get_height()
# # plt.text(bar.get_x() + bar.get_width()/2., height, str(height),
# # ha='center', va='bottom')
# # plt.savefig(output_filename)
# # plt.close()
# # # 绘制各个直方图
# # plot_histogram(totals, 'Total Distribution', 'Total Value', 'total_histogram.png')
# # plot_histogram(special_totals, 'Special Total Distribution', 'Special Total Value', 'special_total_histogram.png')
# # plot_histogram(hops, 'Hop Distribution', 'Hop Value', 'hop_histogram.png')
# import json
# import matplotlib.pyplot as plt
# from collections import Counter, OrderedDict
# # 读取数据
# file_path = '/share/project/sunshuang/deep_search/data_syn/data/mixed_data/splits/tagged_domain_keypoints/merged_tagged_domain_keypoints_keywords_count_hop.json'
# with open(file_path, 'r') as f:
# data = json.load(f)
# # 提取所需字段
# domains = []
# totals = []
# special_totals = []
# hops = []
# for entry in data:
# domain_info = entry.get('domain_keypoints', {})
# keywords_info = entry.get('keywords_count', {})
# domains.append(domain_info.get('domain', 'Unknown'))
# totals.append(keywords_info.get('total', 0))
# special_totals.append(keywords_info.get('special_total', 0))
# hops.append(entry.get('hop', 0))
# # 统计domain分布
# domain_counts = Counter(domains)
# # 按照 value 降序排序
# domain_counts = OrderedDict(sorted(domain_counts.items(), key=lambda item: item[1], reverse=True))
# # 保存所有domain及其计数到JSON文件
# domain_count_file = 'domain_counts.json'
# with open(domain_count_file, 'w') as f:
# json.dump(domain_counts, f, indent=4)
# # 合并数目少于10的domain为Other
# threshold = 100
# filtered_domains = {}
# other_count = 0
# for domain, count in domain_counts.items():
# if count < threshold:
# other_count += count
# else:
# filtered_domains[domain] = count
# if other_count > 0:
# filtered_domains['Other'] = other_count
# # 绘制饼图
# domain_labels = [f'{dom} ({cnt})' for dom, cnt in filtered_domains.items()]
# domain_sizes = list(filtered_domains.values())
# plt.figure(figsize=(12, 7))
# plt.pie(domain_sizes, labels=domain_labels, autopct='%1.1f%%', startangle=140)
# plt.title('Domain Distribution (Count in Parentheses)')
# plt.savefig('domain_distribution_pie.png')
# plt.close()
# # 辅助函数:绘制带数值标签的直方图
# def plot_histogram(data, title, xlabel, output_filename):
# counts = Counter(data)
# sorted_items = sorted(counts.items())
# labels, values = zip(*sorted_items)
# plt.figure(figsize=(10, 6))
# bars = plt.bar(labels, values)
# plt.title(title)
# plt.xlabel(xlabel)
# plt.ylabel('Count')
# for bar in bars:
# height = bar.get_height()
# plt.text(bar.get_x() + bar.get_width()/2., height, str(height),
# ha='center', va='bottom')
# plt.savefig(output_filename)
# plt.close()
# # 绘制各个直方图
# plot_histogram(totals, 'Total Distribution', 'Total Value', 'total_histogram.png')
# plot_histogram(special_totals, 'Special Total Distribution', 'Special Total Value', 'special_total_histogram.png')
# plot_histogram(hops, 'Hop Distribution', 'Hop Value', 'hop_histogram.png')
import json
import matplotlib.pyplot as plt
from collections import Counter, OrderedDict
# 读取数据
file_path = '/share/project/sunshuang/deep_search/data_for_rl/musique_tagged/final_selected_dataset.json'
with open(file_path, 'r') as f:
data = json.load(f)
# 提取所需字段
domains = []
totals = []
special_totals = []
hops = []
for entry in data:
domain_info = entry.get('domain_keypoints', {})
keywords_info = entry.get('keywords_count', {})
domains.append(domain_info.get('domain', 'Unknown'))
totals.append(keywords_info.get('total', 0))
special_totals.append(keywords_info.get('special_total', 0))
hops.append(entry.get('hop', 0))
# 统计domain分布
domain_counts = Counter(domains)
# 按照 value 降序排序
domain_counts = OrderedDict(sorted(domain_counts.items(), key=lambda item: item[1], reverse=True))
# 保存所有domain及其计数到JSON文件
domain_count_file = 'domain_counts.json'
with open(domain_count_file, 'w') as f:
json.dump(domain_counts, f, indent=4)
# 合并数目少于100的domain为Other
threshold = 1
filtered_domains = {}
other_count = 0
for domain, count in domain_counts.items():
if count < threshold:
other_count += count
else:
filtered_domains[domain] = count
if other_count > 0:
filtered_domains['Other'] = other_count
# 绘制饼图
domain_labels = [f'{dom}' for dom, cnt in filtered_domains.items()] # 只显示domain名称
domain_sizes = list(filtered_domains.values())
plt.figure(figsize=(12, 7))
plt.pie(
domain_sizes,
labels=None, # 不直接在饼图上显示标签
autopct='%1.1f%%',
startangle=140
)
# 添加图例
plt.legend(
labels=[f'{dom} ({cnt})' for dom, cnt in filtered_domains.items()],
loc='upper right',
bbox_to_anchor=(1.2, 1), # 图例位置调整到右侧
fontsize=10
)
plt.title('Domain Distribution (Count in Parentheses)')
plt.tight_layout() # 优化布局
plt.savefig('domain_distribution_pie.png', bbox_inches='tight') # 避免裁剪
plt.close()
# 辅助函数:绘制带数值标签的直方图
def plot_histogram(data, title, xlabel, output_filename):
counts = Counter(data)
sorted_items = sorted(counts.items())
labels, values = zip(*sorted_items)
plt.figure(figsize=(10, 6))
bars = plt.bar(labels, values)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel('Count')
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height, str(height),
ha='center', va='bottom')
plt.savefig(output_filename)
plt.close()
# 绘制各个直方图
plot_histogram(totals, 'Total Distribution', 'Total Value', 'total_histogram.png')
plot_histogram(special_totals, 'Special Total Distribution', 'Special Total Value', 'special_total_histogram.png')
plot_histogram(hops, 'Hop Distribution', 'Hop Value', 'hop_histogram.png') |