-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_modelling_analyses.py
179 lines (133 loc) · 5.6 KB
/
run_modelling_analyses.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
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
import os
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.ion()
import arviz as az
mpl.rcParams['axes.linewidth'] = 0.5
mpl.rcParams['xtick.major.width'] = 0.5
mpl.rcParams['ytick.major.width'] = 0.5
# custom modules
import beh_analysis
import plots
import modelling
## set up figures etc.
save_figs = 'paper'
# control whether we should do sampling or not. Note that sampling can take a while, and
# also storing traces takes up quite a bit of space (~3GB).
do_sampling = False
#traces_path = '../traces'
traces_path = '/project/3018045.01/traces-4paper'
try:
os.makedirs(traces_path)
except:
pass
# output dir for summary stats and figures
output_dir = '../results/modelling'
try:
os.makedirs(output_dir)
except:
pass
# this is optional if you want publication-quality fonts
# if save_figs == 'paper':
# plots.set_font(font='Helvetica Neue LT Std', stretch='condensed', size=14)
# else:
# plots.set_font(font='Muli', size=16)
fig_counter = 0
def savefig(fig, title):
global fig_counter
fig_counter += 1
fig.tight_layout()
if save_figs == 'paper':
fig.savefig('{}/{:03d}-{}.pdf'.format(output_dir, fig_counter, title))
elif save_figs == 'png':
fig.savefig('{}/{:03d}-{}.png'.format(output_dir, fig_counter, title), dpi=300)
outfile = open(output_dir + '/statresults-modelling.txt', 'w', buffering=1)
pd.set_option('display.max_columns', 12)
## load data
df_all_cd = pd.read_pickle('../data/exp1-cd.pkl.gz')
df_all_2afc = pd.read_pickle('../data/exp2-2afc.pkl.gz')
## reject outliers
df_all_cd = beh_analysis.reject_outliers(df_all_cd,
depvars=['log_rt', 'log_loc_error', 'log_loc_rt'],
group_columns=['subnum', 'item'])
df_all_2afc = beh_analysis.reject_outliers(df_all_2afc,
depvars=['log_correct_rt', 'correct'],
group_columns=['subnum', 'item'])
## cd: sampling
if do_sampling:
df = df_all_cd.dropna()
for depvar in ('log_rt', 'log_loc_error'):
model = modelling.build_bambi_model(df, depvar,
family='gaussian', factors='1 + con_incon')
results = modelling.sample_and_get_results(model)
az.to_netcdf(results, '{}/cd-{}.az'.format(traces_path, depvar))
## 2afc: sampling
if do_sampling:
for depvar in ('correct', 'log_correct_rt'):
if depvar == 'log_correct_rt':
df = df_all_2afc.dropna()
family = 'gaussian'
else:
df = df_all_2afc
family = 'bernoulli'
model = modelling.build_bambi_model(df, depvar,
family=family, factors='1 + con_incon * probe_key_other')
results = modelling.sample_and_get_results(model)
az.to_netcdf(results, '{}/2afc-{}.az'.format(traces_path, depvar))
## both tasks: define variables and label them
# variables to inspect/plot (mapping from name to label)
variables_2afc = {'Intercept': 'Intercept',
'con_incon': 'Congruency',
'probe_key_other': 'Probe',
'con_incon:probe_key_other': 'Congruency X Probe',
'1|subnum_sigma': '$\sigma_{Subject}$(1)',
'con_incon|subnum_sigma': '$\sigma_{Subject}$(Congruency)',
'probe_key_other|subnum_sigma': '$\sigma_{Subject}$(Probe)',
'con_incon:probe_key_other|subnum_sigma': '$\sigma_{Subject}$(Congruency X Probe)',
'1|item_sigma': '$\sigma_{Item}$(1)',
'con_incon|item_sigma': '$\sigma_{Item}$(Congruency)',
'probe_key_other|item_sigma': '$\sigma_{Item}$(Probe)',
'con_incon:probe_key_other|item_sigma': '$\sigma_{Item}$(Congruency X Probe)'}
variables_cd = {k: v for k, v in variables_2afc.items() if not 'probe' in k}
## cd: stats, plotting
has_refline = ['con_incon']
for depvar in ('log_rt', 'log_loc_error'):
results = az.from_netcdf('{}/cd-{}.az'.format(traces_path, depvar))
fig, allax = plt.subplots(3, 2, figsize=(5, 6))
prop_above = plots.plot_posterior(results, allax.ravel(), variables_cd, has_refline)
savefig(fig, 'model-cd-{}'.format(depvar))
print('CD, {}'.format(depvar), file=outfile)
summary = az.summary(results, kind='stats', var_names=list(variables_cd.keys()),
round_to=6)
print(summary.to_string() + '\n', file=outfile)
print('p(<> 0):\n' + str(prop_above) + '\n', file=outfile)
## 2afc: stats, plotting
# variables to inspect/plot (mapping from name to label)
variables_2afc = {'Intercept': 'Intercept',
'con_incon': 'Congruency',
'probe_key_other': 'Probe',
'con_incon:probe_key_other': 'Congruency X Probe',
'1|subnum_sigma': '$\sigma_{Subject}$(1)',
'con_incon|subnum_sigma': '$\sigma_{Subject}$(Congruency)',
'probe_key_other|subnum_sigma': '$\sigma_{Subject}$(Probe)',
'con_incon:probe_key_other|subnum_sigma': '$\sigma_{Subject}$(Congruency X Probe)',
'1|item_sigma': '$\sigma_{Item}$(1)',
'con_incon|item_sigma': '$\sigma_{Item}$(Congruency)',
'probe_key_other|item_sigma': '$\sigma_{Item}$(Probe)',
'con_incon:probe_key_other|item_sigma': '$\sigma_{Item}$(Congruency X Probe)'}
has_refline = ['con_incon', 'probe_key_other', 'con_incon:probe_key_other']
for depvar in ('correct', 'log_correct_rt'):
results = az.from_netcdf('{}/2afc-{}.az'.format(traces_path, depvar))
fig, allax = plt.subplots(3, 4, figsize=(9, 6))
prop_above = plots.plot_posterior(results, allax.ravel(), variables_2afc, has_refline)
savefig(fig, 'model-2afc-{}'.format(depvar))
print('2AFC, {}'.format(depvar), file=outfile)
summary = az.summary(results, kind='stats', var_names=list(variables_2afc.keys()),
round_to=6)
print(summary.to_string() + '\n', file=outfile)
print('p(<> 0):\n' + str(prop_above) + '\n', file=outfile)
## wrap up
outfile.close()
plt.close('all')