tmp_Pro / draw /tools /data_source_distribution.py
czyPL's picture
Add files using upload-large-folder tool
293a64d verified
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import json
from matplotlib import font_manager as fm
from matplotlib.font_manager import FontProperties
font_path = "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
font_bold_path = "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf"
fm.fontManager.addfont(font_path)
fm.fontManager.addfont(font_bold_path)
plt.rcParams["font.family"] = "Arial"
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
plt.rcParams["axes.titleweight"] = "bold"
plt.rcParams["font.size"] = 12
plt.rcParams["xtick.labelsize"] = 12
plt.rcParams["ytick.labelsize"] = 12
bold_font = FontProperties(fname=font_bold_path, weight="bold")
'''
数据获取和统计
'''
# with open("/cpfs/user/chenziyang/LongBenchmark/eval/dataset/longbench_pro_final.json", "r") as f:
# data = json.load(f)
# print(len(data))
# en, zh = 0, 0
# full, partial = 0, 0
# k8, k16, k32, k64, k128, k256 = 0, 0, 0, 0, 0, 0
# extreme, hard, moderate, easy = 0, 0, 0, 0
# for d in data:
# if d["language"] == "English":
# en += 1
# elif d["language"] == "Chinese":
# zh += 1
# if d["contextual_requirement"] == "Full":
# full += 1
# elif d["contextual_requirement"] == "Partial":
# partial += 1
# if d["token_length"] == "8k":
# k8 += 1
# elif d["token_length"] == "16k":
# k16 += 1
# elif d["token_length"] == "32k":
# k32 += 1
# elif d["token_length"] == "64k":
# k64 += 1
# elif d["token_length"] == "128k":
# k128 += 1
# elif d["token_length"] == "256k":
# k256 += 1
# if d["difficulty"] == "Extreme":
# extreme += 1
# elif d["difficulty"] == "Hard":
# hard += 1
# elif d["difficulty"] == "Moderate":
# moderate += 1
# elif d["difficulty"] == "Easy":
# easy += 1
# print(f"English: {en}, Chinese: {zh}")
# print(f"Full: {full}, Partial: {partial}")
# print(f"k8: {k8}, k16: {k16}, k32: {k32}, k64: {k64}, k128: {k128}, k256: {k256}")
# print(f"extreme: {extreme}, hard: {hard}, moderate: {moderate}, easy: {easy}")
# from collections import Counter
# with open("/cpfs/user/chenziyang/LongBenchmark/eval/dataset/doc_type/doc_type_stat_final_patch.json", "r") as f:
# data = json.load(f)
# print(len(data))
# text_type_conter = Counter()
# for item in data:
# text_type_conter[item["doc_type"]] += 1
# print(text_type_conter)
# ---------------------------
# Example data (replace with your real values)
# ---------------------------
data = {
"Difficulty": {"Extreme": 440, "Hard": 289, "Moderate": 289, "Easy": 482},
"Length": {"8k": 250, "16k": 250, "32k": 250, "64k": 250, "128k": 250, "256k": 250},
"Context\nRequirement": {"Full": 840, "Partial": 660},
"Language": {"English": 750, "Chinese": 750},
"Text Type": {
"Literature (Novel)": 320,
"Literature (Other)": 179,
"Law": 176,
"Social": 121,
"Finance": 115,
"Technology": 113,
"Science": 107,
"History": 103,
"News": 69,
"Policy": 55,
"Education": 51,
"Structured": 41,
"Medicine": 27,
"Other": 23,
},
}
# ---------------------------
# Convert to dataframe and compute percentages
# ---------------------------
all_categories = []
for dimension_data in data.values():
for category in dimension_data.keys():
if category not in all_categories:
all_categories.append(category)
df = pd.DataFrame([{c: d.get(c, 0) for c in all_categories} for d in data.values()],
index=list(data.keys()), columns=all_categories)
difficulty_categories = set(data["Difficulty"].keys())
row_sums = df.sum(axis=1)
# ---------------------------
# Plot
# ---------------------------
fig, ax = plt.subplots(figsize=(16, 4)) # compact wide layout
y_spacing = 0.8 # reduce gap between bars
y_pos = np.arange(len(df)) * y_spacing
left = np.zeros(len(df))
# Use a discrete colormap
category_colors = {
# Difficulty
"Extreme": "#DA7B89",
"Hard": "#E6C875",
"Moderate": "#C5DB80",
"Easy": "#92C56A",
# Length
"8k": "#d2e3f3",
"16k": "#aacee4",
"32k": "#68abd5",
"64k": "#3788bf",
"128k": "#0f5ba6",
"256k": "#09336f",
# Context Requirement
"Full": "#495481",
"Partial": "#e3882f",
# Language
"English": "#80a492",
"Chinese": "#d23918",
# Text Type
"Literature (Novel)": "#393B79",
"Law": "#5254A3",
"Literature (Other)": "#6B6ECF",
"Social": "#9C9EDE",
"Technology": "#637939",
"Finance": "#8CA252",
"Science": "#B5CF6B",
"History": "#CEDB9C",
"News": "#8C6D31",
"Policy": "#BD9E39",
"Education": "#E7BA52",
"Structured": "#E7CB94",
"Medicine": "#843C39",
"Other": "#AD494A",
}
for i, cat in enumerate(all_categories):
values = df[cat].values
bar_color = category_colors[cat]
ax.barh(y_pos, values, left=left, color=bar_color, edgecolor="white", height=0.45)
# put category name INSIDE bar if segment > threshold
for j, val in enumerate(values):
if val >= 52: # threshold to avoid clutter
percentage = (val / row_sums[j]) * 100 if row_sums[j] > 0 else 0
text_x = left[j] + val / 2
text_y = y_pos[j]
text_color = "white"
ax.text(text_x, text_y + 0.08, cat,
va="center", ha="center", fontsize=10, color=text_color,
fontproperties=bold_font)
ax.text(text_x, text_y - 0.08, f"{percentage:.1f}%",
va="center", ha="center", fontsize=8, color=text_color,
fontproperties=bold_font)
left += values
# ---------------------------
# Formatting
# ---------------------------
ax.set_yticks(y_pos)
ax.set_yticklabels(df.index, fontsize=12, fontweight="bold")
for label in ax.get_yticklabels():
label.set_horizontalalignment("center")
label.set_fontproperties(bold_font)
ax.tick_params(axis='y', pad=40, labelsize=12)
ax.set_xlim(0, max(row_sums))
xlabel = ax.set_xlabel("Percentage of Samples (Total Samples = 1,500)", fontsize=12, fontweight="bold")
xlabel.set_fontproperties(bold_font)
ax.set_xticks([])
ax.tick_params(axis='x', bottom=False, labelbottom=False, labelsize=12)
# No legend
ax.legend().remove()
plt.tight_layout()
plt.show()
save_path = "/cpfs/user/chenziyang/LongBenchmark/draw/images/data_source_distribution.png"
plt.savefig(save_path, dpi=600, bbox_inches='tight', facecolor='white', edgecolor='none')
print(f"Figure saved to: {save_path}")