# -*- encoding:utf-8 -*-
import numpy as np
from keras.models import load_model


def read_data(path):
    lines = []
    with open(path) as f:
        for line in f.readlines()[:]:
            lines.append(eval(line.strip()))

    size = len(lines[0])
    train_x=[s[:size - 2] for s in lines]
    train_y=[s[size-1] for s in lines]
    return np.array(train_x),np.array(train_y),lines


def predict(file_path='', model_path='15min_dnn_seq.h5'):
    test_x,test_y,lines=read_data(file_path)

    model=load_model(model_path)
    score = model.evaluate(test_x, test_y)
    print('DNN', score)

    up_num = 0
    up_right = 0
    i = 0
    result=model.predict(test_x)
    win_dnn = []
    with open('dnn_predict_5d.txt', 'a') as f:
        for r in result:
            fact = test_y[i]
            if r[0] > 0.5:
                f.write(str([lines[i][-2], lines[i][-1]]) + "\n")
                win_dnn.append([lines[i][-2], lines[i][-1]])
                if fact[0] == 1:
                    up_right = up_right + 1.15
                elif fact[1] == 1:
                    up_right = up_right + 1.05
                elif fact[2] == 1:
                    up_right = up_right + 1
                else:
                    up_right = up_right - 0.15
                up_num = up_num + 1
            elif r[1] > 0.5:
                f.write(str([lines[i][-2], lines[i][-1]]) + "\n")
                win_dnn.append([lines[i][-2], lines[i][-1]])
                if fact[0] == 1:
                    up_right = up_right + 1.15
                elif fact[1] == 1:
                    up_right = up_right + 1.05
                elif fact[2] == 1:
                    up_right = up_right + 1
                else:
                    up_right = up_right - 0.15
                up_num = up_num + 1

            i = i + 1
    if up_num == 0:
        up_num = 1
    print('DNN', up_right, up_num, up_right/up_num)
    return win_dnn,up_right/up_num


def multi_predict():
    r = 0
    for x in [0]:
        win_dnn, ratio = predict(file_path='D:\\data\\quantization\\kmeans\\stock2_10_' + str(x) + '_test.log', model_path='5d_dnn_seq_' + str(x) + '.h5')
        r = r + ratio
    print(r)


if __name__ == '__main__':
    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
    multi_predict()