import time
import re
import json
import os
from turkishnlp import detector
start = time.time()
obj = detector.TurkishNLP()
obj.download()
obj.create_word_set()

# writes into json file
def to_json(_poems):
    poem_dic = {}
    for _, _poem in enumerate(_poems):
        poem_dic[_] = _poem
    with open('clean_poems/' + topic + '.json', 'w', encoding="utf-8") as f_js:
        json.dump(poem_dic, f_js, sort_keys=True, indent=4, ensure_ascii=False)

# if there is match with the regex
def is_match(regex, text):
    pattern = re.compile(regex)
    return pattern.search(text) is not None

def clean(file_path):
    with open(file_path, encoding='utf-8') as json_file:
        data = json.load(json_file)
        poems = []
        for datum in data:
            poem = str(data[datum]['poem'])
            if len(poem) < 30:
                continue
            if not obj.is_turkish(poem):
                continue

            new_poem = ''
            # replace more than 2 \n to 2 n
            poem = re.sub(r'\n{3,}', '\n\n', poem)
            # removes unnecessary indents
            _list = poem.split('\n\n')
            poem = ''
            for i in _list:
                if len(re.findall('\n', i)) == 0 and i != '':
                    poem += i
                    poem += '\n'
                elif len(re.findall('\n', i)) > 0:
                    poem += '\n'
                    poem += i
                    poem += '\n'
            poem = poem.rstrip()
            poem = poem.lstrip()
            # checks each line
            for line in poem.split('\n'):
                line = line.lstrip()
                # replace ........ to ...
                line = re.sub(r'\.{3,}', "...", line)
                # line = re.sub('...br', "", line)
                # line = re.sub(r'\bbr\b', "", line)
                line = re.sub(r'\.\.\.br(.)*', "", line)

                # ... sonra yeni satira geç?
                if len(line) > 75:
                    continue
                # deletes the punctuations
                line = re.sub(r'[#$%&*+''/:;•<=>@[\\]^_`{|()}~\']', '', line)
                words = line.lower().split(' ')
                # if len(words) < 2:
                #     continue
                # deletes the names
                if len(words) == 2 or len(words) == 3:
                    for k in words:
                        if k in isim or k in soyisimler:
                            continue

                # deletes the dates
                if is_match(r'(\d+[/.\\]\d+[/.\\]\d+)', line):
                    continue
                # if there is a date or whatever with ex: 12.
                # if is_match(r'(\d+[/.\\])', line):
                #     add_line = False
                if is_match(r'(\d+[^a-zA-Z0-9\s\p{P}ıöüşğ\'])', line):
                    continue
                if is_match(r'(^[\d| ]*$)', line):
                    continue
                if len(line) < 3:
                    continue
                if is_match(r'(hotmail|gmail|outlook|com)', line):
                    continue

                new_poem += line + '\n'
            new_poem = new_poem.rstrip()
            new_poem += '<s>'
            if len(new_poem) > 45:
                poems.append(new_poem)

    to_json(poems)

if __name__ == '__main__':
    f = open('isimler.txt', 'r', encoding='utf-8')
    isim = f.read()
    isim = isim.split('\n')
    f = open('soyisimler.txt', 'r', encoding='utf-8')
    soyisimler = f.read()
    soyisimler = soyisimler.split('\n')
    for i in os.listdir(u'./data/'):
        topic = i
        start = time.time()
        clean('data/' + topic + '/' + topic + '.json')
        print('finished in %r' % (time.time() - start))


from plenum.test.bls.helper import check_bls_multi_sig_after_send
from plenum.test.pool_transactions.conftest import looper, clientAndWallet1, \
    client1, wallet1, client1Connected

nodeCount = 7
nodes_wth_bls = 7

def test_each_node_has_bls(txnPoolNodeSet):
    for node in txnPoolNodeSet:
        assert node.bls_bft
        assert node.replicas[0]._bls_bft_replica

def test_send_txns_full_bls(looper, txnPoolNodeSet,
                            client1, client1Connected, wallet1):
    check_bls_multi_sig_after_send(looper, txnPoolNodeSet,
                                   client1, wallet1,
                                   saved_multi_sigs_count=nodeCount)


# -*- coding: utf-8 -*-
import sys
import xlrd
import xlwt
import numpy as np
import joblib

from sklearn import preprocessing
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QTableWidgetItem, QMessageBox, QGraphicsScene
from window.feature_extraction_Window import Ui_MainWindow
from feature_extraction_models.mspm_principal_component_analysis import MspmPrincipalComponentAnalysis as mspm_pca
# from models.mspm_kernel_principal_component_analysis import MspmKernelPrincipalComponentAnalysis as mspm_kpca
from feature_extraction_models.mspm_partial_least_squares import MspmPartialLeastSquares as mspm_pls
from feature_extraction_models.mspm_kernel_partial_least_squares import MspmKernelPartialLeastSquares as mspm_kpls
from feature_extraction_models.autoencoder import Autoencoder as AE
from feature_extraction_models.denoise_autoencoder import DenoiseAutoencoder as DAE
from feature_extraction_models.sparse_autoencoder import SparseAutoencoder as SAE
from feature_extraction_models.label_autoencoder import LabelAutoencoder as LAE

class FS_window(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(FS_window, self).__init__(parent)

        self.setupUi(self)
        self.setWindowTitle('特征提取')
        self.setStyleSheet("#Main_Window{background-color: white}")
        self.setStyleSheet("#stackedWidget{background-color: white}")

        self.traindata.triggered.connect(self.load_traindata)
        self.testdata.triggered.connect(self.load_testdata)

        # 模型界面选择
        self.button_pca.clicked.connect(self.topage_pca)
        self.button_pls.clicked.connect(self.topage_pls)
        self.button_kpca.clicked.connect(self.topage_kpca)
        self.button_kpls.clicked.connect(self.topage_kpls)
        self.button_ae.clicked.connect(self.topage_ae)
        self.button_sae.clicked.connect(self.topage_sae)
        self.button_dae.clicked.connect(self.topage_dae)
        self.button_lae.clicked.connect(self.topage_lae)

        # PCA
        self.xunlian_pca.clicked.connect(self.construct_pca_model)
        self.xunliantezheng_pca.clicked.connect(self.extact_pca_feature)
        self.baocunmoxing_pca.clicked.connect(self.savemodel_pca)
        self.daorumoxing_pca.clicked.connect(self.loadmodel_pca)
        self.ceshitezheng_pca.clicked.connect(self.testfeature_pca)
        self.baocunshujv_pca.clicked.connect(self.savedata_pca)
        ## 画图
        self.fig_pca = Figure((12, 5))  # 15, 8
        self.canvas_pca = FigureCanvas(self.fig_pca)
        #self.canvas_pca.setParent(self.pca_gongxiantu)
        self.graphicscene_pca = QGraphicsScene()
        self.graphicscene_pca.addWidget(self.canvas_pca)
        self.toolbar_pca = NavigationToolbar(self.canvas_pca, self.pca_gongxiantu)

        # PLS
        self.xunlian_pls.clicked.connect(self.construct_pls_model)
        self.xunliantezheng_pls.clicked.connect(self.extact_pls_feature)
        self.baocunmoxing_pls.clicked.connect(self.savemodel_pls)
        self.daorumoxing_pls.clicked.connect(self.loadmodel_pls)
        self.ceshitezheng_pls.clicked.connect(self.testfeature_pls)
        self.baocunshujv_pls.clicked.connect(self.savedata_pls)
        ## 画图
        self.fig_pls = Figure((12, 5))  # 15, 8
        self.canvas_pls = FigureCanvas(self.fig_pls)
        #self.canvas_pls.setParent(self.pls_gongxiantu)
        self.graphicscene_pls = QGraphicsScene()
        self.graphicscene_pls.addWidget(self.canvas_pls)
        self.toolbar_pls = NavigationToolbar(self.canvas_pls, self.pls_gongxiantu)

        # KPCA
        self.xunlian_kpca.clicked.connect(self.construct_kpca_model)
        self.xunliantezheng_kpca.clicked.connect(self.extact_kpca_feature)
        self.baocunmoxing_kpca.clicked.connect(self.savemodel_kpca)
        self.daorumoxing_kpca.clicked.connect(self.loadmodel_kpca)
        self.ceshitezheng_kpca.clicked.connect(self.testfeature_kpca)
        self.baocunshujv_kpca.clicked.connect(self.savedata_kpca)
        ## 画图
        self.fig_kpca = Figure((6, 2.5))  # 15, 8
        self.canvas_kpca = FigureCanvas(self.fig_kpca)
        #self.canvas_kpca.setParent(self.kpca_gongxiantu)
        self.graphicscene_kpca = QGraphicsScene()
        self.graphicscene_kpca.addWidget(self.canvas_kpca)
        self.toolbar_kpca = NavigationToolbar(self.canvas_kpca, self.kpca_gongxiantu)

        # KPLS
        self.xunlian_kpls.clicked.connect(self.construct_kpls_model)
        self.xunliantezheng_kpls.clicked.connect(self.extact_kpls_feature)
        self.baocunmoxing_kpls.clicked.connect(self.savemodel_kpls)
        self.daorumoxing_kpls.clicked.connect(self.loadmodel_kpls)
        self.ceshitezheng_kpls.clicked.connect(self.testfeature_kpls)
        self.baocunshujv_kpls.clicked.connect(self.savedata_kpls)
        ## 画图
        self.fig_kpls = Figure()  # 15, 8
        self.canvas_kpls = FigureCanvas(self.fig_kpls)
        #self.canvas_kpls.setParent(self.kpls_gongxiantu)
        self.graphicscene_kpls = QGraphicsScene()
        self.graphicscene_kpls.addWidget(self.canvas_kpls)
        self.toolbar_kpls = NavigationToolbar(self.canvas_kpls, self.kpls_gongxiantu)

        # AutoEncoder
        self.chushihua_ae.clicked.connect(self.initialize_ae)
        self.daorumoxin_ae.clicked.connect(self.loadmodel_ae)
        self.xunlian_ae.clicked.connect(self.train_ae)
        self.xunliantezheng_ae.clicked.connect(self.trainfeature_ae)
        self.baocunmoxin_ae.clicked.connect(self.savemodel_ae)
        self.ceshitezheng_ae.clicked.connect(self.testfeature_ae)
        self.baocunshuju_ae.clicked.connect(self.savedata_ae)
        ## 画图用
        self.fig_ae = Figure((6, 2.5))  # 15, 8
        self.canvas_ae = FigureCanvas(self.fig_ae)
        self.canvas_ae.setParent(self.history_sae)
        self.toolbar_ae = NavigationToolbar(self.canvas_ae, self.history_sae)

        # SparseAutoEncoder
        self.chushihua_sae.clicked.connect(self.initialize_sae)
        self.daorumoxin_sae.clicked.connect(self.loadmodel_sae)
        self.xunlian_sae.clicked.connect(self.train_sae)
        self.xunliantezheng_sae.clicked.connect(self.trainfeature_sae)
        self.baocunmoxin_sae.clicked.connect(self.savemodel_sae)
        self.ceshitezheng_sae.clicked.connect(self.testfeature_sae)
        self.baocunshuju_sae.clicked.connect(self.savedata_sae)
        self.fig_sae = Figure((6, 2.5))  # 15, 8
        self.canvas_sae = FigureCanvas(self.fig_sae)
        self.canvas_sae.setParent(self.history_sae)
        self.toolbar_sae = NavigationToolbar(self.canvas_sae, self.history_sae)

        # DenoiseAutoEncoder
        self.chushihua_dae.clicked.connect(self.initialize_dae)
        self.daorumoxin_dae.clicked.connect(self.loadmodel_dae)
        self.xunlian_dae.clicked.connect(self.train_dae)
        self.xunliantezheng_dae.clicked.connect(self.trainfeature_dae)
        self.baocunmoxin_dae.clicked.connect(self.savemodel_dae)
        self.ceshitezheng_dae.clicked.connect(self.testfeature_dae)
        self.baocunshuju_dae.clicked.connect(self.savedata_dae)
        self.fig_dae = Figure((6, 2.5))  # 15, 8
        self.canvas_dae = FigureCanvas(self.fig_dae)
        self.canvas_dae.setParent(self.history_dae)
        self.toolbar_dae = NavigationToolbar(self.canvas_dae, self.history_dae)

        # LabelAutoEncoder
        self.chushihua_lae.clicked.connect(self.initialize_lae)
        self.daorumoxin_lae.clicked.connect(self.loadmodel_lae)
        self.xunlian_lae.clicked.connect(self.train_lae)
        self.xunliantezheng_lae.clicked.connect(self.trainfeature_lae)
        self.baocunmoxin_lae.clicked.connect(self.savemodel_lae)
        self.ceshitezheng_lae.clicked.connect(self.testfeature_lae)
        self.baocunshuju_lae.clicked.connect(self.savedata_lae)
        self.fig_lae = Figure((6, 2.5))  # 15, 8
        self.canvas_lae = FigureCanvas(self.fig_lae)
        self.canvas_lae.setParent(self.history_lae)
        self.toolbar_lae = NavigationToolbar(self.canvas_lae, self.history_lae)

    # 界面切换
    def topage_pca(self):
        self.stackedWidget.setCurrentWidget(self.page_PCA)
    def topage_pls(self):
        self.stackedWidget.setCurrentWidget(self.page_PLS)
    def topage_kpca(self):
        self.stackedWidget.setCurrentWidget(self.page_KPCA)
    def topage_kpls(self):
        self.stackedWidget.setCurrentWidget(self.page_KPLS)
    def topage_ae(self):
        self.stackedWidget.setCurrentWidget(self.page_AE)
    def topage_sae(self):
        self.stackedWidget.setCurrentWidget(self.page_SAE)
    def topage_dae(self):
        self.stackedWidget.setCurrentWidget(self.page_DAE)
    def topage_lae(self):
        self.stackedWidget.setCurrentWidget(self.page_LAE)

    # 导入训练数据
    def load_traindata(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "选择训练数据")
            table = xlrd.open_workbook(datafile).sheets()[0]
            nrows = table.nrows
            ncols = table.ncols
            self.trainWidget.setRowCount(nrows)
            self.trainWidget.setColumnCount(ncols)
            self.train_data = np.zeros((nrows, ncols))

            for i in range(nrows):
                for j in range(ncols):
                    self.trainWidget.setItem(i, j, QTableWidgetItem(str(table.cell_value(i, j))))
                    self.train_data[i, j] = table.cell_value(i, j)
            self.statusbar.showMessage('训练数据已导入')
        except:
            QMessageBox.information(self, 'Warning', '数据为EXCEL表格', QMessageBox.Ok)

    # 导入测试数据
    def load_testdata(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "选择测试数据")
            table = xlrd.open_workbook(datafile).sheets()[0]
            nrows = table.nrows
            ncols = table.ncols
            self.testWidget.setRowCount(nrows)
            self.testWidget.setColumnCount(ncols)
            self.test_data = np.zeros((nrows, ncols))

            for i in range(nrows):
                for j in range(ncols):
                    self.testWidget.setItem(i, j, QTableWidgetItem(str(table.cell_value(i, j))))
                    self.test_data[i, j] = table.cell_value(i, j)
            self.statusbar.showMessage('测试数据已导入')
        except:
            QMessageBox.information(self, 'Warning', '数据为EXCEL表格', QMessageBox.Ok)

    # PCA
    ## 训练模型
    def construct_pca_model(self):
        try:
            n_components = int("".join(filter(str.isdigit, self.n_components_pca.text())))
            nrows0, ncols = self.train_data.shape
            if (n_components > ncols):
                QMessageBox.information(self, 'Warning', '请设定小于原始维度的特征维度', QMessageBox.Ok)
            else:
                self.PCA_model = mspm_pca(self.train_data, n_components=n_components,
                                          preprocess=self.Pre_Processing_kpca.isChecked())
                self.PCA_model.construct_pca_model()
                self.statusbar.showMessage('模型已训练')
                a = self.PCA_model.extract_pca_ratio()
                index = np.arange(n_components)
                self.fig_pca.clear()
                plt = self.fig_pca.add_subplot(111)
                # print(KPCA_model0.explained_variance_ratio_)
                plt.bar(index, a, 0.2)
                self.canvas_pca.draw()
                self.pca_gongxiantu.setScene(self.graphicscene_pca)
                self.pca_gongxiantu.show()
        except:
            QMessageBox.information(self, 'Warning', '模型参数出错', QMessageBox.Ok)

    # 训练特征
    def extact_pca_feature(self):
        try:
            self.features_pca = self.PCA_model.extract_pca_feature(self.train_data)
            nrows, ncols = self.features_pca.shape

            self.pca_features.setRowCount(nrows)
            self.pca_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.pca_features.setItem(i, j, QTableWidgetItem(str(self.features_pca[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_pca(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            joblib.dump(self.PCA_model, datafile + '.m')
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_pca(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            joblib.load(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 测试特征
    def testfeature_pca(self):
        try:
            n_components = int("".join(filter(str.isdigit, self.n_components_pca.text())))
            self.PCA_model = mspm_pca(self.test_data, n_components=n_components,
                                      preprocess=self.Pre_Processing_kpca.isChecked())
            self.PCA_model.construct_pca_model()
            self.features_pca = self.PCA_model.extract_pca_feature(self.test_data)
            nrows, ncols = self.features_pca.shape

            self.pca_features.setRowCount(nrows)
            self.pca_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.pca_features.setItem(i, j, QTableWidgetItem(str(self.features_pca[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_pca(self):
        try:
            nrows, ncols = self.features_pca.shape
            workbook_pca = xlwt.Workbook()
            sheet_pca = workbook_pca.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_pca.write(i, j, float(self.features_pca[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_pca.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # PLS
    ## 构建模型
    def construct_pls_model(self):
        try:
            n_components = int("".join(filter(str.isdigit, self.n_components_pls.text())))
            labelcol = int("".join(filter(str.isdigit, self.biaoqianlieshu_pls.text())))
            nrows, ncols = self.train_data.shape
            self.x_pls = self.train_data[:, 0:(ncols - labelcol)]
            self.y_pls = self.train_data[:, (ncols - labelcol):]
            nrows0, ncols0 = self.x_pls.shape
            if n_components > ncols0:
                QMessageBox.information(self, 'Warning', '请设定小于原始维度的特征维度', QMessageBox.Ok)
            else:
                self.PLS_model = mspm_pls(self.x_pls, self.y_pls, n_components = n_components,
                                          preprocess=self.Pre_Processing_kpca.isChecked())
                self.PLS_model.construct_pls_model()
                self.statusbar.showMessage('模型已训练')
                a = self.PLS_model.pls.coef_
                nrows1, ncols1 = a.shape
                index = np.arange(nrows1)
                if ncols1 == 1:
                    a = np.array(a.reshape((nrows1)))
                    self.fig_pls.clear()
                    plt = self.fig_pls.add_subplot(111)
                    plt.bar(index, a, 0.2)
                    self.canvas_pls.draw()
                    self.pls_gongxiantu.setScene(self.graphicscene_pls)
                    self.pls_gongxiantu.show()
                else:
                    self.fig_pls.clear()
                    for i in range(ncols1):
                        plt = self.fig_pls.add_subplot(ncols1,1,i+1)
                        plt.bar(index, a[:, i])
                    self.fig_kpls.tight_layout()
                    self.canvas_pls.draw()
                    self.pls_gongxiantu.setScene(self.graphicscene_pls)
                    self.pls_gongxiantu.show()

        except:
             QMessageBox.information(self, 'Warning', '模型参数出错', QMessageBox.Ok)

    def extact_pls_feature(self):
        try:
            self.features_pls = self.PLS_model.extract_pls_feature(self.x_pls)
            nrows, ncols = self.features_pls.shape
            self.pls_features.setRowCount(nrows)
            self.pls_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.pls_features.setItem(i, j, QTableWidgetItem(str(self.features_pls[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_pls(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            joblib.dump(self.PLS_model, datafile + '.m')
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_pls(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            joblib.load(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 测试特征
    def testfeature_pls(self):
        try:
            self.features_pls = self.PLS_model.extract_pls_feature(self.test_data)
            nrows, ncols = self.features_pls.shape

            self.pls_features.setRowCount(nrows)
            self.pls_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.pls_features.setItem(i, j, QTableWidgetItem(str(self.features_pls[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_pls(self):
        try:
            nrows, ncols = self.features_pls.shape
            workbook_pls = xlwt.Workbook()
            sheet_pls = workbook_pls.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_pls.write(i, j, float(self.features_pls[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_pls.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # KPCA
    ## 训练模型
    def construct_kpca_model(self):
        try:
            n_components = int("".join(filter(str.isdigit, self.n_components_kpca.text())))
            nrows, ncols = self.train_data.shape
            nkernel_components = int("".join(filter(str.isdigit, self.nkernel_components_kpca.text())))
            if (n_components > ncols):
                QMessageBox.information(self, 'Warning', '请设定小于原始维度的特征维度', QMessageBox.Ok)
            elif (nkernel_components < ncols):
                QMessageBox.information(self, 'Warning', '请设定大于原始维度的核特征维度', QMessageBox.Ok)
            else:
                kernel = self.kernel_function_kpca.currentText()
                self.KPCA_model = mspm_kpca(self.train_data, n_components = n_components, nkernel_components = nkernel_components,
                                        kernel = kernel, preprocess = self.Pre_Processing_kpca.isChecked())
                xKernel = self.KPCA_model.convert_to_kernel(self.train_data)
                self.KPCA_model.construct_kpca_model()
                self.statusbar.showMessage('模型已训练')
                a = self.KPCA_model.extract_kpca_ratio()
                index = np.arange(n_components)
                self.fig_kpca.clear()
                plt = self.fig_kpca.add_subplot(111)
                #print(KPCA_model0.explained_variance_ratio_)
                plt.bar(index, a, 0.2)
                self.canvas_kpca.draw()
                self.kpca_gongxiantu.setScene(self.graphicscene_kpca)
                self.kpca_gongxiantu.show()
        except:
            QMessageBox.information(self, 'Warning', '模型参数出错', QMessageBox.Ok)

    ## 训练特征
    def extact_kpca_feature(self):
        try:
            self.features_kpca = self.KPCA_model.extract_kpca_feature(self.train_data)
            nrows, ncols = self.features_kpca.shape

            self.kpca_features.setRowCount(nrows)
            self.kpca_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.kpca_features.setItem(i, j, QTableWidgetItem(str(self.features_kpca[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_kpca(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            joblib.dump(self.KPCA_model, datafile + '.m')
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_kpca(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            joblib.load(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 测试特征
    def testfeature_kpca(self):
        try:
            self.features_kpca = self.KPCA_model.extract_kpca_feature(self.test_data)
            nrows, ncols = self.features_kpca.shape

            self.kpca_features.setRowCount(nrows)
            self.kpca_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.kpca_features.setItem(i, j, QTableWidgetItem(str(self.features_kpca[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_kpca(self):
        try:
            nrows, ncols = self.features_kpca.shape
            workbook_kpca = xlwt.Workbook()
            sheet_kpca = workbook_kpca.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_kpca.write(i, j, float(self.features_kpca[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_kpca.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # KPLS
    ## 训练模型
    def construct_kpls_model(self):
        try:

            n_components = int("".join(filter(str.isdigit, self.n_components_kpls.text())))
            labelcol = int("".join(filter(str.isdigit, self.biaoqianlieshu_kpls.text())))
            nrows, ncols = self.train_data.shape
            self.x_kpls = self.train_data[:, 0:(ncols - labelcol)]
            self.y_kpls = self.train_data[:, (ncols - labelcol):]
            nrows0, ncols0 = self.x_kpls.shape
            nkernel_components = int("".join(filter(str.isdigit, self.nkernel_components_kpls.text())))
            if (n_components > ncols0):
                QMessageBox.information(self, 'Warning', '请设定小于原始维度的特征维度', QMessageBox.Ok)
            elif (nkernel_components < ncols0):
                QMessageBox.information(self, 'Warning', '请设定大于原始维度的核特征维度', QMessageBox.Ok)
            else:
                kernel = self.kernel_function_kpls.currentText()
                self.KPLS_model = mspm_kpls(self.x_kpls, self.y_kpls, n_components=n_components,
                                            nkernel_components=nkernel_components,
                                            kernel=kernel, preprocess=self.Pre_Processing_kpls.isChecked())
                xKernel = self.KPLS_model.convert_to_kernel(self.x_kpls)
                self.KPLS_model.construct_kpls_model()
                self.statusbar.showMessage('模型已训练')
                a = self.KPLS_model.kpls.coef_
                nrows1, ncols1 = a.shape
                index = np.arange(nrows1)
                if ncols1 == 1:
                    a = np.array(a.reshape((nrows1)))
                    self.fig_kpls.clear()
                    plt = self.fig_kpls.add_subplot(111)
                    plt.bar(index, a)
                    self.canvas_kpls.draw()
                    self.kpls_gongxiantu.setScene(self.graphicscene_kpls)
                    self.kpls_gongxiantu.show()
                else:
                    self.fig_kpls.clear()
                    for i in range(ncols1):
                        plt = self.fig_kpls.add_subplot(ncols1, 1, i + 1)
                        plt.bar(index, a[:, i])
                    self.fig_kpls.tight_layout()
                    self.canvas_kpls.draw()
                    self.kpls_gongxiantu.setScene(self.graphicscene_kpls)
                    self.kpls_gongxiantu.show()
                    #plt.tight_layout()
        except:
            QMessageBox.information(self, 'Warning', '模型参数出错', QMessageBox.Ok)

    ##训练特征
    def extact_kpls_feature(self):
        try:
            self.features_kpls = self.KPLS_model.extract_kpls_feature(self.x_kpls)
            nrows, ncols = self.features_kpls.shape
            self.kpls_features.setRowCount(nrows)
            self.kpls_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.kpls_features.setItem(i, j, QTableWidgetItem(str(self.features_kpls[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ##保存模型
    def savemodel_kpls(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            joblib.dump(self.KPLS_model, datafile + '.m')
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ##导入模型
    def loadmodel_kpls(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            joblib.load(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ##测试特征
    def testfeature_kpls(self):
        #try:
            self.features_kpls = self.KPLS_model.extract_kpls_feature(self.test_data)
            nrows, ncols = self.features_kpls.shape
            self.kpls_features.setRowCount(nrows)
            self.kpls_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.kpls_features.setItem(i, j, QTableWidgetItem(str(self.features_kpls[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        #except:
           # QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ##保存数据
    def savedata_kpls(self):
        try:
            nrows, ncols = self.features_kpls.shape
            workbook_kpls = xlwt.Workbook()
            sheet_kpls = workbook_kpls.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_kpls.write(i, j, float(self.features_kpls[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_kpls.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # AE
    ## 构建模型
    def initialize_ae(self):
        try:
            ### 数据标准化
            self.x_ae = self.train_data
            self.StandardScaler_x_ae = preprocessing.StandardScaler().fit(self.x_ae)
            self.x_ae = self.StandardScaler_x_ae.transform(self.x_ae)
            try:
                structure = []
                for i in self.structure_ae.text().split(","):
                    structure.append(int("".join(filter(str.isdigit, i))))
                self.autoencoder = AE(self.x_ae, structure)
                self.autoencoder.construct_model()
                self.statusbar.showMessage('模型已构建')
            except:
                QMessageBox.information(self, 'Warning', '请设定隐层结构', QMessageBox.Ok)
        except:
            QMessageBox.information(self, 'Warning', '数据初始化失败，请重新导入数据', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_ae(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            self.autoencoder.load_model(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 重新训练
    def train_ae(self):
        try:
            self.statusbar.showMessage('模型训练中。。。')
            epoch = int("".join(filter(str.isdigit, self.epoch_ae.text())))
            batchsize = int("".join(filter(str.isdigit, self.batchsize_ae.text())))
            self.autoencoder.train_model(epochs=epoch, batch_size=batchsize,
                                         use_Earlystopping=self.earlystop_ae.isChecked())
            self.statusbar.showMessage('模型训练完成')
            self.fig_ae.clear()
            plt = self.fig_ae.add_subplot(111)
            plt.plot(self.autoencoder.history.history['loss'])
            self.canvas_ae.draw()
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 训练特征
    def trainfeature_ae(self):
        try:
            self.features_ae = self.autoencoder.get_features(self.x_ae)
            nrows, ncols = self.features_ae.shape

            self.AE_features.setRowCount(nrows)
            self.AE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.AE_features.setItem(i, j, QTableWidgetItem(str(self.features_ae[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_ae(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            self.autoencoder.save_model(datafile)
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 测试特征
    def testfeature_ae(self):
        try:
            self.features_ae = self.autoencoder.get_features(self.StandardScaler_x_ae.transform(self.test_data))
            nrows, ncols = self.features_ae.shape

            self.AE_features.setRowCount(nrows)
            self.AE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.AE_features.setItem(i, j, QTableWidgetItem(str(self.features_ae[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_ae(self):
        try:
            nrows, ncols = self.features_ae.shape
            workbook_ae = xlwt.Workbook()
            sheet_ae = workbook_ae.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_ae.write(i, j, float(self.features_ae[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_ae.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # SAE
    ## 构建模型
    def initialize_sae(self):
        try:
            ### 数据标准化
            self.x_sae = self.train_data
            self.StandardScaler_x_sae = preprocessing.StandardScaler().fit(self.x_sae)
            self.x_sae = self.StandardScaler_x_sae.transform(self.x_sae)
            try:
                structure = []
                for i in self.structure_sae.text().split(","):
                    structure.append(int("".join(filter(str.isdigit, i))))
                self.sparseautoencoder = SAE(self.x_sae, structure)
                self.sparseautoencoder.construct_model()
                self.statusbar.showMessage('模型已构建')
            except:
                QMessageBox.information(self, 'Warning', '请设定隐层结构', QMessageBox.Ok)
        except:
            QMessageBox.information(self, 'Warning', '数据初始化失败，请重新导入数据', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_sae(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            print(datafile)
            self.sparseautoencoder.load_model(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 重新训练
    def train_sae(self):
        try:
            self.statusbar.showMessage('模型训练中。。。')
            epoch = int("".join(filter(str.isdigit, self.epoch_sae.text())))
            batchsize = int("".join(filter(str.isdigit, self.batchsize_sae.text())))
            self.sparseautoencoder.train_model(epochs=epoch, batch_size=batchsize,
                                               use_Earlystopping=self.earlystop_sae.isChecked())
            self.statusbar.showMessage('模型训练完成')
            self.fig_sae.clear()
            plt = self.fig_sae.add_subplot(111)
            plt.plot(self.sparseautoencoder.history.history['loss'])
            self.canvas_sae.draw()
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 训练特征
    def trainfeature_sae(self):
        try:
            self.features_sae = self.sparseautoencoder.get_features(self.x_sae)
            nrows, ncols = self.features_sae.shape

            self.SAE_features.setRowCount(nrows)
            self.SAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.SAE_features.setItem(i, j, QTableWidgetItem(str(self.features_sae[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_sae(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            self.sparseautoencoder.save_model(datafile)
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 测试特征
    def testfeature_sae(self):
        try:
            self.features_sae = self.sparseautoencoder.get_features(self.StandardScaler_x_sae.transform(self.test_data))
            nrows, ncols = self.features_sae.shape

            self.SAE_features.setRowCount(nrows)
            self.SAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.SAE_features.setItem(i, j, QTableWidgetItem(str(self.features_sae[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_sae(self):
        try:
            nrows, ncols = self.features_sae.shape
            workbook_sae = xlwt.Workbook()
            sheet_sae = workbook_sae.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_sae.write(i, j, float(self.features_sae[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_sae.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # DAE
    ## 构建模型
    def initialize_dae(self):
        try:
            ### 数据标准化
            self.x_dae = self.train_data
            self.StandardScaler_x_dae = preprocessing.StandardScaler().fit(self.x_dae)
            self.x_dae = self.StandardScaler_x_dae.transform(self.x_dae)
            try:
                structure = []
                for i in self.structure_dae.text().split(","):
                    structure.append(int("".join(filter(str.isdigit, i))))
                corrput = int("".join(filter(str.isdigit, self.corrupt_dae.text())))
                self.denoiseautoencoder = DAE(x=self.x_dae, hidden_dims=structure, corrupt_rate=corrput)
                self.denoiseautoencoder.construct_model()
                self.statusbar.showMessage('模型已构建')
            except:
                QMessageBox.information(self, 'Warning', '请设定隐层结构', QMessageBox.Ok)
        except:
            QMessageBox.information(self, 'Warning', '数据初始化失败，请重新导入数据', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_dae(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            self.denoiseautoencoder.load_model(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 重新训练
    def train_dae(self):
        try:
            self.statusbar.showMessage('模型训练中。。。')
            epoch = int("".join(filter(str.isdigit, self.epoch_dae.text())))
            batchsize = int("".join(filter(str.isdigit, self.batchsize_dae.text())))
            self.denoiseautoencoder.train_model(epochs=epoch, batch_size=batchsize,
                                                use_Earlystopping=self.earlystop_sae.isChecked())
            self.statusbar.showMessage('模型训练完成')
            self.fig_dae.clear()
            plt = self.fig_dae.add_subplot(111)
            plt.plot(self.denoiseautoencoder.history.history['loss'])
            self.canvas_dae.draw()
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 训练特征
    def trainfeature_dae(self):
        try:
            self.features_dae = self.denoiseautoencoder.get_features(self.x_dae)
            nrows, ncols = self.features_dae.shape

            self.DAE_features.setRowCount(nrows)
            self.DAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.DAE_features.setItem(i, j, QTableWidgetItem(str(self.features_dae[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_dae(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            self.denoiseautoencoder.save_model(datafile)
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 测试特征
    def testfeature_dae(self):
        try:
            self.features_dae = self.denoiseautoencoder.get_features(
                self.StandardScaler_x_dae.transform(self.test_data))
            nrows, ncols = self.features_dae.shape

            self.DAE_features.setRowCount(nrows)
            self.DAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.DAE_features.setItem(i, j, QTableWidgetItem(str(self.features_dae[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_dae(self):
        try:
            nrows, ncols = self.features_dae.shape
            workbook_dae = xlwt.Workbook()
            sheet_dae = workbook_dae.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_dae.write(i, j, float(self.features_dae[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_dae.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

    # LAE
    ## 构建模型
    def initialize_lae(self):
        try:
            ### 数据标准化
            nrows, ncols = self.train_data.shape
            labelcol = int("".join(filter(str.isdigit, self.biaoqianlieshu_lae.text())))
            self.x_lae = self.train_data[:, 0:(ncols - labelcol)]
            self.y_lae = self.train_data[:, (ncols - labelcol):]

            self.StandardScaler_x_lae = preprocessing.StandardScaler().fit(self.x_lae)
            self.x_lae = self.StandardScaler_x_lae.transform(self.x_lae)

            if self.zuoyong_lae.currentText() == "分类":
                use_onehot = True
            else:
                use_onehot = False
                self.StandardScaler_y_lae = preprocessing.StandardScaler().fit(self.y_lae)
                self.y_lae = self.StandardScaler_y_lae.transform(self.y_lae)
            try:
                structure = []
                for i in self.structure_lae.text().split(","):
                    structure.append(int("".join(filter(str.isdigit, i))))
                self.labelautoencoder = LAE(x=self.x_lae, labels=self.y_lae,
                                            hidden_dims=structure, use_onehot=use_onehot)
                self.labelautoencoder.construct_model()
                self.statusbar.showMessage('模型已构建')
            except:
                QMessageBox.information(self, 'Warning', '请设定隐层结构', QMessageBox.Ok)
        except:
            QMessageBox.information(self, 'Warning', '数据初始化失败，请重新导入数据', QMessageBox.Ok)

    ## 导入模型
    def loadmodel_lae(self):
        try:
            datafile, _ = QFileDialog.getOpenFileName(self, "导入模型")
            self.labelautoencoder.load_model(datafile)
            self.statusbar.showMessage('模型已加载')
        except:
            QMessageBox.information(self, 'Warning', '模型未定义', QMessageBox.Ok)

    ## 重新训练
    def train_lae(self):
        try:
            self.statusbar.showMessage('模型训练中。。。')
            epoch = int("".join(filter(str.isdigit, self.epoch_lae.text())))
            batchsize = int("".join(filter(str.isdigit, self.batchsize_lae.text())))
            self.labelautoencoder.train_model(epochs=epoch, batch_size=batchsize,
                                              use_Earlystopping=self.earlystop_lae.isChecked())
            self.statusbar.showMessage('模型训练完成')
            self.fig_lae.clear()
            plt = self.fig_lae.add_subplot(111)
            plt.plot(self.labelautoencoder.history_encoder.history['loss'])
            self.canvas_lae.draw()
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 训练特征
    def trainfeature_lae(self):
        try:
            self.features_lae = self.labelautoencoder.get_features(self.x_lae)
            nrows, ncols = self.features_lae.shape

            self.LAE_features.setRowCount(nrows)
            self.LAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.LAE_features.setItem(i, j, QTableWidgetItem(str(self.features_lae[i, j])))
            self.statusbar.showMessage('表格所示为训练特征')
        except:
            QMessageBox.information(self, 'Warning', '训练数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存模型
    def savemodel_lae(self):
        datafile, _ = QFileDialog.getSaveFileName(self, "保存模型")
        try:
            self.labelautoencoder.save_model(datafile)
            self.statusbar.showMessage('模型已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用模型', QMessageBox.Ok)

    ## 测试特征
    def testfeature_lae(self):
        try:
            self.features_lae = self.labelautoencoder.get_features(self.StandardScaler_x_lae.transform(self.test_data))
            nrows, ncols = self.features_lae.shape

            self.LAE_features.setRowCount(nrows)
            self.LAE_features.setColumnCount(ncols)
            for i in range(nrows):
                for j in range(ncols):
                    self.LAE_features.setItem(i, j, QTableWidgetItem(str(self.features_lae[i, j])))
            self.statusbar.showMessage('表格所示为测试特征')
        except:
            QMessageBox.information(self, 'Warning', '测试数据未导入或模型未定义', QMessageBox.Ok)

    ## 保存数据
    def savedata_lae(self):
        try:
            nrows, ncols = self.features_lae.shape
            workbook_lae = xlwt.Workbook()
            sheet_lae = workbook_lae.add_sheet('sheet1')

            for i in range(nrows):
                for j in range(ncols):
                    sheet_lae.write(i, j, float(self.features_lae[i, j]))
            datafile, _ = QFileDialog.getSaveFileName(self, "保存至")
            workbook_lae.save(datafile + '.xls')
            self.statusbar.showMessage('特征数据已保存')
        except:
            QMessageBox.information(self, 'Warning', '无可用数据', QMessageBox.Ok)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = FS_window()
    win.show()
    sys.exit(app.exec_())


# coding utf-8
from cliente.forms import enlace_form, test_form
from cliente.models import ARP, Acceso, Cliente, Configuracion, Enlace, Interface, RPMResult, Trafico, LSP, TraficoLSP
from cliente.telnet import create_connection, ping_vpn, close_connection
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
import datetime, time

#########################
# CLASSES
#########################

class graph:
	def __init__(self, dtype, ddata, dcont, extra):
		self.ctype = dtype
		self.cdata = ddata
		self.ccont = dcont
		self.extra = extra

#########################
# METHODS
#########################

def grafica_rpm(interface, container):
	if interface:
		d = datetime.datetime.now()-datetime.timedelta(days=4)
		res = RPMResult.objects.filter(rpm=interface.rpm, date__gt=d).order_by('date')

		date = []
		rtt = []
		jitter = []
		pLoss = []

		for r in res:
			date.append(int(time.mktime(r.date.timetuple()) * 1000))
			rtt.append(r.rtt)
			jitter.append(r.jitter)
			pLoss.append(r.packet_loss)

		tooltip_date = "%d %b %Y %H:%M:%S %p"
		extra_serie1 = {"tooltip": {"y_start": "", "y_end": " ms"}, "date_format": tooltip_date}
		extra_serie2 = {"tooltip": {"y_start": "", "y_end": " %"}, "date_format": tooltip_date}

		chartdata = {
		    'x': date,
		    'name1': 'RTT', 'y1': rtt, 'extra1': extra_serie1,
		    'name2': 'Jitter', 'y2': jitter, 'extra2': extra_serie1,
		    'name3': 'Packet Loss', 'y3': pLoss, 'extra3': extra_serie2,
		}

		charttype = "lineWithFocusChart"

		extra = {
	        'x_is_date': True,
	        'x_axis_format': '%d %b %H',
	        'tag_script_js': True,
	        'jquery_on_ready': False,
	    }

		return graph(charttype, chartdata, container, extra)
	return None

def grafica_lsp(lsp, container):
	if lsp:
		d = datetime.datetime.now()-datetime.timedelta(days=4)
		res = TraficoLSP.objects.filter(lsp=lsp, date__gt=d).order_by('date')

		date = []
		rate = []

		for r in res:
			date.append(int(time.mktime(r.date.timetuple()) * 1000))
			rate.append(r.rate)

		tooltip_date = "%d %b %Y %H:%M:%S %p"
		extra_serie1 = {"tooltip": {"y_start": "", "y_end": " bps"}, "date_format": tooltip_date}

		chartdata = {
		    'x': date,
		    'name1': 'Traffic Rate', 'y1': rate, 'extra1': extra_serie1,
		}

		charttype = "lineWithFocusChart"

		extra = {
	        'x_is_date': True,
	        'x_axis_format': '%d %b %H',
	        'tag_script_js': True,
	        'jquery_on_ready': False,
	    }

		return graph(charttype, chartdata, container, extra)
	return None

def grafica_bw(interface, container):
	if interface:
		d = datetime.datetime.now()-datetime.timedelta(days=4)
		tra = Trafico.objects.filter(interface=interface, date__gt=d).order_by('date')

		date = []
		in_rate = []
		out_rate = []

		for t in tra:
			date.append(int(time.mktime(t.date.timetuple()) * 1000))
			in_rate.append(int(t.in_rate))
			out_rate.append(int(t.out_rate))

		tooltip_date = "%d %b %Y %H:%M:%S %p"
		extra_serie1 = {"tooltip": {"y_start": "", "y_end": " ms"}, "date_format": tooltip_date}

		chartdata = {
		    'x': date,
		    'name1': 'In Rate',  'y1': in_rate,  'extra1': extra_serie1,
		    'name2': 'Out Rate', 'y2': out_rate, 'extra2': extra_serie1,
		}

		charttype = "lineWithFocusChart"

		extra = {
	        'x_is_date': True,
	        'x_axis_format': '%d %b %Y %H',
	        'tag_script_js': True,
	        'jquery_on_ready': False,
	    }

		return graph(charttype, chartdata, container, extra)
	return None

#########################
# VIEWS
#########################

@login_required
def inicio(request):
	usuario = Cliente.objects.get(user=request.user.id)
	navbarlinks = Acceso.objects.filter(user=request.user.id)
	for l in navbarlinks:
		l.active = l.nombre == 'Inicio'
	return render_to_response( 'cliente_inicio.html', locals(), context_instance=RequestContext(request) )

@login_required
def enlaces(request):
	usuario = Cliente.objects.get(user=request.user.id)
	navbarlinks = Acceso.objects.filter(user=request.user.id)
	for l in navbarlinks:
		l.active = l.nombre == 'Enlaces'

	if request.method == 'POST':
		form = enlace_form(request.POST)
		if form.is_valid():
			e = Enlace.objects.filter(id=form.cleaned_data['id'], empresa=usuario.empresa.id)
			interfaces = Interface.objects.filter(enlace=e) if e else None
			if_o = interfaces.filter(tipo_PE='O') if interfaces else None
			if_i = interfaces.filter(tipo_PE='I') if interfaces else None
			if_r = interfaces.filter(tipo_PE='R') if interfaces else None

			# Configuration
			conf_if_o = Configuracion.objects.filter(interface=if_o[0]) if if_o else None
			if conf_if_o:
				conf_if_o = conf_if_o.latest('date')
			conf_if_r = Configuracion.objects.filter(interface=if_r[0]) if if_r else None
			if conf_if_r:
				conf_if_r = conf_if_r.latest('date')
			## interface I

			# Graphs RPM
			g_rpm_if_o = grafica_rpm(if_o[0], 'g_rpm_if_o') if if_o else None
			g_rpm_if_r = grafica_rpm(if_r[0], 'g_rpm_if_r') if if_r else None
			## interface I

			# Graphs BW
			g_bw_if_o = grafica_bw(if_o[0], 'g_bw_if_o') if if_o else None
			g_bw_if_r = grafica_bw(if_r[0], 'g_bw_if_r') if if_r else None
			## interface I

			# ARP Table
			d = datetime.datetime.now()-datetime.timedelta(hours=4)
			arp_if_o = ARP.objects.filter(interface=if_o[0], date__gt=d) if if_o else None
			arp_if_r = ARP.objects.filter(interface=if_r[0], date__gt=d) if if_r else None
			## interface I

			return render_to_response('cliente_enlace.html', locals(), context_instance=RequestContext(request) )

	enlaces = Enlace.objects.filter(empresa=usuario.empresa.id)
	if enlaces:
		for e in enlaces:
			e.c = ''
			e.t = 'Unknown'
			interfaces = Interface.objects.filter(enlace=e)
			if interfaces:
				for i in interfaces:
					r = RPMResult.objects.filter(rpm=i.rpm).latest('date')
					if r.packet_loss > 60:
						e.c = 'danger'
						e.t = 'Down'
					elif r.packet_loss > 0:
						e.c = 'warning'
						e.t = 'Unstable'
					else:
						e.c = 'success'
						e.t = 'Up'
	form = enlace_form()
	return render_to_response( 'cliente_enlaces.html', locals(), context_instance=RequestContext(request) )

@login_required
def test(request):
	if request.method == 'POST':
		usuario = Cliente.objects.get(user=request.user.id)
		navbarlinks = Acceso.objects.filter(user=request.user.id)
		for l in navbarlinks:
			l.active = l.nombre == 'Enlaces'

		resultado = ''
		mysubmit = 'source' in request.POST.keys()

		enlaces = Enlace.objects.filter(id=request.POST['id'], empresa=usuario.empresa.id)
		interfaces = None
		rd = None
		if enlaces:
			interfaces = Interface.objects.filter(enlace=enlaces)
		else:
			return redirect('/cliente/enlaces/')
		if interfaces:
			rd = interfaces[0].RD
		else:
			return redirect('/cliente/enlaces/')

		form = test_form(request.POST, rd=rd)

		if mysubmit:
			if_s = Interface.objects.get(id=request.POST['source'])
			if_d = Interface.objects.get(id=request.POST['destiny'])
			tn = create_connection(if_s.equipo)
			com, res = ping_vpn(tn, if_s.equipo, if_s, if_d.IP)
			close_connection(tn)
			resultado = com + '\n' + res

		return render_to_response( 'cliente_test.html', locals(), context_instance=RequestContext(request) )
	return redirect('/cliente/enlaces/')

def lsptemp(request, index):
	lsp = LSP.objects.all()[index]
	g_lsp = grafica_lsp(lsp, 'g_lsp') if lsp else None
	return render_to_response('cliente_lsp.html', locals(), context_instance=RequestContext(request))


#!/usr/bin/env python
# %%
import math
from copy import deepcopy
from time import time
from pathlib import Path

import torch
import torch.nn as nn

from torch.optim import SGD, lr_scheduler, Adam
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.transforms import functional as F
from torchvision.datasets import ImageFolder
from torchvision.models import resnet18, resnet50, resnet101

from crop import SliceCrop

num_classes = 36
batch_size = 50
num_workers = 4

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def train_model(
        model,
        dataloader,
        criterion,
        optimizer,
        scheduler,
        epochs,
        checkpoint,
):
    best_model_weights = deepcopy(model.state_dict())
    best_loss = math.inf
    best_acc = 0.
    n_data = len(dataloader.dataset)

    if checkpoint is not None:
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        scheduler.load_state_dict(checkpoint['scheduler'])
        best_loss = checkpoint['loss']
        best_acc = checkpoint['acc']

    for epoch in range(epochs):
        t0 = time()

        model.train()
        running_loss = 0.0
        running_corrects = 0

        for x, y in dataloader:
            x, y = x.to(device), y.to(device)
            optimizer.zero_grad()
            outputs = model(x)
            preds = torch.argmax(outputs, 1)
            loss = criterion(outputs, y)
            loss.backward()
            optimizer.step()
            running_loss += loss.item() * x.size(0)
            running_corrects += torch.sum(y == preds).item()
            epoch_loss = running_loss / n_data
            epoch_acc = running_corrects / n_data
        if epoch_acc > best_acc or (epoch_acc == best_acc and epoch_loss < best_loss):
            best_loss = epoch_loss
            best_acc = epoch_acc
            best_model_weights = deepcopy(model.state_dict())

        print(
            f'EPOCH: {epoch}, {time()-t0:.0f} secs, Loss: {epoch_loss:.4f}, Acc: {epoch_acc:.3f}'
        )
        scheduler.step()

    model.load_state_dict(best_model_weights)
    return model, best_loss, best_acc

# %%

def train(
        position,
        root_dir: str = None,
        epochs: int = 10,
):
    assert position >= 0 and position <= 3

    checkpoint_path = 'checkpoint-' + str(position) + '.tar'
    if not root_dir:
        root_dir = '../../data/capacha/train.' + str(position)
    root_dir = Path(root_dir).expanduser()

    print(f'train: {root_dir} at {position}, {checkpoint_path}')

    transes = transforms.Compose([
        SliceCrop(position),
        transforms.Pad(12),
        # transforms.Resize(64),
        # transforms.CenterCrop(64),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])
    data = ImageFolder(root_dir, transform = transes)

    dataloader = DataLoader(
        data,
        shuffle = True,
        batch_size = batch_size,
        num_workers = num_workers,
    )

    print(f'classes: {len(data.classes)}')

    model = resnet18(pretrained = True)
    model.fc = nn.Linear(model.fc.in_features, num_classes)
    model = model.to(device)

    criterion = nn.CrossEntropyLoss()

    optimizer = Adam(model.parameters(), lr = 0.1)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size = 10, gamma = 0.9)

    try:
        checkpoint = torch.load(checkpoint_path)
    except Exception:
        checkpoint = None

    modek, loss, acc = train_model(
        model,
        dataloader,
        criterion,
        optimizer,
        exp_lr_scheduler,
        epochs,
        checkpoint,
    )

    torch.save({
        'model': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'scheduler': exp_lr_scheduler.state_dict(),
        'loss': loss,
        'acc': acc,
        'classes': data.classes,
    }, checkpoint_path)

if __name__ == '__main__':
    '''
    python train.py --root_dir=~/data/capacha/train. --position=3 --epochs=10
    '''
    from fire import Fire
    Fire(train)


from keras.models import Sequential
from keras.layers import Activation, Dense, LSTM, Reshape, Input
from keras.models import Model
from keras import backend as K
from keras.layers.core import Lambda

def preprocess_reshape(x, input_dim):
    return K.reshape(x, (-1, input_dim))

def backend_reshape(x, w, output_dim):
    return K.reshape(x, (-1, w, output_dim))

def model_cat(encoder, rnn, WINDOW_LEN,
              ENCODING_DIM_INPUT, ENCODING_DIM_OUTPUT):

    data = Input(shape=(WINDOW_LEN, ENCODING_DIM_INPUT, ))
    x = Lambda(
        preprocess_reshape,
        output_shape=(ENCODING_DIM_INPUT,),
        arguments={'input_dim': ENCODING_DIM_INPUT})(data)
    x = encoder(x)
    x = Lambda(
        backend_reshape,
        output_shape=(WINDOW_LEN, ENCODING_DIM_OUTPUT),
        arguments={'w': WINDOW_LEN,
                   'output_dim': ENCODING_DIM_OUTPUT})(x)
    out = rnn(x)

    return Model(inputs=data, output=out)

def lstm(input_shape, hidden_dim=128, dropout=0):
    '''
        input_shape=(input_length,input_feature)
    '''

    input_length, input_feature = input_shape
    model = Sequential()
    model.add(LSTM(hidden_dim, input_shape=(input_length, input_feature),
                   return_sequences=False, dropout=dropout,
                   recurrent_dropout=dropout))
    model.add(Dense(1))
    return model

def multilayer_lstm(input_shape, hidden_dim=128, dropout=0):

    input_length, input_feature = input_shape
    model = Sequential()
    model.add(LSTM(hidden_dim, input_shape=(input_length, input_feature),
                   return_sequences=True, dropout=dropout,
                   recurrent_dropout=dropout))
    model.add(LSTM(hidden_dim, input_shape=(input_length, input_feature),
                   return_sequences=True, dropout=dropout,
                   recurrent_dropout=dropout))
    model.add(LSTM(hidden_dim, input_shape=(input_length, input_feature),
                   return_sequences=False, dropout=dropout,
                   recurrent_dropout=dropout))
    model.add(Dense(1))
    return model

def linear_regression(input_shape):

    input_length, input_feature = input_shape

    model = Sequential()
    model.add(Reshape((-1,)), input_shape=((input_length, input_feature)))
    model.add(Dense(1))

    return model

def nerual_network(input_shape):

    input_length, input_feature = input_shape
    model = Sequential()
    model.add(Reshape((-1,)), input_shape=((input_length, input_feature)))
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dense(1))

    return model

# class lstm_autoencoder():

#     def __init__(self, label_dataset, unlabeled_dataset,
#                       input_shape, dropout=0):

#         self.label_dataset = label_dataset
#         self.unlabeled_dataset = unlabeled_dataset
#         self.input_length, self.input_feature = input_shape
#         self.dropout = dropout
#         self.model = Sequential()
#         self.model.add(
#             LSTM(128, input_shape=(self.input_length, self.input_feature),
#                  return_sequences=False, dropout=self.dropout,
#                  recurrent_dropout=self.dropout))
#         self.model.add(Dense(1))

#         # encoding layer
#         self.encode_layer1 = Dense(128, activation='relu')
#         self.encode_layer2 = Dense(64, activation='relu')
#         self.encode_layer3 = Dense(32, activation='relu')
#         self.encode_output = Dense(16)

#         # decoding layer
#         self.decode_layer1 = Dense(32, activation='relu')
#         self.decode_layer2 = Dense(64, activation='relu')
#         self.decode_layer3 = Dense(128, activation='relu')
#         self.decode_output = Dense(self.input_feature, activation='tanh')

#     def unsupervise_train(self):

#         # input placeholder
#         input_feature = Input(shape=(self.input_feature, ))

#         # encoding layer
#         out = self.encode_layer1(input_feature)
#         out = self.encode_layer2(out)
#         out = self.encode_layer3(out)
#         encode_feature = self.encode_output(out)

#         # decoding layer
#         out = self.decode_layer1(encode_feature)
#         out = self.decode_layer2(out)
#         out = self.decode_layer3(out)
#         decode_feature = self.decode_output(out)

#         autoencoder = Model(inputs=input_feature, outputs=decode_feature)
#         encoder = Model(inputs=input_feature, outputs=encode_feature)

#         # compile autoencoder
#         autoencoder.compile(optimizer='adam', loss='mse')

#         # training
#         autoencoder.fit(
#             self.unlabeled_dataset,
#             steps_per_epoch=np.floor(TRAIN_NUM / BATCH_SIZE)-5,
#             epochs=EPOCHS,
#             shuffle=True,
#             verbose=1,
#             # callbacks=[checkpoint]
#         )
if __name__ == "__main__":
    model = lstm((50, 136))
    model.summary()


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 13 17:12:49 2018

@author: TIM
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
myfont = fm.FontProperties(fname=u'C:\Windows\Fonts\simsun.ttc',size=16)
#%% 定义绘图辅助函数
def plt_helper(label,title):
    fig =plt.figure()
    ax = fig.add_subplot(111,label=label)
    ax.set_title(title,fontproperties=myfont)
    ax.set_xlabel('身高（cm）',fontproperties=myfont)
    ax.set_ylabel('体重（kg）',fontproperties=myfont)
    ax.axis([155,180,55,68])
    ax.grid(True)
    return ax
#%% 准备数据
X = [158, 159, 160, 161, 162, 165, 171, 172, 176]
Y = [56,  57,  58,  64,  59,  63,  62,  65,  66]
X = np.array(X)
Y = np.array(Y)

#%% 分析数据
ax = plt_helper('ax1','中学生身高与体重数据')
ax.plot(X,Y,'r*')

#%%
from sklearn.linear_model import LinearRegression
linear_rg = LinearRegression()
linear_rg.fit( X.reshape(-1,1),Y.reshape(-1,1))

x = np.array([150])
yy = linear_rg.predict(x.reshape(-1,1))

print('预测身高为155cm的中学生的体重：%.2f kg' % yy)

print("模式的参数")
print("线性模型的截距：",linear_rg.intercept_)
print("线性模型的系数：",linear_rg.coef_)

#借助这个模型我们可以预测不同身高的中学生的体重
X2 = np.array([155, 164, 167, 180])
Y2 = linear_rg.predict(X2.reshape(-1, 1))

ax2 = plt_helper('ax2','预测不同身高的中学生的体重')
ax2.plot(X,Y,'r*')
ax2.plot(X2,Y2,'y-')

# 对模型进行评估
import sklearn.metrics as ms
yy = linear_rg.predict(X.reshape(-1,1))

print('模型的平均绝对误差 =',ms.mean_absolute_error(yy,Y))
print('模型的均方根误差 =',ms.mean_squared_error(yy,Y))
print('模型的中位数绝对误差 =',ms.median_absolute_error(yy,Y))
print('模型的解释方差 =',ms.explained_variance_score(yy,Y))
print('模型的R2值 =',ms.r2_score(yy,Y))

# 模型的残差图
ax3 = plt_helper('ax3','回归模型的残差图')
for idx,x in enumerate(X):
    ax3.plot([x,x],[yy[idx],Y[idx]],'r-')
ax3.plot(X2,Y2,'b-')
ax3.plot(X,Y,'b.')

# 计算残差平方和
print('残差平方和: %.2f' % np.mean((yy - Y) ** 2))
#%%
'''
linear_rg_y = linear_rg.predict([155])
#plt.plot(X,linear_rg_y.T,'m-',label='regression')
print('LinearRegression均方误差=',np.mean(linear_rg_y - Y))
print("LinearRegression的score=",linear_rg.score( [X],[Y] ) )

#for (x,y,yy) in zip(X,Y,linear_rg_y):
#    plt.plot((x,x),(y,linear_rg_y),'ro')
'''

##%%拟合，自由度为freedeg
#freedeg = 1
#reg = np.polyfit(X,Y,deg=freedeg)
#ry = np.polyval(reg,X)
#plt.plot(X,ry,'r-',label='polyfit')
## 生成多项式对象
#pl = np.poly1d(reg)
#print(reg)
#print(pl) # 打印多项式
'''
for (x,y,yy) in zip(X,Y,linear_rg_y):
    plt.plot((x,x),(y,yy),'y-')
'''

'''
plt.legend(loc='best')
plt.show()
'''

书稿/5.2.2女中学生身高回归分析(线性回归).py

5.2.2女中学生身高回归分析(线性回归).py


import csv
import os
import subprocess
import logging

from tempfile import NamedTemporaryFile
import shutil

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

taskType, taskPeriod, taskName, taskPath, taskTime = "", "", "", os.path.abspath(
    "shutdownScript.exe"), ""
periodCriteriaC, timeCriteriaC, nameCriteriaC = False, False, False
timerCriteriaU = False
nameCriteriaD = False
cmdLineC = ""
cmdLineU = ""
cmdLineD = ""

csvFileName = "data.csv"
csvField = ["taskName", "taskPeriod", "taskTime"]
taskNameList, taskPeriodList, taskTimeList = [], [], []

periodList = ["-", "Minute", "Hourly", "Daily", "Weekly", "Monthly"]

minuteList = ["Select a minute",
              "1 minute", "2 minutes", "3 minute", "4 minute", "5 minute",
              "10 minutes", "15 minutes", "20 minutes", "25 minutes", "30 minutes",
              "35 minutes", "40 minutes", "45 minutes", "50 minutes", "55 minutes"
              ]
hourlyList = ["Select an hour",
              "1 hour", "2 hour", "3 hour", "4 hour", "5 hour", "6 hour", "7 hour", "8 hour",
              "9 hour", "10 hour", "11 hour", "12 hour", "13 hour", "14 hour", "15 hour", "16 hour",
              "17 hour", "18 hour", "19 hour", "20 hour", "21 hour", "22 hour", "23 hour",
              ]
dailyList = ["-"]
weekList = ["Select a Day", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]
monthList = ["Select a date",
             "1st Day of the Month", "2nd Day of the Month", "3rd Day of the Month",
             "4th Day of the Month", "5th Day of the Month", "6th Day of the Month",
             "7th Day of the Month", "8th Day of the Month", "9th Day of the Month",
             "10th Day of the Month", "11th Day of the Month", "12th Day of the Month",
             "13th Day of the Month", "14th Day of the Month", "15th Day of the Month",
             "16th Day of the Month", "17th Day of the Month", "18th Day of the Month",
             "19th Day of the Month", "20th Day of the Month", "21st Day of the Month",
             "22nd Day of the Month", "23rd Day of the Month", "24th Day of the Month",
             "25th Day of the Month", "26h Day of the Month", "27th Day of the Month",
             "28th Day of the Month", "29th Day of the Month", "30th Day of the Month",
             "31st Day of the Month",
             ]

# Logging function
def startLogging():
    formatter = logging.Formatter('%(levelname)s : %(asctime)s : %(message)s')
    fileHandler = logging.FileHandler('LogFile.log')
    fileHandler.setFormatter(formatter)
    logger.addHandler(fileHandler)

# create new csv file to track scheduled tasks
def newCSV():
    try:
        fileExists = os.path.isfile(csvFileName)
        with open(csvFileName, 'a', encoding='utf-8') as csvFile:
            csvWriter = csv.writer(csvFile)

            if not fileExists:
                csvWriter.writerow(csvField)
            else:
                pass
    except:
        logger.exception("Create CSV error.")

# Create new task in csv
def createCSV(rTaskName, rTaskPeriod, rTaskTime):
    try:
        with open(csvFileName, 'a+', newline='', encoding='utf-8') as csvFile:
            row = [rTaskName, rTaskPeriod, rTaskTime]
            write = csv.writer(csvFile)
            write.writerow(row)
    except:
        logger.exception("Write CSV error.")

# Update task in csv
def updateCSV(rTaskName, rTaskTime):
    try:
        tempfile = NamedTemporaryFile('w+t', newline='', delete=False)
        with open(csvFileName, 'r', newline='') as csvFile, tempfile:
            reader = csv.reader(csvFile, delimiter=',')
            writer = csv.writer(tempfile, delimiter=',')
            for row in reader:
                if len(row) == 0:
                    writer.writerow(row)
                elif row[0] == "taskName" and row[1] == "taskPeriod" and row[2] == "taskTime":
                    writer.writerow(row)
                elif row[0] == rTaskName:
                    tmp = row
                    tmp[2] = rTaskTime
                    writer.writerow(row)
                else:
                    writer.writerow(row)
        shutil.move(tempfile.name, csvFileName)
    except:
        logger.exception("update CSV error.")

# Delete task in csv
def deleteCSV(rTaskName):
    try:
        tempfile = NamedTemporaryFile('w+t', newline='', delete=False)
        with open(csvFileName, 'r', newline='') as csvFile, tempfile:
            reader = csv.reader(csvFile, delimiter=',')
            writer = csv.writer(tempfile, delimiter=',')
            for row in reader:
                if len(row) == 0:
                    writer.writerow(row)
                elif row[0] == "taskName" and row[1] == "taskPeriod" and row[2] == "taskTime":
                    writer.writerow(row)
                elif row[0] == rTaskName:
                    pass
                else:
                    writer.writerow(row)
        shutil.move(tempfile.name, csvFileName)
    except:
        logger.exception("update CSV error.")

# cmd line to create task
def createCmdLine():
    global cmdLineC

    cmdLineC = r'SCHTASKS /{taskType} /SC {taskPeriod} /TN {taskName} /TR {taskPath} /ST {taskTime}' \
        .format(taskType=taskType, taskPeriod=taskPeriod, taskName=taskName, taskPath=taskPath, taskTime=taskTime)

    return cmdLineC

# cmd line to update tassk
def updateCmdLine():
    global cmdLineU
    cmdLineU = r'SCHTASKS /{taskType} /TN {taskName} /ST {taskTime}' \
        .format(taskType=taskType, taskName=taskName, taskTime=taskTime)

    return cmdLineU

# cmd line to delete task
def deleteCmdLine():
    global cmdLineD
    cmdLineD = r'SCHTASKS /{taskType} /TN {taskName} /f' \
        .format(taskType=taskType, taskName=taskName)

    return cmdLineD

# open on-screen keyboard
# def openOnScreenKeyboard():
#     try:
#         # subprocess.call(['start', 'cmd', '/c', 'osk'], shell=True)
#     except Exception as e:
#         print(e)
#         logger.exception("Unable to open on-screen keyboard")

# create task function
def createTask():
    global cmdLineC

    try:
        # subprocess.call(['start', 'cmd', '/k', cmdLineC], shell=True)
        # __logger.info("Successfully written scheduled task")
        subprocess.run(cmdLineC, shell=True)
    except Exception as e:
        print(e)
        logger.exception("Unable to create scheduled task")

# update task function
def updateTask():
    global cmdLineU

    try:
        subprocess.call(['start', 'cmd', '/k', cmdLineU], shell=True)
        # __logger.info("Successfully written scheduled task")
    except Exception as e:
        print(e)
        logger.exception("Unable to update scheduled task")

# delete task function
def deleteTask():
    global cmdLineD

    try:
        subprocess.call(['start', 'cmd', '/k', cmdLineD], shell=True)

    except Exception as e:
        print(e)
        logger.exception("Unable to delete scheduled task")


import mysql.connector as con
db = con.connect(host ="localhost", username ="root",password = "", database = "db_atm")
cursor = db.cursor()
login1 = " "
login2 = " "
others = " "
amount_withdrawn = " "
Trans1 = " "
Trans2 = " "
Trans3 = " "
deposit = " "
user1 = " "
ola = " "
receive = " "
sub = " "
balance = " "
deduct1 = " "
deposit = " "
deposit1 = " "
deposit10 = " "
Keyda = ""
def Register():
    First_name = input("Enter your First_Name: ")
    Last_name = input("Enter your Last name: ")
    Username = Last_name + " " + First_name
    Email = input("Enter your email: ")
    Phone_Number = int(input("Enter your Phone_number: "))
    Pin = input("Enter your Four digit pin: ")
    query = "INSERT INTO user (First_Name, Last_Name, Username,Email, Phone_Number, PIN) VALUES (%s,%s,%s,%s,%s,%s)"
    values = (First_name , Last_name , Username, Email , Phone_Number , Pin)
    cursor.execute(query,values)
    db.commit()
    print("Your Username is " + Username)
    print("Thank You for Registering with us")

def user():
    global user1
    query = "SELECT User_id FROM user WHERE Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    user1 = cursor.fetchone()

def debit():
    query = "Select Transaction_Type from transaction where Username = %s"
    values = (login1)
    cursor.execute(query,values)
    debit = cursor.fetchone()
    print(debit)

def Receiver_id():
    query = "SELECT User_id FROM user WHERE Username = %s and Email = %s"
    values = (Trans1 , Trans2)
    cursor.execute(query,values)
    global receive
    receive = cursor.fetchone()
    print(receive[0])

def db_login():
    # db = con.connect(host="localhost ", user ="root",password= "", database = "db_atm")
    # cursor = db.cursor()
    query ="SELECT * FROM user where Username = %s and PIN = %s"
    # date = INSERT INTO user ("User_id", "First_Name", "Last_Name","Email", "Phone_Number", "Balance") VALUES (%s,%s,%s,%s)
    values = (login1 , login2)
    cursor.execute(query,values)
    product = cursor.fetchall()
    print(product)
    if len(product) <= 0:
       return  print("No Data Found")
    else:
        menu()

def login():
    global login1
    global login2
    login1 = input("Enter your Username ")
    login2 = int(input("Enter your Pin "))
    db_login()

def menu():
    print(" 1. Balance \n 2. Deposit \n 3. Withdraw \n 4. Transfer ")
    menu = input("Select an Option ")
    if menu == "1":
        Balance()
    elif menu == "2":
        Deposit()
    elif menu == "3":
        Withdraw()
    elif menu == "4":
        Transfer()
    else:
        print("Invalid Selection")

def Balance():
    db = con.connect(host="localhost ", user ="root",password= "", database = "db_atm")
    cursor = db.cursor()
    query ="SELECT Balance FROM user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    balance = cursor.fetchone()
    print(balance[0])
    # transaction()

def Deposit():
    deposit = int(input("Enter The Amount to want to Deposit: "))
    query = "Select Balance From user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    deposit2 = cursor.fetchone()
    deposit1 = deposit2[0] + deposit
    Deposit_Trans()
    query = "UPDATE user SET Balance = %s  WHERE Username = %s and PIN = %s "
    values = ( deposit1 , login1 , login2)
    cursor.execute(query,values)
    db.commit()
    query = "Select Balance From user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    deposit10 = cursor.fetchone()
    Keyda = deposit10[0]
    print("Your deposition was successful" )

def Deposit_Trans():
    global user1
    global deposit
    global deposit1
    query = "Select Balance From user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    deposit2 = cursor.fetchone()
    query = "SELECT User_id FROM user WHERE Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    user1 = cursor.fetchone()
    # query = "Select Balance From user where Username = %s and PIN = %s"
    # values = (login1 , login2)
    # cursor.execute(query,values)
    # deposit10 = cursor.fetchone()
    # print(deposit10[0])
    query = "INSERT INTO transaction(User_id, Username,Transaction_Type , Transaction_Performed , Initial_Balance, Final_Balance, Amount, Receivers_id , Status) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    values = (user1[0], login1 ,"Credit", "Deposit" , deposit1[0] , Keyda  , deposit , receive[0] , "Successful")
    cursor.execute(query,values)
    db.commit()

def Withdraw():
    print("1. #500 \n 2. #1000 \n 3. #2000 \n 4. #5000 \n 5. #10000 \n 6. #20000 \n 7. Others")
    withdraw = input("Select The Amount You want to Withdraw: ")
    if withdraw == "1":
        global amount_withdrawn
        amount_withdrawn = 500
        withdraw_money()
        print("You have successfully withdrawn #500")
    elif withdraw == "2":
        amount_withdrawn = 1000
        withdraw_money()
        print("You have successfully withdrawn #1000")
    elif withdraw == "3":
        amount_withdrawn = 2000
        withdraw_money()
        print("You have successfully withdrawn #2000")
    elif withdraw == "4":
        amount_withdrawn = 5000
        withdraw_money()
        print("You have successfully withdrawn #5000")
    elif withdraw == "5":
        amount_withdrawn = 10000
        withdraw_money()
        print("You have successfully withdrawn #10000")
    elif withdraw == "6":
        amount_withdrawn = 20000
        withdraw_money()
        print("You have successfully withdrawn #20000")
    elif withdraw == "7":
        Others()
    else:
        print("invalid option")

def withdraw_money():
    query = "Select Balance from user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    result = cursor.fetchone()
    sql()
    sub = result[0] - amount_withdrawn
    query = "UPDATE user SET Balance = %s  WHERE Username = %s and PIN = %s "
    values =  (sub , login1 , login2)
    cursor.execute(query,values)
    db.commit()

def sql():
    global user1
    global balance
    query = "Select Balance from user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    result = cursor.fetchone()
    query = "SELECT User_id FROM user WHERE Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    user1 = cursor.fetchone()
    query = "INSERT INTO transaction(User_id, Username,Transaction_Type , Transaction_Performed , Initial_Balance, Final_Balance, Amount, Receivers_id , Status) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    values = (user1[0], login1 ,"Debit", "Withdrawal" , result[0] , sub , amount_withdrawn , receive[0],"Successful")
    cursor.execute(query,values)
    db.commit()

def Transfer():
    global Trans1
    global Trans2
    global Trans3
    Trans1 = input("Please Type the Username Of The Person You Want to Transfer to: ")
    Trans2 = input("Please Type the Email of The Person You want to Transfer to: ")
    Trans3 = int(input("Please Enter The Amount you Transfer: "))
    transfer_check()
    transfer_deduction()

def transfer_deduction():
    query = "Select Balance from user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    deduct = cursor.fetchone()
    deduct1 = deduct[0] - Trans3
    Transfer_table()
    print("Your Transfer was Successful")
    query = "UPDATE user SET Balance = %s  WHERE Username = %s and PIN = %s "
    values = (deduct1 , login1 , login2)
    cursor.execute(query,values)
    db.commit()

def transfer_check():
    query = "Select Balance from user where Username = %s and Email = %s"
    values = (Trans1 , Trans2)
    cursor.execute(query, values)
    transfer = cursor.fetchone()
    trans12 = transfer[0] + Trans3
    query = "UPDATE user SET Balance = %s  WHERE Username = %s and Email = %s "
    values = ( trans12 , Trans1 , Trans2)
    cursor.execute(query,values)
    db.commit()

def Transfer_table():
    global Trans3
    global deduct1
    global user1
    global balance
    global receive
    query = "SELECT User_id FROM user WHERE Username = %s and Email = %s"
    values = (Trans1 , Trans2)
    cursor.execute(query,values)
    receive = cursor.fetchone()
    query ="SELECT Balance FROM user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    balance = cursor.fetchone()
    query = "SELECT User_id FROM user WHERE Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    user1 = cursor.fetchone()
    query = "INSERT INTO transaction(User_id, Username,Transaction_Type , Transaction_Performed , Initial_Balance, Final_Balance, Amount, Receivers_id , Status) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    values = (user1[0], login1 ,"Debit", "Transfer" , balance[0] , deduct1 , Trans3 , receive[0] , "Successful")
    cursor.execute(query,values)
    db.commit()

def Others():
    global others
    others = int(input("enter amount :"))
    query = "Select Balance from user where Username = %s and PIN = %s"
    values = (login1 , login2)
    cursor.execute(query,values)
    result = cursor.fetchone()
    sub = result[0] - others
    print(result[0])
    print(sub)
    query = "UPDATE user SET Balance = %s  WHERE Username = %s and PIN = %s  "
    values =  (sub , login1 , login2)
    cursor.execute(query,values)
    db.commit()
    print("You have successfully withdrawn #" + others)

def home():
    print("WELCOME TO YAHOO BANK \n 1. Register \n 2. Login")
    in1 = input("Select an Option ")
    if in1 == "1":
        Register()
    elif in1 == "2":
        login()
    else:
        print("Invalid Option")

home()


# -*- encoding: utf-8 -*-

from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Max
from mptt.fields import TreeForeignKey

from piony import const
from .piony import Pion

class BazaDostepnosci(models.Model):
    dostepny = models.BooleanField(default=True, help_text="""Czy jest dostepny czy niedostepny?""")
    adnotacja = models.CharField(max_length=100, blank=True)
    kolejnosc = models.PositiveSmallIntegerField(default=0, db_index=True)
    pion = TreeForeignKey(Pion, on_delete=models.CASCADE, blank=True, null=True)

    start = models.DateField("Początek", blank=True, null=True)
    koniec = models.DateField("Koniec", blank=True, null=True)

    dzien_1 = models.BooleanField("Poniedziałek", default=True)
    dzien_2 = models.BooleanField("Wtorek", default=True)
    dzien_3 = models.BooleanField("Środa", default=True)
    dzien_4 = models.BooleanField("Czwartek", default=True)
    dzien_5 = models.BooleanField("Piątek", default=True)
    dzien_6 = models.BooleanField("Sobota", default=True)
    dzien_7 = models.BooleanField("Niedziela", default=True)

    class Meta:
        abstract = True

class DostepnoscPionu(BazaDostepnosci):
    class Meta:
        unique_together = [('pion', 'kolejnosc')]
        ordering = ['pion', 'kolejnosc']
        verbose_name = "Reguła dostępności pionu"
        verbose_name_plural = "Reguły dostępności pionu"

    def __str__(self):
        ret = f"{self.pion} {'pracuje' if self.dostepny else 'nie pracuje'}"

        wd = []
        all = True
        all_weekdays = True
        for n, dt in enumerate(["poniedziałek", "wtorek", "środa", "czwartek", "piątek", "sobota", "niedziela"], 1):
            a = getattr(self, "dzien_%i" % n)
            if a is True:
                wd.append(dt)
            else:
                if n < 5:
                    all_weekdays = False
                all = False
        if not all:
            if all_weekdays:
                ret += " od poniedziałku do piątku"
            else:
                ret += " w %s" % ", ".join(wd)
        else:
            ret += " w każdy dzień tygodnia"

        if self.start:
            if self.koniec:
                ret += f" w okresie od {self.start} do {self.koniec}"
            else:
                ret += f" w dniu {self.start}"
        return ret

    def relevant(self, data):
        # Jeżeli podany jest zakres dat, to sprawdź, czy dotyczy parametru:
        if self.start is not None:
            # Jeżeli podany jest start i koniec to sprawdź, czy data jest w zakresie
            if self.koniec is not None:
                if data < self.start or data > self.koniec:
                    return False
            # Jeżeli podany tylko start, sprawdź czy data jest równa
            if data != self.start:
                return False

        # Jeżeli mamy podane dni tygodnia w których dana reguła ma zastosowanie::
        wd = data.isoweekday()
        attr = getattr(self, "dzien_%i" % wd)
        if attr is False:
            # Reguła nie ma zastosowania do tego dnia tygodnia
            return False

        # Reguła ma zastosowanie do zadanej daty
        return True

class DostepnoscManager(models.Manager):
    def add(self, user, **kwargs):
        n = Dostepnosc.objects.filter(user=user).aggregate(n=Max('kolejnosc'))['n']
        if n is None:
            n = -1

        return Dostepnosc.objects.create(
            user=user,
            kolejnosc=n + 1,
            **kwargs
        )

class Dostepnosc(BazaDostepnosci):
    objects = DostepnoscManager()

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    rodzaj_pionu = models.PositiveSmallIntegerField(
        choices=(
            (None, "każdy"),
            (const.DZIENNY, "dzienny"),
            (const.NOCNYSWIATECZNY, "nocny/świąteczny")
        ),
        blank=True,
        null=True,
    )

    ilosc = models.PositiveSmallIntegerField(
        "Ilość", null=True, blank=True, default=None,
        help_text="Ile razy ta reguła ma być spełniona podczas trwania jej czasokresu. Czyli np. "
                  "'chcę maksymalnie 2 dyżury w okresie tygodnia'. ")

    class Meta:
        unique_together = [
            ("user", "kolejnosc")
        ]
        ordering = ['user', 'kolejnosc']
        verbose_name = "Reguła dostępności"
        verbose_name_plural = "Reguły dostępności"

    def __str__(self):
        ret = f"{self.user} jest {'dostępny/a' if self.dostepny else 'niedostępny/a'}"
        if self.rodzaj_pionu:
            if self.rodzaj_pionu is const.DZIENNY:
                ret += " w ciągu dnia"
            elif self.rodzaj_pionu is const.NOCNYSWIATECZNY:
                ret += " na dyżury"

        if self.pion:
            ret += f" w pionie {self.pion}"

        wd = []
        all = True
        for n, dt in enumerate(["pon", "wt", "śr", "czw", "pt", "sob", "nie"], 1):
            a = getattr(self, "dzien_%i" % n)
            if a is True:
                wd.append(dt)
            else:
                all = False
        if not all:
            ret += " w %s" % ", ".join(wd)

        if self.start:
            if self.koniec:
                ret += f" od {self.start} do {self.koniec}"
            else:
                ret += f" w {self.start}"
        return ret

    def clean(self):
        if self.start is None and self.koniec is not None:
            raise ValidationError(
                {"start": "Podano koniec czasokresu, ale nie podano startu",
                 "koniec": "Podano koniec czasokresu, ale nie podano startu"},
            )

        if self.start is not None and self.koniec is not None:
            if self.start > self.koniec:
                raise ValidationError(
                    {"start": "Data w polu startu późniejsza, niż w polu końca"}
                )

        if self.pion is not None and self.rodzaj_pionu is not None:
            if self.rodzaj_pionu is const.DZIENNY and self.pion.rodzaj not in [const.DZIENNY, None]:
                raise ValidationError(
                    {"pion": "Ten pion nie jest dzienny",
                     "rodzaj_pionu": "Ta reguła nie ma sensu logicznego"},
                )
            elif self.rodzaj_pionu is const.NOCNYSWIATECZNY and self.pion.rodzaj not in [const.NOCNYSWIATECZNY, None]:
                raise ValidationError(
                    {"pion": "Ten pion nie jest nocny/świąteczny",
                     "rodzaj_pionu": "Ta reguła nie ma sensu logicznego"},
                )

        if self.ilosc is not None:
            if self.start is None or self.koniec is None:
                raise ValidationError(
                    {'ilosc': "Jeżeli podajesz ilość, musisz podać początek i koniec"}
                )

            res = (self.koniec - self.start).days
            if res < self.ilosc:
                raise ValidationError(
                    {'ilosc': 'Ilość jest zbyt duża dla tego zakresu dat'}
                )

    def relevant(self, pion, data):
        # Jeżeli mamy w regule rodzaj pionu, to sprawdź, czy jest zgodny z zadanym
        if self.rodzaj_pionu is not None:
            if pion.rodzaj != self.rodzaj_pionu:
                return False

        # Jeżeli podany jest zakres dat, to sprawdź, czy dotyczy parametru:
        if self.start is not None:
            # Jeżeli podany jest start i koniec to sprawdź, czy data jest w zakresie
            if self.koniec is not None:
                if data < self.start or data > self.koniec:
                    return False
            else:
                # Jeżeli podany tylko start, sprawdź czy data jest równa
                if data != self.start:
                    return False

        # Jeżeli mamy podane dni tygodnia w których dana reguła ma zastosowanie::
        wd = data.isoweekday()
        attr = getattr(self, "dzien_%i" % wd)
        if attr is False:
            # Reguła nie ma zastosowania do tego dnia tygodnia
            return False

        # Jeżeli mamy w regule konkretny pion, to sprawdź, czy jest identyczny z zadanym
        # lub też, czy pion określony w regule nie jest nad-pionem zadanego
        if self.pion_id is not None:

            if self.pion_id != pion.pk and not pion.pk in self.pion.get_leafnodes().values_list("pk", flat=True):
                return False

        # Reguła ma zastosowanie do zadanej daty
        return True

def pion_dzienny(pion, kolejnosc=0):
    DostepnoscPionu.objects.create(
        pion=pion,
        kolejnosc=kolejnosc,
        dostepny=True,
        dzien_6=False,
        dzien_7=False
    )

def pion_dyzurowy(pion, kolejnosc=0):
    DostepnoscPionu.objects.create(
        pion=pion,
        kolejnosc=kolejnosc,
        dostepny=True,
    )

def pracownik_etatowy(pracownik, pion=None, kolejnosc=0):
    Dostepnosc.objects.create(
        user=pracownik,
        adnotacja="Etat",
        pion=pion,
        rodzaj_pionu=const.DZIENNY,
        kolejnosc=kolejnosc,
        dostepny=True,
        dzien_6=False,
        dzien_7=False
    )


#!bin/python
from flask import Flask
import os
import filecmp
app = Flask(__name__)

@app.route('/judge/<submission_id>/<input_id>/<exoutput_id>/<int:timeout>', methods=['GET']) # time limit = timeout
def index(submission_id, input_id, exoutput_id,timeout):
    submission_path = "submissions/" + str(submission_id) + ".py"
    input_path = "inputs/" + str(input_id) + ".txt"
    print(input_path)
    exoutput_path = "exoutputs/" + str(exoutput_id) + ".txt"
    output_path = "outputs/" + str(submission_id) + ".txt"
    r = os.system("timeout " + str(timeout) + " python3 " + submission_path + " < " + input_path + " > " + output_path)
    if r==31744: # the error code returned when timeout times out. Haha
        return "TLE"
    elif r!=0:
        return "CE"
    elif filecmp.cmp(output_path,exoutput_path):
        return "AC"
    else:
        return "WA"
if __name__ =='__main__':
    app.run(debug=True)