%load_ext autoreload
%autoreload 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 functools import reduce
from tqdm.notebook import tqdm_notebook as tqdm
tqdm.pandas()
from spikewaveform.core import calculate_waveform_features_from_template, cluster_waveform_features
from septum_mec.analysis.plotting import violinplot, despine
from septum_mec.analysis.statistics import load_data_frames, make_paired_tables, make_statistics_table
#################################################
# lfp_location = ''
lfp_location = '-other-tetrode'
# lfp_location = '-other-drive'
##################################################
%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-spike-lfp-response" + lfp_location)
(output_path / "statistics").mkdir(exist_ok=True, parents=True)
(output_path / "figures").mkdir(exist_ok=True, parents=True)
output_path.mkdir(exist_ok=True)
project_path = dp.project_path()
project = expipe.get_project(project_path)
actions = project.actions
data, labels, colors, queries = load_data_frames()
lfp_action = actions['stimulus-spike-lfp-response' + lfp_location]
lfp_results = pd.read_csv(lfp_action.data_path('results'))
# lfp_results has old unit id's but correct on (action, unit_name, channel_group)
lfp_results = lfp_results.drop('unit_id', axis=1)
data = data.merge(lfp_results, how='left')
data['stim_strength'] = data.stim_p_max / data.theta_peak
keys = [
'theta_energy',
'theta_peak',
'theta_freq',
'theta_half_width',
'theta_vec_len',
'theta_ang',
'stim_energy',
'stim_half_width',
'stim_p_max',
'stim_strength',
'stim_vec_len',
'stim_ang'
]
results, labels = make_paired_tables(data, keys)
results['gridcell']['theta_peak']
xlabel = {
'theta_energy': 'Theta coherence energy',
'theta_peak': 'Theta peak coherence',
'theta_freq': '(Hz)',
'theta_half_width': '(Hz)',
'theta_vec_len': 'a.u.',
'theta_ang': 'rad'
}
for cell_type in ['gridcell', 'ns_inhibited', 'ns_not_inhibited']:
for key in xlabel:
fig = plt.figure(figsize=(3.7,2.2))
plt.suptitle(key + ' ' + cell_type)
legend_lines = []
for color, label in zip(colors, labels):
legend_lines.append(matplotlib.lines.Line2D([0], [0], color=color, label=label))
sns.kdeplot(data=results[cell_type][key].loc[:,labels], cumulative=True, legend=False, palette=colors, common_norm=False)
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)
despine()
figname = f'spike-lfp-coherence-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)
xlabel = {
'stim_energy': 'Coherence energy',
'stim_half_width': '(Hz)',
'stim_p_max': 'Peak coherence',
'stim_strength': 'Ratio',
'stim_vec_len': 'a.u.',
'stim_ang': 'rad'
}
# key = 'theta_energy'
# key = 'theta_peak'
for cell_type in ['gridcell', 'ns_inhibited', 'ns_not_inhibited']:
for key in xlabel:
fig = plt.figure(figsize=(3.3,2.2))
plt.suptitle(key + ' ' + cell_type)
legend_lines = []
for color, label in zip(colors[1::2], labels[1::2]):
legend_lines.append(matplotlib.lines.Line2D([0], [0], color=color, label=label))
sns.kdeplot(data=results[cell_type][key].loc[:,labels[1::2]], cumulative=True, legend=False, palette=colors[1::2], common_norm=False)
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)
despine()
figname = f'spike-lfp-coherence-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)
from septum_mec.analysis.statistics import VonMisesKDE
for paradigm in ['stim', 'theta']:
key = paradigm + '_vec_len'
for cell_type in ['gridcell', 'ns_inhibited', 'ns_not_inhibited']:
fig = plt.figure(figsize=(3.2,2.2))
plt.suptitle(key + ' ' + cell_type)
legend_lines = []
for color, query, label in zip(colors, queries, labels):
data_query = data.query(query + ' and ' + cell_type)
values = data_query[key].values
angles = data_query[paradigm + '_ang'].values
kde = VonMisesKDE(angles, weights=values, kappa=5)
bins = np.linspace(-np.pi, np.pi, 100)
plt.polar(bins, kde.evaluate(bins), color=color, lw=2)
plt.polar(angles, values, color=color, lw=1, ls='none', marker='.', markersize=2)
# values.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=2, label=label))
plt.legend(
handles=legend_lines,
bbox_to_anchor=(1.04,1), borderaxespad=0, frameon=False)
plt.tight_layout()
# plt.grid(False)
figname = f'spike-lfp-polar-plot-{paradigm}-{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)
stats = {}
for cell_type, result in results.items():
stats[cell_type], _ = make_statistics_table(result, labels)
stats['gridcell']
for cell_type, stat in stats.items():
stat.to_latex(output_path / "statistics" / f"statistics_{cell_type}.tex")
stat.to_csv(output_path / "statistics" / f"statistics_{cell_type}.csv")
for cell_type, cell_results in results.items():
for key, result in cell_results.items():
result.to_latex(output_path / "statistics" / f"values_{cell_type}_{key}.tex")
result.to_csv(output_path / "statistics" / f"values_{cell_type}_{key}.csv")
from septum_mec.analysis.plotting import plot_bootstrap_timeseries
coher = pd.read_feather(output_path / 'data' / 'coherence.feather')
freqs = pd.read_feather(output_path / 'data' / 'freqs.feather')
freq = freqs.T.iloc[0].values
mask = (freq < 100)
for cell_type in ['gridcell', 'ns_inhibited', 'ns_not_inhibited']:
fig, axs = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(5,2))
axs = axs.repeat(2)
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 = coher.loc[mask, selection].dropna(axis=1).to_numpy()
values = 10 * np.log10(values)
plot_bootstrap_timeseries(freq[mask], values, ax=ax, lw=1, label=labels[i], color=colors[i])
# ax.set_title(titles[i])
ax.set_xlabel('Frequency Hz')
ax.legend(frameon=False)
ax.set_ylim(-30, 0)
axs[0].set_ylabel('Coherence')
despine()
figname = f'spike-lfp-coherence-{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)
nsi_vs_nsni = {}
for key in keys:
df = pd.DataFrame()
dfs = [results[k][key].loc[:, ['entity', 'unit_idnum', 'Baseline I']].rename({'Baseline I': k}, axis=1) for k in ['ns_inhibited', 'ns_not_inhibited']]
df = pd.merge(*dfs, on=['entity', 'unit_idnum'], how='outer')
nsi_vs_nsni[key] = df
nsi_vs_nsni.keys()
nsi_vs_nsni['theta_energy']
from septum_mec.analysis.statistics import LMM
LMM(nsi_vs_nsni['theta_energy'], 'ns_inhibited', 'ns_not_inhibited', 'theta_energy')
stat, stat_vals = make_statistics_table(nsi_vs_nsni, ['ns_inhibited', 'ns_not_inhibited'], wilcoxon_test=False)
stat
stat.to_latex(output_path / "statistics" / f"statistics_nsi_vs_nsni.tex")
stat.to_csv(output_path / "statistics" / f"statistics_nsi_vs_nsni.csv")
action = project.require_action("stimulus-spike-lfp-response" + lfp_location)
copy_tree(output_path, str(action.data_path()))
septum_mec.analysis.registration.store_notebook(action, "20_stimulus-spike-lfp-response.ipynb")