#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 28 16:49:18 2018

@author: weanl
"""

'''
    0.

    1. showScoreData()

    2. show_score_forecast()

    3. show_kpi_forecast()

    3.4 cmpMF(DBID='210100063')

'''

'''
'''
FileDir = '../given_data_1128/'
ForecastStep = 10

'''
'''
from BuildSeq import conSeqs

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

'''
    1. showScoreData()
'''
def showScoreData(DBID='210100063'):
    print('--->call--->showScoreData')

    # 1 load data
    agg_scorex_seqs, agg_scorey_seqs, agg_score_inputs, agg_score_outputs = conSeqs(DBID=DBID)

    slct = 210
    x_seq = agg_scorex_seqs[slct]
    y_seq = agg_scorey_seqs[slct]
    input = agg_score_inputs[:, slct]
    output = agg_score_outputs[:, slct]
    '''
    '''
    input[0] = input[0] + input[1]
    output[0] = output[0] + output[1]

    # 2 plot
    look_back = x_seq.shape[0]
    forecast_step = 10
    dec_lv = input.shape[0] - 1
    t1 = np.arange(0, look_back)
    t2 = np.arange(look_back, look_back+forecast_step)

    plt.figure(1, figsize=(16, 24))
    ROW = dec_lv + 3
    ## 2.1 plot x_seq and y_seq
    plt.subplot(ROW, 1, 1)
    plt.title('DBID = '+DBID)
    plt.plot(t1, x_seq, 'b.-')
    plt.plot(t2, y_seq, 'r.-')
    ## 2.1 plot sum(input) and sum(output)
    plt.subplot(ROW, 1, 2)
    x_sum = np.sum(input, axis=0)
    y_sum = np.sum(output, axis=0)
    plt.plot(t1, x_sum, 'b.-')
    plt.plot(t2, y_sum, 'r.-')
    ## 2.2 plot input and output
    for i in range(dec_lv+1):
        plt.subplot(ROW, 1, i+3)
        plt.plot(t1, input[i], 'b.-')
        plt.plot(t2, output[i], 'r.-')

    plt.show()

    print('--->return from--->showScoreData')
    return 0

'''
    2. show_score_forecast()
'''
def show_score_forecast(DBID='210100063'):
    print('--->call--->show_score_forecast')
    # 1 load data
    file_path = FileDir + DBID + '/' + 'score_forecast.npz'
    data = np.load(file_path)

    # 2
    y_test_pred = data['y_test_pred'].reshape(-1, )
    y_test_truth = data['y_test_truth'].reshape(-1, )
    y_train_pred = data['y_train_pred'].reshape(-1, )
    y_train_truth = data['y_train_truth'].reshape(-1, )

    # 3 compute mase and mse for test
    ae_test = abs(y_test_pred-y_test_truth)
    mae_test = np.mean(ae_test)
    ae_var_test = np.var(ae_test)
    print('mae_test: \t\t', mae_test)
    #print('mae_var_test: \t', ae_var_test)
    square_error_test = np.square(y_test_pred-y_test_truth)
    mse_test = np.mean(square_error_test)
    print('mse_test: \t\t', mse_test)

    # 4 compute mase and mse for train
    ae_train = abs(y_train_pred-y_train_truth)
    mae_train = np.mean(ae_train)
    ae_var_train = np.var(ae_train)
    print('mae_train: \t\t', mae_train)
    #print('mae_var_train: \t', ae_var_train)
    square_error_train = np.square(y_train_pred-y_train_truth)
    mse_train = np.mean(square_error_train)
    print('mse_train: \t\t', mse_train)
    '''
    # 4
    t = np.arange(testTruth.shape[0])
    #plt.plot(t, testTrue, label='testTrue', color='b')
    plt.plot(t, testTruth, label='testTruth', color='g')

    plt.plot(t, testPredict, label='prediction', color='r')

    # set interval size of grid along x
    plt.xticks(np.arange(1, testTruth.shape[0]+1, ForecastStep))

    plt.grid(linestyle='-.')
    plt.legend(loc='best')

    SavePath = FileDir + DBID + '/' + 'show_score_forecast.png'
    #plt.savefig(SavePath, dpi=256)
    plt.show()
    '''

    print('--->return from--->show_score_forecast')
    return mae_test, mse_test, mae_train, mse_train

'''
    # 2.2 cmpScoreBaseline(DBID='210100063')
'''
def cmpScoreBaseline(DBID='210100063'):
    print('--->call--->cmpScoreBaseline')

    # 0.1 load data of averModelScore
    print('# 0.1 load data of averModelScore')
    file_path_averModelScore = FileDir + DBID + '/' + 'score_forecast_averModelScore.npz'
    averModelScore = np.load(file_path_averModelScore)
    aver_y_test_pred = averModelScore['y_test_pred']
    aver_y_test = averModelScore['y_test']
    aver_y_train_pred = averModelScore['y_train_pred']
    aver_y_train = averModelScore['y_train']
    #print('aver_y_test_pred:\n', aver_y_test_pred)
    #print('aver_y_test:\n', aver_y_test)
    aver_y_test_pred = np.concatenate([aver_y_test_pred, aver_y_train_pred], axis=0)
    aver_y_test = np.concatenate([aver_y_test, aver_y_train], axis=0)
    print('np.std(aver_y_test) = ', np.std(aver_y_test))

    # 0.2 compute MAE and RMSE for averModelScore
    print('# 0.2 compute MAE and RMSE')
    aver_AE_test = abs(aver_y_test_pred-aver_y_test)
    aver_MAE_test = np.mean(aver_AE_test, axis=0)
    aver_SE_test = np.square(aver_y_test_pred-aver_y_test)
    aver_MSE_test = np.mean(aver_AE_test, axis=0)
    aver_RMSE_test= np.sqrt(aver_MSE_test)
    #print('aver_MAE_test: \n', aver_MAE_test)
    #print('aver_MAE_test mean = ', np.mean(aver_MAE_test))
    print('aver_RMSE_test: \n', aver_RMSE_test)
    print('aver_RMSE_test mean = ', np.mean(aver_RMSE_test))

    # 1.1 load data of arModelScore
    print('# 1.1 load data of arModelScore')
    file_path_arModelScore = FileDir + DBID + '/' + 'score_forecast_arModelScore.npz'
    arModelScore = np.load(file_path_arModelScore)
    ar_y_test_pred = arModelScore['y_test_pred']
    ar_y_test = arModelScore['y_test']
    ar_y_train_pred = arModelScore['y_train_pred']
    ar_y_train = arModelScore['y_train']
    #print('ar_y_test_pred:\n', ar_y_test_pred)
    #print('ar_y_test:\n', ar_y_test)
    ar_y_test_pred = np.concatenate([ar_y_test_pred, ar_y_train_pred], axis=0)
    ar_y_test = np.concatenate([ar_y_test, ar_y_train], axis=0)
    print('np.std(ar_y_test) = ', np.std(ar_y_test))

    # 1.2 compute MAE and RMSE for arModelScore
    print('# 1.2 compute MAE and RMSE')
    ar_AE_test = abs(ar_y_test_pred-ar_y_test)
    ar_MAE_test = np.mean(ar_AE_test, axis=0)
    ar_SE_test = np.square(ar_y_test_pred-ar_y_test)
    ar_MSE_test = np.mean(ar_AE_test, axis=0)
    ar_RMSE_test= np.sqrt(ar_MSE_test)
    #print('ar_MAE_test: \n', ar_MAE_test)
    #print('ar_MAE_test mean = ', np.mean(ar_MAE_test))
    print('ar_RMSE_test: \n', ar_RMSE_test)
    print('ar_RMSE_test mean = ', np.mean(ar_RMSE_test))

    # ardwtModelScore (recursive)
    # rnndwtModelScore (recursive)

    # 2.1 load data of ourModelScore
    print('# 2.1 load data of ourModelScore')
    file_path_ourModelScore = FileDir + DBID + '/' + 'score_forecast.npz'
    ourModelScore = np.load(file_path_ourModelScore)
    our_y_test_pred = ourModelScore['y_test_pred'].reshape(-1, ForecastStep)
    our_y_test = ourModelScore['y_test_truth']
    our_y_train_pred = ourModelScore['y_train_pred'].reshape(-1, ForecastStep)
    our_y_train = ourModelScore['y_train_truth']
    #print('our_y_test_pred:\n', our_y_test_pred)
    #print('our_y_test:\n', our_y_test)
    our_y_test_pred = np.concatenate([our_y_test_pred, our_y_train_pred], axis=0)
    our_y_test = np.concatenate([our_y_test, our_y_train], axis=0)
    print('np.std(our_y_test) = ', np.std(our_y_test))

    # 2.2 compute MAE and RMSE
    print('# 2.2 compute MAE and RMSE')
    our_AE_test = abs(our_y_test_pred-our_y_test)
    our_MAE_test = np.mean(our_AE_test, axis=0)
    our_SE_test = np.square(our_y_test_pred-our_y_test)
    our_MSE_test = np.mean(our_AE_test, axis=0)
    our_RMSE_test= np.sqrt(our_MSE_test)
    #print('our_MAE_test: \n', our_MAE_test)
    #print('our_MAE_test mean = ', np.mean(our_MAE_test))
    print('our_RMSE_test: \n', our_RMSE_test)
    print('our_RMSE_test mean = ', np.mean(our_RMSE_test))

    # -1 show RMSE for different score forecast method
    plt.figure(1, figsize=(16, 12))
    t = np.arange(ForecastStep)+1
    plt.plot(t, aver_RMSE_test, 'k.-', label='average:'
    +str(np.mean(aver_RMSE_test)))
    plt.plot(t, ar_RMSE_test, 'g.-', label='auto-regression:'
    +str(np.mean(ar_RMSE_test)))
    plt.xticks(t, fontsize=16)

    plt.plot(t, our_RMSE_test, 'r.-', label='MDWT-Seq2Seq:'
    +str(np.mean(our_RMSE_test)))
    plt.ylim(0, 1.6)
    plt.xlabel('ForecastStep', fontsize=20)
    plt.ylabel('RMSE', fontsize=20)

    plt.legend()
    plt.grid(True, linestyle='-.')

    plt.show()

    print('--->return from--->cmpScoreBaseline')
    return 0

'''
    3.1 show_kpi_forecast()
'''
def show_kpi_forecast(DBID='210100063'):
    print('--->call--->show_kpi_forecast')
    # 1 load data
    file_path = FileDir + DBID + '/' + 'kpi_forecast.npz'
    data = np.load(file_path)

    # 2
    y_test_pred = data['y_test_pred']
    y_test_truth = data['y_test_truth']
    y_train_pred = data['y_train_pred']
    y_train_truth = data['y_train_truth']
    KPIsNum = y_test_pred.shape[-1]

    # 3 compute mase and mse for test (all kpi)
    ae_test = abs(y_test_pred-y_test_truth)
    mae_test = np.mean(ae_test)
    ae_var_test = np.var(ae_test)
    print('mae_test: \t\t', mae_test)
    #print('mae_var_test: \t', ae_var_test)
    square_error_test = np.square(y_test_pred-y_test_truth)
    mse_test = np.mean(square_error_test)
    print('mse_test: \t\t', mse_test)

    # 4 compute mase and mse for train (all kpi)
    ae_train = abs(y_train_pred-y_train_truth)
    mae_train = np.mean(ae_train)
    ae_var_train = np.var(ae_train)
    print('mae_train: \t\t', mae_train)
    #print('mae_var_train: \t', ae_var_train)
    square_error_train = np.square(y_train_pred-y_train_truth)
    mse_train = np.mean(square_error_train)
    print('mse_train: \t\t', mse_train)

    '''
    # 4 plot
    pick = 1
    t = np.arange(y_pred.shape[0])
    plt.figure(1)
    plt.plot(t, y_pred[:, pick], 'g.-', label='y_pred')
    plt.plot(t, y_test[:, pick], 'r.-', label='y_test')
    plt.legend()
    plt.show()
    '''
    print('--->return from--->show_kpi_forecast')
    return mae_test, mse_test, mae_train, mse_train

'''
    3.2 show_joint_forecast()
'''
def show_joint_forecast(DBID='210100063'):
    print('--->call--->show_joint_forecast')
    # 1 load data
    file_path = FileDir + DBID + '/' + 'joint_forecast.npz'
    data = np.load(file_path)

    # 2
    scoreTestPrediction = data['scoreTestPrediction']
    scoreTestTruth = data['scoreTestTruth']
    scoreTrainPredict = data['scoreTrainPredict']
    scoreTrainTruth = data['scoreTrainTruth']
    kpicorTestPredict = data['kpicorTestPredict'] # ()
    kpicorTestTruth = data['kpicorTestTruth']
    kpicorTrainPredict = data['kpicorTrainPredict']
    kpicorTrainTruth = data['kpicorTrainTruth']
    '''
    print('kpicorTestPredict.shape = ', kpicorTestPredict.shape)
    print('kpicorTestTruth.shape = ', kpicorTestTruth.shape)
    print('kpicorTrainPredict.shape = ', kpicorTrainPredict.shape)
    print('kpicorTrainTruth.shape = ', kpicorTrainTruth.shape)
    '''
    KPIsNum = kpicorTestPredict.shape[-1]
    test_num = kpicorTestPredict.shape[0]
    train_num = kpicorTrainPredict.shape[0]
    kpicorTestTruth = kpicorTestTruth.reshape(test_num, KPIsNum)
    kpicorTrainTruth = kpicorTrainTruth.reshape(train_num, KPIsNum)

    #
    # 3 compute mae and mse for score test
    ae_score_test = abs(scoreTestPrediction - scoreTestTruth)
    mae_score_test = np.mean(ae_score_test)
    ae_var_score_test = np.var(ae_score_test)
    print('mae_score_test: \t\t', mae_score_test)
    #print('mae_var: \t', ae_var)
    square_error_score_test = np.square(scoreTestPrediction - scoreTestTruth)
    mse_score_test = np.mean(square_error_score_test)
    print('mse_score_test: \t\t', mse_score_test)

    # 4 compute mae and mse for score train
    ae_score_train = abs(scoreTrainPredict - scoreTrainTruth)
    mae_score_train = np.mean(ae_score_train)
    ae_var_score_train = np.var(ae_score_train)
    print('mae_score_train: \t\t', mae_score_train)
    #print('mae_var: \t', ae_var)
    square_error_score_train = np.square(scoreTrainPredict - scoreTrainTruth)
    mse_score_train = np.mean(square_error_score_train)
    print('mse_score_train: \t\t', mse_score_train)

    # 5 compute mae and mse for kpicor test
    ae_kpicor_test = abs(kpicorTestPredict - kpicorTestTruth)
    mae_kpicor_test = np.mean(ae_kpicor_test)
    ae_var_kpicor_test = np.var(ae_kpicor_test)
    print('mae_kpicor_test: \t\t', mae_kpicor_test)
    #print('mae_var: \t', ae_var)
    square_error_kpicor_test = np.square(kpicorTestPredict - kpicorTestTruth)
    mse_kpicor_test = np.mean(square_error_kpicor_test)
    print('mse_kpicor_test: \t\t', mse_kpicor_test)

    # 6 compute mae and mse for kpicor train
    ae_kpicor_train = abs(kpicorTrainPredict - kpicorTrainTruth)
    mae_kpicor_train = np.mean(ae_kpicor_train)
    ae_var_kpicor_train = np.var(ae_kpicor_train)
    print('mae_kpicor_train: \t\t', mae_kpicor_train)
    #print('mae_var: \t', ae_var)
    square_error_kpicor_train = np.square(kpicorTrainPredict - kpicorTrainTruth)
    mse_kpicor_train = np.mean(square_error_kpicor_train)
    print('mse_kpicor_train: \t\t', mse_kpicor_train)

    '''
    # 4 plot
    pick = 1
    t = np.arange(kpicorPredict.shape[0])
    plt.figure(1)
    plt.plot(t, kpicorPredict[:, pick], 'g.-', label='kpicorPredict')
    plt.plot(t, kpicorTruth[:, pick], 'r.-', label='kpicorTruth')
    plt.legend()
    plt.show()
    '''
    print('--->return from--->show_joint_forecast')
    return mae_score_test, mse_score_test, mae_score_train, mse_score_train, mae_kpicor_test, mse_kpicor_test, mae_kpicor_train, mse_kpicor_train

'''
    3.3 show_forecast_compared()
'''
def show_forecast_compared(DBID='210100063'):

    mae_sep, mse_sep = show_kpi_forecast(DBID)
    mae_joint, mse_joint = show_joint_forecast(DBID)

    mae_sep_mean = np.mean(mae_sep)
    mse_sep_mean = np.mean(mse_sep)
    print('mae_sep_mean = ', mae_sep_mean)
    print('mse_sep_mean = ', mse_sep_mean)

    mae_joint_mean = np.mean(mae_joint)
    mse_joint_mean = np.mean(mse_joint)
    print('mae_joint_mean = ', mae_joint_mean)
    print('mse_joint_mean = ', mse_joint_mean)

    return 0

'''
    3.4 cmpMF(DBID='210100063')
'''
def cmpMF(DBID='210100063'):
    print('---> call ---> cmpMF')

    # 1 load data
    file_path1 = FileDir + DBID + '/' + 'score_forecast.npz'
    file_path2 = FileDir + DBID + '/' + 'joint_forecast.npz'
    data_sep = np.load(file_path1)
    data_joint = np.load(file_path2)

    scorePredSep = data_sep['y_test_pred'].reshape(-1, ForecastStep)
    scoreTruthSep = data_sep['y_test_truth']
    scorePredJoint = data_joint['scoreTestPrediction']
    scoreTruthJoint = data_joint['scoreTestTruth']

    # 2
    SE_sep = np.square(scorePredSep-scoreTruthSep)
    MSE_sep = np.mean(SE_sep, axis=0)
    RMSE_sep = np.sqrt(MSE_sep)

    SE_joint = np.square(scorePredJoint-scoreTruthJoint)
    MSE_joint = np.mean(SE_joint, axis=0)
    RMSE_joint = np.sqrt(MSE_joint)

    # 3
    plt.figure(1, figsize=(16, 12))
    t = np.arange(1, ForecastStep+1)
    plt.subplot(1, 1, 1)
    plt.plot(t, RMSE_sep, 'g')
    plt.plot(t, RMSE_joint, 'r')

    plt.show()

    print('---> return from --> cmpMF')
    return 0

'''
    3.5 cmpCor(DBID='210100063')
'''
def cmpCor(DBID='210100063'):    # 1 load data
    file_path1 = FileDir + DBID + '/' + 'score_forecast.npz'
    file_path2 = FileDir + DBID + '/' + 'joint_forecast.npz'
    data_sep = np.load(file_path1)
    data_joint = np.load(file_path2)
    print('---> call ---> cmpCor')

    # 1 load data
    file_path1 = FileDir + DBID + '/' + 'kpi_forecast.npz'
    file_path2 = FileDir + DBID + '/' + 'joint_forecast.npz'
    data_sep = np.load(file_path1)
    data_joint = np.load(file_path2)

    kpiPredSep = data_sep['y_test_pred']
    KPIsNum = kpiPredSep.shape[-1]
    kpiTruthSep = data_sep['y_test_truth']
    kpiPredJoint = data_joint['kpicorTestPredict']
    kpiTruthJoint = data_joint['kpicorTestTruth'].reshape(-1, KPIsNum)
    '''
    print('kpiPredSep.shape = ', kpiPredSep.shape)
    print('kpiTruthSep.shape = ', kpiTruthSep.shape)
    print('kpiPredJoint.shape = ', kpiPredJoint.shape)
    print('kpiTruthJoint.shape = ', kpiTruthJoint.shape)
    '''

    # 2
    SE_sep = np.square(kpiPredSep-kpiTruthSep)
    MSE_sep = np.mean(SE_sep, axis=0)
    RMSE_sep = np.sqrt(MSE_sep)
    print('RMSE_sep.shape = ', RMSE_sep.shape)

    SE_joint = np.square(kpiPredJoint-kpiTruthJoint)
    MSE_joint = np.mean(SE_joint, axis=0)
    RMSE_joint = np.sqrt(MSE_joint)
    print('RMSE_joint.shape = ', RMSE_joint.shape)

    # 3
    plt.figure(1, figsize=(16, 12))
    t = np.arange(1,KPIsNum+1)
    plt.subplot(1, 1, 1)
    mean_sep = np.mean(RMSE_sep)
    plt.bar(t, RMSE_sep, width=0.4, label='Seperate: '+str(mean_sep))
    mean_joint = np.mean(RMSE_joint)
    plt.bar(t+0.4, RMSE_joint, width=0.4, label='Joint: '+str(mean_joint))

    plt.legend()
    plt.show()

    print('---> return from ---> cmpCor')
    return 0

'''
    3.6 cmpCorTop(DBID='210100063')
'''
def cmpCorTop(DBID='210100063'):
    file_path1 = FileDir + DBID + '/' + 'score_forecast.npz'
    file_path2 = FileDir + DBID + '/' + 'joint_forecast.npz'
    data_sep = np.load(file_path1)
    data_joint = np.load(file_path2)
    print('---> call ---> cmpCor')

    # 1 load data
    file_path1 = FileDir + DBID + '/' + 'kpi_forecast.npz'
    file_path2 = FileDir + DBID + '/' + 'joint_forecast.npz'
    data_sep = np.load(file_path1)
    data_joint = np.load(file_path2)

    kpiPredSep = data_sep['y_test_pred']
    KPIsNum = kpiPredSep.shape[-1]
    kpiTruthSep = data_sep['y_test_truth']
    kpiPredJoint = data_joint['kpicorTestPredict']
    kpiTruthJoint = data_joint['kpicorTestTruth'].reshape(-1, KPIsNum)

    # 2 compute mean recall fo Top M with predict
    kpiGroundTruth = abs(kpiTruthSep)
    kpiPredSep = abs(kpiPredSep)
    kpiPredJoint = abs(kpiPredJoint)
    print('kpiGroundTruth.shape = ', kpiGroundTruth.shape)

    M = 8
    TruthIndex = np.argsort(kpiGroundTruth, axis=1)[:, -M:]
    PredSepIndex = np.argsort(kpiPredSep, axis=1)[:, -M:]
    PredJointIndex = np.argsort(kpiPredJoint, axis=1)[:, -M:]
    print('TruthIndex: \n', TruthIndex)
    print('PredSepIndex: \n', PredSepIndex)
    print('PredJointIndex: \n', PredJointIndex)

    return 0

if __name__ == '__main__':
    print('------------ ShowData ------------')

    #showScoreData(DBID='210100063')
    # 1
    '''
    show_score_forecast(DBID='210100063')
    show_kpi_forecast()
    show_joint_forecast()
    '''

    # 2
    '''
    show_kpi_forecast()
    show_joint_forecast()
    '''
    #show_forecast_compared()

    # 3
    #cmpMF(DBID='210100063')

    # 4
    #cmpCor(DBID='210100063')

    # 5
    #cmpCorTop(DBID='210100063')

    # 6
    cmpScoreBaseline(DBID='210100063')

#   END OF FILE


"""Contains DatasetBuffer"""

from cmath import nan
import logging

import numpy as np
import pandas as pd

from PyDSS.common import DatasetPropertyType
from PyDSS.exceptions import InvalidConfiguration
from PyDSS.utils.utils import make_timestamps

KiB = 1024
MiB = KiB * KiB
GiB = MiB * MiB

# The optimal number of chunks to store in memory will vary widely.
# The h5py docs recommend keeping chunk byte sizes between 10 KiB - 1 MiB.
# It needs to be larger than the biggest possible row and also cover enough
# columns to compress duplicate values. Since we might store thousands of
# elements in one dataset, make it the max by default.
# Note that the downside to making this larger is that any read causes the
# entire chunk to be read.
DEFAULT_MAX_CHUNK_BYTES = 1 * MiB

logger = logging.getLogger(__name__)

class DatasetBuffer:
    """Provides a write buffer to an HDF dataset to increase performance.
    Users must call flush_data before the object goes out of scope to ensure
    that all data is flushed.

    """
    # TODO add support for context manager, though PyDSS wouldn't be able to
    # take advantage in its current implementation.

    def __init__(
            self, hdf_store, path, max_size, dtype, columns, scaleoffset=None,
            max_chunk_bytes=None, attributes=None, names=None,
            column_ranges_per_name=None, data=None
        ):
        if max_chunk_bytes is None:
            max_chunk_bytes = DEFAULT_MAX_CHUNK_BYTES
        self._buf_index = 0
        self._hdf_store = hdf_store
        self._max_size = max_size
        num_columns = len(columns)
        if data is None:
            self.chunk_count = self.compute_chunk_count(
                num_columns,
                max_size,
                dtype,
                max_chunk_bytes,
            )
            shape = (self._max_size, num_columns)
            chunks = (self.chunk_count, num_columns)
        else:
            self.chunk_count = None
            shape = None
            chunks = None

        self._dataset = self._hdf_store.create_dataset(
            name=path,
            shape=shape,
            data=data,
            chunks=chunks,
            dtype=dtype,
            compression="gzip",
            compression_opts=4,
            shuffle=True,
            # Does not preserve NaN, so don't use it.
            #scaleoffset=scaleoffset,
        )

        # Columns, names, and column_ranges_per_name can't be stored as
        # attributes because they can exceed the size limit. Store as datasets
        # instead.
        column_dataset_path = path + "Columns"
        column_dataset = self._hdf_store.create_dataset(
            name=column_dataset_path,
            data=np.array(columns, dtype="S"),
        )
        column_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
        self._dataset.attrs["column_dataset_path"] = column_dataset_path

        if names is not None:
            name_dataset_path = path + "Names"
            name_dataset = self._hdf_store.create_dataset(
                name = name_dataset_path,
                data = np.array(names, dtype="S"),
            )
            name_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
            self._dataset.attrs["name_dataset_path"] = name_dataset_path

        if column_ranges_per_name is not None:
            column_ranges_dataset_path = path + "ColumnRanges"
            column_ranges_dataset = self._hdf_store.create_dataset(
                name=column_ranges_dataset_path,
                data=column_ranges_per_name,
            )
            column_ranges_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
            self._dataset.attrs["column_ranges_dataset_path"] = column_ranges_dataset_path

        self._dataset.attrs["length"] = 0
        self._dataset_index = 0
        self._buf = np.empty(chunks, dtype=dtype)

        if attributes is not None:
            for attr, val in attributes.items():
                self._dataset.attrs[attr] = val

        logger.debug("Created DatasetBuffer path=%s shape=%s chunks=%s",
                     path, shape, chunks)

    def __del__(self):
        assert self._buf_index == 0, \
            f"DatasetBuffer destructed with data in memory: {self._dataset.name}"

    def flush_data(self):
        """Flush the data in the temporary buffer to storage."""
        length = self._buf_index
        if length == 0:
            return

        new_index = self._dataset_index + length
        self._dataset[self._dataset_index:new_index] = self._buf[0:length]
        self._buf_index = 0
        self._dataset_index = new_index
        self._dataset.attrs["length"] = new_index
        self._dataset.flush()

    def max_num_bytes(self):
        """Return the maximum number of bytes the container could hold.

        Returns
        -------
        int

        """
        size_row = self._buf.size * self._buf.itemsize / len(self._buf)
        return size_row * self._max_size

    def write_value(self, value):
        """Write the value to the internal buffer, flushing when full."""

        self._buf[self._buf_index] = value
        self._buf_index += 1
        if self._buf_index == self.chunk_count:
            self.flush_data()

    def write_data(self, values):
        """Write the data to the dataset."""
        new_index = self._dataset_index + len(values)
        self._dataset[self._dataset_index:new_index] = values
        self._dataset_index = new_index
        self._dataset.attrs["length"] = new_index

    @staticmethod
    def compute_chunk_count(
            num_columns,
            max_size,
            dtype,
            max_chunk_bytes=DEFAULT_MAX_CHUNK_BYTES
        ):
        assert max_size > 0, f"max_size={max_size}"
        tmp = np.empty((1, num_columns), dtype=dtype)
        size_row = tmp.size * tmp.itemsize
        chunk_count = min(int(max_chunk_bytes / size_row), max_size)
        if chunk_count == 0:
            raise InvalidConfiguration(
                f"HDF Max Chunk Bytes is smaller than the size of a row. Please increase it. " \
                f"max_chunk_bytes={max_chunk_bytes} num_columns={num_columns} " \
                f"size_row={size_row}"
            )

        return chunk_count

    @staticmethod
    def get_column_ranges(dataset):
        """Return the column ranges per name for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        column_ranges_dataset = dataset.file[dataset.attrs["column_ranges_dataset_path"]]
        return column_ranges_dataset[:]

    @staticmethod
    def get_columns(dataset):
        """Return the columns for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        col_dataset = dataset.file[dataset.attrs["column_dataset_path"]]
        return [x.decode("utf8") for x in col_dataset[:]]

    @staticmethod
    def get_names(dataset):
        """Return the names for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        name_dataset = dataset.file[dataset.attrs["name_dataset_path"]]
        return [x.decode("utf8") for x in name_dataset[:]]

    @staticmethod
    def to_dataframe(dataset, column_range=None):
        """Create a pandas DataFrame from a dataset created with this class.

        Parameters
        ----------
        dataset : h5py.Dataset
        column_range : None | list
            first element is column start, second element is length

        Returns
        -------
        pd.DataFrame

        """
        length = dataset.attrs["length"]
        columns = DatasetBuffer.get_columns(dataset)
        if column_range is None:
            return pd.DataFrame(dataset[:length], columns=columns)

        start = column_range[0]
        end = start + column_range[1]
        return pd.DataFrame(
            dataset[:length, start:end],
            columns=columns[start:end],
        )

    @staticmethod
    def to_datetime(dataset):
        """Create a pandas DatetimeIndex from a dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        pd.DatetimeIndex

        """
        length = dataset.attrs["length"]
        return make_timestamps(dataset[:length])


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.contrib.auth.models import User

from .model_base import CoordBaseModel

class Profile(CoordBaseModel):
    """Profile model.
    A profile holds a user's public data like biography, picture,
    and statistics.
    """

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date_joined = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    semester = models.TextField(blank=True,null=True)
    picture = models.ImageField(
        'profile picture',
        upload_to='users/pictures/',
        blank=True,
        null=True
        )
    biography = models.TextField(max_length=500, blank=True)

    def __str__(self):
        """Return username."""
        return str(self.user)


#!/usr/bin/env python
# coding: utf-8

# In[4]:

import pandas  as pd
import matplotlib.pyplot as plt
import numpy as np
import warnings

warnings.filterwarnings('ignore')

get_ipython().run_line_magic('matplotlib', 'inline')

# In[40]:

data = pd.read_csv("st_data.csv")
data = data.replace(to_replace= r'/', value= '', regex=True)
data

# In[7]:

data.dtypes

# In[8]:

import seaborn as sns
sns.countplot(data['Lockdown_Type'],label="Count")
plt.show()

# In[26]:

sns.countplot(data['Fatality'],label="Count")
plt.show()

# In[10]:

data.drop('Country_Region', axis=1).plot(kind='box', subplots=True, layout=(7,7), sharex=False, sharey=False, figsize=(12,12),
                                        title='Box Plot for each input variable')
plt.savefig('Corona')
plt.show()

# In[5]:

import pylab as pl
data.drop('Country_Region' ,axis=1).hist(bins=30, figsize=(9,9))
pl.suptitle("Histogram for each numeric input variable")
plt.savefig('Corona_hist')
plt.show()

# In[6]:

from pandas.plotting import scatter_matrix
from matplotlib import cm
feature_names = ['Population_Size', 'Mean_Age', 'Latitude', 'Longtitude']
X = data[feature_names]
y = data['Lockdown_Type']
cmap = cm.get_cmap('gnuplot')
scatter_matrix(X, c = y, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap = cmap)
plt.suptitle('Scatter-matrix for each input variable')
plt.savefig('fruits_scatter_matrix')

# In[7]:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
print('Accuracy of Logistic regression classifier on training set: {:.2f}'
     .format(logreg.score(X_train, y_train)))
print('Accuracy of Logistic regression classifier on test set: {:.2f}'
     .format(logreg.score(X_test, y_test)))

from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier().fit(X_train, y_train)
print('Accuracy of Decision Tree classifier on training set: {:.2f}'
     .format(clf.score(X_train, y_train)))
print('Accuracy of Decision Tree classifier on test set: {:.2f}'
     .format(clf.score(X_test, y_test)))

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
print('Accuracy of K-NN classifier on training set: {:.2f}'
     .format(knn.score(X_train, y_train)))
print('Accuracy of K-NN classifier on test set: {:.2f}'
     .format(knn.score(X_test, y_test)))

from sklearn.svm import SVC
svm = SVC()
svm.fit(X_train, y_train)
print('Accuracy of SVM classifier on training set: {:.2f}'
     .format(svm.score(X_train, y_train)))
print('Accuracy of SVM classifier on test set: {:.2f}'
     .format(svm.score(X_test, y_test)))

# In[8]:

k_range = range(1, 20)
scores = []
for k in k_range:
    knn = KNeighborsClassifier(n_neighbors = k)
    knn.fit(X_train, y_train)
    scores.append(knn.score(X_test, y_test))
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20])

# In[ ]:

#Data Classification Results

# In[11]:

import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix, precision_recall_fscore_support

clf = LogisticRegression().fit(X_train,y_train)
clf.predict(X_test)
y_pred = clf.predict(X_test)

cm = confusion_matrix(y_test, y_pred)

cm_df = pd.DataFrame(cm,)
print(cm)
plt.figure(figsize=(6.5,4.5))
sns.heatmap(cm_df, annot=True)
plt.title('Logistic Regression \nAccuracy:{0:.4f}'.format(accuracy_score(y_test, y_pred)*100 ))
plt.ylabel('Train label')
plt.xlabel('Predicted label')
plt.show()

# In[12]:

import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix, precision_recall_fscore_support

clf = DecisionTreeClassifier().fit(X_train,y_train)
clf.predict(X_test)
y_pred = clf.predict(X_test)

cm = confusion_matrix(y_test, y_pred)

cm_df = pd.DataFrame(cm,)
print(cm)
plt.figure(figsize=(6.5,4.5))
sns.heatmap(cm_df, annot=True)
plt.title('Decision Tree \nAccuracy:{0:.4f}'.format(accuracy_score(y_test, y_pred)*100 ))
plt.ylabel('Train label')
plt.xlabel('Predicted label')
plt.show()

# In[13]:

import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix, precision_recall_fscore_support

clf = KNeighborsClassifier().fit(X_train,y_train)
clf.predict(X_test)
y_pred = clf.predict(X_test)

cm = confusion_matrix(y_test, y_pred)

cm_df = pd.DataFrame(cm,)
print(cm)
plt.figure(figsize=(6.5,4.5))
sns.heatmap(cm_df, annot=True)
plt.title('KNN \nAccuracy:{0:.4f}'.format(accuracy_score(y_test, y_pred)*100 ))
plt.ylabel('Train label')
plt.xlabel('Predicted label')
plt.show()

# In[14]:

import seaborn as sns
from sklearn.metrics import accuracy_score, confusion_matrix, precision_recall_fscore_support

clf = SVC().fit(X_train,y_train)
clf.predict(X_test)
y_pred = clf.predict(X_test)

cm = confusion_matrix(y_test, y_pred)

cm_df = pd.DataFrame(cm,)
print(cm)
plt.figure(figsize=(6.5,4.5))
sns.heatmap(cm_df, annot=True)
plt.title('Support Vector Macine \nAccuracy:{0:.4f}'.format(accuracy_score(y_test, y_pred)*100 ))
plt.ylabel('Train label')
plt.xlabel('Predicted label')
plt.show()

# In[24]:

data.corr()
sns.heatmap(data.corr(),cmap="YlGnBu",linewidths=.5)
plt.title('Corelation Heatmap')
plt.show()

# In[16]:

correlation = data.corr()
plt.figure(figsize=(10,10))
sns.heatmap(correlation, vmax=1, square=True,annot=True,cmap='viridis')

plt.title('Correlation between different features')

# In[8]:

data.info()

# In[ ]:

#Data Visualization and Global forecasting of corona virus

# In[6]:

# import libraries
import geopandas as gpd
from shapely.geometry import Point, Polygon
import matplotlib.pyplot as plt

df = pd.read_csv("st_data.csv")

# In[7]:

df.head()

# In[20]:

df.Lockdown_Type = df.Lockdown_Type.astype("float")
df.Fatality = df.Fatality.astype("float")

# In[23]:

import seaborn as sns
corr = df.drop(["Country_Region"],axis=1).corr()
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
f, ax = plt.subplots(figsize=(8, 6))

sns.heatmap(corr,
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values,mask=mask,cmap='summer_r',vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": 0.5})

# In[9]:

temp =df[(df.Fatality == max(df.Fatality)) ]

# In[10]:

import folium
m = folium.Map(location=[0, 0], tiles='cartodbpositron',
               min_zoom=1, max_zoom=10, zoom_start=1.5)

for i in range(0, len(temp)):
    folium.Circle(
        location=[temp.iloc[i]['Latitude'], temp.iloc[i]['Longtitude']],
        color='crimson', fill='crimson',
        tooltip =   '
<bold>Country : '+str(temp.iloc[i]['Country_Region'])+

                    '
<bold>Deaths : '+str(temp.iloc[i]['Fatality'])+
                    '
<bold>lockdown Type : '+str(temp.iloc[i]['Lockdown_Type'])+
                    '
<bold>first case date : '+str(temp.iloc[i]['Date_FirstConfirmedCase'])+
                    '
<bold>mean age : '+str(temp.iloc[i]['Mean_Age'])
        ,
        radius=int(temp.iloc[i]['Fatality']*10000)).add_to(m)

m


import pytest
import progressbar

def test_multi_progress_bar_out_of_range():
    widgets = [
        progressbar.MultiProgressBar('multivalues'),
    ]

    bar = progressbar.ProgressBar(widgets=widgets, max_value=10)
    with pytest.raises(ValueError):
        bar.update(multivalues=[123])

    with pytest.raises(ValueError):
        bar.update(multivalues=[-1])

def test_multi_progress_bar_fill_left():
    import examples
    return examples.multi_progress_bar_example(False)


import pandas as pd
import sqlite3
from src.components.backend import load_data as ld
from config import table_columns
import numpy as np

def get_stat_val(input_dataframe: pd.DataFrame, column: str, stat: str):
    if column in ('period_end', 'period_begin'):
        input_dataframe[column] = pd.to_datetime(input_dataframe[column])

    column_stats = input_dataframe[column].describe()

    value = column_stats[stat]

    return value

def rank(df: pd.DataFrame, rank_num: int, metric: str):
    df['Rank'] = df[metric].rank(ascending=False)
    df_ranked = df.sort_values(by='Rank', ascending=False)
    top_n = df_ranked.head(rank_num).sort_values(by='Rank', ascending=True)
    bottom_n = df_ranked.tail(rank_num).sort_values(by='Rank', ascending=True)
    df_plot = pd.concat([top_n, bottom_n]).drop_duplicates()
    df_plot = df_plot.sort_values(by='Rank', ascending=False)
    return df_plot

def convert_to_percent(df: pd.DataFrame, column_name: str):
    """
    Convert a column in a DataFrame from decimal to percent.

    Parameters:
        df (pd.DataFrame): The input DataFrame.
        column_name (str): The name of the column to convert to percent.

    Returns:
        pd.DataFrame: A new DataFrame with the specified column converted to percent.

    Raises:
        ValueError: If the column_name is not present in the DataFrame or if the column contains non-numeric values.
    """
    if column_name not in df.columns:
        raise ValueError(f"The column '{column_name}' does not exist in the DataFrame.")

    # Check if the column contains numeric values
    if not pd.api.types.is_numeric_dtype(df[column_name]):
        raise ValueError(f"The column '{column_name}' does not contain numeric values.")

    # Multiply the column by 100 to convert from decimal to percent
    converted_df = df.copy()
    converted_df[column_name] *= 100

    return converted_df

def calculate_differences(df, state_code, property_type, compare_to):
    metric_list = [column for column in table_columns if 'yoy' in column]
    # Step 1: Filter the dataframe
    max_date = get_stat_val(df, 'period_end', 'max')
    # Step 1: Filter the dataframe by max_date and property_type
    filtered_df_by_date_and_property = df[
        (df['period_end'] == max_date) &
        (df['property_type'] == property_type)
        ]

    # Step 2: Further filter by state_code and region_type
    filtered_df = filtered_df_by_date_and_property[
        ((filtered_df_by_date_and_property['region_type'].isin(['metro', 'state']) &
          (filtered_df_by_date_and_property['state_code'] == state_code)) |
         (filtered_df_by_date_and_property['region_type'] == 'national'))
    ]
    # Step 2: Determine the reference row
    if compare_to == 'state':
        reference_rows = filtered_df[filtered_df['region_type'] == 'state'].iloc[0]
    else:  # Assuming 'national' is the only other option
        reference_rows = filtered_df[filtered_df['region_type'] == 'national'].iloc[0]

    # Step 3: Calculate the differences
    differences = []
    for _, row in filtered_df.iterrows():
        if row['region_type'] == 'metro':  # We only want to compute differences for metros
            diff_row = {}
            diff_row['region'] = row['region']
            for metric in metric_list:
                diff_row[metric] = row[metric] - reference_rows[metric]
            differences.append(diff_row)

    # Convert differences list of dictionaries to a dataframe
    diff_df = pd.DataFrame(differences)

    # Order the dataframe by 'region' column A-Z
    diff_df = diff_df.sort_values(by='region')

    # Drop rows where all metric columns have NaN values
    diff_df = diff_df.dropna(subset=metric_list, how='all')

    return diff_df

def add_heatmap_annotations(fig, data):
    """
    Adds annotations (text) to the heatmap cells.

    Parameters:
    - fig: The figure object to which annotations are added.
    - data: The data used to generate the heatmap.
    """
    annotations = []
    for y in range(data.shape[0]):
        for x in range(data.shape[1]):
            value = data.iloc[y, x]
            # Convert the number to a percentage and format it only if the value is not NaN
            percentage_text = f"{value * 100:.2f}%" if not np.isnan(value) else ""

            annotations.append(
                dict(
                    x=x,
                    y=y,
                    xref='x',
                    yref='y',
                    text=percentage_text,
                    showarrow=False,
                    font=dict(
                        family="Arial",
                        size=10,
                        color="black"
                    )
                )
            )
    fig.update_layout(annotations=annotations)
    return fig

if __name__ == "__main__":
    conn = sqlite3.connect('src/components/backend/market_tracker.db')
    cursor = conn.cursor()
    query = f"""

            SELECT * FROM {ld.table_names['metro']}
            UNION ALL
            SELECT * FROM {ld.table_names['state']}
            UNION ALL
            SELECT * FROM {ld.table_names['national']}

            """
    data = pd.read_sql_query(query, conn)
    state_code = 'TX'
    property_type = 'All Residential'  # replace with the desired property type
    compare_to = 'national'
    result = calculate_differences(data, state_code, property_type, compare_to)
    result.to_csv(r'C:\Users\Eric C. Balduf\Documents\table_test.csv')

    # conn = sqlite3.connect('market_tracker.db')
    # cursor = conn.cursor()
    # query = f"SELECT * FROM {l.table_names['metro']}"
    # data = pd.read_sql_query(query, conn)
    # print(get_stat_val(data, 'period_end', 'max'))


#!/usr/bin/env python
import sys
import numpy as np
from netCDF4 import Dataset

with Dataset(sys.argv[1]) as nc1, Dataset(sys.argv[2]) as nc2:
  # Check if the list of variables are the same
  if nc1.variables.keys()!=nc2.variables.keys():
    print("Variables are different")
    sys.exit(2)

  for varname in nc1.variables.keys():
    if varname == "time_iso":
      continue
    # First check if each variable has the same dimension
    if np.shape(nc1[varname][:])!=np.shape(nc2[varname][:]):
      print(varname,"dimension is different")
      sys.exit(2)
    # If dimension is the same, compare data
    else:
      diff = nc2[varname][:]-nc1[varname][:]

      if (np.abs(diff)).max() != 0:
        print(varname,"is different")
        sys.exit(2)


from enum import Enum
import numpy as np
import random

class DNAException(Exception):
  pass

class CrossoverType(Enum):
  N_POINT = 1
  N_POINT_WITH_IDENTITY = 2
  X_POINT = 3
  pass

class AbstractDNA():
  DICT_REPRESENTATION = 'representation'
  """Non functional DNA base class.

  Use this class as parent for your own dna representation.
  """

  def __init__(self, **kwargs):
    self._representation = kwargs.get(self.DICT_REPRESENTATION, None)
    self._crossover = None
    self._mutation = None
    self._distance = None
    pass

  def __hash__(self):
    return super(object, self).__hash__()

  def __eq__(self, other):
    if not isinstance(other, self.__class__):
      return False
    return self._representation == other._representation

  def __ne__(self, other):
    return not self.__eq__(other)

  def __getstate__(self):
    result = dict()
    result[self.DICT_REPRESENTATION] = self._representation
    return result

  def __setstate__(self, state):
    self.__init__(**state)

  def __mod__(self, probability):
    """
    DNA Mutation
    :param probability: Mutation probability [0,1]
    :return: new DNA object
    """
    if self._mutation is None:
      raise DNAException('ERROR: You need to assign a mutation function to _mutation!')
    return self._mutation(probability)

  def __pow__(self, DNA):
    """
    DNA Crossover
    :param DNA: other DNA object
    :return: Complementary offsprings
    """
    if not isinstance(DNA, self.__class__):
      raise DNAException('ERROR: You can only crossbreed DNA of the same type!')
    if self._crossover is None:
      raise DNAException('ERROR: You need to assign a crossover function to _crossover!')
    return self._crossover(DNA)

  def __sub__(self, DNA):
    """
    DNA Distance
    :param DNA: other DNA object
    :return: Distance of DNA objects
    """
    if not isinstance(DNA, self.__class__):
      raise DNAException('ERROR: You can only calculate the distance of DNA objects with the same type!')
    if self._crossover is None:
      raise DNAException('ERROR: You need to assign a distance function to _distance!')
    return self._distance(DNA)

  @property
  def representation(self):
    return self._representation

  pass

class IndirectLinearDNA(AbstractDNA):
  DICT_CROSSOVER_TYPE = 'crossover_type'
  DICT_POINTS = 'points'

  def __init__(self, **kwargs):
    super(IndirectLinearDNA, self).__init__(**kwargs)
    self._crossoverType = kwargs.get(self.DICT_CROSSOVER_TYPE, CrossoverType.N_POINT)
    self._points = kwargs.get(self.DICT_POINTS, 2)

  def __getstate__(self):
    result = super(IndirectLinearDNA, self).__getstate__()
    result[self.DICT_CROSSOVER_TYPE] = self._crossoverType
    result[self.DICT_POINTS] = self._points
    return result

  """
  Crossover functions for list based representations
  """

  def _cross_x_point(self, other):
    __len = min(len(self._representation), len(other._representation))

    positions = range(1, __len)
    positions = random.sample(positions, min(self._points, len(positions)))
    positions.sort()

    tmp1, tmp2 = self._representation, other._representation
    descendant_a, descendant_b = [], []

    prev_p = 0
    for p in positions:
      descendant_a += tmp1[prev_p:p]
      descendant_b += tmp2[prev_p:p]
      prev_p = p
      tmp1, tmp2 = tmp2, tmp1

    descendant_a += tmp1[prev_p:len(tmp1)]
    descendant_b += tmp2[prev_p:len(tmp2)]

    if random.random() > 0.5:
      descendant_a, descendant_b = descendant_b, descendant_a

    return [self._new_DNA(descendant) for descendant in [descendant_a, descendant_b]]

  def _cross_n_point(self, other):
    __len = min(len(self._representation), len(other._representation))
    while True:
      crossing = np.random.randint(2, size=__len)
      __sum = np.sum(crossing)
      if 0 < __sum < __len:
        break

    descendant_a = [p1 if b else p2 for p1, p2, b in zip(self._representation, other._representation, crossing)]
    descendant_b = [p2 if b else p1 for p1, p2, b in zip(self._representation, other._representation, crossing)]

    tail_1, tail_2 = self._representation[__len:len(self._representation)], other._representation[
                                                                            __len:len(other._representation)]
    if not crossing[-1]:
      tail_1, tail_2 = tail_2, tail_1

    descendant_a += tail_1
    descendant_b += tail_2

    return [self._new_DNA(descendant) for descendant in [descendant_a, descendant_b]]

  def _cross_n_point_u_identity(self, other):
    __len = min(len(self._representation), len(other._representation))
    crossing = np.random.randint(2, size=__len)
    descendant_a = [p1 if b else p2 for p1, p2, b in zip(self._representation, other._representation, crossing)]
    descendant_b = [p2 if b else p1 for p1, p2, b in zip(self._representation, other._representation, crossing)]

    tail_1, tail_2 = self._representation[__len:len(self._representation)], other._representation[
                                                                            __len:len(other._representation)]
    if not crossing[-1]:
      tail_1, tail_2 = tail_2, tail_1

    descendant_a += tail_1
    descendant_b += tail_2

    return [self._new_DNA(descendant) for descendant in [descendant_a, descendant_b]]

  def _new_DNA(self, representation):
    state = self.__getstate__()
    state[self.DICT_REPRESENTATION] = representation
    new = self.__class__(**state)
    return new

  pass

class MNIST625DNA(IndirectLinearDNA):
  class DistanceType(Enum):
    SEARCH_SPACE = 1
    PARAMETER_SPACE = 2

  class MutationType(Enum):
    NEIGHBOR = 1
    RANDOM = 2
    EXCLUSIVE = 3

  __conv = [3, 5, 7, 9, 11]
  __pool = [1, 2, 3, 4, 5]
  _search_mat = [__conv, __pool, __conv, __pool]

  def __init__(self, **kwargs):
    super(MNIST625DNA, self).__init__(**kwargs)
    if self._representation is None:
      self._representation = self._random_representation()

    self.__representation_indexed = np.asarray(
      [__values.index(a) for a, __values in zip(self._representation, self._search_mat)])

    self._crossover = {CrossoverType.N_POINT: self._cross_n_point,
                       CrossoverType.N_POINT_WITH_IDENTITY: self._cross_n_point_u_identity,
                       CrossoverType.X_POINT: self._cross_x_point,
                       }.get(self._crossoverType)

    self._mutationType = kwargs.get('mutation_type', MNIST625DNA.MutationType.NEIGHBOR)
    self._mutation = {MNIST625DNA.MutationType.NEIGHBOR: self.__mut_neighbor,
                      MNIST625DNA.MutationType.RANDOM: self.__mut_random,
                      MNIST625DNA.MutationType.EXCLUSIVE: self.__mut_exclusive,
                      }.get(self._mutationType)

    self._distanceType = kwargs.get('distance_type', MNIST625DNA.DistanceType.PARAMETER_SPACE)
    self._distance = {MNIST625DNA.DistanceType.PARAMETER_SPACE: self._distance_euclidean,
                      MNIST625DNA.DistanceType.SEARCH_SPACE: self.__distance_search_space,
                      }.get(self._distanceType)

  def __getstate__(self):
    result = super(MNIST625DNA, self).__getstate__()
    result['mutation_type'] = self._mutationType
    result['distance_type'] = self._distanceType
    return result

  def __setstate__(self, state):
    self.__init__(**state)

  def __hash__(self):
    return hash(tuple(self._representation))

  def __eq__(self, other):
    if not isinstance(other, self.__class__):
      return False
    return self._representation == other._representation

  def __ne__(self, other):
    if not isinstance(other, self.__class__):
      return True
    return self._representation != other._representation

  def _random_representation(self):
    conv1 = np.random.choice(self.__conv, 1)[0]
    pool1 = np.random.choice(self.__pool, 1)[0]
    conv2 = np.random.choice(self.__conv, 1)[0]
    pool2 = np.random.choice(self.__pool, 1)[0]
    return [conv1, pool1, conv2, pool2]

  """
  Distance
  """

  def _distance_euclidean(self, other):
    if not isinstance(other, self.__class__):
      return float('inf')
    d = np.linalg.norm(np.asarray(self._representation) - np.asarray(other._representation))
    return d

  def __distance_search_space(self, other):
    if not isinstance(other, self.__class__):
      return float('inf')
    d = np.linalg.norm(self.__representation_indexed - other.__representation_indexed)
    return d

  """
  Mutation
  """

  def __mut_neighbor(self, prob):
    representation = [__v[(__v.index(a) + int(np.sign(p))) % len(__v)] if abs(p) < prob else a
                      for p, a, __v in zip(np.random.uniform(-1, 1, size=len(self._representation)),
                                           self._representation, self._search_mat)]
    return [self._new_DNA(representation)]

  def __mut_exclusive(self, prob):
    representation = [random.choice([cp for i, cp in enumerate(t) if i != t.index(a)])
                      if a_p < prob else a for a_p, a, t in
                      zip(np.random.uniform(0, 1, size=len(self._representation)), self._representation,
                          self._search_mat)]
    return [self._new_DNA(representation)]

  def __mut_random(self, prob):
    new_dna = [random.choice(__values) if p < prob else a for p, a, __values in
               zip(np.random.uniform(0, 1, size=len(self._representation)), self._representation, self._search_mat)]
    return [self._new_DNA(new_dna)]

  pass

class MNIST625DNADual(MNIST625DNA):
  def __init__(self, **kwargs):
    super(MNIST625DNA, self).__init__(**kwargs)
    self._crossover = self.__cross_dual
    self._mutation = self.__mut_dual_2
    self._distance = self._distance_euclidean

    self.__dna_one = kwargs.get('dna_one')
    self.__dna_two = kwargs.get('dna_two')

    if not self.DICT_REPRESENTATION in kwargs:
      if not self.__dna_one:
        self.__dna_one = self._random_representation()
      if not self.__dna_two:
        self.__dna_two = self._random_representation()
      self._representation = random.choice([self.__dna_one, self.__dna_two])
      self.__representation_indexed = np.asarray(
        [__values.index(a) for a, __values in zip(self._representation, self._search_mat)])
    else:
      if not self.__dna_one or not self.__dna_two:
        raise DNAException('This Should not happen!')

    pass

  def __getstate__(self):
    result = super(MNIST625DNA, self).__getstate__()
    result['dna_one'] = self.__dna_one
    result['dna_two'] = self.__dna_two
    return result

  def __mut_dual(self, prob):
    representation = [random.choice([__v[(__v.index(a) + int(np.sign(p))) % len(__v)],
                                     b]) if abs(p) < prob else a
                      for p, a, b, __v in zip(np.random.uniform(-1, 1, size=len(self._representation)),
                                              self._representation,
                                              self.__dna_one if self._representation !=
                                                                self.__dna_one else self.__dna_two,
                                              self._search_mat)]
    state = self.__getstate__()
    state['representation'] = representation
    state['dna_one'] = self.__dna_one if self._representation != self.__dna_one else representation
    state['dna_two'] = self.__dna_two if self._representation != self.__dna_two else representation
    return [MNIST625DNADual(**state)]

  def __mut_dual_2(self, prob):
    prob_list = np.random.uniform(0, 1, size=len(self._representation))
    is_one = self._representation == self.__dna_one
    representation = [b if abs(p) < prob else a
                      for p, a, b in zip(prob_list,
                                         self._representation,
                                         self.__dna_two if is_one else self.__dna_one, )]
    rep = [a if abs(p) < prob else b
           for p, a, b in zip(prob_list,
                              self._representation,
                              self.__dna_two if is_one else self.__dna_one, )]

    state = self.__getstate__()
    state['representation'] = representation
    state['dna_one'] = rep if is_one else representation
    state['dna_two'] = representation if is_one else rep
    return [MNIST625DNADual(**state)]

  def __cross_dual(self, other):
    self_a, self_b = random.choice([(self.__dna_one, self.__dna_two), (self.__dna_two, self.__dna_one)])
    other_a, other_b = random.choice([(other.__dna_one, other.__dna_two), (other.__dna_two, other.__dna_one)])
    state = self.__getstate__()
    state['representation'] = None
    state['dna_one'] = self_a
    state['dna_two'] = other_a
    ind_a = MNIST625DNADual(**state)
    state = self.__getstate__()
    state['representation'] = None
    state['dna_one'] = self_b
    state['dna_two'] = other_b
    ind_b = MNIST625DNADual(**state)
    return [ind_a, ind_b]

  pass


import numpy as np
import cv2

def nothing(x):
	pass

cap = cv2.VideoCapture(1)

# cv2.namedWindow('BW')
# cv2.createTrackbar('Trackbar1', 'BW', 100, 255, nothing)

# cv2.namedWindow('Edge')
# cv2.createTrackbar('Trackbar2', 'Edge', 100, 255, nothing)
# cv2.createTrackbar('Trackbar3', 'Edge', 200, 255, nothing)

while cap.isOpened() :

	ret, frame = cap.read()

	if ret == True :
		cv2.imshow('one',frame)
		gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
		gray = cv2.flip(gray,1)
		cv2.imshow('two',gray)
		# thrs1 = cv2.getTrackbarPos('Trackbar1', 'BW')
		ret, balck_and_white= cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
		balck_and_white = cv2.flip(balck_and_white,0)
		cv2.imshow('three',balck_and_white)

		# thrs2 = cv2.getTrackbarPos('Trackbar2', 'Edge')
		# thrs3 = cv2.getTrackbarPos('Trackbar3', 'Edge')
		gray = cv2.flip(gray,0)
		gray = cv2.flip(gray,1)
		edge = cv2.Canny(gray,80,80)
		cv2.imshow('four', edge)

		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
	else :
		break

cap.release()
cv2.destroyAllWindows()


from dateutil.parser import parse
import xlrd
import os
import datetime
import glob
import pandas as pd
import numpy as np

def is_date(string, fuzzy=False):
    """
    Return whether the string can be interpreted as a date.
    :param string: str, string to check for date
    :param fuzzy: bool, ignore unknown tokens in string if True
    """
    try:
        parse(string, fuzzy=fuzzy)
        return True
    except ValueError:
        return False

def convert_string_to_date(date_string):
    for date_format in ('%Y-%m-%d %H:%M:%S', '%d-%m-%Y', '%d.%m.%Y', '%Y.%m.%d', '%d.%m.%y', '%d/%m/%Y', '%d/%m/%Y %H:%M:%S'):
        try:
            return datetime.datetime.strptime(date_string, date_format)
        except ValueError:
            pass
    raise ValueError('no valid date format found')

def convert_excel_date(excel_book, excel_date):
    ms_bbd_date_number = excel_date
    year, month, day, hour, minute, second = xlrd.xldate_as_tuple(ms_bbd_date_number, excel_book.datemode)
    py_date = datetime.datetime(year, month, day, hour, minute, second)
    return py_date

def main():
    # Change directory
    os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place")
    #os.chdir('/home/siwanpark/ExcelData/plenus/')
    excel_files = glob.glob('*.xls*')
    print(excel_files)
    for excel_file in excel_files:
        generate_data_frame(excel_file)  #generate data frame
        os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place")
        #os.chdir('/home/siwanpark/ExcelData/plenus/')

def generate_data_frame(file_path):
    loc = (file_path)
    wb = xlrd.open_workbook(loc)
    sheet = wb.sheet_by_index(0)

    file_name = file_path.split('.')[0]
    product_type = (file_path.split('.')[0]).split('_')[2]  # file_path = 'plenus_dry.xlsx'
    unit = 'ctn'

    stock_list = []
    stock_base_date = datetime.datetime.today().date()
    previous_code = sheet.cell(1, 0).value
    sf_code = previous_code.split('/')[0]
    sf_code = sf_code.strip()
    plenus_code = previous_code.split('/')[1]
    plenus_code = plenus_code.strip()
    product_name = sheet.cell(1, 1).value
    description = sheet.cell(1, 2).value

    i = 1
    while sheet.cell(i, 0).value != 'end':
        if sheet.cell(i, 0).value == previous_code or sheet.cell(i, 0).value == '':
            if sheet.cell(i, 9).value != '': # if stock data found
                bbd, qty = extract_stock_data(sheet.cell(i, 9).value)
                stock_data = create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd)
                stock_list.append(stock_data)
        else:
            previous_code = sheet.cell(i, 0).value
            sf_code = previous_code.split('/')[0]
            sf_code = sf_code.strip()
            plenus_code = previous_code.split('/')[1]
            plenus_code = plenus_code.strip()
            product_name = sheet.cell(i, 1).value
            description = sheet.cell(i, 2).value
            if sheet.cell(i, 9).value != '': # if stock data found
                bbd, qty = extract_stock_data(sheet.cell(i, 9).value)
                stock_data = create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd)
                stock_list.append(stock_data)

        i += 1

    df_usage = pd.DataFrame(stock_list)
    stock_file_name = file_name + '_processed_stock.xlsx'
    #os.chdir('/home/siwanpark/ExcelData/plenus')
    os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place\uploading_files")
    df_usage.to_excel(stock_file_name)

def extract_stock_data(input_string):
    #print(input_string)
    input_list = input_string.split()
    stock_qty = input_list[0]
    temp_string = input_list[1]
    bbd = temp_string.split(':')[1]
    stock_bbd = datetime.datetime.strptime(bbd, "%d/%m/%Y")
    #print(type(stock_bbd))

    return stock_bbd, stock_qty

def create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd):
    stock_data = {'stock_base_date': stock_base_date, 'product_type': product_type, 'sf_code': sf_code, 'plenus_code': plenus_code,
                  'product_name': product_name, 'description': description, 'qty': qty, 'unit': unit, 'bbd': bbd}
    return stock_data

if __name__ == "__main__":
    main()


'''
CS5250 Assignment 4, Scheduling policies simulator
Sample skeleton program
Input file:
    input.txt
Output files:
    FCFS.txt
    RR.txt
    SRTF.txt
    SJF.txt
'''
import copy
import sys
from collections import deque

input_file = 'input.txt'

class Process:
    last_scheduled_time = 0

    def __init__(self, id, arrive_time, burst_time, priority=10000):
        self.id = id
        self.arrive_time = arrive_time
        self.burst_time = burst_time
        self.priority = priority  # enrich Process class to keep track of priority

    # for printing purpose
    def __repr__(self):
        return ('[id %d : arrival_time %d,  burst_time %d]'%(self.id, self.arrive_time, self.burst_time))

def FCFS_scheduling(process_list):
    # store the (switching time, proccess_id) pair
    schedule = []
    current_time = 0
    waiting_time = 0
    for process in process_list:
        if(current_time < process.arrive_time):
            current_time = process.arrive_time
        schedule.append((current_time,process.id))
        waiting_time = waiting_time + (current_time - process.arrive_time)
        current_time = current_time + process.burst_time
    average_waiting_time = waiting_time/float(len(process_list))
    return schedule, average_waiting_time

# Input: process_list, time_quantum (Positive Integer)
# Output_1 : Schedule list contains pairs of (time_stamp, proccess_id) indicating the time switching to that proccess_id
# Output_2 : Average Waiting Time
def RR_scheduling(process_list, time_quantum ):
    schedule = []
    num_of_process = len(process_list)
    processes = copy.deepcopy(process_list)
    ready_queue = deque()  # using double ended queue to simulate ready queue under RR scheduling algo
    process_expired_quantum = None  # add process expired its quantum to the very end even to break the tie
    current_time = 0
    waiting_time = 0
    while(1):
        completed = True

        tmp = []
        for process in processes:
            # check arrival time of process, append to ready_queue if it's less than current time
            if process.arrive_time <= current_time:
                ready_queue.append(process)
                tmp.append(process)
        if process_expired_quantum:
            ready_queue.append(process_expired_quantum)

        # remove arrived process from process list, prevent from re-adding
        for arrived_process in tmp:
            processes.remove(arrived_process)

        # there are processes to be execute but yet arrived
        if processes and not ready_queue:
            completed = False
            current_time += 1

        # waiting for process to arrive or complete executing all processes
        running_process = ready_queue.popleft() if ready_queue else None
        if running_process and running_process.burst_time > 0:
            schedule.append((current_time, running_process.id))
            completed = False
            if running_process.burst_time > time_quantum:
                current_time += time_quantum
                running_process.burst_time -= time_quantum
                process_expired_quantum = running_process  # append non-completed process at the end of the queue
            else:
                current_time += running_process.burst_time
                process_expired_quantum = None  # remove completed process from ready queue
                # use original process burst time which is not modified to calculate waiting time
                original_process = next(iter([process for process in process_list if process.id == running_process.id and process.arrive_time == running_process.arrive_time]))
                waiting_time += (current_time - running_process.arrive_time - original_process.burst_time)

        if completed:
            break
    return (schedule, waiting_time/float(num_of_process))

def SRTF_scheduling(process_list):
    ready_queue = deque()
    processes = copy.deepcopy(process_list)
    running_process = None
    schedule = []
    num_of_process = len(process_list)
    current_time = 0
    waiting_time = 0
    while(1):
        # in this task we don't need to handle processes arrive at the same time
        process = [process for process in processes if process.arrive_time == current_time]
        process = next(iter(process)) if process else None

        # waiting for new process to arrive
        if not process and not running_process:
            current_time += 1
        else:
            # initialize the running process as the 1st arrived process
            if not running_process:
                running_process = process
                # used  as flag to write to output file
                # write to output iff a new process start running, either newly arrived or shorter remaining time
                change_of_running_process = True

            # assumption:
            # current running process has the shortest burst time
            # preempt current running process only if the arrived process has smaller burst time than the remaining time of current process
            # if there is no new process comes in, complete current process in the ready queue
            if process and running_process.burst_time <= process.burst_time:
                if running_process.id != process.id:  # special handling for the first process
                    ready_queue.append(process)
                    change_of_running_process = False
            if process and running_process.burst_time > process.burst_time:
                preempted_process = running_process  # preempt running process, append to the end of the queue
                ready_queue.append(preempted_process)
                running_process = process
                change_of_running_process = True

            # write to output only if running process changed to another process
            if change_of_running_process:
                schedule.append((current_time, running_process.id))
            current_time += 1
            running_process.burst_time -= 1

        if running_process and running_process.burst_time == 0:
            # use original process burst time which is not modified to calculate waiting time
            original_process = next(iter([process for process in process_list if process.id == running_process.id and process.arrive_time == running_process.arrive_time]))
            waiting_time += (current_time - running_process.arrive_time - original_process.burst_time)
            processes.remove(running_process)  # remove process from list once completed
            # select the process with the shortest burst time to run
            if not ready_queue:
                running_process = None
            else:
                # sort in ascending burst time
                ready_queue = deque(sorted(ready_queue, key=lambda p: p.burst_time, reverse=False))
                running_process = ready_queue.popleft()
                change_of_running_process = True
        else:
            change_of_running_process = False

        completed = True if not running_process and not processes else False
        if completed:
            break

    return (schedule, waiting_time/float(num_of_process))

def SJF_scheduling(process_list, alpha):
    processes = copy.deepcopy(process_list)
    num_of_processes = len(processes)
    ready_queue = deque()
    schedule = []
    current_time = 0
    waiting_time = 0
    future_predict = {}
    while(1):
        tmp = []
        for process in processes:
            # add process with calculated priority to ready queue if arrived
            if process.arrive_time <= current_time:
                if process.id not in future_predict:
                    process.priority = 5  # use initial guess for new process
                    future_predict[process.id] = {"last_prediction": 5}
                else:
                    priority = alpha * future_predict[process.id]["last_actual_burst"] + (1 - alpha) * future_predict[process.id]["last_prediction"]
                    process.priority = priority
                    future_predict[process.id]["last_prediction"] = priority
                tmp.append(process)
                # append new process to ready queue and sort
                # ensure the left most process in the queue has the highest priority
                ready_queue.append(process)
                ready_queue = deque(sorted(ready_queue, key=lambda x: x.priority))

        for process in tmp:
            processes.remove(process)

        running_process = ready_queue.popleft() if ready_queue else None
        if running_process:
            # current running process has the shortest burst time based on prediction
            # other processes have to wait until it's completed
            # behave like FCFS here
            schedule.append((current_time, running_process.id))
            current_time += running_process.burst_time
            waiting_time += (current_time - running_process.arrive_time - running_process.burst_time)
            future_predict[running_process.id]["last_actual_burst"] = running_process.burst_time  # update param for next prediciton

        # waiting for new process to arrive
        if processes and not running_process:
            current_time += 1

        completed = True if not processes and not ready_queue else False
        if completed:
            break

    return (schedule, waiting_time/float(num_of_processes))

def read_input():
    result = []
    with open(input_file) as f:
        for line in f:
            array = line.split()
            if (len(array)!= 3):
                print ("wrong input format")
                exit()
            result.append(Process(int(array[0]),int(array[1]),int(array[2])))
    return result

def write_output(file_name, schedule, avg_waiting_time):
    with open(file_name,'w') as f:
        for item in schedule:
            f.write(str(item) + '\n')
        f.write('average waiting time %.2f \n'%(avg_waiting_time))

def main(argv):
    process_list = read_input()
    print ("printing input ----")
    for process in process_list:
        print (process)

    print ("simulating FCFS ----")
    FCFS_schedule, FCFS_avg_waiting_time =  FCFS_scheduling(process_list)
    write_output('FCFS.txt', FCFS_schedule, FCFS_avg_waiting_time )

    print ("simulating RR ----")
    RR_schedule, RR_avg_waiting_time =  RR_scheduling(process_list,time_quantum = 2)
    write_output('RR.txt', RR_schedule, RR_avg_waiting_time )

    print ("simulating SRTF ----")
    SRTF_schedule, SRTF_avg_waiting_time =  SRTF_scheduling(process_list)
    write_output('SRTF.txt', SRTF_schedule, SRTF_avg_waiting_time )

    print ("simulating SJF ----")
    SJF_schedule, SJF_avg_waiting_time =  SJF_scheduling(process_list, alpha = 0.2)
    write_output('SJF.txt', SJF_schedule, SJF_avg_waiting_time )

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


import cadquery as cq
import streamlit as st
import os
import time
from uuid import uuid4

def stl_preview(color, render):
    # Load and embed the JavaScript file
    with open("js/three.min.js", "r") as js_file:
        three_js = js_file.read()

    with open("js/STLLoader.js", "r") as js_file:
        stl_loader = js_file.read()

    with open("js/OrbitControls.js", "r") as js_file:
        orbital_controls = js_file.read()

    with open("js/stl-viewer.js", "r") as js_file:
        stl_viewer_component = (
            js_file.read()
            .replace('{__REPLACE_COLOR__}',f'0x{color[1:]}')
            .replace('{__REPLACE_MATERIAL__}',render)
        )

    session_id = st.session_state['session_id']
    st.components.v1.html(
        r'
'+
        r''+
        r'<stl-viewer model="./app/static/model_' + str(session_id) + '.stl?cache='+str(time.time())+r'"></stl-viewer>'+
        r'
',
        height = 500
        )

def letter(let, angle, fontPath=""):
    """Extrude a letter, center it and rotate of the input angle"""
    wp = (cq.Workplane('XZ')
        .text(let, fontsize, extr, fontPath=fontPath, valign='bottom')
        )
    b_box = wp.combine().objects[0].BoundingBox()
    x_shift = -(b_box.xlen/2 + b_box.xmin )
    wp = (wp.translate([x_shift,extr/2,0])
        .rotate((0,0,0),(0,0,1),angle)
        )
    return wp

def dual_text(text1, text2, fontPath='', save='stl', b_h=2, b_pad=2, b_fil_per=0.8, space_per=0.3, export_name='file'):
    """Generate the dual letter illusion from the two text and save it"""
    space = fontsize*space_per # spece between letter
    res = cq.Assembly() # start assembly
    last_ymax = 0
    for ind, ab in enumerate(zip(text1, text2)):
        try:
            a = letter(ab[0], 45, fontPath=fontPath)
            b = letter(ab[1], 135, fontPath=fontPath,)
            a_inter_b = a & b
            b_box = a_inter_b.objects[0].BoundingBox()
            a_inter_b = a_inter_b.translate([0,-b_box.ymin,0])
            if ind:
                a_inter_b = a_inter_b.translate([0,last_ymax + space,0])
            last_ymax = a_inter_b.objects[0].BoundingBox().ymax
            res.add( a_inter_b) # add the intersection to the assebmly
        except:
            last_ymax += space*1.5

    b_box = res.toCompound().BoundingBox() # calculate the bounding box
    # add the base to the assembly
    res.add(cq.Workplane()
            .box(b_box.xlen+b_pad*2, b_box.ylen+b_pad*2, b_h, centered=(1,0,0))
            .translate([0, -b_pad, -b_h])
            .edges('|Z')
            .fillet(b_box.xlen/2*b_fil_per)
            )
    # convert the assemly toa shape and center it
    res = res.toCompound()
    res = res.translate([0, -b_box.ylen/2,0])
    # export the files
    cq.exporters.export(res, f'./app/static/model_{session_id}.stl')
    cq.exporters.export(res, f"{export_name}.{save}")

if __name__ == "__main__":
    # initialize session id
    if "session_id" not in st.session_state:
        st.session_state['session_id'] = uuid4()
    session_id = st.session_state['session_id']

    for file in os.listdir():
        if 'file' in file:
            try:
                os.remove(file)
            except:
                print(f'Cannot remove file {file}')

    st.title('TextTango: dual text illusion')
    st.write("Generate a custom dual letter illusion, a 3d ambigram! If you like the project put a like on [Printables](https://www.printables.com/it/model/520333-texttango-dual-letter-illusion) or [support me with a coffee](https://www.paypal.com/donate/?hosted_button_id=V4LJ3Z3B3KXRY)! On Printables you can find more info about the project.", unsafe_allow_html=True)
    st.write("The Web App supports all [Google Fonts](https://fonts.google.com/), check them out!", unsafe_allow_html=True)

    col1, col2, col3 = st.columns(3)
    # Input type
    with col1:
        text1 = st.text_input('First text', value="STOP")
    with col2:
        text2 = st.text_input('Second text', value="WORK")
    with col3:
        fontsize = st.number_input('Font size', min_value=1, max_value=None, value=20)
        extr = fontsize*2 # extrude letter

    if len(text1) != len(text2):
        st.warning("The two texts don't have the same length, letters in excess will be cut", icon="тЪая╕П")
    if not text1.isupper() or not text2.isupper():
        st.warning("Non-capital letters with different height lead to bad results", icon="тЪая╕П")

    col1, col2, col3 = st.columns(3)
    # Input type
    font_dir = os.listdir('fonts')
    with col1:
        font_name = st.selectbox('Select font', ['lato'] + sorted(font_dir))
    with col2:
        font_type_list = [f for f in os.listdir('fonts' + os.sep + font_name) if '.ttf' in f and '-' in f]
        # font with explicit type (-bold, -regular, ...)
        if font_type_list:
            font_start_name = font_type_list[0].split('-')[0]
            font_type_list_name = [f.split('-')[1].strip('.ttf') for f in font_type_list]
            font_type = st.selectbox('Font type', sorted(font_type_list_name))
            font_type_pathname = font_start_name + '-' + font_type + '.ttf'
            font_path = 'fonts' + os.sep + font_name + os.sep + font_type_pathname
        else: # font without explicit type
            font_type_list = [f for f in os.listdir('fonts' + os.sep + font_name) if '.ttf' in f]
            font_type = st.selectbox('Font type', sorted(font_type_list))
            font_path = 'fonts' + os.sep + font_name + os.sep + font_type
    with col3:
        space = st.slider('Letters space (%)', 0, 200, step=1, value=30) / 100
    # base parameters
    col1, col2, col3 = st.columns(3)
    with col1:
        b_h = st.slider('Base height', 0.0, fontsize/2, step=0.1, value=1.0)
    with col2:
        b_pad = st.slider('Base Padding', 0.0, fontsize/2, step=0.1, value=2.0)
    with col3:
        b_fil_per = st.slider('Base fillet (%)', 0, 100, step=1, value=80) / 100

    col1, _, _ = st.columns(3)
    with col1:
        out = st.selectbox('Output file type', ['stl', 'step'])

    if st.button('Render'):
        start = time.time()
        with st.spinner('Wait for it...'):
            dual_text(text1, text2, fontPath=font_path, save=out, b_h=b_h, b_pad=b_pad, b_fil_per=b_fil_per, space_per=space)
        end = time.time()
        if f'file.{out}' not in os.listdir():
            st.error('The program was not able to generate the mesh.', icon="ЁЯЪи")
        else:
            st.success(f'Rendered in {int(end-start)} seconds', icon="тЬЕ")
            with open(f'file.{out}', "rb") as file:
                btn = st.download_button(
                        label=f"Download {out}",
                        data=file,
                        file_name=f'TextTango_{text1}_{text2}.{out}',
                        mime=f"model/{out}"
                    )
            st.markdown("Post the make [on Printables](https://www.printables.com/it/model/520333-texttango-dual-letter-illusion) to support the project!", unsafe_allow_html=True)
            st.markdown("I am a student who enjoys 3D printing and programming. To support me with a coffee, just [click here!](https://www.paypal.com/donate/?hosted_button_id=V4LJ3Z3B3KXRY)", unsafe_allow_html=True)

            # stl preview
            stl_preview('#696969', "material")


"""Day 16 solution.

https://adventofcode.com/2018/day/16
"""

import logging as lg

import numpy as np
import _common

_logger = lg.getLogger(__name__)

class State:
    __slots__ = ("r1", "r2", "r3", "r4")

    def __init__(self, r1: int, r2: int, r3: int, r4: int):
        self.r1 = r1
        self.r2 = r2
        self.r3 = r3
        self.r4 = r4

    def __str__(self):
        return "({}, {}, {}, {})".format(self.r1, self.r2, self.r3, self.r4)

    def __repr__(self):
        return "{}({}, {}, {}, {})".format(
            type(self).__name__,
            repr(self.r1),
            repr(self.r2),
            repr(self.r3),
            repr(self.r4))

    def __getitem__(self, idx: int):
        if idx == 0:
            return self.r1
        elif idx == 1:
            return self.r2
        elif idx == 2:
            return self.r3
        elif idx == 3:
            return self.r4
        else:
            raise IndexError(idx)

    def __setitem__(self, idx: int, value: int):
        if idx == 0:
            self.r1 = value
        elif idx == 1:
            self.r2 = value
        elif idx == 2:
            self.r3 = value
        elif idx == 3:
            self.r4 = value
        else:
            raise IndexError(idx)

    def __eq__(self, other):
        if not isinstance(self, type(other)):
            return NotImplemented
        return (
            self.r1 == other.r1
            and self.r2 == other.r2
            and self.r3 == other.r3
            and self.r4 == other.r4)

    def copy(self):
        return type(self)(self.r1, self.r2, self.r3, self.r4)

class Instruction:
    __slots__ = ("opcode", "a", "b", "c")

    def __init__(self, opcode: int, a: int, b: int, c: int):
        self.opcode = opcode
        self.a = a
        self.b = b
        self.c = c

    @classmethod
    def from_str(cls, instruction_str: str):
        return cls(*map(int, instruction_str.strip().split()))

class Operations:
    def __init__(self):
        self.operations = {}
        self._finalised = False
        self.opnames = None

    def register_operation(self, fn):
        if self._finalised:
            raise RuntimeError("Operations set finalised")
        def wrapped(a: int, b: int, c: int, state: State):
            state[c] = fn(a, b, state)
        self.operations[fn.__name__] = wrapped
        return fn

    def apply(self, instruction: Instruction, state: State, opname: str = None):
        if opname is None:
            opname = self.opnames[instruction.opcode]
        self.operations[opname](instruction.a, instruction.b, instruction.c, state)

    def apply_all(self, instruction: Instruction, state: State):
        result_states = {}
        for opname in self.operations:
            result_state = state.copy()
            self.apply(instruction, result_state, opname=opname)
            result_states[opname] = result_state
        return result_states

    def finalise(self):
        self._finalised = True
        self.opnames = tuple(self.operations)

operations = Operations()

@operations.register_operation
def addr(a, b, state):
    return state[a] + state[b]

@operations.register_operation
def addi(a, b, state):
    return state[a] + b

@operations.register_operation
def mulr(a, b, state):
    return state[a] * state[b]

@operations.register_operation
def muli(a, b, state):
    return state[a] * b

@operations.register_operation
def banr(a, b, state):
    return state[a] & state[b]

@operations.register_operation
def bani(a, b, state):
    return state[a] & b

@operations.register_operation
def borr(a, b, state):
    return state[a] | state[b]

@operations.register_operation
def bori(a, b, state):
    return state[a] | b

@operations.register_operation
def setr(a, b, state):
    return state[a]

@operations.register_operation
def seti(a, b, state):
    return a

@operations.register_operation
def gtir(a, b, state):
    return 1 if a > state[b] else 0

@operations.register_operation
def gtri(a, b, state):
    return 1 if state[a] > b else 0

@operations.register_operation
def gtrr(a, b, state):
    return 1 if state[a] > state[b] else 0

@operations.register_operation
def eqir(a, b, state):
    return 1 if a == state[b] else 0

@operations.register_operation
def eqri(a, b, state):
    return 1 if state[a] == b else 0

@operations.register_operation
def eqrr(a, b, state):
    return 1 if state[a] == state[b] else 0

operations.finalise()

class BeforeNAfter:
    __slots__ = ("before_state", "after_state", "instruction", "_valid_ops_cache")
    _state_class = State
    _instruction_class = Instruction

    def __init__(self, before_state: State, after_state: State, instruction: Instruction = None):
        self.before_state = before_state
        self.after_state = after_state
        self.instruction = instruction
        self._valid_ops_cache = None

    @classmethod
    def from_lines(cls, lines: list):
        lines = [line for line in lines if line]
        assert lines[0].startswith("Before: ")
        assert lines[2].startswith("After: ")
        before_state = cls._state_class(*map(int, lines[0][9:19].split(", ")))
        after_state = cls._state_class(*map(int, lines[2][9:19].split(", ")))
        instruction = cls._instruction_class.from_str(lines[1])
        return cls(before_state, after_state, instruction=instruction)

    @property
    def valid_ops(self):
        if self._valid_ops_cache is None:
            result_states = operations.apply_all(self.instruction, self.before_state)
            valid_opnames = []
            for opname, result_state in result_states.items():
                if result_state == self.after_state:
                    valid_opnames.append(opname)
            self._valid_ops_cache = valid_opnames
        return self._valid_ops_cache

class Program:
    _instruction_class = Instruction
    _state_class = State

    def __init__(self, instructions: list, init_state: _state_class = None):
        self.instructions = instructions
        self.init_state = init_state or self._state_class(0, 0, 0, 0)
        self.state = self.init_state.copy()

    def run(self):
        for instruction in self.instructions:
            operations.apply(instruction, self.state)

    @classmethod
    def from_lines(cls, lines):
        instructions = [cls._instruction_class.from_str(line) for line in lines]
        return cls(instructions)

def get_validity_matrix(bnas: list):
    n_ops = len(operations.opnames)
    validity = np.ones((len(bnas), n_ops, n_ops), dtype=np.bool8)
    for j, bna in enumerate(bnas):
        validity[j, bna.instruction.opcode] = False  # 16 falses
        for valid_op in bna.valid_ops:
            validity[j, bna.instruction.opcode, operations.opnames.index(valid_op)] = True
    validity = np.all(validity, axis=0)
    return validity

def reduce_validity(validity: np.ndarray):
    n_ops = len(operations.opnames)
    while np.sum(validity) > n_ops:
        for j in range(n_ops):
            if np.sum(validity[j]) == 1:
                k = np.argwhere(validity[j])[0, 0]
                _logger.debug("Setting column {} to False except row {}".format(k, j))
                validity[:, k] = False
                validity[j, k] = True
        if not np.all(np.any(validity, axis=1)):
            return None
        for j in range(n_ops):
            if np.sum(validity[j]) == 1:
                continue
            for k in range(n_ops):
                if not validity[j, k]:
                    continue
                _logger.debug("Attempting reduction by setting row {} to {}".format(j, k))
                validity_temp = validity.copy()
                validity_temp[j] = False
                validity_temp[j, k] = True
                validity_reduced = reduce_validity(validity_temp)
                if validity_reduced is not None:
                    _logger.debug("Reduction by setting row {} to {} succeeded".format(j, k))
                    validity = validity_reduced
                    break
                _logger.debug("Reduction by setting row {} to {} failed".format(j, k))
            else:
                continue
            break
    return validity

def update_op_names(validity: np.ndarray):
    idxs = [np.argwhere(row)[0, 0] for row in validity]
    operations.opnames = tuple(operations.opnames[j] for j in idxs)
    _logger.debug("Opnames: {}".format(", ".join(operations.opnames)))

class Solution(_common.InputLinesSolution):  # TODO: unit-test, document
    def __init__(self):
        super().__init__()
        self.bnas = None

    def part_1(self):
        self.bnas = [BeforeNAfter.from_lines(self.items[j:j + 4]) for j in range(0, 3244, 4)]
        return len([bna for bna in self.bnas if len(bna.valid_ops) > 2])

    def part_2(self):
        validity = get_validity_matrix(self.bnas)
        _logger.debug("Initial validity matrix:\n{}".format(validity.astype(np.uint8)))

        validity = reduce_validity(validity.copy())
        assert np.array_equal(
            np.sum(validity, axis=1),
            np.ones((validity.shape[0],), dtype=np.uint8))
        assert np.array_equal(
            np.sum(validity, axis=0),
            np.ones((validity.shape[1],), dtype=np.uint8))
        _logger.debug("Reduced validity matrix:\n{}".format(validity.astype(np.uint8)))

        update_op_names(validity)

        program = Program.from_lines(self.items[3246:])
        program.run()
        _logger.debug("Final program state: {}".format(program.state))
        return program.state[0]

main = Solution.main
if __name__ == "__main__":  # pragma: no cover
    main()


import dns.resolver
import dns.dnssec
from tldextract import extract

class DnssecChecker:

    @staticmethod
    def get_interface_list():
        return [
            "dnssec_domain",
            "dnssec_nameserver",
            "has_dnssec",
            "dnssec_is_valid",
            "dnssec_algorithm",
        ]

    def __init__(self, website):
        self.domain = None
        self.__get_domain__(website)
        self.nameserver = None
        self.ns_ip_address = None
        self.__dnskeys = None
        self.__dnssigs = None
        self.__algorithm_name = None
        self.__has_dnssec = False
        self.__dnssec_is_valid = False
        self.__resolver = None
        self.timeout = 20.0

    def __check__(self):
        self.__get_ns__()
        if self.__has_dnssec__():
            self.__has_dnssec = True
            self.__set_algorithm_name__()
            if self.__dnssec_is_valid__():
                self.__dnssec_is_valid = True
        return self

    def __get_domain__(self, domain_name_raw):
        _, td_location, tsu_location = extract(domain_name_raw)
        domain = f"{td_location}.{tsu_location}"
        self.domain = domain

    def __get_ns__(self):
        if self.domain is not None:
            try:
                self.nameserver = \
                    self.__get_resolver__().resolve(self.domain, dns.rdatatype.NS).rrset[0].to_text()
                self.ns_ip_address = \
                    self.__get_resolver__().resolve(self.nameserver, dns.rdatatype.A).rrset[0].to_text()
            except Exception as e:
                #print(e)
                pass

    def __set_algorithm_name__(self):
        if self.__dnskeys is not None:
            algos = set()
            for dns_record in self.__dnskeys:
                dns_key_text = str(dns_record)
                algorithm_code = dns_key_text.split(" ")[2]
                algos.add(dns.dnssec.algorithm_from_text(algorithm_code).name)

            self.__algorithm_name = '|'.join(algos)

    @staticmethod
    def __get_resolver__(nameserver='8.8.8.8'):
        resolver = dns.resolver.Resolver()
        resolver.nameservers = ([nameserver])
        resolver.lifetime = 15.0
        resolver.timeout = self.timeout
        resolver.use_edns(0, dns.flags.CD | dns.flags.DO | dns.flags.RD, 4096)
        return resolver

    def get_information(self):
        self.__check__()
        return {
            "dnssec_domain": self.domain,
            "dnssec_nameserver": self.nameserver,
            "has_dnssec": self.__has_dnssec,
            "dnssec_is_valid": self.__dnssec_is_valid,
            "dnssec_algorithm": self.__algorithm_name
        }

    def __has_dnssec__(self):
        if self.domain is not None:
            try:
                req = dns.message.make_query(self.domain, dns.rdatatype.DNSKEY, want_dnssec=True)

                self.__sec_response= dns.query.udp_with_fallback(req, self.ns_ip_address, timeout=self.timeout)
                self.__dnskeys, self.__dnssigs = self.__sec_response[0].answer

                if self.__dnskeys and self.__dnssigs:
                    return True
                else:
                    return False
            except Exception as e:
                #print(e)
                return False

    def __dnssec_is_valid__(self):
        try:
            q_name = dns.name.from_text(self.domain)
            dns.dnssec.validate(self.__dnskeys, self.__dnssigs,{q_name: self.__dnskeys})
            return True
        except Exception as e:
            #print(e)
            return False


# coding=utf-8

import sys
import numpy as np
import pandas
from sklearn.metrics import roc_auc_score, roc_curve

Ch_pred1 = str(sys.argv[1])
Ch_pred2 = str(sys.argv[2])
Labels = str(sys.argv[3])
weights = str(sys.argv[4])

# load dataset
dataframe_P1 = pandas.read_csv(Ch_pred1, header=None)
Prediction_1 = dataframe_P1.values

dataframe_P2 = pandas.read_csv(Ch_pred2, header=None)
Prediction_2 = dataframe_P2.values

dataframe_Y = pandas.read_csv(Labels, header=None)
Y = dataframe_Y.values

dataframe_weights = pandas.read_csv(weights, header=None)
weights_Y = dataframe_weights.values

print(Prediction_1[0])
print(Prediction_2[0])
print(Y[0])

Prediction1_AUC = np.copy(Prediction_1)
Prediction2_AUC = np.copy(Prediction_2)

for i in range(len(Prediction_1)):
	Prediction1_AUC[i]= Prediction_1[i]*2-1
	Prediction2_AUC[i]= Prediction_2[i]*2-1

c, r = Y.shape
Y_AUC = Y.reshape(c,)

# Statistiques
# Précision des classifieurs :

AUC1 = roc_auc_score(Y_AUC, Prediction1_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC du modèle 1 est de : "+str(AUC1))

Vrais_positifs1 = 0.0
Vrais_negatifs1 = 0.0
total_positifs1 = 0.0

Vrais_positifs2 = 0.0
Vrais_negatifs2 = 0.0
total_positifs2 = 0.0

for i in range(len(Y)):
	if (Y[i] == 1):
		total_positifs1 += 1
		if (Prediction_1[i]>0.5):
			Vrais_positifs1 += 1
	if (Y[i] == 0 and Prediction_1[i] < 0.5):
		Vrais_negatifs1 += 1

Vrais_negatifs1 = Vrais_negatifs1 / (len(Y)-total_positifs1)
Vrais_positifs1 = Vrais_positifs1 / (total_positifs1)

print("Le premier classifieur prédit justement la nature de "+str(Vrais_positifs1)+" des elements de la classe+")
print("Le premier classifieur prédit justement la nature de "+str(Vrais_negatifs1)+" des elements de la classe- \n")

AUC2 = roc_auc_score(Y_AUC, Prediction2_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC du modèle 2 est de : "+str(AUC2))

for i in range(len(Y)):
	if (Y[i] == 1):
		total_positifs2 += 1
		if (Prediction_2[i]>0.5):
			Vrais_positifs2 += 1
	if (Y[i] == 0 and Prediction_2[i] < 0.5):
		Vrais_negatifs2 += 1

Vrais_negatifs2 = Vrais_negatifs2 / (len(Y)-total_positifs2)
Vrais_positifs2 = Vrais_positifs2 / (total_positifs2)

print("Le premier classifieur prédit justement la nature de "+str(Vrais_positifs2)+" des elements de la classe+")
print("Le premier classifieur prédit justement la nature de "+str(Vrais_negatifs2)+" des elements de la classe- \n")

# Pourcentage de la base sur laquelle les classifieurs sont d'accords :
en_accords = 0.0

for i in range(len(Y)):
	if ((Prediction_1[i]<0.5 and Prediction_2[i]<0.5) or (Prediction_1[i]>=0.5 and Prediction_2[i]>=0.5)):
		en_accords += 1

en_accords /= len(Y)
en_desaccords = 1 - en_accords

print("Les deux classifieurs sont en accord sur "+str(en_accords)+" de la base. \n")

# Pourcentage de la base ou au moins l'un des classifieurs a raison :

un_a_raison = 0.0

for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 or abs(Prediction_2[i]-Y[i])<0.5):
		un_a_raison += 1

un_a_raison /= len(Y)

print("Sur "+str(un_a_raison)+" de la base, au moins l'un des classifieurs à raison.\n")

# Pourcentage de la base sur lequel le classifieur 1 a raison, mais pas le classifieur 2

un_seul_a_raison = 0.0

for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 and abs(Prediction_2[i]-Y[i])>0.5):
		un_seul_a_raison += 1

un_seul_a_raison /= (en_desaccords*len(Y))

print(str(un_seul_a_raison)+": Pourcentage des évenements douteux ou seul le classifieur 1 a raison.\n")

# 5 exemples ou seul le classifieur un a raison.
print("5 exemples ou seul le classifieur un a raison.")
k = 0
for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 and abs(Prediction_2[i]-Y[i])>0.5):
		print("Classifieur 1 : "+str(Prediction_1[i])+"\nClassifieur 2 : "+str(Prediction_2[i])+"\n")
		k = k + 1
		if (k >= 5):
			break

# 5 exemples ou seul le classifieur deux a raison.
print("5 exemples ou seul le classifieur deux a raison.")
k = 0
for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])>0.5 and abs(Prediction_2[i]-Y[i])<0.5):
		print("Classifieur 1 : "+str(Prediction_1[i])+"\nClassifieur 2 : "+str(Prediction_2[i])+"\n")
		k = k + 1
		if (k >= 5):
			break

# Let's try to combine these two models

max_p1 = max(Prediction_1)
max_p2 = max(Prediction_2)

for i in range(len(Prediction_1)):
	Prediction_1[i]=Prediction_1[i]/max_p1
	Prediction_2[i]=Prediction_2[i]/max_p2

Prediction_3 = (Prediction_1+Prediction_2)/2

Prediction3_AUC = np.copy(Prediction_3)

np.savetxt("combi.csv", Prediction_3, delimiter=",")

for i in range(len(Prediction_3)):
	Prediction3_AUC[i]= Prediction_3[i]*2-1

AUC3 = roc_auc_score(Y_AUC, Prediction3_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC de la combinaison est de : "+str(AUC3))

print("voulez vous conserver les prédictions du nouveau modèle ? (o/n)")
R_user = raw_input()
if (R_user == 'o'):
	print("Nom du fichier ?")
	nom = raw_input()
	np.savetxt(nom+'.csv', Prediction_3, delimiter=",")


import numpy as np
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Employee_Data.csv')
X = dataset.iloc[:, 0:4].values
y = dataset.iloc[:, 4].values
print(X)
#Encoding categorical data
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
X[:, 0] = labelencoder.fit_transform(X[:, 0])
print("##########After Label Encoding###########")
print(X)
from sklearn.model_selection import train_test_split
#from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test =train_test_split(X, y,
                    test_size = .25, random_state = 42)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred=regressor.predict(X_test)

from sklearn import metrics
print("Error:",np.sqrt(metrics.mean_squared_error
                       (y_test,y_pred)))
print("C=",regressor.intercept_)
m=regressor.coef_
print("m=",m)

print("result2",
      regressor.predict([[1, 2700, 0, 2.3]]))

accuracy = (regressor.score(X_test,y_pred))
print("Accuracy=",accuracy)