gp_neurobio/code/spikes_chirp.py
2018-11-30 17:19:28 +01:00

113 lines
4.0 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
from read_chirp_data import *
from utility import *
from IPython import embed
# define sampling rate and data path
sampling_rate = 40 #kHz
data_dir = "../data"
dataset = "2018-11-13-al-invivo-1"
# parameters for binning, smoothing and plotting
cut_window = 20
chirp_duration = 14 #ms
neuronal_delay = 5 #ms
chirp_start = int((-chirp_duration / 2 + neuronal_delay + cut_window * 2) * sampling_rate) #index
chirp_end = int((chirp_duration / 2 + neuronal_delay + cut_window * 2) * sampling_rate) #index
number_bins = 12
window = 1 #ms
time_axis = np.arange(-cut_window*2, cut_window*2, 1/sampling_rate) #steps
spike_bins = np.arange(-cut_window*2, cut_window*2) #ms
colors = ['k', 'k', 'k',
'k', 'k', 'k',
'k', 'k', 'k',
'k', 'k', 'firebrick']
sizes = [12, 12, 12,
12, 12, 12,
12, 12, 12,
12, 12, 18]
# differentiate between phases
phase_vec = np.arange(0, 1 + 1 / number_bins, 1 / number_bins)
cut_range = np.arange(-cut_window*2*sampling_rate, cut_window*2*sampling_rate, 1)
df_phase_binary = {}
spikes = read_chirp_spikes(os.path.join(data_dir, dataset))
df_map = map_keys(spikes)
for deltaf in df_map.keys():
df_phase_binary[deltaf] = {}
for rep in df_map[deltaf]:
chirp_size = int(rep[-1].strip('Hz'))
if chirp_size == 150:
continue
for phase in spikes[rep]:
for idx in np.arange(number_bins):
# check the phase
if phase[1] > phase_vec[idx] and phase[1] < phase_vec[idx+1]:
# get spikes between 40 ms before and after the chirp
spikes_to_cut = np.asarray(spikes[rep][phase])
spikes_cut = spikes_to_cut[(spikes_to_cut > -cut_window*2) & (spikes_to_cut < cut_window*2)]
spikes_idx = np.round(spikes_cut*sampling_rate)
# save as binary, 0 no spike, 1 spike
binary_spikes = np.isin(cut_range, spikes_idx)*1
# add the spikes to the dictionary with the correct df and phase
if idx in df_phase_binary[deltaf].keys():
df_phase_binary[deltaf][idx] = np.vstack((df_phase_binary[deltaf][idx], binary_spikes))
else:
df_phase_binary[deltaf][idx] = binary_spikes
csi_rates = {}
for df in df_phase_binary.keys():
csi_rates[df] = {}
beat_duration = int(abs(1/df*1000)*sampling_rate) #steps
beat_window = 0
# beat window is at most 20 ms long, multiples of beat_duration
while beat_window+beat_duration <= cut_window*sampling_rate:
beat_window = beat_window+beat_duration
for phase in df_phase_binary[df].keys():
# csi calculation
trials_binary = df_phase_binary[df][phase]
train_chirp = []
train_beat = []
for i, trial in enumerate(trials_binary):
smoothed_trial = smooth(trial, window, 1/sampling_rate)
train_chirp.append(smoothed_trial[chirp_start:chirp_end])
train_beat.append(smoothed_trial[chirp_start-beat_window:chirp_start])
std_chirp = np.std(np.mean(train_chirp, axis=0))
std_beat = np.std(np.mean(train_beat, axis=0))
csi_spikerate = (std_chirp - std_beat) / (std_chirp + std_beat)
csi_rates[df][phase] = np.mean(csi_spikerate)
upper_limit = np.max(sorted(csi_rates.keys()))+30
lower_limit = np.min(sorted(csi_rates.keys()))-30
inch_factor = 2.54
fig, ax = plt.subplots(figsize=(20/inch_factor, 10/inch_factor))
ax.plot([lower_limit, upper_limit], np.zeros(2), 'silver', linewidth=2, linestyle='--')
for i, df in enumerate(sorted(csi_rates.keys())):
for j, phase in enumerate(sorted(csi_rates[df].keys())):
ax.plot(df, csi_rates[df][phase], 'o', color=colors[j], ms=sizes[j])
plt.xlabel("$\Delta$f", fontsize = 22)
plt.xticks(fontsize = 18)
plt.ylabel("CSI", fontsize = 22)
plt.yticks(fontsize = 18)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
fig.tight_layout()
#plt.show()
plt.savefig('CSI.png')