from email.mime import image
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from math import ceil
from pathlib import Path
import argparse
import imageio.v2 as imageio
import os

fontsize = 24

def f(data,X,Y,T):
    Z = []
    z = []
    for y in Y:
        z = []
        for x in X:
            name = "V["+str(x)+","+str(y)+"]"
            z.append(data[name][T])
        Z.append(z)
    return Z

def RVTaxes(ax,ur = False):
    ax.set_xlabel(r'Temperature  (K)',fontsize=fontsize)
    ax.set_ylabel(r'$\rho(T)$  (a.u.)',fontsize=fontsize)
    ax.set_yticks([])
    ax.legend(loc='upper right',prop={'size': 10})
    ax.tick_params(axis='x', labelsize=16)
    if ur == False:
        ax.set_xlim(0,300)
        ax.legend(fontsize='16')

def contouraxes(ax,heatmap,Nx,Ny,axtitle=1,gift = False,mixed = 'none'):
    ax.set_xticks([1,ceil(Nx/2),Nx])
    ax.set_yticks([1,ceil(Ny/2),Ny])
    if (gift == False)&(mixed == 'none'):
        ax.tick_params(axis='x', labelsize=fontsize)
        ax.tick_params(axis='y', labelsize=fontsize)
        if axtitle == True:
            ax.set_xlabel('a-b plane',fontsize=fontsize)
            ax.set_ylabel('c-axis',fontsize=fontsize)
        cbr = plt.colorbar(heatmap,ax=ax)
        cbr.set_label('Voltage (V)', rotation=270,fontsize=fontsize)
        cbr.ax.get_yaxis().set_ticks([-0.5,0,0.5])
        cbr.ax.tick_params(labelsize=fontsize)
        cbr.ax.set_yticklabels(['-0.5','0','0.5'])
    # elif(gift==True):
    #     ax.set_xticks([])
    #     ax.set_yticks([])
    #     ax.tick_params(axis='x')
    #     ax.tick_params(axis='y')
    #     if axtitle > 0:
    #         ax.set_xlabel('a-b plane')
    #         ax.set_ylabel('c-axis')
        # cbr = plt.colorbar(heatmap,ax=ax)
        # cbr.set_label('Voltage (V)', rotation=270)
        # cbr.ax.get_yaxis().set_ticks([-0.5,0,0.5])
        # cbr.ax.set_yticklabels(['-0.5','0','0.5'])
    else:
        if axtitle == 2:
            ax.set_ylabel('a-b plane')
            ax.set_xlabel('c-axis')
        elif axtitle > 0:
            ax.set_xlabel('a-b plane')
            ax.set_ylabel('c-axis')
        if(mixed == 'top'):
            ax.set_xticks([])
            ax.set_yticks([])
        if(mixed == 'left'):
            ax.set_xticks([])
            if (gift==False):
                ax.set_ylabel(r'$\bf{300 K}$' '\n' 'c-axis')
        if(mixed == 'corner'):
            if (gift==False):
                ax.set_ylabel(r'$\bf{2 K}$' '\n' 'c-axis')
        if(mixed == 'bottom'):
            ax.set_yticks([])

def loadsimdata(type,Nx,Ny,input,output,experimental = 0,pressure = 0,implicit = False,folder=''):
    files = []
    data = []
    path = '../../Data/Sr327_Simulator/'
    if implicit == True:
        path = '../../Data/Sr327_ImplicitSimulator/'
    path = path + folder + '/'
    L_check = Path(path+'Sr327_L_'+str(input)+'_to_'+str(output)+'.dat')
    if (type == 'c') | (type == 'm'):
        files.append(path+'Sr327_c_'+str(input)+'_to_'+str(output)+'.dat')
    if (type == 'd') | (type == 'm'):
        files.append(path+'Sr327_d_'+str(input)+'_to_'+str(output)+'.dat')
    if (type == 'x1') | (type == 'm'):
        files.append(path+'Sr327_x1_'+str(input)+'_to_'+str(output)+'.dat')
    if (type == 'x2'):
        files.append(path+'Sr327_x2_'+str(input)+'_to_'+str(output)+'.dat')
    if (type == 'L')| ((type == 'm')&(L_check.is_file() == True)):
        files.append(path+'Sr327_L_'+str(input)+'_to_'+str(output)+'.dat')
    for fname in files:
        data.append(pd.read_csv(fname,header=[0]))
    return data, L_check

def main(input,output,Nx,Ny,type = 'c',contours = 0,savefile = True, experimental = 0, gif = False, pressure = 0, implicit = False,folder=''):
    if folder == '':
        if experimental == 0:
            if pressure == 0:
                folder = str(Nx)+'x'+str(Ny)+'DAT'
            else:
                folder = str(pressure)+'P'+str(Nx)+'x'+str(Ny)+'DAT'
        else:
            folder = 'test'+str(experimental)+'_'+str(Nx)+'x'+str(Ny)+'DAT'

    inputpin = "V["+str(input+1)+",1]"; outputpin = "V["+str(output+1)+","+str(Ny)+"]"
    mirrorinputpin = "V["+str(Nx-input)+",1]"; mirroroutputpin = "V["+str(Nx-output)+","+str(Ny)+"]"
    leftinput = "V[1,1]"; leftoutput = "V[1,"+str(Ny)+"]"
    middleinput = "V["+str(ceil(Nx/2))+",1]"; middleoutput = "V["+str(ceil(Nx/2))+","+str(Ny)+"]"
    righttinput = "V["+str(Nx)+",1]"; rightoutput = "V["+str(Nx)+","+str(Ny)+"]"
    data, L_check = loadsimdata(type,Nx,Ny,input,output,experimental,pressure,implicit,folder)

    if (contours == 0)&(gif==False):
        fig = plt.figure(figsize=(10,8))
        ax = fig.add_axes([0.1,0.1,0.85,0.85])

        if (type =='m')|(type == 'c'):
            ax.scatter(data[0]["T"],0.36*data[0]["rx"],color='red',s=4.0,label= r'$\rho_{ab}$(T)')
            ax.scatter(data[0]["T"],0.36*data[0]["ry"],color='blue',s=4.0,label= r'$\rho_c$(T)')
            # ax.scatter(data[0]["T"],data[0]["ry"],color='blue',s=1.0,label='ry(T)')
        if (type == 'c')| (type == 'd'):
            ax.scatter(data[0]["T"],2.65*(data[0][leftoutput]-data[0][leftinput])/data[0]["I"],color='purple',s=8.0,marker='.',label='Left Edge')
            ax.scatter(data[0]["T"],2.65*(data[0][middleoutput]-data[0][middleinput])/data[0]["I"],color='green',s=8.0,marker='+',label='Center')
            ax.scatter(data[0]["T"],2.65*(data[0][rightoutput]-data[0][righttinput])/data[0]["I"],color='brown',s=8.0,marker='x',label='Right Edge')
        if type == 'c':
            ax.scatter(data[0]["T"],2.65*(data[0][outputpin]-data[0][inputpin])/data[0]["I"],color='black',s=8.0,marker='v',label='Input')
            ax.scatter(data[0]["T"],2.65*(data[0][mirroroutputpin]-data[0][mirrorinputpin])/data[0]["I"],color='yellow',s=8.0,marker='^',label='Output')
            # ax.scatter(data[0]["T"],2.65*(data[0][mirroroutputpin]-data[0][mirrorinputpin]),color='yellow',s=8.0,marker='^',label='Voltage')
            # ax.scatter(data[0]["T"],data[0]["I"],color='purple',s=8.0,marker='.',label='Current')
        if type == 'd':
            ax.scatter(data[0]["T"],2.65*(data[0][mirroroutputpin ]-data[0][inputpin])/data[0]["I"],color='black',s=8.0,marker='v',label='Input')
            ax.scatter(data[0]["T"],2.65*(data[0][outputpin]-data[0][mirrorinputpin])/data[0]["I"],color='yellow',s=8.0,marker='^',label='Output')
        if (type == 'x1')|(type == 'x2'):
            ax.scatter(data[0]["T"],2.65*(data[0][rightoutput]-data[0][leftoutput])/data[0]["I"],color='purple',s=8.0,marker='.',label='Top Edge')
            ax.scatter(data[0]["T"],2.65*(data[0][righttinput]-data[0][leftinput])/data[0]["I"],color='brown',s=8.0,marker='x',label='Bottom Edge')
            ax.scatter(data[0]["T"],2.65*(data[0][mirrorinputpin]-data[0][inputpin])/data[0]["I"],color='black',s=8.0,marker='v',label='Input')
            ax.scatter(data[0]["T"],2.65*(data[0][mirroroutputpin]-data[0][outputpin])/data[0]["I"],color='red',s=8.0,marker='^',label='Output')
            # ax.scatter(data[0]["T"],data[0]["rx"],color='red',s=4.0,label= r'$\rho_{ab}$(T)')
        if type == 'L':
            # ax.scatter(data[0]["T"],data[0]["I"],s=8.0,marker='.',label='Current')
            for x in range(1,8):
                ax.scatter(data[0]["T"],-2.65*(data[0]["V["+str(1+x)+",1]"]-data[0]["V["+str(Nx-x)+",1]"])/data[0]["I"],s=8.0,marker='.',label='long c-axis '+str(x))
                # ax.scatter(data[0]["T"],2.65*(data[0]["V["+str(1+x)+",1]"]-data[0]["V["+str(Nx-x)+",1]"]),s=8.0,marker='.',label='Voltage '+str(x))
            # ax.scatter(data[0]["T"],-2.65*(data[0]["V[8,1]"]-data[0]["V["+str(Nx-7)+",1]"])/data[0]["I"],color='purple',s=8.0,marker='.',label='long c-axis')
        if type == 'm':
            scale = 0.36*data[0]["ry"][99]
            ax.plot(data[0]["T"],(scale/((data[0][mirroroutputpin][99]-data[0][mirrorinputpin][99])/data[0]["I"][99]))*(data[0][mirroroutputpin]-data[0][mirrorinputpin])/data[0]["I"],color='C1',marker='^',label='c-axis')
            ax.plot(data[1]["T"],(scale/((data[1][outputpin][99]-data[1][mirrorinputpin][99])/data[1]["I"][99]))*(data[1][outputpin]-data[1][mirrorinputpin])/data[1]["I"],color='green',marker='^',label='diagonal')
            ax.plot(data[2]["T"],(Ny/(Nx-output-input))*2.65*(data[2][mirroroutputpin]-data[2][outputpin])/data[2]["I"],color='red',marker='^',label='in-plane')
            #ax.plot(data[2]["T"],2.65*(data[2][mirrorinputpin]-data[2][inputpin])/data[2]["I"],color='red',marker='^')
            if L_check.is_file() == True:
                ax.plot(data[3]["T"],(scale/((data[3]["V[8,1]"][99]-data[3]["V["+str(Nx-7)+",1]"][99])/data[3]["I"][99]))*(data[3]["V[8,1]"]-data[3]["V["+str(Nx-7)+",1]"])/data[3]["I"],color='purple',marker='^',label='long c-axis')
        RVTaxes(ax)
        plt.show()
        if (savefile == True):
            fig.savefig('../../Plots/Sr327/Simulations/'+folder+'_'+type+'.svg')

    elif gif == False:
        x = []; y = []
        for i in range(1,Nx+1):
            x.append(i)
        for j in range(1,Ny+1):
            y.append(j)
        X, Y = np.meshgrid(x,y)
        if (type == 'm')&(L_check.is_file() == True):
            fig = plt.figure(figsize=(16,5))
            grid = plt.GridSpec(4,16, hspace=0.4, wspace=0.8)
            c_low_ax = fig.add_subplot(grid[:-2,0:4])
            d_low_ax = fig.add_subplot(grid[:-2,4:8])
            x_low_ax = fig.add_subplot(grid[:-2,8:12])
            L_low_ax = fig.add_subplot(grid[:-2,12:])
            c_high_ax = fig.add_subplot(grid[-2:,0:4])
            d_high_ax = fig.add_subplot(grid[-2:,4:8])
            x_high_ax = fig.add_subplot(grid[-2:,8:12])
            L_high_ax = fig.add_subplot(grid[-2:,12:])
            contour_axes = [[c_low_ax,c_high_ax],[d_low_ax,d_high_ax],[x_low_ax,x_high_ax],[L_low_ax,L_high_ax]]
            for number, ax in enumerate(contour_axes):
                heatmap1 = ax[0].contourf(X,Y, f(data[number],x,y,0),cmap='RdGy')
                heatmap2 = ax[1].contourf(X,Y, f(data[number],x,y,99),cmap='RdGy')
                if number+1 == 1:
                    ax[0].set_title('c-axis',fontweight='bold')
                    contouraxes(ax[0],heatmap1,Nx,Ny,1,mixed='left')
                    contouraxes(ax[1],heatmap2,Nx,Ny,1,mixed='corner')
                    cbr = fig.colorbar(heatmap1,ax=contour_axes)
                    cbr.set_label('Voltage (V)', rotation=270,fontweight='bold')
                    cbr.ax.get_yaxis().set_ticks([-0.5,0,0.5])
                    cbr.ax.set_yticklabels(['-0.5','0','0.5'])
                if number+1 == 2:
                    ax[0].set_title('diagonal',fontweight='bold')
                    contouraxes(ax[0],heatmap1,Nx,Ny,1,mixed='top')
                    contouraxes(ax[1],heatmap2,Nx,Ny,1,mixed='bottom')
                if number+1 == 3:
                    ax[0].set_title('in-plane',fontweight='bold')
                    contouraxes(ax[0],heatmap1,Nx,Ny,1,mixed='top')
                    contouraxes(ax[1],heatmap2,Nx,Ny,1,mixed='bottom')
                if number+1 == 4:
                    ax[0].set_title('long c-axis',fontweight='bold')
                    contouraxes(ax[0],heatmap1,Nx,Ny,2,mixed='top')
                    contouraxes(ax[1],heatmap2,Nx,Ny,2,mixed='bottom')
            plt.show()
        else:
            Z = f(data[0],x,y,contours-1)
            fig = plt.figure(figsize=(6,4))
            ax = fig.add_axes([0.2,0.2,0.7,0.6])
            heatmap = ax.contourf(X,Y, Z,cmap='RdGy')

            contouraxes(ax,heatmap,Nx,Ny)

            plt.show()

        if (savefile == True):
            fig.savefig('../../Plots/Sr327/Simulations/'+folder+'_'+type+'_contour_'+str(contours)+'.svg')

    else:
        x = []; y = []
        for i in range(1,Nx+1):
            x.append(i)
        for j in range(1,Ny+1):
            y.append(j)
        X, Y = np.meshgrid(x,y)
        gifdir = 'tempgif/'
        os.mkdir(gifdir)
        images = []
        images2 = []
        if not(type == 'm'):
            for i in range(0,100):
                Z = f(data[0],x,y,i)
                fig = plt.figure(figsize=(6,4))
                ax = fig.add_axes([0.2,0.2,0.7,0.6])
                heatmap = ax.contourf(X,Y, Z,cmap='RdGy')
                contouraxes(ax,heatmap,Nx,Ny,gift=True)

                fig.savefig(gifdir+str(i)+".png")
                images.append(imageio.imread(gifdir+str(i)+".png"))
                plt.close(fig=fig)
        else:
            if L_check.is_file() == True:
                for i in range(0,100):
                    fig = plt.figure(figsize=(12,6))
                    grid = plt.GridSpec(8,4, hspace=0.2, wspace=0.2)
                    main_ax = fig.add_subplot(grid[:-1,:-1])

                    c_ax = fig.add_subplot(grid[0:1,-1:])
                    d_ax = fig.add_subplot(grid[2:3,-1:])
                    x_ax = fig.add_subplot(grid[4:-3,-1:])
                    L_ax = fig.add_subplot(grid[-2:-1,-1:])
                    contour_axes = [c_ax,d_ax,x_ax,L_ax]
                    for number, ax in enumerate(contour_axes):
                        Z = f(data[number],x,y,i)
                        heatmap = ax.contourf(X,Y, Z,cmap='RdGy')
                        if number+1 == 1:
                            ax.set_title('c-axis')
                            cbr = fig.colorbar(heatmap,ax=contour_axes)
                            cbr.set_label('Voltage (V)', rotation=270,fontweight='bold')
                            cbr.ax.get_yaxis().set_ticks([-0.5,0,0.5])
                            cbr.ax.set_yticklabels(['-0.5','0','0.5'])
                            contouraxes(ax,heatmap,Nx,Ny,1,mixed='top')
                        if number+1 == 2:
                            ax.set_title('diagonal')
                            contouraxes(ax,heatmap,Nx,Ny,1,mixed='top')
                        if number+1 == 3:
                            ax.set_title('in-plane')
                            contouraxes(ax,heatmap,Nx,Ny,1,mixed='top')
                        if number+1 == 4:
                            ax.set_title('long c-axis')
                            contouraxes(ax,heatmap,Nx,Ny,2,mixed='top')
                        # contouraxes(ax,heatmap,Nx,Ny,0,True)

                    scale = 0.36*data[0]["ry"][99]
                    main_ax.plot(data[0]["T"][:i+1],(scale/((data[0][mirroroutputpin][99]-data[0][mirrorinputpin][99])/data[0]["I"][99]))*(data[0][mirroroutputpin][:i+1]-data[0][mirrorinputpin][:i+1])/data[0]["I"][:i+1],color='orange',marker='^',label='c-axis')
                    main_ax.plot(data[1]["T"][:i+1],(scale/((data[1][outputpin][99]-data[1][mirrorinputpin][99])/data[1]["I"][99]))*(data[1][outputpin][:i+1]-data[1][mirrorinputpin][:i+1])/data[1]["I"][:i+1],color='green',marker='^',label='diagonal')
                    main_ax.plot(data[2]["T"][:i+1],2.65*(data[2][mirroroutputpin][:i+1]-data[2][outputpin][:i+1])/data[2]["I"][:i+1],color='red',marker='^',label='in-plane')
                    main_ax.plot(data[3]["T"][:i+1],(scale/((data[3]["V[8,1]"][99]-data[3]["V["+str(Nx-7)+",1]"][99])/data[3]["I"][99]))*(data[3]["V[8,1]"][:i+1]-data[3]["V["+str(Nx-7)+",1]"][:i+1])/data[3]["I"][:i+1],color='purple',marker='^',label='long c-axis')
                    RVTaxes(main_ax,True)
                    fig.savefig(gifdir+str(i)+".png")
                    images.append(imageio.imread(gifdir+str(i)+".png"))
                    plt.close(fig=fig)
                ticker = 0
                while ticker < 50:
                    images.append(imageio.imread(gifdir+str(99)+".png"))
                    ticker += 1
            else:
                for i in range(0,100):
                    fig = plt.figure(figsize=(8,5))
                    grid = plt.GridSpec(6,4, hspace=0.2, wspace=0.2)
                    main_ax = fig.add_subplot(grid[:-1,:-1])

                    c_ax = fig.add_subplot(grid[0:1,-1:])
                    d_ax = fig.add_subplot(grid[2:-3,-1:])
                    x_ax = fig.add_subplot(grid[-2:-1,-1:])
                    contour_axes = [c_ax,d_ax,x_ax]
                    for number, ax in enumerate(contour_axes):
                        Z = f(data[number],x,y,i)
                        if number+1 == 1:
                            ax.set_title('c-axis')
                        if number+1 == 2:
                            ax.set_title('diagonal')
                        if number+1 == 3:
                            ax.set_title('in-plane')
                        heatmap = ax.contourf(X,Y, Z,cmap='RdGy')
                        contouraxes(ax,heatmap,Nx,Ny,0,True)

                    main_ax.plot(data[0]["T"][:i+1],2.65*(data[0][mirroroutputpin][:i+1]-data[0][mirrorinputpin][:i+1])/data[0]["I"][:i+1],color='orange',marker='^',label='c-axis')
                    main_ax.plot(data[1]["T"][:i+1],2.65*(data[1][outputpin][:i+1]-data[1][mirrorinputpin][:i+1])/data[1]["I"][:i+1],color='green',marker='^',label='diagonal')
                    main_ax.plot(data[2]["T"][:i+1],2.65*(data[2][mirroroutputpin][:i+1]-data[2][outputpin][:i+1])/data[2]["I"][:i+1],color='red',marker='^',label='in-plane')
                    RVTaxes(main_ax,True)
                    fig.savefig(gifdir+str(i)+".png")
                    images.append(imageio.imread(gifdir+str(i)+".png"))
                    plt.close(fig=fig)
                ticker = 0
                while ticker < 50:
                    images.append(imageio.imread(gifdir+str(99)+".png"))
                    ticker += 1
        imageio.mimsave('../../Plots/Sr327/Simulations/gif_'+folder+'_'+type+'.gif',images,fps=10)
        for i in range(0,100):
            os.remove(gifdir+str(i)+".png")
        os.rmdir(gifdir)
    return True

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Plots 327 Simulation')
    parser.add_argument("-t", "--type", help="Which type, c = c-axis, d = diagonal, x1,2 = in-plane or m = mixed")
    parser.add_argument("-c", "--contours", help="Plot contours instead of resistivity, value is temperature", type=int, default=0)
    parser.add_argument("-s", "--save", help="Enables saving the plot", action="store_true")
    parser.add_argument("-d", "--dimensions", help="Dimensions of simulation, x y = [x,y]", type=int, nargs=2, default=[24,5])
    parser.add_argument("-p", "--pins", help="Input and output pins, i o = [i,o]", type=int, nargs=2, default=[2,3])
    parser.add_argument("-e", "--experimental", help="Experimental version number", type=int, default=0)
    parser.add_argument("-g", "--gif", help="Enables giffing the plot", action="store_true")
    parser.add_argument("-P", "--pressure", help="Selects which pressure to use for implicit sim", type=int, default=0)
    parser.add_argument("-f", "--folder", help="Explicitely names data folder", type=str, default='')
    args = parser.parse_args()
    if (args.type == 'c')|(args.type == 'd')|(args.type == 'm')|(args.type == 'x1')|(args.type == 'x2')|(args.type == 'L'):
        main(args.pins[0],args.pins[1],args.dimensions[0],args.dimensions[1],type=args.type,contours=args.contours,savefile=args.save,experimental=args.experimental,gif=args.gif,pressure=args.pressure,implicit=True,folder=args.folder)
    else:
        print("No type specified, defaulting to c-axis")
        main(args.pins[0],args.pins[1],args.dimensions[0],args.dimensions[1],contours=args.contours,savefile=args.save,experimental=args.experimental,gif=args.gif)


import sys,os, nltk,string,re
import pandas as pd
from sklearn.multioutput import MultiOutputRegressor
from sklearn.linear_model import Lasso,MultiTaskLasso,ElasticNet
from sklearn.feature_extraction.text import CountVectorizer # To get a vector of occurences
from nltk.corpus import stopwords
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from config import elastic
import pickle
nltk.download('stopwords')
# cleaning the stopwords from the corpus will help in predictions later
stop_words = set(stopwords.words('english'))

# Set up regressor early so it runs  whenever class is called
multiple_class_regression = MultiOutputRegressor(ElasticNet(alpha=0.0001,max_iter=10000,random_state=1999, selection='random',normalize=True))

# helper function
def clean_stop_words(string):
    return ' '.join([word for word in string.split(' ') if word not in stop_words])

class Emotions():
    """
    This class is used to score a song by its emotional content using an ElasticNEt multiclass regressor.
    It first checks for the presence of a pretrained model in .sav format to cu down on processing time.
    if one is not found, it pulls data from ElasticSearch to train and fit the model.
    """
    def __init__(self):
        try: # load both models
            self.emotions_model = pickle.load(open('./saved_models/emotions_model.sav', 'rb'))
            self.vectorize = pickle.load(open('./saved_models/vectorizer.sav', 'rb'))
        except FileNotFoundError: # if  either model isnt present then...
            self.vectorize = CountVectorizer()
            results_dictionary = elastic().search(index='songs',size=100)['hits']['hits']
            data = pd.DataFrame([song['_source']['doc'] for song in results_dictionary])
            y = data[['anger','fear','joy','sadness','surprise']]
            y = [list(y.iloc[row]) for row in range(len(y))]
            X = list(data['cleaned_lyrics'])
            X = [clean_stop_words(lyrics) for lyrics in X]
            X = self.vectorize.fit_transform(X)
            self.emotions_model = multiple_class_regression.fit(X, y)
            # the following write the pickle files out
            pickle.dump(self.emotions_model, open('./saved_models/emotions_model.sav', 'wb'))
            pickle.dump(self.vectorize, open('./saved_models/vectorizer.sav', 'wb')) # this needs to be here so the vectorizer later is using the same instance and avoids fitting issues

    def predict(self,song_string):
        """
        Used for the API endpoint, this returns predictions of emotions based on the model that weve trained above
        """
        song_to_predict = self.vectorize.transform(song_string)
        return self.emotions_model.predict(song_to_predict)
if __name__ == "__main__":
    test = Emotions()
    print(test.predict(['yeah yeah oh love thing  working day long im ready come home mm mm guys roll eyes dont realize want want say im wrapped around finger dont understand got diamond hand  baby love thing whoa baby love thing yeah  sure could hang around complain way things ought yeah theres trouble world youre girl whose open arms really need thats come runnin side let em call crazy cant denied  baby love thing whoa ah baby love thing yeah  last name thing explained make feel inside way hold every night  love thing whoa oh baby love thing yeah  go love thing  say im wrapped around finger dont understand got diamond hand baby love thing whoa oh baby love thing yeah come love thing whoa oh baby love thing yeah oh love thing whoa oh sugar love thing aint diamond ring love thing whoa oh baby love thing love love love thing']))


from __future__ import unicode_literals
import frappe,json
from frappe.model.document import Document
from frappe import _

from datetime import datetime
from datetime import time
from datetime import timedelta
from frappe.utils import add_to_date

from frappe.integrations.utils import make_get_request, make_post_request, create_request_log
from frappe.utils import get_request_session

import requests
from requests.structures import CaseInsensitiveDict

from frepple.frepple.doctype.frepple_data_export.frepple_data_export import get_frepple_params,export_sales_orders

class FreppleRunPlan(Document):
	pass

@frappe.whitelist()
def run_plan(doc):
	doc = json.loads(doc)

	if doc["update_frepple"]:
		export_sales_orders()
		export_manufacturing_orders()
		export_purchase_orders()

	constraint= 0
	plantype = 1
	# filter = "/execute/api/runplan/?"

	if doc['constraint']:
		plantype = 1
	if doc['unconstraint']:
		plantype = 2
	if doc['capacity']:
		constraint = constraint + 4
	if doc['lead_time']:
		constraint = constraint + 1
	if doc['release_fence']:
		constraint = constraint + 8

	filter = "/execute/api/runplan/?constraint="+ str(constraint)+"&plantype="+ str(plantype)+"&env=supply"
	frepple_settings = frappe.get_doc("Frepple Settings")
	temp_url = frepple_settings.url.split("//")
	url = "http://"+ frepple_settings.username + ":" + frepple_settings.password + "@" + temp_url[1] + filter
	print(url + "-----------------------------------------------------------------------")
	headers= {
		'Content-type': 'application/json; charset=UTF-8',
		'Authorization': frepple_settings.authorization_header,
	}

	output = make_post_request(url,headers=headers, data=None)

	frappe.msgprint(
		msg='Plan have been runned successfully.',
		title='Success',
	)

	return output

@frappe.whitelist()
def generate_result(doc):
	doc = json.loads(doc)

	import_datas = []

	# Import manufacturing order
	if doc["manufacturing_orders"]:
		data = import_manufacturing_order()
		import_datas.append("Manufacturing Order Result")
		generate_manufacturing_order(data)
		print("Manufacturing order result was successfuly imported")

	# Import purchase order
	if doc["purchase_orders"]:
		data = import_purchase_order()
		import_datas.append("Purchase Order Result")
		generate_purchase_order(data)
		print("Purchase order result was successfuly imported")

	# Import distribution order
	if doc["distribution_orders"]:
		data = import_distribution_order()
		import_datas.append("Distribution Order Result")
		generate_distribution_order(data)
		print("Distribution order result was successfuly imported")

	# Output msg
	for import_data in import_datas:
		frappe.msgprint(_("{0} is imported.").format(import_data))

def export_manufacturing_orders():
	api = "manufacturingorder" #equivalent work order
	url,headers = get_frepple_params(api=api,filter=None)

	mos = frappe.db.sql(
		"""
		SELECT latest_reference,operation,status,quantity
		FROM `tabFrepple Manufacturing Order`
		""",
	as_dict=1)

	for mo in mos:
		#print(mo)
		data = json.dumps({
			"reference": mo.latest_reference,
			# "operation": mo.operation,
			"status": mo.status,
			# "quantity": mo.quantity,
		})
		output = make_post_request(url,headers=headers, data=data)

def export_purchase_orders():
	api = "purchaseorder" #equivalent purchase order
	url,headers = get_frepple_params(api=api,filter=None)

	pos = frappe.db.sql(
		"""
		SELECT latest_reference,supplier,status
		FROM `tabFrepple Purchase Order`
		""",
	as_dict=1)

	for po in pos:
		#print(po)
		data = json.dumps({
			"reference": po.latest_reference,
			# "supplier": po.supplier,
			"status": po.status,
		})
		output = make_post_request(url,headers=headers, data=data)

def import_manufacturing_order():
	api = "manufacturingorder"

	''' With filtering'''
	# filter = "?name=SAL-ORDER-0002"
	# filter = None
	# filter = "?status__contain=open"
	# url,headers = get_frepple_params(api=None,filter=filter)

	#filter = "?status=proposed&operation_in=BOM"
	# filter = "?operation_in=BOM"

	url,headers = get_frepple_params(api=api,filter=None)
	outputs = make_get_request(url,headers=headers)
	# print(type(outputs))
	# idx = 0
	# for output in outputs:
	# 	print(output["operation"])
	# 	print(output["operation"].split("@"))
	# 	if (len(output["operation"].split("@")) > 1): #routing type operation should only have 1 element, use this to filter out the routing type
	# 		print("Delete")
	# 		del outputs[idx]
	# 	idx = idx + 1

	# Delete dictionary from list using list comprehension
	res = [output for output in outputs if not (len(output["operation"].split("@")) > 1)]
	return res

def generate_manufacturing_order(data):

	for i in data:
		#print(i["plan"])
		# idx = 0
		# demand = (list(i["plan"]["pegging"].keys())[idx])
		demands = list(i["plan"]["pegging"].keys())
		resource_list = list(i["resources"])
		material_list = list(i["materials"])

		if not frappe.db.exists("Frepple Manufacturing Order", i["reference"]):
			new_doc = frappe.new_doc("Frepple Manufacturing Order")
			new_doc.reference = i["reference"]
			new_doc.latest_reference = i["reference"]
			new_doc.operation = i["operation"]
			new_doc.status = i["status"]
			new_doc.quantity = i["quantity"]
			new_doc.completed_quantity = i["quantity_completed"]
			new_doc.start_date = datetime.fromisoformat(i["startdate"])
			new_doc.end_date = datetime.fromisoformat(i["enddate"])
			new_doc.demand = demands[0]
			for resource in resource_list:
				new_doc.append('resource', {
					'reference': i["reference"],
					'resource': resource["resource"],
					'resource_quantity': resource["quantity"]
				})
			for material in material_list:
				new_doc.append('material', {
					'reference': i["reference"],
					'material': material["item"],
					'material_quantity': material["quantity"]
				})
			new_doc.insert()

		elif frappe.db.exists("Frepple Manufacturing Order", i["reference"]):
				frappe.db.set_value('Frepple Manufacturing Order', i["reference"], {
					#'latest_reference': i["reference"],
					'operation': i["operation"],
					'status': i["status"],
					'demand' : demands[0],
					'quantity': i["quantity"],
					#'completed_quantity': i["quantity_completed"],
					'start_date': datetime.fromisoformat(i["startdate"]),
					'end_date': datetime.fromisoformat(i["enddate"])
 				})

	''' Draft
		#To load data into table
		doc = frappe.get_doc("Frepple Manufacturing Order", i["reference"])
		for resource in resource_list:
			doc.append('resource', {
				'reference': i["reference"],
				'resource': resource["resource"],
				'resource_quantity': resource["quantity"]
			})
		for material in material_list:
			doc.append('material', {
				'reference': i["reference"],
				'material': material["item"],
				'material_quantity': material["quantity"]
			})
		doc.save()

		demands = (list(i["plan"]["pegging"].keys()))
		for demand in demands:
			mos = frappe.db.sql(
				"""
				SELECT name,demand
				FROM `tabFrepple Manufacturing Order`
				WHERE demand = %s
				""",
			demand,as_dict=1)
			#print(mos)
			# if not frappe.db.exists("Frepple Manufacturing Order",i["reference"]):
			if not frappe.db.exists("Frepple Manufacturing Order",i["reference"]) and len(mos)== 0:
				#create new document
				new_doc = frappe.new_doc("Frepple Manufacturing Order")
				new_doc.reference = i["reference"]
				new_doc.latest_reference = i["reference"]
				new_doc.operation = i["operation"]
				new_doc.status = i["status"]
				new_doc.quantity = i["quantity"]
				new_doc.completed_quantity = i["quantity_completed"]
				new_doc.start_date = datetime.fromisoformat(i["startdate"])
				new_doc.end_date = datetime.fromisoformat(i["enddate"])
				new_doc.demand = demand
				new_doc.insert()
				#print(new_doc.name)
			elif frappe.db.exists("Frepple Manufacturing Order",i["reference"]) and len(mos)== 0:
				continue
			else:#update
				if frappe.db.exists("Frepple Manufacturing Order",mos[0].name):
					existing_doc = frappe.get_doc("Frepple Manufacturing Order",mos[0].name)
					#print(existing_doc)
					frappe.db.set_value('Frepple Manufacturing Order', mos[0].name, #Update the status
					{
						'latest_reference': i["reference"],
						'operation': i["operation"],
						'status': i["status"],
						'quantity': i["quantity"],
						# 'completed_quantity': i["quantity_completed"],
						'start_date': datetime.fromisoformat(i["startdate"]),
						'end_date': datetime.fromisoformat(i["enddate"])
					})
	'''
					# existing_doc.reference = i["reference"]
					# existing_doc.operation = i["operation"]
					# existing_doc.status = i["status"]
					# existing_doc.quantity = i["quantity"]
					# existing_doc.completed_quantity = i["quantity_completed"]
					# existing_doc.start_date = datetime.fromisoformat(i["startdate"])
					# existing_doc.end_date = datetime.fromisoformat(i["enddate"])
					# existing_doc.save(ignore_permissions=True, ignore_version=True)
					# existing_doc.reload()

def import_purchase_order():
	api = "purchaseorder"

	''' With filtering'''
	# filter = "?name=SAL-ORDER-0002"
	# filter = None
	# filter = "?status__contain=open"
	# url,headers = get_frepple_params(api=None,filter=filter)
	#filter = "?status=proposed"

	url,headers = get_frepple_params(api=api,filter=None)
	output = make_get_request(url,headers=headers)

	return output

def generate_purchase_order(data):
	for i in data:
		#print(i)
		demands = list(i["plan"]["pegging"].keys())

		if not frappe.db.exists("Frepple Purchase Order",i["reference"]):
			new_doc = frappe.new_doc("Frepple Purchase Order")
			new_doc.reference = i["reference"]
			new_doc.latest_reference = i["reference"]
			new_doc.supplier = i["supplier"]
			new_doc.status = i["status"]
			new_doc.ordering_date = datetime.fromisoformat(i["startdate"])
			new_doc.receive_date =  datetime.fromisoformat(i["enddate"])
			new_doc.item = i["item"]
			new_doc.quantity =i["quantity"]
			new_doc.location = i["location"]
			new_doc.demand = demands[0]
			new_doc.insert()

		elif frappe.db.exists("Frepple Purchase Order", i["reference"]):
				frappe.db.set_value('Frepple Purchase Order', i["reference"], {
					'latest_reference': i["reference"],
					"ordering_date" : datetime.fromisoformat(i["startdate"]),
					"receive_date" :  datetime.fromisoformat(i["enddate"]),
					"quantity" : i["quantity"],
					"status" : i["status"],
					"supplier" : i["supplier"],
					"location" : i["location"],
				})

''' Draft
	for i in data:
		pos = frappe.db.sql(
			"""
			SELECT name,item,supplier
			FROM `tabFrepple Purchase Order`
			WHERE item = %s and supplier = %s
			""",
		[i["item"],i["supplier"]],as_dict=1)
		print (pos)
		if len(pos) == 0:
		# if not frappe.db.exists("Frepple Purchase Order",i["reference"]):
			new_doc = frappe.new_doc("Frepple Purchase Order")
			new_doc.reference = i["reference"]
			new_doc.latest_reference = i["reference"]
			new_doc.supplier = i["supplier"]
			new_doc.status = i["status"]
			new_doc.ordering_date = datetime.fromisoformat(i["startdate"])
			new_doc.receive_date =  datetime.fromisoformat(i["enddate"])
			new_doc.item = i["item"]
			new_doc.quantity =i["quantity"]
			new_doc.insert()
			#print(new_doc.name)
		else: #update
			existing_doc = frappe.get_doc("Frepple Purchase Order",pos[0].name)
			frappe.db.set_value('Frepple Purchase Order', pos[0].name, #Update the status
			{
				'latest_reference': i["reference"],
				"ordering_date" : datetime.fromisoformat(i["startdate"]),
				"receive_date" :  datetime.fromisoformat(i["enddate"]),
				"quantity" : i["quantity"]
			})
'''

def import_distribution_order():
	api = "distributionorder"

	''' With filtering'''
	# filter = "?name=SAL-ORDER-0002"
	# filter = None
	# filter = "?status__contain=open"
	# url,headers = get_frepple_params(api=None,filter=filter)
	#filter = "?status=proposed"

	url,headers = get_frepple_params(api=api,filter=None)
	output = make_get_request(url,headers=headers)

	return output

def generate_distribution_order(data):
	for i in data:
		#print(i)
		#print("_______________________________________")
		demands = list(i["plan"]["pegging"].keys())
		#resource_list = list(i["resources"])
		resource_list = []

		if not frappe.db.exists("Frepple Distribution Order", i["reference"]):
			new_doc = frappe.new_doc("Frepple Distribution Order")
			new_doc.reference = i["reference"]
			new_doc.item = i["item"]
			new_doc.origin = i["origin"]
			new_doc.destination = i["destination"]
			new_doc.quantity = i["quantity"]
			new_doc.status = i["status"]
			new_doc.shipping_date = datetime.fromisoformat(i["startdate"])
			new_doc.receipt_date =  datetime.fromisoformat(i["enddate"])
			new_doc.demand = demands[0]
			for resource in resource_list:
				new_doc.append('resource', {
					'reference': i["reference"],
					'resource': resource["resource"],
					'resource_quantity': resource["quantity"]
				})
			new_doc.insert()

		elif frappe.db.exists("Frepple Distribution Order", i["reference"]):
				frappe.db.set_value('Frepple Distribution Order', i["reference"], {
					'item' : i["item"],
					'origin': i["origin"],
					'status': i["status"],
					'demand' : demands[0],
					'quantity': i["quantity"],
					'shipping_date': datetime.fromisoformat(i["startdate"]),
					'receipt_date': datetime.fromisoformat(i["enddate"])
 				})

	''' Draft
		demands = list(i["plan"]["pegging"].keys())

		#check_value = i["origin"]+' > '+i["destination"]+' | '+i["startdate"]+' / '+i["enddate"]+' | '+demands[0]
		#check_list = []

		#if check_value not in check_list:
		#if not frappe.db.exists("Frepple Distribution Order", i["origin"]+'>'+i["destination"]+'|'+i["startdate"]+'/'+i["enddate"]+'|'+demands[0]):
		if not frappe.db.exists("Frepple Distribution Order", i["origin"]+"-To-"+i["destination"]+"-At-"+i["startdate"]+"-"+i["enddate"]):
			#check_list.append(check_value)
			new_list =[]
			for j in data:
				if (i["reference"] != j["reference"])&(i["origin"] == j["origin"])&(i["destination"] == j["destination"])&(i["startdate"] == j["startdate"])&(i["enddate"] == j["enddate"]):
					new_list.append(i) if i not in new_list else new_list
					new_list.append(j) if j not in new_list else new_list
				else:
					new_list.append(i) if i not in new_list else new_list
			new_doc = frappe.new_doc("Frepple Distribution Order")
			new_doc.origin = i["origin"]
			new_doc.destination = i["destination"]
			new_doc.status = i["status"]
			new_doc.shipping_date = datetime.fromisoformat(i["startdate"])
			new_doc.receipt_date =  datetime.fromisoformat(i["enddate"])
			new_doc.demand = demands[0]
			for item in new_list:
				new_doc.append('item', {
					'reference': item["reference"],
					'material': item["item"],
					'material_quantity': item["quantity"]
				})
			new_doc.insert()
		#elif frappe.db.exists("Frepple Distribution Order", i["origin"]+'>'+i["destination"]+'|'+i["startdate"]+'/'+i["enddate"]+'|'+demands[0]):
	'''


from glfwToolbox.easy_shaders import GPUShape as _GPUShape
from OpenGL.GL import GL_POLYGON as _GL_POLYGON
from OpenGL.GL import glUniformMatrix4fv as _glUniformMatrix4fv
from OpenGL.GL import glGetUniformLocation as _glGetUniformLocation
from OpenGL.GL import GL_TRUE as _GL_TRUE
from OpenGL.GL import GL_TRIANGLES as _GL_TRIANGLES
from OpenGL.GL import glUseProgram as _glUseProgram
import glfwToolbox.transformations as _tr

class AdvancedGPUShape(object):
    def __init__(self, shapes, model=_tr.identity(), enabled=True, shader=None, mode=None):
        """
        Constructor.

        :param shapes: List or GPUShape object
        :param model: Basic model transformation matrix
        :param enabled: Indicates if the shape is enabled or not
        :param shader: Shader program
        """
        if not isinstance(shapes, list):
            shapes = [shapes]
        for i in range(len(shapes)):
            if not isinstance(shapes[i], _GPUShape):
                raise Exception('Object {0} of shapes list is not GPUShape instance'.format(i))
        if mode is None:
            mode = _GL_TRIANGLES

        self._shapes = shapes
        self._model = model
        self._modelPrev = None
        self._enabled = enabled
        self._shader = shader
        self._drawMode = mode

    def set_shader(self, shader):
        """
        Set shader.

        :param shader:
        :return:
        """
        self._shader = shader

    def translate(self, tx=0, ty=0, tz=0):
        """
        Translate model.

        :param tx:
        :param ty:
        :param tz:
        :return:
        """
        self._model = _tr.matmul([_tr.translate(tx, ty, tz), self._model])

    def scale(self, sx=1, sy=1, sz=1):
        """
        Scale model.

        :param sx:
        :param sy:
        :param sz:
        :return:
        """
        self._model = _tr.matmul([_tr.scale(sx, sy, sz), self._model])

    def uniform_scale(self, s=1):
        """
        Uniform scale model.

        :param s:
        :return:
        """
        self._model = _tr.matmul([_tr.uniform_scale(s), self._model])

    def rotation_x(self, theta=0):
        """
        Rotate model.

        :param theta:
        :return:
        """
        self._model = _tr.matmul([_tr.rotation_x(theta), self._model])

    def rotation_y(self, theta=0):
        """
        Rotate model.

        :param theta:
        :return:
        """
        self._model = _tr.matmul([_tr.rotation_y(theta), self._model])

    def rotation_z(self, theta=0):
        """
        Rotate model.

        :param theta:
        :return:
        """
        self._model = _tr.matmul([_tr.rotation_z(theta), self._model])

    def rotation_a(self, theta, axis):
        """
        Rotate model.

        :param theta:
        :param axis:
        :return:
        """
        self._model = _tr.matmul([_tr.rotation_a(theta, axis), self._model])

    def shearing(self, xy=0, yx=0, xz=0, zx=0, yz=0, zy=0):
        """
        Apply shear to model.

        :param xy:
        :param yx:
        :param xz:
        :param zx:
        :param yz:
        :param zy:
        :return:
        """
        self._model = _tr.matmul([_tr.shearing(xy, yx, xz, zx, yz, zy), self._model])

    def apply_temporal_transform(self, t):
        """
        Apply temporal transform to model until drawing.

        :param t:
        :return:
        """
        self._modelPrev = self._model
        self._model = _tr.matmul([t, self._model])

    def draw(self, view=None, projection=None, mode=None, shader=None, usemodel=True):
        """
        Draw model.

        :param view:
        :param projection:
        :param mode:
        :param shader:
        :param usemodel:
        :return:
        """
        if not self._enabled:
            return
        if mode is None:
            if self._drawMode is None:
                mode = _GL_POLYGON
            else:
                mode = self._drawMode
        if shader is None:
            if self._shader is None:
                raise Exception('MergedShape shader is not set')
            shader = self._shader
        _glUseProgram(shader.shaderProgram)
        if usemodel and shader.keyModel != '':
            _glUniformMatrix4fv(_glGetUniformLocation(shader.shaderProgram, shader.keyModel), 1, _GL_TRUE, self._model)
        if projection is not None and shader.keyProjection != '':
            _glUniformMatrix4fv(_glGetUniformLocation(shader.shaderProgram, shader.keyProjection), 1, _GL_TRUE,
                                projection)
        if view is not None and shader.keyView != '':
            _glUniformMatrix4fv(_glGetUniformLocation(shader.shaderProgram, shader.keyView), 1, _GL_TRUE, view)
        for i in self._shapes:
            shader.draw_shape(i, mode)
        if self._modelPrev is not None:
            self._model = self._modelPrev
            self._modelPrev = None

    def disable(self):
        """
        Disable the model.

        :return:
        """
        self._enabled = False

    def enable(self):
        """
        Enable the model.

        :return:
        """
        self._enabled = True

    def clone(self):
        """
        Clone the model.

        :return:
        """
        return AdvancedGPUShape(self._shapes.copy(), self._model, enabled=self._enabled, shader=self._shader)


from twisted.trial import unittest
from twisted.internet.address import IPv4Address

from txyam.factory import ConnectingMemCacheProtocol, MemCacheClientFactory

class FactoryTest(unittest.TestCase):

    def test_buildProtocol(self):
        f = MemCacheClientFactory()
        addy = IPv4Address('TCP', 'ahost', 1234)
        p = f.buildProtocol(addy, timeOut=123)
        self.assertIsInstance(p, ConnectingMemCacheProtocol)
        self.assertEqual(p.persistentTimeOut, 123)
        self.assertEqual(str(p), "memcache[%s]" % addy)


from django.shortcuts import render
from . import models
from packages.models import Package,PackageStudent
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect,Http404
from django.shortcuts import get_object_or_404

# Create your views here.

def TestView(request,package_id):
    if request.method == 'POST':
        marks = 0
        test = models.Test.objects.filter(package_id=package_id)
        for i in range(0,len(test)):
            test_id = test[i].id - 1
            if request.POST.get(str(test_id+1)) == test[i].correct:
                marks = marks + test[i].marks
            else:
                continue

        packagestudent = get_object_or_404(PackageStudent,package=package_id,student=request.user)

        packagestudent.marks = marks
        packagestudent.submission = True
        packagestudent.save()
        return HttpResponseRedirect(reverse('tests:test_successful'))
    else:
        try:
            ps = PackageStudent.objects.get(package_id=package_id,student_id=request.user.id,package__q_visibility=True,submission=False)
            data = ps.package.package_test.all()
        except:
            return HttpResponseRedirect(reverse('tests:test_denied'))

        package_end_date = get_object_or_404(Package,id=package_id).end_time
        # package_start_date = get_object_or_404(Package,id=package_id).start_time
        return render(request,'tests/test.html',{'form':data,'end_date':str(package_end_date)})


import numpy as np
from matplotlib import pyplot as plt

# Load the data
filename = 'randomData.txt'
coord = np.dtype(dict(names=('x', 'y', 'z'), \
                      formats=('float', 'float', 'float') ) )
data = np.loadtxt(filename, dtype=coord, delimiter=',')

# create the subplots
f, axs = plt.subplots(2,2)
axs[0][0].plot(data['x'], data['y'], 'bo')
axs[0][1].plot(data['x'], data['z'], 'ro')
axs[1][0].plot(data['y'], data['z'], 'go')
axs[1][1].plot(data['x'], data['y'], 'bo', data['y'], data['z'], 'ro', \
               data['x'], data['z'], 'go' )


from PyQt5.QtWidgets import QWidget
from PyQt5 import uic
from PyQt5.Qt import QSize
from PyQt5.Qt import QByteArray
from PyQt5.Qt import QIcon
from PyQt5.QtWidgets import QMenu
from mc.app.Settings import Settings
from mc.common.globalvars import gVar
from PyQt5.Qt import Qt
from mc.bookmarks.bookmarksimport.BookmarksImportDialog import BookmarksImportDialog
from mc.bookmarks.bookmarksexport.BookmarksExportDialog import BookmarksExportDialog
from mc.history.HistoryManager import HistoryManager
from mc.bookmarks.BookmarksManager import BookmarksManager
from mc.lib3rd.FancyTabWidget import FancyTabWidget

class BrowsingLibrary(QWidget):
    def __init__(self, window, parent=None):
        '''
        @param: window BrowserWindow
        @param: parent QWidget
        '''
        super().__init__(parent)
        self._ui = uic.loadUi('mc/other/BrowsingLibrary.ui', self)
        self._historyManager = HistoryManager(window)  # HistoryManager
        self._bookmarksManager = BookmarksManager(window)  # BookmarksManager

        settings = Settings()
        settings.beginGroup('BrowsingLibrary')
        self.resize(settings.value('size', QSize(760, 470)))
        self._historyManager.restoreState(settings.value('historyState', QByteArray()))
        settings.endGroup()

        gVar.appTools.centerWidgetOnScreen(self)

        historyIcon = QIcon()
        historyIcon.addFile(':/icons/other/bighistory.svg', QSize(), QIcon.Normal)
        historyIcon.addFile(':/icons/other/bighistory-selected.svg', QSize(), QIcon.Selected)

        bookmarksIcon = QIcon()
        bookmarksIcon.addFile(':/icons/other/bigstar.svg', QSize(), QIcon.Normal)
        bookmarksIcon.addFile(':/icons/other/bigstar-selected.svg', QSize(), QIcon.Selected)

        self._ui.tabs.AddTab(self._historyManager, historyIcon, _('History'))
        self._ui.tabs.AddTab(self._bookmarksManager, bookmarksIcon, _('Bookmarks'))
        self._ui.tabs.SetMode(FancyTabWidget.Mode_LargeSidebar)
        self._ui.tabs.setFocus()

        m = QMenu()
        m.addAction(_('Import Bookmarks...'), self._importBookmarks)
        m.addAction(_('Export Bookmarks...'), self._exportBookmarks)
        self._ui.importExport.setMenu(m)

        self._ui.tabs.CurrentChanged.connect(self._ui.searchLine.clear)
        self._ui.searchLine.textChanged.connect(self._search)

        gVar.appTools.setWmClass('Browsing Library', self)

    def showHistory(self, window):
        '''
        @param: window BrowserWindow
        '''
        self._ui.tabs.SetCurrentIndex(0)
        self.show()
        self._historyManager.setMainWindow(window)

        self.raise_()
        self.activateWindow()

    def showBookmarks(self, window):
        '''
        @param: window BrowserWindow
        '''
        self._ui.tabs.SetCurrentIndex(1)
        self.show()
        self._bookmarksManager.setMainWindow(window)

        self.raise_()
        self.activateWindow()

    def historyManager(self):
        '''
        @return: HistoryManager
        '''
        return self._historyManager

    def bookmarksManager(self):
        '''
        @return: BookmarksManager
        '''
        return self._bookmarksManager

    # private Q_SLOTS:
    def _search(self):
        if self._ui.tabs.current_index() == 0:
            self._historyManager.search(self._ui.searchLine.text())
        else:
            self._bookmarksManager.search(self._ui.searchLine.text())

    def _importBookmarks(self):
        dialog = BookmarksImportDialog(self)
        dialog.open()

    def _exportBookmarks(self):
        dialog = BookmarksExportDialog(self)
        dialog.open()

    # private:
    def closeEvent(self, event):
        '''
        @param: event QCloseEvent
        '''
        settings = Settings()
        settings.beginGroup('BrowsingLibrary')
        settings.setValue('size', self.size())
        settings.setValue('historySize', self._historyManager.saveState())
        settings.endGroup()
        event.accept()

    def keyPressEvent(self, event):
        '''
        @param: event QKeyEvent
        '''
        evtKey = event.key()
        evtMod = event.modifiers()
        if evtKey == Qt.Key_Escape or \
                (evtKey == Qt.Key_W and evtMod == Qt.ControlModifier):
            self.close()

        super().keyPressEvent(event)


import gym
from gym import spaces
from gym.utils import seeding
import numpy as np
import itertools

class TradingEnv(gym.Env):

	def __init__(self, train_data, init_invest=20000):
		self.stock_price_history = np.around(train_data)
		self.n_stock, self.n_step = self.stock_price_history.shape

		self.init_invest = init_invest
		self.cur_step = None
		self.stock_owned = None
		self.stock_price = None
		self.cash_in_hand = None

		self.action_space = spaces.Discrete(3**self.n_stock)

		stock_max_price = self.stock_price_history.max(axis=1)
		stock_range = [[0, init_invest * 2 // mx] for mx in stock_max_price]
		price_range = [[0, mx] for mx in stock_max_price]
		cash_in_hand_range = [[0, init_invest*2]]
		self.observation_space = spaces.MultiDiscrete(stock_range + price_range + cash_in_hand_range)

		self._seed()
		self._reset()

	def _seed(self, seed=None):
		self.np_random, seed = seeding.np_random(seed)
		return [seed]

	def _reset(self):
		self.cur_step = 0
		self. stock_owned = [0] * self.n_stock
		self.stock_price = self.stock_price_history[:, self.cur_step]
		self.cash_in_hand = self.init_invest
		return self._get_obs()

	def _step(self, action):
		assert self.action_space.contains(action)
		prev_val = self._get_val()
		self.cur_step += 1
		self.stock_price = self.stock_price_history[:, self.cur_step]
		self._trade()
		cur_val = self._get_val()
		reward = cur_val - prev_val
		done = self.cur_step == self.n_step - 1
		info = {'cur_val': cur_val}
		return self._get_obs(), reward, done, info

	def _get_obs(self):
		obs = []
		obs.extend(self.stock_owned)
		obs.extend(list(self.stock_price))
		obs.append(self.cash_in_hand)
		return obs

	def _get_val(self):
		return np.sum(self.stock_owned*self.stock_price) + self.cash_in_hand

	def _trade(self, action):
		action_combo = map(list, itertools.product([0,1,2], repeat=self.n_stock))
		action_vec = action_combo[action]

		sell_index = []
		buy_index = []
		for i, a in enumerate(action_vec):
			if a == 0:
				sell_index.append(i)
			elif a == 2:
				buy_index.append(i)

		if sell_index:
			for i in sell_index:
				self.cash_in_hand += self.stock_price[i]*self.stock_owned[i]
				self.stock_owned[i] = 0
			if buy_index:
				can_buy = True
				while can_buy:
					for i in buy_index:
						if self.cash_in_hand > self.stock_price[i]:
							self.stock_owned[i] += 1
							self.cash_in_hand -= self.stock_price[i]
						else:
							can_buy = False


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import typing
import os
import os.path
import sys
from setuptools import find_packages, setup
try:
    from pip._internal.req import parse_requirements
except ModuleNotFoundError:
    from pip.req import parse_requirements

cwd = os.getcwd()

about = {}
with open(os.path.join(cwd, "jail", "__version__.py"), encoding="utf-8") as f:
    VERSION = exec(f.read(), about)

with open(os.path.join(cwd, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

setup(
	name="jail",
	version=about["__version__"],
	description="Native FreeBSD jail bindings with libc.",
	long_description=long_description,
	long_description_content_type="text/markdown",
	url="https://github.com/gronke/py-jail",
	author="Stefan Grönke",
	author_email="stefan@gronke.net",
	python_requires=">=3.6",
	install_requires=[
		"freebsd-sysctl==0.0.7"
	],
	tests_require=["pytest", "pytest-runner", "pytest-benchmark"],
	packages=find_packages(exclude=("tests",))
)


"""在线策略系统"""
from   const import Valid_Col
import pandas as pd
from   policy.base_policy import BasePolicy
from   callback import error_callback
import itertools
import numpy as np
from data.hive_sql import *
from solvers.base_solver import *

class MPSFilterPolicy(BasePolicy):
    """mps解空间过滤策略"""

    def run(self):
        pred_candidate = self.df
        ecrdf = self.dao.get_wyc_ecr()
        crdf = self.dao.get_pred_cr()
        '''读取城市分框信息'''
        filter_pred_candidate_list = []
        print("城市分框数量：", len(self.dao.city_group_list))
        if len(self.dao.city_group_list) > 0:
            for group_info in self.dao.city_group_list:
                sdate, edate = group_info['start_date'], group_info['end_date']
                if type(group_info['city_list']) == str:
                    cis = list(map(int, group_info['city_list'].split(',')))
                elif type(group_info['city_list']) == list:
                    cis = group_info['city_list']
                city_bucket_budget = group_info['budget_upperbound']
                print("城市分框：", "开始日期：", sdate, "结束日期：", edate, "预算：", city_bucket_budget)
                print("城市分框 cis:", cis)

                '''生成每个ecr分框的预算'''
                group_ecrdf = ecrdf.query("city_id in @cis and stat_date >= '%s' and stat_date <= '%s'" % (sdate, edate))
                bin_costdf = self.gen_ecr_bucket_budget(group_ecrdf, sdate, edate, cis, city_bucket_budget)
                if len(bin_costdf) == 0:
                    continue
                else:
                    group_crdf = crdf.query("city_id in @cis and stat_date >= '%s' and stat_date <= '%s'" % (sdate, edate))
                    '''生成ecr-cr分框的锚点补贴率'''
                    cr_ratedf = self.gen_cr_bucket_rate(group_crdf, group_ecrdf, bin_costdf)
                    '''过滤候选集'''
                    cr_ratedf['min_combo_rate'] = cr_ratedf['avg_rate'].apply(lambda x: x * 0.8).astype(float)
                    cr_ratedf['max_combo_rate'] = cr_ratedf['avg_rate'].apply(lambda x: x * 1.2).astype(float)
                    # 临时兜底约束
                    cr_ratedf['max_combo_rate'] = cr_ratedf.apply(lambda x: 0.01 if x.city_id in (8, 25) else x.max_combo_rate, axis=1)
                    cr_ratedf['min_combo_rate'] = cr_ratedf.apply(lambda x: 0.0 if x.city_id in (8, 25) else x.min_combo_rate, axis=1)
                    cr_ratedf['max_combo_rate'] = cr_ratedf.apply(lambda x: 0.02 if x.city_id in (14, 16, 22) else x.max_combo_rate, axis=1)
                    cr_ratedf['min_combo_rate'] = cr_ratedf.apply(lambda x: 0.0 if x.city_id in (14, 16, 22) else x.min_combo_rate, axis=1)

                    group_candidate = cr_ratedf.merge(pred_candidate, on=['city_id', 'stat_date'], how='left')
                    print("城市分框，候选集数量：", len(group_candidate))
                    filter_group_candidate = group_candidate.query("is_candidate==1 and crate>=min_combo_rate and crate<=max_combo_rate")
                    print("城市分框，过滤后的候选集数量：", len(filter_group_candidate))
                    filter_pred_candidate_list.append(filter_group_candidate)
        filter_pred_candidate = pd.concat(filter_pred_candidate_list) # 过滤后的候选集
        print('过滤后的候选集数量:', len(filter_pred_candidate))
        print(filter_pred_candidate.head(5))
        return filter_pred_candidate

    def gen_ecr_bucket_budget(self, ecrdf, start_date, end_date, city_list, city_bucket_budget):
        '''生成ecr分框预算'''
        ecrdf['ecr_bin'] = ecrdf['wyc_ecr'].apply(self.gen_ecr_bucket)
        ecrdf = ecrdf[~ecrdf['ecr_bin'].isna()]

        dates = list(pd.date_range(start_date, end_date).astype(str))
        fkhf_list = list(itertools.product(city_list, dates, np.arange(0, 0.11, 0.0005)))
        ratedf = pd.DataFrame(fkhf_list, columns=['city_id', 'stat_date', 'fkhf'])
        print("ratedf:", ratedf.head(5))
        print("citys:", city_list)

        df = pd.merge(ecrdf, ratedf, on=['city_id', 'stat_date'])
        df['ecr_dgmv_ratio'] = df.apply(self.gen_ecr_dgmv_ratio, axis=1)
        gmvdf = self.df[['city_id', 'stat_date', 'total_gmv']].drop_duplicates() # candidate表
        opti_data = pd.merge(df, gmvdf, on=['city_id', 'stat_date'], how='left')

        opti_data['delta_gmv'] = opti_data['ecr_dgmv_ratio'] * opti_data['total_gmv']
        opti_data['delta_cost'] = opti_data['fkhf'] * opti_data['total_gmv']
        opti_data[Valid_Col] = 1

        print("ecr bucket opti_data number:", len(opti_data))
        if len(opti_data) == 0:
            return pd.DataFrame([], columns=['ecr_bin', 'budget'])
        else:
            # 整数规划求解
            solver = BaseSolver(self.CONF, opti_data, is_bp=False, city_must_cost=True, mode='city_bucket', total_budget=city_bucket_budget)
            resdf = solver.solve()
            bin_costdf = resdf.groupby(['ecr_bin'])[['delta_cost']].agg(sum).reset_index().rename(columns={'delta_cost': 'budget'})
            print("ecr分框的预算:")
            print(bin_costdf)
            return bin_costdf

    def gen_ecr_bucket(self, x):
        if x <= 0.59:  # x > 0.15
            return 0
        elif x > 0.59 and x <= 0.635:
            return 1
        elif x > 0.635 and x <= 0.66:
            return 2
        elif x > 0.66 and x <= 0.685:
            return 3
        elif x > 0.685: # and x <= 0.78
            return 4

    def gen_ecr_dgmv_ratio(self,x):
        w = 0.65
        if x.ecr_bin == 0:
            return 0.744 * (x.fkhf ** (w))
        elif x.ecr_bin == 1:
            return 0.69 * (x.fkhf ** (w))
        elif x.ecr_bin == 2:
            return 0.64 * (x.fkhf ** (w))
        elif x.ecr_bin == 3:
            return 0.597 * (x.fkhf ** (w))
        elif x.ecr_bin == 4:
            return 0.542 * (x.fkhf ** (w))

    def gen_cr_bucket(self,x):
        if x > 0 and x <= 0.5:
            return 0
        elif x > 0.5 and x <= 0.6:
            return 1
        elif x > 0.6 and x <= 0.68:
            return 2
        elif x > 0.68 and x <= 0.72:
            return 3
        elif x > 0.72 and x <= 0.76:
            return 4
        elif x > 0.76 and x <= 0.8:
            return 5
        elif x > 0.8 and x <= 0.85:
            return 6
        elif x > 0.85:
            return 7

    def gen_cr_bucket_rate(self, score, ecrdf, bin_costdf):
        cr_col = 'score'
        score = score[score[cr_col] > 0]
        score['bin'] = score[cr_col].apply(self.gen_cr_bucket)
        score['bin_val'] = score['bin'].apply(lambda x: (x + 1) / 8 if x <= 3 else (0.2 * (x + 1) if x <= 5 else 0.25 * x))

        gmvdf = self.df[['city_id', 'stat_date', 'total_gmv']].drop_duplicates()  # candidate表
        weightdf = pd.merge(score, gmvdf, on=['city_id', 'stat_date'])
        weightdf['weight'] = weightdf.apply(lambda x: x['total_gmv'] * x['bin_val'], axis=1)

        df_li = []
        ecr_bins = ecrdf.ecr_bin.unique()
        print("ecr bins:", ecr_bins)
        for ecr_bin in ecr_bins:
            tmpdf = ecrdf.query("ecr_bin==%d" % ecr_bin)[['city_id', 'stat_date']]
            tmpdf2 = pd.merge(weightdf, tmpdf)
            if len(tmpdf2) > 0:
                bin_budget = bin_costdf.query("ecr_bin==%d" % ecr_bin)['budget'].iloc[0] # ecr分框预算
                avg_rate = (bin_budget) / (tmpdf2['weight'].sum())  # 泛快呼返
                print("avg_rate:", avg_rate)
                tmpdf2['avg_rate'] = tmpdf2['bin_val'].apply(lambda x: avg_rate * x)
                tmpdf2['ecr_bin'] = ecr_bin
                df_li.append(tmpdf2)
            else:
                continue
        cr_ratedf = pd.concat(df_li, ignore_index=True)
        return cr_ratedf


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 25 08:48:49 2019

@author: mc
"""

from InstagramAPI import InstagramAPI
from mc_get_all_comments import get_all_comments
from service_functions import get_id
import time
from threading import Thread
import json
from read_credential import credential
import datetime
import os
from pytz import reference
import requests
import string
import re

import sys
sys.path.append('../')

'''
# =============================================================================
# download all the info about storiline:
#     medias
#     comments
#     suggested users
#     follow suggested users
# =============================================================================
'''
from make_dir import make_dir
#make_dir('..')

popular_hashtags = {}

try: party = sys.argv[1]
except IndexError: party = 'LEGA'

def get_popular_hashtag(pary):
    with open('../partyMedias/%s_medias.json'%party, 'r') as fp:
        medias = json.load(fp)

    RE_EMOJI = re.compile('[\U00010000-\U0010ffff]', flags=re.UNICODE)
    def strip_emoji(text):
        return RE_EMOJI.sub(r'', text)

    my_punt = string.punctuation.replace('#', '')
    my_punt +='\n'
    media_strange = []
    for media in medias:

        try :
            text = media['caption']['text']
            text = text.translate(str.maketrans('', '', my_punt))
            text = strip_emoji(text)

            text = text.split(' ')
            for word in text:
                if '#' in word:
                    word = word.lower().rstrip().lstrip()
                    if word[0] == '#':
                        continue
                    else:
                        word =  word.split('#')[1]
                        word = '#'+word

                    if word not in popular_hashtags.keys():
                        popular_hashtags[word] = 1
                    else:
                        popular_hashtags[word] +=1
        except TypeError:
            media_strange.append(media)

    sorted_x = sorted(popular_hashtags.items(), key=lambda kv: kv[1])[::-1]
    return sorted_x

#for party in ['LEGA', 'M5S', 'PD']:
for party in ['matteosalvini']:
    sorted_hasthags = get_popular_hashtag(party)
#    fp = open('../MEDIA/%s/Hashtags/%s_popular_hashtags.txt'%(party,party), 'w')
#    for hashtag in sorted_hasthags:
#        fp.write(hashtag[0] + '\n')
#    fp.close()


from django.views.generic import DetailView, ListView

from traces.models import RouteByCities
from traces.models import RouteBySights

class RouteByCitiesList(ListView):
    model = RouteByCities
    context_object_name = 'route_cities_list'
    template_name = 'traces/route_cities_list.html'

class RouteByCitiesDetail(DetailView):
    model = RouteByCities
    context_object_name = 'route_city_detail'
    template_name = 'traces/route_cities_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        cities = self.object.cities.all()
        index_middle_city = len(cities) // 2
        context['first_city'] = cities[0]
        context['middle_city'] = cities[index_middle_city]
        context['last_city'] = cities[len(cities) - 1]
        return context

class RouteBySightsList(ListView):
    model = RouteBySights
    context_object_name = 'route_sights_list'
    template_name = 'traces/route_sights_list.html'
    paginate_by = 2

class RouteBySightsDetail(DetailView):
    model = RouteBySights
    context_object_name = 'route_sight_detail'
    template_name = 'traces/route_sights_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        sights = self.object.sights.all()
        index_middle_sight = len(sights) // 2
        context['first_sight'] = sights[0]
        context['middle_sight'] = sights[index_middle_sight]
        context['last_sight'] = sights[len(sights) - 1]
        return context


"""
Time utility tools that aim to parse any date/time into either a
local time or a UTC time.
"""
import datetime as dt
import dateutil.parser as dp
from dateutil import tz
import pytz
from dateutil.relativedelta import relativedelta

DAY_SECS = 60 * 60 * 24

# Build a timezone map that includes daylight savings time-zones.
TZ_MAP = {}
now = dt.datetime.utcnow()
summer_day = dt.datetime(now.year, 7, 1)
winter_day = dt.datetime(now.year, 1, 1)
for zone in pytz.all_timezones:
    for day in (summer_day, winter_day):
        try:
            tzdate = pytz.timezone(zone).localize(day, is_dst=None)
            TZ_MAP[tzdate.tzname()] = tz.gettz(zone)
        except pytz.NonExistentTimeError:
            pass

# Local timezone
TZ_LOCAL = dt.datetime.now(dt.timezone.utc).astimezone().tzinfo

def days_from_month(dt):
    """
    Calculates numbers of days from beginning of the month and to the
    end of the month for 'point_dt'. Examples:
    days_from_month(dp.parse('2014-04-01'))
    >(0, 30)
    days_from_month(dp.parse('2014-04-02'))
    >(1, 29)
    days_from_month(dp.parse('2014-04-30'))
    >(29, 1)
    """
    dt = to_utc(dt)
    start_day = to_utc(dt.strftime('%Y-%m-01'))
    end_day = to_utc(
        (dt + relativedelta(months=1)).strftime('%Y-%m-01'))
    return (int((dt - start_day).total_seconds() / DAY_SECS),
            int((end_day - dt).total_seconds() / DAY_SECS))

def is_local(d):
    """ Returns true if datetime 'd' has the local timezone. """
    if is_tz_aware(d):
        dz = dt.datetime(d.year, d.month, d.day)
        return d.strftime('%Z') == to_local(dz).strftime('%Z')
    return False

def is_tz_aware(d):
    """ Returns True if datetime 'd' is timezone aware."""
    return d.tzinfo is not None and d.tzinfo.utcoffset(d) is not None

def is_utc(d):
    """ Returns true if datetime 'd' has the UTC timezone. """
    return is_tz_aware(d) and d.strftime('%Z') == to_utc().strftime('%Z')

def last_day_in_week(date):
    """ Finds the last day in week for 'date'. """
    dx = to_utc(date)
    d0 = dx.replace(hour=0, minute=0, second=0, microsecond=0)
    wk = dx.strftime('%Y-%W')
    td = dt.timedelta(days=1)
    for i in range(1, 8):
        d1 = d0 + td
        if d1.strftime('%Y-%W') != wk:
            return d0
        d0 = d1
    return d0

def to_local(d=None):
    """
    Produce a local timezone aware date from 'd' if given or using the current
    datetime if 'd' is None. If 'd' is a string then parse it as a date/time.
    If the date/time has a timezone then offset to local timezone. If there is
    no timezone then assume the date as given is local.
    """
    if d is not None:
        if not isinstance(d, dt.datetime):
            d = dp.parse(d, tzinfos=TZ_MAP)
        if is_tz_aware(d):
            dz = dt.datetime(d.year, d.month, d.day)
            return d.astimezone(dz.tzinfo)
        else:
            return d.astimezone()
    return dt.datetime.now().astimezone(TZ_LOCAL)

def to_utc(d=None):
    """
    Produce a UTC timezone aware date from 'd' if given or using the current
    datetime if 'd' is None. If 'd' is a string then parse it as a date/time.
    If the date/time has a timezone then offset to UTC. If there is no
    timezone then assume the date as given is UTC.
    """
    if d is not None:
        if not isinstance(d, dt.datetime):
            d = dp.parse(d, tzinfos=TZ_MAP)
        if is_tz_aware(d):
            return d.astimezone(tz.tzutc())
        else:
            return d.replace(tzinfo=dt.timezone.utc)
    return dt.datetime.now(dt.timezone.utc)

def truncate_to_day(d):
    """ Truncate datetome 'd' to day granularity. """
    return d.replace(hour=0, minute=0, second=0, microsecond=0)

def truncate_to_hour(d):
    """ Truncate datetome 'd' to hour granularity. """
    return d.replace(minute=0, second=0, microsecond=0)

def truncate_to_minute(d):
    """ Truncate datetome 'd' to minute granularity. """
    return d.replace(second=0, microsecond=0)

def truncate_to_second(d):
    """ Truncate datetome 'd' to minute granularity. """
    return d.replace(microsecond=0)


import logging
from operator import itemgetter

from distance import levenshtein
from aiohttp_xmlrpc.client import ServerProxy

LOG = logging.getLogger(__name__)

class PypiPlugin:
    __name__ = "pypi"
    SEARCH_URL = "https://pypi.python.org/pypi"
    ROOT_URL = "https://pypi.org"
    PROJECT_URL = ROOT_URL + "/project/{0}"
    RESULT_URL = ROOT_URL + "/search/?q={0}"

    def __init__(self):
        self.api = None

    def load(self, sirbot):
        self.api = ServerProxy(self.SEARCH_URL, client=sirbot.http_session)

    async def search(self, search):
        results = await self.api.search({"name": search})
        for item in results:
            item["distance"] = levenshtein(str(search), item["name"])
        results.sort(key=itemgetter("distance"))
        return results


import os
from PIL import Image

inverse = True
f = "ww.png"

def main():
    img = Image.open(os.path.join("images", f))  # Open the file
    px = img.load()  # Put the pixels in an array

    t = "# {}\n{} = [\n".format(img.size, f[0: -4])
    for y in range(img.size[1]):  # for each lines of pixels
        t += "["
        for x in range(img.size[0]):  # for each pixels from that line
            if px[x, y] == 0:
                t += "1" if inverse else "0"
            else:
                t += "0" if inverse else "1"
            # Do not add a comma at the end if it's the last item
            t += "," if x < img.size[0] - 1 else ""
        t += "]"
        if y < img.size[1] - 1:
            # Do not add a comma at the end if it's the last item
            t += ","
        t += "\n" # Add a new line
    t += "]\n"

    # Sets up the filename of the ouput file. Adds _inverse if the image is inversed
    outfile = "{}{}.py".format(f[0: -4], "_inverse" if inverse else "")
    outfile = os.path.join("output", outfile)
    out = open(outfile, "w")
    out.write(t) # Write the string to the output file
    out.close()
    # Remove the commas and brackets so it looks better in the terminal
    t = t.replace(",", "").replace("[", "").replace("]", "")
    print(t)

if __name__ == "__main__":
    main()


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

import numpy as np
import cv2

img=cv2.imread('./resources/panad.jpg')
print(img.shape)
cv2.imshow('origin', img)

shift_mat=np.float32([[1, 0, -100],[0, 1, -200]])
shift = cv2.warpAffine(img, shift_mat, img.shape[0:2])
cv2.imshow('shift', shift)

cv2.waitKey(0)
cv2.destroyAllWindows()


#!/usr/bin/python3
"""
This module contains class Base
"""
import json

class Base:
    """Represents the base model.

    Attributes:
        __nb_objects (int): The number of instantiated Bases.
    """
    __nb_objects = 0

    def __init__(self, id=None):
        """Initialize a new Base.

        Args:
            id (int): The identity of the new Base.
        """
        if id is not None:
            self.id = id
        else:
            Base.__nb_objects += 1
            self.id = Base.__nb_objects

    @staticmethod
    def to_json_string(list_dictionaries):
        """
        Static method
        Returns:
            the JSON string representation of list_dictionaries
        """
        if list_dictionaries is None or list_dictionaries == []:
            return "[]"
        else:
            return json.dumps(list_dictionaries)

    @classmethod
    def save_to_file(cls, list_objs):
        """
        Writes the JSON string representation of list_objs to a file
        Args:
            list_objs (list): List of instances who inherits of Base
        """
        if list_objs is None:
            list_objs = []
        filename = cls.__name__ + ".json"
        json_string = [obj.to_dictionary() for obj in list_objs]
        with open(filename, "w") as f:
            f.write(Base.to_json_string(json_string))

    @staticmethod
    def from_json_string(json_string):
        """
        Static method
        Returns:
            the list of the JSON string representation
        """
        if json_string is None or json_string == []:
            return []
        else:
            return json.loads(json_string)

    @classmethod
    def create(cls, **dictionary):
        """
        Class method
        Args:
            **dictionary (dict): key/value pairs
        Returns:
            an instance with all attributes already set
        """
        if cls.__name__ == "Rectangle":
            dummy = cls(1, 1)
        else:
            dummy = cls(1)
        dummy.update(**dictionary)
        return dummy

    def load_from_file(cls):
        """
        Class method
        Returns:
            a list of instances
        """
        filename = cls.__name__ + ".json"
        try:
            with open(filename, "r") as f:
                list_dicts = Base.from_json_string(jsonfile.read())
                return [cls.create(**d) for d in list_dicts]
        except FileNotFoundError:
            pass
        return []


import warnings
warnings.filterwarnings('ignore')
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from PIL import Image
import numpy as np
import torch
import cv2
import math
from typing import List, Callable, Optional
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget

""" Model wrapper to return a tensor"""
class HuggingfaceToTensorModelWrapper(torch.nn.Module):
  def __init__(self, model):
    super(HuggingfaceToTensorModelWrapper, self).__init__()
    self.model = model

  def forward(self, x):
    return self.model(x).logits

""" Translate the category name to the category index.
    Some models aren't trained on Imagenet but on even larger datasets,
    so we can't just assume that 761 will always be remote-control.

"""
def category_name_to_index(model, category_name):
  name_to_index = dict((v, k) for k, v in model.config.id2label.items())
  return name_to_index[category_name]

""" Helper function to run GradCAM on an image and create a visualization.
    (note to myself: this is probably useful enough to move into the package)
    If several targets are passed in targets_for_gradcam,
    e.g different categories,
    a visualization for each of them will be created.

"""
def run_grad_cam_on_image(model: torch.nn.Module,
                          target_layer: torch.nn.Module,
                          targets_for_gradcam: List[Callable],
                          reshape_transform: Optional[Callable],
                          input_tensor: torch.nn.Module,
                          input_image: Image,
                          method: Callable=GradCAM):
  with method(model=HuggingfaceToTensorModelWrapper(model), target_layers=[target_layer], reshape_transform=reshape_transform) as cam:

    # Replicate the tensor for each of the categories we want to create Grad-CAM for:
    repeated_tensor = input_tensor[None, :].repeat(len(targets_for_gradcam), 1, 1, 1)

    batch_results = cam(input_tensor=repeated_tensor, targets=targets_for_gradcam)
    results = []
    for grayscale_cam in batch_results:
      visualization = show_cam_on_image(np.float32(input_image)/255, grayscale_cam, use_rgb=True)
      visualization = cv2.resize(visualization, (visualization.shape[1], visualization.shape[0]))
      results.append(visualization)
    return [batch_results, results]

def reshape_transform_vit_huggingface(x):
  print(x.shape)
  activations = x[:, 1:, :]
  magic_number = int(math.sqrt(activations.shape[1]))
  print(magic_number)
  activations = activations.view(activations.shape[0], magic_number, magic_number, activations.shape[2])
  activations = activations.transpose(2, 3).transpose(1, 2)
  return activations

## dive into reshape transform
def get_map(model, predicted_labels, inputs_tensor, image_resized):
  target_layer_gradcam = model.vit.encoder.layer[-2].output
  targets_for_gradcam = [ClassifierOutputTarget(i) for i in predicted_labels]
  return run_grad_cam_on_image(model=model,
                      target_layer=target_layer_gradcam,
                      targets_for_gradcam=targets_for_gradcam,
                      input_tensor=inputs_tensor.pixel_values[0],
                      input_image=image_resized,
                      reshape_transform=reshape_transform_vit_huggingface)


# 转换APP国际化翻译字符重构汇总.xlsx文件中的简体中文到繁体中文

import re
import xlrd
import xlwt
import codecs
import os
from langconv import *

def Simplified2Traditional(sentence):
    sentence = Converter('zh-hant').convert(sentence)
    return sentence

target_path = 'D:/WorkProjects/SmartHomeV6Code_andriod_develop/SmartHome/src/main/res/'
app_source_file = './APP国际化翻译字符重构汇总.xlsx'
app_zh_column = 4
h5_source_file = './H5国际化翻译字符重构汇总.xlsx'
h5_zh_column = 3

def convert_safe_string(text):
    s = str(text).replace('<font>', '[font]').replace('</font>', '[/font]') \
        .replace('\n', '\\n') \
        .replace('\'', '\\\'') \
        .replace('&', '&')
    return re.sub('%\D|%$', lambda x: "%%" + x.group()[1:], s)

def read_excel():
    outfile = xlwt.Workbook()
    # 新建一个sheet
    wtable = outfile.add_sheet('info', cell_overwrite_ok=True)

    data = xlrd.open_workbook(app_source_file)
    table = data.sheets()[0]
    nrows = table.nrows
    write_column = 0
    wtable.write(0, write_column, "APP")
    for i in range(1, nrows):
        value = str(table.row_values(i)[app_zh_column])
        # 写入数据table.write(行,列,value)
        wtable.write(i, write_column, Simplified2Traditional(value))
    print('读取完毕 app')

    data = xlrd.open_workbook(h5_source_file)
    table = data.sheets()[0]
    nrows = table.nrows
    write_column = 1
    wtable.write(0, write_column, "H5")
    for i in range(1, nrows):
        value = str(table.row_values(i)[h5_zh_column])
        # 写入数据table.write(行,列,value)
        wtable.write(i, write_column, Simplified2Traditional(value))
    print('读取完毕 h5')
    # 保存文件
    outfile.save('zht.xls')

if __name__ == '__main__':
    read_excel()


import datetime, requests, json, psycopg2, psycopg2.extras, re, ast
from collections import defaultdict
from utils import *

class Spider:
    base_url = 'http://search.51job.com/jobsearch/search_result.php?funtype=0100,2400,2500,2600,2700&ord_field=1&jobarea='
    prefix = 'woou'
    skipped = False

    def __init__(self, city, max_page = 10):
        r = requests.get('http://js.51jobcdn.com/in/js/2009/jobarea_array_c.js')
        self.city_dict = {y:x for x,y in ast.literal_eval('{' + re.sub('\]=', ':', re.sub(';', ',', re.sub('var ja=\[\];|ja\[|\r\n', '', r.text))) + '}').items()}
        self.city_code = self.city_dict[city]
        self.city = city
        self.max_page = max_page

    def start_request(self):
        url = self.base_url + self.city_code
        self.parse_list(url)

    def parse_list(self, url, pn = 1):
        if pn > self.max_page and self.skipped:
            print('Out of pn: ' + str(pn) + ', exit')
            return -2
        try:
            cookies = dict(guide = '1')
            r = requests.get(url + '&curr_page=' + str(pn), timeout=5, cookies=cookies)
            r.encoding = 'gb2312'
        except requests.exceptions.Timeout:
            print('Read list timeout')
            return -1
        print(self.prefix + '------------------- NOW PAGE: ' + str(pn))
        if self.parse_list_html(r.text) == 0 or pn == 1:
            self.parse_list(url, pn = pn + 1)

    def parse_list_html(self, list_html):
        html_list = html_split(list_html,'#resultList > div.el:not(.mk)')
        for single_html in html_list:
            if self.parse_single_html(single_html) == -1:
                print('Out of date, stop.')
                return -1
        if len(html_split(list_html, 'li.bk > a[href]')) == 2 and len(html_list) > 0:
            return 0
        else:
            print('No further page, stop.')
            return -2

    def parse_single_html(self, single_html):
        global fuck_html
        fuck_html = single_html
        info_dict = html_to_dict(single_html,
                                 [
                                     {
                                         'name': 'job_url',
                                         'selector': 'p.t1 > a[href^="http://jobs.51job.com/"]',
                                         'attribute': 'href'
                                     },
                                     {
                                         'name': 'job_id',
                                         'selector': 'p.t1 > input',
                                         'attribute': 'value'
                                     },
                                     {
                                         'name': 'create_date',
                                         'selector': 'span.t5'
                                     },
                                     {
                                         'name': 'position_name',
                                         'selector': 'p.t1 > a[href^="http://jobs.51job.com/"]'
                                     },
                                     {
                                         'name': 'company_name',
                                         'selector': 'span.t2 > a'
                                     },
                                     {
                                         'name': 'company_url',
                                         'selector': 'span.t2 > a',
                                         'attribute': 'href'
                                     }
                                 ])
        global fuck_dict
        fuck_dict = info_dict
        if info_dict['create_date'] != datetime.datetime.now().strftime("%m-%d"):
            return -1
        print(self.prefix + ' - // ' + info_dict['position_name'] + ' - ' + info_dict['company_name'] + ' - ' + self.city + ' - ' + info_dict['create_date'])
        records = find_job(self.prefix + '_' + info_dict['job_id'])
        if len(records) > 0:
            update_job([datetime.datetime.now(), self.prefix + '_' + job_id,])
            self.skipped = True
            print('J-*')
        elif info_dict['job_url'] != '':
            try:
                r = requests.get(info_dict['job_url'], timeout=5)
                r.encoding = 'gb2312'
            except requests.exceptions.Timeout:
                print('Read job timeout')
                return -1
            # find the full company name(instead of brand, such as: 滴滴打车 -> 北京小桔科技有限公司)
            company_name = self.parse_company(info_dict['company_url'])
            self.parse_job(info_dict['job_id'], company_name, info_dict['job_url'], r.text)

    def parse_job(self, job_id, company_name, job_url, job_html):
        info_dict = html_to_dict(job_html,
                     [
                         {
                             'name': 'title',
                             'selector': 'div.tCompany_center.clearfix > div.tHeader.tHjob > div > div.cn > h1'
                         },
                         {
                             'name': 'salary',
                             'selector': 'div.tCompany_center.clearfix > div.tHeader.tHjob > div > div.cn > strong'
                         },
                         {
                             'name': 'work_year',
                             'selector': 'div.tCompany_center.clearfix > div.tCompany_main > div.tBorderTop_box.bt > div > div > span',
                             'contains': ''
                         },
                         {
                             'name': 'address',
                             'selector': 'div.tCompany_center.clearfix > div.tCompany_main > div > div > p.fp',
                             'lambda': lambda rr,rd: re.sub('上班地址：','',rr).strip()
                         },
                         {
                             'name': 'location',
                             'lambda': lambda rr,rd: get_gps(self.city, rd['address'])
                         },
                         {
                             'name': 'description',
                             'selector': 'div.tCompany_center.clearfix > div.tCompany_main > div > div.job_msg',
                             'lambda': lambda rr,rd: re.sub('职位描述：|举报|分享','',rr).strip()
                         }
                     ])
        salary_range = defaultdict(lambda: 0, (enumerate(re.findall('\d+', info_dict['salary']))))
        work_year_range = defaultdict(lambda: 0, (enumerate(re.findall('\d+', info_dict['work_year']))))
        # get numbers
        salary_factor = 1
        if '年' in info_dict['salary']:
            salary_factor = 10000/12
        info_dict['min_salary'] = int(salary_range[0]) * salary_factor
        info_dict['max_salary'] = int(salary_range[1]) * salary_factor

        info_dict['min_work_year'] = int(work_year_range[0])
        info_dict['max_work_year'] = int(work_year_range[1])
        # del raw data
        del info_dict['salary']
        del info_dict['work_year']
        # fill extra data
        info_dict['id'] = self.prefix + '_' + job_id
        info_dict['city'] = self.city
        info_dict['company'] = company_name
        info_dict['raw_data'] = job_html
        info_dict['raw_url'] = job_url
        info_dict['update_date'] = datetime.datetime.now()
        # insert it
        insert_data('job', info_dict)
        print('Inserting Job')

    def parse_company(self, company_url):
        try:
            r = requests.get(company_url, timeout=5)
            r.encoding = 'gb2312'
        except requests.exceptions.Timeout:
            print('Read company timeout')
            return -1

        info_dict = html_to_dict(r.text,[
            {
                'name': 'short_name',
                'selector': 'div.tCompanyPage > div.tCompany_center.clearfix > div.tHeader.tHCop > div > h1'
            },
            {
                'name': 'description',
                'selector': 'div.tCompany_center.clearfix > div.tCompany_full > div.tBorderTop_box.bt > div > div.con_msg'
            },
            {
                'name': 'industry',
                'selector': 'div.tCompanyPage > div.tCompany_center.clearfix > div.tHeader.tHCop > div > p.ltype',
                'lambda': lambda rr,rd: re.sub('.*\|.*\|','', rr).strip()
            },
            {
                'name': 'address',
                'selector': 'div.tCompanyPage > div.tCompany_center.clearfix > div.tCompany_full > div > div > p.fp',
                'lambda': lambda rr,rd: re.sub('公司地址：|(邮编：.*)','', rr).strip()
            },
            {
                'name': 'location',
                'lambda': lambda rr,rd: get_gps(self.city, rd['address'])
            },
            {
                'name': 'website',
                'selector': 'div.tCompany_center.clearfix > div.tCompany_full > div > div > p > a',
                'attribute': 'href'
            },
            {
                'name': 'name_string',
                'selector': 'div.tCompany_full > p.tPosition_center_bottomText'
            }])

        if info_dict['name_string']:
            info_dict['name'] = re.sub('\*发布本招聘广告企业的营业执照名称：','',info_dict['name_string']).strip()
        else:
            info_dict['name'] = info_dict['short_name']
        # check if exists
        records = find_company(info_dict['name'])
        if len(records) > 0:
            print('C-S')
        else:
            # fill extra data
            info_dict['raw_data'] = r.text
            info_dict['raw_url'] = company_url
            info_dict['update_date'] = datetime.datetime.now()
            del info_dict['name_string']
            # insert it
            insert_data('company', info_dict)
            print('Inserting Company')
        return info_dict['name']