This is now my code folder

This commit is contained in:
saschuta 2020-12-01 12:00:50 +01:00
parent 6bf087c67c
commit 37cfb0ecc1
112 changed files with 10562 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
*.pdf
*.png
*.pdf
*.pkl
*.csv
*.npy

213
MPFmodulation.py Normal file
View File

@ -0,0 +1,213 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from myfunctions import remove_tick_ymarks
import string
def plot_amp(ax, mean1, dev,nrs = [3,4,5],nr_size = 12,name = 'amp',nr = 1, wide = 'gainsboro', middle = 'darkgrey', narrow = 'black'):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [original, m05, m2] #m005,
lim = [[]]*len(versions)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name+' std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name+' 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name+' 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
ax[nr, i].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[nr,i].transAxes,
size=nr_size, weight='bold')
ax[nr,i].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)
ax[nr,i].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color=middle)
if i != 0:
ax[nr, i] = remove_tick_ymarks(ax[nr,i])
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax[nr,i].plot(np.array(keys)[sim], data[sim], color=narrow)
lim[i] = np.nanmax(std95[sim])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
for i in range(len(versions)):
ax[nr, i].set_ylim(0,np.nanmax(lim))
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_mean_cells():
mean1 = pd.read_pickle('mean.pkl')
colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
inch_factor = 2.54
whole_page_width = 6.28
intermediate_length = 3.7
plt.rcParams['figure.figsize'] = (whole_page_width, intermediate_length)
plt.rcParams['font.size'] = 11
plt.rcParams['axes.titlesize'] = 12
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.loc'] = 'upper right'
plt.rcParams["legend.frameon"] = False
# load data for plot
# data1 = pd.read_csv('ma_allcells_unsmoothed.csv')
# data2 = pd.read_csv('ma_allcells_05.csv')
# data3 = pd.read_csv('ma_allcells_2.csv')
# data_tob = pd.read_csv('ma_toblerone.csv')
# smothed = df_beat[df_beat['dev'] == 'original']
# data1 = smothed[smothed['type'] == 'amp']
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == 'max mean']
#versions = [[]]*len(dev)
#for i in range(len(dev)):
original = all_means[all_means['dev'] == 'original']
m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
dev = ['original', '05', '2']#'005',
titels = ['binary','0.5 ms','2 ms']
fig, ax = plt.subplots(nrows=2, ncols=3, sharex=True)
versions = [original, m05, m2]#m005,
nrs = [0,1,2]
nr_size = 12
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
#embed()
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == 'max std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[0,i].set_title(dev[i] +' ms')
ax[0, i].set_title(titels[i])
ax[ 0,0].set_ylabel('MPF [EODf]')
all_means = mean1[mean1['type'] == 'max 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == 'max 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#std[np.where(list(data[sim] + std[sim])>std95[sim])] = std95[sim][np.where(list(data[sim] + std[sim])>std95[sim])]
#embed()
middle = 'pink'#'LightSalmon'
wide='lightpink'#mistyrose'
narrow = 'crimson'
ax[0,i].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide, alpha = 0.45)#
#embed()
ax[0,i].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]), color=middle)
ax[0,i].plot(x / 500, corr / 500 + 1, color='grey', linestyle='--', label='AMf')
ax[0,i].plot(np.array(keys)[sim], data[sim], color=narrow)
# plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1]))
ax[0, i].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[0,i].transAxes,
size=nr_size, weight='bold')
if i != 0:
ax[0, i] = remove_tick_ymarks(ax[0, i])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
ax[0, 2].legend(bbox_to_anchor=(0.7, 1, 0.7, .1), loc='lower left',
ncol=3, mode="expand", borderaxespad=0.)
#ax = plot_amp(ax, mean1, dev,name = 'amp',nr = 2)
ax[ 1,0].legend(bbox_to_anchor=(0.3, 1, 0.7, .1), loc='lower left',
ncol=3, mode="expand", borderaxespad=0.)
ax = plot_amp(ax,mean1, dev,nrs = [3,4,5],nr_size = nr_size,name='amp max', nr=1,wide = 'gainsboro', middle = 'lightblue', narrow = 'steelblue')
# ax[1].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[1].plot(data2.x, transfer_array, color=colors[0])
# ax[1].plot(data2.x, data2.freq20, color=colors[1])
# ax[2].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[2].plot(data3.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
# ax[2].plot(data_tob.ff, transfer_array, color='grey', linestyle='--')
# ax[2].plot(data4.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
ax[ 0,0].set_ylabel('MPF [EODf]')
#ax[1, 0].set_ylabel('Modulation depth')
ax[1, 0].set_ylabel('Modulation Peak')
#ax[2, 0].set_ylabel('Whole modulation ')
plt.subplots_adjust(hspace = 0.35)
for i in range(2):
ax[i,0].spines['right'].set_visible(False)
ax[i,0].spines['top'].set_visible(False)
ax[i,1].spines['right'].set_visible(False)
ax[i,1].spines['top'].set_visible(False)
ax[i,2].spines['right'].set_visible(False)
ax[i,2].spines['top'].set_visible(False)
#ax[i,3].spines['right'].set_visible(False)
#ax[i,3].spines['top'].set_visible(False)
#for i in range(3):
ax[1, 1].set_xlabel('stimulus frequency [EODf]')
#ax[2, i].set_ylim([0, 370])
#ax[2,i].set_ylim([0, 4700])
#ax[2, 1].set_xlabel('stimulus frequency [EODf]')
plt.subplots_adjust(bottom = 0.13)
#fig.tight_layout()f
# fig.label_axes()
return fig
if __name__ == "__main__":
fig = plot_mean_cells()
#fig.savefig()
plt.savefig('MPFmodulation.pdf')
plt.savefig('../highbeats_pdf/MPFmodulation.pdf')
plt.show()
# plt.close()

Binary file not shown.

199
MPFmodulation_tob.py Normal file
View File

@ -0,0 +1,199 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from myfunctions import remove_tick_ymarks
import string
from myfunctions import default_settings
def plot_amp(ax, mean1, dev,lw = 0.8,nrs = [3,4,5],nr_size = 12,name = 'amp',nr = 1, wide = 'gainsboro', middle = 'darkgrey', narrow = 'black'):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [original, m05, m2] #m005,
lim = [[]]*len(versions)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name+' std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name+' 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name+' 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
ax[nr, i].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[nr,i].transAxes,
size=nr_size, weight='bold')
ax[nr,i].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)
ax[nr,i].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color=middle)
if i != 0:
ax[nr, i] = remove_tick_ymarks(ax[nr,i])
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax[nr,i].plot(np.array(keys)[sim], data[sim],linewidth = lw, color=narrow)
lim[i] = np.nanmax(std95[sim])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
for i in range(len(versions)):
ax[nr, i].set_ylim(0,np.nanmax(lim))
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_mean_cells(type = '',tob_lw = 0.8):
mean1 = pd.read_pickle('mean'+type+'.pkl')
whole_page_width = 6.28
intermediate_length = 4.7
default_settings([0],intermediate_width = 6.29,intermediate_length = 3.7, ts = 10, ls = 9, fs = 9)
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == 'max mean']
#versions = [[]]*len(dev)
#for i in range(len(dev)):
original = all_means[all_means['dev'] == 'original']
m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
dev = ['original', '05', '2']#'005',
titels = ['binary','0.5 ms','2 ms']
fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True)
plt.suptitle(type)
#embed()
versions = [original, m05, m2]#m005,
nrs = [0,1,2]
nr_size = 12
lw = 1.2
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
#embed()
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == 'max std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[0,i].set_title(dev[i] +' ms')
ax[0, i].set_title(titels[i])
ax[ 0,0].set_ylabel('MPF [EODf]')
all_means = mean1[mean1['type'] == 'max 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == 'max 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#std[np.where(list(data[sim] + std[sim])>std95[sim])] = std95[sim][np.where(list(data[sim] + std[sim])>std95[sim])]
#embed()
middle = 'pink'#'LightSalmon'
wide='lightpink'#mistyrose'
wide = 'gainsboro'
narrow = 'crimson'
tob_color = 'black'
ax[0,i].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)# alpha = 0.45
#embed()
ax[0,i].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]), color=middle)
ax[0,i].plot(x / 500, corr / 500 + 1, color=tob_color, linestyle = '--', linewidth = tob_lw, label='AMf')
ax[0,i].plot(np.array(keys)[sim], data[sim], color=narrow, linewidth = lw)
# plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1]))
ax[0, i].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[0,i].transAxes,
size=nr_size, weight='bold')
#embed()
if i != 0:
ax[0, i] = remove_tick_ymarks(ax[0, i])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
#ax[0, 2].legend(bbox_to_anchor=(0.7, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
#ax = plot_amp(ax, mean1, dev,name = 'amp',nr = 2)
#ax[ 1,0].legend(bbox_to_anchor=(0.3, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
ax = plot_amp(ax,mean1, dev,lw = lw,nrs = [3,4,5],nr_size = nr_size,name='amp max', nr=1,wide = 'gainsboro', middle = 'lightblue', narrow = 'steelblue')
ax = plot_amp(ax,mean1, dev,lw = lw, nrs = [6,7,8],nr_size = nr_size,name='tob max', nr=2,wide = 'gainsboro', middle = 'grey', narrow = 'black')
ax[ 0,0].set_ylabel('EOD multiples')
#ax[1, 0].set_ylabel('Modulation depth')
ax[1, 0].set_ylabel('Modulation')
#ax[2, 0].set_ylabel('Whole modulation ')
plt.subplots_adjust(hspace = 0.35)
for i in range(3):
ax[i,0].spines['right'].set_visible(False)
ax[i,0].spines['top'].set_visible(False)
ax[i,1].spines['right'].set_visible(False)
ax[i,1].spines['top'].set_visible(False)
ax[i,2].spines['right'].set_visible(False)
ax[i,2].spines['top'].set_visible(False)
#ax[i,3].spines['right'].set_visible(False)
#ax[i,3].spines['top'].set_visible(False)
#for i in range(3):
ax[2, 1].set_xlabel('EOD multiples')
#ax[2, i].set_ylim([0, 370])
#ax[2,i].set_ylim([0, 4700])
#ax[2, 1].set_xlabel('stimulus frequency [EODf]')
plt.subplots_adjust(bottom = 0.13, right = 0.96)
#fig.tight_layout()f
# fig.label_axes()
return fig
if __name__ == "__main__":
#type = ''
type = ''
type = 'simulation' #'simulation',
nrs = ['simulation','sinz','sinz3','sinz5','sinz7','sinz9','' ,]# 'sinz11'
for n in range(len(nrs)):
type = str(nrs[n])
print(type)
fig = plot_mean_cells(type = type)
#fig.savefig()
plt.savefig('MPFmodulation_tob'+type+'.pdf')
plt.savefig('../highbeats_pdf/MPFmodulation_tob'+type+'.pdf')
plt.show()
# plt.close()

Binary file not shown.

Binary file not shown.

221
MPFmodulation_trans.py Normal file
View File

@ -0,0 +1,221 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from myfunctions import remove_tick_ymarks
from myfunctions import remove_tick_marks
import string
from matplotlib import gridspec
def plot_amp(grid, nrow, ncol,ax, mean1, dev,nrs = [3,4,5],nr_size = 12,name = 'amp',nr = 1, wide = 'gainsboro', middle = 'darkgrey', narrow = 'black'):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [original, m05, m2] #m005,
lim = [[]]*len(versions)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name+' std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name+' 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name+' 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#ax['mod'+str(i)] = plt.subplot(nrow, ncol, i * 2 + 2)
ax['mod' + str(i)] = plt.subplot(grid[i * 2 + 1])
if nrs:
ax['mod'+str(i)].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax['mod'+str(i)].transAxes,
size=nr_size, weight='bold')
ax['mod'+str(i)].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)
ax['mod'+str(i)].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color=middle)
#if i != 0:
# ax['mod'+str(i)] = remove_tick_ymarks(ax['mod'+str(i)])
if i != 2:
ax['mod' + str(i)] = remove_tick_marks(ax['mod' + str(i)])
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax['mod'+str(i)].plot(np.array(keys)[sim], data[sim], color=narrow)
lim[i] = np.nanmax(std95[sim])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
for i in range(len(versions)):
ax['mod'+str(i)].set_ylim(0,np.nanmax(lim))
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_mean_cells():
mean1 = pd.read_pickle('mean.pkl')
colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
inch_factor = 2.54
whole_page_width = 4
intermediate_length = 7.5
plt.rcParams['figure.figsize'] = (whole_page_width, intermediate_length)
plt.rcParams['font.size'] = 11
plt.rcParams['axes.titlesize'] = 12
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.loc'] = 'upper right'
plt.rcParams["legend.frameon"] = False
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == 'max mean']
#versions = [[]]*len(dev)
#for i in range(len(dev)):
original = all_means[all_means['dev'] == 'original']
m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
dev = ['original', '05', '2']#'005',
titels = ['binary','0.5 ms','2 ms']
#fig, ax = plt.subplots(nrows=3, ncols=2, sharex=True)
fig = plt.figure()
versions = [original, m05, m2]#m005,
nrs = [0,1,2]
nrs = [0,2,4]
nrs = []
nr_size = 12
ax = {}
nrow = 6
ncol = 1
grid = gridspec.GridSpec(6,1,hspace = 0.4,left = 0.2)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
#embed()
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == 'max std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[0,i].set_title(dev[i] +' ms')
ax[i+1] = plt.subplot(grid[i*2])
ax[i+1].set_title(titels[i])
#ax[i+1].set_ylabel('MPF [EODf]')
all_means = mean1[mean1['type'] == 'max 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == 'max 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#std[np.where(list(data[sim] + std[sim])>std95[sim])] = std95[sim][np.where(list(data[sim] + std[sim])>std95[sim])]
#embed()
middle = 'pink'#'LightSalmon'
wide='lightpink'#mistyrose'
narrow = 'crimson'
ax[i+1].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide, alpha = 0.45)#
#embed()
ax[i+1].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]), color=middle)
ax[i+1].plot(x / 500, corr / 500 + 1, color='grey', linestyle='--', label='AMf')
ax[i+1].plot(np.array(keys)[sim], data[sim], color=narrow)
# plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1]))
if nrs:
ax[i+1].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[i+1].transAxes,
size=nr_size, weight='bold')
ax[i+1] = remove_tick_marks(ax[i+1])
#if i != 0:
# ax[i+1] = remove_tick_ymarks(ax[i+1])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
#ax[1].legend(bbox_to_anchor=(0.7, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
#ax = plot_amp(ax, mean1, dev,name = 'amp',nr = 2)
#ax[1].legend(bbox_to_anchor=(0.3, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
nrs = [1,3,5]
nrs = []
ax = plot_amp(grid, nrow, ncol, ax,mean1, dev,nrs = nrs,nr_size = nr_size,name='amp max', nr=1,wide = 'gainsboro', middle = 'lightblue', narrow = 'steelblue')
# ax[1].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[1].plot(data2.x, transfer_array, color=colors[0])
# ax[1].plot(data2.x, data2.freq20, color=colors[1])
# ax[2].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[2].plot(data3.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
# ax[2].plot(data_tob.ff, transfer_array, color='grey', linestyle='--')
# ax[2].plot(data4.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
ax[2+1].set_ylabel('MPF [EODf]')
#ax[1, 0].set_ylabel('Modulation depth')
ax['mod'+str(2)].set_ylabel('Modulation')
#ax[2, 0].set_ylabel('Whole modulation ')
plt.subplots_adjust(hspace = 0.35)
for i in range(3):
ax['mod'+str(i)].spines['right'].set_visible(False)
ax['mod'+str(i)].spines['top'].set_visible(False)
ax[i+1].spines['right'].set_visible(False)
ax[i+1].spines['top'].set_visible(False)
#ax[i].spines['right'].set_visible(False)
#ax[i].spines['top'].set_visible(False)
#ax[i,3].spines['right'].set_visible(False)
#ax[i,3].spines['top'].set_visible(False)
#for i in range(3):
ax['mod'+str(2)].set_xlabel('stimulus frequency [EODf]')
#ax[2, i].set_ylim([0, 370])
#ax[2,i].set_ylim([0, 4700])
#ax[2, 1].set_xlabel('stimulus frequency [EODf]')
plt.subplots_adjust(bottom = 0.13)
#fig.tight_layout()f
# fig.label_axes()
return fig
if __name__ == "__main__":
fig = plot_mean_cells()
#fig.savefig()
plt.savefig('MPFmodulation_trans.pdf')
plt.savefig('../highbeats_pdf/MPFmodulation_trans.pdf')
plt.show()
# plt.close()

View File

@ -0,0 +1,221 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from myfunctions import remove_tick_ymarks
from myfunctions import remove_tick_marks
import string
from matplotlib import gridspec
def plot_amp(grid, nrow, ncol,ax, mean1, dev,nrs = [3,4,5],nr_size = 12,name = 'amp',nr = 1, wide = 'gainsboro', middle = 'darkgrey', narrow = 'black'):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [ m05] #m005,
lim = [[]]*len(versions)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name+' std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name+' 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name+' 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#ax['mod'+str(i)] = plt.subplot(nrow, ncol, i * 2 + 2)
ax['mod' + str(i)] = plt.subplot(grid[i * 2 + 1])
if nrs:
ax['mod'+str(i)].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax['mod'+str(i)].transAxes,
size=nr_size, weight='bold')
ax['mod'+str(i)].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)
ax['mod'+str(i)].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color=middle)
#if i != 0:
# ax['mod'+str(i)] = remove_tick_ymarks(ax['mod'+str(i)])
#if i != :
# ax['mod' + str(i)] = remove_tick_marks(ax['mod' + str(i)])
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax['mod'+str(i)].plot(np.array(keys)[sim], data[sim], color=narrow)
lim[i] = np.nanmax(std95[sim])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
for i in range(len(versions)):
ax['mod'+str(i)].set_ylim(0,np.nanmax(lim))
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_mean_cells(whole_page_width = 4,
intermediate_length = 7.5):
mean1 = pd.read_pickle('mean.pkl')
colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
inch_factor = 2.54
plt.rcParams['figure.figsize'] = (whole_page_width, intermediate_length)
plt.rcParams['font.size'] = 11
plt.rcParams['axes.titlesize'] = 12
plt.rcParams['axes.labelsize'] = 11
plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.loc'] = 'upper right'
plt.rcParams["legend.frameon"] = False
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == 'max mean']
#versions = [[]]*len(dev)
#for i in range(len(dev)):
original = all_means[all_means['dev'] == 'original']
m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
dev = ['05']#'005',
titels = ['0.5 ms']
#fig, ax = plt.subplots(nrows=3, ncols=2, sharex=True)
fig = plt.figure()
versions = [ m05]#m005,
nrs = [0,1,2]
nrs = [0,2,4]
nrs = []
nr_size = 12
ax = {}
nrow = 6
ncol = 1
grid = gridspec.GridSpec(2,1,hspace = 0.4,left = 0.2)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
#embed()
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == 'max std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[0,i].set_title(dev[i] +' ms')
ax[i+1] = plt.subplot(grid[i*2])
ax[i+1].set_title(titels[i])
#ax[i+1].set_ylabel('MPF [EODf]')
all_means = mean1[mean1['type'] == 'max 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == 'max 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
#std[np.where(list(data[sim] + std[sim])>std95[sim])] = std95[sim][np.where(list(data[sim] + std[sim])>std95[sim])]
#embed()
middle = 'pink'#'LightSalmon'
wide='lightpink'#mistyrose'
narrow = 'crimson'
ax[i+1].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide, alpha = 0.45)#
#embed()
ax[i+1].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]), color=middle)
ax[i+1].plot(x / 500, corr / 500 + 1, color='grey', linestyle='--', label='AMf')
ax[i+1].plot(np.array(keys)[sim], data[sim], color=narrow)
# plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1]))
if nrs:
ax[i+1].text(-0.1, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[i+1].transAxes,
size=nr_size, weight='bold')
ax[i+1] = remove_tick_marks(ax[i+1])
#if i != 0:
# ax[i+1] = remove_tick_ymarks(ax[i+1])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
#ax[1].legend(bbox_to_anchor=(0.7, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
#ax = plot_amp(ax, mean1, dev,name = 'amp',nr = 2)
#ax[1].legend(bbox_to_anchor=(0.3, 1, 0.7, .1), loc='lower left',
# ncol=3, mode="expand", borderaxespad=0.)
nrs = [1,3,5]
nrs = []
ax = plot_amp(grid, nrow, ncol, ax,mean1, dev,nrs = nrs,nr_size = nr_size,name='amp max', nr=1,wide = 'gainsboro', middle = 'lightblue', narrow = 'steelblue')
# ax[1].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[1].plot(data2.x, transfer_array, color=colors[0])
# ax[1].plot(data2.x, data2.freq20, color=colors[1])
# ax[2].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--')
# ax[2].plot(data3.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
# ax[2].plot(data_tob.ff, transfer_array, color='grey', linestyle='--')
# ax[2].plot(data4.x, transfer_array, color=colors[0])
# ax[2].plot(data3.x, color=colors[1])
ax[0+1].set_ylabel('EOD mult')
#ax[1, 0].set_ylabel('Modulation depth')
ax['mod'+str(0)].set_ylabel('Modulation')
#ax[2, 0].set_ylabel('Whole modulation ')
plt.subplots_adjust(hspace = 0.35)
for i in range(1):
ax['mod'+str(i)].spines['right'].set_visible(False)
ax['mod'+str(i)].spines['top'].set_visible(False)
ax[i+1].spines['right'].set_visible(False)
ax[i+1].spines['top'].set_visible(False)
#ax[i].spines['right'].set_visible(False)
#ax[i].spines['top'].set_visible(False)
#ax[i,3].spines['right'].set_visible(False)
#ax[i,3].spines['top'].set_visible(False)
#for i in range(3):
ax['mod'+str(0)].set_xlabel('EOD multiples')
#ax[2, i].set_ylim([0, 370])
#ax[2,i].set_ylim([0, 4700])
#ax[2, 1].set_xlabel('stimulus frequency [EODf]')
plt.subplots_adjust(bottom = 0.20)
#fig.tight_layout()f
# fig.label_axes()
return fig
if __name__ == "__main__":
fig = plot_mean_cells(whole_page_width = 7.5,intermediate_length = 2.3)
#fig.savefig()
plt.savefig('MPFmodulation_trans_single.pdf')
plt.savefig('../highbeats_pdf/MPFmodulation_trans_single.pdf')
plt.show()
# plt.close()

74
beatcorrplot.py Normal file
View File

@ -0,0 +1,74 @@
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
def plot_beat_corr(eod_fr,eod_fe = np.arange(1, 1500, 5)):
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
gs0 = gridspec.GridSpec(3, 1, height_ratios=[4, 1, 1], hspace=0.7)
plt.figure(figsize=(4.5, 6))
style = 'dotted'
color_v = 'black'
color_b = 'silver'
# plt.subplot(3,1,1)
plt.subplot(gs0[0])
np.max(beats) / eod_fr
plt.plot(beats, beats, color=color_b)
# plt.axvline(x = -250, Linestyle = style,color = color_v)
# plt.axvline(x = 250, Linestyle = style,color = color_v)
# plt.axvline(x = 750, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style)
# plt.subplot(3,1,2)
plt.xlabel('Beats [Hz]')
plt.ylabel('Difference frequency [Hz]')
#plt.subplot(gs0[1])
plt.plot(beats, beat_corr, color=color_b)
plt.axvline(x=-250, Linestyle=style, color=color_v)
plt.axvline(x=250, Linestyle=style, color=color_v)
plt.axvline(x=750, Linestyle=style, color=color_v)
plt.xlabel('EOD adjusted beat [Hz]')
# plt.axvline(x = 1250, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style,color = color_v)
mult = np.array(beats) / eod_fr + 1
# plt.subplot(3,1,3)
plt.xlabel('Beats [Hz]')
plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
#plt.subplot(gs0[2])
#plt.plot(mult, beat_corr, color=color_b)
# plt.axvline(x = 0, Linestyle = style)
#plt.axvline(x=0.5, Linestyle=style, color=color_v)
# plt.axvline(x = 1, Linestyle = style)
#plt.axvline(x=1.5, Linestyle=style, color=color_v)
#plt.axvline(x=2.5, Linestyle=style, color=color_v)
#plt.xlabel('EOD multiples')
#plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
plt.tight_layout()
plt.subplots_adjust(left = 0.2,top = 0.97)
plt.savefig(fname='../pictures_highbeats/' + 'beat_corr_illustration')
plt.show()
embed()
eod_fr = 500
eod_fe = np.arange(1, eod_fr*3, 5)
plot_beat_corr(eod_fr,eod_fe = eod_fe)

404
compare.py Normal file
View File

@ -0,0 +1,404 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from myfunctions import remove_tick_ymarks
from myfunctions import remove_tick_marks
import string
from matplotlib import gridspec
from plot_eod_chirp import power_func, find_beats, find_dev
#from beatsnumerics import plot_beats
from thunderfish.tabledata import TableData
from plotstyle import plot_style, spines_params
from scipy.signal import hilbert, butter, filtfilt, lfilter
from myfunctions import default_settings
def plot_amp(name,grid,ax, mean1, nrs = [3,4,5],nr_size = 12,name_nr = 'amp',nr = 1, wide = 'gainsboro', rem = 'yes', middle = 'darkgrey', narrow = 'black'):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name_nr +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [m05] #m005,
dev = '05'
lim = [[]]*len(versions)
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name_nr+' std']
std = all_means[all_means['dev'] == dev]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name_nr+' 95']
std95 = all_means[all_means['dev'] == dev]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name_nr+' 05']
std05 = all_means[all_means['dev'] == dev]
std05 = np.array(std05[keys])[0]
#ax['mod'+str(i)] = plt.subplot(nrow, ncol, i * 2 + 2)
ax[name] = plt.subplot(grid[nr])
if nrs:
ax[name].text(left, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[name].transAxes,
size=nr_size, weight='bold')
ax[name].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color=wide)
ax[name].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color=middle)
ax[name].spines['right'].set_visible(False)
ax[name].spines['top'].set_visible(False)
ax[name].set_xlim([0, 5])
ax[name].set_xlabel('EOD multiples')
#if i != 0:
# ax['mod'+str(i)] = remove_tick_ymarks(ax['mod'+str(i)])
#if (i != 2) and (rem != 'no'):
# ax[name] = remove_tick_marks(ax[name])
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax[name].plot(np.array(keys)[sim], data[sim], color=narrow)
lim[i] = np.nanmax(std95[sim])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
for i in range(len(versions)):
ax[name].set_ylim(0,np.nanmax(lim))
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_beats(ax, f0, freqs, fexpected, ffirst,color, fmax, title, ylabel = 'yes'):
freqs /= f0
sel = np.abs(freqs - ((freqs+0.1)//0.5)*0.5) < 0.01
ax.set_title(title)
ax.plot(freqs, fexpected/f0, color = 'steelblue')
if ffirst is not None:
ffirst[sel] = np.nan
ax.plot(freqs, ffirst/f0, color = 'palegoldenrod')
if fmax is not None:
fmax[sel] = np.nan
ax.plot(freqs, fmax/f0, color = color)
ax.set_xlim(0, 3.5)
ax.set_ylim(0, 0.7)
ax.set_xticks_delta(1.0)
ax.set_yticks_delta(0.5)
#ax.set_xlabel('Frequency [f0]')
if ylabel == 'yes':
ax.set_ylabel('Frequency [f0]')
else:
ax = remove_tick_ymarks(ax)
ax = remove_tick_marks(ax)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
def plot_mean_cells():
mean1 = pd.read_pickle('mean.pkl')
default_settings([0],intermediate_width = 6.29,intermediate_length = 8, ts = 10, ls = 9, fs = 9)
nr_size = 12
lp = 50
colors_tob = ['orange','brown','firebrick','red']
colors_mod = ['blue','navy','steelblue','steelblue']
grid00 = gridspec.GridSpec(2, 1, height_ratios = [3,0.8], hspace=0.2, left=0.2)
grid0 = gridspec.GridSpecFromSubplotSpec(6, 2, height_ratios = [0.01,1,0.01,1,0.01,1],
subplot_spec=grid00[0],wspace=0.3, hspace=0.75)
grid0_sub = gridspec.GridSpecFromSubplotSpec(3, 1, height_ratios = [1,1,1],
subplot_spec=grid00[0],wspace=0.25, hspace=0.4)
eod_fr = 670
start = 5
end = eod_fr*5
step = 25
eod_fe, beat_corr, beats = find_beats(start, end, step, eod_fr)
sampling = 100000
deviation_ms, deviation_s, deviation_dp = find_dev([1.5], sampling)
left = -0.5
ax = {}
#axis_sub = gridspec.GridSpecFromSubplotSpec(1, 1,
# subplot_spec=grid0_sub[0],wspace=0, hspace=0.7)
axis_sub = gridspec.GridSpecFromSubplotSpec(1, 1,
subplot_spec=grid0[0,:],wspace=0, hspace=0.7)
ax['sub'] = plt.subplot(axis_sub[0])
ax['sub'].spines['top'].set_visible(False)
ax['sub'].spines['bottom'].set_visible(False)
ax['sub'].spines['left'].set_visible(False)
ax['sub'].spines['right'].set_visible(False)
ax['sub'].set_yticks([])
ax['sub'].set_xticks([])
plt.title('Analytic', pad = -lp,fontsize = 10, fontweight='bold')
#legend_elements = [Line2D([0], [0], color=color_tob[0], lw=1, label='Maximal Frequency'),
# Line2D([0], [0], color=color_mod[0], label='Modulation')]
#ax['sub'].legend(handles=legend_elements, loc='center')
#embed()
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[1,0],wspace=0, hspace=0.7)
data = TableData('highbeatspecs10.dat')
ax['analytic_threhold'] = plt.subplot(axis[0])
ax['analytic_threhold'].text(left, 1.1, string.ascii_uppercase[0], transform=ax['analytic_threhold'].transAxes,
size=nr_size, weight='bold')
f0 = data[data[:,'freq>norm'] == 1.0,'freq>freq']
f0 = f0[len(f0)//2]
plot_beats(ax['analytic_threhold'] , f0, data[:,'freq>freq'], data[:,'freq>expected'],
data[:,'threshold>first_f'], colors_tob[0], data[:,'threshold>max_f'], 'Thresholded')
ax['analytic_threhold'].set_xlim([0, 5])
ax['analytic_threhold_a'] = plt.subplot(axis[1])
ax['analytic_threhold_a'] .spines['right'].set_visible(False)
ax['analytic_threhold_a'] .spines['top'].set_visible(False)
plt.plot(data[:,'freq>freq']/f0,data[:, 'threshold>max_a'], color = colors_mod[0])
ax['analytic_threhold_a'] = remove_tick_marks(ax['analytic_threhold_a'])
plt.xlim([0, 5])
data = TableData('highbeatspecs10.dat')
#embed()
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[1,1],wspace=0, hspace=0.5)
ax['analytic_cube'] = plt.subplot(axis[0])
f0 = data[data[:,'freq>norm'] == 1.0,'freq>freq']
f0 = f0[len(f0)//2]
plot_beats(ax['analytic_cube'] , f0, data[:,'freq>freq'], data[:,'freq>expected'],
data[:,'threshold cubed>first_f'],colors_tob[0], data[:,'threshold cubed>max_f'], 'Threshold cubed', ylabel = 'no')
ax['analytic_cube'].set_xlim([0,5])
ax['analytic_cube_a'] = plt.subplot(axis[1])
plt.plot(data[:,'freq>freq']/f0, data[:,'threshold cubed>max_a'], color = colors_mod[0])
ax['analytic_cube_a'].spines['right'].set_visible(False)
ax['analytic_cube_a'].spines['top'].set_visible(False)
ax['analytic_cube_a'] = remove_tick_marks(ax['analytic_cube_a'])
ax['analytic_cube_a'] = remove_tick_ymarks(ax['analytic_cube_a'])
plt.xlim([0,5])
#embed()
#grid1 = gridspec.GridSpecFromSubplotSpec(1, 2,
# subplot_spec=grid00[1],wspace=0, hspace=0.7)
axis_sub = gridspec.GridSpecFromSubplotSpec(1, 1,
subplot_spec=grid0[2,:],wspace=0, hspace=0.7)
ax['sub'] = plt.subplot(axis_sub[0])
ax['sub'].spines['top'].set_visible(False)
ax['sub'].spines['bottom'].set_visible(False)
ax['sub'].spines['left'].set_visible(False)
ax['sub'].spines['right'].set_visible(False)
ax['sub'].set_yticks([])
ax['sub'].set_xticks([])
plt.title('Numerical',pad = -lp, fontsize = 10, fontweight='bold')
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[3, 0], wspace=0, hspace=0.5)
threshold = power_func(restrict=[2], a_fr=1, a_fe=0.2, eod_fr=eod_fr, eod_fe=eod_fe, win='w2', sigma=0,
sampling=sampling, deviation_dp=deviation_dp, deviation_s=deviation_s, beat_corr=beat_corr,
size=[120],
phase_zero=[0], delta_t=1, show_figure=True,
plot_dist=False, save=False, bef_c=-2, aft_c=-1)
ax['t_f'] = plt.subplot(axis[0])
plt.title('Threshold')
ax['t_f'].plot(beats / eod_fr + 1, np.array(threshold['result_frequency'][0]) / eod_fr, color=colors_tob[1])
ax['t_f'].spines['right'].set_visible(False)
ax['t_f'].spines['top'].set_visible(False)
ax['t_f'] = remove_tick_marks(ax['t_f'])
ax['t_f'].set_xlim([0, 5])
ax['t_f'].text(left, 1.1, string.ascii_uppercase[1], transform=ax['t_f'].transAxes,
size=nr_size, weight='bold')
ax['t_a'] = plt.subplot(axis[1])
ax['t_a'].plot(beats / eod_fr + 1, threshold['result_amplitude_max'][0], color=colors_mod[1])
ax['t_a'].spines['right'].set_visible(False)
ax['t_a'].spines['top'].set_visible(False)
ax['t_a'] = remove_tick_marks(ax['t_a'])
ax['t_a'].set_xlim([0, 5])
ax['t_f']= plt.subplot(axis[0])
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[3, 1], wspace=0, hspace=0.5)
cubed =power_func(restrict = [5],a_fr=1, a_fe=0.2, eod_fr=eod_fr, eod_fe=eod_fe, win='w2', sigma=0,
sampling=sampling, beat_corr=beat_corr, size=[120],
phase_zero=[0], delta_t=1, show_figure=True,
plot_dist=False, save=False, bef_c=-2, aft_c=-1)
ax['t_f'] = plt.subplot(axis[0])
plt.title('Threshold cubed')
ax['t_f'].plot(beats/eod_fr+1,np.array(cubed['result_frequency'][0])/eod_fr, color = colors_tob[1])
ax['t_f'].spines['right'].set_visible(False)
ax['t_f'].spines['top'].set_visible(False)
ax['t_f'] = remove_tick_marks(ax['t_f'])
ax['t_f'] = remove_tick_ymarks(ax['t_f'])
ax['t_f'].set_xlim([0, 5])
ax['t_a'] = plt.subplot(axis[1])
ax['t_a'] = remove_tick_marks(ax['t_a'])
ax['t_a'] = remove_tick_ymarks(ax['t_a'])
ax['t_a'].plot(beats/eod_fr+1,cubed['result_amplitude_max'][0], color = colors_mod[1])
ax['t_a'].spines['right'].set_visible(False)
ax['t_a'].spines['top'].set_visible(False)
ax['t_a'].set_xlim([0, 5])
#ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='tob max', nr=2, wide='gainsboro',
# middle='grey', narrow='black')
axis_sub = gridspec.GridSpecFromSubplotSpec(1, 1,
subplot_spec=grid0[4,:],wspace=0, hspace=0.7)
ax['sub'] = plt.subplot(axis_sub[0])
ax['sub'].spines['top'].set_visible(False)
ax['sub'].spines['bottom'].set_visible(False)
ax['sub'].spines['left'].set_visible(False)
ax['sub'].spines['right'].set_visible(False)
ax['sub'].set_yticks([])
ax['sub'].set_xticks([])
plt.title('Leaky Integrate and Fire - P-unit model',pad = -lp, fontsize = 10, fontweight='bold')
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[5,0], wspace=0, hspace=0.5)
data = pd.read_pickle('mean' + 'simulation' + '.pkl')
ax = plot_hz('data_hz',axis, 0, ax, data,nrs = [2],left = left, color = colors_tob[2])
plt.title('Threshold cubed')
ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='amp max', nr=1, wide='gainsboro',
middle='lightblue', narrow=colors_mod[2])
#ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='tob max', nr=2, wide='gainsboro',
# middle='grey', narrow='black')
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid0[5,1], wspace=0, hspace=0.5)
data = pd.read_pickle('mean' + 'sinz' + '.pkl')
ax = plot_hz('data_hz',axis, 0, ax, data,nr_size = nr_size,color = colors_tob[2])
plt.title('Threshold cubed')
ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='amp max', nr=1, wide='gainsboro',
middle='lightblue', narrow=colors_mod[2],rem = 'no')
#ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='tob max', nr=2, wide='gainsboro',
# middle='grey', narrow='black',rem = 'no')
ax['data_mod'] = remove_tick_ymarks(ax['data_mod'])
ax['data_hz'] = remove_tick_ymarks(ax['data_hz'])
ax_intermediate = gridspec.GridSpecFromSubplotSpec(1, 3,
subplot_spec=grid00[1], width_ratios = [1,2,1], wspace=0, hspace=0.5)
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=ax_intermediate[1], wspace=0, hspace=0.5)
data = pd.read_pickle('mean' + '' + '.pkl')
ax = plot_hz('data_hz',axis, 0, ax, data, nrs = [3],left = left, color = colors_tob[2])
plt.title('P-units', fontweight = 'bold')
ax = plot_amp('data_mod',axis, ax, data, nrs=[], nr_size=nr_size, name_nr='amp max', nr=1, wide='gainsboro',
middle='lightblue', narrow='steelblue')
plt.subplots_adjust(bottom = 0.1, top = 0.96)
plt.savefig('compare.pdf')
try:
plt.savefig('../highbeats_pdf/compare.pdf')
except:
a = 0
plt.show()
#fig.tight_layout()f
# fig.label_axes()
return fig
def plot_hz(name, grid, nr, ax, mean1,nrs = [], nr_size = 12,left = -0.1, color = 'red'):
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
all_means = mean1[mean1['type'] == 'max mean']
original = all_means[all_means['dev'] == 'original']
m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
dev = '05'#'005',
titels = ['0.5 ms']
versions = m05#m005,
keys = [k for k in versions][2::]
try:
data = np.array(versions[keys])[0]
except:
a = 0
i = 0
# grid = gridspec.GridSpec(5, 1, hspace=0.4, left=0.2)
# grid = gridspec.GridSpecFromSubplotSpec(1, 2, hspace = 0.63, subplot_spec=grid0[0])
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == 'max std']
std = all_means[all_means['dev'] == dev]
std = np.array(std[keys])[0]
# ax[0,i].set_title(dev[i] +' ms')
ax[name] = plt.subplot(grid[nr])
#ax[name].set_title(titels[i])
# ax[i+1].set_ylabel('MPF [EODf]')
all_means = mean1[mean1['type'] == 'max 95']
std95 = all_means[all_means['dev'] == dev]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == 'max 05']
std05 = all_means[all_means['dev'] == dev]
std05 = np.array(std05[keys])[0]
# std[np.where(list(data[sim] + std[sim])>std95[sim])] = std95[sim][np.where(list(data[sim] + std[sim])>std95[sim])]
# embed()
middle = 'pink' # 'LightSalmon'
wide = 'lightpink' # mistyrose'
narrow = color #'crimson'
ax[name].fill_between(np.array(keys)[sim], list(std95[sim]-1), list(std05[sim]-1),
color=wide, alpha=0.45) #
# embed()
ax[name].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]-1), list(data[sim] - std[sim]-1), color=middle)
ax[name].plot(x / 500, corr / 500, color='grey', linestyle='--', label='AMf')
ax[name].plot(np.array(keys)[sim], data[sim]-1, color=narrow)
ax[name].spines['right'].set_visible(False)
ax[name].spines['top'].set_visible(False)
# plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1]))
if nrs:
ax[name].text(left, 1.1, string.ascii_uppercase[nrs[i]], transform=ax[name].transAxes,
size=nr_size, weight='bold')
ax[name] = remove_tick_marks(ax[name])
ax[name].set_xlim([0,5])
# if i != 0:
# ax[i+1] = remove_tick_ymarks(ax[i+1])
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
return ax
if __name__ == "__main__":
#plot_style()
fig = plot_mean_cells()
#fig.savefig()
plt.savefig('compare.pdf')
try:
plt.savefig('../highbeats_pdf/compare.pdf')
except:
pass
plt.show()
# plt.close()

180
differentcells.py Normal file
View File

@ -0,0 +1,180 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
#from axes import label_axes, labelaxes_params
from myfunctions import auto_rows
from myfunctions import default_settings
from myfunctions import remove_tick_marks
from myfunctions import remove_tick_ymarks
import matplotlib.gridspec as gridspec
import string
def plot_single_cells(ax, data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1']):
colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
# labelaxes_params(xoffs=-3, yoffs=0, labels='A', font=dict(fontweight='bold'))
#baseline = pd.read_pickle('data_baseline.pkl')
#data_beat = pd.read_pickle('data_beat.pkl')
data_all = pd.read_pickle('beat_results_smoothed.pkl')
#data = np.unique(data_all['dataset'])[0]
#data = np.unique(data_all['dataset'])
end = ['original', '005','05', '2' ]
end = ['original','005']
y_sum = [[]]*len(data)*len(end)
counter = 0
for dd,set in enumerate(data):
for ee, e in enumerate(end):
d = data_all[data_all['dataset'] == set]
#x = d['delta_f'] / d['eodf'] + 1
#embed()
#y = d['result_frequency_' + e]
y = d['result_amplitude_max_' + e]
#y2 = d['result_amplitude_max_' + e]
y_sum[counter] = np.nanmax(y)
counter += 1
#print(np.nanmax(y))
#embed()
lim = np.max(y_sum)
rows = 1
cols = 3
#embed()
grid = gridspec.GridSpec(2,2,hspace = 0.3, width_ratios = [0.7,5], wspace = 0)
#grid1 = gridspec.GridSpecFromSubplotSpec(rows,cols,subplot_spec=grid[0], wspace=ws, hspace=0.4)#,
#grid3 = gridspec.GridSpecFromSubplotSpec(rows,cols,subplot_spec=grid[2], wspace=ws, hspace=0.4)#,
label1 = plt.subplot(grid[0])
label2 = plt.subplot(grid[2])
labels = [label1,label2]
titels = ['binary spikes','Gaussian 0.5 ms']# < 0.5 EODf,with 0.5 ms wide
fs_big = 11
weight = 'bold'
for ll,l in enumerate(labels):
#embed()
l.spines['right'].set_visible(False)
l.spines['left'].set_visible(False)
l.spines['top'].set_visible(False)
l.spines['bottom'].set_visible(False)
l.set_yticks([])
l.set_xticks([])
l.set_ylabel(titels[ll],labelpad = 15, fontsize = fs_big, fontweight=weight)
hd = 0.3
ws = 0.3
grid2 = gridspec.GridSpecFromSubplotSpec(rows,cols,subplot_spec=grid[1], wspace=ws, hspace=0.4)#,
end = ['original']
color_modul = ['steelblue']*len(end)
color_mpf = ['crimson']*len(end)
weight = 'normal'
y_sum1 = plot_single(lim, data, end, data_all, grid2, color_mpf, color_modul,weight = weight, nrs = [0,1,2],fs_big = fs_big,title = True, xlabel = False, label =False,remove =True)
grid4 = gridspec.GridSpecFromSubplotSpec(rows, cols, subplot_spec=grid[3], wspace=ws, hspace=0.4)#,
end = ['05']
#y_sum = [[]] * len(data)
y_sum2 = plot_single(lim, data, end, data_all, grid4, color_mpf, color_modul,weight = weight, nrs = [3,4,5],fs_big = fs_big,title = True, label = True,remove =False)
#for dd, d in enumerate(data):
# embed()
#embed()
#ax[1].set_ylim([0, np.nanmax([y_sum1,y_sum2])])
#ax[1, dd].set_ylim([0, 350])
plt.subplots_adjust(wspace = 0.4,left = 0.1, right = 0.96,top = 0.98,bottom = 0.2)
def plot_single(lim, data, end, data_all, grid1, color_mpf, color_modul,tob_lw = 0.5,tob_col = 'black', nr_size = 12, weight = 'bold',nrs = [0,1,2],fs_big = 11, title = True,label = True, xlabel = True,remove =True):
baseline = pd.read_pickle('data_baseline.pkl')
y_sum = [[]] * len(data)
ax = {}
for dd,set in enumerate(data):
for ee, e in enumerate(end):
d = data_all[data_all['dataset'] == set]
x = d['delta_f'] / d['eodf'] + 1
y = d['result_frequency_' + e]
y2 = d['result_amplitude_max_' + e]
y_sum[dd] = np.nanmax(y)
ff = d['delta_f'] / d['eodf'] + 1
fe = d['beat_corr']
#fig.suptitle(set)
grid2 = gridspec.GridSpecFromSubplotSpec(2,1, subplot_spec=grid1[dd], wspace=0.02, hspace=0.2) # ,
ax[0] = plt.subplot(grid2[0])
ax[0].plot(ff, fe, color=tob_col, linestyle='--',linewidth = tob_lw)
ax[0].plot(x, y, color=color_mpf[ee])
b = baseline[baseline['dataset'] == set]['spike_rate']
ax[0].axhline(y = b.iloc[0], color = 'grey', linestyle = 'dotted')
if title == True:
ax[0].set_title('Cell '+str(dd+1),fontsize = fs_big, fontweight=weight)
ax[set+e+str(1)] = plt.subplot(grid2[1])
if (set == data[0]) and (label == True):
ax[set+e+str(1)].set_ylabel('Modulation ')
ax[0].set_ylabel('MPF [EODf]')
if (set == data[0]) and (xlabel == True):
ax[set + e + str(1)].set_xlabel('EOD multiples')
#ax[0, dd].set_title(e + ' ms')
ax[0].set_xlim([0, 4])
ax[0].text(-0.1, 1.1, string.ascii_uppercase[nrs[dd]], transform=ax[0].transAxes,
size=nr_size, weight='bold')
ax[set+e+str(1)].plot(x, y2, color=color_modul[ee])
mod_tob = d['result_toblerone_max_' + e]
ax[set + e + str(1)].plot(x, mod_tob, color=tob_col,linestyle = '--', linewidth = tob_lw)
# ax[1,0].set_ylabel('modulation depth [Hz]')
#ax[2, ee].plot(x, y2, color=colors[0])
#ax[2, 0].set_ylabel(' modulation depth [Hz]')
# ax[1,ee].annotate("", xy=(0.53, 16.83), xytext=(0.53, 17.33), arrowprops=dict(arrowstyle="->"))
# ax[1,ee].annotate("", xy=(1.51, 16.83), xytext=(1.51, 17.33), arrowprops=dict(arrowstyle="->"))
#ax[1, 0].set_xlabel('stimulus frequency [EODf]')
#ax[1, 2].set_xlabel('stimulus frequency [EODf]')
#ax[2, 3].set_xlabel('stimulus frequency [EODf]')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[set+e+str(1)].spines['right'].set_visible(False)
ax[set+e+str(1)].spines['top'].set_visible(False)
#ax[2, ee].spines['right'].set_visible(False)
#ax[2, ee].spines['top'].set_visible(False)
ax[0].set_xlim([0, 5])
ax[set+e+str(1)].set_xlim([0, 5])
#plt.tight_layout()
# fig.label_axes()
ax[set+e+str(1)].set_ylim([0, lim])
#ax[0].set_ylim([0, 240])
#ax[set+e+str(1)] = remove_tick_marks(ax[0])
ax[0] = remove_tick_marks(ax[0])
if set != data[0]:
ax[0] = remove_tick_ymarks(ax[0])
ax[set + e + str(1)] = remove_tick_ymarks(ax[set+e+str(1)] )
if remove == True:
ax[set+e+str(1)] = remove_tick_marks(ax[set+e+str(1)])
#ax[0].set_ylim([0, lim])
return y_sum
if __name__ == "__main__":
data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1']
good_cells = ['2019-10-21-aa-invivo-1']
medium_cells = ['2019-11-18-am-invivo-1','2019-11-18-aj-invivo-1','2019-10-21-au-invivo-1']
bad_cells = ['2019-11-18-al-invivo-1','2019-10-28-aj-invivo-1','2019-10-21-ar-invivo-1']
data = ['2019-10-21-aa-invivo-1','2019-11-18-ac-invivo-1', '2019-10-28-aj-invivo-1']#'2019-11-18-ag-invivo-1''2019-11-18-ab-invivo-1'
data = ['2019-11-18-ac-invivo-1','2019-11-08-aa-invivo-1', '2019-10-28-aj-invivo-1']
default_settings(data,intermediate_width = 6.28,intermediate_length = 6.6)
#fig, ax = plt.subplots(nrows=2, ncols=3, sharex=True)
ax = {}
plot_single_cells(ax, data = data)
#fig.savefig()
plt.savefig('differentcells.pdf')
plt.savefig('../highbeats_pdf/differentcells.pdf')
# plt.subplots_adjust(left = 0.25)
plt.show()
#plt.close()

205
differentcells_trans.py Normal file
View File

@ -0,0 +1,205 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
#from axes import label_axes, labelaxes_params
from myfunctions import auto_rows
from myfunctions import default_settings
from myfunctions import remove_tick_marks
import matplotlib.gridspec as gridspec
import string
def plot_single_cells(ax, data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1']):
colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
# labelaxes_params(xoffs=-3, yoffs=0, labels='A', font=dict(fontweight='bold'))
#baseline = pd.read_pickle('data_baseline.pkl')
#data_beat = pd.read_pickle('data_beat.pkl')
data_all = pd.read_pickle('beat_results_smoothed.pkl')
#data = np.unique(data_all['dataset'])[0]
#data = np.unique(data_all['dataset'])
end = ['original', '005','05', '2' ]
end = ['original','005']
y_sum = [[]]*len(data)*len(end)
counter = 0
for dd,set in enumerate(data):
for ee, e in enumerate(end):
d = data_all[data_all['dataset'] == set]
#x = d['delta_f'] / d['eodf'] + 1
#embed()
#y = d['result_frequency_' + e]
y = d['result_amplitude_max_' + e]
#y2 = d['result_amplitude_max_' + e]
y_sum[counter] = np.nanmax(y)
counter += 1
#print(np.nanmax(y))
#embed()
lim = np.max(y_sum)
#embed()
grid = gridspec.GridSpec(1,3,width_ratios = [0.2,4,4],)
grid0 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=grid[0], wspace=0.02, hspace=0.1) # ,
label1 = plt.subplot(grid0[0])
label2 = plt.subplot(grid0[1])
label3 = plt.subplot(grid0[2])
labels = [label1,label2,label3]
titels = ['Cell 1','Cell 2','Cell 3']# < 0.5 EODf,with 0.5 ms wide
fs_big = 11
weight = 'bold'
for ll,l in enumerate(labels):
#embed()
l.spines['right'].set_visible(False)
l.spines['left'].set_visible(False)
l.spines['top'].set_visible(False)
l.spines['bottom'].set_visible(False)
l.set_yticks([])
l.set_xticks([])
l.set_ylabel(titels[ll],labelpad = 15, fontsize = fs_big, fontweight=weight, rotation = 0)
hd = 0.3
grid1 = gridspec.GridSpecFromSubplotSpec(3,1,subplot_spec=grid[1], wspace=0.02, hspace=0.3)#,
end = ['original']
color_modul1 = 'steelblue'
color_mpf = 'red'
y_sum1,moduls = plot_single(lim, data, end, data_all, grid1, color_mpf, color_modul1,arrows = False,nrs = [0,1,2], title = 'Binary spike trains',xlabel = True,yticks = False)
grid2 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=grid[2], wspace=0.02, hspace=0.4)#,
end = ['05']
end = ['2']
#y_sum = [[]] * len(data)
color_mpf = 'red'
color_modul = 'royalblue'
color_modul = 'steelblue'
color_mpf = 'darkred'
color_mpf = 'tomato'
color_mpf = 'red'
alpha = 1
y_sum2, moduls = plot_single(lim, data, end, data_all, grid2, color_mpf, color_modul,color_upper = color_modul1, alpha = alpha, arrows = True, mods = moduls, nrs = [3,4,5], title = '2 ms Gaussian',xlabel = False)
#for dd, d in enumerate(data):
# embed()
#embed()
#ax[1].set_ylim([0, np.nanmax([y_sum1,y_sum2])])
#ax[1, dd].set_ylim([0, 350])
plt.subplots_adjust(wspace = 0.4,left = 0.1,top = 0.94, right = 0.96,bottom = 0.1)
def plot_single(lim, data, end, data_all, grid1, color_mpf, color_modul,alpha = 1, color_upper = 'grey',mods = [],tob_col = 'black', tob_lw = '0.8', arrows = True, nr_size = 12, nrs = [0,1,2], xlabel = True,title = '',yticks =True):
baseline = pd.read_pickle('data_baseline.pkl')
y_sum = [[]] * len(data)
ax = {}
moduls = [[]]*len(data)
for dd,set in enumerate(data):
for ee, e in enumerate(end):
d = data_all[data_all['dataset'] == set]
x = d['delta_f'] / d['eodf'] + 1
#embed()
y = d['result_frequency_' + e]
y2 = d['result_amplitude_max_' + e]
y_sum[dd] = np.nanmax(y)
ff = d['delta_f'] / d['eodf'] + 1
fe = d['beat_corr']
#fig.suptitle(set)
grid2 = gridspec.GridSpecFromSubplotSpec(2, 1, height_ratios = [1,2], subplot_spec=grid1[dd], wspace=0.02, hspace=0.2) # ,
ax[0] = plt.subplot(grid2[0])
ax[0].plot(ff, fe, color=tob_col,linestyle = '--', linewidth = tob_lw, zorder = 2)
ax[0].plot(x, y, color=color_mpf, zorder = 3,alpha = alpha)
ax[0].text(-0.1, 1.1, string.ascii_uppercase[nrs[dd]], transform=ax[0].transAxes,
size=nr_size, weight='bold')
b = baseline[baseline['dataset'] == set]['spike_rate']
ax[0].axhline(y = b.iloc[0], color = 'grey', linestyle = 'dotted', zorder = 1)
if dd == 0:
plt.title(title)
ax[set+e+str(1)] = plt.subplot(grid2[1])
#ax[0, dd].set_title(e + ' ms')
ax[0].set_xlim([0, 4])
#if (e == 2) and xlabel == True:
ax[set+e+str(1)].plot(x, y2, color=color_modul,alpha = alpha,zorder = 2)
mod_tob = d['result_toblerone_max_' + e]
ax[set + e + str(1)].plot(x, mod_tob, color=tob_col,linestyle = '--', alpha = alpha,linewidth = tob_lw, zorder = 1)
if arrows == True:
plt.fill_between(x, y2,mods[dd], color = 'gainsboro', edgecolor= 'grey',zorder = 1)
plt.plot(x, mods[dd], alpha = 0.6, color = color_upper)#linewidth = 0.4,
array = [0.65, ]#2.651.65,
small_arrows = True
if small_arrows == True:
for a in range(len(array)):
#embed()
pos = np.argmin(np.abs(np.array(x)-array[a]))
x_present = np.array(x)[pos]
y2 = np.array(y2)
#embed()
pos_change = 2
nr = 19
nr2 = 5
#embed()
if (np.array(mods[dd])[pos]-y2[pos])>nr:
plt.plot([x_present, x_present],
[y2[pos] + nr, np.max(np.array(mods[dd])[pos - pos_change:pos + pos_change])-nr2],
color='black')
plt.scatter([x_present],[y2[pos]+nr], marker = 'v',s = 10, color='black')
moduls[dd] = y2
# ax[1,0].set_ylabel('modulation depth [Hz]')
#ax[2, ee].plot(x, y2, color=colors[0])
#ax[2, 0].set_ylabel(' modulation depth [Hz]')
# ax[1,ee].annotate("", xy=(0.53, 16.83), xytext=(0.53, 17.33), arrowprops=dict(arrowstyle="->"))
# ax[1,ee].annotate("", xy=(1.51, 16.83), xytext=(1.51, 17.33), arrowprops=dict(arrowstyle="->"))
#ax[1, 0].set_xlabel('stimulus frequency [EODf]')
if (dd == 2) and xlabel == True:
ax[set+e+str(1)].set_xlabel('stimulus frequency [EODf]')
ax[0].set_ylabel('[Hz]')
ax[set + e + str(1)].set_ylabel('Modulation ')
#ax[1, 2].set_xlabel('stimulus frequency [EODf]')
#ax[2, 3].set_xlabel('stimulus frequency [EODf]')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[set+e+str(1)].spines['right'].set_visible(False)
ax[set+e+str(1)].spines['top'].set_visible(False)
#ax[2, ee].spines['right'].set_visible(False)
#ax[2, ee].spines['top'].set_visible(False)
ax[0].set_xlim([0, 5])
ax[set+e+str(1)].set_xlim([0, 5])
#plt.tight_layout()
# fig.label_axes()
ax[set+e+str(1)].set_ylim([0, lim])
#ax[0].set_ylim([0, 240])
ax[0] = remove_tick_marks(ax[0])
if set != data[-1]:
ax[set+e+str(1)] = remove_tick_marks(ax[set+e+str(1)])
if yticks == True:
remove_tick_ymarks(ax[set+e+str(1)])
remove_tick_ymarks(ax[0])
#ax[0].set_ylim([0, lim])
return y_sum, moduls
if __name__ == "__main__":
data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1']
data = ['2019-10-21-aa-invivo-1', '2019-10-21-au-invivo-1', '2019-10-28-aj-invivo-1']
data = ['2019-09-23-ad-invivo-1', '2019-10-21-au-invivo-1', '2019-10-28-aj-invivo-1']
data = ['2019-10-21-aa-invivo-1','2019-11-18-ac-invivo-1', '2019-10-28-aj-invivo-1']#'2019-11-18-ag-invivo-1''2019-11-18-ab-invivo-1'
data = ['2019-11-18-ac-invivo-1','2019-11-08-aa-invivo-1', '2019-10-28-aj-invivo-1']
default_settings(data,intermediate_width = 6.28,intermediate_length = 6)
#fig, ax = plt.subplots(nrows=2, ncols=3, sharex=True)
ax = {}
plot_single_cells(ax, data = data)
#fig.savefig()
plt.savefig('differentcells_trans.pdf')
plt.savefig('../highbeats_pdf/differentcells_trans.pdf')
# plt.subplots_adjust(left = 0.25)
plt.show()
#plt.close()

295
functionssimulation.py Normal file
View File

@ -0,0 +1,295 @@
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
#from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
def remove_all_spines(ax, nr):
ax[nr].spines['right'].set_visible(False)
ax[nr].spines['top'].set_visible(False)
ax[nr].spines['left'].set_visible(False)
ax[nr].spines['bottom'].set_visible(False)
def find_beats(start,end,step,eod_fr):
eod_fe = np.arange(start, end, step)
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
return eod_fe, beat_corr, beats
def snip(left_c,right_c,e,g,sampling, deviation_s,d,eod_fr, a_fr, eod_fe,phase_zero,p, size,s, sigma,a_fe,deviation,beat_corr, chirp = True):
time, time_cut, cut = find_times(left_c[g], right_c[g], sampling, deviation_s[d])
eod_fish_e, eod_fish_r, period_fish_r, period_fish_e = find_periods(a_fe, time, eod_fr, a_fr, eod_fe, e)
#embed()
if chirp == True:
eod_fe_chirp = integrate_chirp(a_fe, time, eod_fe[e], phase_zero[p], size[s], sigma)
else:
eod_fe_chirp = eod_fish_e
eod_rec_down, eod_rec_up = rectify(eod_fish_r, eod_fe_chirp) # rectify
eod_overlayed_chirp = (eod_fish_r + eod_fe_chirp)[cut:-cut]
threshold_cube = (eod_rec_up) ** 3
maxima_values, maxima_index, maxima_interp = global_maxima(period_fish_e, period_fish_r,
eod_rec_up[cut:-cut]) # global maxima
index_peaks, value_peaks, peaks_interp = find_lm(eod_rec_up[cut:-cut]) # local maxima
middle_conv, eod_conv_down, eod_conv_up, eod_conv_downsampled = conv(eod_fr,sampling, cut, deviation[d], eod_rec_up,
eod_rec_down) # convolve
eod_fish_both = integrate_chirp(a_fe, time, eod_fe[e] - eod_fr, phase_zero[p], size[s], sigma)
am_corr_full = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s],
sigma) # indirect am calculation
_, time_fish, cut_f = find_times(left_c[g], right_c[g], eod_fr, deviation_s[d]) # downsampled through fish EOD
am_corr_ds = integrate_chirp(a_fe, time_fish, beat_corr[e], phase_zero[p], size[s], sigma)
am_df_ds = integrate_chirp(a_fe, time_fish, eod_fe[e] - eod_fr, phase_zero[p], size[s],
sigma) # indirect am calculation
return cut, threshold_cube , time_cut, eod_conv_up, am_corr_full, peaks_interp, maxima_interp, am_corr_ds,am_df_ds,eod_fish_both,eod_overlayed_chirp
def single_stim(ax,colors, row, col, eod_fr, eod_fe, e,lower, s = 0, p = 0, d = 0, labels = True,col_basic = 'silver',add = 'simple',df_col = 'blue', factor = 200, beat_corr_col = 'gold',col_hline = 'no', nfft = 4096, minus_bef = -30, delta_t = 0.014, sampling = 100000, deviation = [150],plus_bef = -10, a_fr = 1, phase_zero = [0], shift_phase = 0, size = [120],a_fe = 0.8,ax_nr = 'no',lw_whole = 0.5,y = 'yes'):
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
sigma = delta_t / math.sqrt((2 * math.log(10)))
# time, time_cut = find_times(time_range, sampling, deviation[d], 1)
left_c = minus_bef * delta_t * sampling
right_c = plus_bef * delta_t * sampling
time, time_cut, cut = find_times(left_c, right_c, sampling, deviation[d] / (1000 * sampling))
#embed()
time_fish_both = time * 2 * np.pi * (eod_fr - eod_fe[e])
eod_fish_both = 0.05 * np.sin(time_fish_both)
eod_fish_e, eod_fish_r, period_fish_r, period_fish_e = find_periods(a_fe, time, eod_fr, a_fr, eod_fe, e)
eod_fish_both = integrate_chirp(a_fe, time, eod_fe[e] - eod_fr, phase_zero[p] + shift_phase, size[s], sigma)
eod_fe_chirp = integrate_chirp(a_fe, time, eod_fe[e], phase_zero[p], size[s], sigma)
eod_overlayed_chirp = eod_fish_r + eod_fe_chirp
eod_rectified_down, eod_recitified_up = rectify(eod_fish_r, eod_fe_chirp) # rectify
maxima_values, maxima_index, maxima_interp = global_maxima(period_fish_e, period_fish_r,
eod_recitified_up) # global maxima
index_peaks, value_peaks, peaks_interp = find_lm(eod_recitified_up) # local maxima
try:
middle_conv, eod_convolved_down, eod_convolved_up, eod_conv_downsampled = conv(eod_fr, sampling, cut, deviation[d],
eod_recitified_up,
eod_rectified_down) # convolve
except:
middle_conv = []
eod_convolved_down = []
eod_convolved_up = []
eod_conv_downsampled = []
left_c = -200 * delta_t * sampling
right_c = 200 * delta_t * sampling
_, time_fish, _ = find_times(left_c, right_c, eod_fr, deviation[d]) # downsampled through fish EOD
am_fish = integrate_chirp(a_fe, time_fish, beat_corr[e], phase_zero[p], size[s], sigma)
middle_am = int(len(am_fish) / 2)
print(beat_corr[e])
am_corr = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p] + shift_phase, size[s],
sigma) # indirect am calculation
power, freq = ml.psd(maxima_interp - np.mean(maxima_interp), Fs=sampling, NFFT=nfft, noverlap=nfft / 2)
f_max = freq[np.argmax(power[freq < 0.5 * eod_fr])]
#ax['upper'].scatter(eod_fe[e] - eod_fr, f_max, color='red', s=19)
if plus_bef < 0:
green_true = False
ending = time[0] * 1000,
else:
ending = 0
green_true = True
plt.axvline(x=-7.5, color='black', linestyle='dotted', linewidth=1)
plt.axvline(x=7.5, color='black', linestyle='dotted', linewidth=1)
print(colors[e])
#embed()
ax[e] = pl_eods(eod_fish_both, cut, maxima_interp, maxima_index,
maxima_values, lower, e, e, time_cut, am_corr, eod_fe, eod_overlayed_chirp, deviation, d,
eod_fr, sampling, value_peaks, time_fish, am_fish, factor, eod_convolved_down, index_peaks,
eod_convolved_up, eod_recitified_up, add=add, green_true=green_true,
beat_corr_col=beat_corr_col, ending = ending, col_basic = col_basic, color_am=colors[e], df_col=df_col,ax_nr = ax_nr,lw_whole = lw_whole) #
for i in range(3):
ax[e].spines['right'].set_visible(False)
ax[e].spines['top'].set_visible(False)
ax[e].spines['left'].set_visible(True)
ax[e].spines['bottom'].set_visible(True)
if col_hline != 'no':
plt.axhline(y=0, color=col_hline, linewidth=0.5)
# embed()
xticks = 'off'
yticks = 'off'
plot_pos = col * row - col + 1
# if e+1 == plot_pos:
# ax[e].set_xlabel('Time [ms]', labelpad=5)
xaxis = np.arange(row * col - col + 1, row * col + 1, 1)
if e + 1 == xaxis[int(len(xaxis) / 2)] and (labels == True):
ax[e].set_xlabel('Time [ms]', labelpad=5)
if (e + 1 in np.arange(1, row * col + 1, col)) and (y == 'yes')and (labels == True):
ax[e].set_ylabel('[mv]', labelpad=5)
if (beat_corr_col != 'no') and (df_col != 'steelblue'):
ax[e].set_yticks([])
plt.subplots_adjust(wspace = 0.2)
# else:
# if xticks == 'off':
# ax[e].set_xticks([])
# if yticks == 'off':
# ax[e].set_yticks([])
# lower_left_label(e+1, col, row, 'Time [ms]', '[mv]',xticks = 'off',yticks = 'off',)
return f_max,eod_overlayed_chirp,ax
def title_variation(add, ax, eod_fe, eod_fr, e):
if add == True:
ax.title.set_text('DF:' + str(eod_fe[e] - eod_fr) + 'Hz ' + 'rf:' + str(eod_fr) + ' ef:' + str(eod_fe[e]))
elif add == 'simple':
ax.title.set_text('Beat:' + str(eod_fe[e] - eod_fr) + 'Hz')
elif add == 'no':
a = 2
else:
ax.title.set_text(
'Beat:' + str(eod_fe[e] - eod_fr) + 'Hz, Mult:' + str(int(((eod_fe[e] - eod_fr) / eod_fr + 1) * 100) / 100))
def pl_eods(eod_fish_both, cut, maxima_interp, maxima_index, maxima, gs0, i, e, time, am_corr, eod_fe, eod_overlayed_chirp, deviation, d, eod_fr, sampling, value_peaks, time_fish, am_fish, factor, eod_convolved_down, index_peaks, eod_convolved_up, eod_rectified_up, add = False,lw_red = 1.2, lw = 1, add1 = False,share = False,green_true = True,beat_corr_col = 'orange',color_am = 'red',df_col = 'pink',ax_nr = 'no',col_basic = 'silver',ending = 0, lw_whole = 0.5):
#if share == True:
# ax = fig.add_subplot(row, col, i + 1, sharex=ax,
# sharey=ax)
#else:
# ax = fig.add_subplot(row, col, i + 1)
#embed()
if type(ax_nr) != str:
ax = plt.subplot(gs0[ax_nr])
else:
ax = plt.subplot(gs0[int(e)])
# title variation
title_variation(add, ax, eod_fe, eod_fr, e)
# main version variations
if col_basic != 'no':
ax.plot(time * 1000-ending, eod_overlayed_chirp[cut:-cut],
label='EOD both fish',
color=col_basic, linewidth=lw_whole)
if beat_corr_col != 'no':
ax.plot(time * 1000-ending, am_corr +2.4, color=beat_corr_col, label='EOD adjusted beat', linewidth = lw)
if color_am != 'no':
ax.plot(time*1000-ending, maxima_interp[cut:-cut], color=color_am, label= 'AM',linewidth = lw)#[int(3 * deviation[d]):int(-3 * deviation[d])]
if df_col != 'no':
ax.plot(time*1000-ending,eod_fish_both[cut:-cut]+ 3.60,color=df_col,label= 'Difference frequency', linewidth = 0.6)
# additional version variations
if add1 == True:
ax.scatter((maxima_index - 0.5 * len(eod_rectified_up)) / (sampling / 1000), maxima,
color='red', s=10)
ax.plot(time_fish[int(3 * deviation[d] / factor):int(-3 * deviation[d] / factor)] * 1000,
am_fish[int(3 * deviation[d] / factor):int(-3 * deviation[d] / factor)] + 0.4, color='purple',
label='indirect am - downgesampled', linewidth=lw)
ax.plot((index_peaks - 0.5 * len(eod_rectified_up)) / (sampling / 1000), value_peaks,
color='green', label='all maxima')
if add == True:
ax.plot(time * 1000-ending, eod_convolved_up, color='red',linewidth = lw)
ax.plot(time * 1000-ending, eod_convolved_down, color='red', label='convolved',linewidth = lw)
# embed()
return ax
def find_times(left_c,right_c, sampling,deviation_s):
for_conv = 5 * deviation_s
time = np.arange(int(np.round(left_c))-1000, int(np.round(right_c))+1000, 1)
time = time[(time >left_c) &(time < right_c)]
time = time/sampling
#time = np.arange(-for_conv+left_c,for_conv+right_c, 1 / sampling)
cut = int(np.ceil(for_conv*sampling))
if cut == 0:
#time_cut = time*1
cut = 1
time_cut = time[cut:-cut]
else:
time_cut = time[cut:-cut]
#embed()
return time, time_cut, cut
def conv(eod_fr, sampling, cut,deviation, eod_rectified_up, eod_rectified_down):
if deviation* 5 % 2:
points = deviation * 5
else:
points = deviation * 5 - 1
#embed()
gaussian = signal.gaussian(points, std=deviation, sym=True)
gaussian_normalised = (gaussian * 2) / np.sum(gaussian)
length_convolved = int(len(gaussian_normalised) / 2)
eod_convolved_up = np.convolve(gaussian_normalised, eod_rectified_up)
eod_convolved_up = eod_convolved_up[length_convolved + cut:-length_convolved - cut]
eod_convolved_down = np.convolve(gaussian_normalised, eod_rectified_down)
eod_convolved_down = eod_convolved_down[length_convolved + cut:-length_convolved - cut]
middle_conv = int(len(eod_convolved_up) / 2)
eod_conv_downsampled = eod_convolved_up[0:-1:int(np.round(sampling / eod_fr))]
return middle_conv, eod_convolved_down, eod_convolved_up,eod_conv_downsampled
def find_dev(x, sampling):
deviation_ms = np.array(x)
deviation_s = deviation_ms/1000
deviation_dp = sampling*deviation_s
deviation_dp = list(map(int, deviation_dp))
return deviation_ms, deviation_s, deviation_dp
def find_periods(a_fe, time, eod_fr,a_fr,eod_fe,e):
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
period_fish_r = time_fish_r[(time_fish_r <= np.mean(time_fish_r)+2 * np.pi) & (time_fish_r > np.mean(time_fish_r))]
time_fish_e = time * 2 * np.pi * eod_fe[e]
eod_fish_e = a_fe * np.sin(time_fish_r)
period_fish_e = time_fish_e[(time_fish_e <= np.mean(time_fish_e)+ 2 * np.pi) & (time_fish_e > np.mean(time_fish_e))]
return eod_fish_e, eod_fish_r,period_fish_r,period_fish_e
def integrate_chirp(a_fe,time,beat,phase_zero,size, sigma):
I = ((np.pi ** 0.5) / 2) * sp.special.erf(time / sigma) - ((np.pi ** 0.5) / 2) * sp.special.erf(-np.inf)
phase = time * 2 * np.pi * beat+ 2 * np.pi * size * sigma * I + phase_zero
eod_fe_chirp = a_fe * np.sin(phase)
return eod_fe_chirp
def rectify(eod_fish_r,eod_fe_chirp):
eod_rec_up = eod_fish_r + eod_fe_chirp
eod_rectified_down = eod_fish_r + eod_fe_chirp
eod_rec_up[eod_rec_up < 0] = 0 # rectify
eod_rectified_down[eod_rectified_down > 0] = 0 # rectify
return eod_rectified_down, eod_rec_up
def find_lm(eod_rec_up):
x = signal.find_peaks(eod_rec_up)
index_peaks = x[0]
value_peaks = eod_rec_up[index_peaks]
peaks_interp = np.interp(np.arange(0, len(eod_rec_up), 1), index_peaks, value_peaks)
return index_peaks, value_peaks, peaks_interp
def global_maxima(period_fish_e,period_fish_r,eod_rectified_up):
#period_length = max(len(period_fish_e), len(period_fish_r))
period_length = len(period_fish_r)
if period_length >len(eod_rectified_up):
maxima_values = np.max(eod_rectified_up)
maxima_index = np.argmax(eod_rectified_up)
maxima_interp = [maxima_values]*len(eod_rectified_up)
else:
split_windows = np.arange(period_length, len(eod_rectified_up), period_length)
splits = np.split(eod_rectified_up, split_windows)
steps = np.arange(0, len(eod_rectified_up), len(splits[0]))
maxima_values = np.max(splits[0:-1], 1)
maxima_index = np.argmax(splits[0:-1], 1)
maxima_index = maxima_index + steps[0:-1]
maxima_interp = np.interp(np.arange(0, len(eod_rectified_up), 1), maxima_index, maxima_values)
return maxima_values,maxima_index, maxima_interp

162
introamtuning.py Normal file
View File

@ -0,0 +1,162 @@
from functionssimulation import find_lm
from functionssimulation import find_periods
from functionssimulation import integrate_chirp
from functionssimulation import global_maxima
from functionssimulation import find_lm
from functionssimulation import find_dev
from functionssimulation import remove_all_spines
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
#embed()
from functionssimulation import pl_eods
from functionssimulation import single_stim
from functionssimulation import find_times
#embed()
if __name__ == "__main__":
delta_t = 0.014 # ms
interest_interval = delta_t * 1.2
bef_c = interest_interval / 2
aft_c = interest_interval / 2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
size = [120] # maximal frequency excursion during chirp / 60 or 100 here
phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
phase_zero = np.arange(0,2*np.pi,2*np.pi/10)
eod_fr = 500 # eod fish reciever
a_fr = 1 # amplitude fish reciever
amplitude = a_fe = 0.50 # amplitude fish emitter
factor = 200
sampling = eod_fr * factor
sampling_fish = 500
start = 510
end = 3500
step = 500
win = 'w2'
minus_bef = -15
plus_bef = -10
eod_fe = np.array([10, 510,1010,1510])
eod_fe = np.array([560])
eod_fr = 500
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
#plot_eod(beat_corr, minus_bef, plus_bef,2,2,a_fe, delta_t, a_fr, size, beat_corr, 0, 0, a_fe, phase_zero, deviation_ms, eod_fe, deviation_dp, eod_fr, sampling, factor,fs = [6.2992, 5],shift_phase = (2 * np.pi) / 4)
ax = {}
s = 0
times_width = 1.5
times_up = 1
fig = plt.figure(figsize = [6.7,1.8*times_up])
beat_corr_col = 'goldenrod'
df_col = 'steelblue'
beat_corr_col = 'no'
df_col = 'no'
colors = ['red']
sampling_rate = sampling
nfft = 4096
row = 1
col = 1
p = 0
delta_t = 0.014 # ms
x = [ 1.5]
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
d = 0
e = 0
shift_phase = 0
ax = {}
lower = gridspec.GridSpec(1, 5,width_ratios = [1,0.33,1,0.33,1],left = 0.13,hspace = 0.5, wspace = 0.1, bottom = 0.27)
#ax = plot_beat_corr(ax,lower,beat_corr_col = beat_corr_col,df_col = df_col,ax_nr = 0)
#plt.grid()
left_c = minus_bef * delta_t * sampling
right_c = plus_bef * delta_t * sampling
time, time_cut, cut = find_times(left_c, right_c, sampling, deviation_dp[d] / (1000 * sampling))
time_fish_r = time * 2 * np.pi * eod_fe[0]
eod_fish_e = a_fe * np.sin(time_fish_r)
nr = 1
ax[nr] = plt.subplot(lower[nr])
plt.scatter(0,0, 200,marker = '+',color = 'black')
plt.xlim([-50,100])
#plt.ylim([-200, 200])
remove_all_spines(ax, nr)
ax[nr].set_yticks([])
ax[nr].set_xticks([])
ax[nr] .spines['right'].set_visible(False)
ax[nr] .spines['top'].set_visible(False)
#embed()
nr = 0
ax[nr] = plt.subplot(lower[nr])
plt.ylabel('[mv]')
#plt.xlabel('Time [ms]')
plt.xlabel('Time [ms]')
plt.title('Receiver f = '+str(eod_fr)+' Hz')
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
plt.plot(time * 1000-time[0]* 1000,eod_fish_r,color = 'silver')
ax[nr].set_xlabel('Time [ms]')
ax[nr] .spines['right'].set_visible(False)
ax[nr] .spines['top'].set_visible(False)
nr = 3
ax[nr] = plt.subplot(lower[nr])
#plt.scatter(0,0, 200,marker = '+',color = 'black')
plt.plot([-35, 15],[20,20], 10, color='black',linewidth = 3)
plt.plot([-35, 15],[-20,-20], 10, color='black',linewidth = 3)
ax[nr] .spines['right'].set_visible(False)
ax[nr] .spines['top'].set_visible(False)
plt.xlim([-50,100])
#plt.ylim([-200, 200])
remove_all_spines(ax, nr)
ax[nr].set_yticks([])
ax[nr].set_xticks([])
plt.ylim([-200, 200])
fmax,lims,ax = single_stim(ax,colors, row, col, eod_fr, eod_fe,e,lower, s = s, p = p, d = d, df_col = df_col, factor = factor, beat_corr_col = beat_corr_col, nfft = nfft, minus_bef = minus_bef, delta_t = delta_t, sampling = sampling,
plus_bef = plus_bef,a_fe = a_fe, a_fr = a_fr, phase_zero = phase_zero, shift_phase = shift_phase, size = size, a_fe = a_fe,ax_nr = 4,lw_whole = 1,y = 'no')
#embed()
minumum = np.min([lims, eod_fish_e, eod_fish_r])
maximum = np.max([lims, eod_fish_e, eod_fish_r])
plt.ylim([minumum,maximum])
nr = 2
ax[nr] = plt.subplot(lower[nr])
#plt.ylabel('[mv]')
plt.title('Emitting f = ' + str(eod_fe[0])+' Hz')
plt.plot(time * 1000-time[0]* 1000,eod_fish_e,color = 'silver')
plt.xlabel('Time [ms]')
plt.ylim([minumum, maximum])
ax[0].set_ylim([minumum, maximum])
ax[nr] .spines['right'].set_visible(False)
ax[nr] .spines['top'].set_visible(False)
ax[2].set_ylim([minumum, maximum])
plt.savefig('introamtuning.pdf')
plt.savefig('../highbeats_pdf/introamtuning.pdf')
plt.show()

94
isi_model.py Normal file
View File

@ -0,0 +1,94 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model import simulate, load_models
import matplotlib.gridspec as gridspec
from myfunctions import default_settings
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
#embed()
parameters = load_models("models.csv")
freq_all = [[]]*len(parameters)
isis_all = [[]] * len(parameters)
spikes_all = [[]] * len(parameters)
period_all = [[]]* len(parameters)
#for i in range(4):
for i in range(len(parameters)):
model_params = parameters[i]
cell = model_params.pop('cell')
print(cell)
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length,deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
period_all[i] = 1/EODf
# cut off first second of response
new_spikes = spikes[spikes >1]
#spikes_all[i] = new_spikes*1
freq,isis = calculate_isi_frequency(new_spikes, deltat)
freq_all[i] = freq
isis_all[i] = isis
#embed()
default_settings([0], intermediate_width=6.29*2, intermediate_length=10, ts=9, ls=9, fs=7)
row = int(np.sqrt(len(parameters)))
col = int(np.ceil(np.sqrt(len(parameters))))
grid = gridspec.GridSpec(row,col,hspace = 1, wspace = 0.5)
ax = {}
for i in range(len(freq_all)):
ax[i] = plt.subplot(grid[i])
plt.title('#'+str(i) +'B: '+str(int(np.mean(freq_all[i])))+'Hz')
plt.hist(isis_all[i]/(1/EODf), bins = 100, density = True)
ax[int(row*col-row/2)].set_xlabel('EOD multiples')
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
embed()
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

121
main.py Normal file
View File

@ -0,0 +1,121 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model import simulate, load_models
import matplotlib.gridspec as gridspec
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
example_cell_idx = 20
# load model parameter:
parameters = load_models("models.csv")
model_params = parameters[example_cell_idx]
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
print("Example with cell:", cell)
# generate EOD-like stimulus with an amplitude step:
deltat = model_params["deltat"]
stimulus_length = 2.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2*np.pi*EODf*time)
# amplitude step with given contrast:
t0 = 0.5
t1 = 1.5
contrast = 0.3
stimulus[int(t0//deltat):int(t1//deltat)] *= (1.0+contrast)
# integrate the model:
spikes = simulate(stimulus, **model_params)
# some analysis an dplotting:
#embed()
grid = gridspec.GridSpec(int(np.sqrt(len(parameters))), int(np.ceil(np.sqrt(len(parameters)))))
parameters = load_models("models.csv")
for i in range(4):
#for i in range(len(parameters)):
model_params = parameters[i]
print(cell)
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
# cut off first second of response
new_spikes = spikes[spikes >1]
freq,isis = calculate_isi_frequency(new_spikes, deltat)
#embed()
plt.subplot(grid[i])
plt.title('B:'+np.mean(freq))
plt.hist(isis, bins = 100, density = True)
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
freq_time = np.arange(spikes[0], spikes[-1], deltat)
fig, axs = plt.subplots(2, 1, sharex="col")
axs[0].plot(time, stimulus)
axs[0].set_title("Stimulus")
axs[0].set_ylabel("Amplitude in mV")
axs[1].plot(freq_time, freq)
axs[1].set_title("Model Frequency")
axs[1].set_ylabel("Frequency in Hz")
axs[1].set_xlabel("Time in s")
plt.show()
plt.close()
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

80
model.py Normal file
View File

@ -0,0 +1,80 @@
import numpy as np
try:
from numba import jit
except ImportError:
def jit(nopython):
def decorator_jit(func):
return func
return decorator_jit
def load_models(file):
""" Load model parameter from csv file.
Parameters
----------
file: string
Name of file with model parameters.
Returns
-------
parameters: list of dict
For each cell a dictionary with model parameters.
"""
parameters = []
with open(file, 'r') as file:
header_line = file.readline()
header_parts = header_line.strip().split(",")
keys = header_parts
for line in file:
line_parts = line.strip().split(",")
parameter = {}
for i in range(len(keys)):
parameter[keys[i]] = float(line_parts[i]) if i > 0 else line_parts[i]
parameters.append(parameter)
return parameters
@jit(nopython=True)
def simulate(stimulus, deltat=0.00005, v_zero=0.0, a_zero=2.0, threshold=1.0, v_base=0.0,
delta_a=0.08, tau_a=0.1, v_offset=-10.0, mem_tau=0.015, noise_strength=0.05,
input_scaling=60.0, dend_tau=0.001, ref_period=0.001):
""" Simulate a P-unit.
Returns
-------
spike_times: 1-D array
Simulated spike times in seconds.
"""
# initial conditions:
v_dend = stimulus[0]
v_mem = v_zero
adapt = a_zero
# prepare noise:
noise = np.random.randn(len(stimulus))
noise *= noise_strength / np.sqrt(deltat)
# rectify stimulus array:
stimulus = stimulus.copy()
stimulus[stimulus < 0.0] = 0.0
# integrate:
spike_times = []
for i in range(len(stimulus)):
v_dend += (-v_dend + stimulus[i]) / dend_tau * deltat
v_mem += (v_base - v_mem + v_offset + (
v_dend * input_scaling) - adapt + noise[i]) / mem_tau * deltat
adapt += -adapt / tau_a * deltat
# refractory period:
if len(spike_times) > 0 and (deltat * i) - spike_times[-1] < ref_period + deltat/2:
v_mem = v_base
# threshold crossing:
if v_mem > threshold:
v_mem = v_base
spike_times.append(i * deltat)
adapt += delta_a / tau_a
return np.array(spike_times)

80
model_max.py Normal file
View File

@ -0,0 +1,80 @@
import numpy as np
try:
from numba import jit
except ImportError:
def jit(nopython):
def decorator_jit(func):
return func
return decorator_jit
def load_models(file):
""" Load model parameter from csv file.
Parameters
----------
file: string
Name of file with model parameters.
Returns
-------
parameters: list of dict
For each cell a dictionary with model parameters.
"""
parameters = []
with open(file, 'r') as file:
header_line = file.readline()
header_parts = header_line.strip().split(",")
keys = header_parts
for line in file:
line_parts = line.strip().split(",")
parameter = {}
for i in range(len(keys)):
parameter[keys[i]] = float(line_parts[i]) if i > 0 else line_parts[i]
parameters.append(parameter)
return parameters
@jit(nopython=True)
def simulate(stimulus, deltat=0.00005, v_zero=0.0, a_zero=2.0, threshold=1.0, v_base=0.0,
delta_a=0.08, tau_a=0.1, v_offset=-10.0, mem_tau=0.015, noise_strength=0.05,
input_scaling=60.0, dend_tau=0.001, ref_period=0.001):
""" Simulate a P-unit.
Returns
-------
spike_times: 1-D array
Simulated spike times in seconds.
"""
# initial conditions:
v_dend = stimulus[0]
v_mem = v_zero
adapt = a_zero
# prepare noise:
noise = np.random.randn(len(stimulus))
noise *= noise_strength / np.sqrt(deltat)
# rectify stimulus array:
stimulus = stimulus.copy()
#stimulus[stimulus < 0.0] = 0.0
# integrate:
spike_times = []
for i in range(len(stimulus)):
v_dend += (-v_dend + stimulus[i]) / dend_tau * deltat
v_mem += (v_base - v_mem + v_offset + (
v_dend * input_scaling) - adapt + noise[i]) / mem_tau * deltat
adapt += -adapt / tau_a * deltat
# refractory period:
if len(spike_times) > 0 and (deltat * i) - spike_times[-1] < ref_period + deltat/2:
v_mem = v_base
# threshold crossing:
if v_mem > threshold:
v_mem = v_base
spike_times.append(i * deltat)
adapt += delta_a / tau_a
return np.array(spike_times)

81
model_sinz.py Normal file
View File

@ -0,0 +1,81 @@
import numpy as np
try:
from numba import jit
except ImportError:
def jit(nopython):
def decorator_jit(func):
return func
return decorator_jit
def load_models(file):
""" Load model parameter from csv file.
Parameters
----------
file: string
Name of file with model parameters.
Returns
-------
parameters: list of dict
For each cell a dictionary with model parameters.
"""
parameters = []
with open(file, 'r') as file:
header_line = file.readline()
header_parts = header_line.strip().split(",")
keys = header_parts
for line in file:
line_parts = line.strip().split(",")
parameter = {}
for i in range(len(keys)):
parameter[keys[i]] = float(line_parts[i]) if i > 0 else line_parts[i]
parameters.append(parameter)
return parameters
@jit(nopython=True, )
def simulate(stimulus,nr, deltat=0.00005, v_zero=0.0, a_zero=2.0, threshold=1.0, v_base=0.0,
delta_a=0.08, tau_a=0.1, v_offset=-10.0, mem_tau=0.015, noise_strength=0.05,
input_scaling=60.0, dend_tau=0.001, ref_period=0.001):
""" Simulate a P-unit.
Returns
-------
spike_times: 1-D array
Simulated spike times in seconds.
"""
# initial conditions:
v_dend = stimulus[0]
v_mem = v_zero
adapt = a_zero
# prepare noise:
noise = np.random.randn(len(stimulus))
noise *= noise_strength / np.sqrt(deltat)
# rectify stimulus array:
stimulus = stimulus.copy()
stimulus[stimulus < 0.0] = 0.0
stimulus = (stimulus)**nr
# integrate:
spike_times = []
for i in range(len(stimulus)):
v_dend += (-v_dend + stimulus[i]) / dend_tau * deltat
v_mem += (v_base - v_mem + v_offset + (
v_dend * input_scaling) - adapt + noise[i]) / mem_tau * deltat
adapt += -adapt / tau_a * deltat
# refractory period:
if len(spike_times) > 0 and (deltat * i) - spike_times[-1] < ref_period + deltat/2:
v_mem = v_base
# threshold crossing:
if v_mem > threshold:
v_mem = v_base
spike_times.append(i * deltat)
adapt += delta_a / tau_a
return np.array(spike_times)

245
modell_all_cell.py Normal file
View File

@ -0,0 +1,245 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model import simulate, load_models
import matplotlib.gridspec as gridspec
from plot_eod_chirp import power_parameters
from scipy.ndimage import gaussian_filter
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
example_cell_idx = 11
# load model parameter:
parameters = load_models("models.csv")
counter = 0
beat_results = [[]] * (3 * len(parameters))
beat_results = []
for d in range(len(parameters)):
model_params = parameters[d]
cell = model_params.pop('cell')
eod_fr = model_params.pop('EODf')
print("Example with cell:", cell)
step = 20
eod_fe = np.arange(0, eod_fr * 5, step)
# generate EOD-like stimulus with an amplitude step:
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
#time = np.arange(0, stimulus_length, deltat)
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
a_fr = 1 # amplitude fish reciever
a_fe = 0.2 # amplitude fish emitter
# results = [[]]*
p_array = [[]]*len(eod_fe)
for e in range(len(eod_fe)):
f_corr = create_beat_corr(np.array([eod_fe[e]]), np.array([eod_fr]))
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
time_fish_e = time * 2 * np.pi * eod_fe[e]
eod_fish_e = a_fe * np.sin(time_fish_e)
stimulus = eod_fish_e+eod_fish_r
# integrate the model:
spikes = simulate(stimulus, **model_params)
spikes_new = spikes[spikes > 1]
sampling_rate = 1/deltat
spikes_mat = np.zeros(int(spikes_new[-1] * sampling_rate) + 2)
spikes_idx = np.round((spikes_new) * sampling_rate)
for spike in spikes_idx:
spikes_mat[int(spike)] = 1
spikes_mat = spikes_mat*sampling_rate
#window005 = 0.00005 * sampling_rate
window05 = 0.0005 * sampling_rate
window2 = 0.002 * sampling_rate
#smoothened_spikes_mat005 = gaussian_filter(spikes_mat, sigma=window005)
smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05)
smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2)
nfft = 4096*2
freq_step = sampling_rate / nfft
name = 'threshold'
p_array, f = ml.psd(spikes_mat - np.mean(spikes_mat), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
p05_array, f = ml.psd(smoothened_spikes_mat05 - np.mean(smoothened_spikes_mat05), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
p2_array, f = ml.psd(smoothened_spikes_mat2 - np.mean(smoothened_spikes_mat2), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
beat_results.append({})
#embed()
#p_array = np.nanmean(p_array, axis=1)
beat_results[-1]['result_frequency_original'] = f[np.argmax(p_array[f < 0.5 * eod_fr])] # f ist immer gleich
beat_results[-1]['result_amplitude_original'] = np.sqrt(si.trapz(p_array, f, freq_step))
beat_results[-1]['result_amplitude_max_original'] = np.sqrt(
np.max(p_array[f < 0.5 * eod_fr]) * freq_step)
beat_results[-1]['result_toblerone_max_original'] = np.sqrt(
(p_array[np.argmin(np.abs(f - f_corr[0]))]) * freq_step)
beat_results[-1]['result_frequency_whole'] = f[np.argmax(p_array)] # f ist immer gleich
beat_results[-1]['result_amplitude_whole'] = np.sqrt(si.trapz(p_array, f, freq_step))
beat_results[-1]['result_amplitude_max_whole'] = np.sqrt(np.max(p_array) * freq_step)
beat_results[-1]['result_toblerone_max_whole'] = np.sqrt(
(p_array[np.argmin(np.abs(f - f_corr[0]))]) * freq_step)
#if b != 'no':
# beat_results[-1]['result_baseline_max_whole'] = np.sqrt(
# (p_array[np.argmin(np.abs(f - b))]) * freq_step)
# beat_results[-1]['result_baseline_max_original'] = np.sqrt(
# (p_array[np.argmin(np.abs(f - b))]) * freq_step)
# arrays = [p05_array, p005_array, p2_array, p_inst_array]
# titles = ['05', '005', '2', 'inst']
#array = [smoothened_spikes_mat05,smoothened_spikes_mat2]
arrays = [p05_array, p2_array]
#arrays = [[]]*len(array)
titles = ['05','2']
for a in range(len(arrays)):
beat_results[-1]['result_frequency_' + titles[a]] = f[np.argmax(arrays[a])]
beat_results[-1]['result_frequency_half' + titles[a]] = f[np.argmax(arrays[a][f < 0.5 * eod_fr])]
beat_results[-1]['result_amplitude_' + titles[a]] = np.sqrt(si.trapz(arrays[a], f, freq_step))
beat_results[-1]['result_amplitude_max_half' + titles[a]] = np.sqrt(
np.max(arrays[a][f < 0.5 * eod_fr]) * freq_step)
beat_results[-1]['result_amplitude_max_' + titles[a]] = np.sqrt(np.max(arrays[a]) * freq_step)
beat_results[-1]['result_toblerone_max_' + titles[a]] = np.sqrt(
arrays[a][np.argmin(np.abs(f - f_corr[0]))] * freq_step)
#if b != 'no':
# beat_results[-1]['result_baseline_max_' + titles[a]] = np.sqrt(
# arrays[a][np.argmin(np.abs(f - b))] * freq_step)
# try:
# beat_results[-1],p,f = power_parameters(beat_results[-1], array[i], 1/deltat, nfft, name, eod_fr,title = titles[i],max_all = 'result_amplitude_max_',max = 'result_amplitude_max_half',integral = 'result_amplitude_',plot = False,freq_half = 'result_frequency_half' ,freq_whole = 'result_frequency_' )
# except:
# embed()
beat_results[-1]['dataset'] = cell
beat_results[-1]['eodf'] = eod_fr
#beat_results[counter]['baseline_fr'] = fr
beat_results[-1]['beat_corr'] = f_corr
beat_results[-1]['delta_f'] = eod_fe[e] -eod_fr
#if key[0] == 50:
# embed()
beat_results[-1]['contrast'] = a_fe
beat_results[-1]['type'] = name
#beat_results[-1]['f_original'] = max_peak
beat_results[-1]['f'] = f
#counter += 1
#embed()
df_beat = pd.DataFrame(beat_results)
df_beat = df_beat.dropna()
df_beat.to_pickle('modell_all_cell.pkl')
df_beat.to_csv('modell_all_cell.csv')
np.save('modell_all_cell.npy', df_beat)
embed()
ax = {}
for i in range(len(results)):
ax[i] = plt.subplot(2,3,i+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['f'],color = 'red')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['f'],color = 'red')
ax[0].set_ylabel('[Hz]')
ax[i].set_ylim([0,eod_fr/2])
for i in range(len(results)):
ax[i+len(results)] = plt.subplot(2,3,i+len(results)+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['max'],color = 'steelblue')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['max'],color = 'blue')
ax[len(results)].set_ylabel('Modulation')
ax[len(results)+i].set_xlabel('EOD multiples')
plt.subplots_adjust(wspace = 0.3)
plt.savefig('modell_single_cell.pdf')
plt.savefig('../highbeats_pdf/cell_simulations/modell_single_cell'+cell+'.pdf')
plt.show()
embed()
# some analysis an dplotting:
#embed()
grid = gridspec.GridSpec(int(np.sqrt(len(parameters))), int(np.ceil(np.sqrt(len(parameters)))))
parameters = load_models("models.csv")
for i in range(4):
#for i in range(len(parameters)):
model_params = parameters[i]
print(cell)
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
# cut off first second of response
new_spikes = spikes[spikes >1]
freq,isis = calculate_isi_frequency(new_spikes, deltat)
#embed()
plt.subplot(grid[i])
plt.title('B:'+np.mean(freq))
plt.hist(isis, bins = 100, density = True)
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
freq_time = np.arange(spikes[0], spikes[-1], deltat)
fig, axs = plt.subplots(2, 1, sharex="col")
axs[0].plot(time, stimulus)
axs[0].set_title("Stimulus")
axs[0].set_ylabel("Amplitude in mV")
axs[1].plot(freq_time, freq)
axs[1].set_title("Model Frequency")
axs[1].set_ylabel("Frequency in Hz")
axs[1].set_xlabel("Time in s")
plt.show()
plt.close()
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

252
modell_all_cell_sinz.py Normal file
View File

@ -0,0 +1,252 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model_sinz import simulate, load_models
import matplotlib.gridspec as gridspec
from plot_eod_chirp import power_parameters
from scipy.ndimage import gaussian_filter
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
example_cell_idx = 11
# load model parameter:
nrs = [5, 7, 9, 11, 3]
nrs = [2,4,2.5,0.5,1.5]
for n in nrs:
print(n)
parameters = load_models("models.csv")
counter = 0
beat_results = [[]] * (3 * len(parameters))
beat_results = []
beat_results = []
for d in range(len(parameters)):
model_params = parameters[d]
cell = model_params.pop('cell')
eod_fr = model_params.pop('EODf')
print("Example with cell:", cell)
step = 20
eod_fe = np.arange(0, eod_fr * 5, step)
# generate EOD-like stimulus with an amplitude step:
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
#time = np.arange(0, stimulus_length, deltat)
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
a_fr = 1 # amplitude fish reciever
a_fe = 0.2 # amplitude fish emitter
# results = [[]]*
p_array = [[]]*len(eod_fe)
for e in range(len(eod_fe)):
f_corr = create_beat_corr(np.array([eod_fe[e]]), np.array([eod_fr]))
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
time_fish_e = time * 2 * np.pi * eod_fe[e]
eod_fish_e = a_fe * np.sin(time_fish_e)
stimulus = eod_fish_e+eod_fish_r
# integrate the model:
#embed()
spikes = simulate(stimulus,n, **model_params)
spikes_new = spikes[spikes > 1]
sampling_rate = 1/deltat
if len(spikes_new>0):
spikes_mat = np.zeros(int(spikes_new[-1] * sampling_rate) + 2)
spikes_idx = np.round((spikes_new) * sampling_rate)
for spike in spikes_idx:
spikes_mat[int(spike)] = 1
spikes_mat = spikes_mat*sampling_rate
#window005 = 0.00005 * sampling_rate
window05 = 0.0005 * sampling_rate
window2 = 0.002 * sampling_rate
#smoothened_spikes_mat005 = gaussian_filter(spikes_mat, sigma=window005)
smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05)
smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2)
nfft = 4096*2
freq_step = sampling_rate / nfft
name = 'threshold'
p_array, f = ml.psd(spikes_mat - np.mean(spikes_mat), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
p05_array, f = ml.psd(smoothened_spikes_mat05 - np.mean(smoothened_spikes_mat05), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
p2_array, f = ml.psd(smoothened_spikes_mat2 - np.mean(smoothened_spikes_mat2), Fs=sampling_rate, NFFT=nfft,
noverlap=nfft / 2) #
beat_results.append({})
#embed()
#p_array = np.nanmean(p_array, axis=1)
beat_results[-1]['result_frequency_original'] = f[np.argmax(p_array[f < 0.5 * eod_fr])] # f ist immer gleich
beat_results[-1]['result_amplitude_original'] = np.sqrt(si.trapz(p_array, f, freq_step))
beat_results[-1]['result_amplitude_max_original'] = np.sqrt(
np.max(p_array[f < 0.5 * eod_fr]) * freq_step)
beat_results[-1]['result_toblerone_max_original'] = np.sqrt(
(p_array[np.argmin(np.abs(f - f_corr[0]))]) * freq_step)
beat_results[-1]['result_frequency_whole'] = f[np.argmax(p_array)] # f ist immer gleich
beat_results[-1]['result_amplitude_whole'] = np.sqrt(si.trapz(p_array, f, freq_step))
beat_results[-1]['result_amplitude_max_whole'] = np.sqrt(np.max(p_array) * freq_step)
beat_results[-1]['result_toblerone_max_whole'] = np.sqrt(
(p_array[np.argmin(np.abs(f - f_corr[0]))]) * freq_step)
#if b != 'no':
# beat_results[-1]['result_baseline_max_whole'] = np.sqrt(
# (p_array[np.argmin(np.abs(f - b))]) * freq_step)
# beat_results[-1]['result_baseline_max_original'] = np.sqrt(
# (p_array[np.argmin(np.abs(f - b))]) * freq_step)
# arrays = [p05_array, p005_array, p2_array, p_inst_array]
# titles = ['05', '005', '2', 'inst']
#array = [smoothened_spikes_mat05,smoothened_spikes_mat2]
arrays = [p05_array, p2_array]
#arrays = [[]]*len(array)
titles = ['05','2']
for a in range(len(arrays)):
beat_results[-1]['result_frequency_' + titles[a]] = f[np.argmax(arrays[a])]
beat_results[-1]['result_frequency_half' + titles[a]] = f[np.argmax(arrays[a][f < 0.5 * eod_fr])]
beat_results[-1]['result_amplitude_' + titles[a]] = np.sqrt(si.trapz(arrays[a], f, freq_step))
beat_results[-1]['result_amplitude_max_half' + titles[a]] = np.sqrt(
np.max(arrays[a][f < 0.5 * eod_fr]) * freq_step)
beat_results[-1]['result_amplitude_max_' + titles[a]] = np.sqrt(np.max(arrays[a]) * freq_step)
beat_results[-1]['result_toblerone_max_' + titles[a]] = np.sqrt(
arrays[a][np.argmin(np.abs(f - f_corr[0]))] * freq_step)
#if b != 'no':
# beat_results[-1]['result_baseline_max_' + titles[a]] = np.sqrt(
# arrays[a][np.argmin(np.abs(f - b))] * freq_step)
# try:
# beat_results[-1],p,f = power_parameters(beat_results[-1], array[i], 1/deltat, nfft, name, eod_fr,title = titles[i],max_all = 'result_amplitude_max_',max = 'result_amplitude_max_half',integral = 'result_amplitude_',plot = False,freq_half = 'result_frequency_half' ,freq_whole = 'result_frequency_' )
# except:
# embed()
beat_results[-1]['dataset'] = cell
beat_results[-1]['eodf'] = eod_fr
#beat_results[counter]['baseline_fr'] = fr
beat_results[-1]['beat_corr'] = f_corr
beat_results[-1]['delta_f'] = eod_fe[e] -eod_fr
#if key[0] == 50:
# embed()
beat_results[-1]['contrast'] = a_fe
beat_results[-1]['type'] = name
#beat_results[-1]['f_original'] = max_peak
beat_results[-1]['f'] = f
#counter += 1
#embed()
df_beat = pd.DataFrame(beat_results)
df_beat = df_beat.dropna()
df_beat.to_pickle('modell_all_cell_sinz'+str(n)+'.pkl')
df_beat.to_csv('modell_all_cell_sinz'+str(n)+'.csv')
np.save('modell_all_cell_sinz'+str(n)+'.npy', df_beat)
embed()
ax = {}
for i in range(len(results)):
ax[i] = plt.subplot(2,3,i+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['f'],color = 'red')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['f'],color = 'red')
ax[0].set_ylabel('[Hz]')
ax[i].set_ylim([0,eod_fr/2])
for i in range(len(results)):
ax[i+len(results)] = plt.subplot(2,3,i+len(results)+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['max'],color = 'steelblue')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['max'],color = 'blue')
ax[len(results)].set_ylabel('Modulation')
ax[len(results)+i].set_xlabel('EOD multiples')
plt.subplots_adjust(wspace = 0.3)
plt.savefig('modell_single_cell.pdf')
plt.savefig('../highbeats_pdf/cell_simulations/modell_single_cell'+cell+'.pdf')
plt.show()
embed()
# some analysis an dplotting:
#embed()
grid = gridspec.GridSpec(int(np.sqrt(len(parameters))), int(np.ceil(np.sqrt(len(parameters)))))
parameters = load_models("models.csv")
for i in range(4):
#for i in range(len(parameters)):
model_params = parameters[i]
print(cell)
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
# cut off first second of response
new_spikes = spikes[spikes >1]
freq,isis = calculate_isi_frequency(new_spikes, deltat)
#embed()
plt.subplot(grid[i])
plt.title('B:'+np.mean(freq))
plt.hist(isis, bins = 100, density = True)
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
freq_time = np.arange(spikes[0], spikes[-1], deltat)
fig, axs = plt.subplots(2, 1, sharex="col")
axs[0].plot(time, stimulus)
axs[0].set_title("Stimulus")
axs[0].set_ylabel("Amplitude in mV")
axs[1].plot(freq_time, freq)
axs[1].set_title("Model Frequency")
axs[1].set_ylabel("Frequency in Hz")
axs[1].set_xlabel("Time in s")
plt.show()
plt.close()
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

166
modell_single_cell.py Normal file
View File

@ -0,0 +1,166 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model import simulate, load_models
import matplotlib.gridspec as gridspec
from plot_eod_chirp import power_parameters
from scipy.ndimage import gaussian_filter
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
example_cell_idx = 11
# load model parameter:
parameters = load_models("models.csv")
model_params = parameters[example_cell_idx]
cell = model_params.pop('cell')
eod_fr = model_params.pop('EODf')
print("Example with cell:", cell)
step = 20
eod_fe = np.arange(0,eod_fr*5,step)
# generate EOD-like stimulus with an amplitude step:
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
a_fr = 1 # amplitude fish reciever
a_fe = 0.2 # amplitude fish emitter
#results = [[]]*
results = [[]] * 3
for e in range(len(eod_fe)):
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
time_fish_e = time * 2 * np.pi * eod_fe[e]
eod_fish_e = a_fe * np.sin(time_fish_e)
stimulus = eod_fish_e+eod_fish_r
# integrate the model:
spikes = simulate(stimulus, **model_params)
spikes_new = spikes[spikes > 1]
sampling_rate = 1/deltat
spikes_mat = np.zeros(int(spikes_new[-1] * sampling_rate) + 2)
spikes_idx = np.round((spikes_new) * sampling_rate)
for spike in spikes_idx:
spikes_mat[int(spike)] = 1
spikes_mat = spikes_mat*sampling_rate
window005 = 0.00005 * sampling_rate
window05 = 0.0005 * sampling_rate
window2 = 0.002 * sampling_rate
smoothened_spikes_mat005 = gaussian_filter(spikes_mat, sigma=window005)
smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05)
smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2)
nfft = 4096*2
array = [spikes_mat, smoothened_spikes_mat05,smoothened_spikes_mat2]
name = ['binary','05','2']
for i in range(len(array)):
results[i] = power_parameters(results[i], array[i], 1/deltat, nfft, name[i], eod_fr)
embed()
ax = {}
for i in range(len(results)):
ax[i] = plt.subplot(2,3,i+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['f'],color = 'red')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['f'],color = 'red')
ax[0].set_ylabel('[Hz]')
ax[i].set_ylim([0,eod_fr/2])
for i in range(len(results)):
ax[i+len(results)] = plt.subplot(2,3,i+len(results)+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['max'],color = 'steelblue')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['max'],color = 'blue')
ax[len(results)].set_ylabel('Modulation')
ax[len(results)+i].set_xlabel('EOD multiples')
plt.subplots_adjust(wspace = 0.3)
plt.savefig('modell_single_cell.pdf')
plt.savefig('../highbeats_pdf/cell_simulations/modell_single_cell'+cell+'.pdf')
plt.show()
embed()
# some analysis an dplotting:
#embed()
grid = gridspec.GridSpec(int(np.sqrt(len(parameters))), int(np.ceil(np.sqrt(len(parameters)))))
parameters = load_models("models.csv")
for i in range(4):
#for i in range(len(parameters)):
model_params = parameters[i]
print(cell)
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
# cut off first second of response
new_spikes = spikes[spikes >1]
freq,isis = calculate_isi_frequency(new_spikes, deltat)
#embed()
plt.subplot(grid[i])
plt.title('B:'+np.mean(freq))
plt.hist(isis, bins = 100, density = True)
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
freq_time = np.arange(spikes[0], spikes[-1], deltat)
fig, axs = plt.subplots(2, 1, sharex="col")
axs[0].plot(time, stimulus)
axs[0].set_title("Stimulus")
axs[0].set_ylabel("Amplitude in mV")
axs[1].plot(freq_time, freq)
axs[1].set_title("Model Frequency")
axs[1].set_ylabel("Frequency in Hz")
axs[1].set_xlabel("Time in s")
plt.show()
plt.close()
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

171
modell_single_cell_max.py Normal file
View File

@ -0,0 +1,171 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from model_max import simulate, load_models
import matplotlib.gridspec as gridspec
from plot_eod_chirp import power_parameters
from scipy.ndimage import gaussian_filter
"""
Dependencies:
numpy
matplotlib
numba (optional, speeds simulation up: pre-compiles functions to machine code)
"""
def main():
# tiny example program:
example_cell_idx = 11
# load model parameter:
parameters = load_models("models.csv")
model_params = parameters[example_cell_idx]
cell = model_params.pop('cell')
eod_fr = model_params.pop('EODf')
print("Example with cell:", cell)
step = 20
eod_fe = np.arange(0,eod_fr*5,step)
# generate EOD-like stimulus with an amplitude step:
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
a_fr = 1 # amplitude fish reciever
a_fe = 0.2 # amplitude fish emitter
#results = [[]]*
results = [[]] * 3
counter = 0
for e in range(len(eod_fe)):
time_fish_r = time * 2 * np.pi * eod_fr
eod_fish_r = a_fr * np.sin(time_fish_r)
time_fish_e = time * 2 * np.pi * eod_fe[e]
eod_fish_e = a_fe * np.sin(time_fish_e)
stimulus = eod_fish_e+eod_fish_r
# integrate the model:
spikes = simulate(stimulus, **model_params)
spikes_new = spikes[spikes > 1]
sampling_rate = 1/deltat
counter +=1
if len(spikes_new)>0:
spikes_mat = np.zeros(int(spikes_new[-1] * sampling_rate) + 2)
spikes_idx = np.round((spikes_new) * sampling_rate)
for spike in spikes_idx:
spikes_mat[int(spike)] = 1
spikes_mat = spikes_mat*sampling_rate
window005 = 0.00005 * sampling_rate
window05 = 0.0005 * sampling_rate
window2 = 0.002 * sampling_rate
smoothened_spikes_mat005 = gaussian_filter(spikes_mat, sigma=window005)
smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05)
smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2)
nfft = 4096*2
array = [spikes_mat, smoothened_spikes_mat05,smoothened_spikes_mat2]
name = ['binary','05','2']
for i in range(len(array)):
results[i] = power_parameters(results[i], array[i], 1/deltat, nfft, name[i], eod_fr)
else:
print(counter)
embed()
ax = {}
for i in range(len(results)):
ax[i] = plt.subplot(2,3,i+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['f'],color = 'red')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['f'],color = 'red')
ax[0].set_ylabel('[Hz]')
ax[i].set_ylim([0,eod_fr/2])
for i in range(len(results)):
ax[i+len(results)] = plt.subplot(2,3,i+len(results)+1)
plt.plot((eod_fe -eod_fr)/(eod_fr)+1,results[i]['max'],color = 'steelblue')
#plt.scatter((eod_fe - eod_fr) / (eod_fr) + 1, results[i]['max'],color = 'blue')
ax[len(results)].set_ylabel('Modulation')
ax[len(results)+i].set_xlabel('EOD multiples')
plt.subplots_adjust(wspace = 0.3)
plt.savefig('modell_single_cellmax.pdf')
plt.savefig('../highbeats_pdf/cell_simulations/modell_single_cell'+cell+'max.pdf')
plt.show()
embed()
# some analysis an dplotting:
#embed()
grid = gridspec.GridSpec(int(np.sqrt(len(parameters))), int(np.ceil(np.sqrt(len(parameters)))))
parameters = load_models("models.csv")
for i in range(4):
#for i in range(len(parameters)):
model_params = parameters[i]
print(cell)
cell = model_params.pop('cell')
EODf = model_params.pop('EODf')
# generate EOD-like stimulus
deltat = model_params["deltat"]
stimulus_length = 11.0 # in seconds
time = np.arange(0, stimulus_length, deltat)
# baseline EOD with amplitude 1:
stimulus = np.sin(2 * np.pi * EODf * time)
# das lasse ich eine sekunde integrieren dann weitere 10 sekunden integrieren und das nehmen
spikes = simulate(stimulus, **model_params)
# cut off first second of response
new_spikes = spikes[spikes >1]
freq,isis = calculate_isi_frequency(new_spikes, deltat)
#embed()
plt.subplot(grid[i])
plt.title('B:'+np.mean(freq))
plt.hist(isis, bins = 100, density = True)
plt.savefig('isi_model.pdf')
plt.savefig('../highbeats_pdf/isi_model.pdf')
plt.show()
freq_time = np.arange(spikes[0], spikes[-1], deltat)
fig, axs = plt.subplots(2, 1, sharex="col")
axs[0].plot(time, stimulus)
axs[0].set_title("Stimulus")
axs[0].set_ylabel("Amplitude in mV")
axs[1].plot(freq_time, freq)
axs[1].set_title("Model Frequency")
axs[1].set_ylabel("Frequency in Hz")
axs[1].set_xlabel("Time in s")
plt.show()
plt.close()
def calculate_isi_frequency(spikes, deltat):
"""
calculates inter-spike interval frequency
(wasn't tested a lot may give different length than time = np.arange(spikes[0], spikes[-1], deltat),
or raise an index error for some inputs)
:param spikes: spike time points
:param deltat: integration time step of the model
:return: the frequency trace:
starts at the time of first spike
ends at the time of the last spike.
"""
isis = np.diff(spikes)
freq_points = 1 / isis
freq = np.zeros(int((spikes[-1] - spikes[0]) / deltat))
current_idx = 0
for i, isi in enumerate(isis):
end_idx = int(current_idx + np.rint(isi / deltat))
freq[current_idx:end_idx] = freq_points[i]
current_idx = end_idx
return freq,isis
if __name__ == '__main__':
main()

766
myfunctions.py Normal file
View File

@ -0,0 +1,766 @@
import numpy as np
import matplotlib.pyplot as plt
from IPython import embed
from scipy import signal
import time
from scipy.stats import alpha
from scipy.stats import norm
from random import random
import os
import pandas as pd
def load_cell(set, fname = 'singlecellexample5', big_file = 'data_beat', new = False):
if (not os.path.exists(fname +'.pkl')) or (new == True):
data_all = pd.read_pickle(big_file +'.pkl')
d = data_all[data_all['dataset'] == set]
results = pd.DataFrame(d)
results.to_pickle(fname +'.pkl')
#np.save('singlecellexample5.npy', results)
else:
d = pd.read_pickle(fname +'.pkl')
return d
def default_settings(data,lw = 1,intermediate_width = 2.6*3,intermediate_length = 3, ts = 10, ls = 10, fs = 10):
inch_factor = 2.54
plt.rcParams['figure.figsize'] = (intermediate_width, intermediate_length)
plt.rcParams['font.size'] = fs
plt.rcParams['axes.titlesize'] = ts
plt.rcParams['axes.labelsize'] = ls
plt.rcParams['lines.linewidth'] = lw
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.loc'] = 'upper right'
plt.rcParams["legend.frameon"] = False
def remove_tick_marks(ax):
labels = [item.get_text() for item in ax.get_xticklabels()]
empty_string_labels = [''] * len(labels)
ax.set_xticklabels(empty_string_labels)
return ax
def remove_tick_ymarks(ax):
labels = [item.get_text() for item in ax.get_yticklabels()]
empty_string_labels = [''] * len(labels)
ax.set_yticklabels(empty_string_labels)
return ax
def euc_d(conv_beat,conv_chirp,a):
d_euclidean = np.sqrt(np.nansum((conv_beat - conv_chirp) ** 2, a))
return d_euclidean
def mean_euc_d(conv_beat,conv_chirp,a):
mean = np.sqrt(np.nanmean((conv_beat - conv_chirp) ** 2,a))
return mean
def ten_per_to_std(width):
deviation = width / 4.29
return deviation
def gauss(mult,sampling,width,x,conv_fact = 100):
# width in s
deviation = ten_per_to_std(width)
deviation = int(sampling * deviation)
gaussian_n = just_gauss(conv_fact,mult,deviation,x)
return gaussian_n
def just_gauss(conv_fact,mult, deviation,x, func = 'alpha',test = False):
points,to_cut = points_cut(deviation, mult,conv_fact)
if func == 'gaussian':
gaussian = signal.gaussian(points, std=deviation, sym=True)
elif func == 'exp':
gaussian = signal.exponential(points, 0, deviation, False)
if test == True:
check_alpha()
check_exponentials()
elif func == 'exp_self':
x_axis = np.arange(0,points, 1)
gaussian = np.exp(-x_axis / deviation)
elif func == 'alpha':
x_axis = np.arange(0, points, 1)
gaussian = x_axis* np.exp(-2.45 * x_axis / deviation)
gaussian_n = (gaussian*x) / np.sum(gaussian)
return gaussian_n
def points_cut(deviation, mult,conv_fact):
if deviation * 10 % 2:
points = deviation * mult
else:
points = deviation * mult - 1
to_cut = (int(points / 2))/conv_fact
return points,to_cut
def check_exponentials():
devs = [1, 5, 25, 50, 75, 100, 150, 250, 350, 500, 700]
for i in range(len(devs)):
if deviation * 10 % 2:
points = devs[i] * mult
else:
points = devs[i] * mult - 1
gaussian = signal.exponential(points, 0, devs[i], False)
plt.plot(gaussian)
plt.show()
def check_alpha():
conv_fact = 100
aa = [2, 3, 4, 5, 6, 7, 10]
for i in range(len(aa)):
a = aa[i]
# x = np.arange(alpha.ppf(0.01, a),
# alpha.ppf(0.99, a), 1/100000)
x = np.linspace(alpha.ppf(0.01, a),
alpha.ppf(0.99, a), 1000)
# alp = alpha.pdf(x, a)/(np.sum(alpha.pdf(x, a)))
# print(np.argmax(alp)/100)
alp = alpha.pdf(x, a)
print(alpha.ppf(0.90, a))
# plt.scatter(alpha.ppf(0.90, a),alpha.pdf(0.90, a))
plt.scatter(alpha.pdf(0.90, a), alpha.ppf(0.90, a))
# print(len(alp))
plt.plot(np.arange(0, 1000, 1) / conv_fact, alp, label=str(a))
plt.xlabel('ms')
#alpha_func = 1/(x**2*norm.cdf(a)*np.sqrt(2*np.pi))*np.exp(-0.5*(a-1/x)*2)
plt.legend()
plt.show()
rv = alpha(a)
x = np.linspace(0,10,points)
plt.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
gaussian = signal.gaussian(points, std=deviation, sym=True)
def shift_gauss(step_ms,sampling,C,gaussian):
G_all = []
step = int(step_ms*sampling/1000)
for i in np.arange(0, len(C) - len(gaussian)+1, step): # shift by 3 ms = 300 data points
G = np.zeros(len(C))
G[0 + i:len(gaussian) + i] = gaussian
G_all.append(G)
return G_all, step_ms
def shift_chirp(step_ms,sampling,C,gaussian):
step = int(step_ms * sampling / 1000)
a = np.arange(0, len(C) - len(gaussian)+1, step)
G_all = [[]]*len(a)
for i in a: # shift by 3 ms = 300 data points
G_all[int(i/step)]= C[0 + i:len(gaussian) + i]
#G_all.append(chirp)
G_all = np.asarray(G_all)
return G_all, step_ms
def shift_beat(step_ms,sampling,C,gaussian):
G_all = []
step = int(step_ms*sampling/1000)
for i in np.arange(0, len(C[0]) - len(gaussian)+1, step): # shift by 3 ms = 300 data points
chirp= C[:,0 + i:len(gaussian) + i]
G_all.append(chirp)
G_all = np.asarray(G_all)
return G_all, step_ms
def consp_create(type,G_all, C, B):
nom = np.dot(G_all, np.transpose(C * B))
if type == 'corr_p':
denom = np.transpose((np.transpose((np.sqrt(np.dot(G_all, np.transpose(np.array(B) * np.array(B))))))* np.transpose(((np.sqrt(np.nansum(G_all * C * C, axis=1)))))))
if type == 'corr_a':
denom = np.transpose((np.transpose(
(0.5 * np.dot(G_all, np.transpose(np.array(B) * np.array(B)))))
+ np.transpose(
((0.5 * np.nansum(G_all * C * C, axis=1))))))
index = nom/denom
#if denom.all() == 0 and nom.all() == 0:
# S_shift2 = np.ones(len(G_all))
#else:
S_shift2 = np.nanmax(index, axis=1)
ConSpi2 = 1 - np.abs(np.asarray(S_shift2))
all = 1-index
return ConSpi2,all
def consp_max(chir, step_ms, ConSpi2, left_c):
if chir == 'stim':
side = 17 / step_ms
I = ConSpi2[int(0.5 * len(ConSpi2)-side):int(0.5 * len(ConSpi2)+side)]#FIXME check if the middle is really the middle
if chir == 'chirp':
I = ConSpi2
Integral = np.sum(I)
max_position = np.argmax(ConSpi2) -left_c
max_value = np.nanmax(I)
return Integral, max_position, max_value
def remove_mean(chirp, beat):
C = chirp - np.mean(chirp)
B = np.transpose(np.transpose(beat) - np.mean(beat, axis = 1))
return C, B
def single_dist(left_c, beat_window, eod_fe, eod_fr, col, filter, beat, chirp, sampling, chir, plot = False, show = False,stages = 'two',test = False):
C, B = remove_mean(chirp, beat)
step_ms = 1
if stages == 'two':
C_all, step_ms = shift_chirp(step_ms, sampling, C, filter)
B_all, step_ms = shift_beat(step_ms, sampling, B, filter)
a, b, c = np.shape(B_all)
distances = [[]] * a
for i in range(a):
distances[i] = mean_euc_d(B_all[i], C_all[i], 1)
optimal_phase = np.nanmin(distances, axis=1)
max_value = np.nanmax(optimal_phase)
else:
C_all = C
B_all = B
a, b = np.shape(B_all)
distances = [[]] * a
for i in range(a):
distances[i] = mean_euc_d(B_all[i], C_all, None)
optimal_phase = np.nanmin(distances)
optimal_phase_position = np.nanargmin(distances)
max_value = optimal_phase
position = optimal_phase_position
if test == True:
for i in range(len(B_all)):
plt.title(str(i)+' '+str(distances[i]))
#plt.plot(B_all[0], color = 'red')
plt.plot(B_all[int(len(B_all)/2)], color='red')
plt.plot(B_all[i], color='blue')
plt.plot(C_all,color = 'green')
plt.show()
if plot:
plot_cons(filter, left_c, beat_window, optimal_phase, eod_fe, col, sampling, B, C, 1, chir)
if show:
plt.show()
return max_value,distances,position
def single_consp(type,left_c, beat_window, eod_fe, eod_fr, col, filter, beat, chirp, sampling, chir, plot = False, show = False):
C, B = remove_mean(chirp, beat)
step_ms = 1
G_all, step_ms = shift_gauss(step_ms, sampling, C, filter)
ConSpi2,all = consp_create(type,G_all, C, B)
Integral, max_position, max_value = consp_max(chir, step_ms, ConSpi2, left_c)
if plot:
plot_cons(filter, left_c, beat_window, ConSpi2, eod_fe, col, sampling, B, C, 0, chir)
if show:
plt.show()
return max_position, max_value, Integral,ConSpi2,all
def plot_cons(gaussian, left_c,beat_window,ConSpi2,beat,col, sampling, B, C, side, var):
fig = plt.figure(figsize=[4, 3])
ax = create_subpl(0.4, fig, 3, 3, 1, share2=True)
if var == 'chirp':
ax[0].plot((np.arange(0, len(ConSpi2), 1) - 0.5 * len(ConSpi2))+len(B[0])/(100*2) -left_c, ConSpi2, color=col)
ax[0].axvline(x = len(B[0])/(100*2)-3.5)
ax[0].plot(np.linspace(-left_c+(len(gaussian)/100)/2, len(B[0]) / 100 - left_c-(len(gaussian)/100)/2, len(ConSpi2)) , ConSpi2, color='red')
if var == 'stim':
ax[0].plot((np.arange(0, len(ConSpi2), 1) - 0.5 * len(ConSpi2)), ConSpi2, color=col)
ax[0].axvline(x=beat_window, color = 'red')
ax[0].axvline(x=-beat_window, color = 'red')
ax[1].axvline(x=-beat_window-13.5, color = 'red')
ax[1].axvline(x=+beat_window+13.5, color = 'red')
ax[1].axvline(x=0, color='red')
ax[0].set_title(beat)
ax[0].set_ylabel('Conspicousy [0-1]')
if var == 'stim':
ax[0].axvline(x=int(side), color=col)
ax[0].axvline(x=-int(side), color=col)
if var == 'chirp':
ax[0].axvline(x=int(side/2 +len(B[0]) / (100 * 2)-left_c), color=col)
ax[0].axvline(x=int(-side/2 +len(B[0]) / (100 * 2)-left_c), color=col)
ax[0].set_ylim([0, 1])
#if chirp == 'chirp':
# B = np.transpose(B)
if var == 'chirp':
ax[2].plot(np.linspace(-left_c,len(B[0])/100 -left_c,len(B[0])),
np.transpose(B[0:int(len(B)):1]))
if var == 'stim':
ax[2].plot(np.arange(-len(np.transpose(B[0:int(len(B)):1])) * 0.5 * 1000 / sampling,
len(np.transpose(B[0:int(len(B)):1])) * 0.5 * 1000 / sampling, 1000 / sampling),
np.transpose(B[0:int(len(B)):1]))
ax[2].set_title('10 shifted Beats')
ax[2].set_ylabel('AM')
if var == 'chirp':
ax[1].plot(np.linspace(-left_c,len(B[0])/100 -left_c,len(B[0])), C, color=col)
if var == 'stim':
ax[1].plot(np.arange(-len(np.transpose(B[0:int(len(B)):1])) * 0.5 * 1000 / sampling,
len(np.transpose(B[0:int(len(B)):1])) * 0.5 * 1000 / sampling, 1000 / sampling), C,
color=col)
ax[1].set_ylabel('AM')
ax[1].set_title('Chirp time window')
def mean_euc_d3(conv_beat,conv_chirp,a):
mean = np.sqrt((np.nansum((conv_beat - conv_chirp)/len(conv_beat) ** 2,a)))
return mean
def mean_euc_d2(conv_beat,conv_chirp,a):
mean = np.sqrt(np.nanmean((conv_beat - conv_chirp) ** 2,a))/len(conv_beat)
return mean
def frame_subplots(w, h, xlabel, ylabel,title):
fig = plt.figure(figsize=[w, h])
plt.axes(frameon=False)
plt.xticks([])
plt.yticks([])
plt.xlabel(xlabel,labelpad=30).set_visible(True)
plt.ylabel(ylabel, labelpad=50).set_visible(True)
ttl = plt.title(title)
ttl.set_position([.5, 1.05])
return fig
def create_subpl(h,fig, subplots,nrow, ncol, share2 = False):
ax = {}
if share2:
ax[0] = fig.add_subplot(nrow, ncol, 1)
for i in range(subplots):
if share2:
ax[i] = fig.add_subplot(nrow,ncol, i+1,sharex=ax[0])
else:
ax[i] = fig.add_subplot(nrow,ncol, i+1)
plt.subplots_adjust(hspace=h)
return ax
def windows_periods2(bef_c_t, aft_c_t,beat_window, min_bw, win, period, time_transform, period_shift, period_distance, perc_bef, perc_aft):
length = ((min_bw / time_transform) / (1 + perc_bef))* time_transform
#embed()
if length < bef_c_t+aft_c_t:
print('length in win2 is smaller as the chirp')
elif length > (bef_c_t+aft_c_t)*2.5:
length = (bef_c_t+aft_c_t)*2.5
for p in range(len(period)):
if win == 'w2':
#embed()
length_exp = 0.020
period_shift[p] = np.ceil(((length_exp+0.012)* time_transform)/period[p])*period[p] #0.12 is the length of the deviations i guess
#period_shift[p] = np.ceil((length_exp + 0.04 * time_transform) / period[p]) * period[
# p] # 0.12 is the length of the deviations i guess
#embed()
#period_shift[p] = 2*np.ceil((length_exp * time_transform) / period[p]) * period[p]
period_distance[p] = length_exp * time_transform
if (period_shift[p]+period_distance[p] *perc_bef)>beat_window[p]:
print('period'+str(p)+' uncool 1')
period_shift[p] = np.ceil((length_exp * time_transform) / period[p]) * period[p]
if (period_shift[p]+period_distance[p] *perc_bef) > beat_window[p]:
print('period' + str(p) + ' uncool 2')
period_shift[p] = 0.020 * time_transform
if win == 'w4':
period_shift[p] = length
period_distance[p] = length
period_left = period_distance *perc_bef
period_right = period_distance * perc_aft
#embed()
return period_shift,period_left, period_right
def auto_rows(data):
ncol = int(np.sqrt(len(data)))
nrow = int(np.sqrt(len(data)))
if ncol * nrow < len(data):
ncol = int(np.sqrt(len(data)))
nrow = int(np.sqrt(len(data)) + 1)
if ncol * nrow < len(data):
ncol = int(np.sqrt(len(data)) + 1)
nrow = int(np.sqrt(len(data)) + 1)
return nrow, ncol
def period_calc(beat_window, m, win, deviation_s, time_transform, beat_corr, bef_c, aft_c, chir, ver = '',conv_fact = 100,shuffled = ''):
period, bef_c_t, aft_c_t, deviation_t, period_shift, period_distance, perc_bef, perc_aft,nr_of_cycles,interval = pre_window(beat_corr,
time_transform,
deviation_s,
bef_c, aft_c, chir)
#if win == 'w1'or win == 'w3':
period_shift, period_left, period_right,exclude,consp_needed = windows_periods(ver,m, time_transform, win, beat_window,perc_bef, perc_aft, period_shift, period_distance, period, interval, nr_of_cycles, bef_c_t,
aft_c_t,shuffled = shuffled)
#if win == 'w2' or win == 'w4':
# period_shift, period_left, period_right = windows_periods2(bef_c_t, aft_c_t,beat_window, m, win, period, time_transform, period_shift, period_distance, perc_bef, perc_aft)
#if win == 'w3':
# period_shift,period_left, period_right = windows_periods3(beat_window,perc_bef, perc_aft, period_shift, period, interval, nr_of_cycles, bef_c_t, aft_c_t)
period_distance_c, period_distance_b, left_b, right_c, left_c, right_b, to_cut = post_window(conv_fact,period_left, period_right,
deviation_t, period_shift)
#embed()
#left_c = left_c+right_c+np.abs(right_b)
#right_c = right_c+right_c+np.abs(right_b)
#left_b_consp = left_b-consp_needed
dels = {}
dels['left_c'] = left_c
dels['right_c'] = right_c
dels['left_b'] = left_b
dels['right_b'] = right_b
dels['consp_needed'] = consp_needed
dels['to_cut'] = to_cut
return left_c, right_c, left_b, right_b,period_distance_c, period_distance_b,period_shift,period,to_cut,exclude,consp_needed,dels,interval
def choose_randomly(beat_window,interval,l_all = 400,r_all = 800):
#np.array(beat_window) * 2 - np.abs(bef_c_t) - np.abs(aft_c_t)
#left_c = np.zeros(len(beat_window))
#for i in range(len(beat_window)):
ra = random()
# print(ra)
# left_c[i] = ra * (np.array(beat_window[i]) * 2 - interval) - beat_window[i]
left_c = ra * (r_all + np.abs(l_all) - interval) - np.abs(l_all)
right_c = left_c + interval
return left_c, right_c
def windows_periods3(beat_window,perc_bef, perc_aft, period_shift, period, interval, nr_of_cycles, bef_c_t, aft_c_t):
period_left = np.zeros(len(period))
period_right = np.zeros(len(period))
for p in range(len(period)):
# & period[p] * perc_bef+
if (period[p] < interval) or ((period[p]+ period[p] * perc_bef)>beat_window[p]):
period_shift[p] = (period[p] * nr_of_cycles[p])
period_left[p] = bef_c_t
period_right[p] = aft_c_t
else:
period_left[p] = period[p] * perc_bef
period_right[p] = period[p] * perc_aft
period_shift[p] = period[p]
if (period[p]+ bef_c_t)>beat_window[p]:
period_shift[p] = (bef_c_t+aft_c_t)
period_left[p] = bef_c_t
period_right[p] = aft_c_t
return period_shift,period_left, period_right
def windows_periods(ver, min_bw, time_transform, win, beat_window,perc_bef, perc_aft,period_shift, period_distance, period, interval, nr_of_cycles, bef_c_t, aft_c_t, make_interval_shorter = False,closest_to_2m = True,shuffled = ''):
exclude = np.zeros(len(period))
exclude = [[]]*len(period)
consp_needed = [[]] * len(period)
interval = abs(aft_c_t) + abs(bef_c_t)
nr_of_cycles = ((interval) / period).astype(int) + 1 # nr of cycles in the length of the chirp
puffer = abs(bef_c_t) -4
if win == 'w1' or win == 'w2' or win == 'w3':
for p in range(len(period)):
if 'consp' in ver:
consp_needed[p] = period[p] * 0.5
consp_puffered = consp_needed[p] - puffer
if consp_puffered <0:
consp_puffered = 0
else:
consp_needed[p] = 0
consp_puffered = 0
mult_interval = period[p] * nr_of_cycles[p]
shorter_bw_aft = interval * perc_aft - 13.5
if period[p] < interval:
if period[p] == 0:
period_shift[p] = (interval) * 2
period_distance[p] = (interval)
exclude[p] = True
else:
if win == 'w1':
period_distance[p] = mult_interval
elif win == 'w3':
period_distance[p] = interval
elif win == 'w2':
period_distance[p] = interval
if closest_to_2m == True:
if ((mult_interval*2) <= beat_window[p]+shorter_bw_aft-period_distance[p]* perc_bef -consp_needed[p]) and (mult_interval * 2 >= period_distance[p] +consp_puffered):
period_shift[p] = mult_interval * 2
exclude[p] = True
else:
period_shift[p] = (((beat_window[p]+shorter_bw_aft- period_distance[p] * perc_bef-consp_needed[p]) / period[p]).astype(int)) * period[p]
exclude[p] = True
if period_shift[p] < mult_interval+consp_puffered:
if make_interval_shorter == False:
if ver == 'consp':
#embed()
exclude[p] = False
#period_shift[p] = []
elif ver == 'dist':
exclude[p] = False
#period_shift[p] = []
elif ver == 'conspdist':
consp_needed[p] = 0
if ((mult_interval * 2) <= beat_window[p]+shorter_bw_aft - period_distance[p] * perc_bef - consp_needed[p]) and (mult_interval * 2 >= period_distance[p] + consp_needed):
period_shift[p] = mult_interval * 2
exclude[p] = True
else:
period_shift[p] = (((beat_window[p]+shorter_bw_aft - period_distance[p] * perc_bef - consp_needed[p]) /
period[p]).astype(int)) * period[p]
if period_shift[p] < mult_interval + consp_needed[p]:
exclude[p] = False
#period_shift[p] = []
if make_interval_shorter == True:
short_interval = 5
#bef_c_t, aft_c_t, perc_bef, perc_aft = percentage(bef_c, aft_c, time_transform)
# FIXME a period distance adapt for w2 (and also for the backwards calculation
# FIXME in beat window adapt 0.8 times period plus minus
# FIXME look that it doesnt has to be 2 periods in long periods
shorter_bw_aft = (interval)*perc_aft-13.5
period_distance[p], period_shift[p] = exclude_interval(consp_needed[p],shorter_bw_aft, p,interval,short_interval,period, win, period_distance, period_shift, beat_window, perc_bef)
exclude[p] = True
if period_shift[p] <= mult_interval+consp_needed[p]:
short_interval = 7.5
#bef_c_t, aft_c_t, perc_bef, perc_aft = percentage(bef_c, aft_c, time_transform)
shorter_bw_aft = (interval) * perc_aft - 13.5
period_distance[p], period_shift[p] = exclude_interval(consp_needed[p],shorter_bw_aft, p,interval, short_interval, period, win,
period_distance, period_shift,
beat_window, perc_bef)
exclude[p] = True
if period_shift[p] < mult_interval+consp_needed[p]:
exclude[p] = False
#period_shift[p] = []
print('exclude')
else:
exclude[p] = True
elif closest_to_2m == False:
if (mult_interval * 2) <= beat_window[p] - period_distance[p] * perc_bef - consp_needed[p]:
period_shift[p] = mult_interval * 2
exclude[p] = True
if ((period[p] * nr_of_cycles[p] + np.round((12.5/period[p]))*period[p]+ period[p] * nr_of_cycles[p] * perc_bef)>beat_window[p]):
period_shift[p] = period[p] * nr_of_cycles[p]
else:
period_shift[p] = period[p] * nr_of_cycles[p] + np.round((12.5/period[p]))*period[p]
#elif ((period[p] + period[p] * perc_bef) > beat_window[p]+shorter_bw_aft):
# #embed()
# if (win == 'w3') or (win == 'w1'):
# exclude[p] = False
# print('should be excluded, not at least a period!!')
# else:
# period_distance[p] = interval
# if ((period[p] + period_distance[p] * perc_bef) > beat_window[p] + shorter_bw_aft):
# exclude[p] = False
# print('should be excluded, not at least a period!!')
# else:
# exclude[p] = True
# if ((bef_c_t + aft_c_t) * 2 + bef_c_t) > beat_window[p]:
# period_shift[p] = (interval)
# period_distance[p] = (interval)
# else:
# period_shift[p] = (interval) * 2
# period_distance[p] = (interval)
else:
if win == 'w1':
period_distance[p] = period[p]
elif win == 'w3':
period_distance[p] = period[p]
elif win == 'w2':
period_distance[p] = interval
if ((period[p]*2 + period_distance[p] * perc_bef) <= beat_window[p]-consp_needed[p]+shorter_bw_aft) and (period[p]*2 >= period_distance[p] + consp_puffered):
exclude[p] = True
period_shift[p] = period[p]*2
elif((period[p] + period_distance[p] * perc_bef) <= beat_window[p]-consp_needed[p]+shorter_bw_aft) and (period[p] >= period_distance[p] + consp_puffered):
exclude[p] = True
period_shift[p] = period[p]
else:
if ver == 'conspdist':
consp_needed[p] = 0
if ((period[p] * 2 + period_distance[p] * perc_bef) <= beat_window[
p] - consp_needed[p] + shorter_bw_aft) and (
period[p] * 2 >= period_distance[p] +consp_puffered):
exclude[p] = True
period_shift[p] = period[p] * 2
elif ((period[p] + period_distance[p] * perc_bef) <= beat_window[
p] - consp_needed[p] + shorter_bw_aft) and (period[p] >= period_distance[p] +consp_puffered):
exclude[p] = True
period_shift[p] = period[p]
else:
exclude[p] = False
#period_shift[p] = []
else:
exclude[p] = False
#period_shift[p] = []
#elif ((period[p] + period_distance[p] * perc_bef) < beat_window[p]+shorter_bw_aft):
# if ver == 'consp':
# exclude[p] = True
# else:
# exclude[p] = False
# period_shift[p] = period[p]
#if period_shift[p]<period_distance[p]:
#embed()
#if (period_distance[p]*perc_bef +period_shift[p])>beat_window[p]:
# print('error')
#if win == 'w2':
# period_distance[p] = interval
#embed()
#break
#if win == 'w2' or win == 'w4':
# for p in range(len(period)):
# if win == 'w2':
# #embed()
# length_exp = 0.020
# period_shift[p] = np.ceil(((length_exp+0.012)* time_transform)/period[p])*period[p] #0.12 is the length of the deviations i guess
# #period_shift[p] = np.ceil((length_exp + 0.04 * time_transform) / period[p]) * period[
# # p] # 0.12 is the length of the deviations i guess
# #embed()
# #period_shift[p] = 2*np.ceil((length_exp * time_transform) / period[p]) * period[p]
# period_distance[p] = length_exp * time_transform
# if (period_shift[p]+period_distance[p] *perc_bef)>beat_window[p]:
# print('period'+str(p)+' uncool 1')
# period_shift[p] = np.ceil((length_exp * time_transform) / period[p]) * period[p]
# if (period_shift[p]+period_distance[p] *perc_bef) > beat_window[p]:
# print('period' + str(p) + ' uncool 2')
# period_shift[p] = 0.020 * time_transform
elif win == 'w4':
for p in range(len(period)):
if interval*perc_bef+interval*2<=beat_window[p]:
period_shift[p] = interval*2
period_distance[p] = interval
exclude[p] = True
# FIXME here das mit den consp noch reinmachen
elif interval*perc_bef+interval<=beat_window[p]:
period_shift[p] = interval
period_distance[p] = interval
exclude[p] = True
else:
exclude[p] = False
#period_shift[p] = []
#length = ((min_bw / time_transform) / (1 + perc_bef))* time_transform
#embed()
#exclude[p] = False
#if length < bef_c_t+aft_c_t:
# exclude[p] = True
# print('length in win2 is smaller as the chirp')
#elif length > (bef_c_t+aft_c_t)*2.5:
# exclude[p] = False
# length = (bef_c_t+aft_c_t)*2.5
#else:
# exclude[p] = False
#period_shift[p] = length*2
#period_distance[p] = length
period_left = period_distance *perc_bef
period_right = period_distance * perc_aft
#embed()
return period_shift, period_left, period_right,exclude,consp_needed
def exclude_interval(consp_needed,bw, p, interval, short_interval, period, win, period_distance, period_shift, beat_window, perc_bef):
nr_of_cycles = ((interval - short_interval) / period).astype(int) + 1
interval = interval - short_interval
if win == 'w1':
period_distance[p] = period[p] * nr_of_cycles[p]
elif win == 'w3':
period_distance[p] = interval
elif win == 'w2':
period_distance[p] = interval
period_shift[p] = (((beat_window[p]+bw - period[p] * nr_of_cycles[p] * perc_bef -consp_needed) / period[
p]).astype(int)) * period[p]
return period_distance[p], period_shift[p]
def post_window(conv_fact,period_left, period_right, deviation_t, period_shift):
points, to_cut = points_cut(deviation_t*conv_fact,10,conv_fact)
#to_cut = 5 * deviation_t
#embed()
left_c = np.round((-period_left- to_cut)*conv_fact)/conv_fact
right_c = np.round((period_right+ to_cut)*conv_fact)/conv_fact
left_b = np.round((-period_left - to_cut -period_shift)*conv_fact)/conv_fact
right_b = np.round((period_right+ to_cut -period_shift)*conv_fact)/conv_fact
period_distance_c = abs(right_c - left_c)-2*to_cut
period_distance_b = abs(right_b - left_b)-2*to_cut
return period_distance_c, period_distance_b, left_b, right_c, left_c, right_b,to_cut
def one_dict_str(beats, spike_phases):
AUCI = {}
for b in range(len(beats)):
AUCI[beats[b]] = [[]] * len(spike_phases[beats[b]])
return AUCI
def one_array_str(beats, spike_phases):
AUCI = [[]]*len(beats)
for b in range(len(beats)):
AUCI[b] = [[]] * len(spike_phases[beats[b]])
return AUCI
def only_one_dict_str(beats, spike_phases):
AUCI = {}
for b in range(len(beats)):
AUCI[beats[b]] = [[]] * len(spike_phases)
return AUCI
def three_dict_str(deviation_list, data):
AUCI = {}
#embed()
for dev in range(len(deviation_list)):
AUCI[deviation_list[dev]] = {}
for d in range(len(data)):
AUCI[deviation_list[dev]][data[d]] = {}
return AUCI
def four_dict_str(deviation_list, data, beats, bin_array):
AUCI = {}
for dev in range(len(deviation_list)):
AUCI[deviation_list[dev]] = {}
for d in range(len(data)):
AUCI[deviation_list[dev]][data[d]] = [[]]*len(beats[d])
for b in range(len(beats[d])):
AUCI[deviation_list[dev]][data[d]][b] = [[]]*len(bin_array)
for p in range(len(bin_array)):
AUCI[deviation_list[dev]][data[d]][b][p] = []
return AUCI
def pre_window(beat_corr, time_transform,deviation_s,bef_c,aft_c,chir):
period = period_func(beat_corr, time_transform, chir)
deviation_t = deviation_s * time_transform
period_shift = np.zeros(len(period))
period_distance = np.zeros(len(period))
exclude = np.zeros(len(period))
bef_c_t, aft_c_t, perc_bef, perc_aft = percentage(bef_c, aft_c, time_transform)
nr_of_cycles = ((bef_c_t + aft_c_t) / period).astype(int) + 1 # nr of cycles in the length of the chirp
interval = abs(aft_c_t) + abs(bef_c_t)
#embed()
return period, bef_c_t, aft_c_t, deviation_t, period_shift, period_distance, perc_bef, perc_aft,nr_of_cycles,interval
def percentage(bef_c, aft_c, time_transform):
bef_c_t = bef_c * time_transform
aft_c_t = aft_c * time_transform
perc_bef = (np.abs(bef_c_t) / (np.abs(bef_c_t) + np.abs(aft_c_t)))
perc_aft = (np.abs(aft_c_t) / (np.abs(bef_c_t) + np.abs(aft_c_t)))
return bef_c_t, aft_c_t, perc_bef, perc_aft
def period_func(beat_corr,time_transform, chir):
if chir == 'chir':
period = np.abs(1 / beat_corr) * time_transform
if chir == 'stim':
period = (np.round(np.abs(1 / beat_corr) * time_transform)).astype(int)
period[period == float('inf')] = np.max(period[period != np.inf])
return period
def two_ylabels(y_pad,x,y,z,fig,left, right):
ax1 = fig.add_subplot(x, y, z)
ax1.set_xticks([])
ax1.set_yticks([])
ax0 = ax1.twinx()
ax0.set_xticks([])
ax0.set_yticks([])
ax1.set_ylabel(left, labelpad = y_pad)
ax0.set_ylabel(right, rotation = 270,labelpad = 40)
def del_ticks(data, ncol, ax):
for d in range(len(data) - ncol):
ax[d].set_xticks([])
a = np.arange(0, len(data), 1)
deleted = np.delete(a, (np.arange(0, len(data), ncol)))
for d in deleted:
ax[d].set_yticks([])
def axvline_everywhere(ax,subplots,eod_fe,eod_fr):
for e in range(subplots):
for i in range(int(np.max(eod_fe) / np.max(eod_fr))):
ax[e].axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-')

163
numerical.py Normal file
View File

@ -0,0 +1,163 @@
from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
fig, ax = plt.subplots(nrows=len(results), ncols=2, sharex=True)
all_max = [[]] * len(results)
for i in range(len(results)):
ax[i, 0].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
ax[i, 0].plot(beats / eod_fr + 1, np.array(results['result_frequency' ][i]) / eod_fr + 1, color=colors[i])
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i,0].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.25)
ii, jj = np.shape(ax)
ax[0, 0].set_title('Most popular frequency')
ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in range(ii):
for j in range(jj):
ax[-1, j].set_xlabel('EOD multiples')
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.savefig('numerical.pdf')
plt.show()
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
eod_fr = 637# eod fish reciever
eod_fr = 537
eod_fr = 1435
eod_fr = 734
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
sampling = 112345
#sampling = [83425,98232,100000,112683]
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
start = 5
end = 3500
step = 25
#step = [10,25, 30]
eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr)
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = 0.5
bef_c = -2
aft_c = -1
load = True
if (load == True) or (not os.path.exists('numerical.pkl')):
results = power_func(a_fr = a_fr, a_fe = a_fe, eod_fr = eod_fr, eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling, deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results = pd.DataFrame(results)
results.to_pickle('numerical.pkl')
np.save('numerical.npy', results)
else:
results = pd.read_pickle('numerical.pkl')
#embed()
plot_power(beats, results,'w2', deviation_s, sigma, sampling, deviation_ms, beat_corr, size, [phase_zero[0]], delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True,bef_c = bef_c,aft_c =aft_c)

223
numerical_compar.py Normal file
View File

@ -0,0 +1,223 @@
from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
from thunderfish.tabledata import TableData
import string
from plotstyle import plot_style, spines_params
from myfunctions import remove_tick_ymarks
def plot_beats(ax, f0, freqs, fexpected, ffirst, fmax, title, ylabel = 'yes'):
freqs /= f0
sel = np.abs(freqs - ((freqs+0.1)//0.5)*0.5) < 0.01
ax.set_title(title)
ax.plot(freqs, fexpected/f0, color = 'black', linestyle = 'dashed',linewidth = 0.8)
if ffirst is not None:
ffirst[sel] = np.nan
ax.plot(freqs, ffirst/f0, color = 'gold')
if fmax is not None:
fmax[sel] = np.nan
ax.plot(freqs, fmax/f0, color = 'orange')
ax.set_xlim(0, 5)
ax.set_ylim(0, 0.7)
ax.set_xticks_delta(1.0)
#ax.set_yticks_delta(0.2)
#ax.set_xlabel('Frequency [f0]')
if ylabel == 'yes':
ax.set_ylabel('Frequency [f0]')
else:
ax = remove_tick_ymarks(ax)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
#colors = ['brown']*len(name)
default_settings([0], intermediate_width=6.29, intermediate_length=8, ts=10, ls=9, fs=9)
all_max = [[]] * len(results)
first = ['hilbert>first_f','threshold>first_f','rectified>first_f','square>first_f','threshold cubed>first_f']
max = ['hilbert>max_f', 'threshold>max_f', 'rectified>max_f', 'square>max_f', 'threshold cubed>max_f']
name = ['Hilbert','Thresholded','Rectified','Squaring','Threshold cubed']
colors = ['brown'] * len(name)
fig, ax = plt.subplots(nrows=len(name), ncols=2)
nrs1 = [0,2,4,6,8]
nrs2 = [1,3,5,7,9]
for i in range(len(name)):
data = TableData('highbeatspecs10.dat')
#ax['analytic_threhold'] = plt.subplot(axis[0])
nr_size = 11
left = -0.1
ax[i, 0].text(left, 1.1, string.ascii_uppercase[nrs1[i]], transform=ax[i, 0].transAxes,
size=nr_size, weight='bold')
f0 = data[data[:, 'freq>norm'] == 1.0, 'freq>freq']
f0 = f0[len(f0) // 2]
plot_beats(ax[i, 0], f0, data[:, 'freq>freq'], data[:, 'freq>expected'],
data[:,first[i]], data[:, max[i]], name[i]+' analytic')
if i != (len(name)-1):
ax[i, 1] = remove_tick_marks(ax[i, 1])
ax[i, 0] = remove_tick_marks(ax[i, 0])
#ax[i, 1].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
ax[i, 1].plot(beats / eod_fr +1, np.array(results['result_frequency' ][i]) / eod_fr, color=colors[i])
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i, 1].set_title(name[i]+' numerical')
ax[i, 1].set_xticks_delta(1.0)
ax[i, 1].plot(data[:, 'freq>freq']/ f0, data[:, 'freq>expected'] / f0, color='black', linestyle='dashed',linewidth = 0.8)
ax[i, 1].set_xlim(0, 5)
ax[i, 1].set_ylim(0, 0.7)
ax[i, 1] = remove_tick_ymarks(ax[i, 1])
ax[i, 1].text(left, 1.1, string.ascii_uppercase[nrs2[i]], transform=ax[i, 1].transAxes,
size=nr_size, weight='bold')
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
#ax[i,1].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.13, hspace = 0.45)
ii, jj = np.shape(ax)
#ax[0, 0].set_title('Most popular frequency')
#ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in range(ii):
for j in range(jj):
ax[-1, j].set_xlabel('EOD multiples')
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.savefig('numerical_compar.pdf')
plt.show()
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
eod_fr = 637# eod fish reciever
eod_fr = 537
eod_fr = 1435
eod_fr = 734
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
sampling = 112345
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
start = 5
end = 3500
step = 25
eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr)
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = 0.5
bef_c = -2
aft_c = -1
load = False
restrict = np.array([0,2,3,4,5])
if (load == True) or (not os.path.exists('numerical.pkl')):
results = power_func(restrict = restrict, a_fr = a_fr, a_fe = a_fe, eod_fr = eod_fr, eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling, deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results = pd.DataFrame(results)
results.to_pickle('numerical.pkl')
np.save('numerical.npy', results)
else:
results = pd.read_pickle('numerical.pkl')
#embed()
plot_power(beats, results,'w2', deviation_s, sigma, sampling, deviation_ms, beat_corr, size, [phase_zero[0]], delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True,bef_c = bef_c,aft_c =aft_c)

251
numerical_compar_both.py Normal file
View File

@ -0,0 +1,251 @@
from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
from thunderfish.tabledata import TableData
import string
from plotstyle import plot_style, spines_params
from myfunctions import remove_tick_ymarks
from matplotlib import gridspec
def plot_beats(ax, f0, freqs, fexpected, ffirst, fmax, title, ylabel = 'yes'):
freqs /= f0
sel = np.abs(freqs - ((freqs+0.1)//0.5)*0.5) < 0.01
ax.set_title(title)
ax.plot(freqs, fexpected/f0, color = 'black', linestyle = 'dashed',linewidth = 0.8)
if ffirst is not None:
ffirst[sel] = np.nan
ax.plot(freqs, ffirst/f0, color = 'gold')
if fmax is not None:
fmax[sel] = np.nan
ax.plot(freqs, fmax/f0, color = 'orange')
ax.set_xlim(0, 5)
ax.set_ylim(0, 0.7)
ax.set_xticks_delta(1.0)
#ax.set_yticks_delta(0.2)
#ax.set_xlabel('Frequency [f0]')
if ylabel == 'yes':
ax.set_ylabel('EOD mult.')
else:
ax = remove_tick_ymarks(ax)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
#colors = ['brown']*len(name)
default_settings([0], intermediate_width=6.29, intermediate_length=8, ts=10, ls=9, fs=9)
all_max = [[]] * len(results)
first = ['hilbert>first_f','threshold>first_f','rectified>first_f','square>first_f','threshold cubed>first_f']
max = ['hilbert>max_f', 'threshold>max_f', 'rectified>max_f', 'square>max_f', 'threshold cubed>max_f']
maxa = ['hilbert>max_a', 'threshold>max_a', 'rectified>max_a', 'square>max_a', 'threshold cubed>max_a']
name = ['Hilbert','Thresholded','Rectified','Squaring','Threshold cubed']
colors = ['brown'] * len(name)
#fig, ax = plt.subplots(nrows=len(name), ncols=2)
ax = {}
grid0 = gridspec.GridSpec(len(name), 1)
nrs1 = [0,2,4,6,8]
nrs2 = [1,3,5,7,9]
for i in range(len(name)):
data = TableData('highbeatspecs10.dat')
#ax['analytic_threhold'] = plt.subplot(axis[0])
nr_size = 11
left = -0.1
grid = gridspec.GridSpecFromSubplotSpec(2,2,subplot_spec=grid0[i])
#embed()
ax[0] = plt.subplot(grid[0])
ax[0].text(left, 1.1, string.ascii_uppercase[nrs1[i]], transform=ax[0].transAxes,
size=nr_size, weight='bold')
f0 = data[data[:, 'freq>norm'] == 1.0, 'freq>freq']
f0 = f0[len(f0) // 2]
plot_beats(ax[0], f0, data[:, 'freq>freq'], data[:, 'freq>expected'],
data[:,first[i]], data[:, max[i]], name[i]+' analytic')
ax[1] = plt.subplot(grid[1])
#ax[i, 1].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
ax[1].plot(beats / eod_fr +1, np.array(results['result_frequency' ][i]) / eod_fr, color=colors[i])
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[1].set_title(name[i]+' numerical')
ax[1].set_xticks_delta(1.0)
ax[1].plot(data[:, 'freq>freq']/ f0, data[:, 'freq>expected'] / f0, color='black', linestyle='dashed',linewidth = 0.8)
ax[1].set_xlim(0, 5)
ax[1].set_ylim(0, 0.7)
ax[1] = remove_tick_ymarks(ax[1])
ax[1].text(left, 1.1, string.ascii_uppercase[nrs2[i]], transform=ax[1].transAxes,
size=nr_size, weight='bold')
ax[2] = plt.subplot(grid[2])
#embed()
ax[2].plot(data[:, 'freq>freq'] / f0, (data[:, maxa[i]]-np.nanmin(data[:, maxa[i]]))/np.nanmax((data[:, maxa[i]]-np.nanmin(data[:, maxa[i]]))), color='blue')
ax[2].set_ylabel('r. amp.')
ax[3] = plt.subplot(grid[3])
ax[3].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i])/(np.max(results['result_amplitude_max'][i])), color='navy')
ax[3].set_xlim(0, 5)
ax[3] = remove_tick_ymarks(ax[3])
ax[2].set_xlim(0, 5)
ax[1] = remove_tick_marks(ax[1])
ax[0] = remove_tick_marks(ax[0])
if i != (len(name)-1):
ax[2] = remove_tick_marks(ax[2])
ax[3] = remove_tick_marks(ax[3])
for i in range(4):
ax[i].spines['right'].set_visible(False)
ax[i].spines['top'].set_visible(False)
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
#ax[i,1].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
ax[3].set_xlabel('EOD multiples')
ax[2].set_xlabel('EOD multiples')
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.13, hspace = 0.45, top = 0.94, right = 0.97)
#ii, jj = np.shape(ax)
#ax[0, 0].set_title('Most popular frequency')
#ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
#for i in range(ii):#
#
# for j in range(jj):
# ax[-1, j].set_xlabel('EOD multiples')
# ax[i, j].spines['right'].set_visible(False)
# ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.savefig('numerical_compar_both.pdf')
plt.show()
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
eod_fr = 637# eod fish reciever
eod_fr = 537
eod_fr = 1435
eod_fr = 734
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
sampling = 112345
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
start = 5
end = 3500
step = 25
eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr)
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = 0.5
bef_c = -2
aft_c = -1
load = False
restrict = np.array([0,2,3,4,5])
if (load == True) or (not os.path.exists('numerical.pkl')):
results = power_func(restrict = restrict, a_fr = a_fr, a_fe = a_fe, eod_fr = eod_fr, eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling, deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results = pd.DataFrame(results)
results.to_pickle('numerical.pkl')
np.save('numerical.npy', results)
else:
results = pd.read_pickle('numerical.pkl')
#embed()
plot_power(beats, results,'w2', deviation_s, sigma, sampling, deviation_ms, beat_corr, size, [phase_zero[0]], delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True,bef_c = bef_c,aft_c =aft_c)

View File

@ -0,0 +1,229 @@
from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
from thunderfish.tabledata import TableData
import string
from plotstyle import plot_style, spines_params
from myfunctions import remove_tick_ymarks
def plot_beats(ax, f0, freqs, fexpected, ffirst, fmax, title, ylabel = 'yes'):
freqs /= f0
sel = np.abs(freqs - ((freqs+0.1)//0.5)*0.5) < 0.01
ax.set_title(title)
ax.plot(freqs, fexpected/f0, color = 'black', linestyle = 'dashed',linewidth = 0.8)
if ffirst is not None:
ffirst[sel] = np.nan
ax.plot(freqs, ffirst/f0, color = 'gold')
if fmax is not None:
fmax[sel] = np.nan
ax.plot(freqs, fmax/f0, color = 'orange')
ax.set_xlim(0, 5)
ax.set_ylim(0, 0.7)
ax.set_xticks_delta(1.0)
#ax.set_yticks_delta(0.2)
#ax.set_xlabel('Frequency [f0]')
if ylabel == 'yes':
ax.set_ylabel('Frequency [f0]')
else:
ax = remove_tick_ymarks(ax)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
#colors = ['brown']*len(name)
default_settings([0], intermediate_width=6.29, intermediate_length=8, ts=10, ls=9, fs=9)
all_max = [[]] * len(results)
#'threshold>max_a'
first = ['hilbert>first_f','threshold>first_f','rectified>first_f','square>first_f','threshold cubed>first_f']
max = ['hilbert>max_a', 'threshold>max_a', 'rectified>max_a', 'square>max_a', 'threshold cubed>max_a']
name = ['Hilbert','Thresholded','Rectified','Squaring','Threshold cubed']
colors = ['brown'] * len(name)
fig, ax = plt.subplots(nrows=len(name), ncols=2)
nrs1 = [0,2,4,6,8]
nrs2 = [1,3,5,7,9]
for i in range(len(name)):
data = TableData('highbeatspecs10.dat')
#ax['analytic_threhold'] = plt.subplot(axis[0])
nr_size = 11
left = -0.1
ax[i, 0].text(left, 1.1, string.ascii_uppercase[nrs1[i]], transform=ax[i, 0].transAxes,
size=nr_size, weight='bold')
f0 = data[data[:, 'freq>norm'] == 1.0, 'freq>freq']
f0 = f0[len(f0) // 2]
#plot_beats(ax[i, 0], f0, data[:, 'freq>freq'], data[:, 'freq>expected'],
# data[:,first[i]], data[:, max[i]], name[i]+' analytic')
ax[i, 0].plot(data[:, 'freq>freq'] / f0, data[:, max[i]], color='blue')
if i != (len(name)-1):
ax[i, 1] = remove_tick_marks(ax[i, 1])
ax[i, 0] = remove_tick_marks(ax[i, 0])
#ax[i, 1].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
#embed()
ax[i, 1].plot(beats / eod_fr +1, np.array(results['result_amplitude_max' ][i]) / eod_fr, color = 'navy')
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i, 0].set_title(name[i] + ' analytical')
ax[i, 1].set_title(name[i]+' numerical')
ax[i, 1].set_xticks_delta(1.0)
ax[i, 0].set_xticks_delta(1.0)
# ax[i, 1].plot(data[:, 'freq>freq']/ f0, data[:, 'freq>expected'] / f0, color='black', linestyle='dashed',linewidth = 0.8)
ax[i, 1].set_xlim(0, 5)
ax[i, 0].set_xlim(0, 5)
#ax[i, 1].set_ylim(0, 0.7)
#ax[i, 1] = remove_tick_ymarks(ax[i, 1])
ax[i, 1].text(left, 1.1, string.ascii_uppercase[nrs2[i]], transform=ax[i, 1].transAxes,
size=nr_size, weight='bold')
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
#ax[i,1].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.13, hspace = 0.45)
ii, jj = np.shape(ax)
#ax[0, 0].set_title('Most popular frequency')
#ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in range(ii):
for j in range(jj):
ax[-1, j].set_xlabel('EOD multiples')
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.savefig('numerical_compar_modulation.pdf')
plt.show()
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
eod_fr = 637# eod fish reciever
eod_fr = 537
eod_fr = 1435
eod_fr = 734
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
sampling = 112345
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
start = 5
end = 3500
step = 25
eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr)
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = 0.5
bef_c = -2
aft_c = -1
load = False
restrict = np.array([0,2,3,4,5])
if (load == True) or (not os.path.exists('numerical.pkl')):
results = power_func(restrict = restrict, a_fr = a_fr, a_fe = a_fe, eod_fr = eod_fr, eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling, deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results = pd.DataFrame(results)
results.to_pickle('numerical.pkl')
np.save('numerical.npy', results)
else:
results = pd.read_pickle('numerical.pkl')
#embed()
plot_power(beats, results,'w2', deviation_s, sigma, sampling, deviation_ms, beat_corr, size, [phase_zero[0]], delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True,bef_c = bef_c,aft_c =aft_c)

304
numerical_test.py Normal file
View File

@ -0,0 +1,304 @@
#from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
#from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
fig, ax = plt.subplots(nrows=len(results), ncols=2, sharex=True)
all_max = [[]] * len(results)
for i in range(len(results)):
ax[i, 0].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
ax[i, 0].plot(beats / eod_fr + 1, np.array(results['result_frequency' ][i]) / eod_fr + 1, color=colors[i])
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i,0].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.25)
ii, jj = np.shape(ax)
ax[0, 0].set_title('Most popular frequency')
ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in range(ii):
for j in range(jj):
ax[-1, j].set_xlabel('EOD multiples')
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
return fig
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
#eod_fr = 637# eod fish reciever
#eod_fr = 537
#eod_fr = 1435
load = False
counter = 0
results = []
results = pd.DataFrame(results)
if load == True:
#results = []
eod_fr = [500,734,820,1000,1492]
#eod_fr = [500]
for ee in range(len(eod_fr)):
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
#sampling = 112345
sampling = [83425,98232,100000,112683]
#sampling = [8425, 9232]
for s in range(len(sampling)):
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling[s])
start = 5
end = 3500
# step = 25
step = [30, 25, 10]
#step = [250]
for ss in range(len(step)):
eod_fe, beat_corr, beats = find_beats(start,end,step[ss],eod_fr[ee])
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = [0.5, 0.3, 0.2]
#a_fe = [0.5]
for a in range(len(a_fe)):
bef_c = -2
aft_c = -1
load = True
#if counter != 0:
# results = results1*1
results1 = power_func(a_fr = a_fr, a_fe = a_fe[a], eod_fr = eod_fr[ee], eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling[s], deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results1['sampling'] = sampling[s]
results1['eod_fr'] = eod_fr[ee]
results1['amplitude'] = a_fe[a]
results1['step'] = step[ss]
#if counter == 0:
# results = results1
#elif counter != 0:
results = results.append(results1,ignore_index = True)
#if counter != 0:
# results = results.append(results1)
counter += 1
#embed()
#for i in range
#embed()
#embed()
results = pd.DataFrame(results)
results.reset_index(drop=True)
results.to_pickle('numerical_test.pkl')
np.save('numerical_test.npy', results)
else:
results = pd.read_pickle('numerical_test.pkl')
results = results.reset_index(drop=True)
present_type = np.unique(results.type)
#present_type = list(present_type).pop('samped threshold')
error = {}
#error = pd.DataFrame(error)
for i in range(len(present_type)):
#data_baseline[data_baseline['dataset'] == set]
if present_type[i] != 'samped threshold':
result = results[np.array(results['type'] == present_type[i])]
#result['result_frequency']
#error[present_type[i]] =np.empty((4,7))
#error[present_type[i]] = np.NaN
error[present_type[i]] = []
step_all = np.unique(results['step'])
eod_fr_all = np.unique(results['eod_fr'])
sampling_all = np.unique(results['sampling'])
a_fe_all = np.unique(results['amplitude'])
for r in range(len(result)):
step = result.iloc[r]['step']
eod_fr = result.iloc[r]['eod_fr']
sampling = result.iloc[r]['sampling']
eod_fr = result.iloc[r]['eod_fr']
start = 5
end = 3500
# step = 25
#step = [30, 25, 10]
# step = [250]
#for ss in range(len(step)):
eod_fe, beat_corr, beats = find_beats(start, end, step, eod_fr)
error[present_type[i]].append(np.mean((beat_corr-result.iloc[r]['result_frequency'])**2))
#plt.title(present_type[i])
#plt.plot(beat_corr)
#plt.plot(result.iloc[r]['result_frequency'])
#plt.show()
embed()
err = {}
for i in range(len(error)):
if present_type[i] != 'samped threshold':
err[present_type[i]] = np.mean(error[present_type[i]])
plt.bar(np.array(list(err.keys())),np.array(list(err.values())))#np.arange(0,len(err),1)
plt.savefig('..bars')
values = [error[k] for k in error]
maximum = [[]]*len(error)
minimum = [[]]*len(error)
for i in range(len(error)):
if present_type[i] != 'samped threshold':
maximum[i] = np.max(error[present_type[i]])
minimum[i] = np.min(error[present_type[i]])
for e in range(len(error)):
if present_type[e] != 'samped threshold':
plt.subplot(3,4,e+1)
plt.title(present_type[e])
plt.imshow(np.array(error[present_type[e]]).reshape(-1,int(np.sqrt(len(error[present_type[e]])))), vmin = np.nanmin(minimum), vmax = np.nanmax(maximum))#
plt.colorbar()
plt.show()
embed()
#for i in range(len(results)):
#results = []
eod_fr = [500,639,734,820,952,1000,1492]
for ee in range(len(eod_fr)):
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
#sampling = 112345
sampling = [83425,98232,100000,112683]
for s in range(len(sampling)):
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling[s])
start = 5
end = 3500
# step = 25
step = [30, 25, 10]
for ss in range(len(step)):
eod_fe, beat_corr, beats = find_beats(start,end,step[ss],eod_fr[ee])
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = [0.5, 0.3, 0.2]
for a in range(len(a_fe)):
res_amp = results[np.array(results['amplitude'] == a_fe[a])]
res_samp = res_amp[np.array(res_amp['sampling'] == sampling[s])]
res_step = res_samp[np.array(res_samp['step'] == step[ss])]
res_eod = res_step[np.array(res_step['eod_fr'] == eod_fr[ee])]
#res_eod = res_eod[np.array(res_step['result frequency'] == eod_fr[ee])]
res = res_eod.reset_index(drop=True)
try:
fig = plot_power(beats, res,'w2', deviation_s, sigma, sampling[s], deviation_ms, beat_corr, size, [phase_zero[0]], delta_t, a_fr, a_fe[a], eod_fr[ee], eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True)#bef_c = bef_c,aft_c =aft_c
plt.suptitle('sampling '+str(sampling[s])+ 'step '+str(step[ss])+'eod_fr '+str(eod_fr[ee])+'amplitude '+str(a_fe[a]))
plt.savefig('../highbeats_pdf/numerical_test/sampling'+str(sampling[s])+ 'step'+str(step[ss])+'eod_fr'+str(eod_fr[ee])+'amplitude'+str(a_fe[a])+'.pdf')
plt.savefig('../highbeats_pdf/numerical_test/sampling' + str(sampling[s]) + 'step' + str(
step[ss]) + 'eod_fr' + str(eod_fr[ee]) + 'amplitude' + str(a_fe[a]) + '.png')
except:
a = 2
#plt.show()
#results = pd.read_pickle('numerical_test.pkl')
# embed()

249
numerical_test_bar.py Normal file
View File

@ -0,0 +1,249 @@
#from matplotlib.colors import LinearSegmentedColormap
#from plot_eod_chirp import find_times
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
#embed()
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
#from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
from functionssimulation import single_stim
from plot_eod_chirp import rectify,find_dev, conv,global_maxima,integrate_chirp,find_periods,find_lm,pl_eods
from plot_eod_chirp import find_beats,snip, power_func
import os
from myfunctions import default_settings
def plot_power(beats, results, win, deviation_s, sigma, sampling, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1, share = False):
#embed()
plt.rcParams['figure.figsize'] = (6.27, 8)
plt.rcParams["legend.frameon"] = False
colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime']
fig, ax = plt.subplots(nrows=len(results), ncols=2, sharex=True)
all_max = [[]] * len(results)
for i in range(len(results)):
ax[i, 0].set_ylabel(results['type'][i], rotation=0, labelpad=40, color=colors[i])
ax[i, 0].plot(beats / eod_fr + 1, np.array(results['result_frequency' ][i]) / eod_fr + 1, color=colors[i])
# plt.title(results['type'][i])
#ax[i, 1].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i, 1].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i])
#ax[i, 2].plot(beats / eod_fr + 1, np.array(results['amp'][i]), color=colors[i])
ax[i,0].set_ylim([1,1.6])
all_max[i] = np.max(np.array(results['result_amplitude_max'][i]))
#for i in range(len(results)):
# ax[i, 2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.25)
ii, jj = np.shape(ax)
ax[0, 0].set_title('Most popular frequency')
ax[0, 1].set_title('Modulation depth')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in range(ii):
for j in range(jj):
ax[-1, j].set_xlabel('EOD multiples')
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
def onclick(event):
#embed()
eod_fe = [(event.xdata-1)*eod_fr]
nfft = 4096
e = 0
#sampling = 121000
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
d = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
#cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = onclick_func(e,nfft,beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = show_figure, plot_dist = plot_dist, save = save,bef_c = bef_c,aft_c =aft_c, sampling = sampling)
nfft = 4096
results = [[]] * 1
name = ['cubed']
var = [cubed]
var = [maxima_interp_b]
samp = [sampling]
nfft = int((4096 * samp[0] / 10000) * 2)
i = 0
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
plt.figure()
plt.subplot(1,2,1)
plt.plot(f, pp)
plt.xlim([0,2000])
#plt.subplot(1,3,2)
#plt.plot(time_b, cubed)
plt.subplot(1,2,2)
plt.plot(time_b, maxima_interp_b)
plt.show()
if share == True:
cid = fig.canvas.mpl_connect('button_press_event', onclick)
return fig
def onclick_func(e,nfft, beats, results, disc, win, deviation_s, sigma, d_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False, plot_dist = False, save = False,bef_c = 1.1,aft_c =-0.1,sampling = 100000):
left_b = [bef_c * sampling] * len(beat_corr)
right_b = [aft_c * sampling] * len(beat_corr)
p = 0
s = 0
time_fish,sampled, cut, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp = snip(
left_b, right_b, e, e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr, chirp=False)
return pp, f, cubed, cubed, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b, am_corr_ds_b, am_df_ds_b, am_df_b, eod_overlayed_chirp
delta_t = 0.014
interest_interval = delta_t * 1.2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
phase_zero = np.arange(0,2*np.pi,2*np.pi/10) #phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
#eod_fr = 637# eod fish reciever
#eod_fr = 537
#eod_fr = 1435
load = False
counter = 0
results = []
results = pd.DataFrame(results)
if load == True:
#results = []
eod_fr = [500,734,820,1000,1492]
#eod_fr = [500]
for ee in range(len(eod_fr)):
factor = 200
sampling_fish = 500
step = 500
win = 'w2'
d = 1
x = [ 1.5]#x = [ 1.5, 2.5,0.5,]
time_range = 200 * delta_t
#sampling = 112345
sampling = [83425,98232,100000,112683]
#sampling = [8425, 9232]
for s in range(len(sampling)):
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling[s])
start = 5
end = 3500
# step = 25
step = [30, 25, 10]
#step = [250]
for ss in range(len(step)):
eod_fe, beat_corr, beats = find_beats(start,end,step[ss],eod_fr[ee])
delta_t = 1
load = True
size = [120]
a_fr = 1
a_fe = [0.5, 0.3, 0.2]
#a_fe = [0.5]
for a in range(len(a_fe)):
bef_c = -2
aft_c = -1
load = True
#if counter != 0:
# results = results1*1
results1 = power_func(a_fr = a_fr, a_fe = a_fe[a], eod_fr = eod_fr[ee], eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = sampling[s], deviation_ms = deviation_ms, beat_corr = beat_corr, size = size,phase_zero = [phase_zero[0]], delta_t = delta_t,deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False,bef_c = bef_c,aft_c = aft_c)
results1['sampling'] = sampling[s]
results1['eod_fr'] = eod_fr[ee]
results1['amplitude'] = a_fe[a]
results1['step'] = step[ss]
#if counter == 0:
# results = results1
#elif counter != 0:
results = results.append(results1,ignore_index = True)
#if counter != 0:
# results = results.append(results1)
counter += 1
#embed()
#for i in range
#embed()
#embed()
results = pd.DataFrame(results)
results.reset_index(drop=True)
results.to_pickle('numerical_test.pkl')
np.save('numerical_test.npy', results)
else:
results = pd.read_pickle('numerical_test.pkl')
results = results.reset_index(drop=True)
present_type = np.unique(results.type)
#present_type = list(present_type).pop('samped threshold')
error = {}
#error = pd.DataFrame(error)
for i in range(len(present_type)):
#data_baseline[data_baseline['dataset'] == set]
if present_type[i] != 'samped threshold':
result = results[np.array(results['type'] == present_type[i])]
#result['result_frequency']
#error[present_type[i]] =np.empty((4,7))
#error[present_type[i]] = np.NaN
error[present_type[i]] = []
step_all = np.unique(results['step'])
eod_fr_all = np.unique(results['eod_fr'])
sampling_all = np.unique(results['sampling'])
a_fe_all = np.unique(results['amplitude'])
for r in range(len(result)):
step = result.iloc[r]['step']
eod_fr = result.iloc[r]['eod_fr']
sampling = result.iloc[r]['sampling']
eod_fr = result.iloc[r]['eod_fr']
start = 5
end = 3500
# step = 25
#step = [30, 25, 10]
# step = [250]
#for ss in range(len(step)):
eod_fe, beat_corr, beats = find_beats(start, end, step, eod_fr)
error[present_type[i]].append(np.mean((beat_corr-result.iloc[r]['result_frequency'])**2))
#plt.title(present_type[i])
#plt.plot(beat_corr)
#plt.plot(result.iloc[r]['result_frequency'])
#plt.show()
embed()
default_settings([0], intermediate_width=8.29, intermediate_length=3.7, ts=10, ls=9, fs=9)
plt.figure(figsize = [6.28, 4])
err = {}
for i in range(len(error)):
if present_type[i] != 'samped threshold':
err[present_type[i]] = np.mean(error[present_type[i]])
plt.bar(np.array(list(err.keys())),np.array(list(err.values())))#np.arange(0,len(err),1)
plt.ylabel('Mean Square Root Error (MSE)')
plt.xticks(rotation=30)
plt.subplots_adjust(bottom = 0.3)
plt.savefig('numerical_test_bar.pdf')
plt.show()
#results = pd.read_pickle('numerical_test.pkl')
# embed()

2449
plot_eod_chirp.py Normal file

File diff suppressed because it is too large Load Diff

558
rotated.py Normal file
View File

@ -0,0 +1,558 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
from myfunctions import auto_rows
from functionssimulation import default_settings
import matplotlib.gridspec as gridspec
from myfunctions import remove_tick_marks
import string
from myfunctions import load_cell
def ps_df(data, d = '2019-09-23-ad-invivo-1', wish_df = 310, window = 'no',sampling_rate = 40000):
#nfft = 4096
#trial_cut = 0.1
#freq_step = sampling_rate / nfft
#d = load_cell(data[0], fname='singlecellexample5_2', big_file='beat_results_smoothed')
data_cell = data[data['dataset'] == d]#
dfs = np.unique(data_cell['df'])
df_here = dfs[np.argmin(np.abs(dfs - wish_df))]
dfs310 = data_cell[data_cell['df'] == df_here]
#pp = [[]]*len(dfs310)
pp = []
ppp = []
trial_cut = 0.1
for i in range(len(dfs310)):
duration = dfs310.iloc[i]['durations']
#cut_vec = np.arange(0, duration, trial_cut)
cut_vec = np.arange(0, duration, trial_cut)
#spikes_cut = spikes[(spikes > 0.05) & (spikes < 0.95)]
#for j, cut in enumerate(cut_vec):
# # print(j)
# spike_times = dfs310.iloc[i]['spike_times']
# spikes = spike_times - spike_times[0]
# spikes_cut = spikes[(spikes > cut) & (spikes < cut_vec[j + 1])]
# if cut == cut_vec[-2]:
# #counter_cut += 1
# break
# if len(spikes_cut) < 10:
# #counter_spikes += 1
# break
# spikes_mat = np.zeros(int(trial_cut * sampling_rate) + 1)
# spikes_idx = np.round((spikes_cut - trial_cut * j) * sampling_rate)
# for spike in spikes_idx:
# spikes_mat[int(spike)] = 1#
#
# #spikes_mat = np.zeros(int(spikes[-1]* sampling_rate + 5))
# #spikes_idx = np.round((spikes) * sampling_rate)
# #for spike in spikes_idx:
# # spikes_mat[int(spike)] = 1
# spikes_mat = spikes_mat * sampling_rate
# if type(window) != str:
# spikes_mat = gaussian_filter(spikes_mat, sigma=window)
# # smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05) * sampling_rate
# # smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2) * sampling_rate
# else:
# smoothened = spikes_mat * 1
# nfft = 4096
# p, f = ml.psd(spikes_mat - np.mean(spikes_mat), Fs=sampling_rate, NFFT=nfft, noverlap=nfft / 2)
# pp.append(p)
spike_times = dfs310.iloc[i]['spike_times']
if len(spike_times) < 3:
counter_spikes += 1
break
spikes = spike_times - spike_times[0]
spikes_cut = spikes[(spikes > 0.05) & (spikes < 0.95)]
if len(spikes_cut) < 3:
counter_cut += 1
break
spikes_mat = np.zeros(int(spikes[-1] * sampling_rate + 5))
spikes_idx = np.round((spikes) * sampling_rate)
for spike in spikes_idx:
spikes_mat[int(spike)] = 1
spikes_mat = spikes_mat * sampling_rate
if type(window) != str:
spikes_mat = gaussian_filter(spikes_mat, sigma=window)
# smoothened_spikes_mat05 = gaussian_filter(spikes_mat, sigma=window05) * sampling_rate
# smoothened_spikes_mat2 = gaussian_filter(spikes_mat, sigma=window2) * sampling_rate
else:
spikes_mat = spikes_mat*1
nfft = 4096
p, f = ml.psd(spikes_mat - np.mean(spikes_mat), Fs=sampling_rate, NFFT=nfft, noverlap=nfft / 2)
ppp.append(p)
#spike_times = data_cell.iloc[i]['spike_times']#
#if len(spike_times) < 3:
# counter_spikes += 1
# break
#spikes = spike_times - spike_times[0]
# cut trial into snippets of 100 ms
#cut_vec = np.arange(0, duration, trial_cut)
#spikes_cut = spikes[(spikes > 0.05) & (spikes < 0.95)]
#if len(spikes_cut) < 3:
# counter_cut += 1
# break
#spikes_new = spikes_cut - spikes_cut[0]
#spikes_mat = np.zeros(int(spikes_new[-1] * sampling_rate) + 2)
# spikes_mat = np.zeros(int(trial_cut * sampling_rate) + 1)
#spikes_idx = np.round((spikes_new) * sampling_rate)
#for spike in spikes_idx:
# spikes_mat[int(spike)] = 1
#spikes_mat = spikes_mat * sampling_rate
#nfft = 4096
#p, f = ml.psd(smoothened - np.mean(smoothened), Fs=sampling_rate, NFFT=nfft, noverlap=nfft / 2)
#ppp.append(p)
#p_mean = np.mean(pp,axis = 0)
p_mean2 = np.mean(ppp, axis=0)
#ref = (np.max(p_mean2))
#
db = 10 * np.log10(p_mean2 / np.max(p_mean2))
#ref = (np.max(p_mean2))
#db2 = 10 * np.log10(p_mean2 / ref)
#embed()
return df_here,p_mean2,f,db
def plot_example_ps(grid,lw_hline = 0.8,line_style = (0,(1,10)), hs = 0.5,hr = [1,1], multiples = 1.1,colors = ['brown'],fc = 'lightgrey',line_col = 'black',input = ['2019-10-21-aa-invivo-1'],sigma = [0.00005,0.00025,0.0005, 0.002],wish_df = 150, color_eod = 'orange',color_stim = 'red', color_df = 'green'):
sampling_rate = 40000
#colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B']
plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['lines.markersize'] = 6
#data = pd.read_pickle('../pictures_highbeats/data_beat.pkl')
#iter = np.unique(data['dataset'])
iter = ['2019-05-07-by-invivo-1']
iter = ['2019-09-23-ad-invivo-1']
iter = input
for cell in iter:
data = pd.read_pickle('data_beat.pkl')
beat_results = pd.read_pickle('beat_results_smoothed.pkl')
#embed()
eodf = int(beat_results[beat_results['dataset'] == cell]['eodf'].iloc[0])
df = [[]] * (len(sigma) + 1)
p = [[]] * (len(sigma) + 1)
f = [[]] * (len(sigma) + 1)
db = [[]] * (len(sigma) + 1)
sigmaf = [[]] * (len(sigma) + 1)
gauss = [[]] * (len(sigma) + 1)
df[0], p[0], f[0], db[0] = ps_df(data, d=cell, wish_df= wish_df, window='no', sampling_rate=sampling_rate)
for i in range(len(sigma)):
df[1+i], p[1+i], f[1+i], db[1+i] = ps_df(data, d=cell, wish_df= wish_df, window = sigma[i]*sampling_rate,sampling_rate = sampling_rate)
sigmaf[i + 1] = 1 / (2 * np.pi * sigma[i])
gauss[i + 1] = np.exp(-(f[1+i] ** 2 / (2 * sigmaf[i + 1] ** 2)))
db = 'no'
stepsize = f[0][1] - f[0][0]
if db == 'db':
p = db
# fig.suptitle(d, labelpad = 25)
#print(d)
ax = {}
ec = 'grey'
#fc = 'moccasin'
#ec = 'wheat'
scale = 1
#ax = plot_whole_ps(f, ax, grid, colors, eodf, stepsize, p, df, scale = scale, ax_nr = 0,nr=0, filter='whole' ,color_eod = color_eod,color_stim = color_stim , color_df = color_df,fc = fc, ec = ec)
#ax[0].legend( loc=(0,1),
# ncol=3, mode="expand", borderaxespad=0.)#bbox_to_anchor=(0.4, 1, 0.6, .1),
#ax[0] = remove_tick_marks(ax[0])
plots = gridspec.GridSpecFromSubplotSpec(2,1,
subplot_spec=grid[0], height_ratios = hr,wspace=0.4, hspace=hs)
ax = plot_whole_ps(f,ax, plots, colors, eodf, stepsize, p, df,mult = multiples, scale = scale, ax_nr = 0,nr = 0, filter = 'original',color_eod = color_eod,color_stim = color_stim , color_df = color_df,fc = fc, ec = ec)
ax[0].legend( loc=(0,1),
ncol=3, mode="expand", borderaxespad=0.)#bbox_to_anchor=(0.4, 1, 0.6, .1),
ax[0] = remove_tick_marks(ax[0])
nr_size = 10
ax[0].text(-0.1, 1.1, string.ascii_uppercase[0], transform=ax[0].transAxes,
size=nr_size, weight='bold')
#ax[0].set_ylim([0, 2000])
wide = 2
#embed()
for i in range(len(sigma)):
plots = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid[i+1],height_ratios = hr, wspace=0.4, hspace=hs)
ax[i+2] = plt.subplot( plots[0])
plot_filter(ax, i+2, f[1+i], p,i+1, colors, gauss[1+i], eodf, stepsize, wide, df[1+i],scale = scale,color_eod = color_eod,color_stim = color_stim , color_df = color_df,fc = fc, ec = ec)
ax[i+2].set_ylim([0, eodf*multiples])
ax[2].text(-0.1, 1.1, string.ascii_uppercase[1], transform=ax[2].transAxes,
size=nr_size, weight='bold')
ax[3].text(-0.1, 1.1, string.ascii_uppercase[2], transform=ax[3].transAxes,
size=nr_size, weight='bold')
ax[2] = remove_tick_marks(ax[2])
#embed()
#if db == 'db':
# ax[0].set_ylim([np.min([p]),0])#p[0][,p[1][0:2000],p[2][0:2000],p[3][0:2000]
#else:
# ax[0].set_ylim([ 0,np.max([p])])
ax[int(len(df))].set_ylabel('Frequency [Hz]')
# ax[1].set_ylabel(r'power [Hz$^2$/Hz]')
#ax[0].ticklabel_format(axis='y', style='sci', scilimits=[0, 0])
#print(df[3])
for i in [0,2,3]:
ax[i].spines['right'].set_visible(False)
ax[i].spines['top'].set_visible(False)
cols = grid.ncols
rows = grid.nrows
ax[int(len(df))].set_xlabel(' psd [Hz²/Hz]')
#ax[2].set_ylabel('Hz²/Hz')
#ax[3].set_ylabel('Hz²/Hz')
#ax[0].set_ylabel('Hz²/Hz')
for i in [0,2,3]:
ax[i].axhline(y = eodf/2, color = line_col, linestyle = line_style, linewidth = lw_hline)
plt.tight_layout()
#embed()
#fig.label_axes()
def plot_whole_ps(f,ax,grid, colors, eodf, stepsize, p, df, mult = 1.05,ax_nr = 0,nr = 0, filter = 'original', scale = 1, color_eod = 'orange',color_stim = 'red', color_df = 'green',fc = 'lightgrey', ec = 'grey',):
ax[ax_nr] = plt.subplot(grid[ax_nr])
if filter == 'whole':
#ax[nr].set_facecolor('lightgrey')
ax[ax_nr].plot(p[nr], f[nr], color=colors[0])
ax[ax_nr].fill_between([np.min(p), np.max(p)], [f[0][-1],f[0][-1]], color=fc,edgecolor=ec)
ax[ax_nr].plot(np.max(p[nr][int(abs(df[nr]) / stepsize) - 5:int(abs(df[nr]) / stepsize) + 5]) * scale, df[0],
color=color_df, marker='o', linestyle='None', label='Df')
ax[ax_nr].plot(p[nr][int((df[nr] + eodf) / stepsize) + 1], df[nr] + eodf, color=color_stim, marker='o',
linestyle='None',
label='stimulus')
ax[ax_nr].plot(np.max(p[nr][int(eodf / stepsize) - 5:int(eodf / stepsize) + 5]) * scale, eodf - 1, color=color_eod,
marker='o', linestyle='None', label='EODf') # = '+str(int(eodf))+' Hz')
elif filter == 'original':
#ax[nr].fill_between([eodf] * len(p[nr]), p[nr], color='lightgrey')
#ax[nr].fill_between([max(p[0])]*len(f[nr]),f[nr], color = 'lightgrey')
#embed()
ax[ax_nr].plot(p[nr], f[nr], color=colors[0])
#ax[ax_nr].plot(p[nr][f[nr]<eodf/2], f[nr][f[nr]<eodf/2], color=colors[0])
#ax[ax_nr].plot(np.zeros(len(f[nr][f[nr] > eodf / 2])), f[nr][f[nr] > eodf / 2], color=colors[0])
#embed()
ax[ax_nr].plot(np.max(p[nr][int(abs(df[nr]) / stepsize) - 5:int(abs(df[nr]) / stepsize) + 5]) * scale, df[0],
color=color_df, marker='o', linestyle='None', label='Df')
ax[ax_nr].plot(p[nr][int((df[nr] + eodf) / stepsize) + 1], df[nr] + eodf, color=color_stim, marker='o',
linestyle='None',
label='stimulus')
ax[ax_nr].plot(np.max(p[nr][int(eodf / stepsize) - 5:int(eodf / stepsize) + 5]) * scale, eodf - 1, color=color_eod,
marker='o', linestyle='None', label='EODf') # = '+str(int(eodf))+' Hz')
ax[ax_nr].fill_between([np.min(p),np.max(p)], [eodf/2,eodf/2], color=fc,edgecolor=ec)
#ax[ax_nr].plot(np.max(p[nr][int(abs(df[nr]) / stepsize) - 5:int(abs(df[nr]) / stepsize) + 5]) * scale, df[0],
# color=color_df, marker='o',zorder = 2, linestyle='None', label='Df')#edgecolors = 'black'
#ax[ax_nr].plot(0, df[nr] + eodf, color=color_stim, marker='o',
# linestyle='None',
# label='stimulus',zorder = 2)#,edgecolors = 'black'
#ax[ax_nr].plot(0, eodf - 1, color=color_eod,
# marker='o', linestyle='None', label='EODf',zorder = 2) #edgecolors = 'black', # = '+str(int(eodf))+' Hz')
#plt.plot([np.min(p),np.max(p)],[eodf,eodf], color = 'red')
#embed()
#ax[nr].plot([0]*5)
#ax[nr].plot([1000]*5)
# ax[0].fill_between( [max(p[0])]*len(f[1]),f[0], facecolor='lightgrey', edgecolor='grey')
ax[ax_nr].set_ylim([0, eodf * mult])
ax[ax_nr].set_xlim(ax[ax_nr].get_xlim()[::-1])
return ax
def plot_filter(ax, ax_nr, f, p4,array_nr, colors, gauss3, eodf, stepsize, wide, df, fc = 'lightgrey', scale = 1, ec = 'grey',color_eod = 'orange',color_stim = 'red', color_df = 'green'):
ax[ax_nr].plot( p4[array_nr],f, color=colors[0])
prev_height = np.max((p4[0][int(abs(df) / stepsize) - wide:int(abs(df) / stepsize) + wide]) * scale)
now_height = np.max((p4[array_nr][int(abs(df) / stepsize) - wide:int(abs(df) / stepsize) + wide]) *scale)
ax[ax_nr].plot([prev_height, now_height+440],[np.abs(df), np.abs(df)], color = 'black')
ax[ax_nr].scatter( now_height+440, np.abs(df), marker = '>', color='black', zorder = 2)
#embed()
ax[ax_nr].fill_between(max(p4[0]) * gauss3 ** 2,f, facecolor=fc, edgecolor=ec)
ax[ax_nr].plot(np.max(p4[array_nr][int(eodf / stepsize) - wide:int(eodf / stepsize) + wide]) * scale, eodf, color=color_eod, marker='o',
linestyle='None')
ax[ax_nr].plot( np.max(p4[array_nr][int(abs(df) / stepsize) - wide:int(abs(df) / stepsize) + wide]) * scale,abs(df),
color=color_df, marker='o', linestyle='None')
ax[ax_nr].plot(
np.max(p4[array_nr][int((df + eodf) / stepsize) - wide:int((df + eodf) / stepsize) + wide]) * scale,df + eodf,
color=color_stim, marker='o', linestyle='None')
#p[nr][int((df[nr] + eodf) / stepsize) + 1], df[nr] + eodf,
#np.max(p4[array_nr][int((df + eodf) / stepsize) - wide:int((df + eodf) / stepsize) + wide]) * scale, df + eodf
#embed()
ax[ax_nr].set_xlim(ax[ax_nr].get_xlim()[::-1])
return ax
def plot_amp(ax, mean1, dev,name = 'amp',nr = 1):
np.unique(mean1['type'])
all_means = mean1[mean1['type'] == name +' mean']
original = all_means[all_means['dev'] == 'original']
#m005 = all_means[all_means['dev'] == '005']
m05 = all_means[all_means['dev'] == '05']
m2 = all_means[all_means['dev'] == '2']
# fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True)
versions = [original, m05, m2] #m005,
for i in range(len(versions)):
keys = [k for k in versions[i]][2::]
try:
data = np.array(versions[i][keys])[0]
except:
break
axis = np.arange(0, len(data), 1)
axis_new = axis * 1
similarity = [keys, data]
sim = np.argsort(similarity[0])
# similarity[sim]
all_means = mean1[mean1['type'] == name+' std']
std = all_means[all_means['dev'] == dev[i]]
std = np.array(std[keys])[0]
#ax[1, 1].set_ylabel('Modulation depth')
#ax[nr,i].set_title(dev[i] + ' ms')
all_means = mean1[mean1['type'] == name+' 95']
std95 = all_means[all_means['dev'] == dev[i]]
std95 = np.array(std95[keys])[0]
all_means = mean1[mean1['type'] == name+' 05']
std05 = all_means[all_means['dev'] == dev[i]]
std05 = np.array(std05[keys])[0]
ax[nr,i].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]),
color='gainsboro')
ax[nr,i].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]),
color='darkgrey')
# ax[i].plot(data_tob.ff, data_tob.fe, color='grey', linestyle='--', label='AMf')
ax[nr,i].plot(np.array(keys)[sim], data[sim], color='black')
# ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %')
#embed()
return ax
def create_beat_corr(hz_range, eod_fr):
beat_corr = hz_range%eod_fr
beat_corr[beat_corr>eod_fr/2] = eod_fr[beat_corr>eod_fr/2] - beat_corr[beat_corr>eod_fr/2]
return beat_corr
def plot_mean_cells( grid,lw_hline = 0.8, hs = 0.5,line_style = (0,(1,10)), multiples = 1.1,data = ['2019-10-21-aa-invivo-1'],hr = [1,1],line_col = 'black',lw = 0.5, sigma = ['original','05','2'],colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B'], wish_df = 150, color_eod = 'black',color_df = 'orange', size = 17, color_modul = ['steelblue']):
#mean1 = pd.read_pickle('mean.pkl')
data_all = pd.read_pickle('beat_results_smoothed.pkl')
d = data_all[data_all['dataset'] == data[0]]
#embed()
inch_factor = 2.54
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.loc'] = 'upper right'
plt.rcParams["legend.frameon"] = False
# load data for plot
# data1 = pd.read_csv('ma_allcells_unsmoothed.csv')
# data2 = pd.read_csv('ma_allcells_05.csv')
# data3 = pd.read_csv('ma_allcells_2.csv')
# data_tob = pd.read_csv('ma_toblerone.csv')
# smothed = df_beat[df_beat['dev'] == 'original']
# data1 = smothed[smothed['type'] == 'amp']
x = np.arange(0, 2550, 50)
corr = create_beat_corr(x, np.array([500] * len(x)))
#np.unique(mean1['type'])
#all_means = mean1[mean1['type'] == 'max mean']
#versions = [[]]*len(dev)
#for i in range(len(dev)):
version =[[]]*len(sigma)
version2 = [[]] * len(sigma)
dev = [[]] * len(sigma)
limits = [[]]*len(sigma)
minimum = [[]] * len(sigma)
y_max = [[]] * len(sigma)
y_min = [[]] * len(sigma)
ax ={}
for i, e in enumerate(sigma):
y2 = d['result_amplitude_max_' + e]
y_max[i] = np.max(y2)
y_min[i] = np.min(y2)
for i,e in enumerate(sigma):
dev[i] = sigma[i]
plots = gridspec.GridSpecFromSubplotSpec(2,1,
subplot_spec=grid[i], height_ratios = hr,wspace=0.4, hspace=hs)
d = data_all[data_all['dataset'] == data[0]]
x = d['delta_f'] / d['eodf'] + 1
#embed()
data = ['2019-10-21-aa-invivo-1']
#end = ['original', '005', '05', '2']
y = d['result_frequency_' + e]
#embed()
y2 = d['result_amplitude_max_' + e]
#y_sum[i] = np.nanmax(y)
ff = d['delta_f'] / d['eodf'] + 1
fe = d['beat_corr']
#fig.suptitle(set)
ax[0] = plt.subplot(plots[0])
#if e != sigma[-1]:
ax[0] = remove_tick_marks(ax[0])
tob_col = 'black'
tob_lw = 0.8
ax[0].plot(ff, fe, color=tob_col, linestyle='--', linewidth=tob_lw, zorder=1)
ax[0].plot(x, y, color=colors[0], zorder = 2,linewidth = lw)
#embed()
eod = d['eodf'].iloc[0]
ax[0].axhline(y=eod / 2, color=line_col, linestyle=line_style,linewidth = lw_hline)
if np.max(y)<d['eodf'].iloc[0]*0.6:
color_chosen = color_df
else:
color_chosen = color_eod
#embed()
x_scatter = x.iloc[np.argmin(np.abs(np.array(x) - (wish_df / eod + 1)))]
#ax[0].scatter(x_scatter, y.iloc[np.argmin(np.abs(np.array(x) - (wish_df/eod+1)))], zorder=3, color=color_chosen, s = size)
#ax[0].set_ylabel('MPF [EODf]')
#ax[0].set_ylabel('Modulation ')
#ax[0, dd].set_title(e + ' ms')
ax[0].set_xlim([0, 4])
ax[1] = plt.subplot(plots[1])
if e != sigma[-1]:
ax[1] = remove_tick_marks(ax[1])
else:
ax[1].set_ylabel('F. height')
ax[1].plot(x, y2, color=color_modul[0],zorder = 2, linewidth = lw)
mod_tob = d['result_toblerone_max_' + e]
ax[1].plot(x, mod_tob, color=tob_col, linestyle='--', linewidth=tob_lw, zorder=1)
height = y2.iloc[np.argmin(np.abs(np.array(x)-(wish_df/eod+1)))]
if e == 'original':
whole_height = height
whole_height = np.max(y2)
if (e != 'whole') and (e!= 'original'):
ax[1].scatter(x_scatter, height+70, zorder=2, marker = 'v', color='black', s=size)
ax[1].plot([x_scatter,x_scatter], [whole_height,height +70], zorder=3,
color = 'black')
for ii in [1,2,3]:
ax[1].scatter(x_scatter+ii, height + 70, zorder=2, marker='v', color='black', s=size)
ax[1].plot([x_scatter+ii, x_scatter+ii], [whole_height, height + 70], zorder=3,
color='black')
#ax[1].scatter(x_scatter,height,zorder = 2, color = color_chosen, s =size)
#y_all[i] = np.max(y2)
#if i == len(sigma)-1:
# ax[1].set_xlabel('stimulus frequency [EODf]')
ax[1].set_ylim([np.min(y_min)*0.8, np.max(y_max)*1.2])
ax[1].spines['top'].set_visible(False)
ax[1].spines['right'].set_visible(False)
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
#ax[1].spines['right'].set_visible(False)
#ax[1].spines['top'].set_visible(False)
ax[0].set_xlim([0, 5])
ax[1].set_xlim([0, 5])
#embed()
ax[0].set_ylim([0, d['eodf'].iloc[0]* multiples])
ax[0].set_yticks([])
# fig.tight_layout()
# fig.label_axes()
print(sigma[i])
ax[1].set_xlabel('EOD multiples')
#embed()
plt.subplots_adjust(bottom = 0.1)
#fig.tight_layout()
# fig.label_axes()
if __name__ == "__main__":
data = ['2019-10-21-aa-invivo-1']
trans = False
sigma = [0.0005, 0.002] # 0.00005,0.00025,
if trans == True:
col = 1
row = 2
col_small = len(sigma)+1
row_small = 1
l = 6
t = 'horizontal'
wd = [1]
hd = [1,2.5]
left = 1
right = 0
else:
col = 2
row = 1
row_small = len(sigma)+1
col_small = 1
t = 'vertical'
l = 9
wd = [3, 4]
hd = [1]
left = 0
right = 1
default_settings(data, intermediate_width=6.28, intermediate_length=6, ts=6, ls=9, fs=9)
grid = gridspec.GridSpec(row, col, right = 0.95,wspace=0.02,top = 0.97,height_ratios = hd, width_ratios=wd, hspace=0.2)#,
hs = 0.03
axis = gridspec.GridSpecFromSubplotSpec(row_small,col_small,
subplot_spec=grid[0,0], wspace=0.15, hspace=hs)
wish_df = -220#324
color_eod = 'darkgreen'#'orange'
color_stim = 'navy'
color_df = 'orange'#'green'
colors = ['brown']
colors_mpf = ['crimson']
colors_mod = ['steelblue']
line_col = 'darkgrey'
line_style = 'dashdot'
hr = [2,1]
mult = 1.1
hss = 0.29
lw_hline = 1
plot_example_ps(axis,lw_hline = lw_hline, line_style = line_style,hs = hss,multiples = mult,hr = hr,input = ['2019-10-21-aa-invivo-1'],fc = 'lightgrey',line_col = line_col,colors = colors,sigma = sigma,wish_df = wish_df,color_eod = color_eod,color_stim = color_stim , color_df = color_df)
#plt.show()
#embed()
#fig.savefig()
left = 0
right = 1
#embed()
axis = gridspec.GridSpecFromSubplotSpec(row_small,col_small,
subplot_spec=grid[left,right], wspace=0.25, hspace=hs)
#embed()
plot_mean_cells(axis, lw_hline = lw_hline, hs = hss,multiples = mult,hr = hr, line_style = line_style,data = ['2019-10-21-aa-invivo-1'],line_col = line_col,lw = 1.23,size = 22, sigma = ['original','05','2'],colors = colors_mpf, wish_df = wish_df,color_eod = color_eod,color_df = color_df, color_modul = colors_mod )#'005','025',
plt.savefig('rotated.pdf')
plt.savefig('../highbeats_pdf/rotated.pdf')
#plt.savefig('.pdf')
plt.show()
# plt.savefig('../results/Ramona/ma_powerspecs_negative_df' + d + '.pdf')
# plt.show()
# plt.close()
# embed()
# plot_single_tublerones() # original beat_activity

432
simulationexamples.py Normal file
View File

@ -0,0 +1,432 @@
from plot_eod_chirp import find_lm,find_periods,integrate_chirp,global_maxima,rectify,find_dev,power_func,power_parameters
from plot_eod_chirp import rectify, find_beats,find_dev,find_times,global_maxima, find_lm, conv
from functionssimulation import single_stim,pl_eods
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed
import matplotlib as matplotlib
import math
import scipy.integrate as integrate
from scipy import signal
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import scipy as sp
import pickle
from scipy.spatial import distance
from myfunctions import *
import time
from matplotlib import gridspec
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.mlab as ml
import scipy.integrate as si
import pandas as pd
from myfunctions import default_settings
import os
import string
def snip(left_c,right_c,e,g,sampling, deviation_s,d,eod_fr, a_fr, eod_fe,phase_zero,p, size,s, sigma,a_fe,deviation,beat_corr, chirp = True):
time, time_cut, cut = find_times(left_c[g], right_c[g], sampling, deviation_s[d])
eod_fish_e, eod_fish_r, period_fish_r, period_fish_e = find_periods(a_fe, time, eod_fr, a_fr, eod_fe, e)
#embed()
if chirp == False:
eod_fe_chirp = eod_fish_e
else:
eod_fe_chirp = integrate_chirp(a_fe, time, eod_fe[e], phase_zero[p], size[s], sigma)
eod_rec_down, eod_rec_up = rectify(eod_fish_r, eod_fe_chirp) # rectify
eod_overlayed_chirp = (eod_fish_r + eod_fe_chirp)[cut:-cut]
threshold_cube = (eod_rec_up)**3
#embed()
maxima_values, maxima_index, maxima_interp = global_maxima(period_fish_e, period_fish_r,
eod_rec_up[cut:-cut]) # global maxima
index_peaks, value_peaks, peaks_interp = find_lm(eod_rec_up[cut:-cut]) # local maxima
middle_conv, eod_conv_down, eod_conv_up, eod_conv_downsampled = conv(eod_fr,sampling, cut, deviation[d], eod_rec_up,
eod_rec_down) # convolve
eod_fish_both = integrate_chirp(a_fe, time, eod_fe[e] - eod_fr, phase_zero[p], size[s], sigma)
am_corr_full = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s],
sigma) # indirect am calculation
_, time_fish, cut_f = find_times(left_c[g], right_c[g], eod_fr, deviation_s[d]) # downsampled through fish EOD
am_corr_ds = integrate_chirp(a_fe, time_fish, beat_corr[e], phase_zero[p], size[s], sigma)
am_df_ds = integrate_chirp(a_fe, time_fish, eod_fe[e] - eod_fr, phase_zero[p], size[s],
sigma) # indirect am calculation
return threshold_cube , time_cut, eod_conv_up, am_corr_full, peaks_interp, maxima_interp, am_corr_ds,am_df_ds,eod_fish_both,eod_overlayed_chirp
def power_func2(bef_c = -2, aft_c = -1, a_fr = 1, factor = 200, sampling = 10000, phase_zero = np.arange(0, 2 * np.pi, 2 * np.pi / 10),new = True, eod_fe = [50], beat_corr = [50], beats = [50],eod_fr = 500,size = [120] ,delta_t = 0.014,a_fe = 0.2, win = 'w2',deviation = [150], save = False):
print('simulation')
interest_interval = delta_t * 1.2
#bef_c = interest_interval / 2
#aft_c = interest_interval / 2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
# maximal frequency excursion during chirp / 60 or 100 here
# eod fish reciever
# amplitude fish reciever
#sampling = 500
d = 1
x = [1.5, 2.5, 0.5, ]
x = [1.5]
time_range = 200 * delta_t
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
if (not os.path.exists('power_simulation.pkl')) or (new == True):
results = [[]] * 1
for d in range(len(deviation)):
bef_c = bef_c
aft_c = aft_c
left_c, right_c, left_b, right_b, period_distance_c, period_distance_b, _, period, to_cut,exclude,consp_needed,deli,interval = period_calc(
[float('Inf')]*len(beat_corr), 50, win, deviation_s[d], sampling, beat_corr, bef_c, aft_c, 'stim')
save_n = win
for s in range(len(size)):
for p in range(len(phase_zero)):
beats = eod_fe - eod_fr
for e in range(len(eod_fe)):
#embed()
left_b = [-0.3*sampling]*len(eod_fe)
right_b = [-0.1 * sampling]*len(eod_fe)
threshold_cube, time_b, conv_b, am_corr_b, peaks_interp_b, maxima_interp_b,am_corr_ds_b, am_df_ds_b, am_df_b,eod_overlayed_chirp = snip(left_b, right_b,e,e,
sampling, deviation_s,
d, eod_fr, a_fr, eod_fe,
phase_zero, p, size, s,
sigma, a_fe, deviation,
beat_corr)
#time_c, conv_c, am_corr_c, peaks_interp_c, maxima_interp_c,am_corr_ds_c, am_df_ds_c, am_df_c = snip(left_c, right_c,e,e,
# sampling, deviation_s,
# d, eod_fr, a_fr, eod_fe,
# phase_zero, p, size, s,
# sigma, a_fe, deviation,
# beat_corr)
#embed()
nfft = 4096
if sampling>1000:
#nfft = int((4096 * sampling/ 10000) * 2)
nfft = int(4096 * sampling / 40000 * 2)
else:
nfft = 4096
#embed()
name = ['conv','df','df ds','corr','corr ds','global max','local max']
var = [conv_b,am_df_b,am_df_ds_b, am_corr_b, am_corr_ds_b,maxima_interp_b,peaks_interp_b ]
samp = [sampling,sampling,eod_fr,sampling,eod_fr,sampling,sampling]
name = ['global max']
var = [maxima_interp_b]
samp = [sampling]
#pp, f = ml.psd(eod_overlayed_chirp - np.mean(eod_overlayed_chirp), Fs=sampling, NFFT=nfft, noverlap=nfft / 2)
for i in range(len(samp)):
#print(i)
plot = False
pp, f = ml.psd(var[i] - np.mean(var[i]), Fs=samp[i], NFFT=nfft,
noverlap=nfft / 2)
#plot = True
if plot == True:
plt.figure()
plt.subplot(1,2,1)
plt.plot(var[i])
plt.title(name[i])
plt.subplot(1, 2, 2)
plt.plot(f,pp)
#plt.xlim([0,2000])
plt.show()
#print(results)
#embed()
#eod_fr2 = eod_fr*3
eod_fr2 = eod_fr
try:
if type(results[i]) != dict:
results[i] = {}
results[i]['type'] = name[i]
#embed()
results[i]['f'] = list([f[np.argmax(pp[f < 0.5 * eod_fr2])]])
results[i]['amp'] = list([np.sqrt(si.trapz(pp, f, np.abs(f[1]-f[0])))])
results[i]['max'] = list([np.sqrt(np.max(pp[f < 0.5 * eod_fr2])*np.abs(f[1]-f[0]))])
else:
results[i]['f'].extend(list([f[np.argmax(pp[f < 0.5 *eod_fr2])]]))
#embed()
results[i]['amp'].extend(list([np.sqrt(si.trapz(pp, f, np.abs(f[1]-f[0])))]))
results[i]['max'].extend(list([np.sqrt(np.max(pp[f < 0.5 * eod_fr2]) * np.abs(f[1] - f[0]))]))
except:
embed()
if save:
#embed()
results = pd.DataFrame(results)
results.to_pickle('power_simulation.pkl')
np.save('power_simulation.npy', results)
else:
results = pd.read_pickle('power_simulation.pkl')
return results
def plot_power(a_fe, a_fr,beats, beat_corr, eod_fe, sampling, ax, eod_fr,nr_size = 11, left = 0.15,nrs = [6,7], bef_c = -2, aft_c = -1):
plt.rcParams['figure.figsize'] = (3, 3)
plt.rcParams["legend.frameon"] = False
colors = ['black', 'magenta', 'pink', 'orange', 'moccasin', 'red', 'green', 'silver']
colors = ['red','pink']
#eod_fr = 500
#start = 5
#end = 2500
#step = 5
#eod_fe, beat_corr, beats = find_beats(start, end, step, eod_fr)
#embed()
#beats = eod_fe - eod_fr
factor = 200
#sampling = eod_fr * factor,
#(a_fr
# = 1, a_fe = 0.2, eod_fr = eod_fr, eod_fe = eod_fe, win = 'w2', deviation_s = deviation_s, sigma = sigma, sampling = 100000, deviation_ms = deviation_ms, beat_corr = beat_corr, size =[120], phase_zero =[phase_zero[0]], delta_t = delta_t, deviation_dp = deviation_dp, show_figure = True, plot_dist = False, save = False, bef_c = -2, aft_c = -1)
results = power_func2(bef_c = bef_c, aft_c = aft_c, a_fe = a_fe, a_fr = a_fr, sampling = sampling, phase_zero = [0], eod_fe = eod_fe, beat_corr = beat_corr, eod_fr = eod_fr, save = True)
#results = [results.iloc[5]]
results = [results.iloc[0]]
#fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
all_max = [[]] * len(results)
#embed()
for i in range(len(results)):
#embed()
#ax[0].set_ylabel(results[i]['type'], rotation=0, labelpad=40, color=colors[i])
#embed()
ax[0].plot(beats / eod_fr + 1, np.array(results[i]['f']) / eod_fr + 1, color=colors[i])
ax[0].text(-left, 1.1, string.ascii_uppercase[nrs[0]], transform=ax[0].transAxes,
size=nr_size, weight='bold')
# plt.title(results['type'][i])
#ax[1].plot(beats / eod_fr + 1, np.array(results[i]['amp']), color=colors[0])
ax[1].plot(beats / eod_fr + 1, np.array(results[i]['max']), color=colors[1])
ax[1].text(-0.1, 1.1, string.ascii_uppercase[nrs[1]], transform=ax[1].transAxes,
size=nr_size, weight='bold')
#remove_tick_marks(ax[1])
remove_tick_marks(ax[0])
#ax[2].plot(beats / eod_fr + 1, np.array(results[i]['amp']), color=colors[i])
all_max[i] = np.max(np.array(results[i]['amp']))
#for i in range(len(results)):
# ax[2].set_ylim([0, np.max(all_max)])
plt.subplots_adjust(left=0.25)
#ii, jj = np.shape(ax)
ax[0].set_ylabel('[Hz]')
ax[1].set_ylabel('Modulation')
#ax[0, 2].set_title('Modulation depth (same scale)')
for i in [0,1]:
ax[1].set_xlabel('EOD multiples')
ax[i].spines['right'].set_visible(False)
ax[i].spines['top'].set_visible(False)
ax[i].set_xlim([0,5])
#plt.subplots_adjust(bottom = 0.2)
#plt.savefig('localmaxima.pdf')
#plt.savefig('../highbeats_pdf/localmaxima.pdf')
#plt.show()
def plot_eod(beat_corr_upper, beats_upper,eod_fe_upper,beat_corr, minus_bef ,plus_bef,row,col,a_fe, delta_t, a_fr, size,beat_corr_org,s,p, amplitude, phase_zero, deviation_ms, eod_fe,deviation, eod_fr, sampling,factor,fs = [6.2992, 4], shift_phase = 0, bef_c = -2, aft_c = -1):
beats = eod_fe - eod_fr
nr_size = 11
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['font.size'] = 8
plt.rcParams["legend.frameon"] = False
beat_corr_col = 'goldenrod'
df_col = 'steelblue'
interval = np.hstack([np.linspace(0.25, 0.97)])
colors = plt.cm.Blues(interval)
colors = plt.cm.Reds(interval)
cmap = LinearSegmentedColormap.from_list('name', colors)
colors = cmap(np.linspace(0, 1, len(eod_fe)))
d = 0
fig = plt.figure(figsize=fs)#hspace = 0.55
outer = gridspec.GridSpec(5, 1, top = 0.9, hspace = 0, wspace = 0, height_ratios = [2,1.2,3.2,1.2,2])
#fig, ax = plt.subplots(nrows=row, ncols=col, figsize=fs)
ax = {}
#shift_phase = 0
#lower = gridspec.GridSpecFromSubplotSpec(1, 1, hspace = 0.63, subplot_spec=outer[0])
ax['fill_up'] = plt.subplot(outer[1])
ax['fill_up'].spines['right'].set_visible(False)
ax['fill_up'].spines['top'].set_visible(False)
ax['fill_up'].spines['left'].set_visible(False)
ax['fill_up'].spines['bottom'].set_visible(False)
ax['fill_lower'] = plt.subplot(outer[3])
ax['fill_lower'].spines['right'].set_visible(False)
ax['fill_lower'].spines['top'].set_visible(False)
ax['fill_lower'].spines['left'].set_visible(False)
ax['fill_lower'].spines['bottom'].set_visible(False)
left = 0.15
grid0 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=outer[2], height_ratios =[2,1,1], wspace=0.02, hspace=0.2) # ,
ax['upper'] = plt.subplot(grid0[0])
#start =
#embed()
remove_tick_marks(ax['upper'])
#embed()
ax['upper'].plot((eod_fe_upper - eod_fr)/eod_fr+1, (beat_corr_upper- eod_fr)/eod_fr+1, color = beat_corr_col)
ax['upper'].set_xlabel('EOD multiples')
ax['upper'].set_ylabel('[Hz]')
ax['upper'].plot((eod_fe_upper - eod_fr) / eod_fr + 1,np.abs(eod_fe_upper - eod_fr) / (eod_fr), color=df_col, alpha=0.7,zorder = 1)
#ax['upper'].set_xticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr))
#ax['upper'].set_yticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr))
ax['upper'].set_xticks(
np.arange((eod_fe_upper[0] - eod_fr) / eod_fr + 1, (eod_fe_upper[-1] - eod_fr) / eod_fr + 1, (eod_fr / 2) / eod_fr))
ax['upper'].set_yticks(
np.arange((eod_fe_upper[0] - eod_fr) / eod_fr + 1, (eod_fe_upper[-1] - eod_fr) / eod_fr + 1, (eod_fr / 2) / eod_fr))
nrs = [5]
ax['upper'].text(-left, 1.1, string.ascii_uppercase[nrs[0]], transform=ax['upper'].transAxes,
size=nr_size, weight='bold')
plt.grid()
colors = np.concatenate([['hotpink']*col,['red']*col,['darkred']*col])
plt.xlim([(np.min(eod_fe_upper)- eod_fr)/eod_fr+1, (np.max(eod_fe_upper)- eod_fr)/eod_fr+1])
#ax[d] = fig.add_subplot(row,col, 1)#int(np.ceil(np.sqrt(len(eod_fe))))
#sampling_rate = sampling
plt.ylim([-0.2,1])
ax[0] = plt.subplot(grid0[1])
ax[1] = plt.subplot(grid0[2])
plot_power(a_fe, a_fr, beats_upper, beat_corr_upper, eod_fe_upper, sampling, ax, eod_fr,left = left, nr_size = nr_size,bef_c = bef_c, aft_c = aft_c)
upper = gridspec.GridSpecFromSubplotSpec(int(row / 2), col, hspace=0.5, subplot_spec=outer[0])
nfft = 4096
nfft = int(4096 * sampling / 10000 * 2)
full = eod_fe
colors_full = colors
#embed()
eod_fe = full[5:10]
colors_plot = colors_full[5:10]
colors = colors_full[5:10]
colors = ['black'] * len(eod_fe)
#colors = ['black']*len(eod_fe)
nrs = [0, 1, 2, 3, 4, 5]
for e in range(len(eod_fe)):
f_max,lims,ax = single_stim(ax,colors_plot, int(row/2), col, eod_fr, eod_fe, e, upper,s = s, p = p, d = d, df_col = df_col, factor = factor, beat_corr_col = beat_corr_col, nfft = nfft, minus_bef = minus_bef, delta_t = delta_t, sampling = sampling,
deviation = deviation,plus_bef = plus_bef, a_fr = a_fr, phase_zero = phase_zero, shift_phase = shift_phase, size = size, a_fe = a_fe)
ax[e].text(-left, 1.1, string.ascii_uppercase[nrs[e]], transform=ax[e].transAxes,
size=nr_size, weight='bold')
x = (eod_fe[e] - eod_fr)/eod_fr
ax['upper'].scatter(x, (f_max)/eod_fr, color=colors_full[e], s=19,zorder = 2)
ax['fill_up'].scatter(x, 1, marker = 'v',color=colors[e], s=19,zorder = 2)
ax['fill_up'].plot([x,x], [3,1], color=colors[e], zorder = 2)
axis = (eod_fe_upper - eod_fr) / eod_fr
#embed()
ax['fill_up'].set_xlim([axis[0],axis[-1]])
ax['fill_up'].set_ylim([0.72,6])
ax['fill_up'].set_yticks([])
lower = gridspec.GridSpecFromSubplotSpec(int(row / 2), col, hspace=0.5, subplot_spec=outer[4])
eod_fe = full[0:5]
colors_plot = colors_full[0:5]
colors_plot = colors_full[5:10]
colors = colors_full[5:10]
colors = ['black'] * len(eod_fe)
nrs = [8,9, 10, 11, 12, 13, 14]
for e in range(len(eod_fe)):
f_max,lims,ax = single_stim(ax,colors_plot, int(row/2), col, eod_fr, eod_fe, e, lower, s = s, p = p, d = d, df_col = df_col, factor = factor, beat_corr_col = beat_corr_col, nfft = nfft, minus_bef = minus_bef, delta_t = delta_t, sampling = sampling,
deviation = deviation,plus_bef = plus_bef, a_fr = a_fr, phase_zero = phase_zero, shift_phase = shift_phase, size = size, a_fe = a_fe)
ax[e].text(-left, 1.1, string.ascii_uppercase[nrs[e]], transform=ax[e].transAxes,
size=nr_size, weight='bold')
ax['upper'].scatter((eod_fe[e] - eod_fr)/eod_fr+1, (f_max)/eod_fr, color=colors_full[e], s=19, zorder = 2)
#ax['upper'].scatter((eod_fe[e] - eod_fr) / eod_fr, -0.17, marker = '^', color=colors[e], s=19)
x = (eod_fe[e] - eod_fr) / eod_fr
ax['fill_lower'].scatter(x, 4.5, marker = '^',color=colors[e], s=19, zorder = 2)
ax['fill_lower'].plot([x,x], [1.5,4.5], color=colors[e], zorder = 2)
axis = (eod_fe_upper - eod_fr) / eod_fr
#embed()
ax['fill_lower'].set_xlim([axis[0],axis[-1]])
ax['fill_lower'].set_ylim([0.5,7.3])
ax['fill_lower'].set_yticks([])
#for e in range(int(len(eod_fe)/2)):
# f_max,lims = single_stim(factor, beats, beat_corr, plus_bef,ax,s,df_col, beat_corr_col, colors, lower, row, col, fig, nfft, p, minus_bef, delta_t, sampling,
# deviation, d, eod_fr, eod_fe, e, a_fr, phase_zero, shift_phase, size, sigma, a_fe)
# ax['upper'].scatter((eod_fe[e] - eod_fr)/eod_fr, (f_max)/eod_fr, color=colors[e], s=19)
#plt.xlabel('Time [ms]')
plt.legend(loc=(-5, -0.7),ncol = 4)
plt.subplots_adjust(bottom = 0.1, top = 0.8,left = 0.13)
#embed()
#scalebar = ScaleBar(50,'ms')
#plt.gca().add_artist(scalebar)
#embed()
# plt.xlim([-200,200])
#plt.show()
return fig
if __name__ == "__main__":
delta_t = 0.014 # ms
interest_interval = delta_t * 1.2
bef_c = interest_interval / 2
aft_c = interest_interval / 2
sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp
size = [120] # maximal frequency excursion during chirp / 60 or 100 here
phase_zero = [0] # phase when the chirp occured (vary later) / zero at the peak o a beat cycle
phase_zero = np.arange(0, 2 * np.pi, 2 * np.pi / 10)
a_fr = 1 # amplitude fish reciever
amplitude = a_fe = 0.5 # amplitude fish emitter
factor = 200
eod_fr =500 # eod fish reciever
eod_fr = 537
eod_fr = 734
eod_fr = 500
initial = np.array([60, 310])
stim_matrix = np.transpose([initial, initial + eod_fr, initial + eod_fr*2, initial + eod_fr*3, initial + eod_fr*4])
eod_fe = np.concatenate(stim_matrix)
sampling = eod_fr * factor
sampling = 112683 #122300 #500
sampling_fish = 1000
start = 5
step = 10
eod_fe_upper = np.arange(start,np.int(np.max(eod_fe)/eod_fr)*eod_fr+eod_fr,step)
beats_upper = eod_fe_upper - eod_fr
beat_corr_upper = eod_fe_upper % eod_fr
beat_corr_upper[beat_corr_upper > eod_fr / 2] = eod_fr - beat_corr_upper[beat_corr_upper > eod_fr / 2]
# start = 0
# end = 2500
# step = 10
win = 'w2'
d = 1
x = [1.5, 2.5, 0.5, ]
x = [1.5]
time_range = 200 * delta_t
deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling)
#embed()
pp = [1, 3, 6, 9] # [2,7]
minus_bef = -20
plus_bef = -10
#minus_bef = -10
#plus_bef = -10
bef_c = -2
aft_c = -1
# embed()
# eod_fe = np.array([initial,60+500,310+500,1010,60+1000,310+1000,1010+500,60+1000+500,310+1000+500])
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
row, col = np.shape(stim_matrix)
fig = plot_eod(beat_corr_upper, beats_upper,eod_fe_upper,beat_corr, minus_bef, plus_bef, row, col, a_fe, delta_t, a_fr, size, beat_corr, 0, 0, a_fe, phase_zero,
deviation_ms, eod_fe, deviation_dp, eod_fr, sampling, factor, fs=[6.28, 6],
shift_phase=(2 * np.pi) / 4, bef_c = bef_c, aft_c = aft_c) # 6.2992,6.69
#fig.savefig()
plt.savefig(
fname= 'simulationexamples.pdf')
plt.savefig('../highbeats_pdf/simulationexamples.pdf')
plt.show()

401
singlecellexample5.py Normal file
View File

@ -0,0 +1,401 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
#from axes import label_axes, labelaxes_params
from myfunctions import auto_rows
#from differentcells import default_settings
#from differentcells import plot_single_cells
import matplotlib.gridspec as gridspec
from functionssimulation import single_stim
import math
from functionssimulation import find_times
from functionssimulation import rectify
from functionssimulation import global_maxima
from functionssimulation import integrate_chirp
from functionssimulation import find_periods
from myfunctions import default_settings, load_cell
from axes import labelaxes_params,label_axes
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import string
def plot_single_cells(ax, colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B'], data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1'], var = '05'):
y_sum = [[]] * len(data)
axis = {}
for ee, e in enumerate([var]):
#d = data_all[data_all['dataset'] == ]
d = load_cell(data[0], fname='singlecellexample5_smoothed'+data[0], big_file='beat_results_smoothed')
eod = d['eodf'].iloc[0]
#embed()
x = d['delta_f'] / d['eodf'] + 1
xx = d['delta_f']
y = d['result_frequency_' + e]
y2 = d['result_amplitude_max_' + e]
y_sum[0] = np.nanmax(y)
axis[1] = plt.subplot(ax[0])
axis[1].plot(x, y, zorder = 1,color=colors[0])
axis[1].set_ylabel('[Hz]')
axis[1].set_xlim([0, 4])
labels = [item.get_text() for item in axis[1].get_xticklabels()]
empty_string_labels = [''] * len(labels)
axis[1].set_xticklabels(empty_string_labels)
#axis[2] = host_subplot(ax[1], axes_class=AA.Axes)
axis[2] = plt.subplot(ax[1])
#aixs[3] = axis[2].twinx()
#par2 = axis[2].twinx()
#host = host_subplot(ax[1], axes_class=AA.Axes)
#host.spines['right'].set_visible(False)
#host.spines['top'].set_visible(False)
#axis[2] = host.twiny()
axis[2].plot(xx, y2, label="Beats [Hz]", zorder = 2,color=colors[0])
axis[2].set_ylabel('Modulation ')
axis[1].spines['right'].set_visible(False)
axis[1].spines['top'].set_visible(False)
axis[2].spines['right'].set_visible(False)
axis[2].spines['top'].set_visible(False)
axis[1].set_xlim([0, np.max(x)])
axis[2].set_xlim([-eod, np.max(xx)])
nr_size = 10
axis[2].text(-0.02, 1.1, string.ascii_uppercase[4],
transform=axis[2].transAxes,
size=nr_size, weight='bold')
axis[1].text(-0.02, 1.1, string.ascii_uppercase[3],
transform=axis[1].transAxes,
size=nr_size, weight='bold')
#axis[4] = axis[2].twiny()
axis[3] = axis[2].twiny()
axis[3].set_xlabel('EOD multiples')
offset = -40
#new_fixed_axis = axis[3].get_grid_helper().new_fixed_axis
#axis[3].axis["bottom"] = new_fixed_axis(loc="bottom", axes=axis[3],
# offset=(0,offset))
axis[3].xaxis.set_ticks_position("bottom")
axis[3].xaxis.set_label_position("bottom")
axis[3].spines["bottom"].set_position(("axes", -1.2))
axis[3].spines['right'].set_visible(False)
axis[3].spines['top'].set_visible(False)
#axis[3].axis["bottom"].toggle(all=True)
axis[2].set_xlabel("Difference frequency [Hz]")
#par2.set_xlim([np.min(xx), np.max(xx)])
axis[3].set_xlim([0, np.max(x)])
#p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
#p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = axis[3].plot(x, y2,color = 'grey',zorder = 1)
#p3, = axis[4].plot(x, y2, color='grey', zorder=1)
#embed()
axis[2].set_xticks(np.arange(-eod,np.max(xx),eod/2))
#ax['corr'].set_yticks(np.arange(eod_fe[0] - eod_fr, eod_fe[-1] - eod_fr, eod_fr / 2))
axis[2].set_ylim([0, np.nanmax(y_sum)])
plt.subplots_adjust(wspace = 0.4,left = 0.17, right = 0.96,bottom = 0.2)
return axis,y2
def plot_beat_corr(ax,lower,beat_corr_col = 'red',df_col = 'pink',ax_nr = 3,multiple = 3):
eod_fr = 500
eod_fe = np.arange(0, eod_fr * multiple, 5)
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
#gs0 = gridspec.GridSpec(3, 1, height_ratios=[4, 1, 1], hspace=0.7)
#plt.figure(figsize=(4.5, 6))
style = 'dotted'
color_v = 'black'
color_b = 'silver'
# plt.subplot(3,1,1)
ax['corr'] = plt.subplot(lower[ax_nr])
np.max(beats) / eod_fr
ax['corr'].set_xticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr+1))
ax['corr'].set_yticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr+1))
ax['corr'].set_xticks(np.arange(0,10,0.5))
ax['corr'].set_yticks(np.arange(0, 10, 0.5))
# plt.axvline(x = -250, Linestyle = style,color = color_v)
# plt.axvline(x = 250, Linestyle = style,color = color_v)
# plt.axvline(x = 750, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style)
# plt.subplot(3,1,2)
plt.xlabel('Beats [Hz]')
plt.ylabel('Difference frequency [Hz]')
#plt.subplot(gs0[1])
if beat_corr_col != 'no':
plt.plot(beats/eod_fr+1, beat_corr/(eod_fr+1), color=beat_corr_col, alpha = 0.7)
plt.ylim([0,np.max(beat_corr/(eod_fr+1))*1.4])
plt.xlim([(beats/eod_fr+1)[0],(beats/eod_fr+1)[-1]])
if df_col != 'no':
plt.plot(beats/eod_fr+1, np.abs(beats)/(eod_fr+1), color=df_col, alpha = 0.7)
#plt.axvline(x=-250, Linestyle=style, color=color_v)
#plt.axvline(x=250, Linestyle=style, color=color_v)
#plt.axvline(x=750, Linestyle=style, color=color_v)
plt.xlabel('EOD adjusted beat [Hz]')
ax['corr'] .spines['right'].set_visible(False)
ax['corr'] .spines['top'].set_visible(False)
ax['corr'] .spines['left'].set_visible(True)
ax['corr'] .spines['bottom'].set_visible(True)
# plt.axvline(x = 1250, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style,color = color_v)
mult = np.array(beats) / eod_fr + 1
# plt.subplot(3,1,3)
plt.xlabel('EOD multiples')
plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
plt.grid()
#plt.subplot(gs0[2])
#plt.plot(mult, beat_corr, color=color_b)
# plt.axvline(x = 0, Linestyle = style)
#plt.axvline(x=0.5, Linestyle=style, color=color_v)
# plt.axvline(x = 1, Linestyle = style)
#plt.axvline(x=1.5, Linestyle=style, color=color_v)
#plt.axvline(x=2.5, Linestyle=style, color=color_v)
#plt.xlabel('EOD multiples')
#plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
return ax
def try_resort_automatically():
diffs = np.diff(dfs)
fast_sampling = dfs[np.concatenate([np.array([True]),diffs <21])]
second_derivative = np.diff(np.diff(fast_sampling))
first_index = np.concatenate([np.array([False]),second_derivative <0])
second_index = np.concatenate([second_derivative > 0,np.array([False])])
remaining = fast_sampling[np.concatenate([np.array([True]),second_derivative == 0, np.array([True])])]
first = np.arange(0,len(first_index),1)[first_index]
second = np.arange(0, len(second_index), 1)[second_index]-1
residual = []
indeces = []
for i in range(len(first)):
index = np.arange(first[i],second[i],2)
index2 = np.arange(first[i], second[i], 1)
indeces.append(index2)
residual.append(fast_sampling[index])#first[i]:second[i]:2
indeces = np.concatenate(indeces)
remaining = fast_sampling[~indeces]
residual = np.concatenate(residual)
new_dfs = np.sort(np.concatenate([residual, remaining]))
def gap_subplot(ax, dfs, pos_left = 0, pos_right = 1, xl = -0.5, xr = 30, ax_name = 'between'):
ax[ax_name] = plt.subplot(grid[pos_left, pos_right])
ax[ax_name].spines['right'].set_visible(False)
ax[ax_name].spines['top'].set_visible(False)
ax[ax_name].spines['left'].set_visible(False)
ax[ax_name].spines['bottom'].set_visible(False)
ax[ax_name].set_ylim([np.min(dfs), np.max(dfs)])
ax[ax_name].set_xlim([xl,xr])
ax[ax_name].set_xticks([])
ax[ax_name].set_yticks([])
ax[ax_name].set_ylim(ax[ax_name].get_ylim()[::-1])
return ax[ax_name]
def morane_plot(ax, dfs, grid, d,nr_size = 11, ll = 0.1, ul = 0.3, ax_name = 'scatter',gl = 0, gr =0):
ax[ax_name] = plt.subplot(grid[gl,gr])
ax[ax_name].spines['right'].set_visible(False)
ax[ax_name].spines['top'].set_visible(False)
counter = 0
new_dfs = np.concatenate([dfs[0:25], dfs[25:40:2], dfs[40:53:2], dfs[54:-1]])
for i in range(len(new_dfs)):
spikes = d[d['df'] == new_dfs[i]]['spike_times']
counter += 1
transformed_spikes = spikes.iloc[0]-spikes.iloc[0][0]
used_spikes = transformed_spikes[transformed_spikes>ll]
used_spikes = used_spikes[used_spikes<ul]*1000
ax['scatter'].scatter(used_spikes,np.ones(len(used_spikes))*new_dfs[i],s = 0.2,color = 'silver')
ax[ax_name].set_ylim([np.min(dfs),np.max(dfs)])
ax[ax_name].set_xlim([ll*1000,ul*1000])
ax[ax_name].set_ylabel('Difference frequency [Hz]')
ax[ax_name].set_xlabel('Time [ms]')
ax[ax_name].set_ylim(ax['scatter'].get_ylim()[::-1])
ax[ax_name].text(-0.1, 1.025, string.ascii_uppercase[0], transform=ax['scatter'].transAxes,
size= nr_size, weight='bold')
return ax[ax_name]
def examples(dfs,ax, axis, eod,grid, lg = 0, rg = 2):
new_dfs = np.concatenate([dfs[0:25], dfs[25:40:2], dfs[40:53:2], dfs[54:-1]])
first = ['darkviolet','lightcoral','crimson', ]
third = ['orange', 'orangered', 'darkred']
fourth = ['DarkGreen', 'LimeGreen', 'YellowGreen']
fifth = ['lightblue','blue','navy']
colors = np.concatenate([fourth, third, first, fifth])
low_nr = 60
from_middle = 45 # 20
example_df = [low_nr - eod, eod / 2 - from_middle - eod,
low_nr, eod / 2 - from_middle,
low_nr + eod, eod / 2 - from_middle + eod,
low_nr + eod * 2, eod / 2 - from_middle + eod * 2,
low_nr + eod * 3, eod / 2 - from_middle + eod * 3,
low_nr + eod * 4 + eod * 4]
example_df = [low_nr - eod, eod / 2 - from_middle - eod, eod - low_nr - eod, low_nr, eod / 2 - from_middle,
low_nr + eod,eod / 2 - from_middle+eod*2]
#embed()
dfs_all = [[]]*len(example_df)
for i in range(len(example_df)):
df = new_dfs[np.argmin(np.abs(new_dfs - example_df[i]))]
dfs_all[i] = [df]
example_df = np.unique(dfs_all)
max_p = [[]] * len(example_df)
min_p = [[]] * len(example_df)
rows = len(example_df)
cols = 1
power_raster = gridspec.GridSpecFromSubplotSpec(rows, cols,
subplot_spec=grid[lg, rg], wspace=0.05, hspace=0.3)
for i in range(len(example_df)):
power = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1, 1.7], hspace=0.2, wspace=0.2,
subplot_spec=power_raster[i])
ax_nr = 0
ax['scatter_small' + str(i)] = plt.subplot(power[ax_nr])
eod_fr = np.array(500)
eod_fr = np.array(eod)
eod_fe = np.array([example_df[i] + eod])
e = 0
factor = 200
sampling = 100000
minus_bef = -250
plus_bef = -200
f_max, lims, _ = single_stim(ax, [colors[i]], 1, 1, eod_fr, eod_fe, e, power, delta_t=0.001, add='no',
minus_bef=minus_bef, plus_bef=plus_bef, sampling=sampling,
col_basic='silver', col_hline='no', labels=False, a_fr=1, ax_nr=ax_nr,
phase_zero=[2*np.pi*0.4], shift_phase=0, df_col='no', beat_corr_col='no', size=[120],
a_fe=0.8)
ax['between'].scatter(0.12, example_df[i], zorder=2, s=25, marker='<', color=colors[i])
ax['between'].scatter(0.12, example_df[i], zorder=2, s=25, marker='<', color=colors[i])
ll = np.abs(plus_bef)
ul = np.abs(minus_bef)
#df = new_dfs[np.argmin(np.abs(new_dfs - example_df[i]))]
df = example_df[i]
spikes = d[d['df'] == df]['spike_times']
tranformed_spikes = spikes.iloc[0] * 1000 - spikes.iloc[0][0] * 1000
used_spikes = tranformed_spikes[tranformed_spikes > ll]
used_spikes = used_spikes[used_spikes < ul]
used_spikes = used_spikes - used_spikes[0]
ax['scatter_small' + str(i)].scatter((used_spikes), np.ones(len(used_spikes)) * -2, zorder=2, s=2, marker='|',
color='black') # color = colors[i]
ax['scatter_small' + str(i)].set_ylim([-2.5, 2.2])
ax['power' + str(i)] = plt.subplot(power[1])
nfft = 4096 #
sampling_rate = 40000
# embed()
pp = [[]] * len(spikes)
for s in range(len(spikes)):
new_spikes = list(map(int, (spikes.iloc[s] - spikes.iloc[s][0]) * sampling_rate))
array = np.zeros(new_spikes[-1] + 2)
array[new_spikes] = 1
array = array * sampling_rate
if var == '05':
window05 = 0.0005 * sampling_rate
array = gaussian_filter(array, sigma=window05) * sampling_rate
pp[s], f = ml.psd(array - np.mean(array), Fs=sampling_rate, NFFT=nfft, noverlap=nfft / 2)
p = np.mean(pp, axis=0)
diff = d['eodf'].iloc[0] * (df / d['eodf'].iloc[0] - int(df / d['eodf'].iloc[0]))
if diff > d['eodf'].iloc[0] * 0.5:
diff = diff - d['eodf'].iloc[0] * 0.5
ref = (np.max(p))
p = 10 * np.log10(p / ref)
plt.plot(f, p, zorder=1, color=main_color)
max_p[i] = np.max(p)
min_p[i] = np.min(p)
ax['power' + str(i)].scatter(f[np.argmax(p[f < 0.5 * eod])], max(p[f < 0.5 * eod]), zorder=2, color=colors[i],
s=25)
ax['power' + str(i)].scatter(f[f == f[np.argmin(np.abs(f - eod))]],
p[f == f[np.argmin(np.abs(f - eod))]] * 0.90, zorder=2,
s=25, color='darkgrey', edgecolor='black')
ax['power' + str(i)].axvline(x=eod / 2, color='black', linestyle='dashed', lw=0.5)
plt.xlim([-40, 1600])
axis[3].scatter(example_df[i] / (eod) + 1, np.sqrt(np.max(p[f < 0.5 * eod]) * np.abs(f[0] - f[1])), zorder=3,
s=20, marker='o', color=colors[i])
axis[1].scatter(example_df[i] / (eod) + 1, f[np.argmax(p[f < 0.5 * eod])], zorder=2, s=20, marker='o',
color=colors[i])
if i != rows - 1:
labels = [item.get_text() for item in ax['scatter_small' + str(i)].get_xticklabels()]
empty_string_labels = [''] * len(labels)
ax['scatter_small' + str(i)].set_xticklabels(empty_string_labels)
labels = [item.get_text() for item in ax['power' + str(i)].get_xticklabels()]
empty_string_labels = [''] * len(labels)
ax['power' + str(i)].set_xticklabels(empty_string_labels)
else:
ax['power' + str(i)].set_xlabel('Frequency [Hz]')
ax['scatter_small' + str(i)].set_xlabel('Time [ms]')
ax['power' + str(i)].set_yticks([])
ax['power' + str(i)].spines['left'].set_visible(False)
ax['scatter_small' + str(i)].spines['left'].set_visible(False)
ax['scatter_small' + str(i)].set_yticks([])
ax['power' + str(i)].spines['right'].set_visible(False)
ax['power' + str(i)].spines['top'].set_visible(False)
ax['scatter_small' + str(i)].spines['right'].set_visible(False)
ax['scatter_small' + str(i)].spines['top'].set_visible(False)
for i in range(len(example_df)):
#ax['power' + str(i)].set_ylim([0, np.max(max_p)])
#ax['power' + str(i)].set_ylim([np.min(min_p), np.max(max_p)*1.3])
ax['power' + str(i)].set_ylim([np.min(min_p), 4])
#embed()
ax['power' + str(0)].text(-0.1, 1.1, string.ascii_uppercase[2], transform=ax['power' + str(0)].transAxes,
size=nr_size, weight='bold')
ax['scatter_small' + str(0)].text(-0.1, 1.1, string.ascii_uppercase[1],
transform=ax['scatter_small' + str(0)].transAxes,
size=nr_size, weight='bold')
return ax, axis
if __name__ == "__main__":
# data = ['2019-10-21-aa-invivo-1']
data = ['2019-11-18-ac-invivo-1']# '2019-10-28-aj-invivo-1'
data = ['2019-10-28-aj-invivo-1']
data = ['2019-09-23-ad-invivo-1']#6.29
default_settings(data,intermediate_width = 6.28,intermediate_length = 7.5, ts = 6, ls = 9, fs = 9)
fig = plt.figure()
ax = {}
nr_size = 10
main_color = 'darkgrey'
var = 'original'#var = '05'
d = load_cell(data[0], fname='singlecellexample5_data_beat'+data[0], big_file='data_beat',new = False)
eod = d['eodf'].iloc[0]
dfs = np.unique(d['df'])
x = d['df'] / d['eodf'] + 1
grid = gridspec.GridSpec(2, 3, wspace=0.0, height_ratios=[6, 2], width_ratios=[1,0.3,3], hspace=0.24)
#first subplot
ax['scatter'] = morane_plot(ax, dfs, grid, d, nr_size=nr_size, ll=0.1, ul=0.3, ax_name='scatter', gl=0, gr=0)
#gap between subplots
ax['between'] = gap_subplot(ax,dfs, pos_left=0, pos_right=1, xl=-0.5, xr=30, ax_name='between')
#summary picture
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid[1,:], wspace=0, hspace=0.5)
axis,y2 = plot_single_cells(axis, colors = [main_color], data = data,var = var)
#examples
ax, axis = examples(dfs,ax, axis, eod, grid, lg=0, rg=2)
plt.subplots_adjust(left = 0.11, bottom = 0.16, top = 0.94)
plt.savefig('singlecellexample5.pdf')
plt.savefig('../highbeats_pdf/singlecellexample5.pdf')
plt.savefig('singlecellexample5'+data[0]+'.pdf')
plt.savefig('../highbeats_pdf/singlecellexample5'+data[0]+'.pdf')
plt.show()

472
singlecellexample5_all.py Normal file
View File

@ -0,0 +1,472 @@
import nixio as nix
import os
from IPython import embed
#from utility import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.mlab as ml
import scipy.integrate as si
from scipy.ndimage import gaussian_filter
from IPython import embed
from myfunctions import *
#from axes import label_axes, labelaxes_params
from myfunctions import auto_rows
#from differentcells import default_settings
#from differentcells import plot_single_cells
import matplotlib.gridspec as gridspec
from functionssimulation import single_stim
import math
from functionssimulation import find_times
from functionssimulation import rectify
from functionssimulation import global_maxima
from functionssimulation import integrate_chirp
from functionssimulation import find_periods
from myfunctions import default_settings, load_cell
from axes import labelaxes_params,label_axes
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import string
def plot_single_cells(ax, colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B'], data = ['2019-10-21-aa-invivo-1','2019-11-18-af-invivo-1','2019-10-28-aj-invivo-1'], var = '05'):
y_sum = [[]] * len(data)
axis = {}
baseline = pd.read_pickle('data_baseline.pkl')
for ee, e in enumerate([var]):
#d = data_all[data_all['dataset'] == ]
#d = load_cell(data[0], fname='singlecellexample5_smoothed'+data[0], big_file='beat_results_smoothed')
big_file = 'beat_results_smoothed'
data_all = pd.read_pickle(big_file + '.pkl')
d = data_all[data_all['dataset'] == data[0]]
#d = data_all[data_all['dataset'] == set]
try:
eod = d['eodf'].iloc[0]
except:
embed()
#embed()
x = d['delta_f'] / d['eodf'] + 1
xx = d['delta_f']
y = d['result_frequency_' + e]
y2 = d['result_amplitude_max_' + e]
y_sum[0] = np.nanmax(y)
axis[1] = plt.subplot(ax[0])
axis[1].plot(x, y, zorder = 1,color=colors[0])
axis[1].set_ylabel('AF [Hz]')
axis[1].set_xlim([0, 4])
labels = [item.get_text() for item in axis[1].get_xticklabels()]
empty_string_labels = [''] * len(labels)
axis[1].set_xticklabels(empty_string_labels)
b = baseline[baseline['dataset'] == data[0]]['spike_rate']
axis[1].axhline(y=b.iloc[0], color='grey', linestyle='dotted')
#axis[2] = host_subplot(ax[1], axes_class=AA.Axes)
axis[2] = plt.subplot(ax[1])
#aixs[3] = axis[2].twinx()
#par2 = axis[2].twinx()
#host = host_subplot(ax[1], axes_class=AA.Axes)
#host.spines['right'].set_visible(False)
#host.spines['top'].set_visible(False)
#axis[2] = host.twiny()
axis[2].plot(xx, y2, label="Beats [Hz]", zorder = 2,color=colors[0])
axis[2].set_ylabel('Modulation ')
axis[1].spines['right'].set_visible(False)
axis[1].spines['top'].set_visible(False)
axis[2].spines['right'].set_visible(False)
axis[2].spines['top'].set_visible(False)
axis[1].set_xlim([0, np.max(x)])
axis[2].set_xlim([-eod, np.max(xx)])
mod_tob = d['result_toblerone_max_' + e]
tob_col = 'black'
tob_lw = 0.8
axis[2].plot(xx, mod_tob, color=tob_col, linestyle='--', linewidth=tob_lw)
nr_size = 10
axis[2].text(-0.02, 1.1, string.ascii_uppercase[4],
transform=axis[2].transAxes,
size=nr_size, weight='bold')
axis[1].text(-0.02, 1.1, string.ascii_uppercase[3],
transform=axis[1].transAxes,
size=nr_size, weight='bold')
#axis[4] = axis[2].twiny()
axis[3] = axis[2].twiny()
axis[3].set_xlabel('EOD multiples')
offset = -40
#new_fixed_axis = axis[3].get_grid_helper().new_fixed_axis
#axis[3].axis["bottom"] = new_fixed_axis(loc="bottom", axes=axis[3],
# offset=(0,offset))
axis[3].xaxis.set_ticks_position("bottom")
axis[3].xaxis.set_label_position("bottom")
axis[3].spines["bottom"].set_position(("axes", -0.9))
axis[3].spines['right'].set_visible(False)
axis[3].spines['top'].set_visible(False)
#axis[3].axis["bottom"].toggle(all=True)
axis[2].set_xlabel("Difference frequency [Hz]")
#par2.set_xlim([np.min(xx), np.max(xx)])
axis[3].set_xlim([0, np.max(x)])
#p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
#p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = axis[3].plot(x, y2,color = 'grey',zorder = 1)
#p3, = axis[4].plot(x, y2, color='grey', zorder=1)
#embed()
axis[2].set_xticks(np.arange(-eod,np.max(xx),eod/2))
#ax['corr'].set_yticks(np.arange(eod_fe[0] - eod_fr, eod_fe[-1] - eod_fr, eod_fr / 2))
axis[2].set_ylim([0, np.nanmax(y_sum)])
plt.subplots_adjust(wspace = 0.4,left = 0.17, right = 0.96,bottom = 0.2)
return axis,y2
def plot_beat_corr(ax,lower,beat_corr_col = 'red',df_col = 'pink',ax_nr = 3,multiple = 3):
eod_fr = 500
eod_fe = np.arange(0, eod_fr * multiple, 5)
beats = eod_fe - eod_fr
beat_corr = eod_fe % eod_fr
beat_corr[beat_corr > eod_fr / 2] = eod_fr - beat_corr[beat_corr > eod_fr / 2]
#gs0 = gridspec.GridSpec(3, 1, height_ratios=[4, 1, 1], hspace=0.7)
#plt.figure(figsize=(4.5, 6))
style = 'dotted'
color_v = 'black'
color_b = 'silver'
# plt.subplot(3,1,1)
ax['corr'] = plt.subplot(lower[ax_nr])
np.max(beats) / eod_fr
ax['corr'].set_xticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr+1))
ax['corr'].set_yticks(np.arange((eod_fe[0]-eod_fr)/eod_fr+1, (eod_fe[-1]-eod_fr)/eod_fr+1,(eod_fr/2)/eod_fr+1))
ax['corr'].set_xticks(np.arange(0,10,0.5))
ax['corr'].set_yticks(np.arange(0, 10, 0.5))
# plt.axvline(x = -250, Linestyle = style,color = color_v)
# plt.axvline(x = 250, Linestyle = style,color = color_v)
# plt.axvline(x = 750, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style)
# plt.subplot(3,1,2)
plt.xlabel('Beats [Hz]')
plt.ylabel('Difference frequency [Hz]')
#plt.subplot(gs0[1])
if beat_corr_col != 'no':
plt.plot(beats/eod_fr+1, beat_corr/(eod_fr+1), color=beat_corr_col, alpha = 0.7)
plt.ylim([0,np.max(beat_corr/(eod_fr+1))*1.4])
plt.xlim([(beats/eod_fr+1)[0],(beats/eod_fr+1)[-1]])
if df_col != 'no':
plt.plot(beats/eod_fr+1, np.abs(beats)/(eod_fr+1), color=df_col, alpha = 0.7)
#plt.axvline(x=-250, Linestyle=style, color=color_v)
#plt.axvline(x=250, Linestyle=style, color=color_v)
#plt.axvline(x=750, Linestyle=style, color=color_v)
plt.xlabel('EOD adjusted beat [Hz]')
ax['corr'] .spines['right'].set_visible(False)
ax['corr'] .spines['top'].set_visible(False)
ax['corr'] .spines['left'].set_visible(True)
ax['corr'] .spines['bottom'].set_visible(True)
# plt.axvline(x = 1250, Linestyle = style,color = color_v)
# plt.axvline(x = 1500, Linestyle = style,color = color_v)
mult = np.array(beats) / eod_fr + 1
# plt.subplot(3,1,3)
plt.xlabel('EOD multiples')
plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
plt.grid()
#plt.subplot(gs0[2])
#plt.plot(mult, beat_corr, color=color_b)
# plt.axvline(x = 0, Linestyle = style)
#plt.axvline(x=0.5, Linestyle=style, color=color_v)
# plt.axvline(x = 1, Linestyle = style)
#plt.axvline(x=1.5, Linestyle=style, color=color_v)
#plt.axvline(x=2.5, Linestyle=style, color=color_v)
#plt.xlabel('EOD multiples')
#plt.ylabel('EOD adj. beat [Hz]', fontsize = 10)
return ax
def try_resort_automatically():
diffs = np.diff(dfs)
fast_sampling = dfs[np.concatenate([np.array([True]),diffs <21])]
second_derivative = np.diff(np.diff(fast_sampling))
first_index = np.concatenate([np.array([False]),second_derivative <0])
second_index = np.concatenate([second_derivative > 0,np.array([False])])
remaining = fast_sampling[np.concatenate([np.array([True]),second_derivative == 0, np.array([True])])]
first = np.arange(0,len(first_index),1)[first_index]
second = np.arange(0, len(second_index), 1)[second_index]-1
residual = []
indeces = []
for i in range(len(first)):
index = np.arange(first[i],second[i],2)
index2 = np.arange(first[i], second[i], 1)
indeces.append(index2)
residual.append(fast_sampling[index])#first[i]:second[i]:2
indeces = np.concatenate(indeces)
remaining = fast_sampling[~indeces]
residual = np.concatenate(residual)
new_dfs = np.sort(np.concatenate([residual, remaining]))
def gap_subplot(ax, dfs, pos_left = 0, pos_right = 1, xl = -0.5, xr = 30, ax_name = 'between'):
ax[ax_name] = plt.subplot(grid[pos_left, pos_right])
ax[ax_name].spines['right'].set_visible(False)
ax[ax_name].spines['top'].set_visible(False)
ax[ax_name].spines['left'].set_visible(False)
ax[ax_name].spines['bottom'].set_visible(False)
ax[ax_name].set_ylim([np.min(dfs), np.max(dfs)])
ax[ax_name].set_xlim([xl,xr])
ax[ax_name].set_xticks([])
ax[ax_name].set_yticks([])
ax[ax_name].set_ylim(ax[ax_name].get_ylim()[::-1])
return ax[ax_name]
def morane_plot(ax, dfs, grid, d,nr_size = 11, ll = 0.1, ul = 0.3, ax_name = 'scatter',gl = 0, gr =0):
ax[ax_name] = plt.subplot(grid[gl,gr])
ax[ax_name].spines['right'].set_visible(False)
ax[ax_name].spines['top'].set_visible(False)
counter = 0
new_dfs = np.concatenate([dfs[0:25], dfs[25:40:2], dfs[40:53:2], dfs[54:-1]])
for i in range(len(new_dfs)):
spikes = d[d['df'] == new_dfs[i]]['spike_times']
counter += 1
transformed_spikes = spikes.iloc[0]-spikes.iloc[0][0]
used_spikes = transformed_spikes[transformed_spikes>ll]
used_spikes = used_spikes[used_spikes<ul]*1000
ax['scatter'].scatter(used_spikes,np.ones(len(used_spikes))*new_dfs[i],s = 0.2,color = 'silver')
ax[ax_name].set_ylim([np.min(dfs),np.max(dfs)])
ax[ax_name].set_xlim([ll*1000,ul*1000])
ax[ax_name].set_ylabel('Difference frequency [Hz]')
ax[ax_name].set_xlabel('Time [ms]')
ax[ax_name].set_ylim(ax['scatter'].get_ylim()[::-1])
ax[ax_name].text(-0.1, 1.025, string.ascii_uppercase[0], transform=ax['scatter'].transAxes,
size= nr_size, weight='bold')
return ax[ax_name]
def examples(d,dfs,ax, axis, eod,grid, lg = 0, rg = 2, var = '05', log = 'log'):
new_dfs = np.concatenate([dfs[0:25], dfs[25:40:2], dfs[40:53:2], dfs[54:-1]])
first = ['crimson', 'lightcoral', 'darkviolet']
third = ['orange', 'orangered', 'darkred']
fourth = ['DarkGreen', 'LimeGreen', 'YellowGreen']
fifth = ['lightblue','blue','navy']
colors = np.concatenate([fourth, third, first, fifth])
low_nr = 60
from_middle = 45 # 20
example_df = [low_nr - eod, eod / 2 - from_middle - eod, eod - low_nr - eod, low_nr, eod / 2 - from_middle,
low_nr + eod]
example_df = [low_nr - eod, eod / 2 - from_middle - eod,
low_nr, eod / 2 - from_middle,
low_nr + eod, eod / 2 - from_middle + eod,
low_nr + eod * 2, eod / 2 - from_middle + eod * 2,
low_nr + eod * 3, eod / 2 - from_middle + eod * 3,
low_nr + eod * 4 + eod * 4]
#embed()
dfs_all = [[]]*len(example_df)
for i in range(len(example_df)):
df = new_dfs[np.argmin(np.abs(new_dfs - example_df[i]))]
dfs_all[i] = [df]
example_df = np.unique(dfs_all)
max_p = np.zeros(len(example_df))
min_p = np.zeros(len(example_df))
rows = len(example_df)
cols = 1
power_raster = gridspec.GridSpecFromSubplotSpec(rows, cols,
subplot_spec=grid[lg, rg], wspace=0.05, hspace=0.3)
for i in range(len(example_df)):
power = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1, 1.7], hspace=0.2, wspace=0.2,
subplot_spec=power_raster[i])
ax_nr = 0
ax['scatter_small' + str(i)] = plt.subplot(power[ax_nr])
eod_fr = np.array(500)
eod_fr = np.array(eod)
eod_fe = np.array([example_df[i] + eod])
e = 0
factor = 200
sampling = 100000
minus_bef = -250
plus_bef = -200
f_max, lims, _ = single_stim(ax, [colors[i]], 1, 1, eod_fr, eod_fe, e, power, delta_t=0.001, add='no',
minus_bef=minus_bef, plus_bef=plus_bef, sampling=sampling,
col_basic='silver', col_hline='no', labels=False, a_fr=1, ax_nr=ax_nr,
phase_zero=[0], shift_phase=0, df_col='no', beat_corr_col='no', size=[120],
a_fe=0.8)
ax['between'].scatter(0.12, example_df[i], zorder=2, s=25, marker='<', color=colors[i])
ax['between'].scatter(0.12, example_df[i], zorder=2, s=25, marker='<', color=colors[i])
ll = np.abs(plus_bef)
ul = np.abs(minus_bef)
#df = new_dfs[np.argmin(np.abs(new_dfs - example_df[i]))]
df = example_df[i]
spikes = d[d['df'] == df]['spike_times']
tranformed_spikes = spikes.iloc[0] * 1000 - spikes.iloc[0][0] * 1000
used_spikes = tranformed_spikes[tranformed_spikes > ll]
used_spikes = used_spikes[used_spikes < ul]
ax['power' + str(i)] = plt.subplot(power[1])
if len(used_spikes)>0:
used_spikes = used_spikes - used_spikes[0]
ax['scatter_small' + str(i)].scatter((used_spikes), np.ones(len(used_spikes)) * -2, zorder=2, s=2, marker='|',
color='black') # color = colors[i]
ax['scatter_small' + str(i)].set_ylim([-2.5, 2.2])
nfft = 4096 #
sampling_rate = 40000
# embed()
pp = [[]] * len(spikes)
for s in range(len(spikes)):
new_spikes = list(map(int, (spikes.iloc[s] - spikes.iloc[s][0]) * sampling_rate))
array = np.zeros(new_spikes[-1] + 2)
array[new_spikes] = 1
array = array * sampling_rate
if var == '05':
window05 = 0.0005 * sampling_rate
array = gaussian_filter(array, sigma=window05)
pp[s], f = ml.psd(array - np.mean(array), Fs=sampling_rate, NFFT=nfft, noverlap=nfft / 2)
p = np.mean(pp, axis=0)
diff = d['eodf'].iloc[0] * (df / d['eodf'].iloc[0] - int(df / d['eodf'].iloc[0]))
if diff > d['eodf'].iloc[0] * 0.5:
diff = diff - d['eodf'].iloc[0] * 0.5
if log == 'log':
ref = (np.max(p))
p = 10 * np.log10(p / ref)
plt.plot(f, p, zorder=1, color=main_color)
max_p[i] = np.max(p)
min_p[i] = np.min(p)
ax['power' + str(i)].scatter(f[np.argmax(p[f < 0.5 * eod])], max(p[f < 0.5 * eod]), zorder=2, color=colors[i],
s=25)
ax['power' + str(i)].scatter(f[f == f[np.argmin(np.abs(f - eod))]],
p[f == f[np.argmin(np.abs(f - eod))]] * 0.90, zorder=2,
s=25, color='darkgrey', edgecolor='black')
ax['power' + str(i)].axvline(x=eod / 2, color='black', linestyle='dashed', lw=0.5)
plt.xlim([-40, 1600])
axis[3].scatter(example_df[i] / (eod) + 1, np.sqrt(np.max(p[f < 0.5 * eod]) * np.abs(f[0] - f[1])), zorder=3,
s=20, marker='o', color=colors[i])
axis[1].scatter(example_df[i] / (eod) + 1, f[np.argmax(p[f < 0.5 * eod])], zorder=2, s=20, marker='o',
color=colors[i])
#embed()
if i != rows - 1:
labels = [item.get_text() for item in ax['scatter_small' + str(i)].get_xticklabels()]
empty_string_labels = [''] * len(labels)
ax['scatter_small' + str(i)].set_xticklabels(empty_string_labels)
labels = [item.get_text() for item in ax['power' + str(i)].get_xticklabels()]
empty_string_labels = [''] * len(labels)
ax['power' + str(i)].set_xticklabels(empty_string_labels)
else:
ax['power' + str(i)].set_xlabel('Frequency [Hz]')
ax['scatter_small' + str(i)].set_xlabel('Time [ms]')
ax['power' + str(i)].set_yticks([])
ax['power' + str(i)].spines['left'].set_visible(False)
ax['scatter_small' + str(i)].spines['left'].set_visible(False)
ax['scatter_small' + str(i)].set_yticks([])
ax['power' + str(i)].spines['right'].set_visible(False)
ax['power' + str(i)].spines['top'].set_visible(False)
ax['scatter_small' + str(i)].spines['right'].set_visible(False)
ax['scatter_small' + str(i)].spines['top'].set_visible(False)
for i in range(len(example_df)):
if log == 'log':
ax['power' + str(i)].set_ylim([np.min(min_p), 4])
else:
ax['power' + str(i)].set_ylim([0, np.max(max_p)])
ax['power' + str(0)].text(-0.1, 1.1, string.ascii_uppercase[2], transform=ax['power' + str(0)].transAxes,
size=nr_size, weight='bold')
ax['scatter_small' + str(0)].text(-0.1, 1.1, string.ascii_uppercase[1],
transform=ax['scatter_small' + str(0)].transAxes,
size=nr_size, weight='bold')
return ax, axis
def define_dataset():
day1 = ['2019-05-07-ak-invivo-1', '2019-05-07-an-invivo-1', '2019-05-07-aq-invivo-1', '2019-05-07-au-invivo-1',
'2019-05-07-av-invivo-1', '2019-05-07-ax-invivo-1', '2019-05-07-az-invivo-1', '2019-05-07-bb-invivo-1',
'2019-05-07-bd-invivo-1', '2019-05-07-bh-invivo-1', '2019-05-07-bl-invivo-1', '2019-05-07-bp-invivo-1',
'2019-05-07-bt-invivo-1', '2019-05-07-by-invivo-1', '2019-05-07-ca-invivo-1',
'2019-05-07-cc-invivo-1', ] # 16
'''
'''
# 09-23 # nr 6
day2 = ['2019-09-23-aj-invivo-1', '2019-09-23-an-invivo-1','2019-09-23-ah-invivo-1']
# 10-21: # nr 11
day3 = [ '2019-10-21-ah-invivo-1', '2019-10-21-ai-invivo-1',
'2019-10-21-aj-invivo-1','2019-10-21-al-invivo-1', '2019-10-21-am-invivo-1',
'2019-10-21-an-invivo-1',
'2019-10-21-ar-invivo-1', '2019-10-21-au-invivo-1']
# 10-28 #8
day4 = ['2019-10-28-aa-invivo-1', '2019-10-28-ac-invivo-1', '2019-10-28-af-invivo-1', \
'2019-10-28-ag-invivo-1', '2019-10-28-ah-invivo-1', '2019-10-28-ai-invivo-1', \
'2019-10-28-aj-invivo-1', '2019-10-28-ak-invivo-1']
# 11-08 #6
day5 = ['2019-11-08-aa-invivo-1', '2019-11-08-ab-invivo-1', '2019-11-08-ac-invivo-1',
'2019-11-08-ae-invivo-1', '2019-11-08-ag-invivo-1', '2019-11-08-ah-invivo-1']
# 11-13 #4
day6 = [ '2019-11-13-ad-invivo-1', '2019-11-13-ae-invivo-1']
# 11-18 #10
day7 = ['2019-11-18-aa-invivo-1', '2019-11-18-ab-invivo-1', '2019-11-18-ac-invivo-1',
'2019-11-18-ad-invivo-1', '2019-11-18-ae-invivo-1', '2019-11-18-af-invivo-1',
'2019-11-18-ai-invivo-1', '2019-11-18-aj-invivo-1', '2019-11-18-al-invivo-1', '2019-11-18-am-invivo-1']
day8 = ['2019-10-21-ak-invivo-1','2019-10-21-ad-invivo-1','2019-11-13-ab-invivo-1','2019-10-21-aa-invivo-1', '2019-09-23-ac-invivo-1',
'2019-09-23-ad-invivo-1', '2019-11-13-aa-invivo-1',]
exclude = ['2019-09-23-ak-invivo-1']
dataset = np.concatenate([day1, day2, day3, day4, day5, day6, day7,day8])
return dataset,day1, day2, day3, day4, day5, day6, day7
if __name__ == "__main__":
data = ['2019-09-23-ad-invivo-1'] # data = ['2019-10-21-aa-invivo-1']
data = ['2019-11-18-ac-invivo-1']# '2019-10-28-aj-invivo-1'
data = ['2019-10-28-aj-invivo-1']
datasets, day1, day2, day3, day4, day5, day6, day7 = define_dataset()
for d in datasets:
data = [d]
#embed()
default_settings(data,intermediate_width = 6.29,intermediate_length = 8, ts = 6, ls = 9, fs = 9)
fig = plt.figure()
ax = {}
nr_size = 10
main_color = 'darkgrey'
var = 'original'#var = '05'
#var = '05'
#d = load_cell(data[0], fname='singlecellexample5_data_beat'+data[0], big_file='data_beat',new = False)
data_all = pd.read_pickle('data_beat' +'.pkl')
d = data_all[data_all['dataset'] == data[0]]
eod = d['eodf'].iloc[0]
dfs = np.unique(d['df'])
x = d['df'] / d['eodf'] + 1
grid = gridspec.GridSpec(2, 3, wspace=0.0, height_ratios=[6, 2], width_ratios=[1,0.3,3], hspace=0.2)
#first subplot
ax['scatter'] = morane_plot(ax, dfs, grid, d, nr_size=nr_size, ll=0.1, ul=0.3, ax_name='scatter', gl=0, gr=0)
#gap between subplots
ax['between'] = gap_subplot(ax,dfs, pos_left=0, pos_right=1, xl=-0.5, xr=30, ax_name='between')
#summary picture
axis = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=grid[1,:], wspace=0, hspace=0.5)
#var =
axis,y2 = plot_single_cells(axis, colors = [main_color], data = data,var = 'original')
#examples
log = 'notlog'
ax, axis = examples(d,dfs,ax, axis, eod, grid, lg=0, rg=2, var = var, log = log)
plt.subplots_adjust(left = 0.11, bottom = 0.18, top = 0.94)
#plt.savefig('singlecellexample5.pdf')
#plt.savefig('../highbeats_pdf/singlecellexample5.pdf')
#plt.savefig('singlecellexample5'+data[0]+'.pdf')
plt.savefig('../highbeats_pdf/singlecellexamples5_all/singlecellexample5'+data[0]+'orig05'+log+'.pdf')
plt.savefig('../highbeats_pdf/singlecellexamples5_all/singlecellexample5' + data[0] + 'orig05'+log+'.png')
#plt.show()
plt.close()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 KiB

After

Width:  |  Height:  |  Size: 107 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 KiB

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 KiB

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

After

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 135 KiB

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 KiB

After

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 159 KiB

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 KiB

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

After

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 154 KiB

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

After

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 152 KiB

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

After

Width:  |  Height:  |  Size: 136 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

After

Width:  |  Height:  |  Size: 142 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 69 KiB

Some files were not shown because too many files have changed in this diff Show More