diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97950d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ + +*.pdf +*.png +*.pdf +*.pkl +*.csv +*.npy diff --git a/MPFmodulation.py b/MPFmodulation.py new file mode 100644 index 0000000..45b811c --- /dev/null +++ b/MPFmodulation.py @@ -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() + diff --git a/MPFmodulation_tob.pdf b/MPFmodulation_tob.pdf index 9a0a250..71b1704 100644 Binary files a/MPFmodulation_tob.pdf and b/MPFmodulation_tob.pdf differ diff --git a/MPFmodulation_tob.py b/MPFmodulation_tob.py new file mode 100644 index 0000000..cabb268 --- /dev/null +++ b/MPFmodulation_tob.py @@ -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() + diff --git a/MPFmodulation_tobsimulation.pdf b/MPFmodulation_tobsimulation.pdf index 64530fe..f00da12 100644 Binary files a/MPFmodulation_tobsimulation.pdf and b/MPFmodulation_tobsimulation.pdf differ diff --git a/MPFmodulation_tobsinz.pdf b/MPFmodulation_tobsinz.pdf index 0cecc5b..006b942 100644 Binary files a/MPFmodulation_tobsinz.pdf and b/MPFmodulation_tobsinz.pdf differ diff --git a/MPFmodulation_trans.py b/MPFmodulation_trans.py new file mode 100644 index 0000000..b825fd9 --- /dev/null +++ b/MPFmodulation_trans.py @@ -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() + diff --git a/MPFmodulation_trans_single.py b/MPFmodulation_trans_single.py new file mode 100644 index 0000000..4068d3d --- /dev/null +++ b/MPFmodulation_trans_single.py @@ -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() + diff --git a/beatcorrplot.py b/beatcorrplot.py new file mode 100644 index 0000000..769d8b2 --- /dev/null +++ b/beatcorrplot.py @@ -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) \ No newline at end of file diff --git a/compare.py b/compare.py new file mode 100644 index 0000000..f2e6709 --- /dev/null +++ b/compare.py @@ -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() + diff --git a/differentcells.py b/differentcells.py new file mode 100644 index 0000000..5b6a4d7 --- /dev/null +++ b/differentcells.py @@ -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() diff --git a/differentcells_trans.py b/differentcells_trans.py new file mode 100644 index 0000000..ef38c1a --- /dev/null +++ b/differentcells_trans.py @@ -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() diff --git a/functionssimulation.py b/functionssimulation.py new file mode 100644 index 0000000..2539328 --- /dev/null +++ b/functionssimulation.py @@ -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 + diff --git a/introamtuning.py b/introamtuning.py new file mode 100644 index 0000000..57a3e8f --- /dev/null +++ b/introamtuning.py @@ -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() \ No newline at end of file diff --git a/isi_model.py b/isi_model.py new file mode 100644 index 0000000..a56bb26 --- /dev/null +++ b/isi_model.py @@ -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() diff --git a/main.py b/main.py new file mode 100644 index 0000000..55a298e --- /dev/null +++ b/main.py @@ -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() diff --git a/model.py b/model.py new file mode 100644 index 0000000..0beb2e8 --- /dev/null +++ b/model.py @@ -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) diff --git a/model_max.py b/model_max.py new file mode 100644 index 0000000..ecd320c --- /dev/null +++ b/model_max.py @@ -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) diff --git a/model_sinz.py b/model_sinz.py new file mode 100644 index 0000000..98ae405 --- /dev/null +++ b/model_sinz.py @@ -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) diff --git a/modell_all_cell.py b/modell_all_cell.py new file mode 100644 index 0000000..8f66ef1 --- /dev/null +++ b/modell_all_cell.py @@ -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() diff --git a/modell_all_cell_sinz.py b/modell_all_cell_sinz.py new file mode 100644 index 0000000..4410654 --- /dev/null +++ b/modell_all_cell_sinz.py @@ -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() diff --git a/modell_single_cell.py b/modell_single_cell.py new file mode 100644 index 0000000..2cd2162 --- /dev/null +++ b/modell_single_cell.py @@ -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() diff --git a/modell_single_cell_max.py b/modell_single_cell_max.py new file mode 100644 index 0000000..f8d6621 --- /dev/null +++ b/modell_single_cell_max.py @@ -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() diff --git a/myfunctions.py b/myfunctions.py new file mode 100644 index 0000000..3ab6532 --- /dev/null +++ b/myfunctions.py @@ -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]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='-') \ No newline at end of file diff --git a/numerical.py b/numerical.py new file mode 100644 index 0000000..869bf38 --- /dev/null +++ b/numerical.py @@ -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) diff --git a/numerical_compar.py b/numerical_compar.py new file mode 100644 index 0000000..04b6557 --- /dev/null +++ b/numerical_compar.py @@ -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) diff --git a/numerical_compar_both.py b/numerical_compar_both.py new file mode 100644 index 0000000..5a3b009 --- /dev/null +++ b/numerical_compar_both.py @@ -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) diff --git a/numerical_compar_modulation.py b/numerical_compar_modulation.py new file mode 100644 index 0000000..d1f5ff1 --- /dev/null +++ b/numerical_compar_modulation.py @@ -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) diff --git a/numerical_test.py b/numerical_test.py new file mode 100644 index 0000000..654cc7e --- /dev/null +++ b/numerical_test.py @@ -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() \ No newline at end of file diff --git a/numerical_test_bar.py b/numerical_test_bar.py new file mode 100644 index 0000000..6c36ad6 --- /dev/null +++ b/numerical_test_bar.py @@ -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() \ No newline at end of file diff --git a/plot_eod_chirp.py b/plot_eod_chirp.py new file mode 100644 index 0000000..c124c46 --- /dev/null +++ b/plot_eod_chirp.py @@ -0,0 +1,2449 @@ +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 fish_indirect(delta_t, amplitude, size, beat, phase_zero, sampling, show_figure = False): + for a in range(len(amplitude)): + for s in range(len(size)): + for p in range(len(phase_zero)): + for b in range(len(beat)): + + # sigma calculation + sigma = delta_t /math.sqrt((2*math.log(10))) # width of the chirp + + # time calculation + #if np.abs(1 / beat[b]) * 10 < 6 * delta_t: + time = np.arange(-3 * delta_t, 3 * delta_t, 1 / sampling) + #else: + # time = np.arange(-np.abs(1/beat[b]) * 3, abs(1/beat[b]) * 3, 1/ sampling_frequency) + + # frequency calculation + chirp = size[s] * np.exp((-(time**2)/(2*sigma**2))) + frequency = beat[b] + chirp + + # phase calculation + I1 = [] + for i in range(len(time)): + y1 = ((np.pi**0.5)/2)*math.erf(time[i]/sigma) - ((np.pi**0.5)/2)*math.erf(-np.inf) + I1.append(y1) + phase = 2 * np.pi * beat[b] * time + 2 * np.pi * size[s] * sigma * np.array(I1) + phase_zero[p] + + # am calculation + am = amplitude[a] * np.cos(phase) + # plot + plt.plot(time*1000, am, label='Amplitude Modulation', color='red', linewidth=2) + plt.axvline(x=-7.5, color='black', linewidth=1, linestyle='-') + plt.axvline(x=7.5, color='black', linewidth=1, linestyle='-') + plt.xlabel('time [ms]') + plt.ylabel('AM [mV]') + plt.title('beat(%5.2f' % (beat[b]) + 'Hz) ' + 'size(%5.2f' % (size[s]) + 'Hz) ' + 'phase(%5.2f' % (phase_zero[p]) + ') ' + 'amplitude(%1.2f' % (amplitude[a]) + ')') + #plt.show() + plt.savefig(fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude[a]) +')_size(%5.2f' % (size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_beat(%5.2f' % (beat[b]) + ')_AM_fish%5.2f' % sampling + '.png') + if show_figure: + plt.show() + else: + plt.close() + +def stimulus_indirect(delta_t,amplitude ,size, beat, phase_zero, show_figure = False): + for a in range(len(amplitude)): + for s in range(len(size)): + for p in range(len(phase_zero)): + for b in range(len(beat)): + # sigma calculation + sigma = delta_t /math.sqrt((2*math.log(10))) # width of the chirp + + # time assignment + if np.abs(1 / beat[b]) * 2 < 3 * delta_t: + time = np.arange(-1.5 * delta_t, 1.5 * delta_t, 1 / 40000) + else: + time = np.arange(-np.abs(1/beat[b]) * 1, abs(1/beat[b]) * 1, 1/40000) + + # frequency calculation + chirp = size[s] * np.exp((-(time**2)/(2*sigma**2))) + frequency = beat[b] + chirp + + # phase calculation + I1 = [] + for i in range(len(time)): + y1 = ((np.pi**0.5)/2)*math.erf(time[i]/sigma) - ((np.pi**0.5)/2)*math.erf(-np.inf) + I1.append(y1) + phase = 2 * np.pi * beat[b] * time + 2 * np.pi * size[s] * sigma * np.array(I1) + phase_zero[p] + + # am calculation + am = amplitude[a] * np.cos(phase) + plt.plot(time*1000, am, label='Amplitude Modulation', color='red', linewidth=2) + plt.axvline(x=-7.5, color='black', linewidth=1, linestyle='-') + plt.axvline(x=7.5, color='black', linewidth=1, linestyle='-') + plt.xlabel('time [ms]') + plt.ylabel('AM [mV]') + plt.title('beat(%5.2f' % (beat[b]) + 'Hz) ' + 'size(%5.2f' % (size[s]) + 'Hz) '+ 'phase(%5.2f' % (phase_zero[p]) + ') '+ 'amplitude(%1.2f' % (amplitude[a]) + ')') + plt.savefig(fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude[a]) +')_size(%5.2f' % (size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_beat(%5.2f' % (beat[b]) + ')_AM_stimulus.png') + if show_figure: + plt.show() + else: + plt.close() + +def create_distance(conv_chirp,conv_beat,a): + dm_wo_max = np.sqrt(np.mean(np.transpose( + (np.transpose(conv_chirp) - np.mean(conv_chirp, a) - np.transpose(conv_beat) + np.mean(conv_beat, a)) ** 2), a)) + range_chirp = np.max(conv_chirp,a)-np.min(conv_chirp,a)/np.sqrt(2) + range_beat = np.max(conv_beat,a)-np.min(conv_beat,a)/np.sqrt(2) + dm_metz = dm_wo_max/np.max(np.transpose([range_chirp,range_beat]),a) + dm_wo_mean2 = np.sqrt(np.mean((conv_chirp - conv_beat) ** 2,a)) / np.max(np.transpose([np.max(conv_chirp,a)-np.min(conv_chirp,a)/np.sqrt(2),np.max(conv_beat,a)-np.min(conv_beat,a)/np.sqrt(2)]),a) + #dm_wo_mean2_max = np.sqrt(np.mean((conv_chirp - conv_beat) ** 2,a)) + dm_wo_mean2_max = mean_euc_d(conv_beat, conv_chirp, a) + d_euclidean = euc_d(conv_beat, conv_chirp, a) + #dst = distance.euclidean(conv_beat, conv_chirp) + return dm_metz, dm_wo_mean2,dm_wo_max,dm_wo_mean2_max,range_chirp,range_beat, d_euclidean + +def snippets2(peaks_interp, maxima_interp, am_indirect, am, d, period_shift, period_distance, middle_am, eod_convolved_up, middle_conv, delta_t, sampling, deviation, time, eod_conv_downsampled): + conv_chirp = eod_convolved_up[ + middle_conv - int(0.5 * period_distance * (sampling)):middle_conv + int(0.5 * period_distance * (sampling))] + + peaks_interp_chirp = peaks_interp[ + 3*deviation[d]+middle_conv - int(0.5 * period_distance * (sampling)):3 * deviation[d] + middle_conv + int(0.5 * period_distance * (sampling))] + maxima_interp_chirp = peaks_interp[ + 3*deviation[d]+middle_conv - int(0.5 * period_distance * (sampling)):3 * deviation[d] + middle_conv + int(0.5 * period_distance * (sampling))] + am_indirect_chirp = am_indirect[ + middle_conv - int(0.5 * period_distance * (sampling)):middle_conv + int(0.5 * period_distance * (sampling))] + + time_chirp = time[middle_conv - int(0.5 * period_distance * (sampling)):middle_conv + int( + 0.5 * period_distance * (sampling))] + + am_fish_chirp = am[middle_am - int(0.5 * period_distance * (eod_fr)):middle_am + int( + 0.5 * period_distance * (eod_fr))] + conv_chirp_corr = eod_conv_downsampled[middle_am - int(0.5 * period_distance * (eod_fr)):middle_am + int( + 0.5 * period_distance * (eod_fr))] + + shift = int(period_shift * (sampling) * 2) + shift_fish = int(period_shift * (eod_fr) * 2) + + am_fish_beat = am[middle_am - int(0.5 * period_distance * (eod_fr)) - shift_fish:middle_am + int( + 0.5 * period_distance * (eod_fr)) - shift_fish] + + conv_beat = eod_convolved_up[middle_conv - int(0.5 * period_distance * (sampling)) - shift:middle_conv + int( + 0.5 * period_distance * (sampling)) - shift] + peaks_interp_beat = peaks_interp[3 * deviation[d] + middle_conv - int(0.5 * period_distance * (sampling)) - shift:3 * deviation[d] + middle_conv + int( + 0.5 * period_distance * (sampling)) - shift] + maxima_interp_beat = maxima_interp[3 * deviation[d] + middle_conv - int(0.5 * period_distance * (sampling)) - shift:3 * deviation[d] + middle_conv + int( + 0.5 * period_distance * (sampling)) - shift] + am_indirect_beat = am_indirect[middle_conv - int(0.5 * period_distance * (sampling)) - shift:middle_conv + int( + 0.5 * period_distance * (sampling)) - shift] + + time_beat = time[middle_conv - int(0.5 * period_distance * (sampling)) - shift: middle_conv + int( + 0.5 * period_distance * (sampling)) - shift] + return am_fish_beat,maxima_interp_beat,peaks_interp_beat,maxima_interp_chirp, peaks_interp_chirp,am_indirect_beat,am_indirect_chirp,time_beat,conv_beat,conv_chirp_corr,am_fish_chirp,time_chirp,conv_chirp + + +def snippets(am_indirect,am, d,period, middle_am,eod_convolved_up,middle_conv,delta_t,sampling,deviation,time,eod_conv_downsampled): + if period <= delta_t*2: + conv_chirp = eod_convolved_up[ + middle_conv - int(delta_t * (sampling)):middle_conv + int( delta_t * (sampling))] + time_chirp = time[middle_conv - int( delta_t * (sampling)):middle_conv + int( + delta_t * (sampling))] + am_indirect_chirp = am_indirect[ + middle_conv - int(delta_t * (sampling)):middle_conv + int(delta_t * (sampling))] + am_fish_chirp = am[middle_am - int(delta_t * (eod_fr)):middle_am + int( delta_t * (eod_fr))] + conv_chirp_corr = eod_conv_downsampled[middle_am - int( delta_t * (eod_fr)):middle_am + int( + delta_t * (eod_fr))] + + if period == 0: + shift = int((delta_t)*3* sampling) + else: + shift = int((5 * period) * sampling)*2 + #if period == 0: + # shift = int(((delta_t + 3 * period) * sampling)) + #else: + # shift = int(((np.ceil(delta_t / period) * (period) + 3 * period) * sampling)) + conv_beat = eod_convolved_up[middle_conv - int(delta_t * (sampling)) - shift:middle_conv + int( + delta_t * (sampling)) - shift] + am_indirect_beat = am_indirect[middle_conv - int( delta_t * (sampling)) - shift:middle_conv + int( + delta_t * (sampling)) - shift] + time_beat = time[ + middle_conv - int( delta_t * (sampling)) - shift:middle_conv + int( + delta_t * (sampling)) - shift] + + else: + conv_chirp = eod_convolved_up[ + middle_conv - int( period * (sampling)):middle_conv + int( period * (sampling))] + am_indirect_chirp = am_indirect[ + middle_conv - int(period * (sampling)):middle_conv + int( period * (sampling))] + + time_chirp = time[middle_conv - int( period * (sampling)):middle_conv + int( + period * (sampling))] + + am_fish_chirp = am[middle_am - int( period * (eod_fr)):middle_am + int( + period * (eod_fr))] + conv_chirp_corr = eod_conv_downsampled[middle_am - int( period * (eod_fr)):middle_am + int( + period * (eod_fr))] + + shift = int(period * (sampling) * 2) + conv_beat = eod_convolved_up[middle_conv - int( period * (sampling)) - shift:middle_conv + int( + period * (sampling)) - shift] + am_indirect_beat = am_indirect[middle_conv - int(period * (sampling)) - shift:middle_conv + int( + period * (sampling)) - shift] + + time_beat = time[middle_conv - int( period * (sampling)) - shift: middle_conv + int( + period * (sampling)) - shift] + return am_indirect_beat,am_indirect_chirp,time_beat,conv_beat,conv_chirp_corr,am_fish_chirp,time_chirp,conv_chirp + + + +def global_maxima_array(period_fish_e,period_fish_r,eod_rectified_up): + period_length = np.max([period_fish_e, np.ones(len(period_fish_e))*len(period_fish_r)],0) + split_windows = np.linspace(period_length, len(eod_rectified_up), 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 + +def global_maxima(sampling, eod_fr, 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) + period_length = int(np.round((1/eod_fr)*sampling)) + #embed() + 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 + + + +def snippets3(skretch,peaks_interp, maxima_interp, am_indirect,am_fish, d,period, middle_am,eod_convolved_up,middle_conv,delta_t,sampling,deviation,time,eod_conv_downsampled): + #period_distance = 0.1 + period_dist = [] + if period <= delta_t*skretch: + if period == 0: + period_shift = delta_t*skretch + period_distance = delta_t*skretch + else: + period_shift = np.ceil(delta_t*skretch/period)*period + period_distance = np.ceil(delta_t*skretch/period)*period + am_fish_beat,maxima_interp_beat,peaks_interp_beat,maxima_interp_chirp, peaks_interp_chirp,am_indirect_beat,am_indirect_chirp,time_beat,conv_beat,conv_chirp_corr,am_fish_chirp,time_chirp,conv_chirp = \ + snippets2(peaks_interp, maxima_interp, am_indirect, am_fish, d, period_shift,period_distance, middle_am, eod_convolved_up, middle_conv, + delta_t, sampling, deviation, time, eod_conv_downsampled) + else: + period_shift = period + period_distance = period + am_fish_beat,maxima_interp_beat,peaks_interp_beat,maxima_interp_chirp, peaks_interp_chirp,am_indirect_beat,am_indirect_chirp,time_beat,conv_beat,conv_chirp_corr,am_fish_chirp,time_chirp,conv_chirp = \ + snippets2( + peaks_interp, maxima_interp, am_indirect, am_fish, d, period_shift,period_distance, middle_am, eod_convolved_up, middle_conv, + delta_t, sampling, deviation, time, eod_conv_downsampled) + period_dist.append(period_distance) + return period_dist, am_fish_beat,maxima_interp_beat,peaks_interp_beat,maxima_interp_chirp, peaks_interp_chirp,am_indirect_beat,am_indirect_chirp,time_beat,conv_beat,conv_chirp_corr,am_fish_chirp,time_chirp,conv_chirp + +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 + 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_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_e) + 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 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 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 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 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)) + time_cut = time[cut:-cut] + return time, time_cut, cut + + +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 + period_length = len(period_fish_r) + #embed() + sampled = np.abs(eod_overlayed_chirp[0:len(eod_overlayed_chirp):period_length]) + time_new = time[0:len(eod_overlayed_chirp):period_length] + maxima_values, maxima_index, maxima_interp = global_maxima(sampling, eod_fr, 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 time_new,sampled, 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 same_lengths(maxima_interp_b, maxima_interp_c, time_b, time_c, am_fish_c, am_fish_b, conv_c, conv_b, am_indirect_b, + am_indirect_c, peaks_interp_b, peaks_interp_c): + diff_s = len(time_b) - len(time_c) + diff_f = len(am_fish_c) - len(am_fish_b) + if len(time_b) > len(time_c): + time_b = time_b[0:len(time_c)] + conv_b = conv_b[0:len(conv_c)] + am_indirect_b = am_indirect_b[0:len(am_indirect_c)] + peaks_interp_b = peaks_interp_b[0:len(peaks_interp_c)] + maxima_interp_b = maxima_interp_b[0:len(maxima_interp_c)] + if len(time_c) > len(time_b): + time_c = time_c[0:len(time_b)] + conv_c = conv_c[0:len(conv_b)] + am_indirect_c = am_indirect_c[0:len(am_indirect_b)] + peaks_interp_c = peaks_interp_c[0:len(peaks_interp_b)] + maxima_interp_c = maxima_interp_c[0:len(maxima_interp_b)] + if len(am_fish_b) > len(am_fish_c): + am_fish_b = am_fish_b[0:len(am_fish_c)] + if len(am_fish_c) > len(am_fish_b): + am_fish_c = am_fish_c[0:len(am_fish_b)] + return diff_s, diff_f, maxima_interp_b, maxima_interp_c, time_b, time_c, am_fish_c, am_fish_b, conv_c, conv_b, am_indirect_b, am_indirect_c, peaks_interp_b, peaks_interp_c + +def power_func(restrict = [],a_fr = 1, a_fe = 0.2, eod_fr = 500, eod_fe = [500], win = 'w2', deviation_s = [0.0015], sigma = 50, sampling = 100000, deviation_ms = [1.5], beat_corr = [0], size = [120],phase_zero = [0], delta_t = 0.014,deviation_dp = [180], show_figure = True, plot_dist = False, save = False,bef_c = -2,aft_c = -1): + print('plot_eod') + results = [[]]*10 + if len(restrict) >0: + results = [[]]*len(restrict) + for d in range(len(deviation_s)): + 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') + for s in range(len(size)): + for p in range(len(phase_zero)): + for e in range(len(eod_fe)): + #embed() + left_b = [bef_c * sampling] * len(beat_corr) + right_b = [aft_c * sampling] * len(beat_corr) + 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_dp, + beat_corr, chirp = False) + thresholding = eod_overlayed_chirp*1 + thresholding[eod_overlayed_chirp<0] = 0 + squaring = eod_overlayed_chirp**2 + fifth = (thresholding)**5 + #embed() + rectified = np.abs(eod_overlayed_chirp)#'df ds','corr','corr ds', + name = ['df','conv','thresholding','rectified','squaring','cubed','fifth','global max','local max', 'samped threshold']#am_df_b,am_df_ds_b, am_corr_b, am_corr_ds_b, + var = [am_df_b,conv_b, thresholding, rectified, squaring,cubed ,fifth, maxima_interp_b,peaks_interp_b, sampled] + samp = [sampling,sampling,sampling, sampling, sampling,sampling,sampling,sampling, sampling, eod_fr]#eod_fr,sampling,eod_fr, + #['Hilbert', 'Thresholded', 'Rectified', 'Squaring', 'Threshold cubed'] + #embed() + if len(restrict) == 1: + var = [np.array(var)[restrict[0]]] + name = [np.array(name)[restrict[0]]] + samp = [np.array(samp)[restrict[0]]] + elif len(restrict)>1: + var = np.array(var)[restrict] + name = np.array(name)[restrict] + samp = np.array(samp)[restrict] + #embed() + for i in range(len(results)): + if samp[i]>1000: + nfft = int(4096 * samp[0] / 40000 * 2) + else: + nfft = 4096 + results[i],pp,f = power_parameters(results[i], var[i], samp[i],nfft,name[i],eod_fr) + results = pd.DataFrame(results) + if save: + results = pd.DataFrame(results) + try: + results.to_pickle('../data/power_simulation.pkl') + np.save('../data/Ramona/power_simulation.npy', results) + except: + a = 0 + return results + +def power_parameters(results, var,samp,nfft,name, eod_fr,max_all = 'max_all',max = 'max',integral = 'amp',plot = False,title = '',freq_half = 'f',freq_whole = 'ff'): + pp, f = ml.psd(var - np.mean(var), Fs=samp, NFFT=nfft, + noverlap=nfft / 2) # + # embed() + # print(nfft * 5 < len(var[i]): + + + if plot == True: + plt.figure() + plt.subplot(1, 2, 1) + plt.plot(var) + plt.title(name) + plt.subplot(1, 2, 2) + plt.plot(f, pp) + # plt.xlim([0,2000]) + plt.show() + # print(results) + freq_step = np.abs(f[0] - f[1]) + if type(results) != dict: + #embed() + results = {} + results['type'+title] = name + # embed() + results['result_frequency_whole'+title] = list([f[np.argmax(pp)]]) + results['result_frequency'+title] = list([f[np.argmax(pp[f < 0.5 * eod_fr])]]) + results['result_amplitude'+title] = list([np.sqrt(si.trapz(pp, f, np.abs(f[1] - f[0])))]) + results['result_amplitude_max'+title] = list([np.sqrt(np.max(pp[f < 0.5 * eod_fr]) * freq_step)]) + results['result_amplitude_max_whole'+title] = list([np.sqrt(np.max(pp) * freq_step)]) + else: + #embed() + results['result_frequency'+title].extend(list([f[np.argmax(pp[f < 0.5 * eod_fr])]])) + results['result_frequency_whole'+title].extend(list([f[np.argmax(pp)]])) + # embed() + results['result_amplitude'+title].extend(list([np.sqrt(si.trapz(pp, f, np.abs(f[1] - f[0])))])) + results['result_amplitude_max'+title].extend(list([np.sqrt(np.max(pp[f < 0.5 * eod_fr]) * freq_step)])) + results['result_amplitude_max_whole'+title].extend(list([np.sqrt(np.max(pp) * freq_step)])) + return results,pp,f + +def distances_func(disc, bef_c, aft_c, 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): + distance_metz_all = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_all = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_max_all = only_one_dict_str(d_ms, eod_fe) + distance_wo_max_all = only_one_dict_str(d_ms, eod_fe) + range_chirp_all = only_one_dict_str(d_ms, eod_fe) + range_beat_all = only_one_dict_str(d_ms, eod_fe) + corr_all = only_one_dict_str(d_ms, eod_fe) + distance_metz_all_corr = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_all_in = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_max_all_in = only_one_dict_str(d_ms, eod_fe) + distance_wo_max_all_in = only_one_dict_str(d_ms, eod_fe) + range_chirp_all_in = only_one_dict_str(d_ms, eod_fe) + range_beat_all_in = only_one_dict_str(d_ms, eod_fe) + distance_metz_all_gm = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_all_gm = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_max_all_gm = only_one_dict_str(d_ms, eod_fe) + distance_wo_max_all_gm = only_one_dict_str(d_ms, eod_fe) + distance_metz_all_lm = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_all_lm = only_one_dict_str(d_ms, eod_fe) + distance_wo_mean_max_all_lm = only_one_dict_str(d_ms, eod_fe) + distance_wo_max_all_lm = only_one_dict_str(d_ms, eod_fe) + range_chirp_all_lm = only_one_dict_str(d_ms, eod_fe) + + distance_metz_all_df_ds,distance_metz_all_corr_ds,range_beat_all_lm,range_chirp_all_gm ,range_beat_all_gm,distance_metz_all_df,distance_wo_mean_all_df,distance_wo_mean_max_all_df,distance_wo_max_all_df,range_chirp_all_df ,euc_all_in,euc_all_df,euc_all_gm,euc_all_lm,euc_all,range_beat_all_df = (only_one_dict_str(d_ms, eod_fe) for i in range(16)) + period_distance_c,period_distance_b,period_shift = ({} for i in range(3)) + for d in range(len(deviation)): + left_c, right_c, left_b, right_b, period_distance_c[d_ms[d]], period_distance_b[d_ms[d]], period_shift[d_ms[d]], 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)): + time_fish,sampled, cut, cube_b, 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_b = 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_fish,sampled,cut, cube_c, time_c, conv_c, am_corr_c, peaks_interp_c, maxima_interp_c,am_corr_ds_c, am_df_ds_c, am_df_c,eod_overlayed_chirp_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) + + + #print(len(time_c)) + #print(len(time_b)) + #diff_s[e], diff_f[e],maxima_interp_b, maxima_interp_c, time_b, time_c, am_df_c, am_df_b, conv_c,conv_b, am_corr_b, am_corr_c, peaks_interp_b, peaks_interp_c = same_lengths( + # maxima_interp_b, maxima_interp_c, time_b, time_c, am_df_c, am_df_b,conv_c, conv_b, am_corr_b, + # am_corr_c, peaks_interp_b, peaks_interp_c) + #time_b[e], conv_b[e], am_corr_b[e], peaks_interp_b[e], maxima_interp_b[e], am_df_b[e] = snip(left_c-shift[e], right_c-shift[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[e], conv_c[e], am_corr_c[e], peaks_interp_c[e], maxima_interp_c[e], am_df_c[e] = snip(left_c, right_c, e, + # sampling, deviation_s, + # d, eod_fr, a_fr, eod_fe, + # # phase_zero, p, size, s, + # sigma, a_fe, deviation, + # beat_corr) + + distance_metz_all_df_ds[d_ms[d]][e], _,_,_,_,_,_ = create_distance(np.array(am_df_ds_c), np.array(am_df_ds_b), None) + distance_metz_all_corr_ds[d_ms[d]][e], _,_,_,_,_,_ = create_distance(np.array(am_corr_ds_c), np.array(am_corr_ds_b), None) + + distance_metz_all[d_ms[d]][e], distance_wo_mean_all[d_ms[d]][e],distance_wo_max_all[d_ms[d]][e],distance_wo_mean_max_all[d_ms[d]][e],range_chirp_all[d_ms[d]][e],range_beat_all[d_ms[d]][e],euc_all[d_ms[d]][e] = create_distance(np.array(conv_c),np.array(conv_b),None) + distance_metz_all_corr[d_ms[d]][e], distance_wo_mean_all_in[d_ms[d]][e],distance_wo_max_all_in[d_ms[d]][e],distance_wo_mean_max_all_in[d_ms[d]][e],range_chirp_all_in[d_ms[d]][e],range_beat_all_in[d_ms[d]][e],euc_all_in[d_ms[d]][e] = create_distance(np.array(am_corr_c),np.array(am_corr_b),None) + distance_metz_all_gm[d_ms[d]][e], distance_wo_mean_all_gm[d_ms[d]][e],distance_wo_max_all_gm[d_ms[d]][e],distance_wo_mean_max_all_gm[d_ms[d]][e],range_chirp_all_gm[d_ms[d]][e],range_beat_all_gm[d_ms[d]][e],euc_all_gm[d_ms[d]][e] = create_distance(np.array(maxima_interp_b),np.array(maxima_interp_c),None) + distance_metz_all_lm[d_ms[d]][e], distance_wo_mean_all_lm[d_ms[d]][e], distance_wo_max_all_lm[d_ms[d]][e], distance_wo_mean_max_all_lm[d_ms[d]][e], range_chirp_all_lm[d_ms[d]][e], range_beat_all_lm[d_ms[d]][e],euc_all_lm[d_ms[d]][e] = create_distance( + np.array(peaks_interp_c), np.array(peaks_interp_b),None) + distance_metz_all_df[d_ms[d]][e], distance_wo_mean_all_df[d_ms[d]][e], distance_wo_max_all_df[d_ms[d]][e], distance_wo_mean_max_all_df[d_ms[d]][e], range_chirp_all_df[d_ms[d]][e], range_beat_all_df[d_ms[d]][e],euc_all_df[d_ms[d]][e] = create_distance( + np.array(am_df_c), np.array(am_df_b),None) + + #distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, range_chirp_all, range_beat_all, euc_all = create_distance( + # np.array(conv_c), np.array(conv_b), None) + #distance_metz_all_corr, distance_wo_mean_all_in, distance_wo_max_all_in, distance_wo_mean_max_all_in, range_chirp_all_in, range_beat_all_in, euc_all_in = create_distance( + # np.array(am_corr_c), np.array(am_corr_b), None) + #distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_max_all_gm, distance_wo_mean_max_all_gm, range_chirp_all_gm, range_beat_all_gm, euc_all_gm = create_distance( + # np.array(maxima_interp_b), np.array(maxima_interp_c), None) + #distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_max_all_lm, distance_wo_mean_max_all_lm, range_chirp_all_lm, range_beat_all_lm, euc_all_lm = create_distance( + # np.array(peaks_interp_c), np.array(peaks_interp_b), None) + #distance_metz_all_df, distance_wo_mean_all_df, distance_wo_max_all_df, distance_wo_mean_max_all_df, range_chirp_all_df, range_beat_all_df, euc_all_df = create_distance( + # np.array(am_df_b), np.array(am_df_c), None) + + + # correlation + #corr = np.corrcoef(am_chirp, conv_chirp_corr) + #corr_all.append(corr[1,0]) + if save: + np.save('../data/' + 'sim_dist3_' + str( + d_ms[d]) + 'ms' + 'beats(%1.2f' % (beats[0]) + '_%1.2f' % (beats[-1]) + '_%1.2f' % (abs(beats[1] - beats[0])) + ')' + save_n + '.npy',[distance_metz_all_df_ds,distance_metz_all_corr_ds,period_shift,period_distance_c, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_df, range_beat_all_df, range_chirp_all_df, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm, + beat_corr, distance_metz_all_df, distance_wo_mean_all_df,distance_wo_mean_max_all_df, distance_wo_max_all_df, distance_metz_all_lm, + distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, + distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, + distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, + d_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_corr, distance_wo_mean_all_in, eod_fe, eod_fr,distance_metz_all, distance_wo_mean_all, distance_wo_max_all,distance_wo_mean_max_all]) + with open('../data/' + 'sim_dist3_' + str( + d_ms[d]) + 'ms' + 'beats(%1.2f' % (beats[0]) + '_%1.2f' % (beats[-1]) + '_%1.2f' % (abs(beats[1] - beats[0])) + ')' + save_n + '.pkl', 'wb') as f: # Python 3: open(..., 'wb') + pickle.dump([distance_metz_all_df_ds,distance_metz_all_corr_ds,period_shift,period_distance_c, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_df, range_beat_all_df, range_chirp_all_df, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm, + beat_corr, distance_metz_all_df, distance_wo_mean_all_df, + distance_wo_mean_max_all_df, distance_wo_max_all_df, distance_metz_all_lm, + distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, + distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, + distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, + d_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, + distance_metz_all_corr, distance_wo_mean_all_in, eod_fe, eod_fr, + distance_metz_all, distance_wo_mean_all, distance_wo_max_all, + distance_wo_mean_max_all], f) + return distance_metz_all_df_ds,distance_metz_all_corr_ds,period_shift,period_distance_c,period_distance_b, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_df, range_beat_all_df, range_chirp_all_df, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm \ + , beat_corr, distance_metz_all_df, distance_wo_mean_all_df, distance_wo_mean_max_all_df, distance_wo_max_all_df, \ + distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, \ + distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, \ + distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, \ + d_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, \ + distance_metz_all_corr, distance_wo_mean_all_in, eod_fe, eod_fr, \ + distance_metz_all, distance_wo_mean_all, distance_wo_max_all, \ + distance_wo_mean_max_all + +def plot_dist_interactiv3(bef_c, aft_c,d, sampling, size, phase_zero, sigma, beat_corr, delta_t, a_fr, a_fe, deviation_dp, deviation_s, d_ms, eod_fe, eod_fr, share = False, show = False): + fig = plt.figure(figsize = [13,6]) + plt.title('convolved (red), indirect (magenta), loc max (green), glo max (black),sampled f (orange), period (purple)') + gs0 = gridspec.GridSpec(3, 2) + win = ['w1', 'w2', 'w3', 'w4'] + ax = {} + gs00 = gridspec.GridSpecFromSubplotSpec(7, 2, subplot_spec=gs0[3]) + ax[0] = plt.subplot(gs00[4]) + p = 0 + s = 0 + for g in range(4): + period_shift, period_distance_c, period_distance_b, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, range_beat_all_ds, range_chirp_all_ds, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm, beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, d_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, \ + distance_wo_mean_max_all = distances_func(bef_c, aft_c, win[g], deviation_s, sigma, sampling, d_ms, beat_corr, size, + phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, + show_figure=False, plot_dist=False, save=True) + + + + gs00 = gridspec.GridSpecFromSubplotSpec(7, 2, subplot_spec=gs0[g]) + #plt.title('w1') + #fig = frame_subplots(6.5, 3, 'Beats [Hz]', '', 'Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (d_ms[d]) + ') '+win) + #two_ylabels(1,2,1,fig, 'distance (not normalised)', 'distance (normalised)') + subplots = 14 + #ax = create_subpl(0,fig, subplots, 7, 2, share2 = share) + plots = [distance_metz_all[d_ms[d]], euc_all[d_ms[d]],distance_metz_all_in[d_ms[d]],euc_all_in[d_ms[d]],distance_metz_all_lm[d_ms[d]], euc_all_lm[d_ms[d]], distance_metz_all_gm[d_ms[d]],euc_all_gm[d_ms[d]], distance_metz_all_ds[d_ms[d]], euc_all_ds[d_ms[d]],period_distance_c[d_ms[d]],period_distance_b[d_ms[d]],period_shift[d_ms[d]],period_shift[d_ms[d]]] + labels = ['conv mean','conv sum', 'indirect mean', 'indirect sum', 'lm mean', 'lm sum', 'gm mean', 'gm sum','ds mean', 'ds sum','period lengths','','period shift',''] + colors = ['red','red', 'magenta','magenta','green','green','black','black','orange','orange','darkblue','darkblue','lime','lime'] + I = {} + + + for i in range(subplots): + if share: + ax[i] = plt.subplot(gs00[i],sharex=ax[0]) + else: + ax[i] = plt.subplot(gs00[i]) + I[i] = ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + ax[i].set_yticks([]) + axvline_everywhere(ax, subplots, eod_fe, eod_fr) + ax[0].set_title(win[g] +' Mean ') + ax[1].set_title(win[g]+' Sum (Euclidean) ') + plots = [distance_wo_mean_all[d_ms[d]],distance_wo_mean_all_in[d_ms[d]],distance_wo_mean_all_lm[d_ms[d]],distance_wo_mean_all_gm[d_ms[d]],distance_wo_mean_all_ds[d_ms[d]]] + for i in np.arange(0,subplots-4,2): + ax[i].plot(eod_fe - eod_fr, plots[int(i/2)], color=colors[i], linestyle='--') + + plots1 = [distance_wo_max_all[d_ms[d]],distance_wo_max_all_in[d_ms[d]],distance_wo_max_all_lm[d_ms[d]],distance_wo_max_all_gm[d_ms[d]],distance_wo_max_all_ds[d_ms[d]]] + plots2 = [distance_wo_mean_max_all[d_ms[d]], distance_wo_mean_max_all_in[d_ms[d]], distance_wo_mean_max_all_lm[d_ms[d]], distance_wo_mean_max_all_gm[d_ms[d]], + distance_wo_mean_max_all_ds[d_ms[d]]] + for i in np.arange(0, subplots - 4, 2): + ax[20+i] = ax[i].twinx() + ax[20 +i].set_yticks([]) + ax[20+i].plot(eod_fe - eod_fr, plots1[int(i/2)],color=colors[i],linestyle='dashed') + ax[20+i].plot(eod_fe - eod_fr, plots2[int(i/2)],linestyle='-.', + color=colors[i]) + plt.subplots_adjust(hspace = 0.15, wspace = 0.15) + #fig.legend(I,labels=labels) + #fig.legend([I[1],I[3],I[5],I[7],I[9],I[11]],labels = ['convolved','indirect','lm','gm','ds','period']) + if win[g] == 'w1': + ax[0].set_ylabel('convolved',color = 'red', rotation = 360, labelpad = 40) + ax[2].set_ylabel('perfect', color='magenta', rotation = 360, labelpad = 40) + ax[4].set_ylabel('local max', color='green', rotation = 360, labelpad = 40) + ax[6].set_ylabel('global max', color='black', rotation = 360, labelpad = 40) + ax[8].set_ylabel('sampled f', color='orange', rotation = 360, labelpad = 40) + ax[10].set_ylabel('period dist', color='darkblue', rotation = 360, labelpad = 40) + ax[12].set_ylabel('period shift', color='lime', rotation=360, labelpad=40) + ax[8].set_xticks([]) + ax[10].set_xticks([]) + if win[g] == 'w3': + ax[0].set_ylabel('w1: pd and ps, mult', rotation=360, labelpad=60) + ax[2].set_ylabel('w2: same pd', rotation=360, labelpad=60) + ax[4].set_ylabel('w3: pd constant high f', rotation=360, labelpad=60) + ax[6].set_ylabel('w4, same pd and ps', rotation=360, labelpad=60) + if win[g] == 'w2': + ax[8].set_xticks([]) + ax[10].set_xticks([]) + + #ax = plt.gca() + #leg = ax.get_legend() + #leg.legendHandles[0].set_color('red') + #leg.legendHandles[1].set_color('darkblue') + + def onclick(event): + e = np.argmin(np.abs((eod_fe - eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t,sampling, deviation_s,d,a_fe, eod_fr, a_fr, eod_fe, e,phase_zero,p, size,s, sigma) + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + #plt.savefig( + # fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + # size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + # eod_fr) + ')_beat_%1.2f' % ( + # beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1]) + ')_deviation(%1.2f' % ( + # d_ms[d]) + '_distances_all' + 'several' + '.png') + + g = 4 + gs00 = gridspec.GridSpecFromSubplotSpec(4, 2, subplot_spec=gs0[g]) + with open('C:/Users/alexi/Desktop/Masterarbeit/labrotation/data/consp4_1.5msbeats(-500.00_1995.00_5.00).pkl', + 'rb') as f: # Python 3: open(..., 'rb') + eod_fe, beat_corr, beats, integral_ds, integral_lm, integral_gm, integral_ind, integral_conv, max_values_gm, max_positions_gm, max_values_lm, max_positions_lm, max_values_conv, max_positions_conv, max_values_ind, max_positions_ind, max_values_ds, max_positions_ds = pickle.load( + f) + d = 1 + #two_ylabels(1,2,1,fig, 'Integral beneath the chirp Consp', '') + #two_ylabels(1, 2, 2, fig, '', 'Conspicousness [range 0 to 1]') + subplots = 8 + #ax = create_subpl(0.3,fig, subplots, 4, 2, share2 = share) + plots = [integral_conv, max_values_conv,integral_lm,max_values_lm, integral_gm, max_values_gm, integral_ind, max_values_ind] + labels = ['Integral conv','Max Values conv','Integral lm','Max Values lm','Integral gm','Max Values gm','Integral ind','Max Values ind'] + colors = ['red','red', 'green','green','black','black','magenta','magenta','orange','orange','darkblue','darkblue'] + for i in range(subplots): + if share: + ax[i] = plt.subplot(gs00[i], sharex=ax[0]) + else: + ax[i] = plt.subplot(gs00[i]) + ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + axvline_everywhere(ax,subplots, eod_fe, eod_fr) + #ax[0].set_ylabel('convolved',color = 'red', rotation) + #ax[2].set_ylabel('loc max', color='green') + #ax[4].set_ylabel('glo max', color='black') + #ax[6].set_ylabel('perfect beat corr', color='magenta') + ax[0].set_title('Integral') + ax[1].set_title('Max Value') + for i in np.arange(0,8,2): + ax[i].set_ylim([0,int(20/3) * 3]) + for i in np.arange(1, 8, 2): + ax[i].set_ylim([0, 1.1]) + #plt.legend(loc='upper left',bbox_to_anchor=(0, 2)) + period = period_func(beat_corr, sampling, 'stim') + def onclick2(event): + + e = np.argmin(np.abs((eod_fe-eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t, sampling, deviation_s, d, a_fe, eod_fr, a_fr, eod_fe, e, phase_zero, p, size, s, sigma) + rang = [0.5*sampling] + time_fish,sampled, cut, cube_c, time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp = snip([-rang[0] * 0.5]*len(eod_fe), [rang[0] * 0.5]*len(eod_fe), e,e,sampling, deviation_s, + d, eod_fr, a_fr, eod_fe, + phase_zero, 0, size, 0, + sigma, a_fe, deviation, + beat_corr) + B_conv, B_indirect,B_downsampled,B_lm,B_gm,time = snippets4(deviation_s,a_fr, sigma,period,time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp, eod_fe, beat_corr, a_fe, eod_fr, e, size, 0, phase_zero, 0, deviation, d, rang[0], delta_t, sampling, plot = False,show = False) + + #fig = plt.figure(figsize = [7,6]) + gaussian = gauss(10,sampling,0.02,1) + pl = True + sh = False + if event.inaxes == ax[4] or event.inaxes == ax[5]: + max_position, max_value, I,ConSpi2 = single_consp(1,eod_fe[e],eod_fr,'black',gaussian,B_gm, gm_chirp, sampling,plot = pl,show = sh) + if event.inaxes == ax[2] or event.inaxes == ax[3]: + max_position, max_value, I,ConSpi2 = single_consp(1,eod_fe[e],eod_fr,'green',gaussian, B_lm, lm_chirp, sampling,plot = pl,show = sh) + if event.inaxes == ax[0] or event.inaxes == ax[1]: + max_position, max_value, I,ConSpi2 = single_consp(1,eod_fe[e],eod_fr,'red',gaussian, B_conv, conv_chirp, sampling,plot = pl,show = sh) + if event.inaxes == ax[6] or event.inaxes == ax[7]: + max_position, max_value, I,ConSpi2 = single_consp(1,eod_fe[e],eod_fr,'magenta',gaussian,B_indirect, am_indirect_chirp, sampling,plot = pl,show = sh) + plt.xlabel('Time [ms]') + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + + if show: + plt.show() + + + +def plot_dist_interactiv4(bef_c, aft_c,disc,d, sampling, size, phase_zero, sigma, beat_corr, delta_t, a_fr, a_fe, deviation_dp, deviation_s, d_ms, eod_fe, eod_fr, share = False, show = False): + fig = plt.figure(figsize = [12,5]) + plt.title('convolved (red), indirect (magenta), loc max (green), glo max (black),sampled f (orange), period (purple)') + gs0 = gridspec.GridSpec(2, 2) + win = ['w1', 'w2', 'w3', 'w4'] + ax = {} + gs00 = gridspec.GridSpecFromSubplotSpec(7, 2, subplot_spec=gs0[3]) + ax[0] = plt.subplot(gs00[4]) + p = 0 + s = 0 + for g in range(4): + period_shift, period_distance_c, period_distance_b, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, range_beat_all_ds, range_chirp_all_ds, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm, beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, d_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, \ + distance_wo_mean_max_all = distances_func(disc, bef_c, aft_c, win[g], deviation_s, sigma, sampling, d_ms, beat_corr, size, + phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, + show_figure=False, plot_dist=False, save=True) + + + + gs00 = gridspec.GridSpecFromSubplotSpec(7, 2, subplot_spec=gs0[g]) + #plt.title('w1') + #fig = frame_subplots(6.5, 3, 'Beats [Hz]', '', 'Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (d_ms[d]) + ') '+win) + #two_ylabels(1,2,1,fig, 'distance (not normalised)', 'distance (normalised)') + subplots = 14 + #ax = create_subpl(0,fig, subplots, 7, 2, share2 = share) + plots = [distance_metz_all[d_ms[d]], euc_all[d_ms[d]],distance_metz_all_in[d_ms[d]],euc_all_in[d_ms[d]],distance_metz_all_lm[d_ms[d]], euc_all_lm[d_ms[d]], distance_metz_all_gm[d_ms[d]],euc_all_gm[d_ms[d]], distance_metz_all_ds[d_ms[d]], euc_all_ds[d_ms[d]],period_distance_c[d_ms[d]],period_distance_b[d_ms[d]],period_shift[d_ms[d]],period_shift[d_ms[d]]] + labels = ['conv mean','conv sum', 'indirect mean', 'indirect sum', 'lm mean', 'lm sum', 'gm mean', 'gm sum','ds mean', 'ds sum','period lengths','','period shift',''] + colors = ['red','red', 'magenta','magenta','green','green','black','black','orange','orange','darkblue','darkblue','lime','lime'] + I = {} + + + for i in range(subplots): + if share: + ax[i] = plt.subplot(gs00[i],sharex=ax[0]) + else: + ax[i] = plt.subplot(gs00[i]) + I[i] = ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + ax[i].set_yticks([]) + axvline_everywhere(ax, subplots, eod_fe, eod_fr) + ax[0].set_title(win[g] +' Mean ') + ax[1].set_title(win[g] +' Sum (Euclidean) ') + plots = [distance_wo_mean_all[d_ms[d]],distance_wo_mean_all_in[d_ms[d]],distance_wo_mean_all_lm[d_ms[d]],distance_wo_mean_all_gm[d_ms[d]],distance_wo_mean_all_ds[d_ms[d]]] + for i in np.arange(0,subplots-4,2): + ax[i].plot(eod_fe - eod_fr, plots[int(i/2)], color=colors[i], linestyle='--') + + plots1 = [distance_wo_max_all[d_ms[d]],distance_wo_max_all_in[d_ms[d]],distance_wo_max_all_lm[d_ms[d]],distance_wo_max_all_gm[d_ms[d]],distance_wo_max_all_ds[d_ms[d]]] + plots2 = [distance_wo_mean_max_all[d_ms[d]], distance_wo_mean_max_all_in[d_ms[d]], distance_wo_mean_max_all_lm[d_ms[d]], distance_wo_mean_max_all_gm[d_ms[d]], + distance_wo_mean_max_all_ds[d_ms[d]]] + for i in np.arange(0, subplots - 4, 2): + ax[20+i] = ax[i].twinx() + ax[20 +i].set_yticks([]) + ax[20+i].plot(eod_fe - eod_fr, plots1[int(i/2)],color=colors[i],linestyle='dashed') + ax[20+i].plot(eod_fe - eod_fr, plots2[int(i/2)],linestyle='-.', + color=colors[i]) + plt.subplots_adjust(hspace = 0.15, wspace = 0.15) + #fig.legend(I,labels=labels) + #fig.legend([I[1],I[3],I[5],I[7],I[9],I[11]],labels = ['convolved','indirect','lm','gm','ds','period']) + if win[g] == 'w1': + ax[0].set_ylabel('convolved',color = 'red', rotation = 360, labelpad = 40) + ax[2].set_ylabel('perfect', color='magenta', rotation = 360, labelpad = 40) + ax[4].set_ylabel('local max', color='green', rotation = 360, labelpad = 40) + ax[6].set_ylabel('global max', color='black', rotation = 360, labelpad = 40) + ax[8].set_ylabel('sampled f', color='orange', rotation = 360, labelpad = 40) + ax[10].set_ylabel('period dist', color='darkblue', rotation = 360, labelpad = 40) + ax[12].set_ylabel('period shift', color='lime', rotation=360, labelpad=40) + ax[8].set_xticks([]) + ax[10].set_xticks([]) + if win[g] == 'w3': + ax[0].set_ylabel('w1: pd and ps, mult', rotation=360, labelpad=60) + ax[2].set_ylabel('w2: same pd', rotation=360, labelpad=60) + ax[4].set_ylabel('w3: pd constant high f', rotation=360, labelpad=60) + ax[6].set_ylabel('w4, same pd and ps', rotation=360, labelpad=60) + if win[g] == 'w2': + ax[8].set_xticks([]) + ax[10].set_xticks([]) + + #ax = plt.gca() + #leg = ax.get_legend() + #leg.legendHandles[0].set_color('red') + #leg.legendHandles[1].set_color('darkblue') + + def onclick(event): + e = np.argmin(np.abs((eod_fe - eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t,sampling, deviation_s,d,a_fe, eod_fr, a_fr, eod_fe, e,phase_zero,p, size,s, sigma) + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + #plt.savefig( + # fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + # size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + # eod_fr) + ')_beat_%1.2f' % ( + # beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1]) + ')_deviation(%1.2f' % ( + # d_ms[d]) + '_distances_all' + 'several' + '.png') + if show: + plt.show() + + +def plot_dist_interactiv2_more(beat_corr, deviation_dp, delta_t, deviation_s, a_fe, a_fr, sigma,disc,win,period_shift,d,d_ms, period_distance, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s, size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, share = False, show = False): + fig = frame_subplots(12, 5, 'Beats [Hz]', '', 'Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (d_ms[d]) + ') '+win) + two_ylabels(110,1,2,1,fig, 'distance (not normalised)', 'distance (normalised)') + subplots = 14 + ax = create_subpl(0,fig, subplots, 7, 2, share2 = share) + plots = [distance_metz_all[d_ms[d]], euc_all[d_ms[d]],distance_metz_all_in[d_ms[d]],euc_all_in[d_ms[d]],distance_metz_all_lm[d_ms[d]], euc_all_lm[d_ms[d]], distance_metz_all_gm[d_ms[d]],euc_all_gm[d_ms[d]], distance_metz_all_ds[d_ms[d]], euc_all_ds[d_ms[d]],period_distance[d_ms[d]],period_distance[d_ms[d]],period_shift[d_ms[d]],period_shift[d_ms[d]]] + labels = ['conv mean','conv sum', 'indirect mean', 'indirect sum', 'lm mean', 'lm sum', 'gm mean', 'gm sum','ds mean', 'ds sum','period lengths','','period shift',''] + colors = ['red','red', 'magenta','magenta','green','green','black','black','orange','orange','darkblue','darkblue','lime','lime'] + I = {} + for i in range(subplots): + I[i] = ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + axvline_everywhere(ax, subplots, eod_fe, eod_fr) + ax[0].set_title('Mean (Mean instead sum)') + ax[1].set_title('Sum (Euclidean distance)') + + plots = [distance_wo_mean_all[d_ms[d]],distance_wo_mean_all_in[d_ms[d]],distance_wo_mean_all_lm[d_ms[d]],distance_wo_mean_all_gm[d_ms[d]],distance_wo_mean_all_ds[d_ms[d]]] + for i in np.arange(0,subplots-4,2): + ax[i].plot(eod_fe - eod_fr, plots[int(i/2)], color=colors[i], linestyle='--') + + plots1 = [distance_wo_max_all[d_ms[d]],distance_wo_max_all_in[d_ms[d]],distance_wo_max_all_lm[d_ms[d]],distance_wo_max_all_gm[d_ms[d]],distance_wo_max_all_ds[d_ms[d]]] + plots2 = [distance_wo_mean_max_all[d_ms[d]], distance_wo_mean_max_all_in[d_ms[d]], distance_wo_mean_max_all_lm[d_ms[d]], distance_wo_mean_max_all_gm[d_ms[d]], + distance_wo_mean_max_all_ds[d_ms[d]]] + for i in np.arange(0, subplots - 4, 2): + ax[20+i] = ax[i].twinx() + ax[20+i].plot(eod_fe - eod_fr, plots1[int(i/2)],color=colors[i],linestyle='dashed') + ax[20+i].plot(eod_fe - eod_fr, plots2[int(i/2)],linestyle='-.', + color=colors[i]) + #fig.legend(I,labels=labels) + #fig.legend([I[1],I[3],I[5],I[7],I[9],I[11]],labels = ['convolved','indirect','lm','gm','ds','period']) + ax[0].set_ylabel('convolved',color = 'red',rotation = 360, labelpad = 40) + ax[2].set_ylabel('indirect', color='magenta',rotation = 360, labelpad = 40) + ax[4].set_ylabel('loc max', color='green',rotation = 360, labelpad = 40) + ax[6].set_ylabel('glo max', color='black',rotation = 360, labelpad = 40) + ax[8].set_ylabel('sampled f', color='orange',rotation = 360, labelpad = 40) + ax[10].set_ylabel('period distance', color='darkblue',rotation = 360, labelpad = 40) + ax[12].set_ylabel('period shift', color='lime', rotation=360, labelpad=40) + #ax = plt.gca() + #leg = ax.get_legend() + #leg.legendHandles[0].set_color('red') + #leg.legendHandles[1].set_color('darkblue') + + + def onclick(event): + e = np.argmin(np.abs((eod_fe - eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t,sampling, deviation_s,d,a_fe, eod_fr, a_fr, eod_fe, e,phase_zero,p, size,s, sigma) + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + + plt.savefig( + fname= disc+'Masterarbeit/labrotation/results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + eod_fr) + ')_beat_%1.2f' % ( + beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1]) + ')_deviation(%1.2f' % ( + d_ms[d]) + '_distances_all' + 'several' + '.png') + + if show: + plt.show() + +def plot_dist_interactiv2(df_ds,corr_ds,beat_corr, deviation_dp, delta_t, deviation_s, a_fe, a_fr, sigma, disc, win, period_shift, d, d_ms, period_distance, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, beat_corr_org, distance_metz_all_df, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s, size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_corr, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, share = False, show = False): + #embed() + fig = frame_subplots(7, 7, 'Beats [Hz]', '', 'Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (d_ms[d]) + ') '+win) + + two_ylabels(130,1,2,1,fig, 'distance (normalised)', 'distance (normalised)') + plots = [distance_metz_all[d_ms[d]],distance_metz_all_df[d_ms[d]], df_ds[d_ms[d]],distance_metz_all_corr[d_ms[d]],corr_ds[d_ms[d]], + distance_metz_all_gm[d_ms[d]], distance_metz_all_lm[d_ms[d]], period_distance[d_ms[d]], + period_shift[d_ms[d]]] + subplots = len(plots) + + ax = create_subpl(0,fig, subplots, subplots, 1, share2 = share) + #plots = [distance_metz_all[d_ms[d]], euc_all[d_ms[d]], distance_metz_all_corr[d_ms[d]], euc_all_in[d_ms[d]], distance_metz_all_lm[d_ms[d]], euc_all_lm[d_ms[d]], distance_metz_all_gm[d_ms[d]], euc_all_gm[d_ms[d]], distance_metz_all_df[d_ms[d]], euc_all_ds[d_ms[d]], period_distance[d_ms[d]], period_distance[d_ms[d]], period_shift[d_ms[d]], period_shift[d_ms[d]]] + labels = ['conv mean', 'df','df ds','corr mean','corr ds', 'gm mean','lm mean', 'period lengths',''] + colors = ['black','magenta','pink','orange','moccasin','red','green','darkblue','lime'] + I = {} + + + for i in range(subplots): + I[i] = ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + axvline_everywhere(ax, subplots, eod_fe, eod_fr) + ax[0].set_title('Mean (Mean instead sum)') + #ax[1].set_title('Sum (Euclidean distance)') + + #plots = [distance_wo_mean_all[d_ms[d]],distance_wo_mean_all_in[d_ms[d]],distance_wo_mean_all_lm[d_ms[d]],distance_wo_mean_all_gm[d_ms[d]],distance_wo_mean_all_ds[d_ms[d]]] + #for i in np.arange(0,subplots,1): + # ax[i].plot(eod_fe - eod_fr, plots[int(i/2)], color=colors[i], linestyle='--') + + #plots1 = [distance_wo_max_all[d_ms[d]],distance_wo_max_all_in[d_ms[d]],distance_wo_max_all_lm[d_ms[d]],distance_wo_max_all_gm[d_ms[d]],distance_wo_max_all_ds[d_ms[d]]] + #plots2 = [distance_wo_mean_max_all[d_ms[d]], distance_wo_mean_max_all_in[d_ms[d]], distance_wo_mean_max_all_lm[d_ms[d]], distance_wo_mean_max_all_gm[d_ms[d]], + # distance_wo_mean_max_all_ds[d_ms[d]]] + #for i in np.arange(0, subplots - 4, 2): + # ax[20+i] = ax[i].twinx() + # ax[20+i].plot(eod_fe - eod_fr, plots1[int(i/2)],color=colors[i],linestyle='dashed') + # ax[20+i].plot(eod_fe - eod_fr, plots2[int(i/2)],linestyle='-.', + # color=colors[i]) + #fig.legend(I,labels=labels) + #fig.legend([I[1],I[3],I[5],I[7],I[9],I[11]],labels = ['convolved','indirect','lm','gm','ds','period']) + ax[0].set_ylabel('convolved', color='black', rotation=360, labelpad=40) + + ax[1].set_ylabel('DF', color='magenta', rotation=360, labelpad=40) + ax[2].set_ylabel('DF downsampled', color='pink', rotation=360, labelpad=40) + + ax[3].set_ylabel('beat corr', color='orange',rotation = 360, labelpad = 40) + ax[4].set_ylabel('beat corr downsampled', color='moccasin', rotation=360, labelpad=40) + + ax[5].set_ylabel('global max', color='red', rotation=360, labelpad=40) + ax[6].set_ylabel('local max', color='green', rotation=360, labelpad=40) + + ax[7].set_ylabel('segments length', color='darkblue',rotation = 360, labelpad = 40) + ax[8].set_ylabel('timewindow', color='lime', rotation=360, labelpad=40) + #ax = plt.gca() + #leg = ax.get_legend() + #leg.legendHandles[0].set_color('red') + #leg.legendHandles[1].set_color('darkblue') + plt.subplots_adjust(left = 0.3) + + def onclick(event): + e = np.argmin(np.abs((eod_fe - eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t,sampling, deviation_s,d,a_fe, eod_fr, a_fr, eod_fe, e,phase_zero,p, size,s, sigma) + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + eod_fr) + ')_beat_%1.2f' % ( + beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1]) + ')_deviation(%1.2f' % ( + d_ms[d]) + '_distances_all' + 'several' + '.png') + + if show: + plt.show() + +def onclick_effect(beat_corr, deviation_dp, delta_t,sampling, deviation_s,d,a_fe, eod_fr, a_fr, eod_fe, e,phase_zero,p, size,s, sigma): + #embed() + left_c = -200 * delta_t*sampling + right_c = 200 * delta_t*sampling + time, time_cut, cut = find_times(left_c, right_c, 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) + 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] + + maxima_values, maxima_index, maxima_interp = global_maxima(sampling, eod_fr, 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_dp[d], eod_rec_up, + eod_rec_down) # convolve + am_corr = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s], + sigma) # indire# ct am calculation + am_df = integrate_chirp(a_fe, time_cut, eod_fe[e]-eod_fr, phase_zero[p], size[s], + sigma) # indire# ct am calculation + _, time_fish, cut_f = find_times(left_c, right_c, eod_fr, deviation_s[d]) # downsampled through fish EOD + am_df_ds = integrate_chirp(a_fe, time_fish, eod_fe[e]-eod_fr, phase_zero[p], size[s], + sigma) # indire# ct am calculation + am_corr_ds = integrate_chirp(a_fe, time_fish, beat_corr[e], phase_zero[p], size[s], sigma) + middle_am = int(len(am_corr_ds) / 2) + plt.figure(figsize=[6.5, 5]) + + #conv + plt.plot(time_cut * 1000, eod_conv_up + 3.3, color='black',label='convolved', linewidth=2) + + plt.plot(time_cut * 1000, am_df + 3.4, color='magenta', label='DF', linewidth=2) + plt.plot( + time_fish * 1000, + am_df_ds +2.8, + color='pink', + label='DF downsampled', linewidth=2) + + plt.plot(time_cut * 1000, am_corr + 2.2, color='orange', label='Beat corr', linewidth=2) + plt.plot( + time_fish * 1000, + am_corr_ds + 1.6, + color='moccasin', + label='Beat corr downsampled', linewidth=2) + + + plt.plot(time_cut * 1000, eod_conv_down - 1.3, color='black', linewidth=2) + + #signal + plt.plot(time_cut * 1000, eod_overlayed_chirp, + label='EOD both fish', + color='silver', linewidth=0.5) + + + #AM maxima + plt.scatter((index_peaks - 0.5 * len(eod_rec_up[cut:-cut])) / (sampling / 1000), value_peaks, + color='green', s=10, linewidth=2) + plt.plot((index_peaks - 0.5 * len(eod_rec_up[cut:-cut])) / (sampling / 1000), + value_peaks, + color='green', label='all maxima', linewidth=2) + + plt.scatter((maxima_index - 0.5 * len(eod_rec_up[cut:-cut])) / (sampling / 1000), maxima_values, + color='red', s=10) + plt.plot((maxima_index - 0.5 * len(eod_rec_up[cut:-cut])) / (sampling / 1000), maxima_values, + color='red', label='maxima on lower frequency', linewidth=2) + + + + + + + # beat corr + + # df + + plt.subplots_adjust(left = 0.4,hspace = 0.8) + #embed() + plt.title('Mult' + str(int(((eod_fe[e] - eod_fr)/eod_fe[e]+1)*100)/100)) + plt.legend(loc=(-0.75,0.6), ncol=1) + plt.xlim([-200, 200]) + + +def plot_cons_interactiv2(deviation_dp, deviation_s, a_fe, a_fr, sigma, delta_t,type,disc,integral_ind,p, s, d, size, amplitude, phase_zero, beats, sampling, deviation,deviation_ms, eod_fe, eod_fr, beat_corr, max_values_ds, max_positions_ds, integral_ds, max_values_lm, max_positions_lm, integral_lm, max_values_gm, max_positions_gm, integral_gm, max_values_ind, max_positions_ind, integral_corr, max_values_conv, max_positions_conv, integral_conv, share = False): + fig = frame_subplots(13, 6, 'Beats [Hz]', '', 'Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (deviation_ms[d]) + ')') + two_ylabels(60,1,2,1,fig, 'Integral beneath the chirp Consp', '') + two_ylabels(60,1, 2, 2, fig, '', 'Conspicousness [range 0 to 1]') + subplots = 8 + ax = create_subpl(0.3,fig, subplots, 4, 2, share2 = share) + plots = [integral_conv, max_values_conv,integral_lm,max_values_lm, integral_gm, max_values_gm, integral_ind, max_values_ind] + labels = ['Integral conv','Max Values conv','Integral lm','Max Values lm','Integral gm','Max Values gm','Integral ind','Max Values ind'] + colors = ['red','red', 'green','green','black','black','magenta','magenta','orange','orange','darkblue','darkblue'] + for i in range(subplots): + ax[i].plot(eod_fe - eod_fr, plots[i], label=labels[i], color=colors[i], linestyle='-') + axvline_everywhere(ax,subplots, eod_fe, eod_fr) + ax[0].set_ylabel('convolved',color = 'red') + ax[2].set_ylabel('loc max', color='green') + ax[4].set_ylabel('glo max', color='black') + ax[6].set_ylabel('perfect beat corr', color='magenta') + ax[0].set_title('Integral') + ax[1].set_title('Max Value') + for i in np.arange(0,8,2): + ax[i].set_ylim([0,int(20/3) * 3]) + for i in np.arange(1, 8, 2): + ax[i].set_ylim([0, 1.1]) + #plt.legend(loc='upper left',bbox_to_anchor=(0, 2)) + period = period_func(beat_corr, sampling,'stim') + def onclick(event): + + e = np.argmin(np.abs((eod_fe-eod_fr) - event.xdata)) + onclick_effect(beat_corr, deviation_dp,delta_t, sampling, deviation_s, d, a_fe, eod_fr, a_fr, eod_fe, e, phase_zero, p, size, s, sigma) + rang = [0.5*sampling] + time_fish,sampled, cut, cube_c, time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp = snip([-rang[0] * 0.5]*len(eod_fe), [rang[0] * 0.5]*len(eod_fe), e,e,sampling, deviation_s, + d, eod_fr, a_fr, eod_fe, + phase_zero, 0, size, 0, + sigma, a_fe, deviation, + beat_corr) + B_conv, B_indirect,B_downsampled,B_lm,B_gm,time = snippets4(deviation_s,a_fr, sigma,period,time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp, eod_fe, beat_corr, a_fe, eod_fr, e, size, 0, phase_zero, 0, deviation, d, rang[0], delta_t, sampling, plot = False,show = False) + + #fig = plt.figure(figsize = [7,6]) + gaussian = gauss(10,sampling,0.02,1) + pl = True + sh = False + if event.inaxes == ax[4] or event.inaxes == ax[5]: + max_position, max_value, I,ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'black',gaussian,B_gm, gm_chirp, sampling,'stim',plot = pl,show = sh) + if event.inaxes == ax[2] or event.inaxes == ax[3]: + max_position, max_value, I,ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'green',gaussian, B_lm, lm_chirp, sampling,'stim',plot = pl,show = sh) + if event.inaxes == ax[0] or event.inaxes == ax[1]: + max_position, max_value, I,ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'red',gaussian, B_conv, conv_chirp, sampling,'stim',plot = pl,show = sh) + if event.inaxes == ax[6] or event.inaxes == ax[7]: + max_position, max_value, I,ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'magenta',gaussian,B_indirect, am_indirect_chirp, sampling,'stim',plot = pl,show = sh) + plt.xlabel('Time [ms]') + plt.show() + + if share == False: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + + plt.savefig( + + fname='../results/' + 'simulations/' +str(type[0]) +'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + eod_fr) + ')_beat_%1.2f' % ( + beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1])+ ')_deviation(%1.2f' % ( + deviation_ms[d]) + '_consp' + 'several' + '.png') + plt.show() + +def plot_dist_interactiv(distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, maxima_index_all, maxima_all, am_indirect_all, am_fish_all, time_fish_all, eod_convolved_down_all, eod_convolved_up_all, value_peaks_all, p, s, d, size, amplitude, phase_zero, beats, sampling, eod_rectified_up_all, index_peaks_all, time, eod_overlayed_chirp_all, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, interact2 = False): + fig = plt.figure(figsize=[13, 6]) + #plt.axes(frameon=False) + ax = fig.add_subplot(1, 1, 1) + plt.xticks([]) + plt.yticks([]) + ax.set_xticks([]) + ax.set_yticks([]) + ax0 = ax.twinx() + ax0.set_xticks([]) + ax0.set_yticks([]) + ax1 = fig.add_subplot(5, 1, 1) + ax2 = fig.add_subplot(5, 1, 4) + ax3 = fig.add_subplot(5, 1, 3) + ax4 = fig.add_subplot(5, 1, 2) + ax5 = fig.add_subplot(5, 1, 5) + ax1.plot(eod_fe - eod_fr, distance_metz_all, label='Metzen', color='red',linestyle=':') + #ax1.set_xticks([]) + ax1.plot(eod_fe - eod_fr, distance_wo_mean_all, label='Metzen ohne Mean', color='red',linestyle='-') + ax11 = ax1.twinx() + ax11.plot(eod_fe - eod_fr, distance_wo_max_all, label='Metzen ohne Normierung', color='red',linestyle='--') + ax11.plot(eod_fe - eod_fr, distance_wo_mean_max_all, label='Metzen ohne Mean und Normierung',linestyle='-.', + color='red') + ax2.plot(eod_fe - eod_fr, distance_metz_all_in, label='Metzen',linestyle=':',color ='magenta') + #ax2.set_xticks([]) + ax2.plot(eod_fe - eod_fr, distance_wo_mean_all_in, label='Metzen ohne Mean',linestyle='-',color ='magenta') + ax22 = ax2.twinx() + ax22.plot(eod_fe - eod_fr, distance_wo_max_all_in, label='Metzen ohne Normierung',linestyle='--',color ='magenta') + ax22.plot(eod_fe - eod_fr, distance_wo_mean_max_all_in, label='Metzen ohne Mean und Normierung',linestyle='-.',color ='magenta') + ax3.plot(eod_fe - eod_fr, distance_metz_all_lm, label='Metzen', color='green',linestyle=':') + ax3.plot(eod_fe - eod_fr, distance_wo_mean_all_lm, label='Metzen ohne Mean', color='green',linestyle='-') + ax33 = ax3.twinx() + ax33.plot(eod_fe - eod_fr, distance_wo_max_all_lm, label='Metzen ohne Normierung', color='green',linestyle='--') + ax33.plot(eod_fe - eod_fr, distance_wo_mean_max_all_lm, label='Metzen ohne Mean und Normierung',linestyle='-.', + color='green') + ax4.plot(eod_fe - eod_fr, distance_metz_all_gm, label='Metzen', color='black',linestyle=':') + ax4.plot(eod_fe - eod_fr, distance_wo_mean_all_gm, label='Metzen ohne Mean', color='black',linestyle='-') + ax44 = ax4.twinx() + ax44.plot(eod_fe - eod_fr, distance_wo_max_all_gm, label='Metzen ohne Normierung', color='black',linestyle='--') + ax44.plot(eod_fe - eod_fr, distance_wo_mean_max_all_gm, label='Metzen ohne Mean und Normierung', + color='black',linestyle='-.') + ax5.plot(eod_fe - eod_fr, distance_metz_all_ds, label='Metzen', color='orange',linestyle=':') + ax5.plot(eod_fe - eod_fr, distance_wo_mean_all_ds, label='Metzen ohne Mean', color='orange',linestyle='-') + ax55 = ax5.twinx() + ax55.plot(eod_fe - eod_fr, distance_wo_max_all_ds, label='Metzen ohne Normierung', color='orange',linestyle='--') + ax55.plot(eod_fe - eod_fr, distance_wo_mean_max_all_ds, label='Metzen ohne Mean und Normierung', + color='orange',linestyle='-.') + ax1.axvline(x=-eod_fr, color='black', linewidth=1, linestyle='-') + for i in range(int(np.max(eod_fe) / np.max(eod_fr))): + ax1.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + ax2.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + ax3.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + ax4.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + ax5.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + + #ax2.axvline(x=-eod_fr, color='black', linewidth=1, linestyle='-') + #for i in range(int(np.max(eod_fe) / np.max(eod_fr))): + + # ax1.legend(loc = 'upper left',bbox_to_anchor=(1.15, 1.6)) + # ax11.legend(loc='upper right', bbox_to_anchor=(1.15, 1.6)) + ax1.legend(loc='upper left',bbox_to_anchor=(0, 2)) + ax11.legend(loc='upper right',bbox_to_anchor=(1.0, 2)) + #ax2.legend(loc='upper left') + #ax22.legend(loc='upper right',bbox_to_anchor=(1, 1)) + #ax3.legend(loc='upper left') + #ax33.legend(loc='upper right',bbox_to_anchor=(1, 1)) + #ax4.legend(loc='upper left') + #ax44.legend(loc='upper right',bbox_to_anchor=(1, 1)) + + ax1.set_title('Convolved: EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (deviation_ms[d]) + ')') + ax2.set_title('Beat corr') + ax3.set_title('Lobal maxima') + ax4.set_title('Global maxima') + ax5.set_title('Down sampled') + #ax1.set_xlabel('beat [hz]') + #ax1.set_ylabel('distance') + #ax2.set_xlabel('beat [hz]') + #ax2.set_ylabel('distance') + ax5.set_xlabel('beat [hz]') + #ax3.set_ylabel('distance') + ax.set_ylabel('distance (not normalised)', labelpad = 40) + ax0.set_ylabel('distance (normalised)', rotation = 270,labelpad = 40) + + plt.subplots_adjust(hspace = 0.65) + + def onclick(event): + i = np.argmin(np.abs((eod_fe-eod_fr) - event.xdata)) + plt.figure(figsize = [7,6]) + plt.plot(time * 1000, eod_overlayed_chirp_all[i][3 * deviation_dp[d]:-3 * deviation_dp[d]], + label='EOD both fish', + color='blue', linewidth=1) + #ax3.title('beat:' + str(beat) + 'Hz ' + 'rf:' + str(eod_fr) + ' ef:' + str(eod_fe[e])) + plt.scatter((index_peaks_all[i] - 0.5 * len(eod_rectified_up_all[i])) / (sampling / 1000), value_peaks_all[i], + color='green', s=10, linewidth=2) + plt.plot((index_peaks_all[i] - 0.5 * len(eod_rectified_up_all[i])) / (sampling / 1000), + value_peaks_all[i], + color='green', label='all maxima', linewidth=2) + plt.plot(time * 1000, eod_convolved_up_all[i], color='red', linewidth=2) + plt.plot(time * 1000, eod_convolved_down_all[i], color='red', label='convolved', linewidth=2) + + plt.plot( + time_fish_all[i][int(3 * deviation_dp[d] / factor):int(-3 * deviation_dp[d] / factor)] * 1000, + am_fish_all[i][int(3 * deviation_dp[d] / factor):int(-3 * deviation_dp[d] / factor)] - 0.3, + color='orange', + label='indirect am - downgesampled', linewidth=2) + plt.scatter((maxima_index_all[i] - 0.5 * len(eod_rectified_up_all[i])) / (sampling / 1000), maxima_all[i], + color='black', s=10) + plt.plot((maxima_index_all[i] - 0.5 * len(eod_rectified_up_all[i])) / (sampling / 1000), maxima_all[i], + color='black', label='all maxima', linewidth=2) + plt.plot(time * 1000, am_indirect_all[i] + 0.3, color='magenta', label='beat corr', linewidth=2) + plt.title('beat'+str(eod_fe[i]-eod_fr)) + plt.legend(loc = 'lower center', ncol = 3, bbox_to_anchor = (0.5,1.05)) + plt.xlim([-400,400]) + plt.show() + + if interact2: + cid = fig.canvas.mpl_connect('button_press_event', onclick) + + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % ( + eod_fr) + ')_beat_%1.2f' % ( + beats[0]) + '_%1.2f' % (abs(beats[1] - beats[0])) +'_%1.2f' % (beats[-1])+ ')_deviation(%1.2f' % ( + deviation_ms[d]) + '_distances_all' + 'several' + '.png') + plt.show() + +def plot_eod_phase(beat_corr, minus_bef ,plus_bef,row,col,a_fe, delta_t, a_fr, size,beat_corr_org,s,pp, amplitude, phase_zero, deviation_ms, eod_fe,deviation, eod_fr, sampling,factor, fs = [6.2992, 4], shift_phase = 0): + beats = eod_fe - eod_fr + #fig = plt.figure(figsize=[6.2992/3, 4]) + fig = plt.figure(figsize=fs) + ax = {} + counter = -1 + ax[0] = plt.subplot(row, col, 1) # int(np.ceil(np.sqrt(len(eod_fe)))) + for p,p_nr in zip(pp,range(len(pp))): + d = 0 + for e in range(len(eod_fe)): + counter += 1 + #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(sampling, eod_fr, period_fish_e, period_fish_r, + eod_recitified_up) # global maxima + index_peaks, value_peaks, peaks_interp = find_lm(eod_recitified_up) # local maxima + 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 + 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 + if plus_bef < 0: + green_true = False + else: + 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) + ax[counter] = pl_eods(row, col,eod_fish_both,cut, beats,beat_corr, peaks_interp, maxima_interp, maxima_index, maxima_values,fig, ax[0],counter, 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,green_true = green_true,add = 'simple') + ax[counter].title.set_text('Beat:' + str(eod_fe[e] - eod_fr) + 'Hz'+' phase'+str(int(phase_zero[p]*100)/100)) + #embed() + xticks = 'off' + yticks = 'off' + plot_pos = col * row - col + 1 + ax[counter].set_xlabel('Time [ms]', labelpad=5) + ax[counter].set_ylabel('[mv]', labelpad=5) + + #if counter == 2: + # ax[counter].set_xlabel('Time [ms]', labelpad=5) + # ax[counter].set_ylabel('[mv]', labelpad=5) + #else: + # if xticks == 'off': + # ax[counter].set_xticks([]) + # if yticks == 'off': + # ax[counter].set_yticks([]) + #lower_left_label(e+1, col, row, 'Time [ms]', '[mv]',xticks = 'off',yticks = 'off',) + #plt.xlabel('Time [ms]') + plt.legend(loc=(-1, -1.3),ncol = 2) + plt.subplots_adjust(wspace=.2, hspace=.7,bottom = 0.3) + #embed() + plt.savefig( + fname='../results/' + 'thesis/ '+'phasepic'+str(p)+str(eod_fe[e])+'.png') + plt.show() + + +def plot_eod(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): + beats = eod_fe - eod_fr + for d in range(len(deviation)): + fig = plt.figure(figsize=fs) + ax = {} + + #shift_phase = 0 + + ax[d] = fig.add_subplot(row,col, 1)#int(np.ceil(np.sqrt(len(eod_fe)))) + for e in range(len(eod_fe)): + #time, time_cut = find_times(time_range, sampling, deviation[d], 1) + left_c = minus_bef * sampling + right_c = plus_bef * 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) + time_test = period_fish_e * 2 * np.pi * eod_fe[e] + eod_test = a_fe * np.sin(time_test) + #embed() + 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(sampling, eod_fr, period_fish_e, period_fish_r, + eod_recitified_up) # global maxima + index_peaks, value_peaks, peaks_interp = find_lm(eod_recitified_up) # local maxima + 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 + 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 + if plus_bef < 0: + green_true = False + else: + 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) + ax[e] = pl_eods(row, col,eod_fish_both,cut, beats,beat_corr, peaks_interp, maxima_interp, maxima_index, maxima_values,fig, ax[d],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 = False,green_true = green_true) + #embed() + xticks = 'off' + yticks = 'off' + plot_pos = col * row - col + 1 + #if e+1 == plot_pos: + # ax[e].set_xlabel('Time [ms]', labelpad=5) + if e+1 in np.arange(row*col-col+1,row*col+1,1): + ax[e].set_xlabel('Time [ms]', labelpad=5) + if e + 1 in np.arange(1,row*col+1,col): + ax[e].set_ylabel('[mv]', labelpad=5) + ax[e].set_yticks([]) + #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',) + #plt.xlabel('Time [ms]') + plt.legend(loc=(-1, -0.7),ncol = 2) + plt.subplots_adjust(wspace=.2, hspace=.4,bottom = 0.20) + #embed() + #scalebar = ScaleBar(50,'ms') + #plt.gca().add_artist(scalebar) + #embed() + # plt.xlim([-200,200]) + plt.savefig( + fname='../highbeats_pdf/cell_simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_deviation(%1.2f' % ( + deviation_ms[d]) + 'beats(%1.2f' % (beats[0]) + '_%1.2f' % (beats[-1]) + '_%1.2f' % ( + abs(beats[1] - beats[0])) + ')_eods_several_range'+str(minus_bef)+'_'+str(plus_bef)+ '.png') + plt.show() + embed() + + +def lower_left_label(number, ncol, nrow, x, y,xticks = 'off',yticks = 'off',labelpad = 5): + plot_pos = ncol * nrow - ncol+1 + if number == plot_pos: + plt.xlabel(x,labelpad = labelpad) + plt.ylabel(y,labelpad = labelpad) + else: + if xticks == 'off': + plt.xticks([]) + if yticks == 'off': + plt.yticks([]) + +def plot_am(a_fe, delta_t, a_fr, size,beat_corr_org,s,p, amplitude, phase_zero, deviation_ms, eod_fe,deviation, eod_fr, sampling,factor): + beats = eod_fe - eod_fr + for d in range(len(deviation)): + fig = plt.figure(figsize=[12, 6]) + ax = {} + ax = fig.add_subplot(int(np.ceil(np.sqrt(len(eod_fe)))), int(np.ceil(np.sqrt(len(eod_fe)))), 1) + for e in range(len(eod_fe)): + time, time_cut = find_times(time_range, sampling, deviation[d], 1) + 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_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_rec_up = rectify(eod_fish_r, eod_fe_chirp) # rectify + maxima_values, maxima_index, maxima_interp = global_maxima(sampling, eod_fr, period_fish_e, period_fish_r, + eod_rec_up) # global maxima + index_peaks, value_peaks, peaks_interp = find_lm(eod_rec_up) # local maxima + middle_conv, eod_conv_down, eod_conv_up,eod_conv_downsampled = conv(eod_fr,sampling, deviation[d], eod_rec_up, eod_rectified_down) # convolve + _, time_fish = find_times(time_range, eod_fr, deviation[d], 50) # 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) + am_indirect = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s], + sigma) # indirect am calculation + period_dist,period_dist,am_beat, maxima_interp_beat, peaks_interp_beat, maxima_interp_chirp, peaks_interp_chirp, am_indirect_beat, \ + time_fish,sampled,cube, am_indirect_chirp, time_beat, conv_beat, conv_chirp_corr, am_chirp, time_chirp, conv_chirp = snippets3( + 1.2, peaks_interp, maxima_interp, am_indirect, am_fish, d, period, middle_am, eod_convolved_up, + middle_conv, + delta_t, sampling, deviation, time, eod_conv_downsampled) + + sav, ax = plot_amp(eod_fr,time, ax, e, fig, 'indirect', eod_fe, time_chirp, time_beat, am_indirect_chirp, + am_indirect_beat, am_indirect) + plt.xlabel('Time [ms]') + plt.legend(loc=(1, 0)) + plt.subplots_adjust(wspace=.2, hspace=.7) + # plt.xlim([-200,200]) + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_deviation(%1.2f' % ( + deviation_ms[d]) + 'beats(%1.2f' % (beats[0]) + '_%1.2f' % (beats[-1]) + '_%1.2f' % ( + abs(beats[1] - beats[0])) + ')_am.png') + plt.show() + +def pl_eods(row,col,eod_fish_both, cut, beats, beat_corr, peaks_interp, maxima_interp, maxima_index, maxima, fig, ax, 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 = 1, add1 = False,share = False,green_true = True): + + 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() + #ax = plt.subplot(1,1,1) + ax.plot(time * 1000, eod_overlayed_chirp[cut:-cut], + label='EOD both fish', + color='silver', linewidth = lw) + #ax.plot(time * 1000, eod_overlayed_chirp[3 * deviation[d]:-3 * deviation[d]], + # label='EOD both fish', + # color='blue', linewidth=2) + if add == True: + ax.title.set_text('Beat:' + 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') + 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)) + if eod_fe[e]-eod_fr <0: + green_true = True + black_true = False + else: + green_true = False + black_true = True + if green_true == True: + #ax.scatter((index_peaks - 0.5 * len(eod_rectified_up[cut:-cut])) / (sampling / 1000), value_peaks, + # color='green', s=10) + ax.plot(time * 1000, peaks_interp[cut:-cut], color='red', label = 'all maxima',linewidth = lw) + #embed() + #ax.plot((index_peaks - 0.5 * len(eod_rectified_up)) / (sampling / 1000), value_peaks, + # color='green', label='all maxima') + #plt.axvline(x=-7.5, color='black', linestyle='dotted', linewidth=1) + #plt.axvline(x=7.5, color='black', linestyle='dotted', linewidth=1) + if add == True: + ax.plot(time * 1000, eod_convolved_up, color='red',linewidth = lw) + ax.plot(time * 1000, eod_convolved_down, color='red', label='convolved',linewidth = lw) + if add1 == True: + 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(time * 1000, am_corr +1.55, color='orange', label='EOD adjusted beat', linewidth = lw) + if add1 == True: + ax.scatter((maxima_index - 0.5 * len(eod_rectified_up)) / (sampling / 1000), maxima, + color='red', s=10) + if black_true == True: + ax.plot(time*1000, maxima_interp[cut:-cut], color='red', label= 'AM',linewidth = lw)#[int(3 * deviation[d]):int(-3 * deviation[d])] + ax.plot(time*1000,eod_fish_both[cut:-cut]+ 2.10,color='magenta',label= 'Difference frequency', linewidth = lw) + + #ax.set_xlim([-(2.5*1/beat_corr)*1000,(2.5*1/beat_corr)*1000]) + #ax.plot((maxima_index - 0.5 * len(eod_rectified_up)) / (sampling / 1000), maxima, + # color='black', label='one maxima pro lower frequency') + #ax.set_ylabel('Time [ms]') + #ax.set_ylabel('[mV]') + return ax + +def plot_amp(eod_fr,time ,ax, e, fig, sav, eod_fe,time_chirp,time_beat,conv_chirp,conv_beat, eod_convolved_up): + ax = fig.add_subplot(int(np.ceil(np.sqrt(len(eod_fe)))), int(np.ceil(np.sqrt(len(eod_fe)))), e + 1,sharex = ax,sharey = ax) + ax.plot(time * 1000, eod_convolved_up,color='red', linewidth=2,label = 'beat') + ax.plot(time_chirp * 1000, conv_chirp, color='blue', linewidth=2, label = 'chirp') + ax.plot(time_beat * 1000, conv_beat, color='green', linewidth=2,label = 'beat') + ax.set_title('beat' + str(eod_fe[e]-eod_fr) + 'Hz') + #ax.set_ylim([0.2, 1]) + return sav,ax + + +def plot_all_distances(d,s,p,show_figure,beats,amplitude,size,phase_zero,corr_all,deviation_ms,eod_fe,eod_fr,distance_metz_all,distance_wo_mean_all,distance_wo_max_all,distance_wo_mean_max_all,range_chirp_all,range_beat_all, version): + # plt.title('deviation(%1.2f' % (deviation[d]) + ')_beat(_%1.2f' % (beats[0]) + '_%1.2f' % ( + # beats[-1]) + '_%1.2f' % (abs(beats[1] - beats[0])) + ')recieverEOD(_%1.2f' % (eod_fr) + ')') + fig = plt.figure(figsize=[10, 10]) + ax1 = fig.add_subplot(3, 1, 1) + ax2 = fig.add_subplot(3, 1, 2, sharex = ax1) + ax3 = fig.add_subplot(3, 1, 3, sharex = ax1) + ax1.plot(eod_fe - eod_fr, distance_metz_all, label='Metzen', color='orange') + ax1.plot(eod_fe - eod_fr, distance_wo_mean_all, label='Metzen ohne Mean', color='blue') + ax11 = ax1.twinx() + ax11.plot(eod_fe - eod_fr, distance_wo_max_all, label='Metzen ohne Normierung', color='red') + ax11.plot(eod_fe - eod_fr, distance_wo_mean_max_all, label='Metzen ohne Mean und Normierung', color='green') + ax22 = ax2.twinx() + ax2.plot(eod_fe - eod_fr, range_chirp_all, label='range chirps',linestyle='-') + ax2.plot(eod_fe - eod_fr, range_beat_all, label='range beat',linestyle='--') + ax1.axvline(x=-eod_fr, color='black', linewidth=1, linestyle='-') + for i in range(int(np.max(eod_fe) / np.max(eod_fr))): + ax1.axvline(x=eod_fr * i, color='black', linewidth=1, linestyle='-') + # ax1.legend(loc = 'upper left',bbox_to_anchor=(1.15, 1.6)) + # ax11.legend(loc='upper right', bbox_to_anchor=(1.15, 1.6)) + ax1.legend(loc='upper left') + ax11.legend(loc='upper right') + + ax1.set_title('EOD reciever ' + str(eod_fr) + '_deviation(%1.2f' % (deviation_ms[d]) + ')') + ax2.legend() + ax1.set_xlabel('beat [hz]') + ax1.set_ylabel('distance') + ax2.set_xlabel('Amplitude') + ax2.set_ylabel('distance') + ax3.plot(eod_fe - eod_fr, corr_all) + ax3.set_xlabel('beat [hz]') + ax3.set_ylabel('Correlation') + + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')recieverEOD(_%1.2f' % (eod_fr) + '_%1.2f' % ( + beats[-1]) + '_%1.2f' % (abs(beats[1] - beats[0])) + ')_deviation(%1.2f' % ( + deviation_ms[d]) + ')_beat(_%1.2f' % (beats[0]) + '_distances_all'+version+'.png') + if show_figure: + plt.show() + else: + plt.close() + + + +def plot_distances(eod_fe,p, s,deviation_ms,d, distance,size,phase_zero,amplitude, show_figure = False): + for e in range(len(eod_fe)): + time, time_cut = find_times(time_range, sampling, deviation_dp[d], 1) + 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_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_rec_up = rectify(eod_fish_r, eod_fe_chirp) # rectify + maxima_values, maxima_index, maxima_interp = global_maxima(sampling, eod_fr, period_fish_e, period_fish_r, + eod_rec_up) # global maxima + index_peaks, value_peaks, peaks_interp = find_lm(eod_rec_up) # local maxima + middle_conv, eod_conv_down, eod_conv_up,eod_conv_downsampled = conv(eod_fr,sampling, deviation_dp[d], eod_rec_up, eod_rectified_down) # convolve + _, time_fish = find_times(time_range, eod_fr, deviation_dp[d], 50) # 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) + am_indirect = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s], + sigma) # indirect am calculation + period_dist,am_beat, maxima_interp_beat, peaks_interp_beat, maxima_interp_chirp, peaks_interp_chirp, am_indirect_beat, \ + cube,am_indirect_chirp, time_beat, conv_beat, conv_chirp_corr, am_chirp, time_chirp, conv_chirp = snippets3( + 1.2, peaks_interp, maxima_interp, am_indirect, am_fish, d, period, middle_am, eod_convolved_up, + middle_conv, + delta_t, sampling, deviation_dp, time, eod_conv_downsampled) + plt.plot(distance) + plt.plot(conv_chirp) + plt.plot(conv_beat) + if show_figure: + plt.show() + else: + plt.close() + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_deviation(%1.2f' % (deviation_ms[d])+')_beat(%5.2f' % ( + beat) + ')_absolute_distance.png') + +def plot_snippets(time_chirp, time_beat, time_range,p, sigma, s,delta_t,period,beat,sampling, deviation,d, time, eod_convolved_up,conv_chirp,conv_beat,size,phase_zero,amplitude,show_figure = False): + plt.plot(time* 1000, eod_convolved_up, label='Amplitude Modulation',color='red', linewidth=2) + plt.plot(time_chirp * 1000, conv_chirp, label='Amplitude Modulation', color='blue', linewidth=2) + plt.plot(time_beat * 1000, conv_beat, label='Amplitude Modulation', color='green', linewidth=2) + if show_figure: + plt.show() + else: + plt.close() + plt.savefig( + fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_deviation(%1.2f' % (deviation_ms[d])+'_beat(%5.2f' % ( + beat) + ')_distances_snippets_single.png') + + + + +def plot_subplots(eod_fe,sides,p,s,deviation,d, deviation_ms,sampling,size,phase_zero,amplitude, show_figure = False, chirp = True): + # frequency calculation + for e in range(len(eod_fe)): + time, time_cut = find_times(time_range, sampling, deviation[d], 1) + eod_fish_e, eod_fish_r, period_fish_r, period_fish_e = find_periods(a_fe,time, eod_fr, a_fr, eod_fe, e) + 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_overlayed_chirp = eod_fish_r + eod_fe_chirp + eod_rectified_down, eod_rec_up = rectify(eod_fish_r, eod_fe_chirp) # rectify + maxima_values, maxima_index, maxima_interp = global_maxima(sampling, eod_fr, period_fish_e, period_fish_r, + eod_rec_up) # global maxima + index_peaks, value_peaks, peaks_interp = find_lm(eod_rec_up) # local maxima + middle_conv, eod_conv_down, eod_conv_up,eod_conv_downsampled = conv(eod_fr,sampling, deviation[d], eod_rec_up, eod_rectified_down) # convolve + _, time_fish = find_times(time_range, eod_fr, deviation[d], 50) # 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) + am_indirect = integrate_chirp(a_fe, time_cut, beat_corr[e], phase_zero[p], size[s], + sigma) # indirect am calculation + + chirp = size[s] * np.exp((-(time ** 2) / (2 * sigma ** 2))) + frequency = eod_fr - eod_fe[e] + chirp + + fig = plt.figure(figsize=[10, 6]) + ax = {} + ax[0] = fig.add_subplot(3, 2, 1) + + for i in range(6): + ax[i] = fig.add_subplot(3, 2, i + 1, sharex = ax[0]) + ax[i].axvline(x=-sides, color='black', linewidth=1, linestyle='-') + ax[i].axvline(x=sides, color='black', linewidth=1, linestyle='-') + ax[0].plot(time * 1000, eod_fish_r[3 * deviation[d]:-3 * deviation[d]]) + ax[0].title.set_text('EOD reciever fish') + ax[2].plot(time * 1000, eod_fe_chirp[3 * deviation[d]:-3 * deviation[d]]) + ax[2].title.set_text('EOD and chirp of emmiting fish') + ax[4].plot(time * 1000, frequency) + ax[4].title.set_text('Frequency') + ax[1].plot(time* 1000, eod_overlayed_chirp[3 * deviation[d]:-3 * deviation[d]]) + # ax[3].plot(time[3 * deviation[d]:-3 * deviation[d]] * 1000,eod_rectified[3 * deviation[d]:-3 * deviation[d]]) + + ax[1].title.set_text('EOD both fish + chirp') + (ax[1]).plot(time * 1000, eod_convolved_up, color='red') + (ax[1]).plot(time * 1000, eod_convolved_down, color='red') + corr_up = np.array([eod_convolved_up - np.min(eod_convolved_up) - np.mean(eod_convolved_up)]) + # calculate AM directly + time_sam = np.arange(-100 * delta_t, 100 * delta_t, 1 / 500*50) + I1 = [] + for i in range(len(time_sam)): + y1 = ((np.pi ** 0.5) / 2) * math.erf(time_sam[i] / sigma) - ((np.pi ** 0.5) / 2) * math.erf(-np.inf) + I1.append(y1) + phase = 2 * np.pi * beat * time_sam + 2 * np.pi * size[s] * sigma * np.array(I1) + phase_zero[p] + + # am calculation + am = amplitude * np.cos(phase) + (ax[3]).plot(time_sam[3 * deviation[d]:-3 * deviation[d]] * 1000, am[ + 3 * deviation[d]: - 3 * deviation[d]], color='green') + + #ax33 = ax[3].twinx() + #ax33.twinx().plot(time * 1000, eod_convolved_up, color='red') + + corr_down = np.array([eod_convolved_down - np.min(eod_convolved_down) - np.mean(eod_convolved_down)]) + + + + # (ax[5]).plot(time * 1000, eod_convolved_down[ + # length_convolved + 3 * deviation[ + # d]:-length_convolved - 3 *deviation[d]]-np.min(eod_convolved_down)-np.mean(eod_convolved_down), color = 'red') + + #time = np.arange(-5 * delta_t, 5 * delta_t, 1 / sampling) + # calculate AM directly + time_fish = np.arange(-100 * delta_t, 100 * delta_t, 1 / 500) + I1 = [] + for i in range(len(time_fish)): + y1 = ((np.pi ** 0.5) / 2) * math.erf(time_fish[i] / sigma) - ((np.pi ** 0.5) / 2) * math.erf(-np.inf) + I1.append(y1) + phase = 2 * np.pi * beat * time_fish + 2 * np.pi * size[s] * sigma * np.array(I1) + phase_zero[p] + + # am calculation + am = amplitude * np.cos(phase) + #FIXME + #(ax[5]).twinx().plot(time * 1000, eod_convolved_down, + # color='red') + (ax[5]).plot(time_fish[int(np.round((3 * deviation[d]) / sampling)):int( + np.round((-3 * deviation[d]) / sampling))] * 1000, am[int( + np.round((3 * deviation[d]) / sampling)):int(np.round((-3 * deviation[d]) / sampling))], + color='green') + ax[1].set_xlim([-60,60]) + ax[2].set_xlim([-60, 60]) + ax[3].set_xlim([-60, 60]) + ax[4].set_xlim([-60, 60]) + ax[5].set_xlim([-60, 60]) + # eod_convolved2 = (eod_convolved[length_convolved + int(3.395 * deviation[d]): len( + # eod_convolved) - length_convolved - int(3.395 * deviation[d])]) + plt.subplots_adjust(wspace=.2, hspace=.7) + plt.savefig(fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % ( + size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_deviation(%1.2f' % (deviation_ms[d])+'_beat(%5.2f' % (beat) + ')_subplots.png') + + if show_figure: + plt.show() + else: + plt.close() + + + +def stimulus_harmonics(size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation, show_figure = False): + sampling = 40000 + time = np.arange(-5 * delta_t, 5 * delta_t, 1 / sampling) + for s in range(len(size)): + for p in range(len(phase_zero)): + for d in range(len(deviation)): + save_div = deviation[d] / (sampling / 1000) + if deviation[d] * 5 % 2: + points = deviation[d] * 5 + else: + points = deviation[d] * 5 -1 + gaussian = signal.gaussian(points, std=deviation[d], sym=True) + gaussian_normalised = (gaussian * 100) / np.sum(gaussian) + length_convolved = int(len(gaussian_normalised) / 2) + #cut_convolved = np.arange(length_convolved + (int(3.395 * deviation[d])), + # (len(time) - length_convolved - int(3.395 * deviation[d])), 1) + fig = plt.figure(figsize=[5, 4]) + ax = {} + # plot only the maxima + for i in range(1): + ax[i] = fig.add_subplot(1, 1, i + 1) + ax[i].axvline(x=-7.5, color='black', linewidth=1, linestyle='-') + ax[i].axvline(x=7.5, color='black', linewidth=1, linestyle='-') + for e in range(len(eod_fe)): + eod_fish_r = a_fr*np.sin(time * 2 * np.pi * eod_fr) + sigma = delta_t / math.sqrt((2 * math.log(10))) # width of the chirp + I1 = [] + for i in range(len(time)): + y1 = ((np.pi ** 0.5) / 2) * math.erf(time[i] / sigma) - ((np.pi ** 0.5) / 2) * math.erf(-np.inf) + I1.append(y1) + phase1 = time * 2 * np.pi * eod_fe[e] + 2 * np.pi * size[s] * sigma * np.array(I1) + phase_zero[p] + eod_fe_chirp = a_fe*np.sin(phase1) + eod_overlayed_chirp = eod_fish_r + eod_fe_chirp + eod_rectified = eod_fish_r + eod_fe_chirp + amplitude = a_fe + beat = eod_fe[e] - eod_fr + # rectify and gaussian + eod_rectified[np.where(eod_overlayed_chirp < 0)] = 0 # rectify + eod_convolved = np.convolve(gaussian_normalised, eod_rectified) + # deviation = 60 # 40 = (1/1000)/(1 / 40000): 40 datapoints for 1 milisecond: 40*5 = 200 + # frequency calculation + chirp = size[s] * np.exp((-(time**2)/(2*sigma**2))) + frequency = beat + chirp + #ax[0].plot(time[(length_convolved + (int(3.395 * deviation[d]))):(len(time) - length_convolved - int(3.395 * deviation[d]))]* 1000, eod_convolved[(length_convolved + (int(3.395 * deviation[d]))):(len(time) - length_convolved - int(3.395 * deviation[d]))],label=str(beat)) + ax[0].plot(time[3*deviation[d]:-3*deviation[d]] * 1000, eod_convolved[length_convolved+3*deviation[d]:-length_convolved-3*deviation[d]], label=str(beat)) + ax[0].title.set_text('deviation('+str(save_div)+')') + ax[0].legend(loc = 'lower left') + plt.subplots_adjust(wspace=.2, hspace=.7) + plt.savefig(fname='../results/' + 'simulations/' + 'amplitude(%1.2f' % (amplitude) + ')_size(%5.2f' % (size[s]) + ')_phase(%1.2f' % (phase_zero[p]) + ')_beat(%5.2f' % (beat) + ')_AM_stimulus_illustration3.png') + if show_figure: + plt.show() + else: + plt.close() + +def snippets4(deviation_s,a_fr, sigma, period, time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp, eod_fe, beat_corr, a_fe,eod_fr, e,size, ss, phase_zero, p, deviation, d, window, delta_t, sampling, plot = False,show = False): + shifts_b = 100 + B_conv = [[]]*shifts_b + B_indirect = [[]]*shifts_b + B_downsampled = [[]]*shifts_b + B_lm = [[]]*shifts_b + time = [[]] * shifts_b + B_gm = [[]] * shifts_b + ran = np.linspace(0, 3* period[e], shifts_b) + shift = np.round(window * 2 + ran) + for s in range(shifts_b): + time_fish,sampled,cut, time[s], B_conv[s] , B_indirect[s], B_lm[s], B_gm[s] , B_downsampled[s] = snip(-window*0.5-shift, window*0.5-shift, e,s, + sampling, deviation_s, + d, eod_fr, a_fr, eod_fe, + phase_zero, p, size, ss, + sigma, a_fe, deviation, + beat_corr) + if plot: + fig = frame_subplots(6, 6, 'Time', 'AM', str(eod_fe[e]-eod_fr)+'Hz') + ax = create_subpl(0.2, fig, 5, 3, 2, share2=True) + ax[0].set_title('convovled') + ax[0].plot(np.transpose(B_conv[0:100:20])) + ax[1].set_title('global maximum') + ax[1].plot(np.transpose(B_gm[0:100:20])) + ax[2].set_title('local maximum') + ax[2].plot(np.transpose(B_lm[0:100:20])) + ax[3].set_title('downsampled') + ax[3].plot(np.transpose(B_downsampled[0:100:20])) + ax[4].set_title('indirect') + ax[4].plot(np.transpose(B_indirect[0:100:20])) + if show: + plt.show() + return B_conv, B_indirect,B_downsampled,B_lm,B_gm,time + + + + +def conspicousy(disc,deviation_s,sigma, type,eod_fe, beats, deviation_ms, d, sampling, beat_corr, a_fe, delta_t, eod_fr, a_fr, size, phase_zero, deviation): + max_values_gm = [[]]*len(eod_fe) + max_positions_gm = [[]]*len(eod_fe) + max_values_lm = [[]]*len(eod_fe) + max_positions_lm = [[]]*len(eod_fe) + max_values_conv = [[]]*len(eod_fe) + max_positions_conv = [[]]*len(eod_fe) + max_values_ind = [[]]*len(eod_fe) + max_positions_ind = [[]]*len(eod_fe) + max_values_ds = [[]]*len(eod_fe) + max_positions_ds = [[]]*len(eod_fe) + integral_ds = [[]]*len(eod_fe) + integral_lm= [[]]*len(eod_fe) + integral_gm= [[]]*len(eod_fe) + integral_ind= [[]]*len(eod_fe) + integral_conv= [[]]*len(eod_fe) + rang = [0.4*sampling] + max = [[]]*len(eod_fe) + min = [[]] * len(eod_fe) + period = period_func(beat_corr,sampling, 'stim') + for e in range(len(eod_fe)): + time_fish,sampled,cut, time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp = snip([-rang[0] * 0.5]*len(eod_fe), [rang[0] * 0.5]*len(eod_fe), e,e,sampling, deviation_s, + d, eod_fr, a_fr, eod_fe, + phase_zero, 0, size, 0, + sigma, a_fe, deviation, + beat_corr) + + B_conv, B_indirect,B_downsampled,B_lm,B_gm, time = snippets4(deviation_s,a_fr, sigma,period,time_c, conv_chirp, am_indirect_chirp, lm_chirp, gm_chirp, am_chirp, eod_fe, beat_corr, a_fe, eod_fr, e, size, 0, phase_zero, 0, deviation, d, rang[0], delta_t, sampling, plot = False,show = False) + + + + gaussian = gauss(10,sampling,0.02,1) + max[e] = np.max(np.concatenate(time)) + min[e] = np.min(np.concatenate(time)) + pl = False + sh = False + max_positions_gm[e], max_values_gm[e], integral_gm[e],ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'black',gaussian,B_gm, gm_chirp, sampling,'stim',plot = pl, show = sh) + max_positions_lm[e], max_values_lm[e], integral_lm[e],ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'green',gaussian,B_lm, lm_chirp, sampling,'stim',plot = pl,show = sh) + max_positions_conv[e], max_values_conv[e], integral_conv[e],ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'red',gaussian,B_conv, conv_chirp, sampling,'stim',plot = pl,show = sh) + max_positions_ind[e], max_values_ind[e], integral_ind[e],ConSpi2 = single_consp(type,0,1,eod_fe[e]-eod_fr,eod_fr,'magenta',gaussian,B_indirect, am_indirect_chirp, sampling,'stim',plot = pl,show = sh) + #max_positions_ds[e], max_values_ds[e], integral_ds[e],ConSpi2 = single_consp(gaussian,B_downsampled, am_chirp, eod_fr) + + with open(disc +'Masterarbeit/labrotation/data/' + 'consp4_' +str(type[0])+ str( + deviation_ms[d]) + 'ms' + 'beats(%1.2f' % (beats[0]) + '_%1.2f' % (beats[-1]) + '_%1.2f' % ( + abs(beats[1] - beats[0])) + ')' + '.pkl', 'wb') as f: # Python 3: open(..., 'wb') + pickle.dump( + [eod_fe,beat_corr,beats,integral_ds, integral_lm, integral_gm, integral_ind, integral_conv, max_values_gm, max_positions_gm, max_values_lm, max_positions_lm, max_values_conv, max_positions_conv, max_values_ind, max_positions_ind, max_values_ds, max_positions_ds], f) + + return eod_fe,beat_corr,beats,integral_ds, integral_lm, integral_gm, integral_ind, integral_conv, max_values_gm,max_positions_gm,max_values_lm,max_positions_lm,max_values_conv,max_positions_conv,max_values_ind,max_positions_ind,max_values_ds,max_positions_ds + +def print_len(time): + for i in range(len(time)): + print(len(time[i])) + + + + + + +def test_periods(): + plt.plot(period) + plt.plot(period <= delta_t * skretch) + plt.plot(period_shift) + plt.plot(np.ceil(delta_t * skretch / period) * period) + plt.plot(np.ceil(delta_t * skretch / period)) + + + a = np.ones(100) + b = np.ones(100)*2 + C1 = [] + C2 = [] + C3 = [] + C4 = [] + euc = [[]]*len(a) + mean = [[]] * len(a) + for c in range(len(a)): + g = np.sqrt(np.mean((a[0:c]-np.mean(a[0:c]) - b[0:c] -np.mean(b[0:c]) ) ** 2)) + C1.append(g) + d = np.sqrt(np.mean((a[0:c] - b[0:c]) ** 2)) + C2.append(d) + k = np.sqrt(np.mean((a[0:c]-np.mean(a[0:c]) - b[0:c] -np.mean(b[0:c]) )** 2)) + #C3.append(k) + o = np.mean(np.sqrt(((a[0:c]-np.mean(a[0:c]) - b[0:c] -np.mean(b[0:c]) ) ** 2))) + k = np.sqrt(np.sum((a[0:c]-np.mean(a[0:c]) - b[0:c] -np.mean(b[0:c]) )** 2)/len(a[0:c])) + C3.append(k) + o = np.sum(np.sqrt(((a[0:c]-np.mean(a[0:c]) - b[0:c] -np.mean(b[0:c]) ) ** 2)))/len(a[0:c]) + C4.append(o) + mean[c] = dm_wo_mean2_max = mean_euc_d(a[0:c], b[0:c], None) + euc[c] = d_euclidean = euc_d(a[0:c], b[0:c], None) + + + +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 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 options(run, win,type): + disc = 'D:/' + 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 + eod_fr = 500 # eod fish reciever + a_fr = 1 # amplitude fish reciever + amplitude = a_fe = 0.2 # amplitude fish emitter + factor = 200 + sampling = eod_fr * factor + sampling_fish = 500 + + x = [0.5, 1.5, 2.5] + deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling) + time_range = 200 * delta_t + if run == 'run_int': + start = 0 + end = 2500 + step = 10 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + eod_fe,beat_corr,beats, integral_ds, integral_lm, integral_gm, integral_ind, integral_conv, max_values_gm,max_positions_gm,max_values_lm,max_positions_lm,max_values_conv,max_positions_conv,max_values_ind,max_positions_ind,max_values_ds,max_positions_ds = conspicousy(disc,deviation_s, sigma, type,eod_fe,beats, deviation_ms, 1, sampling, beat_corr, a_fe, delta_t, eod_fr, a_fr, size, phase_zero, deviation_dp) + d = 1 + plot_cons_interactiv2(deviation_dp, deviation_s, a_fe, a_fr, sigma, delta_t,type,disc,integral_ind,0, 0, d, size, a_fe, phase_zero, beats, sampling,deviation_dp, deviation_ms, eod_fe, eod_fr, beat_corr, max_values_ds, max_positions_ds, integral_ds, max_values_lm, max_positions_lm, integral_lm, max_values_gm, max_positions_gm, integral_gm, max_values_ind, max_positions_ind, integral_ind, max_values_conv, max_positions_conv, integral_conv, share = True) + if run == 'show_int': + with open(disc +'Masterarbeit/labrotation/data/consp4_1.5msbeats(-495.00_1995.00_10.00).pkl', 'rb') as f: # Python 3: open(..., 'rb') + eod_fe,beat_corr,beats, integral_ds, integral_lm, integral_gm, integral_ind, integral_conv, max_values_gm, max_positions_gm, max_values_lm, max_positions_lm, max_values_conv, max_positions_conv, max_values_ind, max_positions_ind, max_values_ds, max_positions_ds = pickle.load(f) + d = 1 + plot_cons_interactiv2(deviation_dp, deviation_s, a_fe, a_fr, sigma, delta_t,type,disc,integral_ind,0, 0, d, size, a_fe, phase_zero, beats, sampling,deviation_dp, deviation_ms, eod_fe, eod_fr, beat_corr, max_values_ds, max_positions_ds, integral_ds, max_values_lm, max_positions_lm, integral_lm, max_values_gm, max_positions_gm, integral_gm, max_values_ind, max_positions_ind, integral_ind, max_values_conv, max_positions_conv, integral_conv, share = False) + if run == 'run_dist': + start = 5 + end = 2500 + step = 10 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + period_shift,period_distance_c,period_distance_b,euc_all_in,euc_all_gm,euc_all_lm,euc_all,euc_all_ds,range_beat_all_ds, range_chirp_all_ds, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm,beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds,distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm,distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm,distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm,distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling,deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in,distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr,distance_metz_all, distance_wo_mean_all, distance_wo_max_all,\ + distance_wo_mean_max_all = distances_func(disc, bef_c, aft_c, win, deviation_s, sigma, sampling, deviation_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True) + #plot_dist_interactiv2(period_shift,0,deviation_ms,period_distance_c,euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds,beat_corr_org,distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s,size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, share = False,show = False) + plot_dist_interactiv2(beat_corr, deviation_dp, delta_t, deviation_s, a_fe, a_fr, sigma,disc,win,period_shift,1,deviation_ms,period_distance_c,euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds,beat_corr_org,distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s,size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, share = False,show = True) + if run == 'show_dist': + + with open('../data/sim_dist3_1.5msbeats(-500.00_1995.00_5.00)'+win+'.pkl', + 'rb') as f: # Python 3: open(..., 'rb') + period_distance, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, range_beat_all_ds, range_chirp_all_ds, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm, beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds, \ + distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, \ + distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, \ + distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, \ + distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling, \ + deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, \ + distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, \ + distance_metz_all, distance_wo_mean_all, distance_wo_max_all, \ + distance_wo_mean_max_all = pickle.load(f) + plot_dist_interactiv2(period_distance, euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds, beat_corr_org, + distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, + distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, + distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, + distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s, d, + size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, + distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, + distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, + share=True) + if run == 'run_all_dist': + start = 5 + end = 2500 + step = 10 + eod_fe, beat_corr, beats = find_beats(start, end, step, eod_fr) + plot_dist_interactiv4(bef_c, aft_c,disc,1, sampling, size, phase_zero, sigma, beat_corr, delta_t, a_fr, a_fe, deviation_dp, + deviation_s, deviation_ms, eod_fe, eod_fr, share=True, show=True) + + + +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='../results/thesis/' + 'beat_corr_illustration') + plt.show() + embed() + +def plot_chirp(delta_t, sampling, deviation_ms, sigma, eod_fr, eod_fe,minus_bef = -3,plus_bef = +3): + plt.figure(figsize=[3, 2]) + left_c = minus_bef * delta_t * sampling + right_c = plus_bef * delta_t * sampling + time, time_cut, _ = find_times(left_c, right_c, sampling, deviation_ms[0] / (1000 * sampling)) + chirp = 60 * np.exp((-(time ** 2) / (2 * sigma ** 2))) + frequency = eod_fr - eod_fe[0] + chirp + plt.plot(time * 1000, frequency, color='black') + plt.ylabel('Frequency [Hz]') + plt.xlabel('Time [ms]') + plt.subplots_adjust(left=0.3, bottom=0.3) + # plt.tight_layout() + plt.savefig(fname='../results/thesis/chirpfigure') + plt.show() + +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): + plt.rcParams['figure.figsize'] = (12, 7) + plt.rcParams["legend.frameon"] = False + colors = ['black','blue','purple','magenta','pink','orange', 'brown', 'red', 'green','lime'] + fig, ax = plt.subplots(nrows=len(results), ncols=3, sharex=True) + all_max = [[]] * len(results) + #embed() + 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['result_amplitude_max'][i]), color=colors[i]) + ax[i, 2].plot(beats / eod_fr + 1, np.array(results['result_amplitude_max'][i]), color=colors[i]) + ax[i,2].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() + cid = fig.canvas.mpl_connect('button_press_event', onclick) + plt.savefig('../highbeats_pdf/simulate_power') + 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) + + # 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() + # plt.plot(cubed) + # embed() + + + name = ['conv', 'df', 'df ds', 'corr', 'corr ds', 'global max', 'local max', 'cubed'] + var = [conv_b, am_df_b, am_df_ds_b, am_corr_b, am_corr_ds_b, maxima_interp_b, peaks_interp_b, cubed] + samp = [sampling, sampling, eod_fr, sampling, eod_fr, sampling, sampling, sampling] + # pp, f = ml.psd(eod_overlayed_chirp - np.mean(eod_overlayed_chirp), Fs=sampling, NFFT=nfft, noverlap=nfft / 2) + + 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 + +def test_different_resolutions(): + 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 + eod_fr = 537# eod fish reciever + + + a_fr = 1 # amplitude fish reciever + amplitude = a_fe = 0.2 # amplitude fish emitter + factor = 200 + sampling = eod_fr * factor + sampling = 100000 + sampling = 121730 + + sampling_fish = 500 + #start = 0 + #end = 2500 + #step = 10 + start = 510 + end = 3500 + end = eod_fr*5 + step = 500 + 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) + + start = 5 + end = 3500 + step = 250 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + bef_c = 1.1 + aft_c = 1 + bef_c = 0.02 + aft_c = 0.01 + bef_c = -2 + aft_c = -1 + + + + samp = [10000,10000,40000,11000,11234,100000, 110000,1100000] + fig = plt.figure() + ax = {} + nr = 4 + ax['second'+str(-1)] = fig.add_subplot(len(samp), nr, 0 * nr + 2) + ax['third'+str(-1)] = fig.add_subplot(len(samp), nr, 0 * nr + 3) + ax['forth' + str(-1)] = fig.add_subplot(len(samp), nr, 0 * nr + 4) + pp_max = [] + for i in range(len(samp)): + eod_fe = [eod_fr*0.75] + eod_fe = [0] + add = 1 + nfft = 4096 + e = 0 + # sampling = 121000 + #embed() + left_b = [bef_c * samp[i]] * len(beat_corr) + right_b = [aft_c * samp[i]] * 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, + samp[i], deviation_s, + d, eod_fr, a_fr, eod_fe, + phase_zero, p, size, s, + sigma, a_fe, deviation_dp, + 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) + if single == True: + eod_fe = [0] + add = 1 + time, time_cut, cut = find_times(left_c[e], right_c[e], 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) + eod_overlayed_chirp = eod_fish_r+1 + nfft = int(4096*samp[i]/samp[0]*2*2) + results = [[]] * 1 + name = ['cubed'] + var = [cubed] + var = cubed + time1 = time_fish + time2 = time_b + time1 = time_b + #samp = [sampling] + #i = 0 + pp, f = ml.psd(var - np.mean(var), Fs=samp[i], NFFT=nfft, + noverlap=nfft / 2) + #plt.figure() + + #plt.subplot(len(samp),3,i*3+1,sharex=True, sharey=True) + #ax[i] = fig.add_subplot(len(samp),3,i*3+1, sharex=ax[0]) + fig.add_subplot(len(samp), 4, i * 4 + 1) + plt.plot(f, pp, label = samp[i]) + #if i == 0: + # pp_max = (np.max(pp)) + plt.title(str(len(pp))) + #plt.ylim([0,pp_max]) + plt.xlim([0, 2000]) + # plt.subplot(len(samp), 3, i*3+2, color = 'red',sharex=True, sharey=True) + + ax['second'+str(i)] = fig.add_subplot(len(samp), nr, i * nr + 2,sharex=ax['second'+str(i-1)],sharey=ax['second'+str(i-1)]) + #embed() + plt.plot(time1, var[cut:-cut], color='red')#[cut:-cut] + # plt.subplot(len(samp), 3, i * 3 + 3,sharex=True, sharey=True) + #ax['third'+str(i)] = fig.add_subplot(1, 1, 1) + plt.xlim([time_b[0], -1.9]) + ax['third'+str(i)] = fig.add_subplot(len(samp), nr, i * nr + 3,sharey=ax['third'+str(i-1)], sharex=ax['third'+str(i-1)])#sharey=ax['third'+str(i)], sharex=ax['third'+str(i)] + plt.plot(time2, maxima_interp_b,color = 'red') + pp, f = ml.psd(eod_overlayed_chirp - np.mean(eod_overlayed_chirp ), Fs=samp[i], NFFT=nfft, + noverlap=nfft / 2) + plt.plot(time2, eod_overlayed_chirp ) + plt.xlim([time_b[0],-1.9]) + ax['forth' + str(i)] = fig.add_subplot(len(samp), nr, i * nr + 4, sharex=ax['forth' + str(i - 1)], + sharey=ax['forth' + str(i - 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.legend() + plt.show() + +def singlemodulation(beat_corr, deviation_s, a_fe, a_fr): + bef_c = 0 + aft_c = 1 + sampling = 100000 + i = 0 + fig = plt.figure() + ax = {} + pp_max = [] + add = 1 + nfft = 4096 + e = 0 + left_c = [bef_c * sampling] * len(beat_corr) + right_c = [aft_c * sampling] * len(beat_corr) + p = 0 + s = 0 + d = 0 + eod_fe = [0] + eod_fr = 200 + time, time_cut, cut = find_times(left_c[e], right_c[e], 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) + eod_overlayed_chirp = eod_fish_r+1 + nfft = int(4096*2*2) + ax[1] = fig.add_subplot(1,2,1)#sharey=ax['third'+str(i)], sharex=ax['third'+str(i)] + plt.plot(time, eod_overlayed_chirp ) + plt.xlim([0,0.05]) + ax[2] = fig.add_subplot(1,2,2) + pp, f = ml.psd(eod_overlayed_chirp, Fs=sampling, NFFT=nfft, + noverlap=nfft / 2) + plt.plot(f, pp) + plt.xlim([-50, 2000]) + plt.legend() + plt.savefig('../highbeats_pdf/psd_singlemodulation.pdf') + plt.show() + + +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 + 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 + eod_fr = 537# eod fish reciever + factor = 200 + sampling_fish = 500 + size = [120] + + d = 1 + x = [ 1.5, 2.5,0.5,] + x = [ 1.5] + sampling = 100000 + time_range = 200 * delta_t + deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling) + + + #sampling = 750 + #sampling = 522300 + + bef_c = -2 + aft_c = -1 + deviation_ms, deviation_s, deviation_dp = find_dev(x, sampling) + #minus_bef = bef_c/(delta*sampling)#minus_bef = -30 + #plus_bef = aft_c/(delta*sampling) + minus_bef = bef_c + plus_bef = aft_c + #plus_bef = -10 + eod_fe = np.array([510, 560,850,1010]) + eod_fr = 750 + eod_fe = np.array([eod_fr + 10, eod_fr + 100,eod_fr + eod_fr/2,eod_fr + eod_fr/2+100,eod_fr*3 + 10,eod_fr*4 + 10]) + a_fe = 0.2 + a_fr = 1 + 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,3,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) + + embed() + + start = 5 + end = 3500 + step = 250 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + singlemodulation(beat_corr, deviation_s, a_fe, a_fr) + + + embed() + delta_t = 1 + results = power_func(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) + 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) + + embed() + + + start = 0 + end = 2500 + step = 25 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + distance_metz_all_df_ds,distance_metz_all_corr_ds,period_shift,period_distance_c,period_distance_b,euc_all_in,euc_all_gm,euc_all_lm,euc_all,euc_all_ds,range_beat_all_ds, range_chirp_all_ds, range_chirp_all, range_beat_all, range_chirp_all_in, range_beat_all_in, range_beat_all_gm, range_chirp_all_gm, range_beat_all_lm, range_chirp_all_lm,beat_corr_org, distance_metz_all_ds, distance_wo_mean_all_ds,distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm,distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm,distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm,distance_wo_max_all_gm, p, s, d, size, phase_zero, beats, sampling,deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in,distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr,distance_metz_all, distance_wo_mean_all, distance_wo_max_all,\ + distance_wo_mean_max_all = distances_func(disc, bef_c, aft_c, 'w2', deviation_s, sigma, sampling, deviation_ms, beat_corr, size, phase_zero, delta_t, a_fr, a_fe, eod_fr, eod_fe, deviation_dp, show_figure = True, plot_dist = False, save = True) + plot_dist_interactiv2(distance_metz_all_df_ds,distance_metz_all_corr_ds,beat_corr, deviation_dp, delta_t, deviation_s, a_fe, a_fr, sigma,disc,win,period_shift,0,deviation_ms,period_distance_c,euc_all_in, euc_all_gm, euc_all_lm, euc_all, euc_all_ds,beat_corr_org,distance_metz_all_ds, distance_wo_mean_all_ds, distance_wo_mean_max_all_ds, distance_wo_max_all_ds, distance_metz_all_lm, distance_wo_mean_all_lm, distance_wo_mean_max_all_lm, distance_wo_max_all_lm, distance_metz_all_gm, distance_wo_mean_all_gm, distance_wo_mean_max_all_gm, distance_wo_max_all_gm, p, s,size, amplitude, phase_zero, beats, sampling, deviation_ms, distance_wo_mean_max_all_in, distance_wo_max_all_in, distance_metz_all_in, distance_wo_mean_all_in, eod_fe, eod_fr, distance_metz_all, distance_wo_mean_all, distance_wo_max_all, distance_wo_mean_max_all, share = False, show = True) + embed() + + + + + + + + + + + + + + + minus_bef = -30 + plus_bef = -10 + eod_fe = np.array([510, 560,850,1010]) + 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) + + + minus_bef = -4 + plus_bef = +4 + eod_fe = np.array([510, 580,750,1010,1250,1510]) + 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,3,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, 7.5],shift_phase = (2 * np.pi) / 4) + + + + minus_bef = -4 + plus_bef = +4 + eod_fe = np.array([510]) + eod_fe = np.array([510,750]) + eod_fe = np.array([580])#[510] + 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] + pp = [1,3,6,9]#[2,7] + #embed() + plot_eod_phase(beat_corr, minus_bef, plus_bef,2,2,a_fe, delta_t, a_fr, size, beat_corr, 0, pp, a_fe, + phase_zero, deviation_ms, eod_fe, deviation_dp, eod_fr, sampling, factor,fs = [6.2992, 5],shift_phase = (2 * np.pi) / 4) + + embed() + + + diff --git a/rotated.py b/rotated.py new file mode 100644 index 0000000..0eee1cd --- /dev/null +++ b/rotated.py @@ -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]) + #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)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() \ No newline at end of file diff --git a/singlecellexample5.py b/singlecellexample5.py new file mode 100644 index 0000000..ad02716 --- /dev/null +++ b/singlecellexample5.py @@ -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 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() diff --git a/singlecellexample5_all.py b/singlecellexample5_all.py new file mode 100644 index 0000000..8e54bf7 --- /dev/null +++ b/singlecellexample5_all.py @@ -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 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() diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.pdf index f9715c0..febbabb 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.png index 7215bac..b81d093 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-ak-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.pdf index 1b469a2..cfdf324 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.png index 77e5076..cdf78d3 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-an-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.pdf index 7de8ed3..70f9ab5 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.png index 90ceb65..789ec9a 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-aq-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.pdf index c5b9a38..3408255 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.png index 6f5662f..e6c340b 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-au-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.pdf index 668d5dc..a74b071 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.png index 56657fb..6d7a99c 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-av-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.pdf index ceb89ec..568cdfc 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.png index 2d610c5..3fd53b6 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-ax-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.pdf index 8571970..acc6816 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.png index 5b1771b..597ed3e 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-az-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.pdf index b93da65..7e4eed1 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.png index 65be610..6f57068 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bb-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.pdf index 57850e8..1c1ff5b 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.png index 70a4561..86b2d67 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bd-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.pdf index d58c614..225b014 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.png index 178ef7d..4ce8713 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bh-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.pdf index c36f59e..7940acd 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.png index b588d66..0a4864b 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bl-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.pdf index 995c7da..50b4c67 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.png index 59f63b5..06e8d04 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bp-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.pdf index 0ea8f9c..887c6c0 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.png index e430241..115b22a 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-bt-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.pdf index bfe80dd..6220de3 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.png index de5432e..c382655 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-by-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.pdf index 26288bb..14da18f 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.png index 275e95f..3902f18 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-ca-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.pdf index b6b28a1..e6489db 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.png index 23ff1a4..f4c5801 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-05-07-cc-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.pdf index 636617c..cffe839 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.png index c4d656a..21cae19 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-09-23-ah-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.pdf index 6fd4c0f..68c2892 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.png index 457a49b..367a814 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-09-23-aj-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.pdf index 35a2b0a..34bd7cf 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.png index 7e95382..d617959 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-09-23-an-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.pdf index 8a9eb3f..129fc92 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.png index abdfaee..67f33fc 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-ah-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.pdf index 52815d6..485c66d 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.png index 9cdb75a..dd67ecd 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-ai-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.pdf index 65295d6..f397bb4 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.png index 69e58b9..755c3db 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-aj-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.pdf index 72bc7d8..a6f8dc6 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.png index 371aa62..d04ac4f 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-al-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.pdf index 08b76c5..4ff17fb 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.png index 818fc78..0259066 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-am-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.pdf index e11d70e..45750b0 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.png index 6da5b8c..51647a9 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-an-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.pdf index 020e937..9259459 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.png index e77f53e..d8a9e89 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-ar-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.pdf index 4cc875a..e72ab9e 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.png index ddb72e1..82c3f0c 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-21-au-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.pdf index 57dc57d..7be0513 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.png index 30dd6ad..140f9dd 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-aa-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.pdf index e9a57d0..ac6adf1 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.png index 11cd109..8de3a04 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-ac-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.pdf index d6a645e..0328957 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.png index 7a83c85..8693bb9 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-af-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.pdf index 3a6dc64..a056219 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.png index 1750fb9..44f180b 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-ag-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.pdf index 20ff8b9..d11e3ea 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.png index 4bd3b6e..4728d04 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-ah-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.pdf index b471a63..c576924 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.png index 0c3cc6e..27da092 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-ai-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.pdf index e7ee74f..98492fc 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.png index 808de5f..88435eb 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-aj-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.pdf index 59477ce..84c52d1 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.png index 1e99331..85c45b7 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-10-28-ak-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.pdf index f6ccd0d..0e78e5c 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.png index cbe4b14..d3475b8 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-11-08-aa-invivo-1orig05notlog.png differ diff --git a/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.pdf b/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.pdf index 68b2ac0..7cca298 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.pdf and b/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.pdf differ diff --git a/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.png b/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.png index 9e106e4..834d3cf 100644 Binary files a/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.png and b/singlecellexamples5_all/singlecellexample52019-11-08-ab-invivo-1orig05notlog.png differ diff --git a/singlemodulation.py b/singlemodulation.py new file mode 100644 index 0000000..756b644 --- /dev/null +++ b/singlemodulation.py @@ -0,0 +1,87 @@ +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 plot_eod_chirp import find_dev, find_beats, find_times, find_periods + + +def singlemodulation(beat_corr, deviation_s, a_fe, a_fr): + bef_c = 0 + aft_c = 1 + sampling = 100000 + i = 0 + fig = plt.figure(figsize = (6,4)) + ax = {} + pp_max = [] + add = 1 + nfft = 4096 + e = 0 + left_c = [bef_c * sampling] * len(beat_corr) + right_c = [aft_c * sampling] * len(beat_corr) + p = 0 + s = 0 + d = 0 + eod_fe = [0] + eod_fr = 200 + time, time_cut, cut = find_times(left_c[e], right_c[e], 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) + eod_overlayed_chirp = eod_fish_r+1 + nfft = int(4096*2*2) + ax[1] = fig.add_subplot(1,2,1)#sharey=ax['third'+str(i)], sharex=ax['third'+str(i)] + plt.plot(time, eod_overlayed_chirp ) + plt.xlim([0,0.05]) + ax[1].set_ylabel('mv') + ax[1].set_xlabel('Time [ms]') + ax[2] = fig.add_subplot(1,2,2) + pp, f = ml.psd(eod_overlayed_chirp, Fs=sampling, NFFT=nfft, + noverlap=nfft / 2) + ax[2].set_ylabel('psd') + ax[2].set_xlabel('Frequency [Hz]') + plt.plot(f, pp) + plt.xlim([-50, 2000]) + plt.subplots_adjust(wspace = 0.45) + plt.legend() + plt.savefig('singlemodulation.pdf') + plt.show() + + +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 + 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 + eod_fr = 537# eod fish reciever + factor = 200 + sampling_fish = 500 + sampling = 100000 + 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) + a_fe = 0.5 + a_fr = 1 + start = 5 + end = 3500 + step = 250 + eod_fe, beat_corr, beats = find_beats(start,end,step,eod_fr) + singlemodulation(beat_corr, deviation_s, a_fe, a_fr) diff --git a/temporalplot.py b/temporalplot.py new file mode 100644 index 0000000..cbfca89 --- /dev/null +++ b/temporalplot.py @@ -0,0 +1,507 @@ +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 + +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 + 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,input = ['2019-10-21-aa-invivo-1'],sigma = [0.00005,0.00025,0.0005, 0.002]): + sampling_rate = 40000 + colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B'] + plt.rcParams['lines.linewidth'] = 1.5 + plt.rcParams['lines.markersize'] = 6 + #data = pd.read_pickle('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) + wish_df = 150 + 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 = {} + ax[0] = plt.subplot(grid[0]) + ax[0].plot(f[0], p[0], color=colors[0]) + ax[0].plot(eodf - 1, np.max(p[0][int(eodf / stepsize) - 5:int(eodf / stepsize) + 5]) * 0.95, color='k', + marker='o', linestyle='None', label='EODf') # = '+str(int(eodf))+' Hz') + #embed() + ax[0].fill_between(f[1], [max(p[0])]*len(f[1]), facecolor='lightgrey', edgecolor='grey') + ax[0].plot(df[0], np.max(p[0][int(abs(df[0]) / stepsize) - 5:int(abs(df[0]) / stepsize) + 5]) * 0.95, + color=colors[1], marker='o', linestyle='None', label='Df') + ax[0].plot(df[0] + eodf, p[0][int((df[0] + eodf) / stepsize) + 1], color=colors[2], marker='o', linestyle='None', + label='stimulus') + ax[0].legend(bbox_to_anchor=(0.4, 1, 0.6, .1), loc='lower left', + ncol=3, mode="expand", borderaxespad=0.) + ax[0].set_xlim([0, 2000]) + wide = 2 + for i in range(len(sigma)): + ax[i+1] = plt.subplot(grid[i+1]) + plot_filter(ax, i+1, f[1+i], p, colors, gauss[1+i], eodf, stepsize, wide, df[1+i]) + ax[i+1].set_xlim([0, 2000]) + #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))-1].set_xlabel('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 range(len(df)): + ax[i].spines['right'].set_visible(False) + ax[i].spines['top'].set_visible(False) + cols = grid.ncols + rows = grid.nrows + ax[int(cols*(rows/2))-1].set_ylabel(' power spectral density [Hz²/Hz]') + #ax[2].set_ylabel('Hz²/Hz') + #ax[3].set_ylabel('Hz²/Hz') + #ax[0].set_ylabel('Hz²/Hz') + for i in range(len(df)): + ax[i].axvline(x = eodf/2, color = 'black', linestyle = 'dashed') + plt.tight_layout() + #fig.label_axes() + +def plot_filter(ax, ax_nr, f, p4, colors, gauss3, eodf, stepsize, wide, df): + ax[ax_nr].plot(f, p4[ax_nr], color=colors[0]) + ax[ax_nr].fill_between(f, max(p4[0]) * gauss3 ** 2, facecolor='lightgrey', edgecolor='grey') + ax[ax_nr].plot(eodf, np.max(p4[ax_nr][int(eodf / stepsize) - wide:int(eodf / stepsize) + wide]) * 0.95, color='k', marker='o', + linestyle='None') + ax[ax_nr].plot(abs(df), np.max(p4[ax_nr][int(abs(df) / stepsize) - wide:int(abs(df) / stepsize) + wide]) * 0.95, + color=colors[1], marker='o', linestyle='None') + ax[ax_nr].plot(df + eodf, + np.max(p4[ax_nr][int((df + eodf) / stepsize) - wide:int((df + eodf) / stepsize) + wide]) * 0.95, + color=colors[2], marker='o', linestyle='None') + 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, sigma = ['original','05','2'],lw = 0.7): + mean1 = pd.read_pickle('mean.pkl') + + colors = ['#BA2D22', '#F47F17', '#AAB71B', '#3673A4', '#53379B'] + inch_factor = 2.54 + + half_page_width = 7.9 / inch_factor + intermediate_width = 12 / inch_factor + whole_page_width = 16 * 2 / inch_factor + + small_length = 6 / inch_factor + intermediate_length = 12 * 1.5 / inch_factor + max_length = 25 / inch_factor + whole_page_width = 6.7 + 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)): + version =[[]]*len(sigma) + version2 = [[]] * len(sigma) + dev = [[]] * len(sigma) + all_means2 = mean1[mean1['type'] == 'amp max' + ' mean'] + for i in range(len(sigma)): + try: + version[i] = all_means[all_means['dev'] == sigma[i]] + version2[i] = all_means2[all_means2['dev'] == sigma[i]] + except: + version[i] = [] + print(sigma[i]) + dev[i] = sigma[i] + #if sigma[i] == 'original': + # titles[i] = 'binary' + #else: + # titles[i] = str(np.float(sigma[i])/1000 + + #original = all_means[all_means['dev'] == 'original'] + # m005 = all_means[all_means['dev'] == '005'] + + #m2 = all_means[all_means['dev'] == '2'] + # fig, ax = plt.subplots(nrows=4, ncols = 3, sharex=True) + #versions = [original, m05, m2] # m005, + #dev = ['original', '05', '2']#'005', + titels = ['binary','0.005 ms','0.25 ms','0.5 ms','2 ms'] + #fig, ax = plt.subplots(nrows=2, ncols=len(sigma), sharex=True) + #plt.subplot(axis) + #grid = gridspec.GridSpec(2, 3, wspace=0.0, height_ratios=[6, 2], width_ratios=[1,0.5,3], hspace=0.2) + + #versions = [original, m05, m2]#m005, + ax = {} + for i in range(len(version)): + if len(version[i])>0: + print(i) + plots = gridspec.GridSpecFromSubplotSpec(2, 1, + subplot_spec=grid[i], wspace=0, hspace=0.5) + keys = [k for k in version[i]][2::] + try: + data = np.array(version[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] = plt.subplot(plots[0]) + ax[0].set_title(titels[i]) + ax[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() + ax[0].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]), + color='mistyrose') + #embed() + + ax[0].fill_between(np.array(keys)[sim], list(data[sim] + std[sim]), list(data[sim] - std[sim]), color='pink') + + ax[0].plot(x / 500, corr / 500 + 1, color='grey', linestyle='--', label='AMf') + ax[0].plot(np.array(keys)[sim], data[sim], color='red', linewidht = lw) + # plt.fill_between(np.array([0,1]),np.array([0,0]),np.array([1,1])) + + #embed() + ax[1] = plt.subplot(plots[1]) + name = 'amp max' + np.unique(mean1['type']) + keys = [k for k in version2[i]][2::] + try: + data = np.array(version2[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[1].fill_between(np.array(keys)[sim], list(std95[sim]), list(std05[sim]), + color='gainsboro') + ax[1].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[1].plot(np.array(keys)[sim], data[sim], color='black') + # ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %') + # ax[0].plot(data1.x, data1.freq20, color=colors[1], label='20 %') + ax[0].spines['right'].set_visible(False) + ax[0].spines['top'].set_visible(False) + ax[1].spines['right'].set_visible(False) + ax[1].spines['top'].set_visible(False) + 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[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,name='amp max', nr=1) + # 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].set_ylabel('MPF [EODf]') + #ax[1, 0].set_ylabel('Modulation depth') + ax[1].set_ylabel('Modulation Peak') + #ax[2, 0].set_ylabel('Whole modulation ') + + + + #for i in range(3): + ax[0].set_xlabel('stimulus frequency [EODf]') + #ax[2, 1].set_xlabel('stimulus frequency [EODf]') + plt.subplots_adjust(bottom = 0.13) + #fig.tight_layout() + # fig.label_axes() + + + + + +if __name__ == "__main__": + data = ['2019-10-21-aa-invivo-1'] + #fig, ax = plt.subplots(nrows=5, sharex=True, sharey=True) + trans = True + 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 = [4, 4] + hd = [1] + left = 0 + right = 1 + default_settings(data, intermediate_width=7.4, intermediate_length=6, ts=6, ls=10, fs=10) + grid = gridspec.GridSpec(row, col, wspace=0.35,height_ratios = hd, width_ratios=wd, hspace=0.2)#, + + axis = gridspec.GridSpecFromSubplotSpec(row_small,col_small, + subplot_spec=grid[0,0], wspace=0.15, hspace=0.1) + plot_example_ps(axis,input = ['2019-10-21-aa-invivo-1'],sigma = sigma) + #plt.show() + #embed() + #fig.savefig() + axis = gridspec.GridSpecFromSubplotSpec(row_small,col_small, + subplot_spec=grid[left,right], wspace=0.15, hspace=0.3) + #embed() + plot_mean_cells(axis, sigma = ['original','05','2'])#'005','025', + plt.savefig('temporalplot.pdf') + plt.savefig('../highbeats_pdf/temporalplot.pdf') + plt.savefig('temporalplot'+t+'.pdf') + plt.show() + # plt.savefig('../results/Ramona/ma_powerspecs_negative_df' + d + '.pdf') + # plt.show() + # plt.close() + # embed() + # plot_single_tublerones() # original beat_activity \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..e69de29