aind-dynamic-foraging-basic-analysis 0.2.2__py3-none-any.whl → 0.2.4__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.2"
3
+ __version__ = "0.2.4"
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
@@ -1,17 +1,27 @@
1
1
  """Load packages."""
2
+ import os
2
3
  import numpy as np
3
4
  import matplotlib.pyplot as plt
4
5
  import matplotlib.gridspec as gridspec
5
6
  from aind_ephys_utils import align
6
7
  from pynwb import NWBHDF5IO
8
+ from hdmf_zarr import NWBZarrIO
7
9
  from scipy.stats import norm
8
10
 
9
11
 
10
12
  def load_nwb(nwb_file):
11
13
  """Load NWB file."""
12
- io = NWBHDF5IO(nwb_file, mode="r")
13
- nwb = io.read()
14
- return nwb
14
+ if os.path.isdir(nwb_file):
15
+ io = NWBZarrIO(nwb_file, mode="r")
16
+ nwb = io.read()
17
+ return nwb
18
+ elif os.path.isfile(nwb_file):
19
+ io = NWBHDF5IO(nwb_file, mode="r")
20
+ nwb = io.read()
21
+ return nwb
22
+ else:
23
+ print("nwb file does not exist.")
24
+ return None
15
25
 
16
26
 
17
27
  def plot_lick_analysis(nwb):
@@ -290,7 +300,7 @@ def cal_metrics(data):
290
300
  ref_kernel = np.convolve(ref, kernel)
291
301
  finish_kernel = np.convolve(finish.astype(float), kernel)
292
302
  finish_kernel = np.divide(finish_kernel, ref_kernel)
293
- finish_kernel = finish_kernel[int(0.5 * len(kernel)):-int(0.5 * len(kernel))]
303
+ finish_kernel = finish_kernel[int(0.5 * len(kernel)): -int(0.5 * len(kernel))]
294
304
  all_go_no_rwd = tbl_trials.loc[
295
305
  (tbl_trials["animal_response"] != 2)
296
306
  & (tbl_trials["rewarded_historyL"] == 0)
@@ -562,7 +572,6 @@ def plot_raster_rate(
562
572
 
563
573
  # example use
564
574
  if __name__ == "__main__":
565
- import os
566
575
  from pathlib import Path
567
576
 
568
577
  """Example."""
@@ -20,12 +20,18 @@ PHOTOSTIM_EPOCH_MAPPING = {
20
20
  "whole trial": "blue",
21
21
  }
22
22
 
23
+ """
24
+ Define a dictionary of plotting styles
25
+ This allows rapid regeneration of figures
26
+ """
27
+ STYLE = {"axis_ticks_fontsize": 12, "axis_fontsize": 16}
28
+
23
29
 
24
30
  def moving_average(a, n=3):
25
31
  """Compute moving average of a list or array."""
26
32
  ret = np.nancumsum(a, dtype=float)
27
33
  ret[n:] = ret[n:] - ret[:-n]
28
- return ret[n - 1:] / n
34
+ return ret[(n - 1):] / n
29
35
 
30
36
 
31
37
  def plot_foraging_session( # noqa: C901
@@ -313,3 +319,152 @@ def plot_foraging_session( # noqa: C901
313
319
  ax.remove()
314
320
 
315
321
  return ax_choice_reward.get_figure(), [ax_choice_reward, ax_reward_schedule]
322
+
323
+
324
+ def plot_session_scroller(df_events, ax=None, adjust_time=True): # pragma: no cover
325
+ """
326
+ Creates an interactive plot of the session.
327
+ Plots left/right licks/rewards, and go cues
328
+
329
+ pressing "left arrow" scrolls backwards in time
330
+ pressing "right arrow" scrolls forwards in time
331
+ pressing "up arrow" zooms out, in time
332
+ pressing "down arrow" zooms in, in time
333
+
334
+ df_events, is a tidy dataframe of session events generated by
335
+ aind_dynamic_foraging_data_utils.nwb_utils.create_events_df
336
+
337
+ ax is a pyplot figure axis. If None, a new figure is created
338
+
339
+ adjust_time (bool). If True, resets time=0 to the first event of the session
340
+
341
+ EXAMPLE:
342
+ df_events = nwb_utils.create_events_df(nwb_object)
343
+ plot_foraging_session.plot_session_scroller(df_events)
344
+ """
345
+ if ax is None:
346
+ fig, ax = plt.subplots(figsize=(15, 3))
347
+
348
+ if adjust_time:
349
+ df_events = df_events.copy()
350
+ df_events["timestamps"] = df_events["timestamps"] - df_events.iloc[0]["timestamps"]
351
+
352
+ xmin = df_events.iloc[0]["timestamps"]
353
+ xmax = xmin + 20
354
+ ax.set_xlim(xmin, xmax)
355
+
356
+ params = {
357
+ "left_lick_bottom": 0,
358
+ "left_lick_top": 0.25,
359
+ "right_lick_bottom": 0.75,
360
+ "right_lick_top": 1,
361
+ "left_reward_bottom": 0.25,
362
+ "left_reward_top": 0.5,
363
+ "right_reward_bottom": 0.5,
364
+ "right_reward_top": 0.75,
365
+ "go_cue_bottom": 0,
366
+ "go_cue_top": 1,
367
+ }
368
+ yticks = [
369
+ (params["left_lick_top"] - params["left_lick_bottom"]) / 2 + params["left_lick_bottom"],
370
+ (params["right_lick_top"] - params["right_lick_bottom"]) / 2 + params["right_lick_bottom"],
371
+ (params["left_reward_top"] - params["left_reward_bottom"]) / 2
372
+ + params["left_reward_bottom"],
373
+ (params["right_reward_top"] - params["right_reward_bottom"]) / 2
374
+ + params["right_reward_bottom"],
375
+ ]
376
+ ylabels = ["left licks", "right licks", "left reward", "right reward"]
377
+
378
+ left_licks = df_events.query('event == "left_lick_time"')
379
+ left_times = left_licks.timestamps.values
380
+ ax.vlines(
381
+ left_times,
382
+ params["left_lick_bottom"],
383
+ params["left_lick_top"],
384
+ alpha=1,
385
+ linewidth=2,
386
+ color="k",
387
+ )
388
+
389
+ right_licks = df_events.query('event == "right_lick_time"')
390
+ right_times = right_licks.timestamps.values
391
+ ax.vlines(
392
+ right_times,
393
+ params["right_lick_bottom"],
394
+ params["right_lick_top"],
395
+ alpha=1,
396
+ linewidth=2,
397
+ color="k",
398
+ )
399
+
400
+ left_reward_deliverys = df_events.query('event == "left_reward_delivery_time"')
401
+ left_times = left_reward_deliverys.timestamps.values
402
+ ax.vlines(
403
+ left_times,
404
+ params["left_reward_bottom"],
405
+ params["left_reward_top"],
406
+ alpha=1,
407
+ linewidth=2,
408
+ color="r",
409
+ )
410
+
411
+ right_reward_deliverys = df_events.query('event == "right_reward_delivery_time"')
412
+ right_times = right_reward_deliverys.timestamps.values
413
+ ax.vlines(
414
+ right_times,
415
+ params["right_reward_bottom"],
416
+ params["right_reward_top"],
417
+ alpha=1,
418
+ linewidth=2,
419
+ color="r",
420
+ )
421
+
422
+ go_cues = df_events.query('event == "goCue_start_time"')
423
+ go_cue_times = go_cues.timestamps.values
424
+ ax.vlines(
425
+ go_cue_times,
426
+ params["go_cue_bottom"],
427
+ params["go_cue_top"],
428
+ alpha=0.75,
429
+ linewidth=1,
430
+ color="b",
431
+ )
432
+
433
+ # Clean up plot
434
+ ax.set_yticks(yticks)
435
+ ax.set_yticklabels(ylabels, fontsize=STYLE["axis_ticks_fontsize"])
436
+ colors = ["k", "k", "r", "r"]
437
+ for tick, color in zip(ax.get_yticklabels(), colors):
438
+ tick.set_color(color)
439
+ ax.set_xlabel("time (s)", fontsize=STYLE["axis_fontsize"])
440
+ ax.set_ylim(0, 1)
441
+ ax.spines["top"].set_visible(False)
442
+ ax.spines["right"].set_visible(False)
443
+ plt.tight_layout()
444
+
445
+ def on_key_press(event):
446
+ """
447
+ Define interaction resonsivity
448
+ """
449
+ x = ax.get_xlim()
450
+ xmin = x[0]
451
+ xmax = x[1]
452
+ xStep = (xmax - xmin) / 4
453
+ if event.key == "<" or event.key == "," or event.key == "left":
454
+ xmin -= xStep
455
+ xmax -= xStep
456
+ elif event.key == ">" or event.key == "." or event.key == "right":
457
+ xmin += xStep
458
+ xmax += xStep
459
+ elif event.key == "up":
460
+ xmin -= xStep
461
+ xmax += xStep
462
+ elif event.key == "down":
463
+ xmin += xStep * (2 / 3)
464
+ xmax -= xStep * (2 / 3)
465
+ ax.set_xlim(xmin, xmax)
466
+ plt.draw()
467
+
468
+ kpid = fig.canvas.mpl_connect("key_press_event", on_key_press) # noqa: F841
469
+
470
+ return fig, ax
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aind-dynamic-foraging-basic-analysis
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: Generated from aind-library-template
5
5
  Author: Allen Institute for Neural Dynamics
6
6
  License: MIT
@@ -16,6 +16,7 @@ Requires-Dist: pynwb
16
16
  Requires-Dist: aind-ephys-utils
17
17
  Requires-Dist: pathlib
18
18
  Requires-Dist: pydantic
19
+ Requires-Dist: hdmf-zarr
19
20
  Provides-Extra: dev
20
21
  Requires-Dist: aind-dynamic-foraging-basic-analysis ; extra == 'dev'
21
22
  Requires-Dist: black ; extra == 'dev'
@@ -0,0 +1,10 @@
1
+ aind_dynamic_foraging_basic_analysis/__init__.py,sha256=ti2seCRcOdLN6XHXb1oId4MB7ghsQp5ScK7kSEs889k,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=Du6sP-Dfr6hBdRS98gc9Gp_kOq5TV_L80pDyj2WYctQ,20120
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=xvOCDutonCDmPBfzeVL49pkV_nqsTiLZPxT5Obn5s6w,16388
6
+ aind_dynamic_foraging_basic_analysis-0.2.4.dist-info/LICENSE,sha256=U0Y7B3gZJHXpjJVLgTQjM8e_c8w4JJpLgGhIdsoFR1Y,1092
7
+ aind_dynamic_foraging_basic_analysis-0.2.4.dist-info/METADATA,sha256=4cqyS-byYZFLPvZ3GxO7JiJ6MaE3T7aixFMmvNmp-ps,6715
8
+ aind_dynamic_foraging_basic_analysis-0.2.4.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
9
+ aind_dynamic_foraging_basic_analysis-0.2.4.dist-info/top_level.txt,sha256=gc5fvvd3cR7uvCHkTaSx-TcC4JmKMt9dBYMPncEO8VY,37
10
+ aind_dynamic_foraging_basic_analysis-0.2.4.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.3.0)
2
+ Generator: setuptools (73.0.1)
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=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,,