File size: 10,883 Bytes
f020aef |
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 |
import json
import re
import os
import shutil
from pathlib import Path
from compare_value import is_number, convert, convert_to_number, clean, is_valid_thousand_separator, only_digits_and_commas
from compare_value import compare_value
file_idx = 0
def compare(data_chart, data_image):
matching_question = []
qid = []
res = []
path = {}
path_score = {}
for question_id, chart_data in data_chart.items():
if "response" not in chart_data:
continue
# 确保对应 question_id 在 data_image 中
if question_id in data_image:
image_data = data_image[question_id]
if "response" not in image_data or "score" not in image_data:
continue
qa = {
"question_id": chart_data["question_id"],
"qid":chart_data["qid"],
"figue":data_qa[chart_data["question_id"]]["figure_path"],
"question":data_qa[chart_data["question_id"]]["question"],
"answer":chart_data["answer"],
"response":chart_data["response"]
}
if chart_data["score"] == 0:
matching_question.append(qa)
# 检查条件
if chart_data["score"] == 0 and image_data["score"] == 1:
qid.append(chart_data["qid"])
if data_qa[chart_data["question_id"]]["figure_path"] not in path:
path[data_qa[chart_data["question_id"]]["figure_path"]] = 0
path[data_qa[chart_data["question_id"]]["figure_path"]] -= 1
if chart_data["score"] == 1 and image_data["score"] == 0:
if data_qa[chart_data["question_id"]]["figure_path"] not in path:
path[data_qa[chart_data["question_id"]]["figure_path"]] = 0
path[data_qa[chart_data["question_id"]]["figure_path"]] += 1
if data_qa[chart_data["question_id"]]["figure_path"] not in path_score:
path_score[data_qa[chart_data["question_id"]]["figure_path"]] = {"image": 0, "chart": 0}
path_score[data_qa[chart_data["question_id"]]["figure_path"]]["image"] += (image_data["score"] if not image_data["score"] == -1 else 0)
path_score[data_qa[chart_data["question_id"]]["figure_path"]]["chart"] += (chart_data["score"] if not chart_data["score"] == -1 else 0)
# 输出结果
# print("Matching question IDs:", matching_question)
# with open("compare_res.json", "w") as f:
# json.dump(matching_question, f, indent=4)
# print(qid)
path = dict(sorted(path.items(), key=lambda item: item[1]))
# print(path)
path_score = dict(sorted(path_score.items(), key=lambda item: item[1]["chart"]))
# print(path_score)
# for item in path_score:
# print(item,path_score[item])
easy_path_score = {k: v for k, v in path_score.items() if v['chart'] < v['image']}
easy_path_score = {k: path_score[k] for k in sorted(easy_path_score)}
even_path_score = {k: v for k, v in path_score.items() if v['chart'] == v['image']}
even_path_score = {k: path_score[k] for k in sorted(even_path_score)}
hard_path_score = {k: v for k, v in path_score.items() if v['chart'] > v['image']}
hard_path_score = {k: path_score[k] for k in sorted(hard_path_score)}
res = {}
# 如果文件存在,读取原有数据
# file_path = "/data/tianchi/minzhi/chartQA/code/ana/bar/simple/gemini/value.json"
# if Path(file_path).exists():
# with open(file_path, "r") as f:
# existing_data = json.load(f)
# else:
# existing_data = {}
# # 更新数据(假设 path_score 是一个字典)
# print(len(existing_data))
# # print(existing_data)
# print("--------------------------------------")
# # print(path_score)
# existing_data.update(path_score) # 或者用其他方式合并数据
# print(len(existing_data))
# # 写入合并后的数据
# with open(file_path, "w") as f:
# json.dump(existing_data, f, indent=4)
# # print(path_score.keys())
# print("-----------------")
# global file_idx
# for item in easy_path_score:
# print(f"/data/tianchi/minzhi/chartQA/code/CharXiv/img/bar/single/archive{file_idx}/plain_chart/"+item+".png",easy_path_score[item])
# source_filepath = f"/data/tianchi/minzhi/chartQA/code/CharXiv/img/bar/single/archive{file_idx}/chart/h/"+item+".png"
# target_filepath = ""
# if os.path.exists(source_filepath):
# print(f" {source_filepath}")
# # shutil.copy(source_filepath, target_filepath)
# source_filepath = f"/data/tianchi/minzhi/chartQA/code/CharXiv/img/bar/single/archive{file_idx}/chart/v/"+item+".png"
# if os.path.exists(source_filepath):
# print(f" {source_filepath}")
# shutil.copy(source_filepath, target_filepath)
def eval_value(path):
print(path.split("/")[-1])
with open(path, 'r') as file:
# 解析 JSON 数据
data = json.load(file)
count = {0:0, 1:0}
for idx in data:
item = data[idx]
if "response" not in item:
continue
# print(item["response"])
if compare_value(item["answer"], item["response"]):
data[idx]["score"] = 1
else:
data[idx]["score"] = 0
# if idx == "3":
# print(item["answer"], item["response"],data[idx]["score"])
# break
count[data[idx]["score"]] += 1
with open(path.replace(path.split('/')[-1],"score-"+path.split('/')[-1]), 'w') as f:
json.dump(data, f, indent=4)
print(count,count[1]/(count[0] + count[1]))
return count
def analysis(chartpath, imagepath):
print("")
eval_value(chartpath)
eval_value(imagepath)
chartpath = chartpath.replace(chartpath.split('/')[-1],"score-"+chartpath.split('/')[-1])
imagepath = imagepath.replace(imagepath.split('/')[-1],"score-"+imagepath.split('/')[-1])
with open(chartpath, 'r') as file:
data_chart = json.load(file)
with open(imagepath, 'r') as file:
data_image = json.load(file)
# compare(data_chart, data_image)
# compare(data_image, data_chart, target_root = "/data/tianchi/minzhi/chartQA/code/ana/bar/simple/value/hard")
file_idx = 3
# with open(f"/data/tianchi/CharXiv/data/line/line_QA_value.json", 'r') as file:
# # 解析 JSON 数据
# data_qa = json.load(file)
# analysis("/data/tianchi/minzhi/chartQA/code/ana/bar/simple/gemini/record/bar-difference-internlm-xcomposer2-4khd-7b-chart.json",
# "/data/tianchi/minzhi/chartQA/code/ana/bar/simple/gemini/record/bar-difference-internlm-xcomposer2-4khd-7b-image.json")
# model = 'gemini-2.0-flash'
# analysis(f"/data/tianchi/CharXiv/results/{model}/{model}_line_value_plain.json",
# f"/data/tianchi/CharXiv/results/{model}/{model}_line_value_info.json")
# analysis(f"/data/tianchi/CharXiv/results/{model}/{model}_line_difference_plain.json",
# f"/data/tianchi/CharXiv/results/{model}/{model}_line_difference_info.json")
# file_list = ["/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/gemini-2.0-flash_label_difference_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/new_gemini-2.0-flash_label_difference_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/gemini-2.0-flash_visual_difference_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/new_gemini-2.0-flash_visual_difference_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/gemini-2.0-flash_label_value_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/new_gemini-2.0-flash_label_value_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/gemini-2.0-flash_visual_value_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/new_gemini-2.0-flash_visual_value_bar_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/line_gemini-2.0-flash_label_difference_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/line_gemini-2.0-flash_visual_difference_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/line_gemini-2.0-flash_label_value_QA.json",
# "/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/line_gemini-2.0-flash_visual_value_QA.json"
# ]
file_list = ["/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_flag_no-label_label_value_bar_QA.json",
"/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_flag_origin_label_value_bar_QA.json",
"/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_no-label_icon_value_bar_QA.json",
"/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_no-label_label_value_bar_QA.json",
"/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_origin_icon_value_bar_QA.json",
"/Users/linminzhi/Documents/chartQA/code/QA/our_bar/icon-label/record/icon-label/gemini-2.0-flash_origin_label_value_bar_QA.json"]
for file in file_list:
count1 = eval_value(file)
# count2 = eval_value(file.replace("gemini-2.0-flash","new_gemini-2.0-flash"))
# count = {0:count1[0]+count2[0],1:count1[1]+count2[1]}
# print(count,count[1]/(count[0] + count[1]))
print()
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/hard/gemini-2.0-flash_value_bar_info_QA.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/hard/gemini-2.0-flash_value_bar_plain_QA.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/radial/gemini-2.0-flash-exp_radial_difference_bar_QA_plain.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/radial/gemini-2.0-flash-exp_radial_extreme_bar_QA_info.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/radial/gemini-2.0-flash-exp_radial_extreme_bar_QA_plain.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/radial/gemini-2.0-flash-exp_radial_value_bar_QA_info.json")
# eval_value("/Users/linminzhi/Documents/chartQA/code/QA/our_bar/radial/gemini-2.0-flash-exp_radial_value_bar_QA_plain.json") |