from read_baseline_data import *
from read_chirp_data import *
from utility import *
#import nix_helpers as nh 
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed #Funktionen imposrtieren


data_dir = "../data"
dataset = "2018-11-09-ad-invivo-1"
#data = ("2018-11-09-aa-invivo-1", "2018-11-09-ab-invivo-1", "2018-11-09-ac-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-09-af-invivo-1", "2018-11-09-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-aa-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-aa-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")



spike_times = read_baseline_spikes(os.path.join(data_dir, dataset))
#inst_frequency = 1. / np.diff(spike_times)
spike_rate = np.diff(spike_times)


x = np.arange(0.001, 0.01, 0.0001)
plt.hist(spike_rate,x)

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

plt.title('A.lepto ISI Histogramm', fontsize = 14)
plt.xlabel('duration ISI[ms]', fontsize = 12)
plt.ylabel('number of ISI', fontsize = 12)

plt.xticks(fontsize = 12)
plt.yticks(fontsize = 12)
plt.show()




#Nyquist-Theorem Plot:

chirp_spikes = read_chirp_spikes(os.path.join(data_dir, dataset))
df_map = map_keys(chirp_spikes)
ls_rate = {}
for i in df_map.keys():
	freq = list(df_map[i])
	ls_rate[i] = []
	for k in freq:
		for phase in chirp_spikes[k]:
			spikes = chirp_spikes[k][phase]
			rate = len(spikes)/ 1.2
			ls_rate[i].append(rate)


plt.figure()
sort_df =  sorted(df_map.keys(),reverse = False)
print(sort_df)

for i in sort_df:
	plt.plot(np.arange(0,len(ls_rate[i]),1),ls_rate[i], label = i)

plt.vlines(10, ymin = 200, ymax = 300)
plt.vlines(30, ymin = 200, ymax = 300)
plt.vlines(50, ymin = 200, ymax = 300)
plt.vlines(70, ymin = 200, ymax = 300)
plt.vlines(90, ymin = 200, ymax = 300)
plt.vlines(110, ymin = 200, ymax = 300)
plt.vlines(130, ymin = 200, ymax = 300)
plt.vlines(150, ymin = 200, ymax = 300)
plt.legend()
plt.show()



#mittlere Feuerrate einer Frequenz auf Frequenz

plt.figure()
ls_mean = []
for i in sort_df:
	mean = np.mean(ls_rate[i])
	ls_mean.append(mean)

plt.plot(np.arange(0,len(ls_mean),1),ls_mean)
plt.show()
