-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvis.py
145 lines (125 loc) · 5.25 KB
/
vis.py
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
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
def plot_actions(cue=0):
mpl.rcParams['axes.labelsize'] = 'large'
d_map = {3:1, 8:2, 14:3, 23:4}
df = pd.read_pickle('data.pkl').reset_index()
df = df.loc[df['cue'] == cue]
g = sns.FacetGrid(df, col='subject',
col_wrap=6, size=1.5, ylim=(0, 5), aspect=1.5)
g.map(plt.plot, 'action')
g.set(xticks=[], yticks=[0,1,2,3], yticklabels=['3', '8', '14', '23'])
g.set(ylim=(-0.5, 4))
g.set_ylabels('choice')
g.fig.tight_layout()
g.fig.subplots_adjust(top=0.93)
subjects = df['subject'].unique()
for ax, subject in zip(g.axes, subjects):
df_subject = df.loc[df['subject'] == subject]
df_subject.reset_index(inplace=True)
df_wins = df_subject.loc[df_subject['reward'] > 0]
df_lose = df_subject.loc[df_subject['reward'] < 0]
pos_win = df_wins.loc[df_wins['subject'] == subject].index
pos_lose = df_lose.loc[df_lose['subject'] == subject].index
ax.eventplot(pos_win, lineoffsets=3.5, linelength=0.75,
linewidths=0.4)
ax.eventplot(pos_lose, lineoffsets=3.5, linelength=0.75,
color='r', linewidths=0.4)
plt.tight_layout()
plt.savefig('actions_0.pdf')
plt.show()
globals().update(locals())
def plot_simulation_run():
plt.style.use('seaborn-white')
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['xtick.labelsize'] = 'large'
mpl.rcParams['ytick.labelsize'] = 'large'
mpl.rcParams['axes.labelsize'] = 'large'
df = pd.read_csv('softmax_experiment.csv')
df_reward = df[df['context'] == 'reward'].reset_index()
df_punishment = df[df['context'] == 'punishment'].reset_index()
xmax = min(len(df_reward['action']), len(df_punishment['action'])) - 1
f, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(df_reward['action'])
ax1.set_xlabel('trial')
ax1.set_ylabel('action')
ax1.set_yticks([3, 8, 14, 23])
ax1.set_ylim(ymin=2, ymax=28)
ax1.set_xlim(xmax=xmax)
ax1.set_title('Win context')
df_wins = df_reward.loc[df_reward['reward'] > 0]
df_lose = df_reward.loc[df_reward['reward'] < 0]
pos_win = df_wins.index
pos_lose = df_lose.index
ax1.eventplot(pos_win, lineoffsets=25.5, linelength=4,
linewidths=2)
ax1.eventplot(pos_lose, lineoffsets=25.5, linelength=4,
color='r', linewidths=2)
ax2.plot(df_punishment['action'])
ax2.set_xlabel('trial')
ax2.set_ylabel('action')
ax2.set_yticks([3, 8, 14, 23])
ax2.set_ylim(ymin=2, ymax=28)
ax2.set_xlim(xmax=xmax)
ax2.set_title('Lose context')
df_wins = df_punishment.loc[df_punishment['reward'] > 0]
df_lose = df_punishment.loc[df_punishment['reward'] < 0]
pos_win = df_wins.index
pos_lose = df_lose.index
ax2.eventplot(pos_win, lineoffsets=25.5, linelength=4,
linewidths=2)
ax2.eventplot(pos_lose, lineoffsets=25.5, linelength=4,
color='r', linewidths=2)
plt.tight_layout()
plt.savefig('softmax_experiment.pdf')
plt.show()
def plot_fit_data():
plt.style.use('seaborn-white')
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['xtick.labelsize'] = 'large'
mpl.rcParams['ytick.labelsize'] = 'large'
mpl.rcParams['axes.labelsize'] = 'large'
df = pd.read_pickle('model.pkl') # model.pkl generated by
# ml.fit_behavioral_data()
alpha, beta = df['alpha_0'], df['beta_0']
errbar_th = 0.3
xerr, yerr = df['se_alpha_0'].as_matrix(), df['se_beta_0'].as_matrix()
err_mask = (xerr < errbar_th) & (yerr < errbar_th)
alpha_poor = np.ma.masked_array(alpha, err_mask)
beta_poor = np.ma.masked_array(beta, err_mask)
alpha_good = np.ma.masked_array(alpha, ~err_mask)
beta_good = np.ma.masked_array(beta, ~err_mask)
xerr[~err_mask] = 0
yerr[~err_mask] = 0
plt.errorbar(alpha, beta, xerr=xerr, yerr=yerr, marker='^', ls='none', ms=0,
mfc='b', ecolor='b', alpha=0.2, label='')
plt.scatter(alpha_good, beta_good, 100, marker='^', label='win good fit')
plt.scatter(alpha_poor, beta_poor, 100, marker='v', label='win poor fit')
xerr, yerr = df['se_alpha_1'].as_matrix(), df['se_beta_1'].as_matrix()
alpha, beta = df['alpha_1'], df['beta_1']
err_mask = (xerr < errbar_th) & (yerr < errbar_th)
alpha_poor = np.ma.masked_array(alpha, err_mask)
beta_poor = np.ma.masked_array(beta, err_mask)
alpha_good = np.ma.masked_array(alpha, ~err_mask)
beta_good = np.ma.masked_array(beta, ~err_mask)
xerr[~err_mask] = 0
yerr[~err_mask] = 0
plt.errorbar(alpha, beta, xerr=xerr, yerr=yerr, marker='^', ls='none', ms=0,
mfc='r', ecolor='r', alpha=0.2, label='')
plt.scatter(alpha_good, beta_good, 100, 'r', marker='<', label='lose good fit')
plt.scatter(alpha_poor, beta_poor, 100, 'r', marker='>', label='lose poor fit')
plt.legend()
plt.xlim(-0.05, 1.05)
plt.ylim(-0.05, 2.05)
plt.xlabel(r'$\alpha_c$', fontsize=18)
plt.ylabel(r'$\beta_c$', fontsize=18)
plt.tight_layout()
plt.savefig('experimental_fit.pdf')
plt.show()
globals().update(locals())
if __name__ == '__main__':
plt.close('all')
plot_actions()