# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 10:08:49 2019

@author: Ralle
"""
import scipy.io
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import KFold
from sklearn import preprocessing
from sklearn.decomposition import PCA
import os
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pickle


def concat_channels(eeg_events):#channels*EEG_value*img 
    #for putting all channels in a single vector. return is n_img x 17600
    n_channels,n_samples,n_img = np.shape(eeg_events)
    concat_all = np.zeros((n_img,n_channels*n_samples))
    for i in range(n_img): #for each image
        concat_row = []
        for j in range(n_channels): #for each channel of channels
            #concat_data[i] = np.concatenate((concat_data[i],eeg_events[j,:,i]),axis=1)
            concat_row = np.concatenate((concat_row,eeg_events[j,:,i]))
        concat_all[i] = concat_row
    return concat_all #[n_img*17600]


def is_animal(class_vector):#creates vector of 1 if animal and 0 if not
    n = np.size(class_vector)
    bin_vector = np.zeros(n)
    for i in range(n):
        if class_vector[i] == 'animal':
            bin_vector[i] = 1
        else:
            bin_vector[i] = 0
    return bin_vector



os.chdir('C:/Users/Ralle/Desktop/Advanced Machine Learning Project/Mindreader folder/data')

n_experiments = 4


full_data_matrix = []
full_superClass_array = []
full_subClass_array = []
for i in range(n_experiments):
    eeg_events = scipy.io.loadmat('exp' + str(i+1) + '\eeg_events.mat')
    image_order = np.genfromtxt('exp' + str(i+1) + '\image_order.txt', delimiter="\t", skip_header = True, dtype=(str))#
    data = eeg_events["eeg_events"]
    concat_data = concat_channels(data)
    if i == 0:
        full_data_matrix = concat_data
        full_superClass_array = image_order[:,0]
        full_subClass_array = image_order[:,1]
    else:
        full_data_matrix  = np.concatenate((full_data_matrix,concat_data))
        full_superClass_array  = np.concatenate((full_superClass_array,image_order[:,0]))
        full_subClass_array  = np.concatenate((full_subClass_array,image_order[:,1]))

#Change superclass toAnimal or not:
full_isAnimal_array = is_animal(full_superClass_array)

#Normalize data
full_normalized_array = preprocessing.scale(full_data_matrix)#normalize




def mean_over_channels(data,n_channels):
    n_obs = np.size(data,0)
    n_dim = np.size(data,1)
    mean_channel = np.zeros((n_obs,int(n_dim/n_channels)))
    for i in range(n_obs):
        for j in range(n_channels):
            mean_channel[i,:] += data[i,j:j+(int(n_dim/n_channels))]
    return mean_channel



def mean_over_classes(data,classes):
    unique_classes = np.unique(classes)
    n_classes = np.size(unique_classes)
    n_obs = np.size(data,0)
    n_dim = np.size(data,1)
    mean_data = np.zeros((n_classes,n_dim))
    count = np.zeros((n_classes))
    for i in range(n_classes):
        for j in range(n_obs):
            if classes[j] == unique_classes[i]:
                mean_data[i,:] += data[j,:]
                count[i]+=1
        mean_data[i,:] = mean_data[i,:]/count[i]
    return mean_data


plt.figure()
plt.plot(full_data_matrix[0,:])
plt.show()

mean_channels = mean_over_channels(full_data_matrix,64)



#########x_axis = np.multiply(np.arange(576),1.125)
plt.figure()
n_dim = 576
n_channels = 64
divider = 10 #only see every x'th channel
for i in range(int(64/divider)):
    i = i*divider
    j = i*n_dim
    mean_data = mean_over_classes(full_data_matrix[:,j:j+n_dim],full_subClass_array)
    plt.subplot(int(64/divider),1,int(i/divider)+1)
    plt.plot(mean_data[0,:],linewidth=3)
    plt.plot(mean_data[1,:],linewidth=3)
    plt.plot(mean_data[2,:],linewidth=3)
    plt.plot(mean_data[3,:],linewidth=3)
    plt.plot(mean_data[4,:],linewidth=3)
    plt.plot(mean_data[5,:],linewidth=3)
    ax = plt.gca()
    ax.yaxis.set_visible(False)
    ax.xaxis.set_visible(False)
    if i == 0:
        plt.title("Mean of classes for every " + str(divider) + "'th channel",fontsize=27)
    if i == 3*divider:    
        plt.legend(np.unique(full_subClass_array),loc=4,prop={'size': 25})
    #plt.ylabel("channel " + str(i),rotation=90)
    #plt.legend(np.unique(full_subClass_array))
    
                
ax.xaxis.set_visible(True)
for i in ax.xaxis.get_major_ticks():
    i.label.set_fontsize(25)
    
    
plt.xlabel("Sample #",size=27)
plt.show()










"""
PCA stuff
"""

#################PCA AND VARIANCE EXPLAINED
#pca = PCA(svd_solver='auto')#PCA with all components
#pca.fit(full_normalized_array)
#pca_cumsum = np.cumsum(pca.explained_variance_ratio_)*100
#
#plt.figure()
#plt.plot(pca_cumsum)
#plt.grid()
#plt.ylabel('% Variance Explained')
#plt.xlabel('# of Features')
#plt.title('PCA Analysis')
#plt.ylim(0,100.5)
#plt.plot(range(2160),np.repeat(95,2160))
#plt.legend(['Cumulative variance explained','95%'])
#plt.show()
#
############################# PCA_ 655 components holds 95% of variance
#pca = PCA(svd_solver='auto', n_components = 655)#PCA with all components
#pca.fit(full_normalized_array)
#full_normPCA_array = pca.transform(full_normalized_array)
#
############################# PCA_ 128 components holds 75% of variance
#pca = PCA(svd_solver='auto', n_components = 128)#PCA with all components
#pca.fit(full_normalized_array)
#full_normPCA_array = pca.transform(full_normalized_array)
#
#
############################# CREATE TEST AND TRAINING FOR THE MODELS
##X_train, X_test, train_index, test_index = train_test_split(full_normPCA_array,range(np.size(full_isAnimal_array)),test_size=0.20)
#
#os.chdir('C:/Users/Ralle/Documents/GitHub/AdvancedMachineLearning/KNN_SVM_RNN_new_data')
#
##with open("train_indexes.txt", "wb") as fp:   #Pickling
##    pickle.dump(train_index, fp)
#
##with open("test_indexes.txt", "wb") as fp:   #Pickling
##    pickle.dump(test_index, fp)
#
#
##To load:
##with open("train_indexes.txt", "rb") as fp:   # Unpickling
##    b = pickle.load(fp)
#
