#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 14 12:10:33 2020

@author: Zhi X. Chen, Cambridge University, zc252@cam.ac.uk
"""
# %%
from scipy.io import loadmat
import numpy as np
import time
import matplotlib.pyplot as plt
from math import sqrt
from scipy.stats import beta
import os

import main_ANN
import func

newDirName = "./model_zc"
if not os.path.exists(newDirName):
    os.mkdir(newDirName)

start_time = time.time()
# copula code not included, contact author if interested
switch_copula = 0
# switch for four- (0) or five-input (1) training
switch_cov = 1

## GLOBAL VARIABLES
#number of pdf bins in c space
N_cBins = 32
#number of pdf bins in Z space
N_ZBins = 36
# bin edges 2D
edges = []
# bin centres 2D
centres = []
# bin areas 2D
binAreas = np.array((N_cBins-1,N_ZBins-1))
# half filter width
band = 0
# number of dices in 1D
inputSize1D = 0
# number of time stamps
No_ts = 0

#%% # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   CASE SETUP AND TRAINING DATA EXTRACTION
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def data_extract(filterSize):
    global edges, jump, band, inputSize1D, centres, binAreas, No_ts
    band = int(filterSize/2)
    jump = int(filterSize/2)
    edges = [];
    edges.append(np.linspace(0,1,N_cBins))
    if case == 'BZ1':
        Zmax = 0.025
    else:
        Zmax = 0.05
    edges.append(np.logspace(-3,np.log10(Zmax/4),N_ZBins-3))
    edges[1] = np.insert(edges[1],0,[0.],axis=0)
    edges[1] = np.insert(edges[1],len(edges[1]),Zmax/2,axis=0)
    edges[1] = np.insert(edges[1],len(edges[1]),Zmax,axis=0)
    binWidth0 = np.diff(edges[0])[:,np.newaxis]
    binWidth1 = np.diff(edges[1])[np.newaxis,:]
    binAreas = np.matmul(binWidth0,binWidth1);
    centres = [];
    centres.append(edges[0][0:-1]/2 + edges[0][1:]/2)
    centres.append(edges[1][0:-1]/2 + edges[1][1:]/2)

    #Adjusting jump and No.TS
    if case == 'AZ1':
        ts_start = 399
        ts_end = 501
        ts_jumpAll = [ts_end-ts_start,6,3]
        ts_jump = ts_jumpAll[filterIdx-1]
        if filterSize == 120:
            jump = int(jump/1.3)
    elif case == 'AZ2':
        ts_start = 500
        ts_end = 670
        ts_jumpAll = [ts_end-ts_start,10,5]
        ts_jump = ts_jumpAll[filterIdx-1]
        if filterSize == 120:
            jump = int(jump/1.3)
    else:
        ts_start = 336
        ts_end = 501
        ts_jumpAll = [12,3]
        ts_jump = ts_jumpAll[filterIdx-1]
        if filterSize == 148:
            jump = int(jump/1.5)

    #obtain number of dices in one direction (for training)
    #first and last half width is excluded
    inputSize1D = 0;
    for i1 in range (band, 512-band, jump):
        inputSize1D = inputSize1D +1
    inputSize = inputSize1D**3
    No_ts = len(range(ts_start,ts_end+1,ts_jump))
    print("\nNo. snapshots = %d" % No_ts)
    inputSizeAll = inputSize*No_ts
    print("\nNo. training sets = %d" %inputSizeAll)

    MX_zc = np.empty((inputSize1D,inputSize1D,inputSize1D,No_ts),dtype=object)
    Mtarget_zc = np.empty(MX_zc.shape, dtype=object)
    betaPDF3D = np.zeros(MX_zc.shape, dtype=object)
    cplPDF3D = np.zeros(MX_zc.shape, dtype=object)
    dnsPDF3D = np.zeros(MX_zc.shape, dtype=object)
    annPDF3D = np.zeros(MX_zc.shape, dtype=object)
    WcT_flt = np.zeros(np.array(MX_zc).shape)
    RHO_flt = np.zeros(np.array(MX_zc).shape)
    Cbar3D = np.zeros(np.array(MX_zc).shape)
    Zbar3D = np.zeros(np.array(MX_zc).shape)
    Zvar3D = np.zeros(np.array(MX_zc).shape)
    Cvar3D = np.zeros(np.array(MX_zc).shape)
    cov3D = np.zeros(np.array(MX_zc).shape)
    X_zc = [] #learning input vector
    target_zc = [] #learning target vector
    count = 0
    countCpl = 0

    print("\nBinning Data")
    t = -1
    for ts in range(ts_start,ts_end+1,ts_jump):
        t += 1
        print("\nReading " + case + "_ts%d_FS%s data..." % (ts,filterSize))
        mat_data = loadmat(fileDir + 'ts' + str(ts) + '_' + case + '_FS'
                            + str(filterSize) + '_Fav.mat')
        mat_data['Z'][(mat_data['Z']>Zmax).nonzero()[0],
                      (mat_data['Z']>Zmax).nonzero()[1],
                      (mat_data['Z']>Zmax).nonzero()[2]] = Zmax

        #obtain pdf for each traning dices
        a = -1
        for i in range (band, 512-band, jump):
            a += 1
            b = -1
            for j in range (band, 512-band, jump):
                b += 1
                c = -1
                for k in range (band, 512-band, jump):
                    c += 1

                    count = count + 1
                    #Z and c fields for the current dice
                    Cb = (mat_data['C'][i-band:i+band,j-band:j+band,k-band:k+band]*
                          mat_data['RHO'][i-band:i+band,j-band:j+band,k-band:k+band]
                          /np.mean(mat_data['RHO'][i-band:i+band,j-band:j+band,k-band:k+band]))
                    Zb = (mat_data['Z'][i-band:i+band,j-band:j+band,k-band:k+band]*
                          mat_data['RHO'][i-band:i+band,j-band:j+band,k-band:k+band]
                          /np.mean(mat_data['RHO'][i-band:i+band,j-band:j+band,k-band:k+band]))

                    #mean and variances for the current dice
                    Cbar3D[a,b,c,t] = mat_data['Ctld'][i,j,k]
                    Zbar3D[a,b,c,t] = mat_data['Ztld'][i,j,k]
                    Zvar3D[a,b,c,t] = mat_data['ZvarFa'][i,j,k]
                    gZ = mat_data['ZvarFa'][i,j,k]/(Zbar3D[a,b,c,t]*(1-Zbar3D[a,b,c,t])+1e-8)
                    Cvar3D[a,b,c,t] = mat_data['CvarFa'][i,j,k]
                    gc = mat_data['CvarFa'][i,j,k]/(Cbar3D[a,b,c,t]*(1-Cbar3D[a,b,c,t])+1e-8)
                    cov3D[a,b,c,t]  = mat_data['covFa'][i,j,k]
                    # gzc = mat_data['covFa'][i,j,k]/np.sqrt(mat_data['ZvarFa'][i,j,k]
                    #             *mat_data['CvarFa'][i,j,k]+1e-8)

                    input_zc = [Cbar3D[a,b,c,t],Zbar3D[a,b,c,t],
                                Zvar3D[a,b,c,t],Cvar3D[a,b,c,t]]
                    if switch_cov:
                        input_zc = np.insert(input_zc,len(input_zc),cov3D[a,b,c,t],axis=0)
                    MX_zc[a,b,c,t] = input_zc
                    X_zc.append(input_zc)
                    NRe,edges[0],edges[1] = np.histogram2d(Cb.flatten(),Zb.flatten(),
                                                            (edges[0],edges[1]))
                    NRe_norm = NRe / np.sum(NRe)
                    output = NRe_norm.reshape(1,(N_cBins-1)*(N_ZBins-1),order='F')
                    Mtarget_zc[a,b,c,t] = NRe_norm
                    target_zc.append(np.concatenate(output))

                    #1D PDF for c
                    betaPDF = np.zeros(centres[0].shape)
                    if Cbar3D[a,b,c,t] > edges[0][-2]:
                        betaPDF[-1] = 1./np.concatenate(binWidth0)[-1]
                    elif Cbar3D[a,b,c,t] < edges[0][1]:
                        betaPDF[0] = 1./np.concatenate(binWidth0)[0]
                    else:
                        aa = Cbar3D[a,b,c,t]*(1/gc-1)
                        bb = (1-Cbar3D[a,b,c,t])*(1/gc-1)
                        betaPDF = beta.pdf(centres[0],aa,bb)
                        betaPDF = betaPDF/np.trapz(betaPDF,x=centres[0])
                        if sum(np.isnan(betaPDF)) > 0:
                            betaPDF = 0.
                            indx = np.concatenate(np.histogram(Cbar3D[a,b,c,t],
                                                                edges[0])[0].nonzero())
                            betaPDF[indx[0]] = 1./binWidth0[indx[0]]

                    #1D PDF for Z
                    zbetaPDF = np.zeros(centres[1].shape)
                    if Zbar3D[a,b,c,t] > edges[1][-1]:
                        zbetaPDF[-1] = 1./np.concatenate(binWidth1)[-1]
                    elif Zbar3D[a,b,c,t] < edges[1][1]:
                        zbetaPDF[0] = 1./np.concatenate(binWidth1)[0]
                    else:
                        aa = Zbar3D[a,b,c,t]*(1/gZ-1)
                        bb = (1-Zbar3D[a,b,c,t])*(1/gZ-1)
                        zbetaPDF = beta.pdf(centres[1],aa,bb)
                        zbetaPDF = zbetaPDF/np.trapz(zbetaPDF,x=centres[1])
                        if sum(np.isnan(zbetaPDF)) > 0:
                            zbetaPDF = 0.
                            indx = np.concatenate(np.histogram(Zbar3D[a,b,c,t],
                                                                edges[1])[0].nonzero())
                            zbetaPDF[indx[0]] = 1./binWidth1[indx[0]]

                    if switch_copula:
                        threCov = 4e-5;
                        if(case == 'BZ1'): threCov = 0.5*threCov
                        if(abs(mat_data['covFa'][i,j,k]) < threCov):
                            cplPDF3D[a,b,c,t] = np.atleast_2d(betaPDF).T*zbetaPDF
                        else:
                            countCpl = countCpl + 1
                            cplPDF3D[a,b,c,t] = func.copulaPDF(i,j,k,centres,edges,
                                                                input_zc)

                    betaPDF3D[a,b,c,t] = np.atleast_2d(betaPDF).T*zbetaPDF
                    dnsPDF3D[a,b,c,t] = Mtarget_zc[a,b,c,t]/binAreas

                    WcT_flt[a,b,c,t] = mat_data['WcTbar'][i,j,k]
                    RHO_flt[a,b,c,t] = mat_data['RHObar'][i,j,k]
                    if(np.remainder(count,100) == 0):
                        print("Progress --> %s%% || copula %s%%"
                          % (str('{:.1f}'.format(count/inputSizeAll*100)),
                              str('{:.1f}'.format(countCpl/inputSizeAll*100))))

    dataDict = {
             'MX_zc': MX_zc
            ,'Mtarget_zc': Mtarget_zc
            ,'betaPDF3D': betaPDF3D
            ,'cplPDF3D': cplPDF3D
            ,'dnsPDF3D': dnsPDF3D
            ,'annPDF3D': annPDF3D
            ,'WcT_flt': WcT_flt
            ,'RHO_flt': RHO_flt
            ,'Zbar3D': Zbar3D
            ,'Cbar3D': Cbar3D
            ,'Zvar3D': Zvar3D
            ,'Cvar3D': Cvar3D
            ,'cov3D': cov3D
            ,'X_zc': X_zc
            ,'target_zc': target_zc
              }

    return dataDict

# %%
def DNN(X_zc,target_zc):
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #   TRAINING DATA MANIPULATION
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    print("\nPre-processing")
    X_zc = np.array(X_zc)
    target_zc = np.array(target_zc)
    main_ANN.preprocessing_zc(X_zc, target_zc,'model_zc/')

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #   LEARNING AND VALIDATION
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    print("\nLearning")
    main_ANN.Learning()

#%%   POST-PROCESSING
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def Post_processing(fileDir,case,dataDict):
    print("\nPost processing")
    predictions_zc = np.load('model_zc/Results-ANN/predictions_zc_newInput.npy')
    outlier_index  = np.load('model_zc/Results-ANN/outlier_index.npy')
    not_outlier = 1 - outlier_index

    n_elements = np.size(predictions_zc,0)
    n_variables = np.size(predictions_zc,1)
    PDFs_zc = np.empty(n_elements, dtype=object)
    PDF3D_zc = np.empty((inputSize1D,inputSize1D,inputSize1D,No_ts), dtype=object)
    unscaled_PDF_vectors = np.zeros((n_elements, n_variables))

    for i in range(n_elements):
        unscaled_PDF_vectors[i,:] = predictions_zc[i,:]

    for i in range(n_elements):
        PDFs_zc[i] = unscaled_PDF_vectors[i,:].reshape((N_cBins-1),(N_ZBins-1)
                                                        ,order='F') / binAreas

    PDF3D_zc = (PDFs_zc.reshape(inputSize1D,inputSize1D,inputSize1D,No_ts,order='F'))
    annPDF3D = np.transpose(PDF3D_zc,(2,1,0,3))
    not_outlier3D = (not_outlier.reshape(inputSize1D,inputSize1D,inputSize1D,No_ts,order='F'))
    not_outlier3D = np.transpose(not_outlier3D,(2,1,0,3))

    '''######################################################
    ### Choosing a test dice
    ######################################################'''

    a = 5; b= 5; c = 7; t = 0

    betaPDF = np.trapz(dataDict['betaPDF3D'][a,b,c,t],x=centres[1],axis=1)
    zbetaPDF = np.trapz(dataDict['betaPDF3D'][a,b,c,t],x=centres[0],axis=0)

    #compute copula joint PDF
    if switch_copula:
        cplPDF = dataDict['cplPDF3D'][a,b,c,t]

    print("Ctld = %s" % dataDict['Cbar3D'][a,b,c,t])
    print("Ztld = %s" % dataDict['Zbar3D'][a,b,c,t])
    print("Zvar = %s" % dataDict['Zvar3D'][a,b,c,t])
    print("Cvar = %s" % dataDict['Cvar3D'][a,b,c,t])
    print("cov = %s" % dataDict['cov3D'][a,b,c,t])

    '''######################################################
    ### Joint PDF contours
    ######################################################'''
    plt.figure()
    #Joint PDF -DNS
    plt.contourf(centres[1],centres[0],dataDict['dnsPDF3D'][a,b,c,t],20)
    plt.colorbar()
    plt.show()

    #Joint PDF - 2 beta
    jbetaPDF =np.atleast_2d(betaPDF).T*zbetaPDF
    plt.contourf(centres[1],centres[0],jbetaPDF)
    plt.colorbar()
    plt.show()

    #Joint PDF - copula
    if switch_copula:
        plt.contourf(centres[1],centres[0],cplPDF)
        plt.colorbar()
        plt.show()

    #Joint PDF-ANN
    plt.contourf(centres[1],centres[0],annPDF3D[a,b,c,t])
    plt.colorbar()
    plt.show()

    '''######################################################
    ### Marginal PDF by integrating joint PDF plots
    ######################################################'''
    #1D PDF for c
    dnsPDF_c = np.trapz(dataDict['dnsPDF3D'][a,b,c,t],x=centres[1],axis=1)
    dnsPDF_c = dnsPDF_c/np.trapz(dnsPDF_c,x=centres[0]);
    plt.plot(centres[0],dnsPDF_c,label='DNS')

    plt.plot(centres[0],betaPDF,label='beta')

    if switch_copula:
        cplPDF_c = np.trapz(cplPDF,x=centres[1],axis=1)
        cplPDF_c = cplPDF_c/np.trapz(cplPDF_c,x=centres[0]);
        plt.plot(centres[0],cplPDF_c,label='copula')

    PDF_c = np.trapz(annPDF3D[a,b,c,t],x=centres[1],axis=1)
    PDF_c = PDF_c/np.trapz(PDF_c,x=centres[0])
    plt.plot(centres[0],PDF_c,label='ANN')

    plt.legend()
    plt.show()
    print('JSD-beta_c = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c),np.atleast_2d(betaPDF))),
          ' | JSD-ANN_c = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c),np.atleast_2d(PDF_c))))
    if switch_copula:
        print(' | JSD-copula_z = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c),np.atleast_2d(cplPDF_c))))

    #1D PDF for Z
    dnsPDF_z = np.trapz(dataDict['dnsPDF3D'][a,b,c,t],x=centres[0],axis=0)
    dnsPDF_z = dnsPDF_z/np.trapz(dnsPDF_z,x=centres[1]);
    plt.plot(centres[1],dnsPDF_z,label='DNS')

    plt.plot(centres[1],zbetaPDF,label='beta')

    if switch_copula:
        cplPDF_z = np.trapz(cplPDF,x=centres[0],axis=0)
        cplPDF_z = cplPDF_z/np.trapz(cplPDF_z,x=centres[1]);
        plt.plot(centres[1],cplPDF_z,label='copula')

    PDF_z = np.trapz(annPDF3D[a,b,c,t],x=centres[0],axis=0)
    PDF_z = PDF_z/np.trapz(PDF_z,x=centres[1])
    plt.plot(centres[1],PDF_z,label='ANN')

    plt.legend()
    plt.show()
    print('JSD-beta_z = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(zbetaPDF))),
          ' | JSD-ANN_z = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(PDF_z))))
    if switch_copula:
        print(' | JSD-copula_z = %s' %np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(cplPDF_z))))

    '''######################################################
    ###JSD stats
    ######################################################'''

    JSD_c_beta3D = np.zeros(annPDF3D.shape)
    JSD_c_cpl3D = np.zeros(annPDF3D.shape)
    JSD_c_ann3D = np.zeros(annPDF3D.shape)
    JSD_z_beta3D = np.zeros(annPDF3D.shape)
    JSD_z_cpl3D = np.zeros(annPDF3D.shape)
    JSD_z_ann3D = np.zeros(annPDF3D.shape)

    for t in range(No_ts):
        for a in range(inputSize1D):
            for b in range(inputSize1D):
                for c in range(inputSize1D):

                    if(b+c == 0):
                        print(str(int((a+1)/inputSize1D*100)) + '%')

                    if switch_copula:
                        cplPDF = dataDict['cplPDF3D'][a,b,c,t]

                    #1D PDF for c
                    dnsPDF_c = np.trapz(dataDict['dnsPDF3D'][a,b,c,t],x=centres[1],axis=1)
                    dnsPDF_c = dnsPDF_c/np.trapz(dnsPDF_c,x=centres[0])
                    betaPDF = np.trapz(dataDict['betaPDF3D'][a,b,c,t],x=centres[1],axis=1)
                    JSD_c_beta3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c),np.atleast_2d(betaPDF)))
                    if switch_copula:
                        cplPDF_c = np.trapz(cplPDF,x=centres[1],axis=1)
                        cplPDF_c = cplPDF_c/np.trapz(cplPDF_c,x=centres[0])
                        JSD_c_cpl3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c), np.atleast_2d(cplPDF_c)))
                    PDF_c = np.trapz(annPDF3D[a,b,c,t],x=centres[1],axis=1)
                    PDF_c = PDF_c/np.trapz(PDF_c,x=centres[0])
                    JSD_c_ann3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_c), np.atleast_2d(PDF_c)))

                    #1D PDF for Z
                    dnsPDF_z = np.trapz(dataDict['dnsPDF3D'][a,b,c,t],x=centres[0],axis=0)
                    dnsPDF_z = dnsPDF_z/np.trapz(dnsPDF_z,x=centres[1])
                    zbetaPDF = np.trapz(dataDict['betaPDF3D'][a,b,c,t],x=centres[0],axis=0)
                    JSD_z_beta3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(zbetaPDF)))
                    if switch_copula:
                        cplPDF_z = np.trapz(cplPDF,x=centres[0],axis=0)
                        cplPDF_z = cplPDF_z/np.trapz(cplPDF_z,x=centres[1])
                        JSD_z_cpl3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(cplPDF_z)))
                    PDF_z = np.trapz(annPDF3D[a,b,c,t],x=centres[0],axis=0)
                    PDF_z = PDF_z/np.trapz(PDF_z,x=centres[1])
                    JSD_z_ann3D[a,b,c,t] = np.mean(func.JSDiv(np.atleast_2d(dnsPDF_z),np.atleast_2d(PDF_z)))

    print('Javg-beta_c = %s' % (np.mean(JSD_c_beta3D*not_outlier3D)),
          ' \ Javg-ANN_c = %s' % (np.mean(JSD_c_ann3D*not_outlier3D)))
    print('J90-beta_c = %s' % (np.percentile(JSD_c_beta3D*not_outlier3D,90)),
          ' \ J90-ANN_c = %s' % (np.percentile(JSD_c_ann3D*not_outlier3D,90)))
    if switch_copula:
        print(' \ Javg-coula_c = %s' % (np.mean(JSD_c_cpl3D*not_outlier3D)),
              ' \ J90-coula_c = %s' % (np.percentile(JSD_c_cpl3D*not_outlier3D,90)))
    plt.hist((JSD_c_beta3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='beta')
    plt.hist((JSD_c_ann3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='ann')
    if switch_copula:
        plt.hist((JSD_c_cpl3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='copula')
    plt.legend()
    plt.show()

    print('Javg-beta_z = %s' % (np.mean(JSD_z_beta3D*not_outlier3D)),
          ' \ Javg-ANN_z = %s' % (np.mean(JSD_z_ann3D*not_outlier3D)) )
    print('J90-beta_z = %s' % (np.percentile(JSD_z_beta3D*not_outlier3D,90)),
          ' \ J90-ANN_z = %s' % (np.percentile(JSD_z_ann3D*not_outlier3D,90)) )
    if switch_copula:
        print(' \ Javg-coula_z = %s' % (np.mean(JSD_z_cpl3D*not_outlier3D)),
              ' \ J90-coula_z = %s' % (np.percentile(JSD_z_cpl3D*not_outlier3D,90)))
    plt.hist((JSD_z_beta3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='beta')
    plt.hist((JSD_z_ann3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='ann')
    if switch_copula:
        plt.hist((JSD_z_cpl3D*not_outlier3D).flatten(),bins=np.linspace(0,0.5,20),label='copula')
    plt.legend()
    plt.show()

    '''######################################################
    ### Reaction rate closure
    ######################################################'''
    conAvg_WcT = loadmat(fileDir + 'conAvg_WcT_all_' + case + '.mat')['conAvg_WcT_all']
    conAvg_RHO = loadmat(fileDir + 'conAvg_WcT_all_' + case + '.mat')['conAvg_RHO_all']
    WcT_dns = np.zeros(annPDF3D.shape)
    WcT_beta = np.zeros(annPDF3D.shape)
    WcT_cpl = np.zeros(annPDF3D.shape)
    WcT_ann = np.zeros(annPDF3D.shape)

    for t in range(No_ts):
        for a in range(inputSize1D):
            for b in range(inputSize1D):
                for c in range(inputSize1D):

                    if switch_copula:
                        integrad = np.multiply(conAvg_WcT/(conAvg_RHO+1e-4),t,dataDict['cplPDF3D'][a,b,c,t])
                        tmp = np.trapz(integrad,x=centres[0],axis=0)
                        WcT_cpl[a,b,c,t] = np.trapz(tmp,x=centres[1])*dataDict['RHO_flt'][a,b,c,t]
                    integrad = np.multiply(conAvg_WcT/(conAvg_RHO+1e-4),dataDict['betaPDF3D'][a,b,c,t])
                    tmp = np.trapz(integrad,x=centres[0],axis=0)
                    WcT_beta[a,b,c,t] = np.trapz(tmp,x=centres[1])*dataDict['RHO_flt'][a,b,c,t]
                    integrad = np.multiply(conAvg_WcT/(conAvg_RHO+1e-4),annPDF3D[a,b,c,t])
                    tmp = np.trapz(integrad,x=centres[0],axis=0)
                    WcT_ann[a,b,c,t] = np.trapz(tmp,centres[1])*dataDict['RHO_flt'][a,b,c,t]
                    integrad = np.multiply(conAvg_WcT/(conAvg_RHO+1e-4),dataDict['dnsPDF3D'][a,b,c,t])
                    tmp = np.trapz(integrad,x=centres[0],axis=0)
                    WcT_dns[a,b,c,t] = np.trapz(tmp,x=centres[1])*dataDict['RHO_flt'][a,b,c,t]

    RMSE1 = 0.;
    if switch_copula:
        RMSE1 = sqrt(np.mean(((WcT_cpl - WcT_dns)/(WcT_dns+10.))**2*not_outlier3D))
        plt.scatter(WcT_dns.flatten(),WcT_cpl.flatten(),1,label='copula')
    RMSE2 = sqrt(np.mean(((WcT_ann - WcT_dns)/(WcT_dns+10.))**2*not_outlier3D))
    plt.scatter((WcT_dns*not_outlier3D).flatten(),(WcT_ann*not_outlier3D).flatten(),1,label='ann')
    RMSE3 = sqrt(np.mean(((WcT_beta - WcT_dns)/(WcT_dns+10.))**2*not_outlier3D))
    plt.scatter((WcT_dns*not_outlier3D).flatten(),(WcT_beta*not_outlier3D).flatten(),1,label='beta')
    plt.title('RMSE ANN:' + str('{:.2f}'.format(RMSE2))
                + ' | beta:' + str('{:.2f}'.format(RMSE3)))
    plt.legend()
    plt.show()

    #contour plots
    plt.contourf(np.squeeze(dataDict['WcT_flt'][int(inputSize1D/2),:,:,0]))
    plt.title('flt')
    plt.clim(0,1000)
    plt.show()

    plt.contourf(np.squeeze(WcT_dns[int(inputSize1D/2),:,:,0]))
    plt.title('dns')
    plt.clim(0,1000)
    plt.show()

    plt.contourf(np.squeeze(WcT_beta[int(inputSize1D/2),:,:,0]))
    plt.title('beta')
    plt.clim(0,1000)
    plt.show()

    if switch_copula:
        plt.contourf(np.squeeze(WcT_cpl[int(inputSize1D/2),:,:,0]))
        plt.title('copula')
        plt.clim(0,1000)
        plt.show()

    plt.contourf(np.squeeze(WcT_ann[int(inputSize1D/2),:,:,0]))
    plt.title('ann')
    plt.clim(0,1000)
    plt.show()

# %%
if __name__ == "__main__":
    for caseIdx in [1]:
    # for caseIdx in range(3+1)[1:]:
        caseAll = ['AZ1','AZ2','BZ1']
        case = caseAll[caseIdx-1]
        fileDir = './DNS_data/'
        filterSizeAll = np.array([[40,80,120],[40,80,120],[74,148]])
        for filterIdx in [1]:
        # for filterIdx in range(3+1)[1:]:
            filterSize = filterSizeAll[caseIdx-1][filterIdx-1]

            datDic = data_extract(filterSize)

            DNN(datDic['X_zc'],datDic['target_zc'])

            Post_processing(fileDir,case,datDic)


# Stock Price Checker

import sys
import requests
import time
from bs4 import BeautifulSoup

print("This application will determine the price of a Stock, and send you an email when a specified price tager is reached")

class Stock:

    Stock_Code = ""
    Stock_Price = 0.0

    def FindStock(self):
        check = True
        while(check == True):
            try:
                self.Stock_Code = input("Enter the Stock Code: ")
                url = ('https://finance.yahoo.com/quote/' + self.Stock_Code + '?p=' + self.Stock_Code + '&.tsrc=fin-srch')
                req = requests.get(url)

                self.Stock_Price = BeautifulSoup(req.text, 'lxml')
                self.Stock_Price = self.Stock_Price.find('div', {"class" : 'My(6px) Pos(r) smartphone_Mt(6px)'})

                self.Stock_Price = self.Stock_Price.find('span').text
                check = False
            except Exception:
                print(self.Stock_Code, " is not a Valid Stock Code")
            else:
                    self.Stock_Price = float(self.Stock_Price)

    def FindStockPrice(self):
        url = ('https://finance.yahoo.com/quote/' + self.Stock_Code + '?p=' + self.Stock_Code + '&.tsrc=fin-srch')
        req = requests.get(url)

        self.Stock_Price = BeautifulSoup(req.text, 'lxml')
        self.Stock_Price = self.Stock_Price.find('div', {"class" : 'My(6px) Pos(r) smartphone_Mt(6px)'})

        self.Stock_Price = self.Stock_Price.find('span').text

    def Send_mail():

        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.ehlo()

        server.login('ahmadmaazin1@gmail.com', 'qjtgbeucfsjvnwnd')

        subject = 'Stock Alert!'
        body = 'Your stock has reached the desired price target! Check the Stock: ' ' https://www.tradingview.com/symbols/' + Stock_Code + '/'

        msg = f"Subject: {subject}\n\n{body}"

        server.sendmail('ahmadmaazin1@gmail.com','maahmed@ucdavis.edu', msg)
        print("Email Sent")

        server.quit()

    def PriceChecker(self):

        TargetPrice = float(input("Enter the Price Point for which you want to be notified: "))

        while(self.Stock_Price > TargetPrice):
            print("Price is currently at: ", self.Stock_Price)
            self.FindStockPrice(self)
            time.sleep(60)
        else:
            Send_mail()

Share = Stock
Share.FindStock(Share)
print(Share.Stock_Code, " is ", Share.Stock_Price)
Share.PriceChecker(Share)


import sys
from csv import reader
from pyspark import SparkContext

sc = SparkContext()

lines = sc.textFile(sys.argv[1], 1)
lines = lines.mapPartitions(lambda x: reader(x))

sum_of_value = lines.map(lambda x: (x[2], float(x[12])))
total_sum = sum_of_value.reduceByKey(lambda x, y: x + y)

counts_of_value = lines.map(lambda x: (x[2], 1))
count = counts_of_value.reduceByKey(lambda x, y: x + y)

average = total_sum.join(count).map(lambda x: (x[0], x[1][0], float((x[1][0] / x[1][1]))))
output = average.map(lambda x: x[0] + '\t' + "{0:.2f}".format(x[1]) + ', ' + "{0:.2f}".format(x[2]))
output.saveAsTextFile("task3.out")


from PIL import Image

from pagermaid.listener import listener
from pagermaid.enums import Message, Client
from pagermaid.single_utils import safe_remove

@listener(
    command="sticker_to_pic", description="将你回复的静态贴纸转换为图片", parameters="（是否发送原图，默认为否）"
)
async def sticker_to_pic(bot: Client, message: Message):
    origin = bool(message.arguments)
    reply = message.reply_to_message
    if not reply:
        return await message.edit("请回复一个静态贴纸")
    if not reply.sticker:
        return await message.edit("请回复一个静态贴纸")
    if reply.sticker.is_animated or reply.sticker.is_video:
        return await message.edit("请回复一个静态贴纸")
    try:
        photo = await bot.download_media(reply, in_memory=True)
        message: Message = await message.edit("正在转换...\n███████70%")
        image = Image.open(photo)
        image.save("sticker.png", "PNG")
    except Exception as e:
        return await message.edit(f"转换失败：{e}")
    if origin:
        await reply.reply_document("sticker.png", quote=True)
    else:
        await reply.reply_photo("sticker.png", quote=True)
    safe_remove("sticker.png")
    await message.safe_delete()


import requests

headers = {'Authorization': 'Bearer 635812bd9ead41dcb46169a629e4f6e7918715e8'}
endpoint = "http://localhost:8000/api/products/"

data = {
    "title": "This field is done",
    "price": 32.00
}

get_response = requests.post(endpoint, json=data, headers=headers)
print(get_response.json())


import logging
from logging.handlers import SysLogHandler
logger = logging.getLogger("classificationbanner")
handler = SysLogHandler(facility=SysLogHandler.LOG_DAEMON, address='/dev/log')
logger.addHandler(handler)

formatter = logging.Formatter('classification-banner[%(process)d] (%(module)s.%(funcName)s) %(levelname)s: %(message)s')
handler.setFormatter(formatter)

logger.setLevel(logging.DEBUG)

import os
import platform

import classificationbanner.utils.config  as utils_config
import classificationbanner.utils.display as utils_display

# GTK and GDK
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gtk, Gdk

def get_user():
  """Helper to get logged in username"""
  try:
      user = os.getlogin()
  except:
      user = 'UnknownUser'
  return user

def get_hostname():
  """Helper to get hostname"""
  return platform.node()

class ClassificationBanner:
  def __init__(self, config=None, classification=None, location="top", banner_height=25):
    """Set up and display the banner's main window """

    # Check we have some sort of configuration
    if config is None:
      # Try to load it from the file system
      try:
        config = utils_config.open_config()
      except:
        exit(1)

    # Check that the classification is set
    if classification is None:
      # Try to load it from the configuration file
      try:
        classification = utils_config.get_system_classification(config)
      except:
        exit(1)

    # Get and save the workable area of the manager supports it.
    work_area = utils_display.get_workable_area()

    # Prepare to use our stylesheet
    style_provider = Gtk.CssProvider()
    style_filename = 'style.css'
    style_path = "/etc/classification-banner/{}".format(style_filename)
    style_provider.load_from_path(style_path)
    Gtk.StyleContext.add_provider_for_screen(
      Gdk.Screen.get_default(),
      style_provider,
      Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
    )

    # Set our current classficiation style class
    style_class = utils_config.get_classification_style_name(config, classification)
    # Die if no class is configured
    if style_class is None:
      exit(1)

    # Get some screen information and save it off
    screen_size = utils_display.get_screen_size()
    self.hres = screen_size['width']
    self.vres = screen_size['height']

    # Create Main Window
    self.window = Gtk.Window(title="Classification Banner")
    self.window.set_position(Gtk.WindowPosition.CENTER)
    self.window.set_type_hint(Gdk.WindowTypeHint.DOCK)
    self.window.set_property('skip-taskbar-hint', True)
    self.window.set_property('skip-pager-hint', True)
    self.window.stick()
    self.window.set_decorated(False)
    self.window.set_keep_above(True)
    self.window.set_app_paintable(True)
    self.window.set_default_size(self.hres, 25)

    # Our main horizontal box for label containment
    hbox = Gtk.Box(spacing=10)
    hbox.get_style_context().add_class(style_class)

    # User label
    label_user = Gtk.Label(label=get_user())
    label_user.get_style_context().add_class(style_class)
    label_user.set_halign(Gtk.Align.START)

    # Classification label
    label_classification = Gtk.Label(label=utils_config.get_classification_label(config, classification))
    label_classification.get_style_context().add_class(style_class)
    label_classification.set_halign(Gtk.Align.CENTER)

    # Hostname label
    label_hostname = Gtk.Label(label=get_hostname())
    label_hostname.get_style_context().add_class(style_class)
    label_hostname.set_halign(Gtk.Align.END)

    # Pack up the horizontal box with the labels we made
    hbox.pack_start(label_user, True, True, 0)
    hbox.pack_start(label_classification, True, True, 0)
    hbox.pack_start(label_hostname, True, True, 0)

    # Add box to window
    self.window.add(hbox)

    # Show window and attach its life to Gtk
    self.window.show_all()
    self.window.connect("destroy", Gtk.main_quit)
    if location.lower() == "top":
      # Throw a hit at X11 to make room for static elements that need to cut down on workspace
      utils_display.hint_X11(
        window=self.window,
        top=(work_area['y'] + banner_height)
      )
      # Move window into place
      self.window.move(0, work_area['y'])
    elif location.lower() == "bottom":
      # Throw a hit at X11 to make room for static elements that need to cut down on workspace
      utils_display.hint_X11(
        window=self.window,
        bottom=banner_height
      )
      # Move window into place
      self.window.move(0, work_area['height']+2)

    logger.info("Classification {} banner loaded".format(location))

def main():
  if utils_display.detect_window_system() != "x11":
    logger.critical("You must be running X11 for this utility to work.")
    exit(1)

  try:
    config = utils_config.open_config()
  except:
    exit(1)

  ClassificationBanner(config=config, location="top")
  ClassificationBanner(config=config, location="bottom")

  Gtk.main()

if __name__ == "__main__":
  main()


import pyqrcode
import requests
from django.shortcuts import render, HttpResponse, redirect
from django_tables2 import Table, RequestConfig, LazyPaginator
from django.core.exceptions import BadRequest
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from .tables import *
from main.models import *
from .forms import *

# Create your views here.
@login_required(login_url='loginPage')
def usersList(request):
    template ='userslist.html'
    table = UsersTable(User.objects.all().order_by('id'))
    RequestConfig(request, paginate={"paginator_class": LazyPaginator,
                                     "per_page": 10}).configure(table)
    context = {'table': table}
    return render(request, template, context)

@login_required(login_url='loginPage')
#Add user from Web interface
def addUser(request):
    template = 'useradd.html'
    form = UserForm(request.POST or None)
    if form.is_valid():
        form.save()
        user = User.objects.all().order_by('-id')[0]
        user_id = user.id
        filename = str(user.facedata)
        print(user_id)
        image = user.facedata.open(mode='rb')
        url = "https://search.facex.io:8443/images/singleImage/"

        payload = {'user_id': '62c81adb7312e67dcfb98d3f',
                   'name': user_id}
        files = [
            ('image', (filename, image, 'application/octet-stream'))
        ]
        headers = {}
        api_resp = requests.request("POST", url, headers=headers, data=payload, files=files)
        if api_resp:
            return redirect('usersList')
        else:
            raise BadRequest

    context = {
        'form':form
    }
    return render(request, template, context)

@login_required(login_url='loginPage')
# Genere Client QRCode
def qrCode(request,pk):
    code = User.objects.get(id=pk)
    code = str(code.id)
    # # Code pour generer le QRCode
    qrobj = pyqrcode.create(code, encoding='utf-8')
    with open('test.png', 'wb') as f:
        qrobj.png(f, scale=10)
    image_data = open('test.png', 'rb').read()
    response = HttpResponse(image_data, content_type='image/png')
    response['Content-Disposition'] = 'attachment; filename=%s.png'
    return response

@login_required(login_url='loginPage')
# Edit user
def userEdit(request,pk):
    template = 'useredit.html'
    user = User.objects.get(id=pk)
    form = UserForm(request.POST or None, instance=user)
    if form.is_valid():
        form.save()
        return redirect('usersList')
    context = {'form':form}
    return render(request,template,context)

@login_required(login_url='loginPage')
def userDelete(request,pk):
    user = User.objects.get(id=pk)
    user.delete()
    return redirect('usersList')

@login_required(login_url='loginPage')
def userDeactivate(request,pk):
    user = User.objects.get(id=pk)
    user.is_active = False
    user.save(update_fields=['is_active'])
    return redirect('usersList')

@login_required(login_url='loginPage')
def userActivate(request,pk):
    user = User.objects.get(id=pk)
    user.is_active = True
    user.save(update_fields=['is_active'])
    return redirect('usersList')

@login_required(login_url='loginPage')
def usersearch(request):
    template = 'userslist.html'
    if request.method == 'POST':
        search = request.POST['search']
        table = UsersTable(User.objects.filter(Q(firstname=search)|Q(lastname =search)|Q(matricule=search)))
        RequestConfig(request, paginate={"paginator_class": LazyPaginator,
                                         "per_page": 10}).configure(table)
        context = {'table': table}
        return render(request, template, context)
    return redirect('usersList')


from matplotlib import *
import matplotlib.pyplot as plt
import numpy as np
from subprocess import *
#rom subprocess import run
ok = subprocess.call('./dec.exe encryptx.txt')
print("\n--------  raw out  ----------")
print(ok)
print("--------  raw tmp  ----------")
file = open("tmp", "r")
string = file.read()
file.close()
print(string)
print("--------  data  ----------")
data = string.split()
integ = []
for k in data:
	j = int(k)
	#if j == 0: j = 6000
	integ.append(j)
x = np.linspace(-0.5244, 3.6644, len(integ))
print(integ)
#plt.plot(data) #linear mode
#plt.show() #linear mode
plt.polar(x, integ)
plt.show()


from decimal import Decimal
from django.shortcuts import render, HttpResponseRedirect
from .models import Cart, CartItem
from django.urls import reverse
from products.models import Product, Variation
# Create your views here.
def view(request):
    try:
        the_id = request.session['cart_id']
        cart = Cart.objects.get(id=the_id)
    except:
        the_id = None
    if the_id:
        context = {"cart": cart}
        new_total = 0.00
        for item in cart.cartitem_set.all():
            line_total =  float(item.product.price) * item.quantity
            new_total += line_total
        request.session['items_total'] = cart.cartitem_set.count()
        two_dec_digits = Decimal(10)  ** -2
        cart.total = Decimal(new_total).quantize(two_dec_digits)
        if cart.total == 0:
            message = "Your Cart is EMPTY"
            context = {"empty": True, "message": message}
        cart.save()
    else:
        message = "Your Cart is EMPTY"
        context = {"empty": True, "message": message}
    template = 'cart/view.html'
    return render(request, template, context)

def remove_from_cart(request, id):
    try:
        the_id = request.session['cart_id']
        cart = Cart.objects.get(id=the_id)
    except:
        return HttpResponseRedirect(reverse("cart"))
    cartitem = CartItem.objects.get(id=id)
    cartitem.delete()
    # cartitem.cart = None
    # cartitem.save()
    return HttpResponseRedirect(reverse("cart"))

def add_to_cart(request, slug):
    try:
        qty = request.GET.get('qty')
        update_qty = True
    except:
        qty = None
        update_qty = False
    try:
        the_id = request.session['cart_id']
    except:
        new_cart = Cart()
        new_cart.save()
        request.session['cart_id'] = new_cart.id
        the_id = new_cart.id
    cart = Cart.objects.get(id=the_id)
    try:
        product = Product.objects.get(slug=slug)
    except Product.DoesNotExist:
        pass
    except:
        pass

    product_var = []
    if request.method == "POST":
        qty = request.POST['qty']
        for item in request.POST:
            key = item
            val = request.POST[key]
            try:
                v = Variation.objects.get(product = product, category__iexact = key, title__iexact = val)
                product_var.append(v)
            except:
                pass

        cart_item = CartItem.objects.create(cart=cart,product=product)

        if len(product_var) > 0:
            cart_item.variations.add(*product_var)
        cart_item.quantity = qty
        cart_item.save()
        return HttpResponseRedirect(reverse("cart"))
    return HttpResponseRedirect(reverse("cart"))


from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail.message import EmailMessage

from common.models import TemplateModel

def send_voting_email(request, voting, exclude_memberships=None):
    ctx_dict = {
        'voting': voting,
        'site': get_current_site(request),
        'protocol': settings.SSL_ON and 'https' or 'http',
    }

    for voter in voting.voters.all():
        member = voter.membership
        if exclude_memberships and member in exclude_memberships:
            continue

        ctx_dict['recipient'] = member
        ctx_dict['link'] = '{procotol}://{site}{url}'.format(procotol=ctx_dict['protocol'], site=ctx_dict['site'], url=voter.get_voting_url())

        tmpl = TemplateModel.objects.get(name=TemplateModel.VOTING_INVITATION)
        subject = tmpl.generate_title(ctx_dict) or voting.account.name
        message = tmpl.generate(ctx_dict)

        mail = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL, [member.user.email])
        admin_emails = [m.user.email for m in voting.account.get_admin_memberships() if m.id != member.id]
        if admin_emails:
            mail.extra_headers['Reply-To'] = ', '.join(admin_emails)

        mail.content_subtype = "html"
        mail.send()


#!/usr/bin/env python
'''module to read data from dtu/template and show in VenusOS'''

# File specific rules
# pylint: disable=broad-except

# system imports:
import logging
import os
import configparser
import sys

# our imports:
#import tests
from dbus_service import DbusService
from dbus_shelly_service import DbusShellyemService

if sys.version_info.major == 2:
    import gobject  # pylint: disable=E0401
else:
    from gi.repository import GLib as gobject  # pylint: disable=E0401

ASECOND = 1000

def main():
    '''main loop'''

    # configure logging
    config = configparser.ConfigParser()
    config.read(f"{(os.path.dirname(os.path.realpath(__file__)))}/config.ini")
    logging_level = config["DEFAULT"]["Logging"]

    logging.basicConfig(
        format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        level=logging_level,
        handlers=[
            logging.FileHandler(f"{(os.path.dirname(os.path.realpath(__file__)))}/current.log"),
            logging.StreamHandler(),
        ],
    )

    #tests.run_tests()

    try:
        logging.info("Start")

        from dbus.mainloop.glib import DBusGMainLoop  # pylint: disable=E0401,C0415

        # Have a mainloop, so we can send/receive asynchronous calls to and from dbus
        DBusGMainLoop(set_as_default=True)

        # formatting
        def _kwh(p, v): return (str(round(v, 2)) + "kWh")
        def _a(p, v): return (str(round(v, 1)) + "A")
        def _w(p, v): return (str(round(v, 1)) + "W")
        def _v_dc(p, v): return (str(round(v, 1)) + "V DC")
        def _v_ac(p, v): return (str(round(v, 1)) + "V AC")
        def _c(p, v): return (str(round(v, 1)) + "Degrees celsius")

        # com.victronenmergy.dcload
        # /Dc/0/Voltage              <-- V DC
        # /Dc/0/Current              <-- A, positive when power is consumed by DC loads
        # /Dc/0/Temperature          <-- Degrees centigrade, temperature sensor on SmarShunt/BMV
        # /Dc/1/Voltage              <-- SmartShunt/BMV secondary battery voltage (if configured)
        # /History/EnergyIn          <-- Total energy consumed by dc load(s).
        paths = {
            "/Dc/0/Voltage": {"initial": None, "textformat": _v_dc},
            "/Dc/0/Current": {"initial": None, "textformat": _a},
            "/Dc/0/Temperature": {"initial": None, "textformat": _c},
            "/Dc/1/Voltage": {"initial": None, "textformat": _w},
            "/History/EnergyIn": {"initial": None, "textformat": _kwh},
            "/Dc/0/Power": {"initial": None, "textformat": _w},
        }

        # Init devices/services, I've two devices
        servicename="com.victronenergy.dcload"
        logging.info("Registering dtu devices")
        inverterList = [
            # [INVERTER0]
            DbusService(
                servicename=servicename,
                paths=paths,
                actual_inverter=0,
            ),
            # [INVERTER1]
            DbusService(
                servicename=servicename,
                paths=paths,
                actual_inverter=1,
            ),
            # [INVERTER2]
            DbusService(
                servicename=servicename,
                paths=paths,
                actual_inverter=2,
            )
        ]

        # com.victronenergy.acload
        # /Ac/Energy/Forward     <- kWh  - bought energy (total of all phases)
        # /Ac/Power              <- W    - total of all phases, real power
        # /Ac/Current            <- A AC - Deprecated
        # /Ac/Voltage            <- V AC - Deprecated
        # /Ac/L1/Current         <- A AC
        # /Ac/L1/Energy/Forward  <- kWh  - bought
        # /Ac/L1/Power           <- W, real power
        # /Ac/L1/Voltage         <- V AC
        # /Ac/L2/*               <- same as L1
        # /Ac/L3/*               <- same as L1
        # /DeviceType
        # /ErrorCode
        paths = {
            '/Ac/Energy/Forward': {'initial': 0, 'textformat': _kwh}, # energy bought from the grid
            '/Ac/Power': {'initial': 0, 'textformat': _w},
            '/Ac/Current': {'initial': 0, 'textformat': _a},
            '/Ac/Voltage': {'initial': 0, 'textformat': _v_ac},
            '/Ac/L1/Voltage': {'initial': 0, 'textformat': _v_ac},
            '/Ac/L1/Current': {'initial': 0, 'textformat': _a},
            '/Ac/L1/Power': {'initial': 0, 'textformat': _w},
            '/Ac/L1/Energy/Forward': {'initial': 0, 'textformat': _kwh},
        }

        #[SHELLY]
        servicename="com.victronenergy.acload"
        logging.info("Registering Shelle EM")
        DbusShellyemService(
            servicename=servicename,
            paths=paths,
            inverter=inverterList,
        )

        #start our main-service
        logging.info("Connected to dbus, and switching over to gobject.MainLoop() (= event based)")
        mainloop = gobject.MainLoop()
        mainloop.run()
    except Exception as error:
        logging.critical("Error at %s", "main", exc_info=error)

if __name__ == "__main__":
    main()


from django.views import View
from django.http import JsonResponse, request
from .models import Owner, Dog
import json
# Create your views here.

class SampleOwnerView(View):
    def post(self, request):
        try:
            data = json.loads(request.body)
            Owner.objects.create(name=data['name'],email=data['email'],age=data['age'])
            return JsonResponse({"message":"SUCCESS!"},status=201)
        except KeyError:
            return JsonResponse({"message":"INVALID_KEY"}, status=400)

    def get(self, request):
        '''owners = Owner.objects.all()
        owner_list=[]
        for owner in owners:
            owner_={}
            owner_['name']=owner.name
            owner_['email']=owner.email
            owner_['age']=owner.age
            owner_list.append(owner_)'''
        owners = Owner.objects.all()
        owner_list=[]
        for owner in owners:
            owner_={}
            dogs = Dog.objects.filter(owner_id=owner.id)
            dog_list = [{'name':dog.name,'age':dog.age} for dog in dogs]
            owner_['name']=owner.name
            owner_['email']=owner.email
            owner_['age']=owner.age
            owner_['dogs']=dog_list
            owner_list.append(owner_)
        return JsonResponse({'result':owner_list}, status=200)

class SampleDogView(View):
    def post(self, request):
        try:
            data = json.loads(request.body)
            dog_owner = Owner.objects.get(name=data['owner'])
            Dog.objects.create(name=data['name'],age=data['age'],owner=dog_owner)
            return JsonResponse({"message":"SUCCESS!"},status=201)
        except KeyError:
            return JsonResponse({"message":"INVALID_KEY"}, status=400)

    def get(self, request):
        dogs = Dog.objects.all()
        dogs_list=[]
        for dog in dogs:
            dog_={}
            owners = Owner.objects.filter(id=dog.owner_id)
            dog_owner = [owner.name for owner in owners]
            dog_['name']=dog.name
            dog_['age']=dog.age
            dog_['owner']=dog_owner
            dogs_list.append(dog_)
        return JsonResponse({'result':dogs_list}, status=200)


import boto3
from requests_aws4auth import AWS4Auth
import requests
import JaSONx
import os
import time
from utility import binary_to_dict, createFileList
from PyQt5 import QtCore
import httpReq
import os
import sys, os, base64, datetime, hashlib, hmac

dict_credentials = {}
dict_response = {}
r = ''
def readCredentials(account):
    pathcredentials = QtCore.QDir.homePath() + '/.aws/credentials'
    with open( pathcredentials , 'r') as fin:
        credentials = fin.read()
    list_credentials = credentials.split("[")
    for lis in list_credentials:
        if( account in lis):
            for attr in lis.split("\n"):
                if("region" in attr or "aws_access_key_id" in attr or "aws_secret_access_key" in attr or "aws_session_token" in attr or "aws_security_token" in attr):
                        key, val =attr.split(" = ")
                        dict_credentials[key] = val
    return dict_credentials

def readfilejson(path):
        with open(path, 'r') as json_file:
            return json_file.read()

def activateThing(hierarchy_name, file, dict_credentials, api_id):

    payload = readfilejson(os.path.join(os.path.realpath(''),'json_files', hierarchy_name, file))
    endpoint = 'https://' + api_id + '.execute-api.eu-central-1.amazonaws.com/dev/v1/gateways/conad-eig1/command'
    post_response = httpReq.post(dict_credentials, endpoint, payload)

    if(post_response[0] == 200):
        endpoint = 'https://' + api_id + '.execute-api.eu-central-1.amazonaws.com/dev/v1/gatewayCommands/'
        response = httpReq.get(dict_credentials,
            endpoint,
            binary_to_dict(post_response[1])["command_id"]
            )
        return response
    else:
        status = post_response[0]
        message = binary_to_dict(post_response[1])["errors"][0]["error_message"]
        response = (status,message)
        return response

#Initialize the activations getting the credentials with readCredentials() and posting all the payloads in list_json_file with activateThing()
def initializeActivations(hierarchy_name, list_json_file, account, api_id):

    #Populate dict_credentials with STS from .\.aws\credentials for the account selected
    dict_credentials = readCredentials(account)

    #Initialize response dictionary
    dict_response = {}
    if(list_json_file != []):
        for file in list_json_file:
            response = activateThing(hierarchy_name, file, dict_credentials, api_id)
            dict_response[file] = "STATUS:" + str(response[0]) + ";" + "MESSAGE:" + response[1]
    print(dict_response)
    return dict_response


import numpy as np
from numpy import linalg as LA
from pylab import *
import cvxpy  as cp

#if using termux
import subprocess
import shlex
#end if

# ax+by <= d
A = np.array(( [3, 3], [3, 2])).T  #
B = np.array([18,15]).reshape(2,1)  #Total available Floor & Fat

# objective function coeffs
c = np.array([30,25])
x = cp.Variable((2,1),nonneg=True)

#Cost function
f = c@x     #P = max(30 25)x
obj = cp.Maximize(f)

#Constraints
constraints = [A@x <= B]

#solution
prob = cp.Problem(obj, constraints)
prob.solve()
print("Maximun :", f.value)
print("Number of type_1 :", x.value[0])
print("Number of type_2 :", x.value[1])

x1=np.linspace(0,8,200)
#print(len(x1))
y1=(18-3*x1)/3
y2=(15-3*x1)/2
plt.plot(x1,y1,label='3x+3y=18')
plt.plot(x1,y2,label='3x+2y=15')

y4=np.zeros(len(x1))
plt.plot(x1,y4,label='y=0')
plt.plot(y4,x1,label='x=0')
plt.title('')
plt.ylim([-2,8])
# Add X and y Label
plt.xlabel('x axis')
plt.ylabel('y axis')

# Add a grid
plt.grid(alpha=1,linestyle='--')
plt.legend()
plt.savefig('/sdcard/download/opt/opt1.pdf')
subprocess.run(shlex.split("termux-open /sdcard/Download/opt/opt1.pdf"))
plt.show()


import json

from django.core.exceptions import ObjectDoesNotExist

import dataflow.batch.settings as settings
from dataflow.batch.models.bkdata_flow import ProcessingJobInfo
from dataflow.shared.log import batch_logger

class ProcessingJobInfoHandler(object):
    @staticmethod
    def create_proc_job_info(args, processing_type="batch", implement_type="sql"):
        def _choose_value(key):
            return args.get(key, getattr(settings, "%s_DEFAULT" % key.upper()))

        job_id = args["job_id"]
        code_version = _choose_value("code_version")
        if code_version is None:
            code_version = getattr(settings, "CODE_VERSION_DEFAULT")

        ProcessingJobInfo.objects.create(
            job_id=job_id,
            processing_type=processing_type,
            code_version=code_version,
            component_type=_choose_value("component_type"),
            job_config=args["job_config"],
            jobserver_config=json.dumps(args["jobserver_config"]),
            cluster_group=_choose_value("cluster_group"),
            cluster_name=_choose_value("cluster_name"),
            deploy_mode=_choose_value("deploy_mode"),
            deploy_config=_choose_value("deploy_config"),
            created_by=args["bk_username"],
            updated_by=args["bk_username"],
            implement_type=implement_type,
        )
        return job_id

    @staticmethod
    def update_proc_job_info(args, processing_type="batch"):
        def _choose_value(key):
            return args.get(key, getattr(settings, "%s_DEFAULT" % key.upper()))

        job_id = args["job_id"]
        code_version = _choose_value("code_version")
        if code_version is None:
            code_version = getattr(settings, "CODE_VERSION_DEFAULT")

        if ProcessingJobInfoHandler.is_proc_job_info(job_id):
            # 为了支持刷新spark配置项和node_label重启flow继续有效，更新JobInfo时合并更新deploy_config
            old_job_info = ProcessingJobInfoHandler.get_proc_job_info(job_id)
            deploy_config = {}
            try:
                if old_job_info.deploy_config is not None:
                    deploy_config = json.loads(old_job_info.deploy_config)
            except ValueError as e:
                batch_logger.info("Can't parse old deploy config for {}".format(job_id))
                batch_logger.exception(e)

            new_deploy_config = {}
            try:
                new_deploy_config = json.loads(_choose_value("deploy_mode"))
            except ValueError as e:
                batch_logger.info("Can't parse new deploy config for {}".format(job_id))
                batch_logger.exception(e)

            for key in new_deploy_config:
                deploy_config[key] = new_deploy_config[key]

            ProcessingJobInfo.objects.filter(job_id=job_id).update(
                processing_type=processing_type,
                code_version=code_version,
                component_type=_choose_value("component_type"),
                job_config=args["job_config"],
                jobserver_config=json.dumps(args["jobserver_config"]),
                cluster_group=_choose_value("cluster_group"),
                cluster_name=_choose_value("cluster_name"),
                deploy_mode=_choose_value("deploy_mode"),
                deploy_config=json.dumps(deploy_config),  # 原为_choose_value('deploy_config')
                updated_by=args["bk_username"],
            )

    @staticmethod
    def create_processing_job_info_v2(**kwargs):
        ProcessingJobInfo.objects.create(**kwargs)

    @staticmethod
    def update_processing_job_info_v2(processing_id, **kwargs):
        ProcessingJobInfo.objects.filter(job_id=processing_id).update(**kwargs)

    @staticmethod
    def update_proc_job_info_job_config(job_id, job_config):
        ProcessingJobInfo.objects.filter(job_id=job_id).update(job_config=json.dumps(job_config))
        return job_id

    @staticmethod
    def update_proc_job_info_deploy_config(job_id, deploy_config):
        ProcessingJobInfo.objects.filter(job_id=job_id).update(deploy_config=json.dumps(deploy_config))
        return job_id

    @staticmethod
    def get_proc_job_info(job_id):
        try:
            return ProcessingJobInfo.objects.get(job_id=job_id)
        except ObjectDoesNotExist as e:
            batch_logger.exception(e)
            return None

    @staticmethod
    def is_proc_job_info(job_id):
        obj = ProcessingJobInfo.objects.filter(job_id=job_id)
        return obj.exists()

    @staticmethod
    def delete_proc_job_info(job_id):
        if ProcessingJobInfoHandler.is_proc_job_info(job_id):
            ProcessingJobInfo.objects.filter(job_id=job_id).delete()

    @staticmethod
    def get_proc_job_info_by_prefix(job_id_prefix):
        # todo 安全起见，暂时不按前缀查询，对目前使用场景下是没有影响的，因为目前不会产生Job的拆分
        # return ProcessingJobInfo.objects.filter(job_id__startswith=job_id_prefix).order_by('-job_id')
        return ProcessingJobInfo.objects.filter(job_id=job_id_prefix).order_by("-job_id")


# -*- coding: utf-8 -*-
import discord
from discord.ext import commands

class AdminCommands(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(hidden=True)
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member : discord.Member,*, reason = "surplus de wati-bizarrerie"):
        """Permet de kick qlqun"""
        await member.send("Yo la frappe \n Tu t'es fait **kick** du wati-serv pour : "+reason +"\n \n CHEH :woman_in_manual_wheelchair:")
        await member.kick(reason = reason)

    @commands.command(aliases=['clear'], hidden=True)
    @commands.has_permissions(manage_messages = True)
    async def clean(self, ctx, number:int):
        if number<=16:
            await ctx.channel.purge(limit=int(number))
        else:
            await ctx.reply(f'Cannot delete more than 15 messages at once', delete_after=10)

    @commands.command(hidden=True)
    @commands.is_owner()
    async def statut(self,ctx,*,statut=''):
        if statut == "":
            await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name = "p!help"))
        else:
            await self.client.change_presence(status=discord.Status.online, activity=discord.Game(kk))
        await ctx.message.delete()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def serverslist(self, ctx):
        letxt = str("```")
        guilds = await self.client.fetch_guilds(limit=150).flatten()
        for server in guilds:
            l = len(server.name)
            letxt += str(server.name) + (30-l)*" " + ":" + str(server.id) + "\n"
        letxt += "```"
        await ctx.author.send(letxt)
        await ctx.message.delete()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def channelslist(self, ctx, serverid):
        """Donne la liste des channels d'un server et leur id"""
        f = open("temp.txt", "w")
        server = self.client.get_guild(int(serverid))
        for channel in server.text_channels:
            l = len(channel.name)
            f.write( channel.name + (30-l)*" " + str(channel.id) + "\n" )
        f.close()
        await ctx.author.send(f"Here's the list of all channles in server : {serverid}",file = discord.File("temp.txt"))
        f = open("temp.txt", "w")
        f.truncate(0)
        f.close()
        await ctx.message.delete()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def getinvite(self, ctx, channelid):
        channel = self.client.get_channel(int(channelid))
        inv = await channel.create_invite()
        await ctx.author.send(inv)
        await ctx.message.delte()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def attachmentslist(self, ctx, channelid):
        """Donne la liste des attachements dans le channel"""
        f = open("temp.txt", "w")
        channel = self.client.get_channel(int(channelid))
        messages = await channel.history(limit=None, oldest_first=True).flatten()
        print(f"je got la listen de longueur : {len(messages)}")
        for message in messages:
            if message.attachments:
                for image in message.attachments:
                    f.write(str(image.url) + "\n")
            elif "http" in message.content:
                for mot in message.content.split():
                    if mot.startswith("http"):
                        f.write(mot + "\n")
        f.close()
        await ctx.author.send(f"Here's the list of all images in channel : {channelid}",file = discord.File("temp.txt"))
        f = open("temp.txt", "w")
        f.truncate(0)
        f.close()
        await ctx.message.delete()

async def setup(bot: commands.Bot):
    await bot.add_cog(AdminCommands(bot))


#!/usr/local/bin/python3

import sys, os, math
from collections import deque

class AOC2022:
    def __init__(s, filename):
        s.f = filename
        s.S1 = 0
        s.S2 = 0

        s.blizzards = [set(), set(), set(), set()] # <>^v
        for r, line in enumerate(open(s.f).read().splitlines()[1:]):
            for c, item in enumerate(line[1:]):
                if item in '<>^v':
                    s.blizzards['<>^v'.find(item)].add((r,c))

        s.r = r
        s.c = c
        s.target = (r,c-1)
        s.lcm = c*r//math.gcd(r,c)
        print('target ',s.target)

    def solve(s):
        targets = [s.target, (-1,0)]
        stage = 0
        Q = deque([(0, -1, 0, stage)])
        seen = set()
        while Q:
            time, cr, cc, stage = Q.popleft()
            time += 1

            for dr, dc in [(0,1), (0,-1), (-1,0), (1,0), (0,0)]:
                nc = cc + dc
                nr = cr + dr

                if (nr,nc) == targets[stage%2]:
                    if stage == 0:
                        print(f'target {(nr,nc)} reached in ', time)
                        s.S1 = time
                    if stage == 2:
                        print(f'target {(nr,nc)} reached in ', time)
                        s.S2 = time
                        return
                    stage += 1

                if (nr < 0 or nc < 0 or nr > s.r or nc > s.c) and ((nr,nc) != (-1,0)) and ((nr,nc) not in targets):
                    continue

                for i, tr, tc in [(0,0,-1), (1,0,1), (2,-1,0), (3,1,0)]:
                    if ((nr - tr * time) % s.r, (nc - tc * time) % s.c) in s.blizzards[i]:
                        break
                else:
                    key = (nr, nc, time % s.lcm, stage)
                    if key in seen:
                        continue

                    seen.add(key)
                    Q.append((time, nr, nc, stage))
#
# #
#

if __name__ == '__main__':
    infile = sys.argv[1] if len(sys.argv) > 1 else 'test.txt'

    if infile == 'test.txt':
        m = AOC2022(infile)
    else:
        m = AOC2022(infile)

    # Part I & II
    m.solve()

    print("------------- A -------------")
    print('S1 ', m.S1)
    print("------------- B -------------")
    print('S2 ', m.S2)
    print("-----------------------------")


import numpy as np
import pandas as pd
import statistics
from sklearn.model_selection import train_test_split
import streamlit as st
import models
import seaborn as sns

from matplotlib import pyplot as plt
from sklearn.tree import DecisionTreeRegressor,plot_tree
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
from validation_functions import regression

def airbnb_regression_prep():
    airbnb_data = pd.read_csv('../xlsx-csv/NYAirbnb/AB_NYC_2019.csv')
    raw_data = airbnb_data
    sns.catplot(x="room_type", y="price", data=raw_data)
    st.pyplot()
    sns.catplot(x="neighbourhood", y="price", data=raw_data)
    st.pyplot()
    sns.catplot(x="neighbourhood_group", y="price", data=raw_data)
    st.pyplot()
    sns.catplot(x="neighbourhood", y="number_of_reviews", data=raw_data)
    st.pyplot()
    sns.catplot(x="neighbourhood_group", y="number_of_reviews", data=raw_data)
    st.pyplot()
    data = airbnb_data.sort_values(["neighbourhood", "room_type"], ascending=(True, True))
    data1 = data.groupby(["neighbourhood", "room_type"])["price"].mean()
    merged_left = pd.merge(left=data, right=data1, how='left', left_on=("neighbourhood", "room_type"),
                           right_on=("neighbourhood", "room_type"))
    merged_left['price_y'] = merged_left['price_y'].astype('category')
    X = merged_left[['room_type', 'neighbourhood_group', 'neighbourhood']]
    y = merged_left[['price_y']]

    X = pd.get_dummies(X)
    return (X, y)

def airbnb_regression():
    X, Y = airbnb_regression_prep()
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.10, random_state=42)
    model = ["-----","OLS", "Decision Tree Regression"]
    option = st.sidebar.selectbox('Machine Learning Model', model)
    if option == "OLS":
        model = models.transcendence_ols(X, Y)
    if option == "Decision Tree Regression":
        model = models.transcendence_dt_regression(X, Y,max_depth=20)
    else:
        pass
    return


from django.conf.urls import url

from . import views

# url namespace
app_name = 'post'  # reverse하는 모든 namespace URL에 앱네임을 지정해줘야 한다, 단 render 함수는 제외
urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    # /post/3/$, /post/35/$
    # 정규표현식에서 매칭된 그룹을 위치인수로 변환하는 방법
    # url(r'(^\d+)/$', views.post_detail),

    # 정규표현식에서 매칭된 그룹을 키워드인수로 변환하는 방법
    # 그룹의 가장 앞부분에 ?P<패턴이름>을 지정
    url(r'^(?P<post_pk>\d+)/$', views.post_detail, name='post_detail'),
    # 위쪽의 결과들과 매칭되지 않을 경우
    url(r'^create/$', views.post_create, name='post_create'),
    url(r'^(?P<post_pk>\d+)/modify/$', views.post_modify, name='post_modify'),

    url(r'^(?P<post_pk>\d+)/delete/$', views.post_delete, name='post_delete'),
    url(r'^(?P<post_pk>\d+)/like-toggle/$', views.post_like_toggle, name='post_like_toggle'),
    url(r'^(?P<post_pk>\d+)/comment/create/$', views.comment_create, name='comment_create'),
    url(r'^comment/(?P<comment_pk>\d+)/modify/$', views.comment_modify, name='comment_modify'),

    url(r'^comment/(?P<comment_pk>\d+)/delete/$', views.comment_delete, name='comment_delete'),

    url(r'^tag/(?P<tag_name>\w+)/$', views.hashtag_post_list, name='hashtag_post_list'),

    url(r'^youtube/search/$', views.youtube_create, name='youtube_create'),
    # url(r'^youtube/create/$', views.youtube_create, name='youtube_create'),
]

# 위쪽의 결과들과 매칭되지 않을 경우
# url(r'^.*/$', views.post_anyway, name='post_anyway'),


# source: https://github.com/PyTorchLightning/pytorch-lightning-bolts (Apache2)

import logging
from collections import Counter
from typing import Any, Dict, Optional, Tuple

import segmentation_models_pytorch as smp
import torchmetrics

import numpy as np
import pandas as pd
import pytorch_lightning as pl
import torch
from deadtrees.loss.gdl import GeneralizedDiceLoss
from deadtrees.loss.gwdl import GeneralizedWassersteinDiceLoss
from deadtrees.loss.losses import (  # GeneralizedDice,   we now use another implementation of GDL
    BoundaryLoss,
    class2one_hot,
    DiceLoss,
    FocalLoss,
)
from deadtrees.network.extra import EfficientUnetPlusPlus, ResUnet, ResUnetPlusPlus
from deadtrees.utils import utils
from deadtrees.visualization.helper import show, show_cm
from omegaconf import DictConfig
from torch import Tensor

log = utils.get_logger(__name__)

def concat_extra(
    img: Tensor, mask: Tensor, distmap: Tensor, lu: Tensor, stats, *, extra
) -> Tuple[Tensor]:
    extra_imgs, extra_masks, extra_distmaps, extra_lus, extra_stats = list(zip(*extra))
    img = torch.cat((img, *extra_imgs), dim=0)
    mask = torch.cat((mask, *extra_masks), dim=0)
    distmap = torch.cat((distmap, *extra_distmaps), dim=0)
    lu = torch.cat((lu, *extra_lus), dim=0)
    stats.extend(sum(extra_stats, []))
    return img, mask, distmap, lu, stats

def create_combined_batch(
    batch: Dict[str, Any]
) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
    img, mask, distmap, lu, stats = batch["main"]

    # grab extra datasets and concat tensors
    extra = [v for k, v in batch.items() if k.startswith("extra")]
    if extra:
        img, mask, distmap, lu, stats = concat_extra(
            img, mask, distmap, lu, stats, extra=extra
        )
    return img, mask, distmap, lu, stats

class SemSegment(pl.LightningModule):  # type: ignore
    def __init__(self, network: DictConfig, training: DictConfig):
        super().__init__()

        architecture = network.architecture.lower().strip()
        if architecture == "unet":
            Model = smp.Unet
        elif architecture in ["unetplusplus", "unet++"]:
            Model = smp.UnetPlusPlus
        elif architecture == "resunet":
            Model = ResUnet
        elif architecture in ["resunetplusplus", "resunet++"]:
            Model = ResUnetPlusPlus
        elif architecture in ["efficientunetplusplus", "efficientunet++"]:
            Model = EfficientUnetPlusPlus
        else:
            raise NotImplementedError(
                "Currently only Unet, ResUnet, Unet++, ResUnet++, and EfficientUnet++ architectures are supported"
            )

        # Model does not accept "architecture" as an argument, but we need to store it in hparams for inference
        # TODO: cleanup?
        clean_network_conf = network.copy()
        del clean_network_conf.architecture
        del clean_network_conf.losses
        n_classes = len(clean_network_conf.classes)
        del clean_network_conf.classes

        self.model = Model(**clean_network_conf, classes=n_classes)

        if clean_network_conf.encoder_weights is None:
            log.info("Initializing unset weights with Kaiming")
            self.model.apply(initialize_weights)
        self.encoder_weights = clean_network_conf.encoder_weights

        self.save_hyperparameters()

        self.classes = self.hparams["network"]["classes"]
        self.classes_int = list(range(len(self.classes)))
        self.classes_int_wout_bg = [c for c in self.classes_int if c != 0]

        self.in_channels = self.hparams["network"]["in_channels"]

        # losses
        self.dice_loss = None
        self.focal_loss = None
        self.boundary_loss = None

        # parse loss config
        self.initial_alpha = 0.01  # make this a hyperparameter and/ or scale with epoch
        self.boundary_loss_ramped = False

        assert (
            ("GDICE" in network.losses) and (("DICE" in network.losses))
        ) is False, f"Only GDICE _OR_ DICE allowed {network.losses}"

        for loss_component in network.losses:
            if loss_component == "GDICE":
                # NOTE: we now use another implementation of GDL
                # self.dice_loss = GeneralizedDice(idc=self.classes_int)
                self.dice_loss = GeneralizedDiceLoss()
            elif loss_component == "GWDICE":
                dist_mat = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [1.0, 0.5, 0.0]])
                if self.classes_int == 2:
                    dist_mat = dist_mat[0:2, 0:2]
                self.dice_loss = GeneralizedWassersteinDiceLoss(
                    dist_matrix=dist_mat,
                )
            elif loss_component == "DICE":
                # This the only required loss term
                self.dice_loss = DiceLoss(idc=self.classes_int_wout_bg)
            elif loss_component == "FOCAL":
                self.focal_loss = FocalLoss(idc=self.classes_int, gamma=2)
            elif loss_component == "BOUNDARY":
                self.boundary_loss = BoundaryLoss(idc=self.classes_int_wout_bg)
            elif loss_component == "BOUNDARY-RAMPED":
                self.boundary_loss = BoundaryLoss(idc=self.classes_int_wout_bg)
                self.boundary_loss_ramped = True
            else:
                raise NotImplementedError(
                    f"The loss component <{loss_component}> is not recognized"
                )

        log.info(f"Losses: {network.losses}")

        # checks: we require GDICE!
        assert self.dice_loss is not None

        self.dice_metric = smp.utils.metrics.Fscore(
            ignore_channels=[0],
        )

        self.dice_metric_with_bg = smp.utils.metrics.Fscore()

        self.stats = {
            "train": Counter(),
            "val": Counter(),
            "test": Counter(),
        }

    @property
    def alpha(self):
        """blending parameter for boundary loss - ramps from 0.01 to 0.99 in 0.01 steps by epoch"""
        return min((self.current_epoch + 1) * self.initial_alpha, 0.99)

    def get_progress_bar_dict(self):
        """Hack to remove v_num from progressbar"""
        tqdm_dict = super().get_progress_bar_dict()
        if "v_num" in tqdm_dict:
            del tqdm_dict["v_num"]
        return tqdm_dict

    def calculate_loss(
        self, y_hat: Tensor, y: Tensor, stage: str, distmap: Optional[Tensor] = None
    ) -> Tensor:
        """calculate compound loss"""
        loss, loss_gd, loss_bd, loss_fo = 0, None, None, None

        if self.dice_loss:
            if isinstance(self.dice_loss, GeneralizedWassersteinDiceLoss):
                # hack to make gwdice work...
                loss_gd = self.dice_loss(y_hat, torch.argmax(y, dim=1))
            else:
                loss_gd = self.dice_loss(y_hat, y)

            if torch.isnan(loss_gd) or torch.isinf(loss_gd):
                log.warn("Train dice loss is NaN! What is going on?")

            self.log(f"{stage}/dice_loss", loss_gd, on_step=False, on_epoch=True)
            loss += loss_gd

        if self.boundary_loss and distmap is not None:
            loss_bd = self.boundary_loss(y_hat, distmap)
            self.log(f"{stage}/boundary_loss", loss_bd, on_step=False, on_epoch=True)
            loss += self.alpha * loss_bd if self.boundary_loss_ramped else loss_bd

        if self.focal_loss:
            loss_fo = self.focal_loss(y_hat, y)
            self.log(f"{stage}/focal_loss", loss_fo, on_step=False, on_epoch=True)
            loss += loss_fo

        self.log(f"{stage}/total_loss", loss, on_step=False, on_epoch=True)

        return loss

    def log_metrics(self, y_hat: Tensor, y: Tensor, *, stage: str):
        dice_score = self.dice_metric(y_hat, y)
        dice_score_with_bg = self.dice_metric_with_bg(y_hat, y)
        self.log(f"{stage}/dice", dice_score, on_step=False, on_epoch=True)
        self.log(
            f"{stage}/dice_with_bg", dice_score_with_bg, on_step=False, on_epoch=True
        )

    def training_step(self, batch, batch_idx):

        img, mask, distmap, _, stats = create_combined_batch(batch)

        logits = self.model(img)
        y = class2one_hot(mask, K=len(self.classes_int))
        y_hat = logits.softmax(dim=1)

        loss = self.calculate_loss(y_hat, y, "train", distmap=distmap)

        if torch.isnan(loss) or torch.isinf(loss):
            log.warn("Train loss is NaN! What is going on?")
            return None

        self.log_metrics(y_hat, y, stage="train")

        # track training batch files
        self.stats["train"].update([x["file"] for x in stats])

        return loss

    def validation_step(self, batch, batch_idx):

        img, mask, distmap, lu, stats = create_combined_batch(batch)

        logits = self.model(img)
        y = class2one_hot(mask, K=len(self.classes_int))
        y_hat = logits.softmax(dim=1)

        loss = self.calculate_loss(y_hat, y, stage="val", distmap=distmap)

        self.log_metrics(y_hat, y, stage="val")

        if batch_idx == 0:
            sample_chart = show(
                x=img.cpu(),
                y=mask.cpu(),
                y_hat=y_hat.cpu(),
                n_samples=min(img.shape[0], 8),
                stats=stats,
                dpi=72,
                display=False,
            )
            for logger in self.logger:
                if isinstance(logger, pl.loggers.wandb.WandbLogger):
                    import wandb

                    logger.experiment.log(
                        {
                            "sample": wandb.Image(
                                sample_chart,
                                caption=f"Sample-{self.trainer.global_step}",
                            )
                        },
                        commit=False,
                    )

        # track validation batch files
        self.stats["val"].update([x["file"] for x in stats])

        return {
            "val_loss": loss,
            "target": mask,
            "prediction": y_hat.argmax(dim=1),
            "lu": lu,
        }

    def test_step(self, batch: Tuple[Tensor], batch_idx) -> Dict[str, Any]:
        img, mask, _, lu, stats = batch

        logits = self.model(img)
        y = class2one_hot(mask, K=len(self.classes_int))
        y_hat = logits.softmax(dim=1)

        self.log_metrics(y_hat, y, stage="test")

        # track validation batch files
        self.stats["test"].update([x["file"] for x in stats])

        return {"target": mask, "prediction": y_hat.argmax(dim=1), "lu": lu}

    def validation_epoch_end(self, outputs: Dict[str, Any]):
        prediction = torch.cat([tmp["prediction"] for tmp in outputs])
        target = torch.cat([tmp["target"] for tmp in outputs])

        # --- masked cm, flatten all tensors, subset by forest pixels
        lu = torch.ravel(torch.cat([tmp["lu"] for tmp in outputs]))
        prediction_masked = torch.ravel(prediction)[lu == 1]
        target_masked = torch.ravel(target)[lu == 1]

        confusion_matrix = torchmetrics.functional.confusion_matrix(
            prediction, target, normalize="true", num_classes=len(self.classes)
        )

        confusion_matrix_masked = torchmetrics.functional.confusion_matrix(
            prediction_masked,
            target_masked,
            normalize="true",
            num_classes=len(self.classes),
        )

        dfs = {}
        for label, cm in zip(
            ["cm_norm", "cm_norm_masked"], [confusion_matrix, confusion_matrix_masked]
        ):
            dfs[label] = pd.DataFrame(
                cm.detach().cpu().numpy(),
                index=self.classes,
                columns=self.classes,
            )

        cm_chart = show_cm(dfs["cm_norm"], dfs["cm_norm_masked"], dpi=72, display=False)

        for logger in self.logger:
            if isinstance(logger, pl.loggers.wandb.WandbLogger):
                import wandb

                logger.experiment.log(
                    {
                        "Confusion matrix (Val)": wandb.Image(
                            cm_chart,
                            caption=f"CM-Val-Norm-{self.trainer.global_step}",
                        )
                    },
                    commit=False,
                )

    def test_epoch_end(self, outputs: Dict[str, Any]):

        # --- original cm
        prediction = torch.cat([tmp["prediction"] for tmp in outputs])
        target = torch.cat([tmp["target"] for tmp in outputs])

        # --- masked cm, flatten all tensors, subset by forest pixels
        lu = torch.ravel(torch.cat([tmp["lu"] for tmp in outputs]))
        prediction_masked = torch.ravel(prediction)[lu == 1]
        target_masked = torch.ravel(target)[lu == 1]

        confusion_matrix = torchmetrics.functional.confusion_matrix(
            prediction, target, normalize="true", num_classes=len(self.classes)
        )

        confusion_matrix_px = torchmetrics.functional.confusion_matrix(
            prediction, target, num_classes=len(self.classes)
        )

        confusion_matrix_masked = torchmetrics.functional.confusion_matrix(
            prediction_masked,
            target_masked,
            normalize="true",
            num_classes=len(self.classes),
        )

        confusion_matrix_masked_px = torchmetrics.functional.confusion_matrix(
            prediction_masked, target_masked, num_classes=len(self.classes)
        )

        dfs = {}
        for label, cm in zip(
            ["cm_norm", "cm_px", "cm_norm_masked", "cm_px_masked"],
            [
                confusion_matrix,
                confusion_matrix_px,
                confusion_matrix_masked,
                confusion_matrix_masked_px,
            ],
        ):
            dfs[label] = pd.DataFrame(
                cm.detach().cpu().numpy(),
                index=self.classes,
                columns=self.classes,
            )

        cm_chart = show_cm(dfs["cm_norm"], dfs["cm_norm_masked"], dpi=72, display=False)
        cm_chart_px = show_cm(dfs["cm_px"], dfs["cm_px_masked"], dpi=72, display=False)

        for logger in self.logger:
            if isinstance(logger, pl.loggers.wandb.WandbLogger):
                import wandb

                logger.experiment.log(
                    {
                        "Confusion Matrix (Test) - Normalized": wandb.Image(
                            cm_chart,
                            caption=f"CM-Test-Norm-{self.trainer.global_step}",
                        ),
                        "Confusion Matrix (Test) - Pixel": wandb.Image(
                            cm_chart_px,
                            caption=f"CM-Test-Px-{self.trainer.global_step}",
                        ),
                    },
                    commit=False,
                )

        log.info(f"CM - DEFAULT - NORMALIZED: {dfs['cm_norm'].to_string()}")
        log.info(f"CM - FORESTONLY - NORMALIZED: {dfs['cm_norm_masked'].to_string()}")
        log.info(f"CM - DEFAULT - PIXEL: {dfs['cm_px'].to_string()}")
        log.info(f"CM - FORESTONLY - PIXEL: {dfs['cm_px_masked'].to_string()}")

    def teardown(self, stage=None) -> None:
        log.debug(f"len(stats_train): {len(self.stats['train'])}")
        pd.DataFrame.from_records(
            list(dict(self.stats["train"]).items()), columns=["filename", "count"]
        ).to_csv("train_stats.csv", index=False)

        log.debug(f"len(stats_val): {len(self.stats['val'])}")
        pd.DataFrame.from_records(
            list(dict(self.stats["val"]).items()), columns=["filename", "count"]
        ).to_csv("val_stats.csv", index=False)

    def configure_optimizers(self):
        opt = torch.optim.Adam(
            self.parameters(),
            lr=self.hparams.training.learning_rate,
            # eps=1e-6,
        )
        sch = torch.optim.lr_scheduler.CosineAnnealingLR(
            opt, T_max=self.hparams.training.cosineannealing_tmax
        )
        return [opt], [sch]

def initialize_weights(m):
    if getattr(m, "bias", None) is not None:
        torch.nn.init.constant_(m.bias, 0)
    if isinstance(m, (torch.nn.Conv2d, torch.nn.Linear)):
        torch.nn.init.kaiming_normal_(m.weight)
    for c in m.children():
        initialize_weights(c)


import logging

from microserviceutil.migrations.migrate_mongo_pro_to_dev import MigrateMongoProToDev

class Migrations:
    try:
        JOBS = {
            "mongo_pro_to_dev": MigrateMongoProToDev
        }

        def __init__(self, args):
            # check the number of arguments
            if len(args) >= 2:
                self.job_name = args[0]
                self.params = args[1:]

                if self.job_name in self.JOBS:
                    job = self.JOBS[self.job_name](self.params)
                    job.run_job()
    except Exception as error:
        logging.error(error)


import sys
import socket
import threading
import argparse

MAIN_PORTS = list(range(1, 1024))

def port_scan(host, port, protocol):
    s = socket.socket(socket.AF_INET, protocol)
    s.settimeout(10)
    if s.connect_ex((host, int(port))) == 0:
        print(f"Port {port}/{protocol} open")

def multi_process(host, port_scan, port, protocol):
    t = threading.Thread(target=port_scan, args=(host, str(port), protocol))
    t.start()

def write_results_to_file(results, filename):
    with open(filename, "w") as f:
        for result in results:
            f.write(result + "\n")

def main():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "-m",
        "--mainports",
        help="Perform a Scan on the 1000 main TCP ports",
        action="store_true",
    )
    group.add_argument(
        "-p", "--ports", help="Specify ports for scan (separated by comma or hyphen)"
    )
    parser.add_argument(
        "-r", "--range", help="Specify a range of ports to scan (e.g. 1-1024)"
    )
    parser.add_argument(
        "-o", "--output", help="Specify a file to output the results to"
    )
    parser.add_argument("ip", help="IP or domain for scanning")
    args = parser.parse_args()

    try:
        host = socket.gethostbyname(args.ip)
    except socket.gaierror:
        print("Invalid host, try again")
        sys.exit(1)

    def parse_ports(args):
        if "-" in args:
            start, end = [int(i) for i in args.split("-")]
            ports = list(range(start, min(end + 1, 65536)))
        else:
            ports = [int(p) for p in args.split(",")]
            ports = [p if p <= 65535 else 65535 for p in ports]
        protocols = [socket.SOCK_STREAM] * len(ports)
        return ports, protocols

    def parse_range(args):
        start, end = args.split("-")
        ports = range(int(start), min(int(end) + 1, 65536))
        protocols = [socket.SOCK_STREAM] * len(ports)
        return ports, protocols

    def parse_mainports(args):
        ports = MAIN_PORTS
        protocols = [socket.SOCK_STREAM] * len(ports)
        return ports, protocols

    def parse_args(args):
        parse_functions = {
            "ports": parse_ports,
            "mainports": parse_mainports,
            "range": parse_range,
        }

        for arg, func in parse_functions.items():
            if getattr(args, arg):
                return func(getattr(args, arg))

        print("Args error")
        sys.exit(1)

    ports, protocols = parse_args(args)

    results = []
    for i, port in enumerate(ports):
        protocol = protocols[i]
        multi_process(host, port_scan, port, protocol)
        results.append(f"Port {port}/{protocol} closed")

    if args.output:
        write_results_to_file(results, args.output)

    sys.exit(0)

if __name__ == "__main__":
    main()


import serial
import socket

arduino= serial.Serial('/dev/ttyACM0' , 9600)
while True:
    a=arduino.readline()
    s=socket.socket()
    s.connect(("169.254.139.148", 8888))
    s.send(a)
    s.close()
arduino.close()

Codi Electrònica/Codi Referència/Codi Raspberry/cliente.py


from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client
from django.conf import settings

class FastDFSStorage(Storage):
    """自定义Django存储系统的类"""
    # 在创建client对象的时候，我们通过原始的/etc/fdfs/client.conf找不到conf'文件，所以我们需要奖这个文件复制到当前项目中
    #client = Fdfs_client('./client.conf')的存在 耦合性太强，所以仿照FileSystemStorage这个类的初始化方法

    def __init__(self,client_conf=None,server_ip=None):    # 初始化方法，外界可以数据，可以不穿，传入数据， 就用，不传入，用自己的
        if client_conf is None:
            client_conf = settings.CLIENT_CONF
        self.client_conf = client_conf

        if server_ip is None:
            server_ip = settings.SERVER_IP
        self.server_ip = server_ip

    def _open(self,name,mode='rb'):
        """读取文件时使用，此处是存储到fasfdfs，所以不需要逻辑"""
        pass
    def _save(self,name,coneten):
        """存储文件时，name表示要上传的文件的名字，content表示File类型的对象，read方法可以读取内容"""
        # 创建client对象
        client = Fdfs_client(self.client_conf)
        # 获取上传的内容
        filter_data = coneten.read()
        # client获取文件内容
        try:
            ret = client.upload_by_buffer(filter_data)
        except Exception as e:
            print(e)  # 方便自己做调试
            raise       # 抓获到什么异常，就展示什么异常
        # 判断是否上传成功   注意  Upload successed.  后面有个点  不要i忘了那个点
        if ret.get('Status') == 'Upload successed.':
            # 上传成功,读取file_id,完成存储到mysql
            file_id = ret.get('Remote file_id')
            # 如果运维当前在操作GoodsCategory模型类,那么我们的Storage会自动的把返回的file_id,存储到GoodsCategory模型类对应的字段中
            return file_id
        else:
            # 上传失败 # 把错误暴露出来
            raise Exception ('上传失败')

    # 因为我们所有逻辑是运用django将图片存储到fdfs中，所以我们要重写exists方法，
    # 在文档中有提到：如果提供的名称所引用的文件在文件系统中存在，则返回True，否则如果这个名称可用于新文件，返回False。
    # 所以我们要重写方法，让它永远false，这样才能保证可以将文件存到fdfs中，django中永远没有数据
    def exists(self, name):
        """判断是否存储数据，有True，没有False"""
        return False   #告诉django文件不存在，可以存储文件

    def url(self, name):
        """可以返回要下载的文件的全路径，提供用户下载时候使用的"""
    #   返回URL，通过它可以访问到name所引用的文件   如果要下载就可以调用url方法，可以得到全路径
    # name就是要下载的文件名字，将来会把从数据库中查询处理来的fil_id传入到url方法中
    # name == 'group1/M00/00/00/wKh9hVqWFo - AffzqAAV6GVaZsqU005.jpg'
    # http://192.168.125.133:8888/group1/M00/00/00/wKh9hVqWFo - AffzqAAV6GVaZsqU005.jpg
        return self.server_ip+name

    """
>>> from fdfs_client.client import Fdfs_client
>>> client = Fdfs_client('/etc/fdfs/client.conf')
>>> ret = client.upload_by_filename('test')
>>> ret
{'Group name':'group1','Status':'Upload successed.', 'Remote file_id':'group1/M00/00/00/
	wKjzh0_xaR63RExnAAAaDqbNk5E1398.py','Uploaded size':'6.0KB','Local file name':'test'
	, 'Storage IP':'192.168.243.133'}
        """


from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.yaml")  # build a new model from scratch

#%%
# Use the model
model.train(data=r"C:\Users\kamra\DataspellProjects\yolov8_test\datasets\typhoon\data.yaml", epochs=100)  # train the model
metrics = model.val()  # evaluate model performance on the validation set
success = model.export(format="onnx")  # export the model to ONNX format


from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QDialog
import sys
import os
import pickle
import math
import time
import SenderGBN

class Sender(QDialog):

    def __init__(self):
        super(Sender, self).__init__()
        uic.loadUi("Sender.ui", self)
        self.pushButton.clicked.connect(self.sent_pressed)
        # self.lineEdit_2.setText("192.168.10.166")
        # self.lineEdit.setText("5010")
        self.pack_size = 0
        self.pocket_lost = 0
        self.window_size = 0
        self.lineEdit_3.setText(os.path.dirname(sys.argv[0])+r'/COSC635_P2_DataSent.txt')

    def sent_pressed(self):
        self.pack_size = int(self.lineEdit_4.text())
        self.pocket_lost = int(self.lineEdit_5.text())
        if self.radioButton.isChecked():
            print("GBN sent")
            self.window_size = 4
        else:
            print("SAW sent")
            self.window_size = 1

        file = open(self.lineEdit_3.text(), 'r', encoding='utf8')
        total_pack = int(math.ceil(sys.getsizeof(file.read()) / self.pack_size))
        # making all the data package along with part number and total pack number
        i = 1
        all_msg = []
        with open(self.lineEdit_3.text(), 'r', encoding='utf8') as in_file:
            payload = in_file.read(self.pack_size)
            while payload:
                msg_pickle = pickle.dumps({"part": i, "total_pack": total_pack, "msg": payload})
                payload = in_file.read(self.pack_size)
                all_msg.append(msg_pickle)
                i += 1
        print(all_msg)
        start_time = time.time()
        client_1 = SenderGBN.ClientServer(
            self.pocket_lost, all_msg, self.window_size, self.lineEdit_2.text(), int(self.lineEdit.text()))
        client_1.client_sent_group_pack()
        client_1.close_socket()
        msgbox = QtWidgets.QMessageBox(self)
        msgbox.setWindowTitle("Summary")
        msgbox.setText(f"File transfer completed:\n"
                       f"Total time used: {round(time.time() - start_time, 4)} seconds \n"
                       f"Total package: {total_pack} \n"
                       f"The pack was lost: {client_1.connection_fail - 1} times. \n"
                       f"Package Lost rate: {self.pocket_lost}%\n"
                       f"Each package size: {self.pack_size} bytes")
        msgbox.exec()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    qt_app = Sender()
    qt_app.show()
    sys._excepthook = sys.excepthook

    def exception_hook(exctype, value, traceback):
        print(exctype, value, traceback)
        sys.excepthook(exctype, value, traceback)
        sys.exit(1)

    sys.excepthook = exception_hook

    app.exec_()


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

# Model.py

from scipy.optimize import minimize
import wx
from math import exp

from Corridor import *
from Line import *
from OD import *
from Parameters import *

class Model():
	""" This class contains all the objects and data needed to run the model.

	It enables to clearly separate the user interface and the visualization of
	the results from the model. It also manages the interactions between the different
	components of the model.
	"""

	def __init__(self, main_window=None):
		self.main_window = main_window
		self.param = Parameters()

		# Defining the initial corridor which is the reference situation
		self.length = 15
		self.typo = 0
		self.reference = Corridor("Référence", self.length, self.typo)

		# Defining some lists that will contain the scenarios and their results
		# Structure :
		# 	- optimizations : [[corridor with lines which won't be optimized,
		#						[lines to optimize],
		#						elasticity (boolean)],...]
		#	- simulations : [[corridor to simulate, elasticity (boolean)],...]
		#	- optimizations_results : [corridor, ...]
		#	- simulations_results : [corridor, ...]
		self.optimizations = []
		self.simulations = []
		self.optimizations_results = []
		self.simulations_results = []

	def SetLength(self, length):
		""" Modify the length of all corridors. """
		self.reference.SetLength(length)
		for opt in self.optimizations:
			opt[0].SetLength(length)
		for sim in self.simulations:
			sim[0].SetLength(length)

	def SetOD(self, i, j, demand):
		""" Modify the demand for the given OD for all corridors. """
		self.reference.GetOD(i,j).demand = demand
		for opt in self.optimizations:
			opt[0].GetOD(i,j).demand = demand
		for sim in self.simulations:
			sim[0].GetOD(i,j).demand = demand

	def SetTypology(self, typo):
		""" Modify the typo of the reference corridor.

		It will erase all lines of the reference and all scenarios. """
		self.reference.SetTypology(typo)
		self.optimizations = []
		self.simulations = []
		self.optimizations_results = []
		self.simulations_results = []
		if self.main_window != None:
			self.main_window.simulation_panel.UpdateUI()
			self.main_window.DeleteResults()

	def Calculate(self):
		""" This class computes the model for the different scenarios and
		give the results to the user interface.
		"""

		# Opening a progess dialog and creating variables to estimate the progression
		if self.main_window != None:
			self.progress_dialog = wx.ProgressDialog(	"Simulation et optimisation",
														"Calculs en cours")
			self.progress_dialog.Update(0)
		self.progress_total = 1 + len(self.simulations) + len(self.optimizations)
		self.progress_count = 0

		# Deleting all former results
		self.optimizations_results = []
		self.simulations_results = []
		if self.main_window != None:
			self.main_window.DeleteResults()
		self.results_success = [] # Elements : [name, success, message]

		# Computing the model
		self.Simulate()
		self.Optimize()

		if self.main_window != None:
			self.main_window.InsertResults()
			self.progress_dialog.Update(100)
			if self.results_success:
				self.main_window.SuccessDialog(self.results_success)

	def Simulate(self):
		""" Computes all the simulation scenarios.

		Uses self.simulations as input and self.simulations_results as output.
		"""
		for sim in self.simulations:
			corridor = sim[0]
			elasticity = sim[1]
			# If demand is elastic, we calculate the new demand
			if elasticity:
				corridor.demand = self.reference.GetElasticDemand(self.param, corridor)
			self.simulations_results.append(corridor)
			# Updating progress dialog
			self.progress_count += 1
			if self.main_window != None:
				self.progress_dialog.Update(100*self.progress_count/self.progress_total)

	def	Optimize(self):
		""" Computes all the optimization scenarios.

		Uses self.optimizations as input and self.optimizations_results as output.
		"""
		for opt in self.optimizations:
			corridor = opt[0] # Corridor with lines which won't get optimized
			lines = opt[1] # List of lines which will be optimized
			elasticity = opt[2]

			def ObjectiveFunction(x):
				new_corridor = corridor.Duplicate("")

				# The argument x is a vector that contains the optimization variables
				# in their order of appearance in the list "lines".
				c = 0 # A counter to identify the elements of x

				# Construction of the set of lines for the new corridor
				# with the data contained in list lines and vector x.
				for i in range(len(lines)):
					# Frequency
					if lines[i].opt[0] == 0:
						f = lines[i].f
					else:
						f = x[c]
						c += 1

					# Stop spacing
					s = []
					for j in range(corridor.n):
						if lines[i].opt[1][j] == 0:
							s.append(lines[i].s[j])
						else:
							s.append(x[c])
							c += 1

					line = Line(new_corridor,
								lines[i].m,
								f,
								s,
								lines[i].v,
								lines[i].k,
								lines[i].dt,
								lines[i].price,
								lines[i].name,
								lines[i].opt)
					new_corridor.lines.append(line)

				for l in corridor.lines:
					new_corridor.lines.append(l.Duplicate())

				if elasticity:
					new_corridor.demand = self.reference.GetElasticDemand(self.param, new_corridor)

				# Capacity constraints are managed by giving a malus
				# if a line is too crowded.
				malus = 0
				for l in new_corridor.lines:
					max_load = new_corridor.Demand(self.param, l) / new_corridor.TotalDemand() * \
									max(new_corridor.MaxLoadA(), new_corridor.MaxLoadB())
					capacity = l.f * l.k
					if max_load > capacity:
						malus += (max_load - capacity)**2

				if elasticity:
					r = -self.reference.TotalSurplus(self.param, new_corridor) + malus
				else:
					r = new_corridor.TotalCost(self.param) + malus
				return r

			# Defining the optimization program
			x0 = []
			bounds = []
			for i in range(len(lines)):
				# Frequency
				if lines[i].opt[0] == 1:
					x0.append(lines[i].f)
					bounds.append(lines[i].cons[0])
				# Stop spacing
				for j in range(corridor.n):
					if lines[i].opt[1][j] == 1:
						x0.append(lines[i].s[j])
						bounds.append(lines[i].cons[1][j])

			if len(x0) != 0:
				r = minimize(ObjectiveFunction, x0, method="L-BFGS-B", bounds=bounds)
				self.results_success.append([corridor.name, r.success, r.message])

				# Construction of the set of lines for the new corridor
				# with the data contained in list lines and optimized vector x.
				c = 0
				new_corridor = corridor.Duplicate(corridor.name)

				# Frequency
				for i in range(len(lines)):
					if lines[i].opt[0] == 0:
						f = lines[i].f
					else:
						f = r.x[c]
						c += 1
					# Stop spacing
					s = []
					for j in range(corridor.n):
						if lines[i].opt[1][j] == 0:
							s.append(lines[i].s[j])
						else:
							s.append(r.x[c])
							c += 1

					line = Line(corridor,
								lines[i].m,
								f,
								s,
								lines[i].v,
								lines[i].k,
								lines[i].dt,
								lines[i].price,
								lines[i].name,
								lines[i].opt)
					new_corridor.lines.append(line)

				for l in corridor.lines:
					new_corridor.lines.append(l.Duplicate())
				if elasticity:
					new_corridor.demand = self.reference.GetElasticDemand(self.param, new_corridor)

				# Updating progress dialog
				self.progress_count += 1
				if self.main_window != None:
					self.progress_dialog.Update(100*self.progress_count/self.progress_total)

				self.optimizations_results.append(new_corridor)