Initial visualizations of your data

This notebook is meant to let you quickly look at your post-manually sorted data


In [1]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import os
import sys
from ephys import core, events, rasters
from importlib import reload

Load the data

Set 'block_path' to the path of the folder containing your post-manually sorted data


In [2]:
reload(core)
reload(events)
reload(rasters)
block_path = '/home/brad/cube/Ice/kperks/B1112/klusta/phy041316/Pen01_Lft_AP300_ML700__Site04_Z3300__B1112_cat_P01_S04_1/'
block_path = '/mnt/cube/btheilma/emily/B1171/klusta/phy103116/Pen02_Lft_AP2500_ML1200__Site14_Z2570__B1171_cat_P02_S14_1/'
block_path = '/mnt/cube/Nasim/experiments/B952/mansort/Pen01_Lft_AP750_ML1750__Site01_Z1100__B952_cat_P01_S01_1/'
#block_path = '/mnt/cube/Nasim/experiments/B952/mansort/Pen01_Lft_AP750_ML1750__Site01_Z1500__B952_cat_P01_S01_2/'

spikes     = core.load_spikes(block_path)
clusters   = core.load_clusters(block_path)
digmarks = events.load_events(block_path, 'DigMark')
fs         = core.load_fs(block_path)
trials     = events.load_trials(block_path)

rec        = 0

In [3]:
trials


Out[3]:
recording time_samples stimulus stimulus_end response response_time consequence correct datetime correction
0 0 80152 m_211_s_25_song_60sec_01 1280152 NaN NaN NaN NaN 2017-06-16 13:55:56.292247 False
1 0 1378271 G124-1 -1 2715450 NaN NaN NaN NaN 2017-06-16 13:57:01.198197 False
2 0 2793056 G124-1 -1 4130235 NaN NaN NaN NaN 2017-06-16 13:58:11.937447 False
3 0 4233521 165_s_02_01 5714172 NaN NaN NaN NaN 2017-06-16 13:59:23.960697 False
4 0 5798394 165_s_02_01 7279045 NaN NaN NaN NaN 2017-06-16 14:00:42.204347 False
5 0 7373423 m_497_17_s1 -1 8573423 NaN NaN NaN NaN 2017-06-16 14:02:00.955797 False
6 0 8669220 165_s_02_01 10149872 NaN NaN NaN NaN 2017-06-16 14:03:05.745647 False
7 0 10223880 G107-17_01 11669902 NaN NaN NaN NaN 2017-06-16 14:04:23.478647 False
8 0 11744939 m_211_s_25_song_60sec_01 12944939 NaN NaN NaN NaN 2017-06-16 14:05:39.531597 False
9 0 13044767 165_s_02_01 14525418 NaN NaN NaN NaN 2017-06-16 14:06:44.522997 False
10 0 14605267 165_s_02_01 16085918 NaN NaN NaN NaN 2017-06-16 14:08:02.547997 False
11 0 16215351 G107-17_01 17661372 NaN NaN NaN NaN 2017-06-16 14:09:23.052197 False
12 0 17755592 m_211_s_25_song_60sec_01 18955592 NaN NaN NaN NaN 2017-06-16 14:10:40.064247 False
13 0 19057022 m_211_s_25_song_60sec_01 20257022 NaN NaN NaN NaN 2017-06-16 14:11:45.135747 False
14 0 20373642 m_497_17_s1 -1 21573642 NaN NaN NaN NaN 2017-06-16 14:12:50.966747 False
15 0 21678572 m_497_17_s1 -1 22878572 NaN NaN NaN NaN 2017-06-16 14:13:56.213247 False
16 0 22997848 165_s_02_01 24478499 NaN NaN NaN NaN 2017-06-16 14:15:02.177047 False
17 0 24559590 165_s_02_01 26040242 NaN NaN NaN NaN 2017-06-16 14:16:20.264147 False
18 0 26144835 G107-17_01 27590856 NaN NaN NaN NaN 2017-06-16 14:17:39.526397 False
19 0 27678866 m_211_s_25_song_60sec_01 28878866 NaN NaN NaN NaN 2017-06-16 14:18:56.227947 False
20 0 28980271 G107-17_01 30426293 NaN NaN NaN NaN 2017-06-16 14:20:01.298197 False
21 0 30532731 G124-1 -1 31869910 NaN NaN NaN NaN 2017-06-16 14:21:18.921197 False
22 0 31985524 165_s_02_01 33466176 NaN NaN NaN NaN 2017-06-16 14:22:31.560847 False
23 0 33546319 G107-17_01 34992341 NaN NaN NaN NaN 2017-06-16 14:23:49.600597 False
24 0 35081667 165_s_02_01 36562318 NaN NaN NaN NaN 2017-06-16 14:25:06.367997 False
25 0 36643537 G124-1 -1 37980716 NaN NaN NaN NaN 2017-06-16 14:26:24.461497 False
26 0 38072497 m_497_17_s1 -1 39272497 NaN NaN NaN NaN 2017-06-16 14:27:35.909497 False
27 0 39393086 m_211_s_25_song_60sec_01 40593086 NaN NaN NaN NaN 2017-06-16 14:28:41.938947 False
28 0 40716484 165_s_02_01 42197135 NaN NaN NaN NaN 2017-06-16 14:29:48.108847 False
29 0 42288035 G124-1 -1 43625214 NaN NaN NaN NaN 2017-06-16 14:31:06.686397 False
... ... ... ... ... ... ... ... ... ... ...
70 0 100501040 G107-17_01 101947061 NaN NaN NaN NaN 2017-06-16 15:19:37.336647 False
71 0 102037034 m_211_s_25_song_60sec_01 103237034 NaN NaN NaN NaN 2017-06-16 15:20:54.136347 False
72 0 103324554 m_211_s_25_song_60sec_01 104524554 NaN NaN NaN NaN 2017-06-16 15:21:58.512347 False
73 0 104634656 m_497_17_s1 -1 105834656 NaN NaN NaN NaN 2017-06-16 15:23:04.017447 False
74 0 105963780 G107-17_01 107409802 NaN NaN NaN NaN 2017-06-16 15:24:10.473647 False
75 0 107507984 G124-1 -1 108845163 NaN NaN NaN NaN 2017-06-16 15:25:27.683847 False
76 0 108919568 m_497_17_s1 -1 110119568 NaN NaN NaN NaN 2017-06-16 15:26:38.263047 False
77 0 110244490 m_497_17_s1 -1 111444490 NaN NaN NaN NaN 2017-06-16 15:27:44.509147 False
78 0 111539978 G107-17_01 112986000 NaN NaN NaN NaN 2017-06-16 15:28:49.283547 False
79 0 113110236 m_497_17_s1 -1 114310236 NaN NaN NaN NaN 2017-06-16 15:30:07.796447 False
80 0 114432242 G124-1 -1 115769421 NaN NaN NaN NaN 2017-06-16 15:31:13.896747 False
81 0 115840735 G124-1 -1 117177914 NaN NaN NaN NaN 2017-06-16 15:32:24.321397 False
82 0 117301287 m_497_17_s1 -1 118501287 NaN NaN NaN NaN 2017-06-16 15:33:37.348997 False
83 0 118585495 G124-1 -1 119922674 NaN NaN NaN NaN 2017-06-16 15:34:41.559397 False
84 0 120025378 165_s_02_01 121506029 NaN NaN NaN NaN 2017-06-16 15:35:53.553547 False
85 0 121602918 m_211_s_25_song_60sec_01 122802918 NaN NaN NaN NaN 2017-06-16 15:37:12.430547 False
86 0 122890446 m_211_s_25_song_60sec_01 124090446 NaN NaN NaN NaN 2017-06-16 15:38:16.806947 False
87 0 124203837 m_211_s_25_song_60sec_01 125403837 NaN NaN NaN NaN 2017-06-16 15:39:22.476497 False
88 0 125476918 G107-17_01 126922940 NaN NaN NaN NaN 2017-06-16 15:40:26.130547 False
89 0 127006158 m_497_17_s1 -1 128206158 NaN NaN NaN NaN 2017-06-16 15:41:42.592547 False
90 0 128326166 G107-17_01 129772188 NaN NaN NaN NaN 2017-06-16 15:42:48.592947 False
91 0 129894521 G107-17_01 131340543 NaN NaN NaN NaN 2017-06-16 15:44:07.010697 False
92 0 131451322 G107-17_01 132897344 NaN NaN NaN NaN 2017-06-16 15:45:24.850747 False
93 0 133010481 G107-17_01 134456503 NaN NaN NaN NaN 2017-06-16 15:46:42.808697 False
94 0 134581558 G107-17_01 136027580 NaN NaN NaN NaN 2017-06-16 15:48:01.362547 False
95 0 136106152 m_497_17_s1 -1 137306152 NaN NaN NaN NaN 2017-06-16 15:49:17.592247 False
96 0 137404286 m_211_s_25_song_60sec_01 138604286 NaN NaN NaN NaN 2017-06-16 15:50:22.498947 False
97 0 138688825 G107-17_01 140134847 NaN NaN NaN NaN 2017-06-16 15:51:26.725897 False
98 0 140253448 165_s_02_01 141734099 NaN NaN NaN NaN 2017-06-16 15:52:44.957047 False
99 0 141817593 G124-1 -1 143154772 NaN NaN NaN NaN 2017-06-16 15:54:03.164297 False

100 rows × 10 columns


In [4]:
print(80152+fs*65.0)
print(digmarks['time_samples'])
print(digmarks['codes'])
rec=0

samps = 1378271
window = 65.0
print(samps+fs*window)

resp_mask = (
        (digmarks['recording']==rec)
        & (digmarks['time_samples']>samps)
        & (digmarks['time_samples']<(samps+fs*window))
        & digmarks['codes'].str.contains('[>#]')
        )
print(digmarks[resp_mask].shape[0])
print(fs)

starts = digmarks[digmarks['codes']==b'<']['time_samples'].values
stops = digmarks[digmarks['codes']==b'>']['time_samples'].values
print((stops - starts)/fs)


1380152.0
0          80152
1        1280152
2        1280154
3        1348157
4        1378271
5        2715450
6        2715452
7        2764399
8        2793056
9        4130235
10       4130237
11       4201046
12       4233521
13       5714172
14       5714173
15       5766826
16       5798394
17       7279045
18       7279047
19       7346059
20       7373423
21       8573423
22       8573425
23       8638078
24       8669220
25      10149872
26      10149873
27      10192578
28      10223880
29      11669902
         ...    
370    132897345
371    132976369
372    133010481
373    134456503
374    134456504
375    134546988
376    134581558
377    136027580
378    136027581
379    136080095
380    136106152
381    137306152
382    137306154
383    137374503
384    137404286
385    138604286
386    138604288
387    138653607
388    138688825
389    140134847
390    140134848
391    140221598
392    140253448
393    141734099
394    141734101
395    141784342
396    141817593
397    143154772
398    143154774
399    143207131
Name: time_samples, Length: 400, dtype: uint64
0      b'<'
1      b'>'
2      b'('
3      b')'
4      b'<'
5      b'>'
6      b'('
7      b')'
8      b'<'
9      b'>'
10     b'('
11     b')'
12     b'<'
13     b'>'
14     b'('
15     b')'
16     b'<'
17     b'>'
18     b'('
19     b')'
20     b'<'
21     b'>'
22     b'('
23     b')'
24     b'<'
25     b'>'
26     b'('
27     b')'
28     b'<'
29     b'>'
       ... 
370    b'('
371    b')'
372    b'<'
373    b'>'
374    b'('
375    b')'
376    b'<'
377    b'>'
378    b'('
379    b')'
380    b'<'
381    b'>'
382    b'('
383    b')'
384    b'<'
385    b'>'
386    b'('
387    b')'
388    b'<'
389    b'>'
390    b'('
391    b')'
392    b'<'
393    b'>'
394    b'('
395    b')'
396    b'<'
397    b'>'
398    b'('
399    b')'
Name: codes, Length: 400, dtype: object
2678271.0
0
20000.0
[ 60.       66.85895  66.85895  74.03255  74.03255  60.       74.0326
  72.3011   60.       74.03255  74.03255  72.30105  60.       60.       60.
  60.       74.03255  74.0326   72.30105  60.       72.3011   66.85895
  74.0326   72.3011   74.03255  66.85895  60.       60.       74.03255
  66.85895  66.859    60.       66.85895  72.3011   60.       66.859
  74.03255  74.03255  66.85895  66.859    60.       60.       60.       66.85895
  60.       74.0326   60.       60.       66.85895  74.0326   72.30105  60.
  60.       60.       74.03255  60.       60.       72.30105  60.       74.03255
  60.       60.       74.0326   72.3011   66.85895  66.85895  60.       72.3011
  74.0326   66.85895  72.30105  60.       60.       60.       72.3011
  66.85895  60.       60.       72.3011   60.       66.85895  66.85895  60.
  66.85895  74.03255  60.       60.       60.       72.3011   60.       72.3011
  72.3011   72.3011   72.3011   72.3011   60.       60.       72.3011
  74.03255  66.85895]

Plot rasters of each trial from each Good unit for all stims


In [ ]:
reload(rasters)
good_clusters = clusters[clusters['quality']=='Good']
good_cluster_ids = good_clusters['cluster'].unique()
print(len(good_cluster_ids))

In [ ]:
print(trials['stimulus'].unique())
print(len(trials['stimulus'].unique()))

In [ ]:
# We have 16 stims, so let's go with a 4x4 subplot layout
subplot_xy = [3, 2]
figsize = (22,20)
period = [-2, 2]

# Make a figure directory
raster_dir = os.path.join(block_path, 'rasters')
if not os.path.exists(raster_dir):
    os.mkdir(raster_dir)

for clusterID in good_cluster_ids:
    print(clusterID)
    fig = rasters.plot_unit_raster(spikes, trials, clusterID, period, rec, fs, subplot_xy, figsize)
    raster_fname = os.path.join(raster_dir, 'unit{}_raster.png'.format(str(clusterID)))
    fig.savefig(raster_fname)
    plt.close(fig)

See a cell / stim combo that looked interesting? Plot it here!


In [ ]:
reload(rasters)
clusterID = 62
stims = ['E_scaled_burung', 'C_scaled_burung', 'B_scaled_burung', 'H_scaled_burung']
trials_to_plot = trials[trials['stimulus'].isin(stims)]

subplot_xy=[2,2]
figsize=(22, 20)
period=[-2, 2]
raster_dir = os.path.join(block_path, 'rasters')


fig = rasters.plot_unit_raster(spikes, trials_to_plot, clusterID, period, rec, fs, subplot_xy, figsize)
raster_fname = os.path.join(raster_dir, 'unit{}_raster_specific_stims.png'.format(str(clusterID)))
fig.savefig(raster_fname)
plt.close(fig)

Plot Average, Gaussian Smoothed PSTHs for each Unit


In [ ]:

Plot rasters of each MUA unit


In [ ]:
mua_clusters = clusters[clusters['quality']=='MUA']
mua_cluster_ids = mua_clusters['cluster'].unique()
print(mua_cluster_ids)

In [ ]:
# We have 16 stims, so let's go with a 4x4 subplot layout
subplot_xy = [3,2]
figsize = (22,18)

# Make a figure directory
raster_dir = os.path.join(block_path, 'rasters')
if not os.path.exists(raster_dir):
    os.mkdir(raster_dir)

for clusterID in mua_cluster_ids[1:]:
    print(clusterID)
    fig = rasters.plot_unit_raster(spikes, trials, clusterID, period, rec, fs, subplot_xy, figsize)
    raster_fname = os.path.join(raster_dir, 'mua_unit{}_raster.png'.format(str(clusterID)))
    fig.savefig(raster_fname)
    plt.close(fig)

In [ ]: