%load_ext autoreload
%autoreload 2
import os
import pathlib
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import re
import shutil
import pandas as pd
import scipy.stats
import exdir
import expipe
from distutils.dir_util import copy_tree
import septum_mec
import septum_mec.analysis.data_processing as dp
import septum_mec.analysis.registration
from septum_mec.analysis.plotting import despine, plot_bootstrap_timeseries, violinplot, savefig
from phase_precession import cl_corr
from spike_statistics.core import permutation_resampling_test
import matplotlib.mlab as mlab
import scipy.signal as ss
from scipy.interpolate import interp1d
from skimage import measure
from tqdm.notebook import tqdm_notebook as tqdm
tqdm.pandas()
import scipy.signal as ss
from tqdm.notebook import tqdm_notebook as tqdm
tqdm.pandas()
import pycwt
plt.rcParams['figure.dpi'] = 150
figsize_violin = (1.7, 3)
figsize_speed = (4, 3)
plt.rc('axes', titlesize=10)
project_path = dp.project_path()
project = expipe.get_project(project_path)
actions = project.actions
output_path = pathlib.Path("output") / "lfp_speed"
(output_path / "statistics").mkdir(exist_ok=True, parents=True)
(output_path / "figures").mkdir(exist_ok=True, parents=True)
data_action = actions['lfp_speed']
output = exdir.File(
data_action.data_path('results'),
plugins = [exdir.plugins.git_lfs, exdir.plugins.quantities])
ignore = ['wavelet_power', 'wavelet_freqs', 'signal']
results = []
for group in output.values():
d = group.attrs.to_dict()
d.update({k: np.array(v.data) for k, v in group.items() if k not in ignore})
results.append(d)
results = pd.DataFrame(results)
results.head()
identify_neurons = actions['identify-neurons']
sessions = pd.read_csv(identify_neurons.data_path('sessions'))
results = results.merge(sessions, on='action')
results.head()
results = results.query('stim_location!="mecl" and stim_location!="mecr"')
def action_group(row):
a = int(row.channel_group in [0,1,2,3])
return f'{row.action}-{a}'
results['action_side_a'] = results.apply(action_group, axis=1)
lfp_results_hemisphere = results.sort_values(
by=['action_side_a', 'channel_group'], ascending=[True, False]
).drop_duplicates(subset='action_side_a', keep='first')
lfp_results_hemisphere.loc[:,['action_side_a','channel_group', 'min_speed']].head()
colors = ['#1b9e77','#d95f02','#7570b3','#e7298a']
labels = ['Baseline I', '11 Hz', 'Baseline II', '30 Hz']
# Hz11 means that the baseline session was indeed before an 11 Hz session
queries = ['baseline and i and Hz11', 'frequency==11', 'baseline and ii and Hz30', 'frequency==30']
def make_entity_date_side(row):
s = row.action_side_a.split('-')
del s[2]
return '-'.join(s)
lfp_results_hemisphere['entity_date_side'] = lfp_results_hemisphere.apply(make_entity_date_side, axis=1)
from functools import reduce
keys = [
'freq_score',
'power_score',
'speed_bins',
'mean_freq',
'mean_power'
]
results = {}
for key in keys:
results[key] = list()
for query, label in zip(queries, labels):
values = lfp_results_hemisphere.query(query).loc[:,['entity_date_side', key]]
results[key].append(values.rename({key: label}, axis=1))
for key, val in results.items():
df = reduce(lambda left,right: pd.merge(left, right, on='entity_date_side', how='outer'), val)
results[key] = df.drop('entity_date_side', axis=1)
vss = [
['Baseline I', '11 Hz'],
['Baseline I', 'Baseline II'],
['Baseline II', '30 Hz'],
['11 Hz', '30 Hz'],
]
ylabel = {
'freq_score': 'Frequency score',
'power_score': 'Power score'
}
for stuff in ['freq_score', 'power_score']:
for vs in vss:
base, stim = results[stuff][vs].dropna().values.T
plt.figure(figsize=figsize_violin)
plt.ylabel(ylabel[stuff])
violinplot(base, stim, colors=[colors[labels.index(l)] for l in vs], xticks=vs)
plt.ylim(-0.35, 0.5)
plt.yticks([-0.25, 0, 0.25, 0.5])
despine()
fig_path = output_path / "figures" / f"{stuff}_{' '.join(vs)}".replace(' ', '_')
savefig(fig_path)
def plot_speed(results, stuff, colors, labels, filename=None, show_raw=False, ylim=None):
base, stim = results[stuff][labels].dropna().values.T
base_bins, stim_bins = results['speed_bins'][labels].dropna().values.T
base = np.array([s for s in base])
stim = np.array([s for s in stim])
if show_raw:
fig, axs = plt.subplots(1, 2, sharey=True, figsize=figsize_speed)
for b, h in zip(base_bins, base):
axs[1].plot(b, h)
axs[1].set_xlim(0.1,1)
axs[1].set_title(labels[0])
for b, h in zip(stim_bins, stim):
axs[0].plot(b, h)
axs[0].set_xlim(0.1,1)
axs[0].set_title(labels[1])
fig, ax = plt.subplots(1, 1, figsize=figsize_speed)
plot_bootstrap_timeseries(base_bins[0], base.T, ax=ax, label=labels[0], color=colors[0])
plot_bootstrap_timeseries(stim_bins[0], stim.T, ax=ax, label=labels[1], color=colors[1])
plt.xlim(0, 0.9)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.legend(frameon=False)
if ylim is not None:
plt.ylim(ylim)
despine()
if filename is not None:
savefig(output_path / "figures" / f"{filename}")
plot_speed(results, 'mean_freq',
colors[:2], labels[:2], filename='lfp_speed_freq_11', ylim=(7.3, 8.3))
plot_speed(results, 'mean_freq',
colors[2:], labels[2:], filename='lfp_speed_freq_30', ylim=(7.3, 8.3))
plot_speed(
results, 'mean_freq',
colors=[colors[0], colors[2]], labels=[labels[0], labels[2]], filename='lfp_speed_freq_baseline', ylim=(7.3, 8.3))
plot_speed(
results, 'mean_freq',
colors=[colors[1], colors[3]], labels=[labels[1], labels[3]], filename='lfp_speed_freq_stim', ylim=(7.3, 8.3))
plot_speed(
results, 'mean_power',
colors[:2], labels[:2], filename='lfp_speed_power_11', ylim=(5, 35))
plot_speed(
results, 'mean_power',
colors[2:], labels[2:], filename='lfp_speed_power_30', ylim=(5, 35))
plot_speed(
results, 'mean_power',
colors=[colors[0], colors[2]], labels=[labels[0], labels[2]], filename='lfp_speed_power_baseline', ylim=(5, 35))
plot_speed(
results, 'mean_power',
colors=[colors[1], colors[3]], labels=[labels[1], labels[3]], filename='lfp_speed_power_stim', ylim=(5, 35))
from septum_mec.analysis.statistics import make_statistics_table
stat, _ = make_statistics_table(
{k:v for k, v in results.items() if k in ['power_score', 'freq_score']},
labels, lmm_test=False, wilcoxon_test=True, use_weighted_stats=False, normality_test=True)
stat
stat.to_latex(output_path / "statistics" / f"statistics.tex")
stat.to_csv(output_path / "statistics" / f"statistics.csv")
for key, result in results.items():
result.to_latex(output_path / "statistics" / f"values_{key}.tex")
result.to_csv(output_path / "statistics" / f"values_{key}.csv")
action = project.actions["lfp_speed"]
outdata = {
"figures": "figures",
"statistics": "statistics"
}
for key, value in outdata.items():
action.data[key] = value
data_path = action.data_path(key)
data_path.parent.mkdir(exist_ok=True, parents=True)
source = output_path / value
if source.is_file():
shutil.copy(source, data_path)
else:
copy_tree(str(source), str(data_path))
septum_mec.analysis.registration.store_notebook(action, "20_lfp_speed.ipynb")