In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
import os
import expipe
import pathlib
import numpy as np
import spatial_maps.stats as stats
import septum_mec
import septum_mec.analysis.data_processing as dp
import septum_mec.analysis.registration
import head_direction.head as head
import spatial_maps as sp
import speed_cells.speed as spd
import re
import joblib
import multiprocessing
import shutil
import psutil
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from distutils.dir_util import copy_tree
from neo import SpikeTrain
import scipy

from tqdm import tqdm_notebook as tqdm
from tqdm._tqdm_notebook import tqdm_notebook
tqdm_notebook.pandas()

from spike_statistics.core import permutation_resampling

from spikewaveform.core import calculate_waveform_features_from_template, cluster_waveform_features

from septum_mec.analysis.plotting import violinplot
19:18:12 [I] klustakwik KlustaKwik2 version 0.2.6
In [3]:
%matplotlib inline
plt.rc('axes', titlesize=12)
plt.rcParams.update({
    'font.size': 12, 
    'figure.figsize': (6, 4), 
    'figure.dpi': 150
})

output_path = pathlib.Path("output") / "stimulus-response"
(output_path / "statistics").mkdir(exist_ok=True, parents=True)
(output_path / "figures").mkdir(exist_ok=True, parents=True)
output_path.mkdir(exist_ok=True)
In [4]:
data_loader = dp.Data()
actions = data_loader.actions
project = data_loader.project
In [5]:
identification_action = actions['identify-neurons']
sessions = pd.read_csv(identification_action.data_path('sessions'))
units = pd.read_csv(identification_action.data_path('units'))
session_units = pd.merge(sessions, units, on='action')
In [6]:
stim_action = actions['stimulus-response']
stim_results = pd.read_csv(stim_action.data_path('results'))
In [9]:
# lfp_results has old unit id's but correct on (action, unit_name, channel_group)
stim_results = stim_results.drop('unit_id', axis=1)
In [7]:
statistics_action = actions['calculate-statistics']
shuffling = actions['shuffling']

statistics_results = pd.read_csv(statistics_action.data_path('results'))
statistics_results = session_units.merge(statistics_results, how='left')
quantiles_95 = pd.read_csv(shuffling.data_path('quantiles_95'))
action_columns = ['action', 'channel_group', 'unit_name']
data = pd.merge(statistics_results, quantiles_95, on=action_columns, suffixes=("", "_threshold"))
In [8]:
data['unit_day'] = data.apply(lambda x: str(x.unit_idnum) + '_' + x.action.split('-')[1], axis=1)
In [10]:
data = data.merge(stim_results, how='left')
In [11]:
waveform_action = actions['waveform-analysis']
waveform_results = pd.read_csv(waveform_action.data_path('results')).drop('template', axis=1)
In [12]:
data = data.merge(waveform_results, how='left')
In [13]:
colors = ['#d95f02','#e7298a']
labels = ['11 Hz', '30 HZ']
queries = ['frequency==11', 'frequency==30']
In [14]:
data.bs = data.bs.astype(bool)
In [15]:
grid_query = 'gridness > gridness_threshold and information_rate > information_rate_threshold'
gridcell_sessions = data.query(grid_query)
print("Number of gridcells", len(gridcell_sessions))
# print("Number of animals", len(gridcell_sessions.groupby(['entity'])))
Number of gridcells 225
In [22]:
data['gridcell'] = data.isin(data.query(grid_query))
In [23]:
data.query('baseline and gridcell')
Out[23]:
action channel_group unit_name average_rate speed_score out_field_mean_rate in_field_mean_rate max_field_mean_rate max_rate sparsity ... p_e_peak t_i_peak p_i_peak half_width peak_to_trough average_firing_rate bs bs_stim bs_ctrl gridcell
32 1833-260619-1 0 118 5.946164 0.169495 4.138169 10.175750 16.836097 29.863371 0.633240 ... NaN NaN NaN 0.272875 0.602667 5.945508 True NaN 1.0 True
34 1833-260619-1 0 130 2.860363 0.081075 1.362852 6.837975 10.333063 21.846576 0.424446 ... NaN NaN NaN 0.226452 0.274814 2.860048 False NaN 0.0 True
35 1833-260619-1 0 132 3.366046 0.072301 1.204876 8.320200 11.903539 24.820419 0.393028 ... NaN NaN NaN 0.247266 0.570104 3.365674 True NaN 1.0 True
39 1833-260619-1 1 116 17.473449 0.193373 12.435315 25.886509 35.066123 58.438209 0.760804 ... NaN NaN NaN 0.284542 0.644111 17.471520 True NaN 1.0 True
40 1833-260619-1 1 126 5.892390 0.183633 4.008668 10.376607 11.424828 22.616252 0.698596 ... NaN NaN NaN 0.259920 0.581698 5.891739 True NaN 1.0 True
42 1833-260619-1 3 114 13.438331 0.224642 10.451118 18.904366 20.482248 37.829102 0.841781 ... NaN NaN NaN 0.263630 0.596746 13.436847 True NaN 1.0 True
43 1833-260619-1 5 100 17.448630 0.144593 12.651420 25.885399 31.780144 50.983827 0.823859 ... NaN NaN NaN 0.281399 0.607354 17.446704 True NaN 1.0 True
45 1833-260619-1 6 102 10.841667 0.235736 7.896926 16.159949 15.994156 37.844022 0.799767 ... NaN NaN NaN 0.279177 0.585152 10.840470 True NaN 1.0 True
48 1833-260619-1 6 112 5.891356 0.226892 4.028409 10.441355 13.169649 24.406383 0.643995 ... NaN NaN NaN 0.282336 0.711705 5.890705 True NaN 1.0 True
49 1833-260619-1 6 124 7.915120 0.182376 4.543545 14.013583 17.035745 30.787249 0.646322 ... NaN NaN NaN 0.285816 0.603160 7.914246 True NaN 1.0 True
57 1834-150319-3 3 61 17.163920 0.021890 12.070353 23.188083 24.427655 44.829894 0.837844 ... NaN NaN NaN 0.277867 0.588852 17.162446 True NaN 1.0 True
124 1833-010719-1 1 219 2.868256 0.170572 1.391229 6.759410 8.941986 21.915347 0.446442 ... NaN NaN NaN 0.271262 0.615002 2.868000 True NaN 1.0 True
125 1833-010719-1 1 221 6.912671 0.090486 4.070879 11.915337 24.220877 32.274461 0.661683 ... NaN NaN NaN 0.307694 0.659653 6.912052 True NaN 1.0 True
126 1833-010719-1 1 229 4.230245 0.018811 1.546702 8.504585 15.581766 33.782863 0.456739 ... NaN NaN NaN 0.267708 0.630543 4.229867 True NaN 1.0 True
128 1833-010719-1 1 8 16.737459 0.254297 12.420895 25.377508 23.273238 52.301684 0.802417 ... NaN NaN NaN 0.289100 0.673221 16.735961 True NaN 1.0 True
129 1833-010719-1 2 202 25.977054 0.226032 21.598716 37.463629 43.547728 66.169116 0.862176 ... NaN NaN NaN 0.290402 0.650772 25.974728 True NaN 1.0 True
131 1833-010719-1 3 171 14.687550 0.163959 11.038136 20.488701 21.342234 45.144706 0.858017 ... NaN NaN NaN 0.272160 0.620429 14.686236 True NaN 1.0 True
132 1833-010719-1 3 198 18.659249 0.282318 15.427596 26.715844 33.932272 51.441681 0.860475 ... NaN NaN NaN 0.241405 0.595513 18.657578 True NaN 1.0 True
134 1833-010719-1 3 240 3.107182 0.076765 1.059941 7.228602 12.831970 33.059125 0.383354 ... NaN NaN NaN 0.269911 0.609574 3.106903 True NaN 1.0 True
135 1833-010719-1 5 134 6.214363 0.168450 4.835608 9.832902 18.534635 33.761835 0.750893 ... NaN NaN NaN 0.273069 0.651265 6.213807 True NaN 1.0 True
136 1833-010719-1 5 144 2.226506 0.119543 1.188425 5.927293 13.273928 26.877971 0.358918 ... NaN NaN NaN 0.263251 0.629310 2.226306 True NaN 1.0 True
209 1833-050619-1 2 99 3.350056 0.095012 1.224499 7.669547 14.470606 29.613931 0.384212 ... NaN NaN NaN 0.251027 0.593786 3.347881 True NaN 1.0 True
214 1833-050619-1 6 60 7.177620 0.259306 5.263129 11.558126 13.097257 24.533320 0.764622 ... NaN NaN NaN 0.296577 0.631283 7.172961 True NaN 1.0 True
215 1833-050619-1 6 64 16.944449 0.243525 13.371230 26.025889 33.591762 60.449939 0.824103 ... NaN NaN NaN 0.295235 0.633010 16.933450 True NaN 1.0 True
216 1833-050619-1 6 91 3.325889 0.155904 2.039584 7.702821 9.078369 21.975777 0.462461 ... NaN NaN NaN 0.268553 0.618949 3.323730 True NaN 1.0 True
221 1833-060619-1 4 172 2.654829 0.119661 1.666324 6.169001 7.323174 22.931784 0.457083 ... NaN NaN NaN 0.263816 0.607601 2.654511 True NaN 1.0 True
223 1833-060619-1 5 164 3.083686 0.021853 1.755081 5.101697 8.325821 21.146134 0.566049 ... NaN NaN NaN 0.313833 0.646825 3.083316 True NaN 1.0 True
227 1833-060619-1 6 170 3.080462 0.155454 1.816201 6.197439 8.744690 18.172981 0.529688 ... NaN NaN NaN 0.261029 0.596500 3.080092 True NaN 1.0 True
262 1834-150319-1 3 95 19.609185 0.063354 14.334866 25.933220 29.106613 53.460587 0.857509 ... NaN NaN NaN 0.282343 0.604147 19.498454 True NaN 1.0 True
274 1839-120619-1 5 158 12.579822 0.285708 9.656518 23.105339 25.311402 59.566964 0.660943 ... NaN NaN NaN 0.265655 0.574791 12.578109 True NaN 1.0 True
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1130 1834-010319-4 0 7 18.428099 0.073675 13.995565 25.034061 27.551569 45.574876 0.864388 ... NaN NaN NaN 0.284016 0.615742 18.426477 True NaN 1.0 True
1151 1833-200619-1 4 165 4.093726 0.112030 1.560769 9.952907 16.871964 34.400735 0.371794 ... NaN NaN NaN 0.272936 0.784972 4.093056 True NaN 1.0 True
1152 1833-200619-1 6 163 17.705502 0.202908 14.631392 24.895637 34.144570 51.462522 0.877996 ... NaN NaN NaN 0.303012 0.661133 17.702603 True NaN 1.0 True
1153 1833-200619-1 6 171 4.061107 0.058014 1.879235 7.260758 11.252257 20.574695 0.561983 ... NaN NaN NaN 0.310060 0.632763 4.060442 True NaN 1.0 True
1154 1833-200619-1 6 206 3.982277 0.150630 2.316705 7.168058 9.286450 19.626376 0.618229 ... NaN NaN NaN 0.290294 0.618949 3.981625 True NaN 1.0 True
1155 1833-200619-1 6 240 4.089649 0.098818 1.539874 10.560745 15.374288 32.783007 0.358157 ... NaN NaN NaN 0.263375 0.622896 4.088979 True NaN 1.0 True
1156 1833-200619-1 7 143 9.300587 0.218310 6.750717 13.150023 13.197378 25.067697 0.825910 ... NaN NaN NaN 0.289628 0.650032 9.299064 True NaN 1.0 True
1162 1839-120619-3 5 131 17.773050 0.076020 9.779864 29.707618 42.215165 77.486029 0.651012 ... NaN NaN NaN 0.245031 0.528413 17.770859 True NaN 1.0 True
1165 1839-120619-3 6 133 2.612293 0.053873 1.055067 6.992168 9.603099 17.060484 0.372375 ... NaN NaN NaN 0.239301 0.531126 2.611971 True NaN 1.0 True
1167 1839-120619-3 7 119 4.950355 0.132893 3.636504 7.175598 7.291281 14.571674 0.836244 ... NaN NaN NaN 0.284221 0.610068 4.949745 True NaN 1.0 True
1168 1839-120619-3 7 127 5.407801 0.091931 3.251329 15.356306 18.617758 37.590469 0.414271 ... NaN NaN NaN 0.273572 0.611548 5.407135 True NaN 1.0 True
1199 1833-260619-3 0 140 3.564682 0.063184 2.498756 5.782665 8.770230 17.134986 0.720704 ... NaN NaN NaN 0.189559 0.248665 3.564358 False NaN 0.0 True
1200 1833-260619-3 0 141 2.694224 0.094154 1.691471 5.502054 10.395725 20.328752 0.519950 ... NaN NaN NaN 0.225575 0.277528 2.693978 False NaN 0.0 True
1202 1833-260619-3 0 182 5.289030 0.148720 3.342163 10.892485 16.803801 30.523793 0.544679 ... NaN NaN NaN 0.275930 0.594526 5.288548 True NaN 1.0 True
1203 1833-260619-3 0 194 6.485358 0.096207 3.706339 12.069498 18.212336 29.243464 0.590584 ... NaN NaN NaN 0.222604 0.576271 6.484767 True NaN 1.0 True
1205 1833-260619-3 0 209 3.425497 0.085117 1.306754 8.551145 11.161798 29.652423 0.378044 ... NaN NaN NaN 0.244049 0.571337 3.425185 True NaN 1.0 True
1207 1833-260619-3 1 170 26.841716 0.218178 22.328079 38.090240 50.981983 74.601637 0.857579 ... NaN NaN NaN 0.257469 0.636957 26.839270 True NaN 1.0 True
1208 1833-260619-3 1 207 4.589791 0.088439 2.309667 8.938164 10.731362 25.229471 0.538208 ... NaN NaN NaN 0.252255 0.587372 4.589373 True NaN 1.0 True
1211 1833-260619-3 3 176 7.407735 0.156101 5.622472 11.694017 16.474141 32.870310 0.757528 ... NaN NaN NaN 0.261129 0.592306 7.407060 True NaN 1.0 True
1213 1833-260619-3 5 111 9.222663 0.179913 6.341652 14.990045 17.803066 32.423819 0.732917 ... NaN NaN NaN 0.277189 0.615988 9.221822 True NaN 1.0 True
1216 1833-260619-3 6 142 9.359639 0.129023 6.738758 14.564994 20.758052 44.189302 0.773930 ... NaN NaN NaN 0.300175 0.610068 9.358786 True NaN 1.0 True
1218 1833-260619-3 6 192 7.836336 0.170862 4.889011 13.019928 17.648343 34.791219 0.715811 ... NaN NaN NaN 0.287132 0.616235 7.835622 True NaN 1.0 True
1222 1833-200619-3 0 91 7.072750 0.074100 4.679924 11.282597 18.578196 35.109099 0.713088 ... NaN NaN NaN 0.293775 0.657679 7.071948 True NaN 1.0 True
1228 1833-200619-3 3 82 15.697615 0.127761 12.267443 21.346293 27.567344 38.706425 0.874674 ... NaN NaN NaN 0.252895 0.600200 15.695836 True NaN 1.0 True
1229 1833-200619-3 4 113 11.770313 0.136640 6.835310 20.280536 22.248766 44.143227 0.676058 ... NaN NaN NaN 0.271023 0.699617 11.768979 True NaN 1.0 True
1231 1833-200619-3 5 59 4.442527 0.110165 2.926793 7.344323 8.786494 20.320606 0.722984 ... NaN NaN NaN 0.343906 0.698383 4.442023 True NaN 1.0 True
1232 1833-200619-3 6 120 22.461229 0.268466 18.182326 32.115585 33.640870 62.235139 0.833921 ... NaN NaN NaN 0.294291 0.639177 22.458685 True NaN 1.0 True
1233 1833-200619-3 6 126 3.102942 0.090727 1.447857 6.981766 9.945472 21.048478 0.436204 ... NaN NaN NaN 0.304748 0.641151 3.102590 True NaN 1.0 True
1234 1833-200619-3 6 132 6.901437 0.072648 4.231220 14.073295 20.697950 36.231604 0.612146 ... NaN NaN NaN 0.277708 0.585645 6.900656 True NaN 1.0 True
1235 1833-200619-3 6 150 3.767582 0.114920 1.422876 10.607271 13.651769 34.348592 0.332963 ... NaN NaN NaN 0.258204 0.608094 3.767155 True NaN 1.0 True

130 rows × 51 columns

In [61]:
density = True
cumulative = True
histtype = 'step'
lw = 2
bins = {
    't_i_peak': None,
    't_e_peak': None,
    'p_i_peak': None,
    'p_e_peak': None,
}
xlabel = {
    't_i_peak': 's',
    't_e_peak': 's',
    'p_i_peak': 'prob',
    'p_e_peak': 'prob',
}

for cell_type in ['gridcell', 'not bs']:
    for key in bins:
        fig = plt.figure(figsize=(3.5,2.2))
        plt.suptitle(key + ' ' + cell_type)
        legend_lines = []
        for color, query, label in zip(colors, queries, labels):
            data.query(query + ' and ' + cell_type)[key].hist(
                bins=bins[key], density=density, cumulative=cumulative, lw=lw, 
                histtype=histtype, color=color)
            legend_lines.append(matplotlib.lines.Line2D([0], [0], color=color, lw=lw, label=label))
        plt.xlabel(xlabel[key])
        plt.legend(
            handles=legend_lines,
            bbox_to_anchor=(1.04,1), borderaxespad=0, frameon=False)
        plt.tight_layout()
        plt.grid(False)
#         plt.xlim(-0.05, bins[key].max() - bins[key].max()*0.02)
        sns.despine()
        figname = f'histogram-{key}-{cell_type}'.replace(' ', '-')
        fig.savefig(
            output_path / 'figures' / f'{figname}.png', 
            bbox_inches='tight', transparent=True)
        fig.savefig(
            output_path / 'figures' / f'{figname}.svg', 
            bbox_inches='tight', transparent=True)
In [30]:
from septum_mec.analysis.plotting import plot_bootstrap_timeseries
In [26]:
psth = pd.read_feather(output_path / 'data' / 'psth.feather')
times = pd.read_feather(output_path / 'data' / 'times.feather')
In [28]:
times = times.T.iloc[0].values
In [62]:
cs = ['#d95f02', '#e7298a', '#993404', '#980043']
lb = ['GC 11 Hz', 'GC 30 Hz', 'NS 11 Hz', 'NS 30 Hz']
In [64]:
fig, axs = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(5,2))
ii = 0
for cell_type, ls in zip(['gridcell', 'not bs'], ['-', '--']):
    for i, (ax, query) in enumerate(zip(axs.ravel(), queries)):
        selection = [
            f'{r.action}_{r.channel_group}_{r.unit_name}' 
            for i, r in data.query(query + ' and ' + cell_type).iterrows()]
        values = psth.loc[:, selection].dropna(axis=1).to_numpy()

        plot_bootstrap_timeseries(times, values, ax=ax, lw=2, label=lb[ii], color=cs[ii], ls=ls)
    #     ax.set_title(titles[i])
        ax.set_xlabel('Time (s)')
        ax.legend(frameon=False)
        ii += 1
    axs[0].set_ylabel('Probability density')
    sns.despine()
    plt.xlim(0, 0.029)
    
figname = f'response-probability'
fig.savefig(
    output_path / 'figures' / f'{figname}.png', 
    bbox_inches='tight', transparent=True)
fig.savefig(
    output_path / 'figures' / f'{figname}.svg', 
    bbox_inches='tight', transparent=True)

Store results in Expipe action

In [144]:
action = project.require_action("stimulus-response")