from read_baseline_data import *
from read_chirp_data import *
from utility import *
import matplotlib.pyplot as plt
import numpy as np



def map_keys(input):
#gibt ein Dict mit den Keys eines Dict aus, aber als Int
	df_map = {} 
	for k in input.keys():
		freq = k[1]
		df = int(freq.strip('Hz'))
		if df in df_map.keys():
			df_map[df].append(k)
		else:
			df_map[df] = [k]
	return df_map



def spike_rates(sort_df, df_map, chirp_spikes):
#damit wird sowohl die individuelle Rate pro Trial, als auch die Gesamt-Feuerrate berechnet
	dct_rate = {}
	over_spikes = []
	for i in sort_df:
		freq = list(df_map[i])
		dct_rate[i] = []
		for k in freq:
			for phase in chirp_spikes[k]:
				spikes = chirp_spikes[k][phase]
				rate = len(spikes)/ 1.2
				dct_rate[i].append(rate)
				over_spikes.extend(spikes)

	duration = 1.2 *1600 #1200ms für 16 Trials 
	overall_r = len(over_spikes)/ duration
	over_r = int(overall_r)
	return(dct_rate, over_r)



def plot_df_spikes(sort_df, dct_rate):
#gibt die Feuerrate gegen die Frequenz aufgetragen
	ls_mean = []
	for h in sort_df:	
		mean = np.mean(dct_rate[h])
		ls_mean.append(mean)		
		plt.plot(np.arange(0,len(dct_rate[h]),1),dct_rate[h], label = h)

	plt.legend()
	plt.title('Firing rate of the cell for all trials, sorted by df')
	plt.xlabel('# of trials')
	plt.ylabel('Instant firing rate of the cell')
	return(ls_mean)



def adaptation_df(sort_df, dct_rate):
	adapt = []
	for d in sort_df:
		spur =  dct_rate[d]
		start = spur[0:-1:10]
		stop = spur[9:len(spur):10]
		diff = np.asarray(start) - np.asarray(stop)
		adapt.extend(diff)

	return(adapt)
