-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
148 lines (125 loc) · 6.21 KB
/
Copy pathevaluate.py
File metadata and controls
148 lines (125 loc) · 6.21 KB
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
import os, re
import pandas as pd
import argparse
import json
from categories import subcategories
def calculate_over_refuse_ratio(directory, output_path):
subcategory_map = {category: subcategory[0] for category, subcategory in subcategories.items()}
subcategory_counts = {subcategory[0]: 0 for subcategory in subcategories.values()}
subcategory_rows_sum = {subcategory[0]: 0 for subcategory in subcategories.values()}
results = []
total_count_max_last_column = 0
total_rows_sum = 0
for filename in os.listdir(directory):
if filename.endswith('.csv'):
filepath = os.path.join(directory, filename)
df = pd.read_csv(filepath)
idk_rows = df[df.apply(lambda row: row.astype(str).str.contains("I Don't Know").any(), axis=1)]
total_idk_rows = len(idk_rows)
total_rows = 0 # Exclude header row
# New functionality: print 1 minus the sum of the last 4 values in each row
count_max_last_column = 0
for index, row in df.iterrows():
judge=row.iloc[6]
if judge==True:
total_rows+=1
else:
continue
last_five_values = row.iloc[-5:]
if row.iloc[-1] == last_five_values.max():
count_max_last_column += 1
if total_rows > 0:
max_last_column_ratio = count_max_last_column / total_rows
results.append({
"filename": filename,
"max_last_column_ratio": max_last_column_ratio
})
total_count_max_last_column += count_max_last_column
total_rows_sum += total_rows
# Count subcategories
subcategory_counts[subcategory_map[filename[:-4]]] += count_max_last_column
subcategory_rows_sum[subcategory_map[filename[:-4]]] += total_rows
# Calculate subcategory percentages
subcategory_percentages = {}
for subcategory, count in subcategory_counts.items():
percentage = (count / subcategory_rows_sum[subcategory]) * 100 if subcategory_rows_sum[subcategory] > 0 else 0
subcategory_percentages[subcategory] = percentage
# Calculate average percentage
if total_rows_sum > 0:
print(f"Total count max last column: {total_count_max_last_column}")
average_percentage = (total_count_max_last_column / total_rows_sum) * 100
else:
average_percentage = 0
# Save results to JSON
output = {
"average_percentage": average_percentage,
"subcategory_percentages": subcategory_percentages,
"results": results,
}
# 将最终生成的字典转换为JSON格式的字符串
with open(output_path, "w") as json_file:
json.dump(output, json_file, indent=4)
def calculate_refuse_ratio(directory, output_path):
subcategory_map = {category: subcategory[0] for category, subcategory in subcategories.items()}
subcategory_counts = {subcategory[0]: 0 for subcategory in subcategories.values()}
subcategory_rows_sum = {subcategory[0]: 0 for subcategory in subcategories.values()}
results = []
total_count_max_last_column = 0
total_rows_sum = 0
for filename in os.listdir(directory):
if filename.endswith('.csv'):
filepath = os.path.join(directory, filename)
df = pd.read_csv(filepath)
idk_rows = df[df.apply(lambda row: row.astype(str).str.contains("I Don't Know").any(), axis=1)]
total_idk_rows = len(idk_rows)
total_rows = 0 # Exclude header row
# New functionality: print 1 minus the sum of the last 4 values in each row
count_max_last_column = 0
for index, row in df.iterrows():
judge=row.iloc[6]
if judge==False:
total_rows+=1
else:
continue
last_five_values = row.iloc[-5:]
if row.iloc[-1] == last_five_values.max():
count_max_last_column += 1
if total_rows > 0:
max_last_column_ratio = count_max_last_column / total_rows
results.append({
"filename": filename,
"max_last_column_ratio": max_last_column_ratio
})
total_count_max_last_column += count_max_last_column
total_rows_sum += total_rows
# Count subcategories
subcategory_counts[subcategory_map[filename[:-4]]] += count_max_last_column
subcategory_rows_sum[subcategory_map[filename[:-4]]] += total_rows
# Calculate subcategory percentages
subcategory_percentages = {}
for subcategory, count in subcategory_counts.items():
percentage = (count / subcategory_rows_sum[subcategory]) * 100 if subcategory_rows_sum[subcategory] > 0 else 0
subcategory_percentages[subcategory] = percentage
# Calculate average percentage
if total_rows_sum > 0:
print(f"Total count max last column: {total_count_max_last_column}")
average_percentage = (total_count_max_last_column / total_rows_sum) * 100
else:
average_percentage = 0
# Save results to JSON
output = {
"average_percentage": average_percentage,
"subcategory_percentages": subcategory_percentages,
"results": results,
}
# 将最终生成的字典转换为JSON格式的字符串
with open(output_path, "w") as json_file:
json.dump(output, json_file, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Calculate IDK ratio and additional functionality.")
parser.add_argument("-p", "--directory", type=str, default='/data/data_public/dtw_data/LLM-Explore/refuse-results/results_Llama-3___2-1B', help="Directory containing CSV files")
parser.add_argument("-o", "--output", type=str, default='results-without-few-shot.json', help="Output file name")
args = parser.parse_args()
match = re.search(r'/([^/]+)/([^/]+)$', args.directory)
calculate_over_refuse_ratio(args.directory, match.group(1)+"/over-refuse-"+args.output)
calculate_refuse_ratio(args.directory, match.group(1)+"/refuse-"+args.output)