aind-dynamic-foraging-basic-analysis 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aind_dynamic_foraging_basic_analysis/__init__.py +1 -1
- aind_dynamic_foraging_basic_analysis/lick_analysis.py +622 -0
- {aind_dynamic_foraging_basic_analysis-0.2.0.dist-info → aind_dynamic_foraging_basic_analysis-0.2.1.dist-info}/METADATA +6 -1
- aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/RECORD +10 -0
- {aind_dynamic_foraging_basic_analysis-0.2.0.dist-info → aind_dynamic_foraging_basic_analysis-0.2.1.dist-info}/WHEEL +1 -1
- aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/RECORD +0 -9
- {aind_dynamic_foraging_basic_analysis-0.2.0.dist-info → aind_dynamic_foraging_basic_analysis-0.2.1.dist-info}/LICENSE +0 -0
- {aind_dynamic_foraging_basic_analysis-0.2.0.dist-info → aind_dynamic_foraging_basic_analysis-0.2.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
"""Load packages."""
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import matplotlib.gridspec as gridspec
|
|
5
|
+
from aind_ephys_utils import align
|
|
6
|
+
from pynwb import NWBHDF5IO
|
|
7
|
+
from scipy.stats import norm
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def load_nwb(nwb_file):
|
|
11
|
+
"""Load NWB file."""
|
|
12
|
+
io = NWBHDF5IO(nwb_file, mode="r")
|
|
13
|
+
nwb = io.read()
|
|
14
|
+
return nwb
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def plot_lick_analysis(nwb):
|
|
18
|
+
"""Plot lick distributions."""
|
|
19
|
+
tbl_trials = nwb.trials.to_dataframe()
|
|
20
|
+
gs = gridspec.GridSpec(
|
|
21
|
+
2,
|
|
22
|
+
6,
|
|
23
|
+
width_ratios=np.ones(6).tolist(),
|
|
24
|
+
height_ratios=[1, 2],
|
|
25
|
+
)
|
|
26
|
+
fig = plt.figure(figsize=(15, 8))
|
|
27
|
+
plt.subplots_adjust(hspace=0.5, wspace=0.5)
|
|
28
|
+
|
|
29
|
+
left_licks = nwb.acquisition["left_lick_time"].timestamps[:]
|
|
30
|
+
right_licks = nwb.acquisition["right_lick_time"].timestamps[:]
|
|
31
|
+
all_licks = np.sort(np.concatenate((right_licks, left_licks)))
|
|
32
|
+
sort_ind = np.argsort(np.concatenate((right_licks, left_licks)))
|
|
33
|
+
all_licks_id = np.concatenate(
|
|
34
|
+
(np.ones_like(right_licks), np.zeros_like(left_licks))
|
|
35
|
+
)
|
|
36
|
+
all_licks_id = all_licks_id[sort_ind]
|
|
37
|
+
all_lick_diffs = np.diff(all_licks)
|
|
38
|
+
# ILI_L-L
|
|
39
|
+
plot_ILI(
|
|
40
|
+
all_lick_diffs[(all_licks_id[:-1] == 0) & (all_licks_id[1:] == 0)],
|
|
41
|
+
"ILI_L-L",
|
|
42
|
+
gs[0, 0],
|
|
43
|
+
fig,
|
|
44
|
+
)
|
|
45
|
+
# ILI_R-R
|
|
46
|
+
plot_ILI(
|
|
47
|
+
all_lick_diffs[(all_licks_id[:-1] == 1) & (all_licks_id[1:] == 1)],
|
|
48
|
+
"ILI_R-R",
|
|
49
|
+
gs[0, 1],
|
|
50
|
+
fig,
|
|
51
|
+
)
|
|
52
|
+
# ILI_R-L
|
|
53
|
+
plot_ILI(
|
|
54
|
+
all_lick_diffs[(all_licks_id[:-1] == 1) & (all_licks_id[1:] == 0)],
|
|
55
|
+
"ILI_R-l",
|
|
56
|
+
gs[0, 2],
|
|
57
|
+
fig,
|
|
58
|
+
)
|
|
59
|
+
# ILI_L-R
|
|
60
|
+
plot_ILI(
|
|
61
|
+
all_lick_diffs[(all_licks_id[:-1] == 0) & (all_licks_id[1:] == 1)],
|
|
62
|
+
"ILI_l-R",
|
|
63
|
+
gs[0, 3],
|
|
64
|
+
fig,
|
|
65
|
+
)
|
|
66
|
+
# ILI_all
|
|
67
|
+
plot_ILI(all_lick_diffs, "ILI_all", gs[0, 4], fig)
|
|
68
|
+
# Pre-trial lick punishment
|
|
69
|
+
lick_delay = (
|
|
70
|
+
tbl_trials["goCue_start_time"]
|
|
71
|
+
- tbl_trials["start_time"]
|
|
72
|
+
- tbl_trials["ITI_duration"]
|
|
73
|
+
)
|
|
74
|
+
bins = np.arange(
|
|
75
|
+
np.min(lick_delay),
|
|
76
|
+
np.max(lick_delay),
|
|
77
|
+
(np.max(lick_delay) - np.min(lick_delay)) / 20,
|
|
78
|
+
)
|
|
79
|
+
ax = fig.add_subplot(gs[0, 5])
|
|
80
|
+
ax.hist(
|
|
81
|
+
lick_delay[tbl_trials["animal_response"] == 1],
|
|
82
|
+
alpha=0.5,
|
|
83
|
+
label="R",
|
|
84
|
+
bins=bins,
|
|
85
|
+
# density=True,
|
|
86
|
+
)
|
|
87
|
+
ax.hist(
|
|
88
|
+
lick_delay[tbl_trials["animal_response"] == 0],
|
|
89
|
+
alpha=0.5,
|
|
90
|
+
label="L",
|
|
91
|
+
bins=bins,
|
|
92
|
+
# density=True,
|
|
93
|
+
)
|
|
94
|
+
ax.hist(
|
|
95
|
+
lick_delay[tbl_trials["animal_response"] == 2],
|
|
96
|
+
alpha=0.5,
|
|
97
|
+
label="0",
|
|
98
|
+
bins=bins,
|
|
99
|
+
# density=True,
|
|
100
|
+
)
|
|
101
|
+
p_early = 1 - np.mean(lick_delay < tbl_trials["delay_duration"] + 0.05)
|
|
102
|
+
ax.set_title(f"Time out punishment prop {p_early:.2f}")
|
|
103
|
+
ax.set_xlabel("s")
|
|
104
|
+
ax.legend()
|
|
105
|
+
|
|
106
|
+
# Pre-trial licks
|
|
107
|
+
|
|
108
|
+
sort_ind_l = np.argsort(
|
|
109
|
+
tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_duration"]
|
|
110
|
+
)
|
|
111
|
+
sort_ind_r = np.argsort(
|
|
112
|
+
tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_duration"]
|
|
113
|
+
)
|
|
114
|
+
l_align = tbl_trials.loc[
|
|
115
|
+
tbl_trials["animal_response"] == 0, "goCue_start_time"
|
|
116
|
+
].values
|
|
117
|
+
r_align = tbl_trials.loc[
|
|
118
|
+
tbl_trials["animal_response"] == 1, "goCue_start_time"
|
|
119
|
+
].values
|
|
120
|
+
l_align = l_align[sort_ind_l.values]
|
|
121
|
+
r_align = r_align[sort_ind_r.values]
|
|
122
|
+
|
|
123
|
+
if len(l_align) > 0:
|
|
124
|
+
# Left lick aligned to left choices
|
|
125
|
+
fig, ax1, ax2 = plot_raster_rate(
|
|
126
|
+
left_licks, l_align, fig, gs[1, 0], "L licks on L choice trials"
|
|
127
|
+
)
|
|
128
|
+
ax1.scatter(
|
|
129
|
+
-np.sort(
|
|
130
|
+
tbl_trials.loc[
|
|
131
|
+
tbl_trials["animal_response"] == 0, "delay_duration"
|
|
132
|
+
]
|
|
133
|
+
),
|
|
134
|
+
range(
|
|
135
|
+
len(
|
|
136
|
+
tbl_trials.loc[
|
|
137
|
+
tbl_trials["animal_response"] == 0, "delay_max"
|
|
138
|
+
]
|
|
139
|
+
)
|
|
140
|
+
),
|
|
141
|
+
c="b",
|
|
142
|
+
marker="|",
|
|
143
|
+
s=1,
|
|
144
|
+
zorder=1,
|
|
145
|
+
)
|
|
146
|
+
ax2.set_ylabel("Licks/s")
|
|
147
|
+
|
|
148
|
+
# Right licks aligned to left choices
|
|
149
|
+
fig, ax1, _ = plot_raster_rate(
|
|
150
|
+
right_licks, l_align, fig, gs[1, 2], "R licks on L choice trials"
|
|
151
|
+
)
|
|
152
|
+
ax1.scatter(
|
|
153
|
+
-np.sort(
|
|
154
|
+
tbl_trials.loc[
|
|
155
|
+
tbl_trials["animal_response"] == 0, "delay_duration"
|
|
156
|
+
]
|
|
157
|
+
),
|
|
158
|
+
range(
|
|
159
|
+
len(
|
|
160
|
+
tbl_trials.loc[
|
|
161
|
+
tbl_trials["animal_response"] == 0, "delay_max"
|
|
162
|
+
]
|
|
163
|
+
)
|
|
164
|
+
),
|
|
165
|
+
c="b",
|
|
166
|
+
marker="|",
|
|
167
|
+
s=1,
|
|
168
|
+
zorder=1,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
if len(r_align) > 0:
|
|
172
|
+
# right licks aligned to right choices
|
|
173
|
+
fig, ax1, _ = plot_raster_rate(
|
|
174
|
+
right_licks, r_align, fig, gs[1, 1], "R licks on R choice trials"
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
ax1.scatter(
|
|
178
|
+
-np.sort(
|
|
179
|
+
tbl_trials.loc[
|
|
180
|
+
tbl_trials["animal_response"] == 1, "delay_duration"
|
|
181
|
+
]
|
|
182
|
+
),
|
|
183
|
+
range(
|
|
184
|
+
len(
|
|
185
|
+
tbl_trials.loc[
|
|
186
|
+
tbl_trials["animal_response"] == 1, "delay_max"
|
|
187
|
+
]
|
|
188
|
+
)
|
|
189
|
+
),
|
|
190
|
+
c="b",
|
|
191
|
+
marker="|",
|
|
192
|
+
s=1,
|
|
193
|
+
zorder=1,
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
# left licks aligned to right choices
|
|
197
|
+
fig, ax1, _ = plot_raster_rate(
|
|
198
|
+
left_licks, r_align, fig, gs[1, 3], "L licks on R choice trials"
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
ax1.scatter(
|
|
202
|
+
-np.sort(
|
|
203
|
+
tbl_trials.loc[
|
|
204
|
+
tbl_trials["animal_response"] == 1, "delay_duration"
|
|
205
|
+
]
|
|
206
|
+
),
|
|
207
|
+
range(
|
|
208
|
+
len(
|
|
209
|
+
tbl_trials.loc[
|
|
210
|
+
tbl_trials["animal_response"] == 1, "delay_max"
|
|
211
|
+
]
|
|
212
|
+
)
|
|
213
|
+
),
|
|
214
|
+
c="b",
|
|
215
|
+
marker="|",
|
|
216
|
+
s=1,
|
|
217
|
+
zorder=1,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
session_id = nwb.session_id
|
|
221
|
+
session_id = session_id.split(".")[0]
|
|
222
|
+
box = nwb.scratch["metadata"][0].box.values
|
|
223
|
+
plt.suptitle(f"{session_id} in {box}")
|
|
224
|
+
|
|
225
|
+
# Response latency
|
|
226
|
+
ax = fig.add_subplot(gs[1, 4:6])
|
|
227
|
+
lick_lat = (
|
|
228
|
+
tbl_trials["reward_outcome_time"] - tbl_trials["goCue_start_time"]
|
|
229
|
+
)
|
|
230
|
+
bins = np.arange(0, 1, 0.05)
|
|
231
|
+
ax.hist(
|
|
232
|
+
lick_lat[tbl_trials["animal_response"] == 1],
|
|
233
|
+
bins=bins,
|
|
234
|
+
alpha=0.5,
|
|
235
|
+
label="R",
|
|
236
|
+
density=True,
|
|
237
|
+
)
|
|
238
|
+
ax.hist(
|
|
239
|
+
lick_lat[tbl_trials["animal_response"] == 0],
|
|
240
|
+
bins=bins,
|
|
241
|
+
alpha=0.5,
|
|
242
|
+
label="L",
|
|
243
|
+
density=True,
|
|
244
|
+
)
|
|
245
|
+
ax.legend()
|
|
246
|
+
ax.set_title("lickLat by lick side")
|
|
247
|
+
ax.set_xlabel("s")
|
|
248
|
+
ax.set_ylabel("density %")
|
|
249
|
+
plt.suptitle(session_id)
|
|
250
|
+
|
|
251
|
+
return fig, session_id
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def load_data(nwb):
|
|
255
|
+
"""Load data from NWB file."""
|
|
256
|
+
session_id = nwb.session_id
|
|
257
|
+
tbl_trials = nwb.trials.to_dataframe()
|
|
258
|
+
left_licks = nwb.acquisition["left_lick_time"].timestamps[:]
|
|
259
|
+
right_licks = nwb.acquisition["right_lick_time"].timestamps[:]
|
|
260
|
+
all_licks = np.sort(np.concatenate((right_licks, left_licks)))
|
|
261
|
+
lick_lat = (
|
|
262
|
+
tbl_trials["reward_outcome_time"] - tbl_trials["goCue_start_time"]
|
|
263
|
+
)
|
|
264
|
+
lick_lat_r = lick_lat[tbl_trials["animal_response"] == 1]
|
|
265
|
+
lick_lat_l = lick_lat[tbl_trials["animal_response"] == 0]
|
|
266
|
+
thresh = [0.05, 0.5, 1.0]
|
|
267
|
+
kernel = norm.pdf(np.arange(-2, 2.1, 0.5))
|
|
268
|
+
bin_width = 0.2
|
|
269
|
+
bin_steps = np.arange(0, 1.5, 0.02)
|
|
270
|
+
win_go = [thresh[0], 1.0]
|
|
271
|
+
win_bl = [-1, 0]
|
|
272
|
+
return {
|
|
273
|
+
"session_id": session_id,
|
|
274
|
+
"tbl_trials": tbl_trials,
|
|
275
|
+
"left_licks": left_licks,
|
|
276
|
+
"right_licks": right_licks,
|
|
277
|
+
"all_licks": all_licks,
|
|
278
|
+
"lick_lat": lick_lat,
|
|
279
|
+
"lick_lat_r": lick_lat_r,
|
|
280
|
+
"lick_lat_l": lick_lat_l,
|
|
281
|
+
"thresh": thresh,
|
|
282
|
+
"kernel": kernel,
|
|
283
|
+
"bin_width": bin_width,
|
|
284
|
+
"bin_steps": bin_steps,
|
|
285
|
+
"win_go": win_go,
|
|
286
|
+
"win_bl": win_bl,
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def cal_metrics(data):
|
|
291
|
+
"""Calculate lick metrics."""
|
|
292
|
+
session_id = data["session_id"]
|
|
293
|
+
tbl_trials = data["tbl_trials"]
|
|
294
|
+
left_licks = data["left_licks"]
|
|
295
|
+
right_licks = data["right_licks"]
|
|
296
|
+
all_licks = data["all_licks"]
|
|
297
|
+
lick_lat_r = data["lick_lat_r"]
|
|
298
|
+
lick_lat_l = data["lick_lat_l"]
|
|
299
|
+
thresh = data["thresh"]
|
|
300
|
+
kernel = data["kernel"]
|
|
301
|
+
bin_width = data["bin_width"]
|
|
302
|
+
bin_steps = data["bin_steps"]
|
|
303
|
+
win_go = data["win_go"]
|
|
304
|
+
win_bl = data["win_bl"]
|
|
305
|
+
|
|
306
|
+
lick_percent_l = [
|
|
307
|
+
np.sum(lick_lat_l <= thresh_curr) / np.shape(lick_lat_l)[0]
|
|
308
|
+
for thresh_curr in thresh
|
|
309
|
+
]
|
|
310
|
+
lick_percent_r = [
|
|
311
|
+
np.sum(lick_lat_r <= thresh_curr) / np.shape(lick_lat_r)[0]
|
|
312
|
+
for thresh_curr in thresh
|
|
313
|
+
]
|
|
314
|
+
finish = tbl_trials["animal_response"] != 2
|
|
315
|
+
ref = np.ones_like(finish)
|
|
316
|
+
ref_kernel = np.convolve(ref, kernel)
|
|
317
|
+
finish_kernel = np.convolve(finish.astype(float), kernel)
|
|
318
|
+
finish_kernel = np.divide(finish_kernel, ref_kernel)
|
|
319
|
+
finish_kernel = finish_kernel[
|
|
320
|
+
int(0.5 * len(kernel)): -int(0.5 * len(kernel))
|
|
321
|
+
]
|
|
322
|
+
all_go_no_rwd = tbl_trials.loc[
|
|
323
|
+
(tbl_trials["animal_response"] != 2)
|
|
324
|
+
& (tbl_trials["rewarded_historyL"] == 0)
|
|
325
|
+
& (tbl_trials["rewarded_historyR"] == 0),
|
|
326
|
+
"goCue_start_time",
|
|
327
|
+
].values
|
|
328
|
+
all_pre_nolick = (
|
|
329
|
+
tbl_trials.loc[
|
|
330
|
+
tbl_trials["animal_response"] != 2, "goCue_start_time"
|
|
331
|
+
].values
|
|
332
|
+
- tbl_trials.loc[
|
|
333
|
+
tbl_trials["animal_response"] != 2, "delay_duration"
|
|
334
|
+
].values
|
|
335
|
+
)
|
|
336
|
+
respond_mean = np.mean(rate_align(all_licks, all_go_no_rwd, win_go))
|
|
337
|
+
bl_mean = np.mean(rate_align(all_licks, all_pre_nolick, win_bl))
|
|
338
|
+
bl_mean_l = np.mean(rate_align(left_licks, all_pre_nolick, win_bl))
|
|
339
|
+
bl_mean_r = np.mean(rate_align(right_licks, all_pre_nolick, win_bl))
|
|
340
|
+
l_major, l_major_perc = slide_mode(lick_lat_l, bin_width, bin_steps)
|
|
341
|
+
r_major, r_major_perc = slide_mode(lick_lat_r, bin_width, bin_steps)
|
|
342
|
+
lick_met = {
|
|
343
|
+
"session_id": session_id,
|
|
344
|
+
"bl_lick": bl_mean,
|
|
345
|
+
"bl_lick_lr": [bl_mean_l, bl_mean_r],
|
|
346
|
+
"resp_lick": respond_mean,
|
|
347
|
+
"bl_lick_trial": rate_align(all_licks, all_pre_nolick, win_bl),
|
|
348
|
+
"resp_lick_trial": rate_align(all_licks, all_go_no_rwd, win_go),
|
|
349
|
+
"peak_ratio": [l_major_perc, r_major_perc],
|
|
350
|
+
"peak_lat": [l_major, r_major],
|
|
351
|
+
"lick_cdf": {
|
|
352
|
+
"thresh": thresh,
|
|
353
|
+
"l": lick_percent_l,
|
|
354
|
+
"r": lick_percent_r,
|
|
355
|
+
},
|
|
356
|
+
"resp_score": respond_mean - bl_mean,
|
|
357
|
+
"finish_ratio": finish_kernel,
|
|
358
|
+
}
|
|
359
|
+
return lick_met
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def plot_met(data, lick_met):
|
|
363
|
+
"""Plot lick metrics."""
|
|
364
|
+
lick_lat_l = data["lick_lat_l"]
|
|
365
|
+
lick_lat_r = data["lick_lat_r"]
|
|
366
|
+
bin_width = data["bin_width"]
|
|
367
|
+
win_bl = data["win_bl"]
|
|
368
|
+
win_go = data["win_go"]
|
|
369
|
+
session_id = data["session_id"]
|
|
370
|
+
all_licks = data["all_licks"]
|
|
371
|
+
tbl_trials = data["tbl_trials"]
|
|
372
|
+
|
|
373
|
+
edges = np.arange(
|
|
374
|
+
np.min(lick_lat_l),
|
|
375
|
+
np.max(lick_lat_l),
|
|
376
|
+
0.02,
|
|
377
|
+
)
|
|
378
|
+
fig = plt.figure(figsize=(8, 4))
|
|
379
|
+
gs = fig.add_gridspec(
|
|
380
|
+
2,
|
|
381
|
+
2,
|
|
382
|
+
width_ratios=[1, 1],
|
|
383
|
+
height_ratios=[2, 1],
|
|
384
|
+
wspace=0.5,
|
|
385
|
+
hspace=0.5,
|
|
386
|
+
)
|
|
387
|
+
ax1 = fig.add_subplot(gs[0, 0])
|
|
388
|
+
ax1.hist(
|
|
389
|
+
lick_lat_l, bins=edges, alpha=0.5, density=True, label="lick hist"
|
|
390
|
+
)
|
|
391
|
+
ax1.set_xlabel("s")
|
|
392
|
+
ax1.set_ylabel("density %")
|
|
393
|
+
ax1.legend(loc="upper left", bbox_to_anchor=(0, 1), ncol=1)
|
|
394
|
+
ax1t = ax1.twinx()
|
|
395
|
+
ax1t.set_ylabel("CDF")
|
|
396
|
+
plot_cdf(lick_lat_l, {"label": "lick cdf"})
|
|
397
|
+
# ax1t.plot(np.array(thresh), lick_met["lick_cdf"]["l"], "k")
|
|
398
|
+
l_major = lick_met["peak_lat"][0]
|
|
399
|
+
l_major_ratio = lick_met["peak_ratio"][0]
|
|
400
|
+
ax1t.fill(
|
|
401
|
+
[
|
|
402
|
+
l_major - 0.5 * bin_width,
|
|
403
|
+
l_major + 0.5 * bin_width,
|
|
404
|
+
l_major + 0.5 * bin_width,
|
|
405
|
+
l_major - 0.5 * bin_width,
|
|
406
|
+
],
|
|
407
|
+
[0, 0, 1, 1],
|
|
408
|
+
"r",
|
|
409
|
+
alpha=0.2,
|
|
410
|
+
)
|
|
411
|
+
ax1t.set_title(f"Lick Latency: L {l_major_ratio:.2f}")
|
|
412
|
+
ax1t.set_ylim(0, 1.1)
|
|
413
|
+
ax1t.legend(loc="upper left", bbox_to_anchor=(0, 0.8), ncol=1)
|
|
414
|
+
ax1.plot(
|
|
415
|
+
[win_bl[0], 0],
|
|
416
|
+
np.ones_like(win_bl) * lick_met["bl_lick"],
|
|
417
|
+
color=[0.3, 0.3, 0.3],
|
|
418
|
+
lw=4,
|
|
419
|
+
)
|
|
420
|
+
ax1.plot(
|
|
421
|
+
win_go,
|
|
422
|
+
np.ones_like(win_bl) * lick_met["resp_lick"],
|
|
423
|
+
color=[0.3, 0.3, 0.3],
|
|
424
|
+
lw=4,
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
ax2 = fig.add_subplot(gs[0, 1])
|
|
428
|
+
ax2.hist(lick_lat_r, bins=edges, alpha=0.5, density=True)
|
|
429
|
+
ax2.set_xlabel("s")
|
|
430
|
+
ax2t = ax2.twinx()
|
|
431
|
+
plot_cdf(lick_lat_r, {"label": "lick cdf"})
|
|
432
|
+
# ax2t.plot(np.array(thresh), lick_met["lick_cdf"]["r"], "k")
|
|
433
|
+
r_major = lick_met["peak_lat"][1]
|
|
434
|
+
r_major_ratio = lick_met["peak_ratio"][1]
|
|
435
|
+
ax2t.fill(
|
|
436
|
+
[
|
|
437
|
+
r_major - 0.5 * bin_width,
|
|
438
|
+
r_major + 0.5 * bin_width,
|
|
439
|
+
r_major + 0.5 * bin_width,
|
|
440
|
+
r_major - 0.5 * bin_width,
|
|
441
|
+
],
|
|
442
|
+
[0, 0, 1, 1],
|
|
443
|
+
"r",
|
|
444
|
+
alpha=0.2,
|
|
445
|
+
)
|
|
446
|
+
ax2t.set_title(f"R {r_major_ratio:.2f}")
|
|
447
|
+
ax2t.set_ylim(0, 1.1)
|
|
448
|
+
ax2.plot(
|
|
449
|
+
[win_bl[0], 0],
|
|
450
|
+
np.ones_like(win_bl) * lick_met["bl_lick"],
|
|
451
|
+
color=[0.3, 0.3, 0.3],
|
|
452
|
+
lw=4,
|
|
453
|
+
)
|
|
454
|
+
ax2.plot(
|
|
455
|
+
win_go,
|
|
456
|
+
np.ones_like(win_bl) * lick_met["resp_lick"],
|
|
457
|
+
color=[0.3, 0.3, 0.3],
|
|
458
|
+
lw=4,
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
ax3 = fig.add_subplot(gs[1, :])
|
|
462
|
+
all_go_no_rwd = tbl_trials.loc[
|
|
463
|
+
(tbl_trials["animal_response"] != 2)
|
|
464
|
+
& (tbl_trials["rewarded_historyL"] == 0)
|
|
465
|
+
& (tbl_trials["rewarded_historyR"] == 0),
|
|
466
|
+
"goCue_start_time",
|
|
467
|
+
].values
|
|
468
|
+
all_go_rwd = tbl_trials.loc[
|
|
469
|
+
(tbl_trials["animal_response"] != 2)
|
|
470
|
+
& (tbl_trials["rewarded_historyL"] == 1)
|
|
471
|
+
| (tbl_trials["rewarded_historyR"] == 1),
|
|
472
|
+
"goCue_start_time",
|
|
473
|
+
].values
|
|
474
|
+
all_pre_nolick = (
|
|
475
|
+
tbl_trials.loc[
|
|
476
|
+
tbl_trials["animal_response"] != 2, "goCue_start_time"
|
|
477
|
+
].values
|
|
478
|
+
- tbl_trials.loc[
|
|
479
|
+
tbl_trials["animal_response"] != 2, "delay_duration"
|
|
480
|
+
].values
|
|
481
|
+
)
|
|
482
|
+
rate_go_rwd = rate_align(all_licks, all_go_rwd, win_go)
|
|
483
|
+
|
|
484
|
+
ax3.plot(
|
|
485
|
+
all_go_no_rwd,
|
|
486
|
+
lick_met["resp_lick_trial"],
|
|
487
|
+
label="Resp-noRwd",
|
|
488
|
+
color="r",
|
|
489
|
+
)
|
|
490
|
+
ax3.plot(
|
|
491
|
+
all_pre_nolick,
|
|
492
|
+
lick_met["bl_lick_trial"],
|
|
493
|
+
label="baseline",
|
|
494
|
+
color="grey",
|
|
495
|
+
)
|
|
496
|
+
ax3.plot(all_go_rwd, rate_go_rwd, label="Resp-Rwd", color="b")
|
|
497
|
+
ax3.set_ylabel("lick rate")
|
|
498
|
+
ax3t = ax3.twinx()
|
|
499
|
+
ax3t.plot(
|
|
500
|
+
tbl_trials["goCue_start_time"],
|
|
501
|
+
lick_met["finish_ratio"],
|
|
502
|
+
color="g",
|
|
503
|
+
label="finish",
|
|
504
|
+
)
|
|
505
|
+
ax3t.set_ylabel("Ratio")
|
|
506
|
+
temp = lick_met["resp_score"]
|
|
507
|
+
ax3.set_title(f"Resp score {temp:.2f}")
|
|
508
|
+
ax3.legend()
|
|
509
|
+
ax3t.legend()
|
|
510
|
+
plt.suptitle(session_id)
|
|
511
|
+
return fig, session_id
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def plot_cdf(x, args):
|
|
515
|
+
"""plot CDF given input x"""
|
|
516
|
+
sorted_data = np.sort(x)
|
|
517
|
+
cdf = np.arange(1, len(sorted_data) + 1) / len(sorted_data)
|
|
518
|
+
plt.plot(sorted_data, cdf, **args)
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def slide_mode(x, bin_size, bin_step):
|
|
522
|
+
"""find mode with sliding window"""
|
|
523
|
+
x = np.sort(x)
|
|
524
|
+
start_inds = np.searchsorted(x, bin_step - 0.5 * bin_size)
|
|
525
|
+
stops_inds = np.searchsorted(x, bin_step + 0.5 * bin_size)
|
|
526
|
+
mode_ind = np.argmax(np.array(stops_inds - start_inds))
|
|
527
|
+
mode = bin_step[mode_ind]
|
|
528
|
+
mode_perc = np.max(np.array(stops_inds - start_inds)) / np.max(x.shape)
|
|
529
|
+
return mode, mode_perc
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def rate_align(x, events, win):
|
|
533
|
+
"""calculate rate of occurrence aligned to events with fixed window."""
|
|
534
|
+
x = np.sort(x)
|
|
535
|
+
start_inds = np.searchsorted(x, events + win[0])
|
|
536
|
+
stop_inds = np.searchsorted(x, events + win[1])
|
|
537
|
+
rate = (stop_inds - start_inds) / (win[1] - win[0])
|
|
538
|
+
return rate
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def plot_ILI(ili, title, subplot_spec, fig):
|
|
542
|
+
"""Plot ILI."""
|
|
543
|
+
nested_gs = gridspec.GridSpecFromSubplotSpec(
|
|
544
|
+
2, 1, subplot_spec=subplot_spec
|
|
545
|
+
)
|
|
546
|
+
bins = np.arange(0, 2500, 20)
|
|
547
|
+
ax1 = fig.add_subplot(nested_gs[0, 0])
|
|
548
|
+
ax2 = fig.add_subplot(nested_gs[1, 0])
|
|
549
|
+
ax1.hist(
|
|
550
|
+
1000 * ili,
|
|
551
|
+
bins=bins,
|
|
552
|
+
)
|
|
553
|
+
ax1.set_title(title)
|
|
554
|
+
ax2.hist(
|
|
555
|
+
1000 * ili,
|
|
556
|
+
bins=bins,
|
|
557
|
+
)
|
|
558
|
+
ax2.set_xlim([0, 300])
|
|
559
|
+
ax2.set_xlabel("ms")
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def plot_raster_rate(
|
|
563
|
+
events,
|
|
564
|
+
align_events,
|
|
565
|
+
fig,
|
|
566
|
+
subplot_spec,
|
|
567
|
+
title,
|
|
568
|
+
tb=-5,
|
|
569
|
+
tf=10,
|
|
570
|
+
bin_size=100 / 1000,
|
|
571
|
+
step_size=50 / 1000,
|
|
572
|
+
):
|
|
573
|
+
"""Plot raster and rate aligned to events"""
|
|
574
|
+
edges = np.arange(tb + 0.5 * bin_size, tf - 0.5 * bin_size, step_size)
|
|
575
|
+
nested_gs = gridspec.GridSpecFromSubplotSpec(
|
|
576
|
+
2, 1, subplot_spec=subplot_spec
|
|
577
|
+
)
|
|
578
|
+
ax1 = fig.add_subplot(nested_gs[0, 0])
|
|
579
|
+
ax2 = fig.add_subplot(nested_gs[1, 0])
|
|
580
|
+
|
|
581
|
+
df = align.to_events(events, align_events, (tb, tf), return_df=True)
|
|
582
|
+
ax1.scatter(df.time, df.event_index, c="k", marker="|", s=1, zorder=2)
|
|
583
|
+
ax1.axvline(x=0, c="r", ls="--", lw=1, zorder=3)
|
|
584
|
+
ax1.set_title(title)
|
|
585
|
+
ax1.set_xlim(tb, tf)
|
|
586
|
+
|
|
587
|
+
counts_pre = np.searchsorted(
|
|
588
|
+
np.sort(df.time.values), edges - 0.5 * bin_size
|
|
589
|
+
)
|
|
590
|
+
counts_post = np.searchsorted(
|
|
591
|
+
np.sort(df.time.values), edges + 0.5 * bin_size
|
|
592
|
+
)
|
|
593
|
+
lick_rate = (counts_post - counts_pre) / (bin_size * len(align_events))
|
|
594
|
+
ax2.plot(edges, lick_rate)
|
|
595
|
+
ax2.set_title("lickRate")
|
|
596
|
+
ax2.set_xlim(tb, tf)
|
|
597
|
+
ax2.set_xlabel("Time from go cue (s)")
|
|
598
|
+
|
|
599
|
+
return fig, ax1, ax2
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
# example use
|
|
603
|
+
if __name__ == "__main__":
|
|
604
|
+
import os
|
|
605
|
+
from pathlib import Path
|
|
606
|
+
|
|
607
|
+
"""Example."""
|
|
608
|
+
data_dir = Path(os.path.dirname(__file__)).parent.parent
|
|
609
|
+
nwbfile = os.path.join(
|
|
610
|
+
data_dir, "tests/data/689514_2024-02-01_18-06-43.nwb"
|
|
611
|
+
)
|
|
612
|
+
# use of load_data depends on data structure
|
|
613
|
+
nwb = load_nwb(nwbfile)
|
|
614
|
+
fig, session_id = plot_lick_analysis(nwb)
|
|
615
|
+
save_dir = os.path.join(data_dir, "tests/data", session_id)
|
|
616
|
+
fig.savefig(save_dir)
|
|
617
|
+
|
|
618
|
+
data = load_data(nwb)
|
|
619
|
+
lick_sum = cal_metrics(data)
|
|
620
|
+
fig, _ = plot_met(data, lick_sum)
|
|
621
|
+
save_dir = os.path.join(data_dir, "tests/data", session_id + "qc")
|
|
622
|
+
fig.savefig(save_dir)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: aind-dynamic-foraging-basic-analysis
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Generated from aind-library-template
|
|
5
5
|
Author: Allen Institute for Neural Dynamics
|
|
6
6
|
License: MIT
|
|
@@ -9,7 +9,12 @@ Requires-Python: >=3.7
|
|
|
9
9
|
Description-Content-Type: text/markdown
|
|
10
10
|
License-File: LICENSE
|
|
11
11
|
Requires-Dist: numpy
|
|
12
|
+
Requires-Dist: scipy
|
|
12
13
|
Requires-Dist: matplotlib
|
|
14
|
+
Requires-Dist: pandas
|
|
15
|
+
Requires-Dist: pynwb
|
|
16
|
+
Requires-Dist: aind-ephys-utils
|
|
17
|
+
Requires-Dist: pathlib
|
|
13
18
|
Requires-Dist: pydantic
|
|
14
19
|
Provides-Extra: dev
|
|
15
20
|
Requires-Dist: aind-dynamic-foraging-basic-analysis ; extra == 'dev'
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=9eee3SXxbiJ_U7VbhoMxyRs7mH3m3VCgOY8EBx6qPM4,194
|
|
2
|
+
aind_dynamic_foraging_basic_analysis/foraging_efficiency.py,sha256=9IhMqQo_aU_RFsDu8D2P3-Qu3J72bo7sASC3dkUQ2YE,8634
|
|
3
|
+
aind_dynamic_foraging_basic_analysis/lick_analysis.py,sha256=WCfSmG7g57f5o7u3gsYoYTipyH7hyqf61R4zWKbDIhI,18136
|
|
4
|
+
aind_dynamic_foraging_basic_analysis/data_model/foraging_session.py,sha256=7PFHfEIXP3QcjFS9Fk88S7PzBBYnUYw81WBjjEu_UKo,3536
|
|
5
|
+
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py,sha256=X-gLffs8tau2Eeh_fF8tlU-KDkYiyRrtbO_jZnTyPxo,11580
|
|
6
|
+
aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
|
|
7
|
+
aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/METADATA,sha256=aliacxrmrBmHlozX0qgJ2mn2MhC0fnzQEMriTZI_mX4,6690
|
|
8
|
+
aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
|
|
9
|
+
aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
|
|
10
|
+
aind_dynamic_foraging_basic_analysis-0.2.1.dist-info/RECORD,,
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=ozBqp0S9SOhV0OnQYo3Azwerr9L2PAcjK4i5-t4POsg,194
|
|
2
|
-
aind_dynamic_foraging_basic_analysis/foraging_efficiency.py,sha256=9IhMqQo_aU_RFsDu8D2P3-Qu3J72bo7sASC3dkUQ2YE,8634
|
|
3
|
-
aind_dynamic_foraging_basic_analysis/data_model/foraging_session.py,sha256=7PFHfEIXP3QcjFS9Fk88S7PzBBYnUYw81WBjjEu_UKo,3536
|
|
4
|
-
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py,sha256=X-gLffs8tau2Eeh_fF8tlU-KDkYiyRrtbO_jZnTyPxo,11580
|
|
5
|
-
aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
|
|
6
|
-
aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/METADATA,sha256=Bt5D8dpWcYwwPiHmwyhxFXe1RftpAuUGLCRi3ji4uqw,6571
|
|
7
|
-
aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
|
|
8
|
-
aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
|
|
9
|
-
aind_dynamic_foraging_basic_analysis-0.2.0.dist-info/RECORD,,
|
|
File without changes
|