-
Notifications
You must be signed in to change notification settings - Fork 0
/
sims.py
210 lines (151 loc) · 5.78 KB
/
sims.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
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
207
208
#!/usr/bin/env python
from __future__ import division
import hddm
import pandas as pd
import numpy as np
def sim_exp(pdict, ntrials=500, p_outlier=None, pfast=0, pslow=0, nsims_per_sub=1):
"""
Simulates a dataset for each subject(i), for every condition(j) in pdict of
size n=ntrials*exp_proportion. <-- Makes simulated trial count for each condition
proportional to the number of empirical observations (unbalanced face/house obs across cues).
Also, does light parsing and reformatting on output dataframe
RETURNS: 2
*sim_df (pandas DataFrame): columns for condition name, sub id, cue, stim,
response (1: upperbound, 0: lowerbound),
accuracy (1: cor, 0: err), and response time
*param_dict (dict): dataframe of parameters used
(only needed if noise/outliers
simulated)
"""
param_dict=dict()
for i, sub in enumerate(pdict):
for num, cond in enumerate(pdict[sub].keys()):
#check if cue predicts image (i.e. 90F --> face)
if cond.split('_')[0][-1].lower()==cond.split('_')[1][0].lower():
#make total trials for this simulated condition
#proportional to number of experimental
#trials in this condition
perc=int(cond[1:3])*.01
else:
perc=1-(int(cond[1:3])*.01)
exptrials=perc*ntrials
if 'p' in pdict[sub][cond].keys():
pfast=pdict[sub][cond]['p']
pslow=pdict[sub][cond]['p']
nfast=int((exptrials/2)*pfast)
nslow=int((exptrials/2)*pslow)
data, parameters = hddm.generate.gen_rand_data(params={cond:pdict[sub][cond]}, subjs=nsims_per_sub,
n_fast_outliers=nfast, n_slow_outliers=nslow, size=exptrials)
data.subj_idx[:]=sub
if i==0 and num==0:
simdf=data
else:
simdf=pd.concat([simdf, data], ignore_index=True)
param_dict[i]=parameters
simdf=ref_simdf(simdf)
return simdf, param_dict
def sim_and_concat(params, nsims=25, ntrials=100):
simdf_list=[]
for i in range(nsims):
simdf, params_used=sim_exp(pdict=params, ntrials=ntrials)
simdf['sim_num']=simdf['subj_idx'].copy()
simdf.sim_num[:]=i
simdf_list.append(simdf)
all_simdfs=pd.concat(simdf_list)
return all_simdfs
def sim_exp_subj(pdict, ntrials=500, p_outlier=None, pfast=0, pslow=0, nsims_per_sub=1):
"""
Simulates a dataset for a single subject, for every condition in pdict of
size n=ntrials*exp_proportion. <-- Makes simulated trial count for each condition
proportional to the number of empirical observations (unbalanced face/house obs across cues).
Also, does light parsing and reformatting on output dataframe.
RETURNS: 2
*sim_df (pandas DataFrame): columns for condition name, sub id, cue, stim,
response (1: upperbound, 0: lowerbound),
accuracy (1: cor, 0: err), and response time
*param_dict (dict): dataframe of parameters used
(only needed if noise/outliers
simulated)
"""
param_dict=dict()
for i, cond in enumerate(pdict.keys()):
#check if cue predicts image (i.e. 90F --> face)
if cond.split('_')[0][-1].lower()==cond.split('_')[1][0].lower():
#make total trials for this simulated condition
#proportional to number of experimental
#trials in this condition
perc=int(cond[1:3])*.01
else:
perc=1-(int(cond[1:3])*.01)
exptrials=perc*ntrials
if 'p' in pdict[cond].keys():
pfast=pdict[cond]['p']
pslow=pdict[cond]['p']
nfast=int((exptrials/2)*pfast)
nslow=int((exptrials/2)*pslow)
data, parameters = hddm.generate.gen_rand_data(params={cond:pdict[cond]}, subjs=nsims_per_sub,
n_fast_outliers=nfast, n_slow_outliers=nslow, size=exptrials)
if i==0:
simdf=data
else:
simdf=pd.concat([simdf, data], ignore_index=True)
param_dict[i]=parameters
simdf=ref_simdf(simdf)
return simdf, param_dict
def sim_subs(pdict, ntrials=500, p_outlier=None, pfast=0, pslow=0, nsims_per_sub=1):
param_dict=dict()
nfast=int((ntrials)*pfast)
nslow=int((ntrials)*pslow)
for i, x in enumerate(pdict):
data, parameters = hddm.generate.gen_rand_data(params=pdict[x], subjs=nsims_per_sub,
n_fast_outliers=nfast, n_slow_outliers=nslow, size=ntrials)
data.subj_idx[:]=x
if i==0:
simdf=data
else:
simdf=pd.concat([simdf, data], ignore_index=True)
param_dict[i]=parameters
simdf=ref_simdf(simdf)
return simdf, param_dict
def sim_noise_sep(pdict, ntrials=100, nsims=10, simfx=sim_exp, pfast=0, pslow=0, nsims_per_sub=1):
for i in range(nsims):
p68=pdict[0]
p69=pdict[1]
simdf68, params_used=simfx(pdict=p68, ntrials=ntrials, pfast=pfast, pslow=pslow, nsims_per_sub=nsims_per_sub)
simdf69, params_used=simfx(pdict=p69, ntrials=ntrials, pfast=pfast, pslow=pslow, nsims_per_sub=nsims_per_sub)
simdf68['noise']=['68']*len(simdf68)
simdf69['noise']=['69']*len(simdf69)
if i==0:
simdf=pd.concat([simdf68, simdf69], ignore_index=True)
else:
simdf=pd.concat([simdf, simdf68], ignore_index=True)
simdf=pd.concat([simdf, simdf69], ignore_index=True)
return simdf
def sim_grp(pdict, ntrials=5000, pfast=0.00, pslow=0.00, nsims_per_sub=25, subj_noise=0.1):
param_dict=dict()
nfast=int((ntrials)*pfast)
nslow=int((ntrials)*pslow)
simdf, parameters = hddm.generate.gen_rand_data(params=pdict, subjs=nsims_per_sub,
n_fast_outliers=nfast, n_slow_outliers=nslow, size=ntrials, subj_noise=subj_noise)
simdf=ref_simdf(simdf)
return simdf, parameters
def ref_simdf(simdf):
#add separate cols for
#stim and cue names
sim_cue=list()
sim_img=list()
for cond in simdf['condition']:
if '_' in cond:
img=cond.split('_')[1]
cue=cond.split('_')[0]
sim_cue.append(cue)
sim_img.append(img)
simdf['stim']=sim_img
simdf['cue']=sim_cue
#add accuracy column to simdf
simdf['acc']=simdf['response'].values
simdf.ix[(simdf['stim']=='house') & (simdf['response']==0), 'acc']=1
simdf.ix[(simdf['stim']=='house') & (simdf['response']==1), 'acc']=0
return simdf
if __name__ == "__main__":
main()