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


def read_data(path):
    lines = []
    with open(path) as f:
        for line in f.readlines()[:]:
            line = eval(line.strip())
            if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
                lines.append(line)

    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 _score(fact, line):
    with open('dnn_predict_dmi_18d.txt', 'a') as f:
        f.write(str([line[-2], line[-1]]) + "\n")

    up_right = 0
    up_error = 0

    if fact[0] == 1:
        up_right = up_right + 1.12
    elif fact[1] == 1:
        up_right = up_right + 1.06
    elif fact[2] == 1:
        up_right = up_right + 1
        up_error = up_error + 0.5
    elif fact[3] == 1:
        up_right = up_right + 0.94
        up_error = up_error + 1
    else:
        up_error = up_error + 1
        up_right = up_right + 0.88
    return up_right,up_error


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

    test_x_a = test_x[:,:18*18]
    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
    test_x_b = test_x[:, 18*18:18*18+2*18]
    test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
    test_x_c = test_x[:,18*18+2*18:]

    model=load_model(model_path)
    score = model.evaluate([test_x_c, test_x_a, test_x_b], test_y)
    print('MIX', score)

    up_num = 0
    up_error = 0
    up_right = 0
    down_num = 0
    down_error = 0
    down_right = 0
    i = 0
    result=model.predict([test_x_c, test_x_a, test_x_b])
    win_dnn = []
    for r in result:
        fact = test_y[i]

        if idx in [-2]:
            if r[0] > 0.5 or r[1] > 0.5:
                pass
        else:
            if r[0] > 0.7 or r[1] > 0.7:
                tmp_right,tmp_error = _score(fact, lines[i])
                up_right = tmp_right + up_right
                up_error = tmp_error + up_error
                up_num = up_num + 1
            elif r[3] > 0.6 or r[4] > 0.6:
                if fact[0] == 1:
                    down_error = down_error + 1
                    down_right = down_right + 1.12
                elif fact[1] == 1:
                    down_error = down_error + 1
                    down_right = down_right + 1.06
                elif fact[2] == 1:
                    down_right = down_right + 1
                elif fact[3] == 1:
                    down_right = down_right + 0.94
                else:
                    down_right = down_right + 0.88
                down_num = down_num + 1

        i = i + 1
    if up_num == 0:
        up_num = 1
    if down_num == 0:
        down_num = 1
    print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
    return win_dnn,up_right/up_num,down_right/down_num


if __name__ == '__main__':
    predict(file_path='D:\\data\\quantization\\stock18_18d_test.log', model_path='18_18d_mix_seq.h5')
    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
    # multi_predict(model='15_18d')
    # predict_today(20200229, model='11_18d')