Use bomb_risk_density.csv
Browse files
app.py
CHANGED
@@ -1,146 +1,8 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
import pandas as pd
|
4 |
-
from collections import defaultdict, Counter
|
5 |
import altair as alt
|
6 |
import panel as pn
|
7 |
|
8 |
-
|
9 |
-
def choices_to_df(choices, hue):
|
10 |
-
df = pd.DataFrame(choices, columns=['choices'])
|
11 |
-
df['hue'] = hue
|
12 |
-
df['hue'] = df['hue'].astype(str)
|
13 |
-
return df
|
14 |
-
|
15 |
-
def arrange_data():
|
16 |
-
|
17 |
-
# Human Data
|
18 |
-
|
19 |
-
df = pd.read_csv('Project/2_scientific/ChatGPT-Behavioral-main/data/bomb_risk.csv')
|
20 |
-
df = df[df['Role'] == 'player']
|
21 |
-
df = df[df['gameType'] == 'bomb_risk']
|
22 |
-
df.sort_values(by=['UserID', 'Round'])
|
23 |
-
|
24 |
-
prefix_to_choices_human = defaultdict(list)
|
25 |
-
prefix_to_IPW = defaultdict(list)
|
26 |
-
prev_user = None
|
27 |
-
prev_move = None
|
28 |
-
prefix = ''
|
29 |
-
bad_user = False
|
30 |
-
for _, row in df.iterrows():
|
31 |
-
if bad_user: continue
|
32 |
-
if row['UserID'] != prev_user:
|
33 |
-
prev_user = row['UserID']
|
34 |
-
prefix = ''
|
35 |
-
bad_user = False
|
36 |
-
|
37 |
-
move = row['move']
|
38 |
-
if move < 0 or move > 100:
|
39 |
-
bad_users = True
|
40 |
-
continue
|
41 |
-
prefix_to_choices_human[prefix].append(move)
|
42 |
-
|
43 |
-
if len(prefix) == 0:
|
44 |
-
prefix_to_IPW[prefix].append(1)
|
45 |
-
elif prefix[-1] == '1':
|
46 |
-
prev_move = min(prev_move, 98)
|
47 |
-
prefix_to_IPW[prefix].append(1./(100 - prev_move))
|
48 |
-
elif prefix[-1] == '0':
|
49 |
-
prev_move = max(prev_move, 1)
|
50 |
-
prefix_to_IPW[prefix].append(1./(prev_move))
|
51 |
-
else: assert False
|
52 |
-
|
53 |
-
prev_move = move
|
54 |
-
|
55 |
-
prefix += '1' if row['roundResult'] == 'SAFE' else '0'
|
56 |
-
|
57 |
-
|
58 |
-
# Model Data
|
59 |
-
|
60 |
-
prefix_to_choices_model = defaultdict(lambda : defaultdict(list))
|
61 |
-
for model in ['ChatGPT-4', 'ChatGPT-3']:
|
62 |
-
if model == 'ChatGPT-4':
|
63 |
-
file_names = [
|
64 |
-
'bomb_gpt4_2023_05_15-12_13_51_AM.json'
|
65 |
-
]
|
66 |
-
elif model == 'ChatGPT-3':
|
67 |
-
file_names = [
|
68 |
-
'bomb_turbo_2023_05_14-10_45_50_PM.json'
|
69 |
-
]
|
70 |
-
|
71 |
-
choices = []
|
72 |
-
scenarios = []
|
73 |
-
for file_name in file_names:
|
74 |
-
with open(os.path.join('Project/2_scientific/ChatGPT-Behavioral-main/records', file_name), 'r') as f:
|
75 |
-
records = json.load(f)
|
76 |
-
choices += records['choices']
|
77 |
-
scenarios += records['scenarios']
|
78 |
-
|
79 |
-
assert len(scenarios) == len(choices)
|
80 |
-
print('loaded %i valid records' % len(scenarios))
|
81 |
-
|
82 |
-
prefix_to_choice = defaultdict(list)
|
83 |
-
prefix_to_result = defaultdict(list)
|
84 |
-
prefix_to_pattern = defaultdict(Counter)
|
85 |
-
wrong_sum = 0
|
86 |
-
for scenarios_tmp, choices_tmp in zip(scenarios, choices):
|
87 |
-
|
88 |
-
result = 0
|
89 |
-
for i, scenario in enumerate(scenarios_tmp):
|
90 |
-
prefix = tuple(scenarios_tmp[:i])
|
91 |
-
prefix = ''.join([str(x) for x in prefix])
|
92 |
-
choice = choices_tmp[i]
|
93 |
-
|
94 |
-
prefix_to_choice[prefix].append(choice)
|
95 |
-
prefix_to_pattern[prefix][tuple(choices_tmp[:-1])] += 1
|
96 |
-
|
97 |
-
prefix = tuple(scenarios_tmp[:i+1])
|
98 |
-
if scenario == 1:
|
99 |
-
result += choice
|
100 |
-
prefix_to_result[prefix].append(result)
|
101 |
-
|
102 |
-
print('# of wrong sum:', wrong_sum)
|
103 |
-
print('# of correct sum:', len(scenarios) - wrong_sum)
|
104 |
-
|
105 |
-
prefix_to_choices_model[model] = prefix_to_choice
|
106 |
-
|
107 |
-
|
108 |
-
# Arrange Data
|
109 |
-
|
110 |
-
round_dict = {'': [1, -1, -1],
|
111 |
-
'0': [2, 0, -1],
|
112 |
-
'1': [2, 1, -1],
|
113 |
-
'00': [3, 0, 0],
|
114 |
-
'01': [3, 0, 1],
|
115 |
-
'10': [3, 1, 0],
|
116 |
-
'11': [3, 1, 1]}
|
117 |
-
|
118 |
-
df_bomb_all = pd.DataFrame()
|
119 |
-
|
120 |
-
for prefix in round_dict:
|
121 |
-
|
122 |
-
df_bomb_human = choices_to_df(prefix_to_choices_human[prefix], hue='Human')
|
123 |
-
df_bomb_human['weight'] = prefix_to_IPW[prefix]
|
124 |
-
|
125 |
-
df_bomb_models = pd.concat([choices_to_df(
|
126 |
-
prefix_to_choices_model[model][prefix], hue=model
|
127 |
-
) for model in prefix_to_choices_model]
|
128 |
-
)
|
129 |
-
df_bomb_models['weight'] = 1
|
130 |
-
|
131 |
-
df_bomb_temp = pd.concat([df_bomb_human, df_bomb_models])
|
132 |
-
df_bomb_temp['prefix'] = prefix
|
133 |
-
|
134 |
-
df_bomb_all = pd.concat([df_bomb_all, df_bomb_temp])
|
135 |
-
|
136 |
-
df_density = df_bomb_all.groupby(['hue', 'prefix'])['choices'].value_counts(normalize=True).unstack(fill_value=0).stack().reset_index()
|
137 |
-
df_density = df_density.rename(columns={'hue': 'Subject', 'choices': 'Boxes', 0: 'Density'})
|
138 |
-
df_density['Round'] = df_density['prefix'].apply(lambda x: round_dict[x][0])
|
139 |
-
|
140 |
-
return df_density
|
141 |
-
|
142 |
-
|
143 |
-
df_density = arrange_data()
|
144 |
alt.data_transformers.disable_max_rows()
|
145 |
|
146 |
# Enable Panel extensions
|
@@ -161,7 +23,7 @@ def create_plot(bomb_1, bomb_2):
|
|
161 |
range_ = ['#009FB7', '#FED766', '#FE4A49']
|
162 |
|
163 |
plot = alt.Chart(df_density).transform_filter(
|
164 |
-
(alt.datum.prefix == '') | (alt.datum.prefix == str(bomb_1)) | (alt.datum.prefix == str(bomb_1) + str(bomb_2))
|
165 |
).mark_bar(opacity=0.5).encode(
|
166 |
x=alt.X('Boxes:Q',
|
167 |
bin=alt.Bin(maxbins=10),
|
|
|
|
|
|
|
1 |
import pandas as pd
|
|
|
2 |
import altair as alt
|
3 |
import panel as pn
|
4 |
|
5 |
+
df_density = pd.read_csv('Project/2_scientific/bomb_risk_density.csv', dtype={'prefix': str})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
alt.data_transformers.disable_max_rows()
|
7 |
|
8 |
# Enable Panel extensions
|
|
|
23 |
range_ = ['#009FB7', '#FED766', '#FE4A49']
|
24 |
|
25 |
plot = alt.Chart(df_density).transform_filter(
|
26 |
+
(alt.datum.prefix == '-1') | (alt.datum.prefix == str(bomb_1)) | (alt.datum.prefix == str(bomb_1) + str(bomb_2))
|
27 |
).mark_bar(opacity=0.5).encode(
|
28 |
x=alt.X('Boxes:Q',
|
29 |
bin=alt.Bin(maxbins=10),
|