from read_baseline_data import *
from read_chirp_data import *
from func_spike import *
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed #Funktionen importieren



data_dir = "../data"
data_base = ("2018-11-09-ab-invivo-1", "2018-11-09-ad-invivo-1", "2018-11-13-aa-invivo-1", "2018-11-13-ab-invivo-1", "2018-11-13-ad-invivo-1", "2018-11-13-af-invivo-1", "2018-11-13-ag-invivo-1", "2018-11-13-ah-invivo-1", "2018-11-13-ai-invivo-1", "2018-11-13-aj-invivo-1", "2018-11-13-ak-invivo-1", "2018-11-13-al-invivo-1", "2018-11-14-ab-invivo-1", "2018-11-14-ac-invivo-1", "2018-11-14-ad-invivo-1", "2018-11-14-ae-invivo-1", "2018-11-14-af-invivo-1", "2018-11-14-ag-invivo-1",  "2018-11-14-aj-invivo-1", "2018-11-14-ak-invivo-1", "2018-11-14-al-invivo-1", "2018-11-14-am-invivo-1", "2018-11-14-an-invivo-1", "2018-11-20-ab-invivo-1", "2018-11-20-ac-invivo-1", "2018-11-20-ad-invivo-1", "2018-11-20-ae-invivo-1", "2018-11-20-af-invivo-1", "2018-11-20-ag-invivo-1", "2018-11-20-ah-invivo-1", "2018-11-20-ai-invivo-1")
data_chirps = ("2018-11-09-ad-invivo-1", "2018-11-09-ae-invivo-1", "2018-11-09-ag-invivo-1", "2018-11-13-aa-invivo-1", "2018-11-13-ac-invivo-1", "2018-11-13-ad-invivo-1", "2018-11-13-ah-invivo-1", "2018-11-13-ai-invivo-1", "2018-11-13-aj-invivo-1", "2018-11-13-ak-invivo-1", "2018-11-13-al-invivo-1", "2018-11-14-aa-invivo-1", "2018-11-14-ac-invivo-1", "2018-11-14-ad-invivo-1", "2018-11-14-af-invivo-1", "2018-11-14-ag-invivo-1", "2018-11-14-ah-invivo-1", "2018-11-14-ai-invivo-1", "2018-11-14-ak-invivo-1", "2018-11-14-al-invivo-1", "2018-11-14-am-invivo-1", "2018-11-14-an-invivo-1", "2018-11-20-aa-invivo-1", "2018-11-20-ab-invivo-1", "2018-11-20-ac-invivo-1", "2018-11-20-ad-invivo-1", "2018-11-20-ae-invivo-1", "2018-11-20-af-invivo-1", "2018-11-20-ag-invivo-1", "2018-11-20-ah-invivo-1", "2018-11-20-ai-invivo-1") 

dataset = "2018-11-13-ad-invivo-1"
inch_factor = 2.54

#for dataset in data_base:

spike_times = read_baseline_spikes(os.path.join(data_dir, dataset))	
spike_iv = np.diff(spike_times)
x = np.arange(0.001, 0.01, 0.0001)

fig, ax = plt.subplots(figsize=(20/inch_factor, 10/inch_factor))
plt.hist(spike_iv,x, color = 'darkblue')

mu = np.mean(spike_iv)
sigma = np.std(spike_iv)
cv = sigma/mu

plt.title('A.lepto ISI Histogramm', fontsize = 24)
plt.xlabel('duration ISI[ms]', fontsize = 22)
plt.ylabel('number of ISI', fontsize = 22)
plt.tick_params(axis='both', which='major', labelsize = 22)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.tight_layout()
plt.show()



#for dataset in data_chirps:

	#Nyquist-Theorem Plot:
chirp_spikes = read_chirp_spikes(os.path.join(data_dir, dataset))
times = read_chirp_times(os.path.join(data_dir, dataset))
eod = read_chirp_eod(os.path.join(data_dir, dataset))
df_map = map_keys(chirp_spikes)
sort_df = sorted(df_map.keys())

dct_rate, over_r = spike_rates(sort_df, df_map, chirp_spikes)

plt.figure()
fig, ax = plt.subplots(figsize=(20/inch_factor, 10/inch_factor))
ls_mean = plot_df_spikes(sort_df, dct_rate)
plt.show()



	#mittlere Feuerrate einer Frequenz auf Frequenz:

fig, ax = plt.subplots(figsize=(20/inch_factor, 10/inch_factor))
plt.plot(np.arange(0,len(ls_mean),1),ls_mean, color = 'darkblue')
plt.scatter(np.arange(0,len(ls_mean),1), np.ones(len(ls_mean))*over_r, color = 'green')
plt.title('Mean firing rate of a cell for a range of frequency differences', fontsize = 24)
plt.xticks(np.arange(1,len(sort_df),1), (sort_df))
plt.xlabel('Range of frequency differences [Hz]', fontsize = 22)
plt.ylabel('Mean firing rate of the cell', fontsize = 22)
plt.tick_params(axis='both', which='major', labelsize = 18)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.tight_layout()
plt.show()



	#Adaption der Zellen:
	#wie viel Prozent der Anfangsrate macht die Adaption von Zellen aus?

adapt = adaptation_df(sort_df, dct_rate)
fig, ax = plt.subplots(figsize=(20/inch_factor, 10/inch_factor))
plt.boxplot(adapt)
plt.title('Adaptation of cell firing rate during a trial', fontsize = 24)
plt.xlabel('Cell', fontsize = 22)
plt.ylabel('Adaptation size [Hz]', fontsize = 22)
plt.tick_params(axis='both', which='major', labelsize = 18)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.tight_layout()
plt.show()	



'''

	#Vatriablen speichern, die man für die Übersicht aller Zellen braucht
	name = str(dataset.replace('-invivo-1', ''))
	f = open('../results/Nyquist/Ny_' + name + '.txt' , 'w') 
	f.write(str(sort_df)) 
	f.write(str(df_map)) 
	f.write(str(chirp_spikes)) 
	f.write(str(times))
	f.write(str(ls_mean))
	f.write(str(over_r)) 
	f.write(str(adapt))
	f.close() 
'''