aind-dynamic-foraging-basic-analysis 0.3.4__py3-none-any.whl → 0.3.5__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/metrics/trial_metrics.py +61 -0
- aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py +82 -30
- {aind_dynamic_foraging_basic_analysis-0.3.4.dist-info → aind_dynamic_foraging_basic_analysis-0.3.5.dist-info}/METADATA +1 -1
- {aind_dynamic_foraging_basic_analysis-0.3.4.dist-info → aind_dynamic_foraging_basic_analysis-0.3.5.dist-info}/RECORD +8 -7
- {aind_dynamic_foraging_basic_analysis-0.3.4.dist-info → aind_dynamic_foraging_basic_analysis-0.3.5.dist-info}/WHEEL +1 -1
- {aind_dynamic_foraging_basic_analysis-0.3.4.dist-info → aind_dynamic_foraging_basic_analysis-0.3.5.dist-info}/LICENSE +0 -0
- {aind_dynamic_foraging_basic_analysis-0.3.4.dist-info → aind_dynamic_foraging_basic_analysis-0.3.5.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tools for computing trial by trial metrics
|
|
3
|
+
df_trials = compute_all_trial_metrics(nwb)
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# TODO, we might want to make these parameters metric specific
|
|
10
|
+
WIN_DUR = 15
|
|
11
|
+
MIN_EVENTS = 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def compute_all_trial_metrics(nwb):
|
|
15
|
+
"""
|
|
16
|
+
Computes all trial by trial metrics
|
|
17
|
+
|
|
18
|
+
response_rate, fraction of trials with a response
|
|
19
|
+
gocue_reward_rate, fraction of trials with a reward
|
|
20
|
+
response_reward_rate, fraction of trials with a reward,
|
|
21
|
+
computed only on trials with a response
|
|
22
|
+
choose_right_rate, fraction of trials where chose right,
|
|
23
|
+
computed only on trials with a response
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
if not hasattr(nwb, "df_trials"):
|
|
27
|
+
print("You need to compute df_trials: nwb_utils.create_trials_df(nwb)")
|
|
28
|
+
return
|
|
29
|
+
|
|
30
|
+
df = nwb.df_trials.copy()
|
|
31
|
+
|
|
32
|
+
df["RESPONDED"] = [x in [0, 1] for x in df["animal_response"].values]
|
|
33
|
+
# Rolling fraction of goCues with a response
|
|
34
|
+
df["response_rate"] = (
|
|
35
|
+
df["RESPONDED"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Rolling fraction of goCues with a response
|
|
39
|
+
df["gocue_reward_rate"] = (
|
|
40
|
+
df["earned_reward"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Rolling fraction of responses with a response
|
|
44
|
+
df["RESPONSE_REWARD"] = [
|
|
45
|
+
x[0] if x[1] else np.nan for x in zip(df["earned_reward"], df["RESPONDED"])
|
|
46
|
+
]
|
|
47
|
+
df["response_reward_rate"] = (
|
|
48
|
+
df["RESPONSE_REWARD"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Rolling fraction of choosing right
|
|
52
|
+
df["WENT_RIGHT"] = [x if x in [0, 1] else np.nan for x in df["animal_response"]]
|
|
53
|
+
df["choose_right_rate"] = (
|
|
54
|
+
df["WENT_RIGHT"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Clean up temp columns
|
|
58
|
+
drop_cols = ["RESPONDED", "RESPONSE_REWARD", "WENT_RIGHT"]
|
|
59
|
+
df = df.drop(columns=drop_cols)
|
|
60
|
+
|
|
61
|
+
return df
|
|
@@ -15,7 +15,12 @@ from aind_dynamic_foraging_basic_analysis.plot.style import (
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
18
|
-
nwb,
|
|
18
|
+
nwb,
|
|
19
|
+
ax=None,
|
|
20
|
+
fig=None,
|
|
21
|
+
plot_bouts=False,
|
|
22
|
+
processing="bright",
|
|
23
|
+
metrics=["pR", "pL", "response_rate"],
|
|
19
24
|
):
|
|
20
25
|
"""
|
|
21
26
|
Creates an interactive plot of the session.
|
|
@@ -56,10 +61,16 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
56
61
|
df_licks = nwb.df_licks
|
|
57
62
|
else:
|
|
58
63
|
df_licks = None
|
|
64
|
+
if not hasattr(nwb, "df_trials"):
|
|
65
|
+
print("computing df_trials")
|
|
66
|
+
nwb.df_trials = nu.create_df_trials(nwb)
|
|
67
|
+
df_trials = nwb.df_trials
|
|
68
|
+
else:
|
|
69
|
+
df_trials = nwb.df_trials
|
|
59
70
|
|
|
60
71
|
if ax is None:
|
|
61
72
|
if fip_df is None:
|
|
62
|
-
fig, ax = plt.subplots(figsize=(15,
|
|
73
|
+
fig, ax = plt.subplots(figsize=(15, 4))
|
|
63
74
|
else:
|
|
64
75
|
fig, ax = plt.subplots(figsize=(15, 8))
|
|
65
76
|
|
|
@@ -78,30 +89,32 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
78
89
|
"right_reward_top": 0.75,
|
|
79
90
|
"go_cue_bottom": 0,
|
|
80
91
|
"go_cue_top": 1,
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
92
|
+
"metrics_bottom": 1,
|
|
93
|
+
"metrics_top": 2,
|
|
94
|
+
"G_1_dff-bright_bottom": 2,
|
|
95
|
+
"G_1_dff-bright_top": 3,
|
|
96
|
+
"G_2_dff-bright_bottom": 3,
|
|
97
|
+
"G_2_dff-bright_top": 4,
|
|
98
|
+
"R_1_dff-bright_bottom": 4,
|
|
99
|
+
"R_1_dff-bright_top": 5,
|
|
100
|
+
"R_2_dff-bright_bottom": 5,
|
|
101
|
+
"R_2_dff-bright_top": 6,
|
|
102
|
+
"G_1_dff-poly_bottom": 2,
|
|
103
|
+
"G_1_dff-poly_top": 3,
|
|
104
|
+
"G_2_dff-poly_bottom": 3,
|
|
105
|
+
"G_2_dff-poly_top": 4,
|
|
106
|
+
"R_1_dff-poly_bottom": 4,
|
|
107
|
+
"R_1_dff-poly_top": 5,
|
|
108
|
+
"R_2_dff-poly_bottom": 5,
|
|
109
|
+
"R_2_dff-poly_top": 6,
|
|
110
|
+
"G_1_dff-exp_bottom": 2,
|
|
111
|
+
"G_1_dff-exp_top": 3,
|
|
112
|
+
"G_2_dff-exp_bottom": 3,
|
|
113
|
+
"G_2_dff-exp_top": 4,
|
|
114
|
+
"R_1_dff-exp_bottom": 4,
|
|
115
|
+
"R_1_dff-exp_top": 5,
|
|
116
|
+
"R_2_dff-exp_bottom": 5,
|
|
117
|
+
"R_2_dff-exp_top": 6,
|
|
105
118
|
}
|
|
106
119
|
yticks = [
|
|
107
120
|
(params["left_lick_top"] - params["left_lick_bottom"]) / 2 + params["left_lick_bottom"],
|
|
@@ -110,9 +123,22 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
110
123
|
+ params["left_reward_bottom"],
|
|
111
124
|
(params["right_reward_top"] - params["right_reward_bottom"]) / 2
|
|
112
125
|
+ params["right_reward_bottom"],
|
|
126
|
+
(params["metrics_top"] - params["metrics_bottom"]) * 0.25 + params["metrics_bottom"],
|
|
127
|
+
(params["metrics_top"] - params["metrics_bottom"]) * 0.50 + params["metrics_bottom"],
|
|
128
|
+
(params["metrics_top"] - params["metrics_bottom"]) * 0.75 + params["metrics_bottom"],
|
|
129
|
+
params["metrics_top"],
|
|
130
|
+
]
|
|
131
|
+
ylabels = [
|
|
132
|
+
"left licks",
|
|
133
|
+
"right licks",
|
|
134
|
+
"left reward",
|
|
135
|
+
"right reward",
|
|
136
|
+
"0.25",
|
|
137
|
+
"0.50",
|
|
138
|
+
"0.75",
|
|
139
|
+
"metrics",
|
|
113
140
|
]
|
|
114
|
-
|
|
115
|
-
ycolors = ["k", "k", "r", "r"]
|
|
141
|
+
ycolors = ["k", "k", "r", "r", "darkgray", "darkgray", "darkgray", "k"]
|
|
116
142
|
|
|
117
143
|
if fip_df is not None:
|
|
118
144
|
fip_channels = [
|
|
@@ -260,6 +286,12 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
260
286
|
"bD",
|
|
261
287
|
)
|
|
262
288
|
|
|
289
|
+
# Plot baiting
|
|
290
|
+
bait_right = df_trials.query("bait_right")["goCue_start_time_in_session"].values
|
|
291
|
+
bait_left = df_trials.query("bait_left")["goCue_start_time_in_session"].values
|
|
292
|
+
ax.plot(bait_right, [params["go_cue_top"] - 0.05] * len(bait_right), "ms", label="baited")
|
|
293
|
+
ax.plot(bait_left, [params["go_cue_bottom"] + 0.05] * len(bait_left), "ms")
|
|
294
|
+
|
|
263
295
|
left_reward_deliverys = df_events.query('event == "left_reward_delivery_time"')
|
|
264
296
|
left_times = left_reward_deliverys.timestamps.values
|
|
265
297
|
ax.vlines(
|
|
@@ -295,6 +327,26 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
295
327
|
label="go cue",
|
|
296
328
|
)
|
|
297
329
|
|
|
330
|
+
# plot metrics
|
|
331
|
+
ax.axhline(params["metrics_bottom"], color="k", linewidth=0.5, alpha=0.25)
|
|
332
|
+
go_cue_times_doubled = np.repeat(go_cue_times, 2)[1:]
|
|
333
|
+
if "pR" in metrics:
|
|
334
|
+
pR = params["metrics_bottom"] + df_trials["reward_probabilityR"]
|
|
335
|
+
pR = np.repeat(pR, 2)[:-1]
|
|
336
|
+
ax.plot(go_cue_times_doubled, pR, color="r", label="pR")
|
|
337
|
+
if "pL" in metrics:
|
|
338
|
+
pL = params["metrics_bottom"] + df_trials["reward_probabilityL"]
|
|
339
|
+
pL = np.repeat(pL, 2)[:-1]
|
|
340
|
+
ax.plot(go_cue_times_doubled, pL, color="b", label="pL")
|
|
341
|
+
|
|
342
|
+
# plot metrics if they are available
|
|
343
|
+
for metric in metrics:
|
|
344
|
+
if metric in df_trials:
|
|
345
|
+
values = df_trials[metric] + params["metrics_bottom"]
|
|
346
|
+
ax.plot(go_cue_times, values, label=metric)
|
|
347
|
+
elif metric not in ["pL", "pR"]:
|
|
348
|
+
print('Metric "{}" not available in df_trials'.format(metric))
|
|
349
|
+
|
|
298
350
|
# Clean up plot
|
|
299
351
|
ax.legend(framealpha=1, loc="lower left", reverse=True)
|
|
300
352
|
ax.set_yticks(yticks)
|
|
@@ -304,9 +356,9 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
304
356
|
tick.set_color(color)
|
|
305
357
|
ax.set_xlabel("time (s)", fontsize=STYLE["axis_fontsize"])
|
|
306
358
|
if fip_df is None:
|
|
307
|
-
ax.set_ylim(0,
|
|
359
|
+
ax.set_ylim(0, 2)
|
|
308
360
|
else:
|
|
309
|
-
ax.set_ylim(0,
|
|
361
|
+
ax.set_ylim(0, 6)
|
|
310
362
|
ax.spines["top"].set_visible(False)
|
|
311
363
|
ax.spines["right"].set_visible(False)
|
|
312
364
|
if fip_df is not None:
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=
|
|
1
|
+
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=Ah_eOo3rRf3GaWJVJ3DKF-ybFmKGLtCxkyUp1DM59HI,194
|
|
2
2
|
aind_dynamic_foraging_basic_analysis/foraging_efficiency.py,sha256=9IhMqQo_aU_RFsDu8D2P3-Qu3J72bo7sASC3dkUQ2YE,8634
|
|
3
3
|
aind_dynamic_foraging_basic_analysis/data_model/foraging_session.py,sha256=7PFHfEIXP3QcjFS9Fk88S7PzBBYnUYw81WBjjEu_UKo,3536
|
|
4
4
|
aind_dynamic_foraging_basic_analysis/licks/annotation.py,sha256=jJpP9crk4pOgij8d2FxHvOlWNkfnvO2R4hW2ANAMcjE,11997
|
|
5
5
|
aind_dynamic_foraging_basic_analysis/licks/lick_analysis.py,sha256=tgFqowzXHMRW2aCXhDi5SFE3Lfj_VMuMG1QPggkW9WE,20158
|
|
6
6
|
aind_dynamic_foraging_basic_analysis/licks/plot_interlick_interval.py,sha256=fldIAzB_Hml7S0xlfUCK1UeNpzRaWdrufXd4FaqKooY,3333
|
|
7
|
+
aind_dynamic_foraging_basic_analysis/metrics/trial_metrics.py,sha256=3I62mCRc-FnaclL-UhJqqtTeg1nsm4MlOCPIH35bzNY,1982
|
|
7
8
|
aind_dynamic_foraging_basic_analysis/plot/plot_fip.py,sha256=_KiEAonrsDrV-ReipDzKEPoKFTgV_E9IUCwVJTR5jac,8414
|
|
8
9
|
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py,sha256=yfxqEu522DkBaD20R8BBBAmII4asu3mZ3psGUdUxqEQ,11534
|
|
9
10
|
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session_plotly.py,sha256=wE-kl1W6KZjaMX08bjawWfSuse6KW6YNSxDbdph3XK0,6715
|
|
10
|
-
aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py,sha256=
|
|
11
|
+
aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py,sha256=spnOzgqHPaDva6zd6-kzfCV8Vu1rFZ8x1SpQADntER8,13580
|
|
11
12
|
aind_dynamic_foraging_basic_analysis/plot/style.py,sha256=UQxCqxnuMJcsRd6j-QGCA_E5XudrC9H2lNltmzmTK1E,1288
|
|
12
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
13
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
14
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
15
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
16
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
13
|
+
aind_dynamic_foraging_basic_analysis-0.3.5.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
|
|
14
|
+
aind_dynamic_foraging_basic_analysis-0.3.5.dist-info/METADATA,sha256=OTp223BgsxDfjkoZpb5MnVSsPJDUVjBQC90XvixklkU,6737
|
|
15
|
+
aind_dynamic_foraging_basic_analysis-0.3.5.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
16
|
+
aind_dynamic_foraging_basic_analysis-0.3.5.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
|
|
17
|
+
aind_dynamic_foraging_basic_analysis-0.3.5.dist-info/RECORD,,
|
|
File without changes
|