aind-dynamic-foraging-basic-analysis 0.3.4__py3-none-any.whl → 0.3.6__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.3.4"
3
+ __version__ = "0.3.6"
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
@@ -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, ax=None, fig=None, plot_bouts=False, processing="bright"
18
+ nwb,
19
+ ax=None,
20
+ fig=None,
21
+ plot_bouts=True,
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, 3))
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
- "G_1_dff-bright_bottom": 1,
82
- "G_1_dff-bright_top": 2,
83
- "G_2_dff-bright_bottom": 2,
84
- "G_2_dff-bright_top": 3,
85
- "R_1_dff-bright_bottom": 3,
86
- "R_1_dff-bright_top": 4,
87
- "R_2_dff-bright_bottom": 4,
88
- "R_2_dff-bright_top": 5,
89
- "G_1_dff-poly_bottom": 1,
90
- "G_1_dff-poly_top": 2,
91
- "G_2_dff-poly_bottom": 2,
92
- "G_2_dff-poly_top": 3,
93
- "R_1_dff-poly_bottom": 3,
94
- "R_1_dff-poly_top": 4,
95
- "R_2_dff-poly_bottom": 4,
96
- "R_2_dff-poly_top": 5,
97
- "G_1_dff-exp_bottom": 1,
98
- "G_1_dff-exp_top": 2,
99
- "G_2_dff-exp_bottom": 2,
100
- "G_2_dff-exp_top": 3,
101
- "R_1_dff-exp_bottom": 3,
102
- "R_1_dff-exp_top": 4,
103
- "R_2_dff-exp_bottom": 4,
104
- "R_2_dff-exp_top": 5,
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
- ylabels = ["left licks", "right licks", "left reward", "right reward"]
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, 1)
359
+ ax.set_ylim(0, 2)
308
360
  else:
309
- ax.set_ylim(0, 5)
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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aind-dynamic-foraging-basic-analysis
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Generated from aind-library-template
5
5
  Author: Allen Institute for Neural Dynamics
6
6
  License: MIT
@@ -58,6 +58,85 @@ To develop the code, run
58
58
  pip install -e .[dev]
59
59
  ```
60
60
 
61
+ ## Usage
62
+ ### Annotate licks
63
+ To create a dataframe of licks that has been annotated with licking bout starts/stops, cue responsive licks, reward triggered licks, and intertrial choices.
64
+ ```
65
+ import aind_dynamic_foraging_basic_analysis.licks.annotation as annotation
66
+ df_licks = annotation.annotate_licks(nwb)
67
+ ```
68
+
69
+ You can then plot interlick interval analyses with:
70
+ ```
71
+ import aind_dynamic_foraging_basic_analysis.licks.plot_interlick_interval as pii
72
+
73
+ #Plot interlick interval of all licks
74
+ pii.plot_interlick_interval(df_licks)
75
+
76
+ #plot interlick interval for left and right licks separately
77
+ pii.plot_interlick_interval(df_licks, categories='event')
78
+ ```
79
+
80
+ ### Create lick analysis report
81
+ To create a figure with several licking pattern analyses:
82
+
83
+ ```
84
+ import aind_dynamic_foraging_basic_analysis.licks.lick_analysis as lick_analysis
85
+ lick_analysis.plot_lick_analysis(nwb)
86
+ ```
87
+
88
+ ### Compute trial by trial metrics
89
+ To annotate the trials dataframe with trial by trial metrics:
90
+
91
+ ```
92
+ import aind_dynamic_foraging_basic_analysis.metrics.trial_metrics as tm
93
+ df_trials = tm.compute_all_trial_metrics(nwb)
94
+ ```
95
+
96
+ ### Plot interactive session scroller
97
+ ```
98
+ import aind_dynamic_foraging_basic_analysis.plot.plot_session_scroller as pss
99
+ pss.plot_session_scroller(nwb)
100
+ ```
101
+
102
+ To disable lick bout and other annotations:
103
+ ```
104
+ pss.plot_session_scroller(nwb,plot_bouts=False)
105
+ ```
106
+
107
+ This function will automatically plot FIP data if available. To change the processing method plotted use:
108
+ ```
109
+ pss.plot_session_scroller(nwb, processing="bright")
110
+ ```
111
+
112
+ To change which trial by trial metrics plotted:
113
+ ```
114
+ pss.plot_session_scroller(nwb, metrics=['response_rate'])
115
+ ```
116
+
117
+ ### Plot FIP PSTH
118
+ You can use the `plot_fip` module to compute and plot PSTHs for the FIP data.
119
+
120
+ To compare one channel to multiple event types
121
+ ```
122
+ from aind_dynamic_foraging_basic_analysis.plot import plot_fip as pf
123
+ channel = 'G_1_dff-poly'
124
+ rewarded_go_cues = nwb.df_trials.query('earned_reward == 1')['goCue_start_time_in_session'].values
125
+ unrewarded_go_cues = nwb.df_trials.query('earned_reward == 0')['goCue_start_time_in_session'].values
126
+ pf.plot_fip_psth_compare_alignments(
127
+ nwb,
128
+ {'rewarded goCue':rewarded_go_cues,'unrewarded goCue':unrewarded_go_cues},
129
+ channel,
130
+ censor=True
131
+ )
132
+ ```
133
+
134
+ To compare multiple channels to the same event type:
135
+ ```
136
+ pf.plot_fip_psth(nwb, 'goCue_start_time')
137
+ ```
138
+
139
+
61
140
  ## Contributing
62
141
 
63
142
  ### Linters and testing
@@ -1,16 +1,17 @@
1
- aind_dynamic_foraging_basic_analysis/__init__.py,sha256=xItEwN8jDvfSUlrqcNzC95XewK3CUDvA4s5a1TcEVH0,194
1
+ aind_dynamic_foraging_basic_analysis/__init__.py,sha256=H-AQ2L_mTKRJaT1nGng1JAhkc5q48TjX37u4Yyz3q7U,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=LopgwLGUwpXjAA6ofQfO-DBLPi0ybcDzYZTENd1CreM,11530
11
+ aind_dynamic_foraging_basic_analysis/plot/plot_session_scroller.py,sha256=EcFAHutiAhB1AyY4LGVjyqZuDflpYxqqjShw39G9GLg,13579
11
12
  aind_dynamic_foraging_basic_analysis/plot/style.py,sha256=UQxCqxnuMJcsRd6j-QGCA_E5XudrC9H2lNltmzmTK1E,1288
12
- aind_dynamic_foraging_basic_analysis-0.3.4.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
13
- aind_dynamic_foraging_basic_analysis-0.3.4.dist-info/METADATA,sha256=qL507m4jwA-6oCsEQ-qLfL3Iy1YdcukahqzU1tXN7f8,6737
14
- aind_dynamic_foraging_basic_analysis-0.3.4.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
15
- aind_dynamic_foraging_basic_analysis-0.3.4.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
16
- aind_dynamic_foraging_basic_analysis-0.3.4.dist-info/RECORD,,
13
+ aind_dynamic_foraging_basic_analysis-0.3.6.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
14
+ aind_dynamic_foraging_basic_analysis-0.3.6.dist-info/METADATA,sha256=aVnojUfh0G5nuJrF_lBEM7qaIe733UrJ7kS0YhqnfZg,9086
15
+ aind_dynamic_foraging_basic_analysis-0.3.6.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
16
+ aind_dynamic_foraging_basic_analysis-0.3.6.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
17
+ aind_dynamic_foraging_basic_analysis-0.3.6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (75.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5