Upload VideoX-Fun/analyze_json.py
Browse files- VideoX-Fun/analyze_json.py +71 -0
VideoX-Fun/analyze_json.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
def plot_filtered_yes_prob_histogram(folder_path, num_bins=10):
|
| 6 |
+
"""
|
| 7 |
+
读取文件夹中所有 .json 文件,但只统计第一个回答为 'No' 或 'Bad' 的
|
| 8 |
+
文件的 "Yes prob" 值,并绘制其频率分布直方图。
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
folder_path (str): 包含 .json 文件的文件夹路径。
|
| 12 |
+
num_bins (int): 直方图的柱子数量(分箱数量)。
|
| 13 |
+
"""
|
| 14 |
+
yes_probs = []
|
| 15 |
+
total_files = 0
|
| 16 |
+
filtered_files = 0
|
| 17 |
+
|
| 18 |
+
for filename in os.listdir(folder_path):
|
| 19 |
+
if filename.endswith(".json"):
|
| 20 |
+
total_files += 1
|
| 21 |
+
file_path = os.path.join(folder_path, filename)
|
| 22 |
+
try:
|
| 23 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 24 |
+
data = json.load(f)
|
| 25 |
+
|
| 26 |
+
# 检查数据结构是否符合预期,并确保第一个字典不为空
|
| 27 |
+
if (isinstance(data, list) and len(data) > 1 and
|
| 28 |
+
isinstance(data[0], dict) and data[0]):
|
| 29 |
+
|
| 30 |
+
# 获取第一个字典中的值(回答)
|
| 31 |
+
# 这种方法不需要知道具体的键(问题)是什么
|
| 32 |
+
first_answer = next(iter(data[0].values()))
|
| 33 |
+
|
| 34 |
+
# ----> 这是新增的筛选条件 <----
|
| 35 |
+
if first_answer in ['No', 'Bad']:
|
| 36 |
+
filtered_files += 1
|
| 37 |
+
# 如果条件满足,则提取 "Yes prob"
|
| 38 |
+
if isinstance(data[1], dict) and 'Yes prob' in data[1]:
|
| 39 |
+
yes_probs.append(data[1]['Yes prob'])
|
| 40 |
+
|
| 41 |
+
except (json.JSONDecodeError, IndexError, KeyError, StopIteration) as e:
|
| 42 |
+
print(f"处理文件 {filename} 时出错或因格式不符已跳过: {e}")
|
| 43 |
+
|
| 44 |
+
if not yes_probs:
|
| 45 |
+
print(f"在 {total_files} 个文件中,未找到符合条件 ('No' 或 'Bad') 的 'Yes prob' 值。")
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
print(f"总共扫描 {total_files} 个文件,其中 {filtered_files} 个文件的回答为 'No' 或 'Bad'。")
|
| 49 |
+
print(f"正在为这 {len(yes_probs)} 个 'Yes prob' 值生成直方图...")
|
| 50 |
+
|
| 51 |
+
# 绘制直方图
|
| 52 |
+
plt.figure(figsize=(12, 7))
|
| 53 |
+
plt.hist(yes_probs, bins=num_bins, edgecolor='black', alpha=0.7)
|
| 54 |
+
|
| 55 |
+
plt.xlabel("'Yes prob'", fontsize=12)
|
| 56 |
+
plt.ylabel("Num", fontsize=12)
|
| 57 |
+
plt.title("x", fontsize=16)
|
| 58 |
+
plt.grid(axis='y', linestyle='--', linewidth=0.7)
|
| 59 |
+
|
| 60 |
+
# 保存图表
|
| 61 |
+
plt.savefig("/nfs/ywang29/Reward_finetuning/VideoX-Fun/output_Sep28_2/filtered_yes_prob_histogram.png")
|
| 62 |
+
print(f"筛选后的直方图已保存为 filtered_yes_prob_histogram.png")
|
| 63 |
+
|
| 64 |
+
# --- 使用说明 ---
|
| 65 |
+
# 1. 将下面的 'your_folder_path' 替换为您的文件夹实际路径。
|
| 66 |
+
# 2. (可选)您可以修改 'num_bins=15' 中的数字来调整直方图的精细程度。
|
| 67 |
+
# 3. 运行此脚本。
|
| 68 |
+
|
| 69 |
+
# 请在这里替换为您的文件夹路径
|
| 70 |
+
your_folder_path = '/nfs/ywang29/Reward_finetuning/VideoX-Fun/output_Sep28_2/train_sample'
|
| 71 |
+
plot_filtered_yes_prob_histogram(your_folder_path, num_bins=15)
|