aind-dynamic-foraging-basic-analysis 0.3.6__py3-none-any.whl → 0.3.7__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 +136 -19
- aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py +192 -50
- aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py +7 -2
- {aind_dynamic_foraging_basic_analysis-0.3.6.dist-info → aind_dynamic_foraging_basic_analysis-0.3.7.dist-info}/METADATA +1 -1
- {aind_dynamic_foraging_basic_analysis-0.3.6.dist-info → aind_dynamic_foraging_basic_analysis-0.3.7.dist-info}/RECORD +9 -9
- {aind_dynamic_foraging_basic_analysis-0.3.6.dist-info → aind_dynamic_foraging_basic_analysis-0.3.7.dist-info}/LICENSE +0 -0
- {aind_dynamic_foraging_basic_analysis-0.3.6.dist-info → aind_dynamic_foraging_basic_analysis-0.3.7.dist-info}/WHEEL +0 -0
- {aind_dynamic_foraging_basic_analysis-0.3.6.dist-info → aind_dynamic_foraging_basic_analysis-0.3.7.dist-info}/top_level.txt +0 -0
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Tools for computing trial by trial metrics
|
|
3
|
-
df_trials =
|
|
3
|
+
df_trials = compute_trial_metrics(nwb)
|
|
4
|
+
df_trials = compute_bias(nwb)
|
|
4
5
|
|
|
5
6
|
"""
|
|
6
7
|
|
|
8
|
+
import pandas as pd
|
|
7
9
|
import numpy as np
|
|
8
10
|
|
|
11
|
+
import aind_dynamic_foraging_models.logistic_regression.model as model
|
|
12
|
+
|
|
9
13
|
# TODO, we might want to make these parameters metric specific
|
|
10
14
|
WIN_DUR = 15
|
|
11
15
|
MIN_EVENTS = 2
|
|
12
16
|
|
|
13
17
|
|
|
14
|
-
def
|
|
18
|
+
def compute_trial_metrics(nwb):
|
|
15
19
|
"""
|
|
16
|
-
Computes
|
|
20
|
+
Computes trial by trial metrics
|
|
17
21
|
|
|
18
22
|
response_rate, fraction of trials with a response
|
|
19
23
|
gocue_reward_rate, fraction of trials with a reward
|
|
@@ -27,35 +31,148 @@ def compute_all_trial_metrics(nwb):
|
|
|
27
31
|
print("You need to compute df_trials: nwb_utils.create_trials_df(nwb)")
|
|
28
32
|
return
|
|
29
33
|
|
|
30
|
-
|
|
34
|
+
df_trials = nwb.df_trials.copy()
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
df_trials["RESPONDED"] = [x in [0, 1] for x in df_trials["animal_response"].values]
|
|
33
37
|
# Rolling fraction of goCues with a response
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
df_trials["response_rate"] = (
|
|
39
|
+
df_trials["RESPONDED"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
36
40
|
)
|
|
37
41
|
|
|
38
42
|
# Rolling fraction of goCues with a response
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
df_trials["gocue_reward_rate"] = (
|
|
44
|
+
df_trials["earned_reward"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
41
45
|
)
|
|
42
46
|
|
|
43
47
|
# Rolling fraction of responses with a response
|
|
44
|
-
|
|
45
|
-
x[0] if x[1] else np.nan for x in zip(
|
|
48
|
+
df_trials["RESPONSE_REWARD"] = [
|
|
49
|
+
x[0] if x[1] else np.nan for x in zip(df_trials["earned_reward"], df_trials["RESPONDED"])
|
|
46
50
|
]
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
df_trials["response_reward_rate"] = (
|
|
52
|
+
df_trials["RESPONSE_REWARD"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
49
53
|
)
|
|
50
54
|
|
|
51
55
|
# Rolling fraction of choosing right
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
df_trials["WENT_RIGHT"] = [x if x in [0, 1] else np.nan for x in df_trials["animal_response"]]
|
|
57
|
+
df_trials["choose_right_rate"] = (
|
|
58
|
+
df_trials["WENT_RIGHT"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean()
|
|
55
59
|
)
|
|
56
60
|
|
|
61
|
+
# TODO, add from process_nwb
|
|
62
|
+
# trial duration (stop-time - start-time) (start/stop time, or gocue to gocue?)
|
|
63
|
+
# n_licks_left (# of left licks in response window)
|
|
64
|
+
# n_licks_left_total (# of left licks from goCue to next go cue)
|
|
65
|
+
# Same for Right, same for all
|
|
66
|
+
# intertrial choices (boolean)
|
|
67
|
+
# number of intertrial choices
|
|
68
|
+
# number of intertrial switches
|
|
69
|
+
# response switch or repeat
|
|
70
|
+
|
|
57
71
|
# Clean up temp columns
|
|
58
|
-
drop_cols = [
|
|
59
|
-
|
|
72
|
+
drop_cols = [
|
|
73
|
+
"RESPONDED",
|
|
74
|
+
"RESPONSE_REWARD",
|
|
75
|
+
"WENT_RIGHT",
|
|
76
|
+
]
|
|
77
|
+
df_trials = df_trials.drop(columns=drop_cols)
|
|
78
|
+
|
|
79
|
+
return df_trials
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def compute_bias(nwb):
|
|
83
|
+
"""
|
|
84
|
+
Computes side bias by fitting a logistic regression model
|
|
85
|
+
returns trials table with the following columns:
|
|
86
|
+
bias, the side bias
|
|
87
|
+
bias_ci_lower, the lower confidence interval on the bias
|
|
88
|
+
bias_ci_upper, the uppwer confidence interval on the bias
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
# Parameters for computing bias
|
|
92
|
+
n_trials_back = 5
|
|
93
|
+
max_window = 200
|
|
94
|
+
cv = 1
|
|
95
|
+
compute_every = 10
|
|
96
|
+
BIAS_LIMIT = 10
|
|
97
|
+
|
|
98
|
+
# Make sure trials table has been computed
|
|
99
|
+
if not hasattr(nwb, "df_trials"):
|
|
100
|
+
print("You need to compute df_trials: nwb_utils.create_trials_df(nwb)")
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
# extract choice and reward
|
|
104
|
+
df_trials = nwb.df_trials.copy()
|
|
105
|
+
df_trials["choice"] = [np.nan if x == 2 else x for x in df_trials["animal_response"]]
|
|
106
|
+
df_trials["reward"] = [
|
|
107
|
+
any(x) for x in zip(df_trials["earned_reward"], df_trials["extra_reward"])
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
# Set up lists to store results
|
|
111
|
+
bias = []
|
|
112
|
+
ci_lower = []
|
|
113
|
+
ci_upper = []
|
|
114
|
+
C = []
|
|
115
|
+
|
|
116
|
+
# Iterate over trials and compute
|
|
117
|
+
compute_on = np.arange(compute_every, len(df_trials), compute_every)
|
|
118
|
+
for i in compute_on:
|
|
119
|
+
# Determine interval to compute on
|
|
120
|
+
start = np.max([0, i - max_window])
|
|
121
|
+
end = i
|
|
122
|
+
|
|
123
|
+
# extract choice and reward
|
|
124
|
+
choice = df_trials.loc[start:end]["choice"].values
|
|
125
|
+
reward = df_trials.loc[start:end]["reward"].values
|
|
126
|
+
|
|
127
|
+
# Determine if we have valid data to fit model
|
|
128
|
+
unique = np.unique(choice[~np.isnan(choice)])
|
|
129
|
+
if len(unique) == 0:
|
|
130
|
+
# no choices, report bias confidence as (-inf, +inf)
|
|
131
|
+
bias.append(np.nan)
|
|
132
|
+
ci_lower.append(-BIAS_LIMIT)
|
|
133
|
+
ci_upper.append(BIAS_LIMIT)
|
|
134
|
+
C.append(np.nan)
|
|
135
|
+
elif len(unique) == 2:
|
|
136
|
+
# Fit model
|
|
137
|
+
out = model.fit_logistic_regression(
|
|
138
|
+
choice, reward, n_trial_back=n_trials_back, cv=cv, fit_exponential=False
|
|
139
|
+
)
|
|
140
|
+
bias.append(out["df_beta"].loc["bias"]["bootstrap_mean"].values[0])
|
|
141
|
+
ci_lower.append(out["df_beta"].loc["bias"]["bootstrap_CI_lower"].values[0])
|
|
142
|
+
ci_upper.append(out["df_beta"].loc["bias"]["bootstrap_CI_upper"].values[0])
|
|
143
|
+
C.append(out["C"])
|
|
144
|
+
elif unique[0] == 0:
|
|
145
|
+
# only left choices, report bias confidence as (-inf, 0)
|
|
146
|
+
bias.append(-1)
|
|
147
|
+
ci_lower.append(-BIAS_LIMIT)
|
|
148
|
+
ci_upper.append(0)
|
|
149
|
+
C.append(np.nan)
|
|
150
|
+
elif unique[0] == 1:
|
|
151
|
+
# only right choices, report bias confidence as (0, +inf)
|
|
152
|
+
bias.append(+1)
|
|
153
|
+
ci_lower.append(0)
|
|
154
|
+
ci_upper.append(BIAS_LIMIT)
|
|
155
|
+
C.append(np.nan)
|
|
156
|
+
|
|
157
|
+
# Pack results into a dataframe
|
|
158
|
+
df = pd.DataFrame()
|
|
159
|
+
df["trial"] = compute_on
|
|
160
|
+
df["bias"] = bias
|
|
161
|
+
df["bias_ci_lower"] = ci_lower
|
|
162
|
+
df["bias_ci_upper"] = ci_upper
|
|
163
|
+
df["bias_C"] = C
|
|
164
|
+
|
|
165
|
+
# merge onto trials dataframe
|
|
166
|
+
df_trials = pd.merge(
|
|
167
|
+
nwb.df_trials.drop(columns=["bias", "bias_ci_lower", "bias_ci_upper"], errors="ignore"),
|
|
168
|
+
df[["trial", "bias", "bias_ci_lower", "bias_ci_upper"]],
|
|
169
|
+
how="left",
|
|
170
|
+
on=["trial"],
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# fill in bias on non-computed trials
|
|
174
|
+
df_trials["bias"] = df_trials["bias"].bfill().ffill()
|
|
175
|
+
df_trials["bias_ci_lower"] = df_trials["bias_ci_lower"].bfill().ffill()
|
|
176
|
+
df_trials["bias_ci_upper"] = df_trials["bias_ci_upper"].bfill().ffill()
|
|
60
177
|
|
|
61
|
-
return
|
|
178
|
+
return df_trials
|
|
@@ -22,6 +22,56 @@ def moving_average(a, n=3):
|
|
|
22
22
|
return ret[(n - 1) :] / n # noqa: E203
|
|
23
23
|
|
|
24
24
|
|
|
25
|
+
def plot_foraging_session_nwb(nwb, **kwargs):
|
|
26
|
+
"""
|
|
27
|
+
Wrapper function that extracts fields
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
if not hasattr(nwb, "df_trials"):
|
|
31
|
+
print("You need to compute df_trials: nwb_utils.create_trials_df(nwb)")
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
if "bias" not in nwb.df_trials:
|
|
35
|
+
fig, axes = plot_foraging_session(
|
|
36
|
+
[np.nan if x == 2 else x for x in nwb.df_trials["animal_response"].values],
|
|
37
|
+
nwb.df_trials["earned_reward"].values,
|
|
38
|
+
[nwb.df_trials["reward_probabilityL"], nwb.df_trials["reward_probabilityR"]],
|
|
39
|
+
**kwargs,
|
|
40
|
+
)
|
|
41
|
+
else:
|
|
42
|
+
if "plot_list" not in kwargs:
|
|
43
|
+
kwargs["plot_list"] = ["choice", "finished", "reward_prob", "bias"]
|
|
44
|
+
fig, axes = plot_foraging_session(
|
|
45
|
+
[np.nan if x == 2 else x for x in nwb.df_trials["animal_response"].values],
|
|
46
|
+
nwb.df_trials["earned_reward"].values,
|
|
47
|
+
[nwb.df_trials["reward_probabilityL"], nwb.df_trials["reward_probabilityR"]],
|
|
48
|
+
bias=nwb.df_trials["bias"].values,
|
|
49
|
+
bias_lower=nwb.df_trials["bias_ci_lower"].values,
|
|
50
|
+
bias_upper=nwb.df_trials["bias_ci_upper"].values,
|
|
51
|
+
autowater_offered=nwb.df_trials[["auto_waterL", "auto_waterR"]].any(axis=1),
|
|
52
|
+
**kwargs,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Add some text info
|
|
56
|
+
# TODO, waiting for AIND metadata to get integrated before adding this info:
|
|
57
|
+
# {df_session.metadata.rig.iloc[0]}, {df_session.metadata.user_name.iloc[0]}\n'
|
|
58
|
+
# f'FORAGING finished {df_session.session_stats.finished_trials.iloc[0]} '
|
|
59
|
+
# f'ignored {df_session.session_stats.ignored_trials.iloc[0]} + '
|
|
60
|
+
# f'AUTOWATER collected {df_session.session_stats.autowater_collected.iloc[0]} '
|
|
61
|
+
# f'ignored {df_session.session_stats.autowater_ignored.iloc[0]}\n'
|
|
62
|
+
# f'FORAGING finished rate {df_session.session_stats.finished_rate.iloc[0]:.2%}, '
|
|
63
|
+
axes[0].text(
|
|
64
|
+
0,
|
|
65
|
+
1.05,
|
|
66
|
+
f"{nwb.session_id}\n"
|
|
67
|
+
f'Total trials {len(nwb.df_trials)}, ignored {np.sum(nwb.df_trials["animal_response"]==2)},'
|
|
68
|
+
f' left {np.sum(nwb.df_trials["animal_response"] == 0)},'
|
|
69
|
+
f' right {np.sum(nwb.df_trials["animal_response"] == 1)}',
|
|
70
|
+
fontsize=8,
|
|
71
|
+
transform=axes[0].transAxes,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
25
75
|
def plot_foraging_session( # noqa: C901
|
|
26
76
|
choice_history: Union[List, np.ndarray],
|
|
27
77
|
reward_history: Union[List, np.ndarray],
|
|
@@ -34,6 +84,10 @@ def plot_foraging_session( # noqa: C901
|
|
|
34
84
|
base_color: str = "y",
|
|
35
85
|
ax: plt.Axes = None,
|
|
36
86
|
vertical: bool = False,
|
|
87
|
+
bias: Union[List, np.ndarray] = None,
|
|
88
|
+
bias_lower: Union[List, np.ndarray] = None,
|
|
89
|
+
bias_upper: Union[List, np.ndarray] = None,
|
|
90
|
+
plot_list: List = ["choice", "finished", "reward_prob"],
|
|
37
91
|
) -> Tuple[plt.Figure, List[plt.Axes]]:
|
|
38
92
|
"""Plot dynamic foraging session.
|
|
39
93
|
|
|
@@ -124,30 +178,78 @@ def plot_foraging_session( # noqa: C901
|
|
|
124
178
|
# Rewarded trials (real foraging, autowater excluded)
|
|
125
179
|
xx = np.nonzero(rewarded_excluding_autowater)[0] + 1
|
|
126
180
|
yy = 0.5 + (choice_history[rewarded_excluding_autowater] - 0.5) * 1.4
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
181
|
+
yy_temp = choice_history[rewarded_excluding_autowater]
|
|
182
|
+
yy_right = yy_temp[yy_temp > 0.5] + 0.05
|
|
183
|
+
xx_right = xx[yy_temp > 0.5]
|
|
184
|
+
yy_left = yy_temp[yy_temp < 0.5] - 0.05
|
|
185
|
+
xx_left = xx[yy_temp < 0.5]
|
|
186
|
+
if not vertical:
|
|
187
|
+
ax_choice_reward.vlines(
|
|
188
|
+
xx_right,
|
|
189
|
+
yy_right,
|
|
190
|
+
yy_right + 0.1,
|
|
191
|
+
alpha=1,
|
|
192
|
+
linewidth=1,
|
|
193
|
+
color="black",
|
|
194
|
+
label="Rewarded choices",
|
|
195
|
+
)
|
|
196
|
+
ax_choice_reward.vlines(
|
|
197
|
+
xx_left,
|
|
198
|
+
yy_left - 0.1,
|
|
199
|
+
yy_left,
|
|
200
|
+
alpha=1,
|
|
201
|
+
linewidth=1,
|
|
202
|
+
color="black",
|
|
203
|
+
)
|
|
204
|
+
else:
|
|
205
|
+
ax_choice_reward.plot(
|
|
206
|
+
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
207
|
+
"|" if not vertical else "_",
|
|
208
|
+
color="black",
|
|
209
|
+
markersize=10,
|
|
210
|
+
markeredgewidth=2,
|
|
211
|
+
label="Rewarded choices",
|
|
212
|
+
)
|
|
135
213
|
|
|
136
214
|
# Unrewarded trials (real foraging; not ignored or autowater trials)
|
|
137
215
|
xx = np.nonzero(unrewarded_trials)[0] + 1
|
|
138
216
|
yy = 0.5 + (choice_history[unrewarded_trials] - 0.5) * 1.4
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
217
|
+
yy_temp = choice_history[unrewarded_trials]
|
|
218
|
+
yy_right = yy_temp[yy_temp > 0.5]
|
|
219
|
+
xx_right = xx[yy_temp > 0.5]
|
|
220
|
+
yy_left = yy_temp[yy_temp < 0.5]
|
|
221
|
+
xx_left = xx[yy_temp < 0.5]
|
|
222
|
+
if not vertical:
|
|
223
|
+
ax_choice_reward.vlines(
|
|
224
|
+
xx_right,
|
|
225
|
+
yy_right + 0.05,
|
|
226
|
+
yy_right + 0.1,
|
|
227
|
+
alpha=1,
|
|
228
|
+
linewidth=1,
|
|
229
|
+
color="gray",
|
|
230
|
+
label="Unrewarded choices",
|
|
231
|
+
)
|
|
232
|
+
ax_choice_reward.vlines(
|
|
233
|
+
xx_left,
|
|
234
|
+
yy_left - 0.1,
|
|
235
|
+
yy_left - 0.05,
|
|
236
|
+
alpha=1,
|
|
237
|
+
linewidth=1,
|
|
238
|
+
color="gray",
|
|
239
|
+
)
|
|
240
|
+
else:
|
|
241
|
+
ax_choice_reward.plot(
|
|
242
|
+
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
243
|
+
"|" if not vertical else "_",
|
|
244
|
+
color="gray",
|
|
245
|
+
markersize=6,
|
|
246
|
+
markeredgewidth=1,
|
|
247
|
+
label="Unrewarded choices",
|
|
248
|
+
)
|
|
147
249
|
|
|
148
250
|
# Ignored trials
|
|
149
251
|
xx = np.nonzero(ignored & ~autowater_ignored)[0] + 1
|
|
150
|
-
yy = [1.
|
|
252
|
+
yy = [1.2] * sum(ignored & ~autowater_ignored)
|
|
151
253
|
ax_choice_reward.plot(
|
|
152
254
|
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
153
255
|
"x",
|
|
@@ -162,18 +264,44 @@ def plot_foraging_session( # noqa: C901
|
|
|
162
264
|
# Autowater offered and collected
|
|
163
265
|
xx = np.nonzero(autowater_collected)[0] + 1
|
|
164
266
|
yy = 0.5 + (choice_history[autowater_collected] - 0.5) * 1.4
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
267
|
+
|
|
268
|
+
yy_temp = choice_history[autowater_collected]
|
|
269
|
+
yy_right = yy_temp[yy_temp > 0.5] + 0.05
|
|
270
|
+
xx_right = xx[yy_temp > 0.5]
|
|
271
|
+
yy_left = yy_temp[yy_temp < 0.5] - 0.05
|
|
272
|
+
xx_left = xx[yy_temp < 0.5]
|
|
273
|
+
|
|
274
|
+
if not vertical:
|
|
275
|
+
ax_choice_reward.vlines(
|
|
276
|
+
xx_right,
|
|
277
|
+
yy_right,
|
|
278
|
+
yy_right + 0.1,
|
|
279
|
+
alpha=1,
|
|
280
|
+
linewidth=1,
|
|
281
|
+
color="royalblue",
|
|
282
|
+
label="Autowater collected",
|
|
283
|
+
)
|
|
284
|
+
ax_choice_reward.vlines(
|
|
285
|
+
xx_left,
|
|
286
|
+
yy_left - 0.1,
|
|
287
|
+
yy_left,
|
|
288
|
+
alpha=1,
|
|
289
|
+
linewidth=1,
|
|
290
|
+
color="royalblue",
|
|
291
|
+
)
|
|
292
|
+
else:
|
|
293
|
+
ax_choice_reward.plot(
|
|
294
|
+
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
295
|
+
"|" if not vertical else "_",
|
|
296
|
+
color="royalblue",
|
|
297
|
+
markersize=10,
|
|
298
|
+
markeredgewidth=2,
|
|
299
|
+
label="Autowater collected",
|
|
300
|
+
)
|
|
173
301
|
|
|
174
302
|
# Also highlight the autowater offered but still ignored
|
|
175
303
|
xx = np.nonzero(autowater_ignored)[0] + 1
|
|
176
|
-
yy = [1.
|
|
304
|
+
yy = [1.2] * sum(autowater_ignored)
|
|
177
305
|
ax_choice_reward.plot(
|
|
178
306
|
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
179
307
|
"x",
|
|
@@ -186,12 +314,13 @@ def plot_foraging_session( # noqa: C901
|
|
|
186
314
|
# Base probability
|
|
187
315
|
xx = np.arange(0, n_trials) + 1
|
|
188
316
|
yy = p_reward_fraction
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
317
|
+
if "reward_prob" in plot_list:
|
|
318
|
+
ax_choice_reward.plot(
|
|
319
|
+
*(xx, yy) if not vertical else [*(yy, xx)],
|
|
320
|
+
color=base_color,
|
|
321
|
+
label="Base rew. prob.",
|
|
322
|
+
lw=1.5,
|
|
323
|
+
)
|
|
195
324
|
|
|
196
325
|
# Smoothed choice history
|
|
197
326
|
y = moving_average(choice_history, smooth_factor) / (
|
|
@@ -199,24 +328,37 @@ def plot_foraging_session( # noqa: C901
|
|
|
199
328
|
)
|
|
200
329
|
y[y > 100] = np.nan
|
|
201
330
|
x = np.arange(0, len(y)) + int(smooth_factor / 2) + 1
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
331
|
+
if "choice" in plot_list:
|
|
332
|
+
ax_choice_reward.plot(
|
|
333
|
+
*(x, y) if not vertical else [*(y, x)],
|
|
334
|
+
linewidth=1.5,
|
|
335
|
+
color="black",
|
|
336
|
+
label="Choice (smooth = %g)" % smooth_factor,
|
|
337
|
+
)
|
|
208
338
|
|
|
209
339
|
# finished ratio
|
|
210
340
|
if np.sum(np.isnan(choice_history)):
|
|
211
341
|
x = np.arange(0, len(y)) + int(smooth_factor / 2) + 1
|
|
212
342
|
y = moving_average(~np.isnan(choice_history), smooth_factor)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
343
|
+
if "finished" in plot_list:
|
|
344
|
+
ax_choice_reward.plot(
|
|
345
|
+
*(x, y) if not vertical else [*(y, x)],
|
|
346
|
+
linewidth=0.8,
|
|
347
|
+
color="m",
|
|
348
|
+
alpha=1,
|
|
349
|
+
label="Finished (smooth = %g)" % smooth_factor,
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
# Bias
|
|
353
|
+
if ("bias" in plot_list) and (bias is not None):
|
|
354
|
+
bias = (np.array(bias) + 1) / (2)
|
|
355
|
+
bias_lower = (np.array(bias_lower) + 1) / (2)
|
|
356
|
+
bias_upper = (np.array(bias_upper) + 1) / (2)
|
|
357
|
+
bias_lower[bias_lower < 0] = 0
|
|
358
|
+
bias_upper[bias_upper > 1] = 1
|
|
359
|
+
ax_choice_reward.plot(xx, bias, color="g", lw=1.5, label="bias")
|
|
360
|
+
ax_choice_reward.fill_between(xx, bias_lower, bias_upper, color="g", alpha=0.25)
|
|
361
|
+
ax_choice_reward.plot(xx, [0.5] * len(xx), color="g", linestyle="--", alpha=0.2, lw=1)
|
|
220
362
|
|
|
221
363
|
# add valid ranage
|
|
222
364
|
if valid_range is not None:
|
|
@@ -267,18 +409,17 @@ def plot_foraging_session( # noqa: C901
|
|
|
267
409
|
ax_reward_schedule.legend(fontsize=5, ncol=1, loc="upper left", bbox_to_anchor=(0, 1))
|
|
268
410
|
|
|
269
411
|
if not vertical:
|
|
270
|
-
ax_choice_reward.set_yticks([0, 1])
|
|
271
|
-
ax_choice_reward.set_yticklabels(["Left", "Right"])
|
|
272
|
-
ax_choice_reward.legend(fontsize=6, loc="upper left", bbox_to_anchor=(0.
|
|
412
|
+
ax_choice_reward.set_yticks([0, 1, 1.2])
|
|
413
|
+
ax_choice_reward.set_yticklabels(["Left", "Right", "Ignored"])
|
|
414
|
+
ax_choice_reward.legend(fontsize=6, loc="upper left", bbox_to_anchor=(0.4, 1.3), ncol=3)
|
|
273
415
|
|
|
274
|
-
# sns.despine(trim=True, bottom=True, ax=ax_1)
|
|
275
416
|
ax_choice_reward.spines["top"].set_visible(False)
|
|
276
417
|
ax_choice_reward.spines["right"].set_visible(False)
|
|
277
418
|
ax_choice_reward.spines["bottom"].set_visible(False)
|
|
278
419
|
ax_choice_reward.tick_params(labelbottom=False)
|
|
279
420
|
ax_choice_reward.xaxis.set_ticks_position("none")
|
|
421
|
+
ax_choice_reward.set_ylim([-0.15, 1.25])
|
|
280
422
|
|
|
281
|
-
# sns.despine(trim=True, ax=ax_2)
|
|
282
423
|
ax_reward_schedule.set_ylim([0, 1])
|
|
283
424
|
ax_reward_schedule.spines["top"].set_visible(False)
|
|
284
425
|
ax_reward_schedule.spines["right"].set_visible(False)
|
|
@@ -305,5 +446,6 @@ def plot_foraging_session( # noqa: C901
|
|
|
305
446
|
ax_reward_schedule.set(ylabel="Trial number")
|
|
306
447
|
|
|
307
448
|
ax.remove()
|
|
449
|
+
plt.tight_layout()
|
|
308
450
|
|
|
309
451
|
return ax_choice_reward.get_figure(), [ax_choice_reward, ax_reward_schedule]
|
|
@@ -38,6 +38,11 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
38
38
|
|
|
39
39
|
plot_bouts (bool), if True, plot licks colored by segmented lick bouts
|
|
40
40
|
|
|
41
|
+
processing (str) processing method for FIP data to plot
|
|
42
|
+
|
|
43
|
+
metrics (list of strings), list of metrics to plot. Must be either 'pR','pL' or
|
|
44
|
+
columns in nwb.df_trials
|
|
45
|
+
|
|
41
46
|
EXAMPLES:
|
|
42
47
|
plot_foraging_session.plot_session_scroller(nwb)
|
|
43
48
|
plot_foraging_session.plot_session_scroller(nwb, plot_bouts=True)
|
|
@@ -333,11 +338,11 @@ def plot_session_scroller( # noqa: C901 pragma: no cover
|
|
|
333
338
|
if "pR" in metrics:
|
|
334
339
|
pR = params["metrics_bottom"] + df_trials["reward_probabilityR"]
|
|
335
340
|
pR = np.repeat(pR, 2)[:-1]
|
|
336
|
-
ax.plot(go_cue_times_doubled, pR, color="
|
|
341
|
+
ax.plot(go_cue_times_doubled, pR, color="b", label="pR")
|
|
337
342
|
if "pL" in metrics:
|
|
338
343
|
pL = params["metrics_bottom"] + df_trials["reward_probabilityL"]
|
|
339
344
|
pL = np.repeat(pL, 2)[:-1]
|
|
340
|
-
ax.plot(go_cue_times_doubled, pL, color="
|
|
345
|
+
ax.plot(go_cue_times_doubled, pL, color="r", label="pL")
|
|
341
346
|
|
|
342
347
|
# plot metrics if they are available
|
|
343
348
|
for metric in metrics:
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=
|
|
1
|
+
aind_dynamic_foraging_basic_analysis/__init__.py,sha256=GwSwodJOZ5rjajJNIuRfHC01Q7NXsipYyPbQ5YLIL64,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=
|
|
7
|
+
aind_dynamic_foraging_basic_analysis/metrics/trial_metrics.py,sha256=Wo9WC8ZxrDG5CbOl83IIEOXuHfGz5CVCLOuD_P3E3zw,6107
|
|
8
8
|
aind_dynamic_foraging_basic_analysis/plot/plot_fip.py,sha256=_KiEAonrsDrV-ReipDzKEPoKFTgV_E9IUCwVJTR5jac,8414
|
|
9
|
-
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py,sha256=
|
|
9
|
+
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session.py,sha256=_aQ5yqrUVvttDCDk9X6JHOFS4L6rBQEOVMuBGvJNVu0,16828
|
|
10
10
|
aind_dynamic_foraging_basic_analysis/plot/plot_foraging_session_plotly.py,sha256=wE-kl1W6KZjaMX08bjawWfSuse6KW6YNSxDbdph3XK0,6715
|
|
11
|
-
aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py,sha256=
|
|
11
|
+
aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py,sha256=8Xd7HtImMl5XFY7WJTX1fwQ5b04Bto-Ye_tKvlDMCHg,13758
|
|
12
12
|
aind_dynamic_foraging_basic_analysis/plot/style.py,sha256=UQxCqxnuMJcsRd6j-QGCA_E5XudrC9H2lNltmzmTK1E,1288
|
|
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.
|
|
17
|
-
aind_dynamic_foraging_basic_analysis-0.3.
|
|
13
|
+
aind_dynamic_foraging_basic_analysis-0.3.7.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
|
|
14
|
+
aind_dynamic_foraging_basic_analysis-0.3.7.dist-info/METADATA,sha256=_7A99Q3LsJxizEg-KUQl_ra1s1X7mBNHQE8Bi4x8jMA,9086
|
|
15
|
+
aind_dynamic_foraging_basic_analysis-0.3.7.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
16
|
+
aind_dynamic_foraging_basic_analysis-0.3.7.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
|
|
17
|
+
aind_dynamic_foraging_basic_analysis-0.3.7.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|