# -*- coding: utf-8 -*-
"""
Created on Sat Mar 17 00:21:10 2018

@author: Admin
"""

from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense, Dropout
from numpy import genfromtxt
import numpy
import codecs
import csv
from sklearn.metrics import confusion_matrix,roc_auc_score,roc_curve,auc,accuracy_score
from sklearn.metrics import precision_score,recall_score,classification_report
import matplotlib.pyplot as plt
from keras.utils import np_utils
import numpy as np




# create a sequence classification instance
def get_sequence(n_timesteps,time):
    x = [data[index] for index in range((time*10)+1 ,(time*10)+10+1)]
    x = numpy.delete(x, (data.shape[1]-1), axis=1)
    x= numpy.array(x)
    y = [Y[index] for index in range((time*10) ,(time*10)+10)]
    y=numpy.array(y)
    # reshape input and output data to be suitable for LSTMs
    X = x.reshape(1, n_timesteps, (data.shape[1]-1))
    y = y.reshape(1, n_timesteps, y.shape[1])
    return X, y



data = genfromtxt('Experiment4.csv', delimiter=',')
m =[data[i][-1] for i in range(1,7671)]
Y = np_utils.to_categorical(m)


# define problem properties
n_timesteps = 10
# define LSTM
model = Sequential()
model.add(LSTM(30,input_shape = (None, (data.shape[1]-1)),return_sequences=True))
model.add(Dropout(0.30))
model.add(Dense(3, activation='softmax'))
#model.add(Dense(1, activation='sigmoid'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

print(model.summary())

# train LSTM
for epoch in range(0,149):
	# generate new random sequence
	X,y = get_sequence(n_timesteps,epoch)
	# fit model for one epoch on this sequence
	model.fit(X, y,batch_size=1, verbose=0)
    
for epoch in range(192,343):
	# generate new random sequence
	X,y = get_sequence(n_timesteps,epoch)
	# fit model for one epoch on this sequence
	model.fit(X, y,batch_size=1, verbose=0)
    
for epoch in range(385,534):
	# generate new random sequence
	X,y = get_sequence(n_timesteps,epoch)
	# fit model for one epoch on this sequence
	model.fit(X, y,batch_size=1, verbose=0)    
    
for epoch in range(576,727):
	# generate new random sequence
	X,y = get_sequence(n_timesteps,epoch)
	# fit model for one epoch on this sequence
	model.fit(X, y,batch_size=1, verbose=0)
countp=0
countn=0

ypredicted =  []
yactual = []



for d in range(150,191):
        X,y = get_sequence(n_timesteps,d)
        #yactual[] = [y[i] for i in range(len(y))]
        yhat = model.predict(X,verbose=0)[0]
        for index1 in range(10):
            i = np.where(yhat[index1] == yhat[index1].max())
            hin = i[0]
            for index2 in range(3):
                if(index2==hin):
                    yhat[index1][index2]=1
                else:
                    yhat[index1][index2]=0
        j= yhat
        k= y[0]
        for index1 in range(10):
            ypredicted.append(j[index1])
            yactual.append(k[index1])
         
            
for d in range(343,384):
        X,y = get_sequence(n_timesteps,d)
        #yactual[] = [y[i] for i in range(len(y))]
        yhat = model.predict(X,verbose=0)[0]
        for index1 in range(10):
            i = np.where(yhat[index1] == yhat[index1].max())
            hin = i[0]
            for index2 in range(3):
                if(index2==hin):
                    yhat[index1][index2]=1
                else:
                    yhat[index1][index2]=0
        j= yhat
        k= y[0]
        for index1 in range(10):
            ypredicted.append(j[index1])
            yactual.append(k[index1])
            
for d in range(534,575):
        X,y = get_sequence(n_timesteps,d)
        #yactual[] = [y[i] for i in range(len(y))]
        yhat = model.predict(X,verbose=0)[0]
        for index1 in range(10):
            i = np.where(yhat[index1] == yhat[index1].max())
            hin = i[0]
            for index2 in range(3):
                if(index2==hin):
                    yhat[index1][index2]=1
                else:
                    yhat[index1][index2]=0
        j= yhat
        k= y[0]
        for index1 in range(10):
            ypredicted.append(j[index1])
            yactual.append(k[index1])
        
for d in range(727,766):
        X,y = get_sequence(n_timesteps,d)
        #yactual[] = [y[i] for i in range(len(y))]
        yhat = model.predict(X,verbose=0)[0]
        for index1 in range(10):
            i = np.where(yhat[index1] == yhat[index1].max())
            hin = i[0]
            for index2 in range(3):
                if(index2==hin):
                    yhat[index1][index2]=1
                else:
                    yhat[index1][index2]=0
        j= yhat
        k= y[0]
        for index1 in range(10):
            ypredicted.append(j[index1])
            yactual.append(k[index1])
            
ya = []
yp = []
for index1 in range(len(ypredicted)):
            if (ypredicted[index1][0]==1 and ypredicted[index1][1]==0 and ypredicted[index1][2]==0):
                yp.append(0)
            if (ypredicted[index1][0]==0 and ypredicted[index1][1]==1 and ypredicted[index1][2]==0 ):
                yp.append(1)
            if (ypredicted[index1][0]==0 and ypredicted[index1][1]==0 and ypredicted[index1][2]==1 ):
                yp.append(2)
            
for index1 in range(len(yactual)):
            if (yactual[index1][0]==1 and yactual[index1][1]==0 and yactual[index1][2]==0):
                ya.append(0)
            if (yactual[index1][0]==0 and yactual[index1][1]==1 and yactual[index1][2]==0 ):
                ya.append(1)
            if (yactual[index1][0]==0 and yactual[index1][1]==0 and yactual[index1][2]==1 ):
                ya.append(2)
           
for i in range(len(ya)):
    if(ya[i] == yp[i]):
                countp = countp+1
    else:
        if(i!=len(ya)-1):
            if((ya[i+1]!=0 and yp[i]!=0) or (ya[i-1]!=0 and yp[i]!=0)):
                countp=countp+1
            else:
                countn = countn+1
        else:
            countn = countn+1

yaa=[]
ypp=[]
for i in range(len(ya)):
    if(ya[i]==yp[i]):
        yaa.append(ya[i])
        ypp.append(yp[i])
    else:
        if(ya[i]!=yp[i]):
            if((ya[i]==2 and yp[i+1]==2) or (ya[i-1]==2 and yp[i]==2) or (ya[i]==2 and yp[i-1]==2) or (ya[i+1]==2 and yp[i]==2)):
                yaa.append(2)
                ypp.append(2)
            else:
                yaa.append(ya[i])
                ypp.append(yp[i])
            




conf_arr = confusion_matrix(yaa, ypp)
conf_ar_1 = confusion_matrix(ya, yp)


print(classification_report(yaa, ypp))
print(classification_report(ya, yp))
print(accuracy_score(yaa,ypp))
print(accuracy_score(ya,yp))

with codecs.open("output.txt", "a", "utf-8") as my_file:
    wr = csv.writer(my_file,delimiter="\n")
    for i in range(len(ya)):
        t = ('Expected:', ya[i], 'Predicted', yp[i])
        wr.writerow(t)

