# -*- 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_())


import datetime
import calendar
import time
from time import mktime
from dateutil import rrule
from django.contrib.auth import login, logout as auth_logout
from django.http.response import HttpResponse
from django.shortcuts import render, get_object_or_404, get_list_or_404
from django.http import HttpResponseRedirect, Http404
from django.core.urlresolvers import reverse
from django.views.generic import View
from sendemail.forms import EmailSendForm
from vehicles.models import Collection, SentEmailTemplateLog
from django.template.loader import render_to_string
from django.core.mail import send_mail
from gomoto.utils import send_html_mail
from datetime import datetime

class CollectionView(View):
    """Class used for view classes to inherit from that need to grab the collection object."""

    def get_collection(self, request):
        collection_id = request.session.get('collection_id')
        return get_object_or_404(Collection, pk=collection_id)

class SendEmailFormView(CollectionView):
    """View class used to handle the test drive booking form."""
    form_class = EmailSendForm

    def get(self, request, *args, **kwargs):
        """HTTP GET"""
        initial = {}

        template_list = ['Meeting Confirmation', 'Meeting Follow Up']

        form = self.form_class()

        return render(request, 'sendemail/first.html', {
            'form': form,
            'template_list': template_list
        })

    def post(self, request, *args, **kwargs):
        """HTTP POST"""
        form = self.form_class(request.POST)
        if form.is_valid():
            fname = form.cleaned_data['first_name']
            lname = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            template = request.POST.get(unicode('template'))
            event_date = form.cleaned_data['event_date']
            event_time = form.cleaned_data['event_time']

            #Send email
            send_new_email(request, fname, lname, email, event_date, event_time, template)

            return render(request, 'sendemail/success.html', {
            })

        else:
            #list_of_tuples = [('1', 'Test'),('2', 'Test 2'),]
            #form = EmailSendForm(list_of_tuples)

            return render(request, 'sendemail/first.html', {
                'form': form,
            })

def emailsent(request):
    """Logs a user out."""
    return render(request, 'sendemail/success.html', {
    })

def logout(request):
    """Logs a user out."""
    auth_logout(request)
    return HttpResponseRedirect(reverse('sendemail_index'))

def send_new_email(request, first_name, last_name, email, date, time, template):
    """Sends a test drive booking email to the customer."""

    if template == 'Meeting Confirmation':
        template = 'sendemail/email/meeting_confirmation.html'
        template_short = 'meeting_confirmation'
    elif template == 'Meeting Follow Up':
        template = 'sendemail/email/meeting_follow_up.html'
        template_short = 'meeting_follow_up'

    #format example: May 5th 2016, 2:15 PM EST
    event_date = datetime.combine(date, time)
    print_date = event_date.strftime('%B %-d %Y, %I:%M %p EST')

    html = render_to_string(template, {
        'first_name': first_name,
        'last_name': last_name,
        'email': email,
        'date': print_date,
    })

    send_to = []

    if email:
        send_to.append(email)
        send_html_mail('Meeting Request', html, 'asmith@shopgomoto.com', send_to)

        SentEmailTemplateLog.objects.create(first_name=first_name, last_name=last_name, email=email, event_date=event_date, template=template_short,
                                            sent_by_first_name = 'Amy', sent_by_last_name='Smith', sent_by_email='asmith@shopgomoto.com')


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()


import time
from urllib import parse
import urllib.request
from multiprocessing import Pool

from bs4 import BeautifulSoup
import re
import PostgresqlDBConnent
import MorphAnal
import json
from PgHelper import PgHelper

# text 정제하기

def clean_text(text):
    # content = text.get_text()
    content = text
    cleaned_text = re.sub('[a-zA-Z]', '', content)
    cleaned_text = re.sub('[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>▶▽♡◀━@\#$%&\\\=\(\'\"ⓒ(\n)(\t)]', '', cleaned_text)
    cleaned_text = cleaned_text.replace("🇲\u200b🇮\u200b🇱\u200b🇱\u200b🇮\u200b🇪\u200b", "")
    cleaned_text = cleaned_text.replace("오류를 우회하기 위한 함수 추가 ", "")
    cleaned_text = cleaned_text.replace("동영상 뉴스 오류를 우회하기 위한 함수 추가 ", "")
    cleaned_text = cleaned_text.replace("무단전재 및 재배포 금지", "")
    cleaned_text = cleaned_text.strip('\n')
    return cleaned_text

def blog_crawling(crawling_url):
    print(crawling_url[0])
    # print(crawling_url[1])
    try:
        req = urllib.request.urlopen(crawling_url[0])
        res = req.read()
        soup = BeautifulSoup(res, 'html.parser')

        try:
            # keywords = soup.findAll('div', {'class': 'se_textView'})
            # keywords = soup.findAll('div', {'class': 'se-main-container'})
            # keywords = soup.findAll('div', {'class': 'se-module se-module-text'})
            keywords = soup.find_all('span', {'class': 'se-fs- se-ff- '})
            # print(len(keywords))
            # print(1)
            if len(keywords) > 0:
                for a in keywords:
                    text = clean_text(a.get_text())
                    if len(text) > 0:
                        # print('\t' + text)
                        PostgresqlDBConnent.update_raw_data(text, crawling_url[1])
                    # print(keywords.get_text(strip=True))
                    # MorphAnal.add_data(keywords.get_text(strip=True))
            else:
                keywords = soup.find_all('div', {'id': 'postViewArea'})
                # print('2')
                if len(keywords) > 0:
                    for a in keywords:
                        # text = a.get_text()
                        text = clean_text(a.get_text())
                        if len(text) > 0:
                            # print(text)
                            PostgresqlDBConnent.update_raw_data(text, crawling_url[1])
                else:
                    keywords = soup.findAll('div', {'class': 'se_textView'})
                    # print('3')
                    if len(keywords) > 0:
                        # print(keywords.get_text(strip=True))
                        # print(keywords)
                        for a in keywords:
                            # text = a.get_text()
                            text = clean_text(a.get_text())
                            if len(text) > 0:
                                # print('\t' + text)
                                PostgresqlDBConnent.update_raw_data(text, crawling_url[1])
                    else:
                        print('xxxxxxxxxxxxxxxx')
        except Exception as err:
            print('??????', err)

    except urllib.error.HTTPError as e:
        # err = e.read()
        code = e.getcode()
        print(code)

def search_keyword(pghelper: object, keyword: object) -> object:
    client_id = "7o0ZnBCQzfLEleVV5JDM"
    client_secret = "uHZM6Mq0hE"
    start = 1
    display = 100
    enc_text = parse.quote(keyword)

    while start <= 1000:
        url = "https://openapi.naver.com/v1/search/blog?query=" + enc_text + "&display=" \
                      + str(display) + "&start=" + str(start)  # json 결과
        # url = "https://openapi.naver.com/v1/search/blog.xml?query=" + encText # xml 결과
        request = urllib.request.Request(url)
        request.add_header("X-Naver-Client-Id", client_id)
        request.add_header("X-Naver-Client-Secret", client_secret)
        response = urllib.request.urlopen(request)
        resp_code = response.getcode()

        if resp_code == 200:
            response_body = response.read()
            # print(response_body.decode('utf-8'))
            jsonobject = json.loads(response_body)
            jsonarray = jsonobject.get("items")
            print("\tTotal : ", jsonobject.get("total"))
            print("\tStart : ", jsonobject.get("start"))
            if jsonobject.get("total") == 0:
                break
            else:
                # PostgresqlDBConnent.keyword_add_db(jsonarray)
                if len(jsonarray) > 0:
                    pghelper.keyword_add_db(jsonarray)
                else:
                    print("xxxxx")
        else:
            print("Error Code:" + str(resp_code))

        start = start + display

if __name__ == '__main__':
    print("Search_Keyword call")
    # Search_Keyword("강원도")
    bloglink = []
    rawdata = []
    tour_api_keyword = []

    start_time = time.time()
    # kangwong = ['춘천 맛집', '원주 맛집', '강릉 맛집', '동해 맛집', '태백 맛집', '속초 맛집', '삼척 맛집', '홍천 맛집', '횡성 맛집',
    #             '영월 맛집', '평창 맛집', '정선 맛집', '철원 맛집', '화천 맛집', '양구 맛집', '인제 맛집', '고성 맛집', '양양 맛집']

    # pghelper = PgHelper()

    # pghelper.get_keyword_data(tour_api_keyword)
    # # print(tour_api_keyword.index('[강릉 바우길 11구간] 신사임당길'))
    # for i in tour_api_keyword[3214:]:
    #     print(i)
    #     Search_Keyword(pghelper, i)

    # for i in kangwong:
    #     print(i)
    #     Search_Keyword(pghelper, i)

    # pghelper.get_blog_link(bloglink)
    # pghelper.CloseDB()
    # url1 ="https://blog.naver.com/PostView.nhn?blogId=soapdiary&logNo=222087156559"
    # url2 ="https://blog.naver.com/PostView.nhn?blogId=soapdiary&logNo=222087156559"
    # bloglink.append([url1, url2])
    # for i in bloglink:
    #     Blog_crawling(i)

    # pool = Pool(processes=8)  # 4개의 프로세스를 사용합니다.
    # pool.map(Blog_crawling, bloglink)  # get_contetn 함수를 넣어줍시다.

    print("--- %s seconds ---" % (time.time() - start_time))
    PostgresqlDBConnent.get_raw_data(rawdata)
    #
    # print(rawdata)
    # print(len(rawdata))
    MorphAnal.process_data(rawdata)
    print("--- %s seconds ---" % (time.time() - start_time))
    # PostgresqlDBConnent.update_raw_data('111asdfasdfasdf',
    # 'https://blog.naver.com/eye4y?Redirect=Log&logNo=222091294214')
    # PostgresqlDBConnent.blog_add_db()
    # Blog_crawing("https://blog.naver.com/PostView.nhn?blogId=d2house&1364")
    # Blog_crawing("https://withbeatles.tistory.com/3544")
    print("Search_Keyword return")


from manager import Manager
from robot import Robot
def atTheWorkStaion(worker):
	print("At the Work Station")
	worker.startWork()
	worker.stopWork()

def atTheCafeteria (worker):
	print("At the Cafeteria")
	worker.startEating()
	worker.stopEating()
manager=Manager()
robot=Robot()
atTheWorkStaion(manager)
atTheWorkStaion(robot)

atTheCafeteria(manager)
atTheCafeteria(robot)


#coding:utf8

import json

from flask import Blueprint, render_template
from flask import request,session,redirect

import models

mod = Blueprint('project', __name__,)
# -----------------------------------
#   项目
# -----------------------------------
@mod.route('/project/')
def project_index():
    # 获取项目组进行渲染
    project_group_list = models.get_project_group_list()
    return render_template('/project/project.html', project_group_list=project_group_list)

'''
ajax 获取项目列表
'''

@mod.route('/project/list/')
def project_list():
    if session.get('user') is None: return redirect('/')
    projects = models.get_projects()
    return json.dumps({'data': projects})

'''
项目详情页
'''

@mod.route('/project/details/')
def project_details():
    if session.get('user') is None: return redirect('/')
    project_id = request.args.get('id', '')
    # 获取项目详情
    project_details = models.get_project_id(project_id)
    # 获取项目组名与项目组ID 关联
    project_group_name_id = models.project_group_name(project_id)
    return render_template('/project/project_details.html', value=project_details,
                           project_group_name_id=project_group_name_id)

'''
项目创建
'''

@mod.route('/project/create/', methods=['POST'])
def project_create():
    # 获取form提交数据
    project_name = request.form.get('project_name', '')
    project_details = request.form.get('project_details', '')
    project_env = request.form.get('project_env', '')
    project_group_name_id_list_s = request.values.getlist('project_group_name')

    # 项目保存前检查
    status = models.validata_project_create(project_name, project_details)
    if not status:
        return json.dumps({'code': 400, 'result': '项目创建失败'})

    # 保存项目
    status, result = models.project_save(project_name, project_details, project_env)
    if not status:
        return json.dumps({'code': 400, 'result': result})

    '''
    保存项目和项目组关联关系：
        1、新项目保存 -> 通过项目名获取其ID(项目ID)
        2、将项目ID与项目组ID存入"project_id_project_group_id"关系表中
    '''
    status, result = models.project_id_project_group_id_correlation(project_group_name_id_list_s, project_name)
    if not status:
        return json.dumps({'code': 400, 'result': result})
    return json.dumps({'code': 200, 'result': result})

'''
项目编辑
'''

@mod.route('/project/view/')
def project_view():
    project_id = request.args.get('id', 0)
    project_ = models.get_project_id(project_id)
    # tmp = models.get_project_id_project_group_id(project_id)
    return json.dumps(project_)

'''
项目 编辑 -->保存
'''

@mod.route('/project/update/', methods=['POST'])
def project_update():
    project_id = request.form.get('id', 0)
    project_name = request.form.get('project_name', 0)
    project_details = request.form.get('project_details', 0)
    project_env = request.form.get('project_env', 0)
    project_status = request.form.get('project_status', 0)
    # 获取项目组ID
    project_group_name_id = request.values.getlist('project_group_name')

    # 检查编辑信息
    # print project_id, project_name, project_details, project_env,project_status

    status, message = models.validata_project_update(project_id, project_name, project_details, project_env,
                                                     project_status)
    if status:
        status, message = models.project_update(project_id, project_name, project_details, project_env, project_status,
                                                project_group_name_id)
        if status:
            return json.dumps({'code': 200, 'error': message})
    return json.dumps({'code': 400, 'error': message})

'''
项目删除
'''

@mod.route('/project/delete/')
def project_delete():
    project_id = request.args.get('id', 0)
    status, message = models.project_delete(project_id)
    if status:
        return json.dumps({'code': 200})
    return json.dumps({'code': 400})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 26 00:04:47 2018

@author: peter
"""

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.models import Model

from keras.layers  import Masking
import random

import math
from datetime import datetime, timedelta
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
#from MAPE import mean_absolute_percentage_error
from sklearn.metrics import mean_absolute_error

PATH = '/home/peter/Desktop/Diplomovka/'

features = 5

pad = 581

data = pd.read_csv(f'{PATH}'+'call.csv')
data = data[['Date','Expiry Date','Instrument Symbol','Strike Price', 'CTB3','Close','t','std','Last Price']]
data = data.iloc[100:102]
data = data.dropna(0)
scaler = MinMaxScaler(feature_range=(0, 1))
date = data[['Date','Instrument Symbol','Expiry Date']]
date = pd.DataFrame(date)
date.reset_index(inplace=True, drop=True)
dataset = data.drop(['Date','Instrument Symbol','Expiry Date'], axis = 1)
dataset = dataset.astype('float32')
dataset.shape
dataset = scaler.fit_transform(dataset)
dataset = pd.DataFrame(dataset)
dataset = pd.concat([date, dataset],axis=1, ignore_index=True)
dataset.columns = ['Date','Instrument Symbol','Expiry Date','Strike Price', 'CTB3','Close','t','std','Last Price']

train_size = int(len(dataset) * 0.90)
valid_size = int(len(dataset) * 0.05)
test_size = len(dataset) - train_size - valid_size
trainX, validX, testX = dataset.iloc[0:train_size,:], dataset.iloc[train_size:train_size+valid_size,:], dataset.iloc[train_size+valid_size:len(dataset),:]
#testX, testY = data_dist_max_pad_2_gen_for_test(dataset, testX)

model = Sequential()
model.add(Masking(mask_value=0., input_shape=(pad, 5)))
model.add(LSTM(100))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

def generator_day(dataset, part, batch_size):
 # Create empty arrays to contain batch of features and labels#
 while True:
    for i in range(batch_size):
      # choose random index in features
      index= part.sample(1, replace=False, weights=None, random_state=None, axis=0)
      dataX, dataY = data_dist_day(index, dataset, part)
      dataX = np.reshape(dataX, (len(dataX),pad,5))
      dataY = np.reshape(dataY,(len(dataY),1))

    yield dataX , dataY

def generator_pred_day(dataset, part, batch_size):
 # Create empty arrays to contain batch of features and labels#
 while True:
    for i in range(batch_size):
      # choose random index in features
      index= part.sample(1, replace=False, weights=None, random_state=None, axis=0)
      dataX, dataY = data_dist_day(index, dataset, part)
      dataX = np.reshape(dataX, (len(dataX),pad,5))
      dataY = np.reshape(dataY,(len(dataY),1))

    yield dataX

def data_dist_day(row, dataset, part):
    dataX, dataY = [], []
    dataset = pd.DataFrame(dataset)
    part = pd.DataFrame(part)
    dat = dataset[dataset['Date'] == row.iloc[0]['Date']]
    dat = dat.sort_values(['Strike Price'])
    result = np.zeros((pad,5))
    X = np.array(dat.iloc[:1,3:dat.shape[1]-1])
    result[:X.shape[0],:X.shape[1]] = X
    dataX.append(result)
    dataY.append(np.array(row.iloc[0]['Last Price']))
    print(dataX, dataY)
    return dataX, dataY

neurons = [5, 15, 50, 100, 150, 300]
scores = {}
scor = []

model = Sequential()
model.add(Masking(mask_value=0., input_shape=(pad, 5)))
model.add(LSTM(15))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit_generator(generator_day(dataset, trainX, 128), steps_per_epoch=int(trainX.shape[0]/128), verbose=1, callbacks=None, validation_data=None, validation_steps=None, class_weight=None, max_queue_size=10, workers=1, use_multiprocessing=False, shuffle=True, initial_epoch=0, epochs=1)
yhat = model.predict_generator(generator_pred_day(dataset, testX, 1), steps=testX.shape[0], max_queue_size=10, workers=1, use_multiprocessing=False)
yhat = pd.DataFrame(yhat)
result = pd.DataFrame(testX.iloc[:,:-1])
result = pd.DataFrame(np.zeros((testX.shape[0], 5)))
inv_yhat = pd.DataFrame(np.concatenate((result, yhat), axis=1))
inv_yhat = scaler.inverse_transform(inv_yhat)
pred = pd.DataFrame(inv_yhat[:,-1])
testY = pd.DataFrame(testX.iloc[:,-1])
inv_y = np.concatenate((result, testY), axis=1)
inv_y = scaler.inverse_transform(inv_y)
Y = pd.DataFrame(inv_y[:,-1])
# calculate metrics
rmse = np.sqrt(mean_squared_error(Y, pred))
print('Test RMSE: %.3f' % rmse)
mae = mean_absolute_error(Y, pred)
print('Test MAE: %.3f' % mae)


# -*- 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


from sklearn import __version__ as sklearn_version
from distutils.version import LooseVersion
from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing   import StandardScaler
from sklearn.linear_model    import Perceptron
from sklearn.metrics         import accuracy_score
from matplotlib.colors       import ListedColormap
import matplotlib.pyplot as plt
from sklearn.linear_model    import LogisticRegression
from sklearn.svm             import SVC
from sklearn.linear_model    import SGDClassifier
from sklearn.tree            import DecisionTreeClassifier
from pydotplus               import graph_from_dot_data
from sklearn.tree            import export_graphviz
from sklearn.ensemble        import RandomForestClassifier
from sklearn.neighbors       import KNeighborsClassifier

def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):

		# setup marker generator and color map
		markers = ('s', 'x', 'o', '^', 'v')
		colors  = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
		cmap    = ListedColormap(colors[:len(np.unique(y))])

		# plot the decision surface
		x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
		x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
		xx1, xx2       = np.meshgrid(np.arange(x1_min, x1_max, resolution),
													 np.arange(x2_min, x2_max, resolution))
		Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
		Z = Z.reshape(xx1.shape)
		plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
		plt.xlim(xx1.min(), xx1.max())
		plt.ylim(xx2.min(), xx2.max())

		# plot the scatter of the actual datas
		for idx, cl in enumerate(np.unique(y)):
				plt.scatter(x=X[y == cl, 0],
										y=X[y == cl, 1],
										alpha=0.8,
										c=colors[idx],
										marker=markers[idx],
										label=cl,
										edgecolor='black')

		# highlight test samples by circle
		if test_idx:
				# plot all samples
				X_test, y_test = X[test_idx, :], y[test_idx]

				plt.scatter(X_test[:, 0],
										X_test[:, 1],
										c='',
										edgecolor='black',
										alpha=1.0,
										linewidth=1,
										marker='o',
										s=100,
										label='test set')

def sigmoid(z):
	return 1.0 / (1.0 + np.exp(-z))

# Logistic Regression Classifier using gradient descent.
class LogisticRegressionGD(object):
		def __init__(self, eta=0.05, n_iter=100, random_state=1):
				self.eta          = eta
				self.n_iter       = n_iter
				self.random_state = random_state

		def fit(self, X, y):
				rgen       = np.random.RandomState(self.random_state)
				self.w_    = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
				self.cost_ = []

				for i in range(self.n_iter):
						net_input = self.net_input(X)
						output    = self.activation(net_input)
						errors    = (y - output)
						self.w_[1:] += self.eta * X.T.dot(errors)
						self.w_[0]  += self.eta * errors.sum()

						# note that we compute the logistic `cost` now
						# instead of the sum of squared errors cost
						cost = -y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output)))
						self.cost_.append(cost)
				return self

		def net_input(self, X):
				"""Calculate net input"""
				return np.dot(X, self.w_[1:]) + self.w_[0]

		def activation(self, z):
				"""Compute logistic sigmoid activation"""
				return 1. / (1. + np.exp(-np.clip(z, -250, 250)))

		def predict(self, X):
				"""Return class label after unit step"""
				return np.where(self.net_input(X) >= 0.0, 1, 0)
				# equivalent to:
				# return np.where(self.activation(self.net_input(X)) >= 0.5, 1, 0)

def gini(p):
		return p * (1 - p) + (1 - p) * (1 - (1 - p))

def entropy(p):
		return - p * np.log2(p) - (1 - p) * np.log2((1 - p))

def error(p):
		return 1 - np.max([p, 1 - p])

iris = datasets.load_iris()
X    = iris.data[:, [2,3]]
y    = iris.target
#-------------------------------------------------------------------------------------------
# split training data and test data
# 全体の30%をテストデータにする。
X_train, X_test, y_train, y_test = train_test_split(
	X, y, test_size=0.3, random_state=1, stratify=y)
#-------------------------------------------------------------------------------------------
# 特徴量のスケーリング
sc = StandardScaler()
sc.fit(X_train) # 平均値と標準偏差の計算
X_train_std = sc.transform(X_train)
X_test_std  = sc.transform(X_test)
#-------------------------------------------------------------------------------------------
# パーセプトロンで学習
ppn = Perceptron(max_iter= 40, eta0=0.1, random_state=0)
ppn.fit(X_train_std, y_train)
#-------------------------------------------------------------------------------------------
# 予測
y_pred = ppn.predict(X_test_std)
print( (y_test!=y_pred).sum() )
#-------------------------------------------------------------------------------------------
# 性能指標の計算
print(accuracy_score(y_test, y_pred))
print(ppn.score(X_test_std, y_test))
#-------------------------------------------------------------------------------------------
# 決定領域のプロット
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined     = np.hstack((y_train, y_test))
plot_decision_regions(X_combined_std, y_combined, classifier=ppn, test_idx = range(105,150))
plt.tight_layout()
plt.close()
#-------------------------------------------------------------------------------------------
# 決定領域のプロット
lr = LogisticRegression(C=100.0, random_state=1)
lr.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier=lr, test_idx=range(105, 150))
plt.legend()
plt.close()
#-------------------------------------------------------------------------------------------
# 線形SVM
svm = SVC(kernel="linear", C=1.0, random_state=1)
svm.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier=svm,test_idx=range(105, 150))
plt.legend()
plt.close()
#-------------------------------------------------------------------------------------------
# XORテストデータpreparation
np.random.seed(1)
X_xor = np.random.randn(200,2)
y_xor = np.logical_xor(X_xor[:,0]>0, X_xor[:,1]>0)
y_xor = np.where(y_xor, 1, -1)
plt.scatter( X_xor[y_xor==1,  0], X_xor[y_xor==1, 1],  c="b", marker="x", label="1")
plt.scatter( X_xor[y_xor==-1, 0], X_xor[y_xor==-1, 1], c="r", marker="s", label="-1")
plt.tight_layout()
plt.close()
#-------------------------------------------------------------------------------------------
# カーネルSVM
svm = SVC(kernel="rbf", C=10.0, gamma=0.10, random_state=1)
svm.fit(X_xor, y_xor)
plot_decision_regions(X_xor, y_xor, classifier=svm)
plt.legend()
#plt.show()
plt.close()
#-------------------------------------------------------------------------------------------
# 条件
x = np.arange(0.0, 1.0, 0.01)

ent    = [entropy(p) if p != 0 else None for p in x]
sc_ent = [e * 0.5 if e else None for e in ent]
err    = [error(i) for i in x]

fig = plt.figure()
ax  = plt.subplot(111)
for i, lab, ls, c, in zip([ent, sc_ent, gini(x), err],
													['Entropy', 'Entropy (scaled)',
													 'Gini Impurity', 'Misclassification Error'],
													['-', '-', '--', '-.'],
													['black', 'lightgray', 'red', 'green', 'cyan']):
		line = ax.plot(x, i, label=lab, linestyle=ls, lw=2, color=c)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15),
					ncol=5, fancybox=True, shadow=False)

ax.axhline(y=0.5, linewidth=1, color='k', linestyle='--')
ax.axhline(y=1.0, linewidth=1, color='k', linestyle='--')
plt.ylim([0, 1.1])
plt.xlabel('p(i=1)')
plt.ylabel('Impurity Index')
plt.close()
#-------------------------------------------------------------------------------------------
# 決定木
tree = DecisionTreeClassifier(criterion='gini',
															max_depth=4,
															random_state=1)
tree.fit(X_train, y_train)

X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X_combined, y_combined,
											classifier=tree, test_idx=range(105, 150))

plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.close()
#-------------------------------------------------------------------------------------------
# 決定木フローの図示
from PIL import Image
dot_data = export_graphviz(tree,
													 filled=True,
													 rounded=True,
													 class_names=['Setosa',
																				'Versicolor',
																				'Virginica'],
													 feature_names=['petal length',
																					'petal width'],
													 out_file=None)
graph = graph_from_dot_data(dot_data)
#Image(graph.create_png())
graph.write_png('tree.png')
#-------------------------------------------------------------------------------------------
# ランダムフォレスト
forest = RandomForestClassifier(criterion='gini',
																n_estimators=25,
																random_state=1,
																n_jobs=2)
forest.fit(X_train, y_train)

plot_decision_regions(X_combined, y_combined,
											classifier=forest, test_idx=range(105, 150))

plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.tight_layout()
#plt.savefig('images/03_22.png', dpi=300)
plt.show()


from django.test import TestCase
from apps.logistics.models import *
from apps.business.models import Department

# Create your tests here.
class CoffeStockAssertionErrorTestCase(TestCase):
    def setUp(self):
        Department.objects.create(name='planta_baja')
        CoffeType.objects.create(name='naranjada', current_price='0.15')
        # Ya se ha creado un CoffeStock de ambos.

    def test_coffe_stock_assertion_error(self):
        department = Department.objects.get(name='planta_baja')
        coffe_type = CoffeType.objects.get(name='naranjada')
        with self.assertRaises(AssertionError) as e:
            CoffeStock.objects.create(department=department, coffe_type=coffe_type)
        self.assertTrue('Ya existe un CoffeStock para ese par Deparament/CoffeType' in str(e.exception))

class CoffeTypeSaveTestCase(TestCase):
    def setUp(self):
        Department.objects.create(name='terraza')
        Department.objects.create(name='planta_baja')
        CoffeType.objects.create(name='cappuccino', current_price=1.5) # Aqui me tiene que crear los dos CoffeStocks.

    def test_coffe_stocks_are_created(self):
        coffe_type = CoffeType.objects.get(name='cappuccino')
        self.assertEqual(CoffeStock.objects.filter(coffe_type=coffe_type, department=Department.objects.get(name='terraza')).count(), 1)
        self.assertEqual(CoffeStock.objects.filter(coffe_type=coffe_type, department=Department.objects.get(name='planta_baja')).count(), 1)

class CoffeOrderAreCreatedWithStock0(TestCase):
    def setUp(self):
        CoffeType.objects.create(name='naranjada', current_price=1.2)
        Department.objects.create(name='terraza')
        #Aqui ya me ha creado un CoffeStock.

    def test_when_stock_is_created(self):
        coffe_stock = CoffeStock.objects.get(department=Department.objects.get(name='terraza'))
        self.assertTrue(CoffeOrder.objects.filter(coffe_stock=coffe_stock, unit_price=1.2, units=100, status='Pending').count()==1)

class CoffeOrderAreCreatedWithStock2(TestCase):
    def setUp(self):
        CoffeType.objects.create(name='naranjada', current_price=1.2)
        Department.objects.create(name='terraza')
        #Aqui ya me ha creado un CoffeStock.
        coffe_stock = CoffeStock.objects.get(department=Department.objects.get(name='terraza'))
        coffe_stock.current_units = 2
        coffe_stock.save()

    def test_when_stock_is_decreased(self):
        # Solo tenemos que tener una orden.
        coffe_stock = CoffeStock.objects.get(department=Department.objects.get(name='terraza'))
        self.assertTrue(CoffeOrder.objects.filter(coffe_stock=coffe_stock, unit_price=1.2, units=100, status='Pending').count()==1)

        # Al disminuir  el stock a 0, se tiene que crear otra orden.
        coffe_stock.current_units = 0
        coffe_stock.save()
        self.assertTrue(CoffeOrder.objects.filter(coffe_stock=coffe_stock, unit_price=1.2, units=100, status='Pending').count()==2)


"""BubbleSorting Algorithm"""

import numpy as np
import matplotlib.pyplot as  plt
import random as rn
import time as tm

def bubbleSortAscendingOrder(group):
    isSorted = False
    length = len(group)
    indexes = range(0,length-1)
    swaps = 0
    comparisons = 0

    while not isSorted:
        isSorted = True

        for i in indexes:
            temp = group[i]
            comparisons = comparisons +1

            if temp > group[i+1]:
                group[i] = group[i+1]
                group[i+1] = temp
                swaps = swaps +1
                isSorted = False

    return [group,comparisons,swaps]

def cuadratic(x):
    return x**2

size = list(range(1,1002,50))

rawResultsSwaps = []
resultsSwaps = []
rawResultsComps = []
resultsComps = []

model = []
raw = 0
"""startTime = tm.time()"""

for quant in size:
    model.append(cuadratic(quant))
    group = list(range(quant))

    rawResultsSwaps.append([])
    rawResultsComps.append([])
    repeats = 0
    while repeats < 50:
        rn.shuffle(group)
        info = bubbleSortAscendingOrder(group)

        comps = info[1]
        swaps = info[2]
        rawResultsComps[raw].append(comps)
        rawResultsSwaps[raw].append(swaps)
        repeats = repeats +1

    resultsSwaps.append(np.average(rawResultsSwaps[raw]))
    resultsComps.append(np.average(rawResultsComps[raw]))
    raw = raw +1

"""elapsedTime = tm.time() - startTime"""
"""print(tm.strftime("%H:%M:%S", tm.gmtime(elapsedTime)))"""

plt.figure()
plt.grid()
plt.plot(size,model,'b--',linewidth=2,label='Model of complexity')
plt.plot(size,resultsSwaps,'*g',markersize=4,label='Number of swaps')
plt.plot(size,resultsComps,'or',markersize=4,label='Number of comparisons')
plt.legend()
plt.xlim(0,1100)
plt.xlabel("n (number of distinct elements)")


import sys
import os
import experiments.registration.split_tools as st
from dipy.fixes import argparse as arg

parser = arg.ArgumentParser(
    description=""
)

parser.add_argument(
    'mod1', action = 'store', metavar = 'mod1',
    help = '''Modality identifier. It will be added as suffix to the filename
    of each input file, e.g. "", "_t2", "_pd" ''')

parser.add_argument(
    'mod2', action = 'store', metavar = 'mod2',
    help = '''Modality identifier. It will be added as suffix to the filename
    of each input file, e.g. "", "_t2", "_pd" ''')

parser.add_argument(
    'action', action = 'store', metavar = 'action',
    help = '''Any of ["s", "s2", "c", "o", "u"] to split all pairs, split
    corresponding pairs, clean, collect or submit, respectively''')

parser.add_argument(
    'names_from', action = 'store', metavar = 'names_from',
    help = '''Text file containing the input file names''')

parser.add_argument(
    '-nt', '--names_to', action = 'store', metavar = 'names_to',
    help = '''Text file containing the input file names''')

if __name__=="__main__":
    req_dir = os.path.dirname(os.path.realpath(__file__))
    required_files = [req_dir+'/dipyreg.py',
                      req_dir+'/job_syn_cc.sh']

    params = parser.parse_args()
    params.mod1
    st.split_script(["", params.action, params.names_from, params.names_to], required_files, 'multi', params.mod1, params.mod2)


import html5lib

from urlparse import urlparse, urljoin

from django.conf import settings
from django.utils.encoding import force_unicode

from django_extensions.db.fields import json
from html5lib import treebuilders
from html5lib.serializer import htmlserializer

BUTTER_ASSETS = {
    'butter.js': 'src/',
    'butter.ui.css': 'css/',
    }

def get_butter_library(src):
    """Determines if the element linked is a valid ``butter`` library"""
    if not src:
        return None
    # Is it already a valid URL?
    url = urlparse(src)
    if url.netloc:
        return None
    # Is this part of butter?
    filename = src.split('/')[-1]
    if filename in BUTTER_ASSETS:
        return settings.STATIC_URL + BUTTER_ASSETS[filename] + filename
    return None

def _serialize_stream(document_tree):
    walker = html5lib.treewalkers.getTreeWalker('lxml')
    stream = walker(document_tree)
    serializer = htmlserializer.HTMLSerializer(omit_optional_tags=False,
                                               quote_attr_values=True)
    return unicode(serializer.render(stream))

def prepare_template_stream(stream, base_url):
    """Prepares the stream to be stored in the DB"""
    stream = force_unicode(stream) if stream else u''
    tree = treebuilders.getTreeBuilder('lxml')
    parser = html5lib.HTMLParser(tree=tree, namespaceHTMLElements=False)
    document_tree = parser.parse(stream)
    script_elements = document_tree.xpath('//script[@src]')
    for script in script_elements:
        src = script.get('src')
        butter_library = get_butter_library(src)
        if butter_library:
            script.set('src', butter_library)
    make_links_absolute(document_tree, base_url)
    return _serialize_stream(document_tree)

def remove_default_values(stream, base_url=None):
    data = json.loads(stream)
    for attr in ['baseDir', 'name', 'savedDataUrl']:
        if attr in data:
            del data[attr]
    return json.dumps(data)

URL_ATTRIBUTES = [
    ('a', 'href'),
    ('applet', 'codebase'),
    ('area', 'href'),
    ('blockquote', 'cite'),
    ('body', 'background'),
    ('del', 'cite'),
    ('form', 'action'),
    ('frame', 'longdesc'),
    ('frame', 'src'),
    ('iframe', 'longdesc'),
    ('iframe', 'src'),
    ('head', 'profile'),
    ('img', 'longdesc'),
    ('img', 'src'),
    ('img', 'usemap'),
    ('input', 'src'),
    ('input', 'usemap'),
    ('ins', 'cite'),
    ('link', 'href'),
    ('object', 'classid'),
    ('object', 'codebase'),
    ('object', 'data'),
    ('object', 'usemap'),
    ('q', 'cite'),
    ('script', 'src'),
    ('video', 'src'),
    ]

def make_links_absolute(document_tree, base_url):
    for tag, attr in URL_ATTRIBUTES:
        xpath = '//%s[@%s]' % (tag, attr)
        element_list = document_tree.xpath(xpath)
        for element in element_list:
            url = get_absolute_url(base_url, element.get(attr))
            element.set(attr, url)
    return document_tree

def get_absolute_url(base_url, path):
    url = urlparse(path)
    if url.netloc:
        return path
    return urljoin(base_url, path)


from typing import Union

from hwt.hdl.operator import Operator
from hwt.hdl.operatorDefs import AllOps
from hwt.hdl.types.defs import BIT, INT
from hwt.hdl.value import Value
from hwt.serializer.exceptions import UnsupportedEventOpErr
from hwt.serializer.generic.context import SerializerCtx
from hwt.synthesizer.rtlLevel.rtlSignal import RtlSignal

class VerilogSerializer_ops():
    # http://www.asicguru.com/verilog/tutorial/operators/57/
    opPrecedence = {
        AllOps.RISING_EDGE: 17,
        AllOps.FALLING_EDGE: 17,
        AllOps.TERNARY: 16,
        AllOps.AND: 11,
        AllOps.XOR: 11,
        AllOps.OR: 11,
        AllOps.EQ: 10,
        AllOps.NEQ: 10,
        AllOps.GT: 9,
        AllOps.LT: 9,
        AllOps.GE: 9,
        AllOps.LE: 9,
        AllOps.CONCAT: 5,
        AllOps.ADD: 7,
        AllOps.SUB: 7,
        AllOps.DIV: 6,
        AllOps.NEG: 5,
        AllOps.MUL: 3,
        AllOps.NOT: 3,
        AllOps.DOWNTO: 2,
        AllOps.TO: 2,
        AllOps.CALL: 2,
        AllOps.INDEX: 1,
        # AllOps.SHIFTL:8,
        # AllOps.SHIFTR:8,
        AllOps.BitsAsSigned: 1,
        AllOps.BitsAsUnsigned: 1,
        AllOps.BitsAsVec: 1,
    }
    _unaryOps = {
        AllOps.NOT: "~%s",
        AllOps.BitsAsSigned: "$signed(%s)",
        AllOps.BitsAsUnsigned: "$unsigned(%s)",
        AllOps.BitsAsVec: "%s",
    }

    _binOps = {
        AllOps.AND: '%s & %s',
        AllOps.OR: '%s | %s',
        AllOps.XOR: '%s ^ %s',
        AllOps.CONCAT: "{%s, %s}",
        AllOps.DIV: '%s / %s',
        AllOps.DOWNTO: '%s:%s',
        AllOps.TO: '%s:%s',
        AllOps.EQ: '%s == %s',
        AllOps.GT: '%s > %s',
        AllOps.GE: '%s >= %s',
        AllOps.LE: '%s <= %s',
        AllOps.LT: '%s < %s',
        AllOps.SUB: '%s - %s',
        AllOps.MUL: '%s * %s',
        AllOps.NEQ: '%s != %s',
        AllOps.ADD: '%s + %s',
        AllOps.POW: '%s ** %s',
    }

    @classmethod
    def _operandIsAnotherOperand(cls, operand):
        if isinstance(operand, RtlSignal) and operand.hidden\
                and isinstance(operand.origin, Operator):
            return True

    @classmethod
    def _operand(cls, operand: Union[RtlSignal, Value], i: int,
                 operator: Operator,
                 expr_requires_parenthesis: bool,
                 cancel_parenthesis: bool,
                 ctx: SerializerCtx):

        # [TODO] if operand is concatenation and parent operator
        #        is not concatenation operand should be extracted
        #        as tmp variable
        #        * maybe flatten the concatenations
        if operator.operator != AllOps.CONCAT\
                and cls._operandIsAnotherOperand(operand)\
                and operand.origin.operator == AllOps.CONCAT:
            tmpVar = ctx.createTmpVarFn("tmp_concat_", operand._dtype)
            tmpVar.def_val = operand
            # Assignment(tmpVar, operand, virtual_only=True)
            operand = tmpVar

        oper = operator.operator
        width = None
        if oper not in [AllOps.BitsAsUnsigned, AllOps.BitsAsVec,
                        AllOps.BitsAsSigned] and\
                oper is not AllOps.INDEX and\
                operand._dtype == INT and\
                operator.result is not None and\
                not operator.result._dtype == INT:
            # has to lock width
            for o in operator.operands:
                try:
                    bl = o._dtype.bit_length
                except AttributeError:
                    bl = None
                if bl is not None:
                    width = bl()
                    break

            assert width is not None, (operator, operand)

        s = super()._operand(operand, i, operator,
                             width is not None or expr_requires_parenthesis,
                             width is None and cancel_parenthesis, ctx)
        if width is not None:
            return "%d'%s" % (width, s)
        else:
            return s

    @classmethod
    def Operator(cls, op: Operator, ctx):
        ops = op.operands
        o = op.operator

        op_str = cls._unaryOps.get(o, None)
        if op_str is not None:
            cancel_parenthesis = op_str.endswith(")")
            return op_str % (cls._operand(ops[0], 0, op, False, cancel_parenthesis, ctx))

        op_str = cls._binOps.get(o, None)
        if op_str is not None:
            return cls._bin_op(op, op_str, ctx, cancel_parenthesis=o == AllOps.CONCAT)
        if o == AllOps.CALL:
            return "%s(%s)" % (
                cls.FunctionContainer(ops[0]),
                # operand i does not matter as they are all in ()
                ", ".join(map(lambda _op: cls._operand(_op, 1, op, False, True, ctx), ops[1:])))
        elif o == AllOps.INDEX:
            return cls._operator_index(op, ctx)
        elif o == AllOps.TERNARY:
            zero, one = BIT.from_py(0), BIT.from_py(1)
            if ops[1] == one and ops[2] == zero:
                # ignore redundant x ? 1 : 0
                return cls.condAsHdl([ops[0]], True, ctx)
            else:
                op0 = cls.condAsHdl([ops[0]], True, ctx)
                op1 = cls._operand(ops[1], op, False, False, ctx)
                op2 = cls._operand(ops[2], op, False, False, ctx)
                return "%s ? %s : %s" % (op0, op1, op2)
        elif o == AllOps.RISING_EDGE or o == AllOps.FALLING_EDGE:
            raise UnsupportedEventOpErr()
        elif o in [AllOps.BitsAsUnsigned, AllOps.BitsAsVec]:
            op0, = ops
            do_cast = bool(op0._dtype.signed)
            op_str = cls._operand(op0, op, False, do_cast, ctx)
            if do_cast:
                return "$unsigned(%s)" % op_str
            else:
                return op_str
        else:
            raise NotImplementedError(
                "Do not know how to convert expression with operator %s to verilog" % (o))


import setuptools

VERSION = "4.2.0"

setuptools.setup(
    name='pytest_mproc',
    author='John Rusnak',
    author_email='jrusnak69@gmail.com',
    version=VERSION,
    description="low-startup-overhead, scalable, distributed-testing pytest plugin",
    package_dir={'': 'src'},
    packages=setuptools.find_packages('src'),
    entry_points={
       "pytest11": ["name_of_plugin = pytest_mproc.plugin"],
    },
    classifiers=["Framework :: Pytest",
                 "Development Status :: 4 - Beta",
                 "License :: OSI Approved :: BSD License"],
    license='BSD 2-CLAUSE',
    keywrds='pytest distributed multiprocessing',
    url='https://github.com/nak/pytest_mproc',
    download_url="https://github.com/nak/pytest_mproc/dist/%s" % VERSION,
    install_requires=[
        'pytest',
        'pytest-cov'
    ]
)


from app.services.spotify_service import SpotifyService
import app.data.models as models
import datetime
import app.data.mongo_functions as mongo_utils

def save_or_update_user_data(access_token, refresh_token):
    spotify_service = SpotifyService(access_token, refresh_token)
    user_spotify_data = spotify_service.get_user_spotify_data()
    if user_spotify_data is not None and "email" not in user_spotify_data:
        return None
    user = mongo_utils.find_user_by_email(user_spotify_data["email"])
    if not user:
        user_top_artists = spotify_service.get_top_artists()
        user_data = models.user_from_spotify_data(user_spotify_data, user_top_artists)
        return mongo_utils.save_user_data(user_data)
    else:
        # if last updated < 2 days
        time_difference = datetime.datetime.utcnow() - user.last_update
        # update every 2 days
        if time_difference.days > 2:
            user_top_artists = spotify_service.get_top_artists()
            user_data = models.user_from_spotify_data(user_spotify_data, user_top_artists)
            return mongo_utils.save_user_data(user_data)
        else:
            return user

def update_user_location(user, location):
    return mongo_utils.update_user_location(user, location)

def get_near_users(user):
    user_db = mongo_utils.find_user_by_email(user.email)
    return [u.name for u in mongo_utils.find_users_in_distance_for_user(user_db)]

def login_user():
    pass


from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from tictactoeapp import views

urlpatterns = [
    url(r'^userprofile_update/$', views.UserProfileView.as_view()),
    url(r'^check_user/$', views.CheckLoginView.as_view()),
    url(r'^logout/$', views.LogoutView.as_view()),
    url(r'^new/$', views.new_room, name='new_room'),
    url(r'^online_user/$', views.online_user, name='online'),
    url(r'^get_online_user/$', views.get_online_user, name='online_user_list'),
    url(r'^game/$', views.GameView.as_view(), name='game'),
    url(r'^available_room/$', views.RoomView.as_view(), name='available_room'),
    url(r'^extendfb/$', views.extendFbToken, name='fbtoken'),
    url(r'^rank/$', views.rank, name='rank'),
    url(r'^imageedit/$', views.imageEdit, name='imageedit'),
    url(r'^imageframe/$', views.imageMerge, name='imagemerge'),
]

urlpatterns = format_suffix_patterns(urlpatterns)