# # 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')