aind-dynamic-foraging-basic-analysis 0.2.1__py3-none-any.whl → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  """Init package"""
2
2
 
3
- __version__ = "0.2.1"
3
+ __version__ = "0.2.2"
4
4
 
5
5
  from .foraging_efficiency import compute_foraging_efficiency # noqa: F401
6
6
  from .plot.plot_foraging_session import plot_foraging_session # noqa: F401
@@ -17,6 +17,8 @@ def load_nwb(nwb_file):
17
17
  def plot_lick_analysis(nwb):
18
18
  """Plot lick distributions."""
19
19
  tbl_trials = nwb.trials.to_dataframe()
20
+ session_id = nwb.session_id
21
+ session_id = session_id.split(".")[0]
20
22
  gs = gridspec.GridSpec(
21
23
  2,
22
24
  6,
@@ -26,227 +28,182 @@ def plot_lick_analysis(nwb):
26
28
  fig = plt.figure(figsize=(15, 8))
27
29
  plt.subplots_adjust(hspace=0.5, wspace=0.5)
28
30
 
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"
31
+ if len(tbl_trials) < 10:
32
+ """No enough trials to plot lick analysis."""
33
+ title_str = f"{session_id} has less than 10 trials"
34
+ plt.suptitle(title_str)
35
+ else:
36
+ left_licks = nwb.acquisition["left_lick_time"].timestamps[:]
37
+ right_licks = nwb.acquisition["right_lick_time"].timestamps[:]
38
+ all_licks = np.sort(np.concatenate((right_licks, left_licks)))
39
+ sort_ind = np.argsort(np.concatenate((right_licks, left_licks)))
40
+ all_licks_id = np.concatenate((np.ones_like(right_licks), np.zeros_like(left_licks)))
41
+ all_licks_id = all_licks_id[sort_ind]
42
+ all_lick_diffs = np.diff(all_licks)
43
+ # ILI_L-L
44
+ plot_ILI(
45
+ all_lick_diffs[(all_licks_id[:-1] == 0) & (all_licks_id[1:] == 0)],
46
+ "ILI_L-L",
47
+ gs[0, 0],
48
+ fig,
127
49
  )
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,
50
+ # ILI_R-R
51
+ plot_ILI(
52
+ all_lick_diffs[(all_licks_id[:-1] == 1) & (all_licks_id[1:] == 1)],
53
+ "ILI_R-R",
54
+ gs[0, 1],
55
+ fig,
145
56
  )
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"
57
+ # ILI_R-L
58
+ plot_ILI(
59
+ all_lick_diffs[(all_licks_id[:-1] == 1) & (all_licks_id[1:] == 0)],
60
+ "ILI_R-l",
61
+ gs[0, 2],
62
+ fig,
151
63
  )
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,
64
+ # ILI_L-R
65
+ plot_ILI(
66
+ all_lick_diffs[(all_licks_id[:-1] == 0) & (all_licks_id[1:] == 1)],
67
+ "ILI_l-R",
68
+ gs[0, 3],
69
+ fig,
169
70
  )
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"
71
+ # ILI_all
72
+ plot_ILI(all_lick_diffs, "ILI_all", gs[0, 4], fig)
73
+ # Pre-trial lick punishment
74
+ lick_delay = (
75
+ tbl_trials["goCue_start_time"] - tbl_trials["start_time"] - tbl_trials["ITI_duration"]
175
76
  )
176
77
 
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,
78
+ bins = np.arange(
79
+ np.min(lick_delay),
80
+ np.max(lick_delay),
81
+ (np.max(lick_delay) - np.min(lick_delay)) / 20,
194
82
  )
195
83
 
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"
84
+ ax = fig.add_subplot(gs[0, 5])
85
+ ax.hist(
86
+ lick_delay[tbl_trials["animal_response"] == 1],
87
+ alpha=0.5,
88
+ label="R",
89
+ bins=bins,
90
+ # density=True,
199
91
  )
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,
92
+ ax.hist(
93
+ lick_delay[tbl_trials["animal_response"] == 0],
94
+ alpha=0.5,
95
+ label="L",
96
+ bins=bins,
97
+ # density=True,
218
98
  )
99
+ ax.hist(
100
+ lick_delay[tbl_trials["animal_response"] == 2],
101
+ alpha=0.5,
102
+ label="0",
103
+ bins=bins,
104
+ # density=True,
105
+ )
106
+ p_early = 1 - np.mean(lick_delay < tbl_trials["delay_duration"] + 0.05)
107
+ ax.set_title(f"Time out punishment prop {p_early:.2f}")
108
+ ax.set_xlabel("s")
109
+ ax.legend()
219
110
 
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}")
111
+ # Pre-trial licks
224
112
 
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)
113
+ sort_ind_l = np.argsort(
114
+ tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_duration"]
115
+ )
116
+ sort_ind_r = np.argsort(
117
+ tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_duration"]
118
+ )
119
+ l_align = tbl_trials.loc[tbl_trials["animal_response"] == 0, "goCue_start_time"].values
120
+ r_align = tbl_trials.loc[tbl_trials["animal_response"] == 1, "goCue_start_time"].values
121
+ l_align = l_align[sort_ind_l.values]
122
+ r_align = r_align[sort_ind_r.values]
123
+
124
+ if len(l_align) > 0:
125
+ # Left lick aligned to left choices
126
+ fig, ax1, ax2 = plot_raster_rate(
127
+ left_licks, l_align, fig, gs[1, 0], "L licks on L choice trials"
128
+ )
129
+ ax1.scatter(
130
+ -np.sort(tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_duration"]),
131
+ range(len(tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_max"])),
132
+ c="b",
133
+ marker="|",
134
+ s=1,
135
+ zorder=1,
136
+ )
137
+ ax2.set_ylabel("Licks/s")
138
+
139
+ # Right licks aligned to left choices
140
+ fig, ax1, _ = plot_raster_rate(
141
+ right_licks, l_align, fig, gs[1, 2], "R licks on L choice trials"
142
+ )
143
+ ax1.scatter(
144
+ -np.sort(tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_duration"]),
145
+ range(len(tbl_trials.loc[tbl_trials["animal_response"] == 0, "delay_max"])),
146
+ c="b",
147
+ marker="|",
148
+ s=1,
149
+ zorder=1,
150
+ )
151
+
152
+ if len(r_align) > 0:
153
+ # right licks aligned to right choices
154
+ fig, ax1, _ = plot_raster_rate(
155
+ right_licks, r_align, fig, gs[1, 1], "R licks on R choice trials"
156
+ )
157
+
158
+ ax1.scatter(
159
+ -np.sort(tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_duration"]),
160
+ range(len(tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_max"])),
161
+ c="b",
162
+ marker="|",
163
+ s=1,
164
+ zorder=1,
165
+ )
166
+
167
+ # left licks aligned to right choices
168
+ fig, ax1, _ = plot_raster_rate(
169
+ left_licks, r_align, fig, gs[1, 3], "L licks on R choice trials"
170
+ )
171
+
172
+ ax1.scatter(
173
+ -np.sort(tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_duration"]),
174
+ range(len(tbl_trials.loc[tbl_trials["animal_response"] == 1, "delay_max"])),
175
+ c="b",
176
+ marker="|",
177
+ s=1,
178
+ zorder=1,
179
+ )
180
+
181
+ box = nwb.scratch["metadata"][0].box.values
182
+ plt.suptitle(f"{session_id} in {box}")
183
+
184
+ # Response latency
185
+ ax = fig.add_subplot(gs[1, 4:6])
186
+ lick_lat = tbl_trials["reward_outcome_time"] - tbl_trials["goCue_start_time"]
187
+ bins = np.arange(0, 1, 0.05)
188
+ ax.hist(
189
+ lick_lat[tbl_trials["animal_response"] == 1],
190
+ bins=bins,
191
+ alpha=0.5,
192
+ label="R",
193
+ density=True,
194
+ )
195
+ ax.hist(
196
+ lick_lat[tbl_trials["animal_response"] == 0],
197
+ bins=bins,
198
+ alpha=0.5,
199
+ label="L",
200
+ density=True,
201
+ )
202
+ ax.legend()
203
+ ax.set_title("lickLat by lick side")
204
+ ax.set_xlabel("s")
205
+ ax.set_ylabel("density %")
206
+ plt.suptitle(session_id)
250
207
 
251
208
  return fig, session_id
252
209
 
@@ -254,13 +211,12 @@ def plot_lick_analysis(nwb):
254
211
  def load_data(nwb):
255
212
  """Load data from NWB file."""
256
213
  session_id = nwb.session_id
214
+ session_id = session_id.split(".")[0]
257
215
  tbl_trials = nwb.trials.to_dataframe()
258
216
  left_licks = nwb.acquisition["left_lick_time"].timestamps[:]
259
217
  right_licks = nwb.acquisition["right_lick_time"].timestamps[:]
260
218
  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
- )
219
+ lick_lat = tbl_trials["reward_outcome_time"] - tbl_trials["goCue_start_time"]
264
220
  lick_lat_r = lick_lat[tbl_trials["animal_response"] == 1]
265
221
  lick_lat_l = lick_lat[tbl_trials["animal_response"] == 0]
266
222
  thresh = [0.05, 0.5, 1.0]
@@ -303,60 +259,72 @@ def cal_metrics(data):
303
259
  win_go = data["win_go"]
304
260
  win_bl = data["win_bl"]
305
261
 
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"
262
+ if tbl_trials.shape[0] < 10:
263
+ """Not enough trials to calculate lick metrics."""
264
+ return {
265
+ "session_id": session_id,
266
+ "bl_lick": 0,
267
+ "bl_lick_lr": [0, 0],
268
+ "resp_lick": 0,
269
+ "bl_lick_trial": 0,
270
+ "resp_lick_trial": 0,
271
+ "peak_ratio": [0, 0],
272
+ "peak_lat": [0, 0],
273
+ "lick_cdf": {
274
+ "thresh": thresh,
275
+ "l": [0, 0, 0],
276
+ "r": [0, 0, 0],
277
+ },
278
+ "resp_score": 0,
279
+ "finish_ratio": 0,
280
+ }
281
+ else: # calculate lick metrics
282
+ lick_percent_l = [
283
+ np.sum(lick_lat_l <= thresh_curr) / np.shape(lick_lat_l)[0] for thresh_curr in thresh
284
+ ]
285
+ lick_percent_r = [
286
+ np.sum(lick_lat_r <= thresh_curr) / np.shape(lick_lat_r)[0] for thresh_curr in thresh
287
+ ]
288
+ finish = tbl_trials["animal_response"] != 2
289
+ ref = np.ones_like(finish)
290
+ ref_kernel = np.convolve(ref, kernel)
291
+ finish_kernel = np.convolve(finish.astype(float), kernel)
292
+ finish_kernel = np.divide(finish_kernel, ref_kernel)
293
+ finish_kernel = finish_kernel[int(0.5 * len(kernel)):-int(0.5 * len(kernel))]
294
+ all_go_no_rwd = tbl_trials.loc[
295
+ (tbl_trials["animal_response"] != 2)
296
+ & (tbl_trials["rewarded_historyL"] == 0)
297
+ & (tbl_trials["rewarded_historyR"] == 0),
298
+ "goCue_start_time",
331
299
  ].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
300
+ all_pre_nolick = (
301
+ tbl_trials.loc[tbl_trials["animal_response"] != 2, "goCue_start_time"].values
302
+ - tbl_trials.loc[tbl_trials["animal_response"] != 2, "delay_duration"].values
303
+ )
304
+ respond_mean = np.mean(rate_align(all_licks, all_go_no_rwd, win_go))
305
+ bl_mean = np.mean(rate_align(all_licks, all_pre_nolick, win_bl))
306
+ bl_mean_l = np.mean(rate_align(left_licks, all_pre_nolick, win_bl))
307
+ bl_mean_r = np.mean(rate_align(right_licks, all_pre_nolick, win_bl))
308
+ l_major, l_major_perc = slide_mode(lick_lat_l, bin_width, bin_steps)
309
+ r_major, r_major_perc = slide_mode(lick_lat_r, bin_width, bin_steps)
310
+ lick_met = {
311
+ "session_id": session_id,
312
+ "bl_lick": bl_mean,
313
+ "bl_lick_lr": [bl_mean_l, bl_mean_r],
314
+ "resp_lick": respond_mean,
315
+ "bl_lick_trial": rate_align(all_licks, all_pre_nolick, win_bl),
316
+ "resp_lick_trial": rate_align(all_licks, all_go_no_rwd, win_go),
317
+ "peak_ratio": [l_major_perc, r_major_perc],
318
+ "peak_lat": [l_major, r_major],
319
+ "lick_cdf": {
320
+ "thresh": thresh,
321
+ "l": lick_percent_l,
322
+ "r": lick_percent_r,
323
+ },
324
+ "resp_score": respond_mean - bl_mean,
325
+ "finish_ratio": finish_kernel,
326
+ }
327
+ return lick_met
360
328
 
361
329
 
362
330
  def plot_met(data, lick_met):
@@ -370,144 +338,141 @@ def plot_met(data, lick_met):
370
338
  all_licks = data["all_licks"]
371
339
  tbl_trials = data["tbl_trials"]
372
340
 
373
- edges = np.arange(
374
- np.min(lick_lat_l),
375
- np.max(lick_lat_l),
376
- 0.02,
377
- )
378
341
  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
- )
342
+ if data["tbl_trials"].shape[0] < 10:
343
+ """Not enough trials to plot lick metrics."""
344
+ plt.suptitle(f"{session_id} has less than 10 trials")
345
+ else:
346
+ edges = np.arange(
347
+ np.min(lick_lat_l),
348
+ np.max(lick_lat_l),
349
+ 0.02,
350
+ )
351
+ gs = fig.add_gridspec(
352
+ 2,
353
+ 2,
354
+ width_ratios=[1, 1],
355
+ height_ratios=[2, 1],
356
+ wspace=0.5,
357
+ hspace=0.5,
358
+ )
359
+ ax1 = fig.add_subplot(gs[0, 0])
360
+ ax1.hist(lick_lat_l, bins=edges, alpha=0.5, density=True, label="lick hist")
361
+ ax1.set_xlabel("s")
362
+ ax1.set_ylabel("density %")
363
+ ax1.legend(loc="upper left", bbox_to_anchor=(0, 1), ncol=1)
364
+ ax1t = ax1.twinx()
365
+ ax1t.set_ylabel("CDF")
366
+ plot_cdf(lick_lat_l, {"label": "lick cdf"})
367
+ # ax1t.plot(np.array(thresh), lick_met["lick_cdf"]["l"], "k")
368
+ l_major = lick_met["peak_lat"][0]
369
+ l_major_ratio = lick_met["peak_ratio"][0]
370
+ ax1t.fill(
371
+ [
372
+ l_major - 0.5 * bin_width,
373
+ l_major + 0.5 * bin_width,
374
+ l_major + 0.5 * bin_width,
375
+ l_major - 0.5 * bin_width,
376
+ ],
377
+ [0, 0, 1, 1],
378
+ "r",
379
+ alpha=0.2,
380
+ )
381
+ ax1t.set_title(f"Lick Latency: L {l_major_ratio:.2f}")
382
+ ax1t.set_ylim(0, 1.1)
383
+ ax1t.legend(loc="upper left", bbox_to_anchor=(0, 0.8), ncol=1)
384
+ ax1.plot(
385
+ [win_bl[0], 0],
386
+ np.ones_like(win_bl) * lick_met["bl_lick"],
387
+ color=[0.3, 0.3, 0.3],
388
+ lw=4,
389
+ )
390
+ ax1.plot(
391
+ win_go,
392
+ np.ones_like(win_bl) * lick_met["resp_lick"],
393
+ color=[0.3, 0.3, 0.3],
394
+ lw=4,
395
+ )
426
396
 
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
- )
397
+ ax2 = fig.add_subplot(gs[0, 1])
398
+ ax2.hist(lick_lat_r, bins=edges, alpha=0.5, density=True)
399
+ ax2.set_xlabel("s")
400
+ ax2t = ax2.twinx()
401
+ plot_cdf(lick_lat_r, {"label": "lick cdf"})
402
+ # ax2t.plot(np.array(thresh), lick_met["lick_cdf"]["r"], "k")
403
+ r_major = lick_met["peak_lat"][1]
404
+ r_major_ratio = lick_met["peak_ratio"][1]
405
+ ax2t.fill(
406
+ [
407
+ r_major - 0.5 * bin_width,
408
+ r_major + 0.5 * bin_width,
409
+ r_major + 0.5 * bin_width,
410
+ r_major - 0.5 * bin_width,
411
+ ],
412
+ [0, 0, 1, 1],
413
+ "r",
414
+ alpha=0.2,
415
+ )
416
+ ax2t.set_title(f"R {r_major_ratio:.2f}")
417
+ ax2t.set_ylim(0, 1.1)
418
+ ax2.plot(
419
+ [win_bl[0], 0],
420
+ np.ones_like(win_bl) * lick_met["bl_lick"],
421
+ color=[0.3, 0.3, 0.3],
422
+ lw=4,
423
+ )
424
+ ax2.plot(
425
+ win_go,
426
+ np.ones_like(win_bl) * lick_met["resp_lick"],
427
+ color=[0.3, 0.3, 0.3],
428
+ lw=4,
429
+ )
460
430
 
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"
431
+ ax3 = fig.add_subplot(gs[1, :])
432
+ all_go_no_rwd = tbl_trials.loc[
433
+ (tbl_trials["animal_response"] != 2)
434
+ & (tbl_trials["rewarded_historyL"] == 0)
435
+ & (tbl_trials["rewarded_historyR"] == 0),
436
+ "goCue_start_time",
477
437
  ].values
478
- - tbl_trials.loc[
479
- tbl_trials["animal_response"] != 2, "delay_duration"
438
+ all_go_rwd = tbl_trials.loc[
439
+ (tbl_trials["animal_response"] != 2) & (tbl_trials["rewarded_historyL"] == 1)
440
+ | (tbl_trials["rewarded_historyR"] == 1),
441
+ "goCue_start_time",
480
442
  ].values
481
- )
482
- rate_go_rwd = rate_align(all_licks, all_go_rwd, win_go)
443
+ all_pre_nolick = (
444
+ tbl_trials.loc[tbl_trials["animal_response"] != 2, "goCue_start_time"].values
445
+ - tbl_trials.loc[tbl_trials["animal_response"] != 2, "delay_duration"].values
446
+ )
447
+ rate_go_rwd = rate_align(all_licks, all_go_rwd, win_go)
483
448
 
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)
449
+ ax3.plot(
450
+ all_go_no_rwd,
451
+ lick_met["resp_lick_trial"],
452
+ label="Resp-noRwd",
453
+ color="r",
454
+ )
455
+ ax3.plot(
456
+ all_pre_nolick,
457
+ lick_met["bl_lick_trial"],
458
+ label="baseline",
459
+ color="grey",
460
+ )
461
+ ax3.plot(all_go_rwd, rate_go_rwd, label="Resp-Rwd", color="b")
462
+ ax3.set_ylabel("lick rate")
463
+ ax3t = ax3.twinx()
464
+ ax3t.plot(
465
+ tbl_trials["goCue_start_time"],
466
+ lick_met["finish_ratio"],
467
+ color="g",
468
+ label="finish",
469
+ )
470
+ ax3t.set_ylabel("Ratio")
471
+ temp = lick_met["resp_score"]
472
+ ax3.set_title(f"Resp score {temp:.2f}")
473
+ ax3.legend()
474
+ ax3t.legend()
475
+ plt.suptitle(session_id)
511
476
  return fig, session_id
512
477
 
513
478
 
@@ -540,9 +505,8 @@ def rate_align(x, events, win):
540
505
 
541
506
  def plot_ILI(ili, title, subplot_spec, fig):
542
507
  """Plot ILI."""
543
- nested_gs = gridspec.GridSpecFromSubplotSpec(
544
- 2, 1, subplot_spec=subplot_spec
545
- )
508
+ nested_gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=subplot_spec)
509
+ nested_gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=subplot_spec)
546
510
  bins = np.arange(0, 2500, 20)
547
511
  ax1 = fig.add_subplot(nested_gs[0, 0])
548
512
  ax2 = fig.add_subplot(nested_gs[1, 0])
@@ -572,9 +536,8 @@ def plot_raster_rate(
572
536
  ):
573
537
  """Plot raster and rate aligned to events"""
574
538
  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
- )
539
+ nested_gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=subplot_spec)
540
+ nested_gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=subplot_spec)
578
541
  ax1 = fig.add_subplot(nested_gs[0, 0])
579
542
  ax2 = fig.add_subplot(nested_gs[1, 0])
580
543
 
@@ -584,12 +547,10 @@ def plot_raster_rate(
584
547
  ax1.set_title(title)
585
548
  ax1.set_xlim(tb, tf)
586
549
 
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
- )
550
+ counts_pre = np.searchsorted(np.sort(df.time.values), edges - 0.5 * bin_size)
551
+ counts_post = np.searchsorted(np.sort(df.time.values), edges + 0.5 * bin_size)
552
+ counts_pre = np.searchsorted(np.sort(df.time.values), edges - 0.5 * bin_size)
553
+ counts_post = np.searchsorted(np.sort(df.time.values), edges + 0.5 * bin_size)
593
554
  lick_rate = (counts_post - counts_pre) / (bin_size * len(align_events))
594
555
  ax2.plot(edges, lick_rate)
595
556
  ax2.set_title("lickRate")
@@ -606,9 +567,8 @@ if __name__ == "__main__":
606
567
 
607
568
  """Example."""
608
569
  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
- )
570
+ nwbfile = os.path.join(data_dir, "tests/data/705599_2024-05-31_14-06-54.nwb")
571
+ nwbfile = os.path.join(data_dir, "tests/data/705599_2024-05-31_14-06-54.nwb")
612
572
  # use of load_data depends on data structure
613
573
  nwb = load_nwb(nwbfile)
614
574
  fig, session_id = plot_lick_analysis(nwb)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aind-dynamic-foraging-basic-analysis
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Generated from aind-library-template
5
5
  Author: Allen Institute for Neural Dynamics
6
6
  License: MIT
@@ -0,0 +1,10 @@
1
+ aind_dynamic_foraging_basic_analysis/__init__.py,sha256=cqxKDIMh39uvMvlWUEtrLZH5vkoSzKlJ9VhEc7S7rjs,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=BTEPdby9iGBF91VOG0Zz4MXVy0qlqfisz1Ae8JOfjQc,19854
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.2.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
7
+ aind_dynamic_foraging_basic_analysis-0.2.2.dist-info/METADATA,sha256=oNeEkM96mvSSFf16brTqxlc2q5wWH1syl4xBNno2iT8,6690
8
+ aind_dynamic_foraging_basic_analysis-0.2.2.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
9
+ aind_dynamic_foraging_basic_analysis-0.2.2.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
10
+ aind_dynamic_foraging_basic_analysis-0.2.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.2.0)
2
+ Generator: setuptools (70.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,10 +0,0 @@
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,,