from read_chirp_data import *
from func_chirp import *
from utility import *
import matplotlib.pyplot as plt
import numpy as np
from IPython import embed



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



#for dataset in data:
eod = read_chirp_eod(os.path.join(data_dir, dataset))
times = read_chirp_times(os.path.join(data_dir, dataset))
df_map = map_keys(eod)

chirp_eod_plot(df_map, eod, times)
plt.close()


#ACHTUNG: df für beide Plots anpassen!
#momentan per Hand durch alle Frequenzen
freq = list(df_map[-100])
ls_mod = []
ls_beat = []
for k in freq: 
	e1 = eod[k]
	zeit = np.asarray(e1[0])
	ampl = np.asarray(e1[1])

	ct = times[k]
	for chirp in ct:
		time_cut = zeit[(zeit > chirp-10) & (zeit < chirp+10)]
		eods_cut = ampl[(zeit > chirp-10) & (zeit < chirp+10)]
		beat_cut = ampl[(zeit > chirp-55) & (zeit < chirp-10)]
		
		chirp_mod = np.std(eods_cut) #Std vom Bereich um den Chirp
		ls_mod.append(chirp_mod)
		ls_beat.extend(beat_cut)

beat_mod = np.std(ls_beat) #Std vom Bereich vor dem Chirp
plt.figure()
plt.scatter(np.arange(0,len(ls_mod),1), ls_mod)
plt.scatter(np.arange(0,len(ls_mod),1), np.ones(len(ls_mod))*beat_mod, color = 'violet')
plt.close()



#Chirps einer Phase zuordnen - zusammen plotten

dct_phase = {}
chirp_spikes = read_chirp_spikes(os.path.join(data_dir, dataset))
df_map = map_keys(chirp_spikes)
sort_df = sorted(df_map.keys())

num_bin = 12
phase_vec = np.arange(0, 1+1/num_bin, 1/num_bin)

for i in sort_df:
	freq = list(df_map[i])
	dct_phase[i] = []
	for k in freq:
		for phase in chirp_spikes[k]:
			dct_phase[i].append(phase[1])

plt.figure()
plt.scatter(dct_phase[-100], ls_mod)
plt.title('Change of std depending on the phase where the chirp occured')
plt.show()
