-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_stats.py
More file actions
337 lines (226 loc) · 9.46 KB
/
Copy pathplot_stats.py
File metadata and controls
337 lines (226 loc) · 9.46 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#plot_stats.py
from __future__ import division
import numpy as np
import pandas as pd
import os
import time
import sys
#sys.path.append('D:/GoogleDrive/02 PROTOCOLS/Python')
from numerical import downsample
from bokeh.io import hplot, output_notebook, gridplot
from bokeh.client import push_session
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc, show
from bokeh.models import Span
from bokeh.charts import Bar
usage = '''bokeh serve
plot_stats.py ID DATAPATH
'''
TODO = '''
1. Use a rolling average for fractions correct and rewarded
display a noticable marker at the 10th point
2. fix the line weights
3.
'''
df = pd.DataFrame([])
df_summary = pd.DataFrame([])
# ## 1. Read the data
ID = sys.argv[1]
DATAPATH = sys.argv[2]
infile = ['/'.join((DATAPATH,f)) for f in os.listdir(DATAPATH)
if ID in f
if f.endswith('.csv')][-1]
first_loop = True
last_mod = time.ctime(os.path.getmtime(infile))
def read_data(df = pd.DataFrame([])):
if df.empty:
df = pd.read_csv(infile)
else:
last_line = df.shape[0]
df = df.append(pd.read_csv(infile,
names = df.columns,
skiprows = last_line))
if df.shape[0] > last_line + 10:
return df, False
if 'time' in df.columns:
df = df.drop_duplicates(subset = 'time')
if 'OFF[0]' in df.columns:
df = df.dropna(subset = ['OFF[0]'])
df['trial_num'] = np.arange(0, df.shape[0])
df.index = df.trial_num
df['reward'] = df['WaterPort[0]'] + df['WaterPort[1]']
return df, True
def update():
global df
global last_mod
global first_loop
mod_time = time.ctime(os.path.getmtime(infile))
if (mod_time > last_mod) or first_loop:
last_mod = mod_time
print mod_time, '\r',
else:
print last_mod, '\r',
time.sleep(10)
return
df, changed = read_data(df)
if not changed:
time.sleep(10)
return
# if the size of the frame is not divisible
# by 10 ignore the excess.
excess = df.shape[0] % 10
df = df[:-excess]
df_raw = df.copy()
# Given the animal did respond:
# One of the ports will have recorded a count,
# response = (response on port 0) OR (response on port 1)
response = (df['count[0]'] > 0).values | (df['count[1]'] > 0).values
# Lets care only about the ones where the animal made a move
df = df[response]
df = df[df.minlickCount > 0]
reward = df.reward.astype(bool).values
reward_L = (df.rewardCond == 'L').values
reward_R = (df.rewardCond == 'R').values
response_L = (df.response.str.upper() == 'L').values
response_R = (df.response.str.upper() == 'R').values
correct = (df.rewardCond == df.response).values
wrong = (df.rewardCond != df.response).values
#compute the values:
total_trials = np.arange(df_raw.shape[0])
total_responses = (df_raw.response == '-').values
total_trials = np.arange(0,total_trials.shape[0], 10)
total_responses = downsample(total_responses, 10, np.nansum)/10
trials = np.arange(df.shape[0])
trials = downsample(trials, 10, np.nanmax)
trials_L = downsample(reward_L, 10, np.nansum)
trials_R = downsample(reward_R, 10, np.nansum)
N_rewards = downsample(reward, 10, np.nansum)
N_rewards_L = downsample(reward & reward_L, 10, np.nansum)
N_rewards_R = downsample(reward & reward_R, 10, np.nansum)
frac = N_rewards / 10
frac_L = N_rewards_L / trials_L
frac_R = N_rewards_R / trials_R
p_correct = downsample(correct, 10, np.nansum) / 10
p_correct_L = downsample(correct & response_L, 10, np.nansum) / trials_L
p_correct_R = downsample(correct & response_R, 10, np.nansum) / trials_R
delta = (( downsample(response_R, 10, np.nansum)
- downsample(response_L, 10, np.nansum)) / 10)
p1_responses.data_source.data = {'x' : total_trials,
'y' : total_responses
}
p2_frac.data_source.data = {'x' : trials,
'y' : frac}
p2_frac_L.data_source.data = {'x' : trials,
'y' : frac_L
}
p2_frac_R.data_source.data = {'x': trials,
'y': frac_R
}
p3_cor.data_source.data = { 'x': trials,
'y': p_correct
}
p3_cor_L.data_source.data = {'x' : trials,
'y' : p_correct_L
}
p3_cor_R.data_source.data = {'x' :trials,
'y' :p_correct_R
}
p4_delta.data_source.data = {'x': trials,
'y' : delta
}
p4_deltam.data_source.data = {'x' :trials,
'y' :delta
}
##generate_plots##
goodline = Span(location = 0.75, dimension = 'width', line_dash = [1,1], line_color = 'tomato', line_width = 2)
chanceline = Span(location = 0.5, dimension = 'width', line_dash = [1,1])
u_chanceline = Span(location = -.5, dimension = 'width', line_dash = [4,8])
l_chanceline = Span(location = .5, dimension = 'width', line_dash = [4,8])
z_line = Span(location = 0, dimension = 'width')
##plot 1
p1 = figure(height = 200,
title='no responses',
y_range=(-.05,1.05),
x_axis_label = 'trial',
y_axis_label = 'fraction'
)
##plot 2
p2 = figure(title="fraction of trials where reward given",
y_range=(-.05,1.05),
x_range = p1.x_range,
x_axis_label = 'trial',
y_axis_label = 'fraction'
)
p3 = figure(title="fraction of trials 'correct'",
y_range= p2.y_range,
x_range = p1.x_range,
x_axis_label = 'trial',
y_axis_label = 'fraction'
)
p4 = figure(title="BIAS",
height = 200,
y_range=(-1.05,1.05),
x_range = p1.x_range,
x_axis_label = 'trial',
y_axis_label = 'Delta'
)
p2.renderers.extend([u_chanceline, goodline, chanceline])
p3.renderers.extend([u_chanceline, goodline, chanceline])
p4.renderers.extend([u_chanceline, z_line, l_chanceline])
p = gridplot([[p2, p3],
[p1, p4]])
df, changed = read_data(df)
df_raw = df.copy()
# Given the animal did respond:
# One of the ports will have recorded a count,
# response = (response on port 0) OR (response on port 1)
response = (df['count[0]'] > 0).values | (df['count[1]'] > 0).values
# Lets care only about the ones where the animal made a move
df = df[response]
df = df[df.minlickCount > 0]
reward = df.reward.astype(bool).values
reward_L = (df.rewardCond == 'L').values
reward_R = (df.rewardCond == 'R').values
response_L = (df.response.str.upper() == 'L').values
response_R = (df.response.str.upper() == 'R').values
correct = (df.rewardCond == df.response).values
wrong = (df.rewardCond != df.response).values
#compute the values:
total_trials = np.arange(df_raw.shape[0])
total_responses = (df_raw.response == '-').values
total_trials = np.arange(0,total_trials.shape[0], 10)
total_responses = downsample(total_responses, 10, np.nansum)/10
trials = np.arange(df.shape[0])
trials = downsample(trials, 10, np.nanmax)
trials_L = downsample(reward_L, 10, np.nansum)
trials_R = downsample(reward_R, 10, np.nansum)
N_rewards = downsample(reward, 10, np.nansum)
N_rewards_L = downsample(reward & reward_L, 10, np.nansum)
N_rewards_R = downsample(reward & reward_R, 10, np.nansum)
frac = N_rewards / 10
frac_L = N_rewards_L / trials_L
frac_R = N_rewards_R / trials_R
p_correct = downsample(correct, 10, np.nansum) / 10
p_correct_L = downsample(correct & response_L, 10, np.nansum) / trials_L
p_correct_R = downsample(correct & response_R, 10, np.nansum) / trials_R
delta = (( downsample(response_R, 10, np.nansum)
- downsample(response_L, 10, np.nansum)) / 10)
p1_responses = p1.line(total_trials, total_responses,
line_color = 'red',
line_dash = [4,4])
p2_frac = p2.line(trials, frac, line_color = 'black', line_width = 5)
p2_frac_L = p2.line(trials, frac_L, line_color = 'red', line_dash = [4,1,2,1])
p2_frac_R = p2.line(trials, frac_R, line_color = 'blue', line_dash = [4,1,2,1])
p3_cor = p3.line(trials, p_correct, line_color = 'black', line_width = 5)
p3_cor_L = p3.line(trials, p_correct_L, line_color = 'red', line_dash = [4,1,2,1])
p3_cor_R = p3.line(trials, p_correct_R, line_color = 'blue', line_dash = [4,1,2,1])
p4_deltam = p4.circle(trials, delta, size = 4)
p4_delta = p4.line(trials, delta)
p4.text(1, 0.5, text = ['Right'], text_color = 'blue')
p4.text(1, -0.5, text = ['Left'], text_color = 'Red')
#-----------------------------------------------------------------#
# open a session to keep our local document in sync with server
session = push_session(curdoc())
curdoc().add_periodic_callback(update, 50)
session.show() # open the document in a browser
session.loop_until_closed() # run forever