from __future__ import print_function
import sys
import json
import math
from processing import *

import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pylab as plt
from sklearn.metrics import mean_squared_error
import time


seed=7
np.random.seed(seed)  # for reproducibility

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.recurrent import LSTM, GRU
from keras.layers import Conv1D, MaxPooling1D, AtrousConvolution1D, RepeatVector
from keras.callbacks import CSVLogger, EarlyStopping, ModelCheckpoint,ReduceLROnPlateau, TensorBoard
from hyperbolic_nonlinearities import *
from keras.layers.wrappers import Bidirectional
from keras import regularizers
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import *
from keras.optimizers import RMSprop, Adam, SGD, Nadam
from keras.initializers import *

start_time = time.time()

batch_size = 128
nb_epoch = 4200
patience = 500
look_back = 7
EMB_SIZE = 4 #numero de features


train = pd.read_csv('minidolar/train.csv', sep = ',',  engine='python', decimal='.',header=0)
test = pd.read_csv('minidolar/test.csv', sep = ',',  engine='python', decimal='.',header=0)

train_shift = train['shift']
train_target = train['f0']
train_open = train[['v0','v4','v8','v12','v16','v20','v24','v28','v32','v36','v40','v44','v48','v52','v56','v60','v64','v68','v72','v76','v80','v84','v88','v92','v96','v100','v104','v108','v112','v116']]
train_high = train[['v1','v5','v9','v13','v17','v21','v25','v29','v33','v37','v41','v45','v49','v53','v57','v61','v65','v69','v73','v77','v81','v85','v89','v93','v97','v101','v105','v109','v113','v117']]
train_low = train[['v2','v6','v10','v14','v18','v22','v26','v30','v34','v38','v42','v46','v50','v54','v58','v62','v66','v70','v74','v78','v82','v86','v90','v94','v98','v102','v106','v110','v114','v118']]
train_close = train[['v3','v7','v11','v15','v19','v23','v27','v31','v35','v39','v43','v47','v51','v55','v59','v63','v67','v71','v75','v79','v83','v87','v91','v95','v99','v103','v107','v111','v115','v119']]

test_shift = test['shift']
test_target = test['f0']
test_open = test[['v0','v4','v8','v12','v16','v20','v24','v28','v32','v36','v40','v44','v48','v52','v56','v60','v64','v68','v72','v76','v80','v84','v88','v92','v96','v100','v104','v108','v112','v116']]
test_high = test[['v1','v5','v9','v13','v17','v21','v25','v29','v33','v37','v41','v45','v49','v53','v57','v61','v65','v69','v73','v77','v81','v85','v89','v93','v97','v101','v105','v109','v113','v117']]
test_low = test[['v2','v6','v10','v14','v18','v22','v26','v30','v34','v38','v42','v46','v50','v54','v58','v62','v66','v70','v74','v78','v82','v86','v90','v94','v98','v102','v106','v110','v114','v118']]
test_close = test[['v3','v7','v11','v15','v19','v23','v27','v31','v35','v39','v43','v47','v51','v55','v59','v63','v67','v71','v75','v79','v83','v87','v91','v95','v99','v103','v107','v111','v115','v119']]



# obtendo train target para O,H,L
train_l_target = []
train_h_target = []
train_o_target = []
for i in range(train_shift.shape[0]-1):
    train_l_target.append(np.array(train_low)[i+1][29]+train_shift[i+1]-train_shift[i])
    train_h_target.append(np.array(train_high)[i+1][29]+train_shift[i+1]-train_shift[i])
    train_o_target.append(np.array(train_open)[i+1][29]+train_shift[i+1]-train_shift[i])

    
train_ohlc_target = np.column_stack((train_o_target, train_h_target, train_l_target, train_target[:-1]))

# obtendo test target para O,H,L
test_l_target = []
test_h_target = []
test_o_target = []
for i in range(test_shift.shape[0]-1):
    test_l_target.append(np.array(test_low)[i+1][29]+test_shift[i+1]-test_shift[i])
    test_h_target.append(np.array(test_high)[i+1][29]+test_shift[i+1]-test_shift[i])
    test_o_target.append(np.array(test_open)[i+1][29]+test_shift[i+1]-test_shift[i])

    
test_ohlc_target = np.column_stack((test_o_target, test_h_target, test_l_target, test_target[:-1]))

#removendo ultimo elemento pq é perdido ao calcular os targets O,H e L
train_shift = train_shift[:-1]
train_target = train_target[:-1]
train_open = train_open[:-1]
train_high = train_high[:-1]
train_low = train_low[:-1]
train_close = train_close[:-1]

test_shift = test_shift[:-1]
test_target = test_target[:-1]
test_open = test_open[:-1]
test_high = test_high[:-1]
test_low = test_low[:-1]
test_close = test_close[:-1]


def evaluate_model(model, name, n_layers, ep):
    X_train, X_test, Y_train, Y_test =  np.column_stack((train_open.values,train_high.values,train_low.values,train_close.values)),  np.column_stack((test_open.values,test_high.values,test_low.values,test_close.values)),  np.array(train_h_target).reshape(len(train_h_target),1),  np.array(test_h_target).reshape(len(test_h_target),1)
    X_trainp, X_testp, Y_trainp, Y_testp = X_train+train_shift.values.reshape(train_shift.size,1), X_test+test_shift.values.reshape(test_shift.size,1), Y_train+train_shift.values.reshape(train_shift.size,1), Y_test + test_shift.values.reshape(test_shift.size,1)


    csv_logger = CSVLogger('output/%d_layers/%s.csv' % (n_layers, name))
    es = EarlyStopping(monitor='loss', patience=patience)
    #mcp = ModelCheckpoint('output/mnist_adaptative_%dx800/%s.checkpoint' % (n_layers, name), save_weights_only=True)
    #tb = TensorBoard(log_dir='output/mnist_adaptative_%dx800' % n_layers, histogram_freq=1, write_graph=False, write_images=False)

    
    #sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)

    #optimizer = sgd
    optimizer = "adam"
    #optimizer = "adadelta"

    model.compile(loss='mean_squared_error', optimizer=optimizer)

    # reshape input to be [samples, time steps, features]
    X_train = np.reshape(X_train, (X_train.shape[0], int(X_train.shape[1]/EMB_SIZE), EMB_SIZE))
    X_test = np.reshape(X_test, (X_test.shape[0], int(X_test.shape[1]/EMB_SIZE), EMB_SIZE))
    #X_train = np.expand_dims(X_train, axis=2)
    #X_test = np.expand_dims(X_test, axis=2)

    history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=ep, verbose=0, validation_split=0.1, callbacks=[csv_logger,es])

    #trainScore = model.evaluate(X_train, Y_train, verbose=0)
    #print('Train Score: %f MSE (%f RMSE)' % (trainScore, math.sqrt(trainScore)))
    #testScore = model.evaluate(X_test, Y_test, verbose=0)
    #print('Test Score: %f MSE (%f RMSE)' % (testScore, math.sqrt(testScore)))

    # make predictions (scaled)
    trainPredict = model.predict(X_train)
    testPredict = model.predict(X_test)
    
    
    # invert predictions (back to original)
    
    new_predicted = testPredict+test_shift.values.reshape(test_shift.size,1)
    new_train_predicted= trainPredict+train_shift.values.reshape(train_shift.size,1)

    # calculate root mean squared error
    trainScore = math.sqrt(mean_squared_error(new_train_predicted, Y_trainp))
    #trainScore = mean_squared_error(trainPredict, Y_train)
    #print('Train Score: %f RMSE' % (trainScore))
    testScore = math.sqrt(mean_squared_error(new_predicted, Y_testp))
    #testScore = mean_squared_error(testPredict, Y_test)
    #print('Test Score: %f RMSE' % (testScore))
    epochs = len(history.epoch)

    # fig = plt.figure()
    # plt.plot(Y_test[:150], color='black') # BLUE - trained RESULT
    # plt.plot(testPredict[:150], color='blue') # RED - trained PREDICTION
    #plt.plot(Y_testp[:150], color='green') # GREEN - actual RESULT
    #plt.plot(new_predicted[:150], color='red') # ORANGE - restored PREDICTION
    #plt.show()

    return trainScore, testScore, epochs, optimizer


def __main__(argv):
    n_layers = int(argv[0])
    print(n_layers,'layers')

    #nonlinearities = ['aabh', 'abh', 'ah', 'sigmoid', 'relu', 'tanh']
    nonlinearities = ['sigmoid', 'relu', 'tanh']
    #nonlinearities = ['relu']

    with open("output/%d_layers/compare.csv" % n_layers, "a") as fp:
        fp.write("-MINIDOLAR/MLP-Multi NN\n")

    hals = []
    #data_original = pd.read_csv('./data/AAPL1216.csv')[::-1]
    #data_original = pd.read_csv('ibov_google_15jun2017_1min_15d.csv', sep = ',',  engine='python', skiprows=8, decimal='.',header=None)

    # openp = data_original.ix[:, 4].tolist()
    # highp = data_original.ix[:, 2].tolist()
    # lowp = data_original.ix[:, 3].tolist()
    # closep = data_original.ix[:, 1].tolist()
    # volumep = data_original.ix[:, 5].tolist()

    #data_original = pd.read_csv('minidolar/wdo.csv', sep = '|',  engine='python', decimal='.',header=0)
    
    #averagep = data_original.ix[:, 1].tolist()
    #openp = data_original.ix[:, 2].tolist()
    #highp = data_original.ix[:, 3].tolist()
    #lowp = data_original.ix[:, 4].tolist()
    #closep = data_original.ix[:, 5].tolist()
    #volumep = data_original.ix[:, 6].tolist()

    # data_chng = data_original.ix[:, 'Adj Close'].pct_change().dropna().tolist()

    WINDOW = 30
    TRAIN_SIZE=WINDOW
    
    STEP = 1
    FORECAST = 1

    
    for f in range(1,2):
        name='relu'
        model = Sequential()

        model.add(Dense(12, input_shape = (TRAIN_SIZE, EMB_SIZE),
                kernel_regularizer=regularizers.l2(0.01)))
        model.add(Activation(name))

        for l in range(n_layers):
            model.add(Dense(12, input_shape = (TRAIN_SIZE, EMB_SIZE)))
            model.add(Activation(name))
        
        model.add(Dropout(0.25))
        model.add(Flatten())
        model.add(Dense(1))
        model.add(Activation('linear'))
        #model.summary()

        trainScore, testScore, epochs, optimizer = evaluate_model(model, name, n_layers,nb_epoch)
        # if(testScore_aux > testScore):
        #     testScore_aux=testScore
        #     f_aux = f

        elapsed_time = (time.time() - start_time)
        with open("output/%d_layers/compare.csv" % n_layers, "a") as fp:
            #fp.write("%i,%s,%f,%f,%d,%s --%s seconds\n" % (f, name, trainScore, testScore, epochs, optimizer, elapsed_time))
            fp.write("%s,%f,%f,%d,%s --%s seconds\n" % (name, trainScore, testScore, epochs, optimizer, elapsed_time))

        model = None

        #print("melhor parametro: %i" % f_aux)

if __name__ == "__main__":
   __main__(sys.argv[1:])

