#######Machine Learning Deployment######
##Libraries
import tkinter
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from tkinter.filedialog import askopenfilename
##
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
##
from sklearn.cluster import KMeans
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering
#--------------------xxx--------------------------------
###Windows creation
class window:
    #browse dataset
    def window1(self):
        window1 = Tk()
        window1.title('ML Algorithms')
        window1.config(bg='lightblue')
        window1.state('zoomed')

        label1 = Label(window1,text='Machine Learning Algorithms',bg='lightblue',font=('helvetica',18,'bold')).place(x=520,y=150)
        label2 = Label(window1,bg='lightyellow',relief='solid').place(x=500,y=300,height=200,width=400)
        label3 = Label(window1,text='Upload your Dataset',bg='lightyellow',font=('helvetica',12,'bold')).place(x=615,y=320)

        def browse():
            global file,data
            file = filedialog.askopenfilename(initialdir="F:/",filetypes = [("csv files","*.csv")])
            entry1.insert(0,file)
            data = pd.read_csv(file)

        entry1 = Entry(window1)
        entry1.place(x=543,y=380,height=40,width=320)
        entry1.configure(bg='lightblue')

        button1 = Button(window1,bg='lightblue',text='Browse',command= browse).place(x=765,y=380,height=40,width=100)
        button2 = Button(window1,bg='lightblue',text='Next',command= w.window2).place(x=645,y=440,height=30,width=100)

        window1.mainloop()

    ##Confirm dataset
    def window2(self):
        window2 = Tk()
        window2.title('ML Algorithms')
        window2.configure(bg='lightblue')
        window2.state('zoomed')

        label1 = Label(window2,text='Confirm your Dataset',bg='lightblue',font=('helvetica',18,'bold'))
        label1.pack(pady=120)

        frame = Frame(window2)
        frame.pack(padx=20)

        scroll1 = ttk.Scrollbar(frame,orient='vertical')
        scroll1.pack(side=RIGHT,fill=Y)

        scroll2 = ttk.Scrollbar(frame,orient='horizontal')
        scroll2.pack(side=BOTTOM,fill=X)

        global tview
        tview = ttk.Treeview(frame, yscrollcommand=scroll1.set, xscrollcommand=scroll2.set)
        tview.pack()

        scroll1.config(command=tview.yview)
        scroll2.config(command=tview.xview)

        tview["column"]=list(data.columns)
        tview["show"]="headings"

        for column in tview["column"]:
            tview.heading(column, text=column)
        df_rows = data.to_numpy().tolist()
        for row in df_rows:
            tview.insert("","end",values=row)
        tview.pack()

        button1 = Button(window2,text='Open',command=w.window3)
        button1.place(x=860,y=570,height=30,width=100)
        button2 = Button(window2,text='Back',command=w.window1)
        button2.place(x=980,y=570,height=30,width=100)

        window2.mainloop()
    ##Selecting features
    def window3(self):
        window3=Tk()
        window3.configure(background="lightblue")
        window3.title('ML Algorithms')
        window3.state('zoomed')

        label1 = Label(window3,relief='solid')
        label1.place(x=730,y=100,width=600,height=500)

        label2 = Label(window3, text="Selected Features", font=("Helvetica", 14, 'bold'))
        label2.pack()
        label2.place(x=930,y=120)

        label3 = Label(window3, text="Select\nIndependent Variables", fg='#000000', bg="lightblue" ,font=("helvetica", 14, 'bold'))
        label3.place(x=70,y=100,width=220,height=50)

        label4 = Label(window3, text="Select\nDependent Variables", fg='#000000', bg="lightblue", font=("helvetica", 14, 'bold'))
        label4.place(x=420,y=100,width=220,height=50)

        frame1 = Frame(window3)
        frame1.place(x=30,y=180,width=300,height=400)

        scrollbar1 = Scrollbar(frame1)
        scrollbar1.pack(side=RIGHT,fill=BOTH )

        listbox1 = Listbox(frame1, selectmode=MULTIPLE)
        listbox1.config(yscrollcommand=scrollbar1.set)
        listbox1.place(width=300, height=400)

        scrollbar1.config(command=listbox1.yview)

        j=0
        for i in tview["column"]:
            listbox1.insert(j, i)
            j = j + 1

        frame2 = Frame(window3)
        frame2.place(x=370, y=180, width=300, height=400)

        scrollbar2 = Scrollbar(frame2)
        scrollbar2.pack(side=RIGHT, fill=BOTH)

        listbox2 = Listbox(frame2, selectmode=SINGLE)
        listbox2.config(yscrollcommand=scrollbar2.set)
        listbox2.place(width=300, height=400)

        scrollbar2.config(command=listbox2.yview)

        j = 0
        for i in tview["column"]:
            listbox2.insert(j, i)
            j = j + 1

        def indep_select():
            global indep, indep1
            indep = []
            indep1 = []
            label34 = Label(window3, text="Independent Variables",font=('helvetica',14,'bold'))
            label34.place(x=770, y=170)
            clicked = listbox1.curselection()
            z = 220
            for item in clicked:
                label10 = Label(window3, text=listbox1.get(item),font=('helvetica',9))
                label10.place(x=770, y=z)
                indep.append(item)
                indep1.append(listbox1.get(item))
                z = z + 25

        button1 = Button(window3,text="Choose",command=indep_select,font=("Arial Black", 9, 'bold'))
        button1.place(x=125,y=600,width=100, height=40)

        def dep_select():
            global dep,dep1
            dep = []
            dep1 = []
            label34 = Label(window3, text="Dependent Variables ",font=('helvetica',14,'bold'))
            label34.place(x=1080, y=170)
            clicked = listbox2.curselection()
            z = 220
            for item in clicked:
                label10 = Label(window3, text=listbox2.get(item),font=('helvetica',9))
                label10.place(x=1080, y=z)
                dep.append(item)
                dep1.append(listbox2.get(item))
                z = z + 25

        button2 = Button(window3, text="Choose", command=dep_select,font=("Arial Black", 9, 'bold'))
        button2.place(x=455, y=600, width=100, height=40)

        button3 = Button(window3, text = "Confirm",command=w.window4,font=("Arial Black", 9, 'bold'))
        button3.place(x=840, y=620, height=50, width=180)

        button4 = Button(window3, text = "Back",font=("Arial Black", 9, 'bold'))
        button4.place(x=1050, y=620, height=50, width=180)
        button4.configure(command=w.window2)

        window3.mainloop()

    ##Reg,class,cluster
    def window4(self):
        window4 = Tk()
        window4.configure(bg="lightblue")
        window4.title("ML Algorithms")
        window4.state('zoomed')

        label1 = Label(window4, text="Select the Problem Type",bg='lightblue', font=("helvetica", 16, 'bold'))
        label1.place(x=580,y=200)

        label2 = Label(window4,relief='solid')
        label2.place(x=300,y=300,height=160,width=800)

        button1 = Button(window4, text="Regression", bg="lightblue",command=w.regression)
        button1.place(x=340,y=360,width=130,height=40)

        button2 = Button(window4, text="Classification", bg="lightblue",command=w.classification)
        button2.place(x=640,y=360,width=130,height=40)

        button3 = Button(window4, text="Clustering", bg="lightblue", command=w.clustering)
        button3.place(x=930,y=360,width=130,height=40)

        window4.mainloop()

    ##Regression Algorithms
    def regression(self):
        window5 = Tk()
        window5.configure(bg="lightblue")
        window5.title("ML - Regression Algorithms")
        window5.state('zoomed')

        global algos

        algos="Regression"

        label1 = Label(window5, text="Select the Alogirithm",bg="lightblue", font=("helvetica",18,"bold"))
        label1.place(x=560,y=180)

        label2 = Label(window5,relief='solid')
        label2.place(x=435,y=260,height=200,width=500)

        def comboclick(event):
            x = data.iloc[:, indep].values
            y = data.iloc[:, dep].values
            global x_train,y_train

            global train_accuracy,test_accuracy,regressor,entryp

            if (combo.get() == "Simple Linear Regression"):
                x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25,random_state=0)
                # regressor=LinearRegression()
                # regressor.fit(x_train,np.ravel(y_train))
                # train_accuracy=regressor.score(x_train,y_train)
                # test_accuracy=regressor.score(x_test,y_test)

            elif (combo.get() == "Multiple Linear Regression"):

                # Splitting the dataset into training and test set.
                x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
                # regressor = LinearRegression()
                # regressor.fit(x_train, y_train)
                # train_accuracy = regressor.score(x_train, y_train)
                # tesst_accuracy = regressor.score(x_test, y_test)

            elif (combo.get() =="Polynomial Regression"):

                # Training the dataset in polynomial regression
                # global poly_reg

                label = Label(window5,text='Enter Polynomial degree',bg='lightblue')
                label.place(x=550,y=350,heigh=30)

                entryp = Entry(window5,text='poly degree',bg='lightblue')
                entryp.place(x=700,y=350,heigh=30)

                # poly_reg = PolynomialFeatures(degree=5)
                # x_poly = poly_reg.fit_transform(x)

                # regressor = LinearRegression()
                # regressor.fit(x_poly,np.ravel(y))
                # train_accuracy = regressor.score(x_poly,y)

            elif(combo.get()=="SVM Regression"):
                global sc_x,sc_y
                x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                sc_x = StandardScaler()
                sc_y = StandardScaler()
                x = sc_x.fit_transform(x)
                y = sc_y.fit_transform(y)

                regressor = SVR(kernel='rbf')
                regressor.fit(x_train,y_train)
                y_pred=regressor.predict(x_test)

                train_accuracy = regressor.score(x_train,y_train)
                test_accuracy = regressor.score(x_test,y_test)

            elif(combo.get()=="Decision Tree Regression"):
                x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                regressor = DecisionTreeRegressor(random_state=0)
                regressor.fit(x_train,y_train)
                y_pred=regressor.predict(x_test)

                train_accuracy = regressor.score(x_train,y_train)
                test_accuracy = regressor.score(x_test,y_test)

            elif(combo.get()=="Random Forest Regression"):
                x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                regressor = RandomForestRegressor(n_estimators=10,random_state=0)
                regressor.fit(x_train,y_train)
                train_accuracy = regressor.score(x_train,y_train)
                test_accuracy = regressor.score(x_test,y_test)
                y_pred=regressor.predict(x_test)

            else:
                pred = regressor.predict([result])
                label64 = Label(window6,text="Click next to view the Prediction")
                label64.place(x=260,y=z+100)

        options = ["Simple Linear Regression", "Multiple Linear Regression", "Polynomial Regression","SVM Regression","Decision Tree Regression","Random Forest Regression"]
        global combo
        combo = ttk.Combobox(window5, value=options)
        combo.config(width=50)
        combo.current(0)
        combo.bind("<<ComboboxSelected>>", comboclick)
        combo.pack()
        combo.place(x=535,y=300,height=40,width=300)

        button6 = Button(window5, text='Train Model', command=w.window6)
        button6.pack()
        button6.config(bg='lightblue')
        button6.place(x=630,y=385,height=50,width=100)

        window5.mainloop()

    ##Classification Algorithms
    def classification(self):
        window8 = Tk()
        window8.configure(bg="lightblue")
        window8.state('zoomed')
        window8.title("ML- Classification Algorithms")

        global algos
        algos="Classification"

        label3 = Label(window8, text="Select the Algorithm", bg="lightblue", font=("helvetica",18,"bold"))
        label3.place(x=560,y=180)

        label2 = Label(window8,relief='solid')
        label2.place(x=435,y=260,height=200,width=500)

        def comboclick1(event):

            x = data.iloc[:, indep].values
            y = data.iloc[:, dep].values
            global x_train,y_train
            x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

            global sc
            sc = StandardScaler()
            x_train = sc.fit_transform(x_train)
            x_test = sc.transform(x_test)

            if(combo2.get()=="Logistic Regression"):
                print('logistic regression')
                # global classifier
                # classifier = LogisticRegression(random_state=0)
                # classifier.fit(x_train,y_train)

                # y_pred = classifier.predict(x_test)

                # cm = confusion_matrix(y_test,y_pred)
                # global acc
                # acc = (sum(np.diag(cm))/len(y_test))

            elif(combo2.get()=="Naive Bayes Classification"):
                classifier = GaussianNB()
                classifier.fit(x_train ,y_train)

                y_pred = classifier.predict(x_test)

                cm = confusion_matrix(y_test,y_pred)
                acc = (sum(np.diag(cm))/len(y_test))

            elif(combo2.get()=="K-Nearest Neighbour Classification"):
                classifier = KNeighborsClassifier(n_neighbors=5,metric="minkowski",p=2)
                classifier.fit(x_train,y_train)

                y_pred = classifier.predict(x_test)

                cm = confusion_matrix(y_test,y_pred)
                acc = sum(np.diag(cm))/len(y_test)

            elif(combo2.get()=="SVM Classification"):
                classifier = SVC(kernel='rbf',random_state=0)
                classifier.fit(X_train,y_train)

                y_pred=classifier.predict(X_test)

                cm = confusion_matrix(y_test,y_pred)
                acc = sum(np.diag(cm))/len(y_test)

            elif(combo2.get()=="Decision Tree Classification"):
                classifier = DecisionTreeClassifier(criterion='entropy',random_state=0)
                classifier.fit(x_train,y_train)

                y_pred = classifier.predict(x_test)

                cm = confusion_matrix(y_test,y_pred)
                acc = sum(np.diag(cm))/len(y_test)

            elif(combo2.get()=="Random Forest Classification"):
                classifier = RandomForestClassifier()
                classifier.fit(x_train,y_train)

                y_pred = classifier.predict(x_test)
                cm = confusion_matrix(y_test,y_pred)
                acc = sum(np.diag(cm))/len(y_test)

        options1 = ["Logistic Regression","Naive Bayes Classification","K-Nearest Neighbour Classification","SVM Classification","Decision Tree Classification","Random Forest Classification"]
        global combo2
        combo2 = ttk.Combobox(window8, value=options1)
        combo2.config(width=50)
        combo2.current(0)
        combo2.bind("<<ComboboxSelected>>", comboclick1)
        combo2.pack()
        combo2.place(x=535,y=300,height=40,width=300)

        button6 = Button(window8, text='Train Model',command=w.window6)
        button6.pack()
        button6.place(x=630,y=385,height=50,width=100)

        window8.mainloop()

    ##Clustering Algorithms
    def clustering(self):
        window9 = Tk()
        window9.configure(bg="lightblue")
        window9.state('zoomed')
        window9.title("ML-Clustering Algorithm")

        global algos
        algos = "clustering"

        label1 = Label(window9, text="Select the Algorithm", bg="lightblue", font=("helvetica", 18,"bold"))
        label1.place(x=560,y=180)

        label2 = Label(window9,relief='solid')
        label2.place(x=435,y=260,height=200,width=500)

        def comboclick2(event):
            X = data.iloc[:, indep].values
            global clusterkmeans,clustersch
            if(combo3.get()=="KMeans Clustering"):

                wcss=[]

                for i in range(1,11):
                    kmeans = KMeans(n_clusters=i,random_state=0)
                    kmeans.fit(X)
                    wcss.append(kmeans.inertia_)
                def plot():

                    plt.figure(figsize = (8,5), dpi=50)
                    plt.scatter(range(1,11),wcss)
                    plt.plot(range(1,11),wcss)
                    plt.title("Elbow Method")
                    plt.xlabel("Number of Clusters")
                    plt.ylabel("WCSS")
                    plt.show()

                button91 = Button(window9, text="KMeans - plot",bg='lightblue', command=plot)
                button91.place(x=500,y=350,width=120)

                label = Label(window9,text='Cluster size',bg='lightblue')
                label.place(x=630,y=350,height=30,width=100)

                clusterkmeans = Entry(window9,bg='lightblue')
                clusterkmeans.place(x=750,y=350,height=30)

            elif(combo3.get()=="Hierarchical Clustering"):

                def plot1():
                    plt.figure(figsize=(8,5),dpi=50)
                    dendrogram = sch.dendrogram(sch.linkage(X,method='ward'))
                    plt.title("Dendrogram")
                    plt.xlabel("X-Values")
                    plt.ylabel("Euclidean Distance")
                    plt.show()

                button = Button(window9, text="Dendrogram - plot",bg='lightblue', command=plot1)
                button.place(x=500,y=350,width=120)

                labell = Label(window9,text='Cluster size',bg='lightblue')
                labell.place(x=630,y=350,height=30,width=100)

                clustersch = Entry(window9,bg='lightblue')
                clustersch.place(x=750,y=350,height=30)

        options2 = ["Select Clustering Algorithm","KMeans Clustering","Hierarchical Clustering"]
        global combo3
        combo3 = ttk.Combobox(window9, value=options2)
        combo3.config(width=50)
        combo3.current(0)
        combo3.bind("<<ComboboxSelected>>", comboclick2)
        combo3.pack()
        combo3.place(x=535,y=300,height=40,width=300)

        button6 = Button(window9, text='Train Model', command=w.window7)
        button6.pack()
        button6.config(bg='lightblue')
        button6.place(x=630,y=385,height=50,width=100)

        window9.mainloop()
    #PREDICTION
    def window6(self):
        window6 = Tk()
        window6.configure(bg="lightblue")
        window6.state('zoomed')
        window6.title("ML-Algorithms")

        label61 = Label(window6, text="Index Name",  bg="lightblue", font=("helvetica", 20, 'bold'))
        label61.place(x=150, y=100, height=30)

        z=140
        global entry61, entries
        entries = []
        for i in indep1:
            label63 = Label(window6, text=i,bg="lightblue", font=("helvetica", 15, 'bold'))
            label63.place(x=145, y=z)

            entry61 = Entry(window6)
            entry61.place(x=420, y=z, height=30, width=150)
            entries.append(entry61)
            z = z + 25

        label62 = Label(window6, text="Index Values", bg="lightblue", font=("helvetica", 20, 'bold'))
        label62.place(x=415, y=90)

        def predict():
            global result
            result=[]
            for entry in entries:
                result.append(entry.get())

            if(algos=="Regression"):
                global pred

                if(combo.get()=="Simple Linear Regression"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25,random_state=0)
                    regressor=LinearRegression()
                    regressor.fit(x_train,np.ravel(y_train))
                    train_accuracy=regressor.score(x_train,y_train)
                    test_accuracy=regressor.score(x_test,y_test)

                    pred = regressor.predict([result])
                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=250)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    return train_accuracy,test_accuracy,pred
                elif(combo.get()=="Multiple Linear Regression"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25,random_state=0)
                    regressor=LinearRegression()
                    regressor.fit(x_train,np.ravel(y_train))
                    train_accuracy=regressor.score(x_train,y_train)
                    test_accuracy=regressor.score(x_test,y_test)

                    pred = regressor.predict([result])
                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=250)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    return train_accuracy,test_accuracy,pred

                elif(combo.get()=="Polynomial Regression"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    global poly_reg,x_poly

                    poly_reg = PolynomialFeatures(degree=int(entryp.get()))
                    x_poly = poly_reg.fit_transform(x)

                    regressor = LinearRegression()
                    regressor.fit(x_poly,y)
                    train_accuracy = regressor.score(x_poly,y)

                    pred = regressor.predict(poly_reg.fit_transform([result]))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)

                elif(combo.get()=="SVM Regression"):

                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    global sc_x,sc_y
                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    sc_x = StandardScaler()
                    sc_y = StandardScaler()
                    x_train = sc_x.fit_transform(x_train)
                    x_test = sc_x.fit_transform(x_test)
                    y_train = sc_y.fit_transform(y_train)
                    y_test = sc_y.fit_transform(y_test)

                    regressor = SVR(kernel='rbf')
                    regressor.fit(x_train,y_train)
                    y_pred=regressor.predict(x_test)

                    train_accuracy = regressor.score(x_train,y_train)
                    test_accuracy = regressor.score(x_test,y_test)

                    pred = sc_y.inverse_transform(regressor.predict(sc_x.transform(([result]))))
                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=250)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    return train_accuracy,test_accuracy,pred

                elif(combo.get()=='Decision Tree Regression'):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    regressor = DecisionTreeRegressor(criterion='mse',random_state=0)
                    regressor.fit(x_train,y_train)
                    y_pred=regressor.predict(x_test)

                    train_accuracy=regressor.score(x_train,y_train)
                    test_accuracy=regressor.score(x_test,y_test)

                    pred = regressor.predict([result])
                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=250)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    return train_accuracy,test_accuracy,pred

                elif(combo.get()=="Random Forest Regression"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    regressor = RandomForestRegressor(n_estimators=8,random_state=0 )
                    regressor.fit(x_train,y_train)
                    y_pred=regressor.predict(x_test)

                    train_accuracy=regressor.score(x_train,y_train)
                    test_accuracy=regressor.score(x_test,y_test)

                    pred = regressor.predict([result])
                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=250)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=250)

                    return train_accuracy,test_accuracy,pred

                else:#Decision tree regression and random forest regression
                    # regressor = LinearRegression()
                    # regressor.fit(x_train, y_train)
                    # pred = regressor.predict([result])
                    print('Sorry Something Error Check Your input')

####classification
            elif(algos=="Classification"):
                if (combo2.get() == "Logistic Regression"):

                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    global classifier
                    classifier = LogisticRegression(random_state=0)
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred

                elif(combo2.get()=="Naive Bayes Classification"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    # global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    #global classifier
                    classifier = GaussianNB()
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    #global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred

                elif(combo2.get()=="K-Nearest Neighbour Classification"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    #global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    #global classifier
                    classifier = KNeighborsClassifier(n_neighbors=10,metric='minkowski',p=2)
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    #global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred

                elif(combo2.get()=="SVM Classification"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    #global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    #global classifier
                    classifier = SVC(C=100,kernel='rbf',random_state=0,gamma=0.5)
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    #global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred
                elif(combo2.get()=="Decision Tree Classification"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    #global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    #global classifier
                    classifier = DecisionTreeClassifier(criterion='entropy',random_state=0)
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    #global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred
                elif(combo2.get()=="Random Forest Classification"):
                    x = data.iloc[:, indep].values
                    y = data.iloc[:, dep].values

                    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

                    #global sc
                    sc = StandardScaler()
                    x_train = sc.fit_transform(x_train)
                    x_test = sc.transform(x_test)

                    #global classifier
                    classifier = RandomForestClassifier(n_estimators=100,criterion='entropy',random_state=0)
                    classifier.fit(x_train,y_train)

                    y_pred = classifier.predict(x_test)

                    cm = confusion_matrix(y_test,y_pred)
                    #global acc
                    acc = (sum(np.diag(cm))/len(y_test))
                    pred = classifier.predict(sc.transform([result]))

                    train_accuracy=accuracy_score(np.ravel(y_train),classifier.predict(x_train))
                    test_accuracy=accuracy_score(np.ravel(y_test),classifier.predict(x_test))

                    Label(window6,text=str(pred),font=('Helvetica',10,'bold'),bg='light blue', relief='solid').place(x=880,y=580)
                    Label(window6,text=f'Train accuracy : {train_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=150)
                    Label(window6,text=f'Test accuracy : {test_accuracy}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=200)
                    Label(window6,text=f'Confusion Matrix\nAcc : {acc}',font=('Helvetica',10,'bold'),bg='light blue',relief='solid').place(x=880,y=250)

                    if abs(train_accuracy - test_accuracy)>0.1 or (train_accuracy<0.6 and test_accuracy<0.6):
                        Label(window6,text='BAD MODEL',font=('Helvetica',10,'bold'),bg='red',relief='solid').place(x=880,y=310)

                    elif (train_accuracy<test_accuracy) and abs(train_accuracy-test_accuracy)>0.1:
                        Label(window6,text='GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    elif train_accuracy>0.85 and test_accuracy >0.85:
                        Label(window6,text='VERY GOOD MODEL',font=('Helvetica',10,'bold'),bg='green',relief='solid').place(x=880,y=310)

                    return train_accuracy,test_accuracy,pred

            else:
                    print('Sorry something wrong input check it')

        button61 = Button(window6, text="Predict", command=predict)
        button61.place(x=300,y=z+50,height=40,width=100)

        label8=tkinter.Label(window6,relief='solid')
        label8.pack()
        label8.place(x=860,y=50,height=100,width=400)
        label8.configure(text='------Summary-----',font=('helvetica',20,'bold'))

        label10=tkinter.Label(window6,relief='solid')
        label10.pack()
        label10.place(x=860,y=130,height=500,width=400)

        label11=tkinter.Label(window6,relief='solid')
        label11.pack()
        label11.place(x=860,y=500,width=400,height=50)
        label11.configure(text='New observation result',font=('helvetica',12,'bold'))

        window6.mainloop()

    def window7(self):
        window7 = Tk()
        window7.configure(bg="lightblue")
        window7.state('zoomed')
        window7.title("ML-Algorithms")

        label = Label(window7,text='Clustering Algorithms',bg='lightblue',font=('helvetica',16,'bold'))
        label.place(x=670,y=330)

        label2 = Label(window7,relief='solid').place(x=500,y=300,height=200,width=400)

        label3 = Label(window7,text='Click here to get Cluster plot and result',font=('helvetica',14,'bold'))
        label3.place(x=520,y=350,height=40)

        label1 = Label(window7, text="Index Name",  bg="lightblue", font=("helvetica", 20, 'bold'))
        label1.place(x=70, y=100, height=30)

        z=140
        global entry1, entries1
        entries1 = []
        for i in indep1:
            label2 = Label(window7, text=i,bg="lightblue", font=("helvetica", 15, 'bold'))
            label2.place(x=75, y=z)

            entry1 = Entry(window7)
            entry1.place(x=290, y=z, height=30, width=150)
            entries1.append(entry1)
            z = z + 25

        label3 = Label(window7, text="Index Values", bg="lightblue", font=("helvetica", 20, 'bold'))
        label3.place(x=295, y=90)

        def cluster():
            if(algos=="clustering"):
                global resultc
                resultc=[]
                for entry in entries1:
                    resultc.append(entry.get())

                if(combo3.get()=="KMeans Clustering"):

                    x_normal = data.iloc[:, indep].values

                    from sklearn.decomposition import KernelPCA
                    kpca=KernelPCA(n_components=2)

                    x=kpca.fit_transform(x_normal)

                    kmeans=KMeans(n_clusters=int(clusterkmeans.get()) ,random_state=0)
                    y_kmeans=kmeans.fit_predict(x)
                    y_predict=kmeans.predict(kpca.transform([resultc]))

                    label4 = Label(window7,text=f'New obs result:{y_predict}',bg='lightgreen',relief='solid')
                    label4.place(x=245,y=z+35,height=30)

                    def plotkmeans():

                        for i in range(0,int(clusterkmeans.get())):
                            if i<=0:

                                plt.figure(dpi=300)
                                plt.scatter(x[:,0],x[:,1],c=kmeans.labels_, cmap='rainbow')
                                plt.scatter(x[y_kmeans==i,0],x[y_kmeans==i,1],s=10)

                                plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],s=200,
                                            c='yellow')
                                plt.title('clusters')
                                plt.xlabel('x')
                                plt.ylabel('y')
                                plt.legend()
                                plt.show()
                    button=Button(window7,text='Cluster',command=plotkmeans())

                elif(combo3.get()=="Hierarchical Clustering"):

                    x_normal = data.iloc[:, indep].values
                    from sklearn.decomposition import KernelPCA
                    kpca=KernelPCA(n_components=2)

                    x=kpca.fit_transform(x_normal)

                    import scipy.cluster.hierarchy as sch
                    dentogram=sch.dendrogram(sch.linkage(x,method='centroid'))

                    from sklearn.cluster import AgglomerativeClustering
                    hc=AgglomerativeClustering(n_clusters=int(clustersch.get()))
                    y=hc.fit_predict(x)

                    np.unique(y)
                    def plotsch():
                        import random

                        for i in range(0,int(clustersch.get())):
                            if i<=0:

                                plt.figure(dpi=300)
                                plt.scatter(x[:,0],x[:,1],s=15,c=hc.labels_, cmap='rainbow')
                                plt.scatter(x[y==i,0],x[y==i,1],s=10)

                                plt.xlabel('x')
                                plt.ylabel('y')
                                plt.title('clusters')
                                plt.legend()
                                plt.show()

                    button=Button(window7,text='Cluster',command=plotsch())

            else:
                print('something wrong input check it')

        button=Button(window7,text='Predict',bg='lightblue',command=cluster)
        button.place(x=620,y=400,width=150,height=60)

        window7.mainloop()

w=window()
w.window1()

{'KernelPCA': 'sklearn.decomposition.KernelPCA', 'sch': 'scipy.cluster.hierarchy', 'AgglomerativeClustering': 'sklearn.cluster.AgglomerativeClustering', 'random': 'random'}


# -*- coding: utf-8 -*-
"""
http://blog.csdn.net/chenghit
"""
import wx
import os
# class MainWindow(wx.Frame):
#     """We simply derive a new class of Frame."""
#     def __init__(self, parent, title):
#         wx.Frame.__init__(self, parent, title = title, size = (200, 100))
#         self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
#         self.CreateStatusBar()    #创建位于窗口的底部的状态栏
#
#         #设置菜单
#         filemenu = wx.Menu()
#         filemenu1 = wx.Menu()
#
#         #wx.ID_ABOUT和wx.ID_EXIT是wxWidgets提供的标准ID
#         filemenu.Append(wx.ID_ABOUT, u"关于", u"关于程序的信息")
#         filemenu.AppendSeparator()
#         filemenu.Append(wx.ID_EXIT, u"退出", u"终止应用程序")
#         menuItem = filemenu1.Append(wx.ID_SAVE, u"保存", u"保存文本")
#
#         #创建菜单栏
#         menuBar = wx.MenuBar()
#         menuBar.Append(filemenu, u"文件")
#         menuBar.Append(filemenu1, u"选项")
#
#         self.SetMenuBar(menuBar)
#         self.Show(True)
#         self.Bind(wx.EVT_MENU, self.OnAbout, menuItem)
#
#
# app = wx.App(False)
# frame = MainWindow(None, title = u"记事本")
# app.MainLoop()

# -*- coding: utf-8 -*-
"""
http://blog.csdn.net/chenghit
"""

class MainWindow(wx.Frame):
    """We simply derive a new class of Frame."""
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size = (600, 400))
        self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
        self.CreateStatusBar()    # 创建位于窗口的底部的状态栏

        # 设置菜单
        filemenu = wx.Menu()
        filemenu1 = wx.Menu()

        # wx.ID_ABOUT和wx.ID_EXIT是wxWidgets提供的标准ID
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About", \
            " Information about this program")    # (ID, 项目名称, 状态栏信息)
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", \
            " Terminate the program")    # (ID, 项目名称, 状态栏信息)
        menuOpen = filemenu1.Append(wx.ID_OPEN, "&Open", \
                                   " Open a file")  # (ID, 项目名称, 状态栏信息)
        # 创建菜单栏
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&File")    # 在菜单栏中添加filemenu菜单

        menuBar.Append(filemenu1, "&Choose")
        self.SetMenuBar(menuBar)    # 在frame中添加菜单栏

        panel = wx.Panel(self)
        self.quote = wx.StaticText(panel, label="Hello!",pos=(10,10) ,size=(40,40))
        self.quote1 = wx.StaticText(panel, label="World!", pos=(10,50) ,size=(40, 40))
        # 设置events
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)

        # 设置sizers
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)  #水平方向布置boxsizer
        self.buttons = []
        for i in range(0, 6):
            self.buttons.append(wx.Button(self,-1, "Button &" + str(i)))
            self.sizer2.Add(self.buttons[i], 1, wx.GROW)

        self.sizer = wx.BoxSizer(wx.VERTICAL)    #垂直方向布置boxsizer
        self.sizer.Add(panel,0, wx.GROW)
        self.sizer.Add(self.control, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.GROW)
        self.Bind(wx.EVT_BUTTON, self.OnOpen, self.buttons[1])

        # 激活sizer
        self.SetSizer(self.sizer)
        self.SetAutoLayout(True)
        self.sizer.Fit(self)
        self.Show(True)

    def OnAbout(self, e):
        # 创建一个带"OK"按钮的对话框。wx.OK是wxWidgets提供的标准ID
        dlg = wx.MessageDialog(self, "A small text editor.", \
            "About Sample Editor", wx.OK)    # 语法是(self, 内容, 标题, ID)
        dlg.ShowModal()    # 显示对话框
        dlg.Destroy()    # 当结束之后关闭对话框

    def OnExit(self, e):
        self.Close(True)    # 关闭整个frame

    def OnOpen(self,e):
        """ Open a file"""
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*",wx.ID_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()

app = wx.App(False)
frame = MainWindow(None, title = "Small editor")
app.MainLoop()


import volmdlr as vm
import volmdlr.primitives2d as p2d
import volmdlr.primitives3d as p3d
import plot_data.core as plot_data
import math
from itertools import product
from random import random
import cma

from dessia_common.core import DessiaObject
from typing import List

class Component(DessiaObject):
    _standalone_in_db = True

    def __init__(self, p0: vm.Point2D, p1: vm.Point2D, p2: vm.Point2D, p3: vm.Point2D, name: str = ''):
        DessiaObject.__init__(self, name=name)
        self.p3 = p3
        self.p2 = p2
        self.p1 = p1
        self.p0 = p0
        self.primitive = self._primitive()

    def _primitive(self):
        points = [self.p0, self.p1, self.p2, self.p3]
        l1 = vm.primitives2d.ClosedRoundedLineSegments2D(points, {})
        return l1

    def update(self, p0: vm.Point2D, p1: vm.Point2D, p2: vm.Point2D, p3: vm.Point2D):
        self.p3 = p3
        self.p2 = p2
        self.p1 = p1
        self.p0 = p0

    def length(self):
        points = [self.p0, self.p1, self.p2, self.p3]
        length = 0
        for p1, p2 in zip(points[0:-1], points[1:]):
            length += p1.point_distance(p2)
        length += points[0].point_distance(points[-1])
        return length

class Optimizer(DessiaObject):
    _standalone_in_db = True
    _dessia_methods = ['optimize']

    def __init__(self, component: Component, name: str = ''):
        DessiaObject.__init__(self, name=name)
        self.component = component

    def optimize(self) -> List[Component]:
        solutions = []
        for i in range(10):
            x0a = [random() for i in range(8)]

            xra, fx = cma.fmin(self.objective, x0a, 0.1,
                               options={'bounds': [0, 1],
                                        'tolfun': 1e-8,
                                        'verbose': 10,
                                        'ftarget': 1e-8,
                                        'maxiter': 100})[0:2]

            x_sol = xra
            points = [vm.Point2D(x_sol[2 * i], x_sol[2 * i + 1]) for i in range(int(len(x_sol) / 2.))]
            self.component.update(*points)
            length = self.component.length()
            if abs(length - 0.2) < 1e-3:
                solutions.append(self.component.copy())
        return solutions

    def objective(self, x):
        objective = 0
        points = [vm.Point2D(x[2 * i], x[2 * i + 1]) for i in range(int(len(x) / 2.))]
        self.component.update(*points)
        length = self.component.length()
        length_obj = 0.2
        return (length - length_obj)**2


# -*- coding: utf-8 -*-
# @Time    : 2022/8/20 21:52
# @Author  : lzc
# @Email   : hybpjx@163.com
# @File    : 23线性回归 数据分析.py
# @Software: PyCharm
import matplotlib.pyplot as plt
import pandas as pd
# 科学计算范畴的库
from scipy.stats import linregress

sales = pd.read_excel("source/23Sales.xlsx", dtype={"Month": str})

slope, intercept, r, p, std_err = linregress(sales.index, sales.Revenue)

exp = sales.index * slope + intercept

# x轴 是sales.index y轴是sales.Revenue
# plt.bar(sales.index, sales.Revenue)
print(slope * 35 + intercept)

plt.scatter(sales.index, sales.Revenue)
plt.plot(sales.index, exp, color="orange")

plt.title(f"y={slope}*x+{intercept}")
# 重新铺了一遍x轴
plt.xticks(sales.index, sales.Month, rotation=90)
# 紧凑布局
plt.tight_layout()
plt.show()

23线性回归 数据分析.py


     #################################
     ### DATA COLLECTION FUNCTIONS ###
     #################################

# List of Data Collection functions:
    # get_move_list
    # white_results
    # clean_fen
    # get_bitwise
    # get_white_win_bs
    # get_white_lose_bs
    # get_board_state_string
    # get_board_state_array
    # get_draw_bs
    # get_fen_from_bitwise

#####################################################################################

import numpy as np
import pandas as pd
import itertools
import chess

def get_move_list(move_string): #### input is a string
    '''
    Takes a move string that was striped from a PGN format, and removes unwanted characters and conserves SAN format.

    Input: String of plain text moves in string format
    Output: List of SAN moves

    '''
    testing = move_string
    testing = testing[0:-2]  ### remove last 2 characters == '\n'
    testing = testing.split('. ')

    for i in range(len(testing)):
        testing[i] = testing[i].split(' ')
    for i in testing:
        try:
            del(i[2])
        except:
            continue
    del(testing[0])

    simplelist = list(itertools.chain.from_iterable(testing))
    return(simplelist)

#####################################################################################

def white_results(result_string_list):
    '''
    Reformats and standardizes stripped result info from chess game in PGN/TXT format

    Input: list of strings - (results text with line breaks included '\n'
    Output: win, lose, draw in list format

    '''
    white_result_list = []
    for i in range(len(result_string_list)):
            result = result_string_list[i][:-1]
            result = result.replace('1-0', 'win')
            result = result.replace('0-1', 'lose')
            result = result.replace('1/2-1/2', 'draw')
            white_result_list.append(result)
    return(white_result_list)

#####################################################################################

def clean_fen(string):
    '''
    Takes a Forsyth-Edwards Notation, with extraneous features, and simplifies it to a list of piece and position on the board [r,n,b,q,k,b,n,r,p,p,p,p,p,p,p,p,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,P,P,P,P,P,P,P,P,R,N,B,Q,K,B,N,R]

    Input: FEN string
    Output: 64 length list of strings, with piece position encoded
    '''
    string = string.replace('8','1'*8)
    string = string.replace('7','1'*7)
    string = string.replace('6','1'*6)
    string = string.replace('5','1'*5)
    string = string.replace('4','1'*4)
    string = string.replace('3','1'*3)
    string = string.replace('2','1'*2)
    string = string.replace('1','1'*1)
    string = string.replace('/','')
    string_list = [i for i in string]
    return(string_list)

#####################################################################################

def get_bitwise(board_state):
    '''
    Takes a 64 length list and decomposes it into 6 - 64 length arrays with bitwise encoding, -1,0,1

    Input: 64 length list of board states with piece encoding (r,n,b,q,k,p,R,N,B,Q,K,P,1)
    Output: 6 - 64 length arrays depicting board states with values -1,0,1
    '''

    bs = board_state
    r_ray = np.zeros(64)
    n_ray = np.zeros(64)
    b_ray = np.zeros(64)
    q_ray = np.zeros(64)
    k_ray = np.zeros(64)
    p_ray = np.zeros(64)

    for i in range(64):
        if bs[i] == 'r':
            r_ray[i] = -1
        if bs[i] == 'R':
            r_ray[i] = 1

        if bs[i] == 'b':
            b_ray[i] = -1
        if bs[i] == 'B':
            b_ray[i] = 1

        if bs[i] == 'n':
            n_ray[i] = -1
        if bs[i] == 'N':
            n_ray[i] = 1

        if bs[i] == 'q':
            q_ray[i] = -1
        if bs[i] == 'Q':
            q_ray[i] = 1

        if bs[i] == 'k':
            k_ray[i] = -1
        if bs[i] == 'K':
            k_ray[i] = 1

        if bs[i] == 'p':
            p_ray[i] = -1
        if bs[i] == 'P':
            p_ray[i] = 1
    master_ray = np.array((r_ray, n_ray, b_ray, q_ray, k_ray, p_ray))
    return(master_ray)

#####################################################################################

def get_white_win_bs(game): ### i from white_win
    '''
    Gets pairs of 'before-and-after' boardstates where white is making the move

    Input: List of game moves
    Output: List of before and after boardstates for white, where white wins
    '''

    board = chess.Board()

    board_state = None
    game_board_states = []

    for i in range(len(game)):
        bs = board.board_fen() ### creates current board state, in FEN "rnbqkbnr/pppppppp....
        board_state = np.array(clean_fen(bs)) ### return
        game_board_states.append(board_state)
        board.push_san(game[i])

    white_wins = []
    i = 0
    while i < len(game_board_states):
        try:
            white_wins.append((get_bitwise(game_board_states[i]).astype(int), get_bitwise(game_board_states[i + 1]).astype(int)))
            i += 2
        except:
            break
    return(white_wins)

#####################################################################################

def get_board_state_string(move_list):
    game = move_list
    board = chess.Board()

    board_states = []
    for move in game:
        try:
            board_states.append(board.board_fen())
            board.push_san(move)
        except:
            continue
    return(board_states)

#####################################################################################

def get_white_lose_bs(game):
    '''
    Gets pairs of 'before-and-after' boardstates where black is making the move

    Input: List of game moves
    Output: List of before and after boardstates for black, where black wins
    '''

    board = chess.Board()

    board_state = None
    game_board_states = []

    for i in range(len(game)):
        bs = board.board_fen() ### creates current board state, in FEN "rnbqkbnr/pppppppp....
        board_state = np.array(clean_fen(bs)) ### return
        game_board_states.append(board_state)
        board.push_san(game[i])

    white_losses = []
    i = 1
    while i < len(game_board_states):
        try:
            white_losses.append((get_bitwise(game_board_states[i]).astype(int), get_bitwise(game_board_states[i + 1]).astype(int)))
            i += 2
        except:
            break
    return(white_losses)

#####################################################################################

def get_draw_bs(game):  ### probably wont use it - dont want neutral positions
    '''
    Gets pairs of 'before-and-after' boardstates for both black and white

    Input: List of game moves
    Output: List of before and after boardstates for both black and white
    '''
    board = chess.Board()

    board_state = None
    game_board_states = []

    for i in range(len(game)):
        bs = board.board_fen() ### creates current board state, in FEN "rnbqkbnr/pppppppp....
        arr = np.array(clean_fen(bs))
        game_board_states.append(arr)
        board.push_san(game[i])

    draws = []
    for i in range(len(game_board_states)):
        try:
            draws.append((game_board_states[i], game_board_states[i+1]))
        except:
            break
    return(draws)

#####################################################################################

def get_board_state_array(move_list): ### i from all move_list
    '''
    Gets board states in arrray format

    input: list of moves
    output: board states in array format
    '''

    game = move_list
    board = chess.Board()
    #game = all_san_list[0]

    board_state_list = []

    for move in game:
        try:
            bs = board.board_fen() ### creates current board state, in FEN "rnbqkbnr/pppppppp....
            board_array = np.array(clean_fen(bs)) ### return
            board_state_list.append(board_array)
            board.push_san(move)
        except:
            board_state_list.append(0)
            break
    return(board_state_list)

#####################################################################################

def get_fen_from_bitwise(game):
    """
    Takes an individual board state in the bitwise format (6 arrays with 64 positions), and returns the game in FEN, a notation that python-chess recognizes

    input: 1 board state in bitwise format: a grouping of 6 arrays with 64 positions
    output: 1 FEN string
    """

    game # range(j, 6)
    fena = np.zeros(64, dtype=int) #((r_ray, n_ray, b_ray, q_ray, k_ray, p_ray))
    for j in range(6):             # Iterate through each piece's board state
        for i in range(64):        # Iterate through each position in each board state
            if j == 0:
                fena[i] = game[j][i]
            if j == 1:
                fena[i] += game[j][i] * (j+1)
            if j == 2:
                fena[i] += game[j][i] * (j+1)
            if j == 3:
                fena[i] += game[j][i] * (j+1)
            if j == 4:
                fena[i] += game[j][i] * (j+1)
            if j == 5:
                fena[i] += game[j][i] * (j+1)

    fena = fena.astype(str)

    fena = np.char.replace(fena, '1', 'R')
    fena = np.char.replace(fena, '-R', 'r')

    fena = np.char.replace(fena, '2', 'N')
    fena = np.char.replace(fena, '-N', 'n')

    fena = np.char.replace(fena, '3', 'B')
    fena = np.char.replace(fena, '-B', 'b')

    fena = np.char.replace(fena, '4', 'Q')
    fena = np.char.replace(fena, '-Q', 'q')

    fena = np.char.replace(fena, '5', 'K')
    fena = np.char.replace(fena, '-K', 'k')

    fena = np.char.replace(fena, '6', 'P')
    fena = np.char.replace(fena, '-P', 'p')

    fena = np.char.replace(fena, '0', '1') # Seems wierd, but I need to add up the open spaces somehow....

    fene = list(itertools.chain.from_iterable(fena))

    # Need to break chain at 8 characters
    fene.insert(8 , '/')
    fene.insert(17, '/')
    fene.insert(26, '/')
    fene.insert(35, '/')
    fene.insert(44, '/')
    fene.insert(53, '/')
    fene.insert(62, '/')

    # Replacing consecutive empty spaces with the product
    fene = ''.join(fene)
    fene = fene.replace('1' * 8, '8')
    fene = fene.replace('1' * 7, '7')
    fene = fene.replace('1' * 6, '6')
    fene = fene.replace('1' * 5, '5')
    fene = fene.replace('1' * 4, '4')
    fene = fene.replace('1' * 3, '3')
    fene = fene.replace('1' * 2, '2')
    fene = fene.replace('1' * 1, '1')
    return(fene)

#####################################################################################
#####################################################################################

     ######################################
     ### RUN AI AND PLAY GAME FUNCTIONS ###
     ######################################

# List OF RUN AI AND PLAY GAME FUNCTIONS:
    # run_AI

#####################################################################################

def run_AI(board, turn):
    """
    Plays the game as the AI, given the turn and current board

    input: board (in python-chess format)
    output: the best move in uci format

    """

    legal_boards = []
    l = []

    boardfen = board.fen()
    for move in [str(i) for i in list(board.legal_moves)]:
        current_board = chess.Board(boardfen)

        future_move = chess.Move.from_uci(str(move))
        current_board.push(future_move)
        current_board = str(current_board)
        current_board = current_board.replace('\n', ' ')
        current_board = current_board.replace('.', '1')
        current_board = current_board.split(' ')
        legal_boards.append((future_move, get_bitwise(current_board)))

    pred_df = pd.DataFrame(legal_boards, columns = ['move', 'bitwise'])
    pred_df['rook'] = [ pred_df['bitwise'][i][0] for i in range(len(pred_df)) ]
    pred_df['night'] = [ pred_df['bitwise'][i][1] for i in range(len(pred_df)) ]
    pred_df['bishop'] = [ pred_df['bitwise'][i][2] for i in range(len(pred_df)) ]
    pred_df['queen'] = [ pred_df['bitwise'][i][3] for i in range(len(pred_df)) ]
    pred_df['king'] = [ pred_df['bitwise'][i][4] for i in range(len(pred_df)) ]
    pred_df['pawn'] = [ pred_df['bitwise'][i][5] for i in range(len(pred_df)) ]
    pred_df = pred_df.drop('bitwise', axis=1)

    ### Need column names for expanded df
    rook_cols = []
    for i in range(64):
        rook_cols.append('rook_'+ str(i))
    night_cols = []
    for i in range(64):
        night_cols.append('night_'+ str(i))
    bishop_cols = []
    for i in range(64):
        bishop_cols.append('bishop_'+ str(i))
    queen_cols = []
    for i in range(64):
        queen_cols.append('queen_'+ str(i))
    king_cols = []
    for i in range(64):
        king_cols.append('king_'+ str(i))
    pawn_cols = []
    for i in range(64):
        pawn_cols.append('pawn_'+ str(i))

    ### Expanding individual board state dfs

    dfr = pd.DataFrame(pred_df['rook'].values.tolist(), columns = rook_cols)
    dfn = pd.DataFrame(pred_df['night'].values.tolist(), columns = night_cols)
    dfb = pd.DataFrame(pred_df['bishop'].values.tolist(), columns = bishop_cols)
    dfq = pd.DataFrame(pred_df['queen'].values.tolist(), columns = queen_cols)
    dfk = pd.DataFrame(pred_df['king'].values.tolist(), columns = king_cols)
    dfp = pd.DataFrame(pred_df['pawn'].values.tolist(), columns = pawn_cols)

    ### Encoding current turn into a seperate turn_df

    placeholder = pd.Series([i for i in range(len(pred_df))])
    turn_df= pd.DataFrame(placeholder, index= range(len(pred_df)), columns=['white_turn'])
    if turn % 2 == False:
        ones = [1 for i in range(len(pred_df))]
        turn_df['white_turn'] = ones
    else:
        zeros = [0 for i in range(len(pred_df))]
        turn_df['white_turn'] = zeros

    ### Merging dfs

    df_merged = turn_df
    df_merged = pd.merge(df_merged, dfr, how='outer', right_index=True, left_index=True)
    df_merged = pd.merge(df_merged, dfn, how='outer', right_index=True, left_index=True)
    df_merged = pd.merge(df_merged, dfb, how='outer', right_index=True, left_index=True)
    df_merged = pd.merge(df_merged, dfq, how='outer', right_index=True, left_index=True)
    df_merged = pd.merge(df_merged, dfk, how='outer', right_index=True, left_index=True)
    df_merged = pd.merge(df_merged, dfp, how='outer', right_index=True, left_index=True)

    ### Running the model over the possible moves and selecting the best move availible

    move_and_score = []
    best_move = ('NaN', -100)

    for i in range(len(legal_boards)):
        m_and_s = (str(legal_boards[i][0]), np.asscalar(model.predict(df_merged)[i][0]))
        move_and_score.append(m_and_s)
        if m_and_s[1] > best_move[1]:
            best_move = m_and_s

    current_board = board
    return(best_move)


import logging
import dendropy as dpy
from pyparsing import Suppress, SkipTo, Word, Regex, Literal, OneOrMore, \
    Group, LineEnd, CharsNotIn, nums, alphanums, ParseException

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())

FLOAT = Word(nums + '.-').setParseAction(lambda x: float(x[0]))
INT = Word(nums).setParseAction(lambda x: int(x[0]))
WORD = Word(alphanums + '_')
SPACEDWORD = Word(alphanums + ' _')

class PhymlParser(object):
    """
    Simple phyml result parser. Assumes one of the standard models
    for nucleotide analyses.
    """

    def __init__(self):
        self.MODEL_LABEL = Regex(r'Model of.*substitution:\s+')
        self.ALPHA_LABEL = Regex(r'Gamma shape parameter:\s+')
        self.LNL_LABEL = Regex(r'Log-likelihood:\s+')
        self.F_LABEL = Regex(r'f\(([ACGT])\)=\s+')
        self.R_LABEL = Regex(r'[ACGT]\s+<->\s+[ACGT]\s+')
        self.TSTV_LABEL = Regex(r'Transition/transversion ratio.*:\s+')
        self.model = Suppress(SkipTo(self.MODEL_LABEL)) + \
                     Suppress(self.MODEL_LABEL) + WORD
        self.lnl = Suppress(SkipTo(self.LNL_LABEL)) + \
                   Suppress(self.LNL_LABEL) + FLOAT
        self.alpha = Suppress(SkipTo(self.ALPHA_LABEL)) + \
                     Suppress(self.ALPHA_LABEL) + FLOAT
        self.common = self.model + self.lnl + self.alpha
        self.tstv = OneOrMore(Suppress(SkipTo(self.TSTV_LABEL)) +
                              Suppress(self.TSTV_LABEL) + FLOAT)
        self.freq = OneOrMore(
            Suppress(SkipTo(self.F_LABEL)) + Suppress(self.F_LABEL) + FLOAT)
        self.rates = OneOrMore(
            Suppress(SkipTo(self.R_LABEL)) + Suppress(self.R_LABEL) + FLOAT)
        self.gtr_specific = Group(self.freq) + Group(self.rates)
        self.hky_specific = Group(self.tstv) + Group(self.freq)

    def parse(self, filename):
        model = None
        alpha = None
        lnl = None
        freq = None
        rates = None

        with open(filename) as fl:
            s = fl.read()

        try:
            model, lnl, alpha = self.common.parseString(s).asList()

        except ParseException as err:
            logger.error(err)

        if model == 'JC69':
            freq = [0.25, 0.25, 0.25, 0.25]
            rates = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

        elif model == 'K80':
            freq = [0.25, 0.25, 0.25, 0.25]
            try:
                tstv = self.tstv.parseString(s).asList()
            except ParseException as err:
                logger.error(err)

            rates = [1.0, tstv[0], 1.0, 1.0, tstv[0], 1.0]

        elif model == 'F81':
            try:
                freq = self.freq.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
            rates = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

        elif model == 'F84' or model == 'HKY85' or model == 'TN93':
            parser = Group(self.tstv) + Group(self.freq)
            try:
                tstv, freq = parser.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
            if model == 'TN93':
                rates = [1.0, tstv[0], 1.0, 1.0, tstv[1], 1.0]
            else:
                rates = [1.0, tstv[0], 1.0, 1.0, tstv[0], 1.0]

        elif model == 'GTR':
            parser = Group(self.freq) + Group(self.rates)
            try:
                freq, rates = parser.parseString(s).asList()
            except ParseException as err:
                logger.error(err)

        return model, alpha, lnl, freq, rates

    def to_dict(self, stats_filename, tree_filename):
        model, alpha, lnl, freq, rates = self.parse(stats_filename)
        try:
            with open(tree_filename) as treefl:
                nwk_tree = treefl.read().rstrip()
                tree = dpy.Tree.get(data=nwk_tree, schema='newick')
        except IOError as err:
            logger.error(err)
            return

        result = {'likelihood': lnl,
                  'alpha': alpha,
                  'frequencies': freq,
                  'rates': rates,
                  'model': model,
                  'tree': tree}
        return result

class RaxmlParser(object):
    def __init__(self):
        self.ALPHA_LABEL = Regex(r'alpha\[\d+\]:')
        self.LNL_LABEL = Literal('Final GAMMA-based Score of best tree')
        self.FRQ_LABEL = Regex(r'Base frequencies: (?=\d+)') ^ \
                         Regex(r'ML estimate base freqs\[\d+\]:')
        self.NAMES_LABEL = Regex(r'Partition: \d+ with name:\s+')
        self.RATES_LABEL = Regex(r'rates\[\d+\].+?:')
        self.MODEL_LABEL = Literal('Substitution Matrix:')
        self.alpha = OneOrMore(Suppress(SkipTo(self.ALPHA_LABEL)) +
                               Suppress(self.ALPHA_LABEL) + FLOAT)
        self.lnl = Suppress(SkipTo(self.LNL_LABEL)) + \
                   Suppress(self.LNL_LABEL) + FLOAT
        self.frq = OneOrMore(Group(Suppress(SkipTo(self.FRQ_LABEL)) +
                                   Suppress(self.FRQ_LABEL) + OneOrMore(FLOAT)))
        self.names = OneOrMore(
            Suppress(SkipTo(self.NAMES_LABEL)) + Suppress(self.NAMES_LABEL) +
            CharsNotIn('\n') + Suppress(LineEnd()))
        self.rates = OneOrMore(
            Group(Suppress(SkipTo(self.RATES_LABEL)) +
                  Suppress(self.RATES_LABEL) + OneOrMore(FLOAT)))
        self.model = Suppress(SkipTo(self.MODEL_LABEL)) + \
                     Suppress(self.MODEL_LABEL) + WORD

        MODEL_LABEL = Literal('Substitution Matrix:')
        SCORE_LABEL = Literal('Final GAMMA  likelihood:')
        BOOT_SCORE_LABEL = Literal('Final ML Optimization Likelihood:')
        DESC_LABEL = Literal('Model Parameters of Partition')
        NAME_LEADIN = Literal(', Name:')
        DATATYPE_LEADIN = Literal(', Type of Data:')
        ALPHA_LEADIN = Literal('alpha:')
        TREELENGTH_LEADIN = Literal('Tree-Length:')
        RATES_LABEL = Regex(r'rate \w <-> \w:')
        FREQS_LABEL = Regex(r'freq pi\(\w\):')

        likelihood = Suppress(SkipTo(SCORE_LABEL)) + Suppress(
            SCORE_LABEL) + FLOAT
        boot_likelihood = Suppress(SkipTo(BOOT_SCORE_LABEL)) + Suppress(
            BOOT_SCORE_LABEL) + FLOAT
        description = Suppress(SkipTo(DESC_LABEL)) + Suppress(
            DESC_LABEL) + INT + Suppress(
            NAME_LEADIN) + SPACEDWORD + Suppress(DATATYPE_LEADIN) + WORD
        treelen = Suppress(SkipTo(TREELENGTH_LEADIN)) + Suppress(
            TREELENGTH_LEADIN) + FLOAT

        alpha = Suppress(SkipTo(ALPHA_LEADIN)) + Suppress(ALPHA_LEADIN) + FLOAT

        rates = OneOrMore(
            Group(Suppress(SkipTo(RATES_LABEL)) + Suppress(
                RATES_LABEL) + OneOrMore(FLOAT)))
        freqs = OneOrMore(
            Group(Suppress(SkipTo(FREQS_LABEL)) + Suppress(
                FREQS_LABEL) + OneOrMore(FLOAT)))

        # output of running different set of raxml analysis
        self.TC_STOCHBI_LABEL = Literal(
            'Tree certainty under stochastic bipartition '
            'adjustment for this tree:')
        self.RTC_STOCHBI_LABEL = Literal(
            'Relative tree certainty under stochastic bipartition adjustment for this tree:')
        self.TCA_STOCHBI_LABEL = Literal(
            'Tree certainty including all conflicting bipartitions (TCA) under '
            'stochastic bipartition adjustment for this tree:')
        self.RTCA_STOCHBI_LABEL = Literal(
            'Relative tree certainty including all conflicting bipartitions (TCA) '
            'under stochastic bipartition adjustment for this tree:')
        self.TC_UNIBI_LABEL = Literal(
            'Tree certainty under uniform bipartition '
            'adjustment for this tree:')
        self.RTC_UNIBI_LABEL = Literal('Relative tree certainty under uniform '
                                       'bipartition adjustment for this tree:')
        self.TCA_UNIBI_LABEL = Literal(
            'Tree certainty including all conflicting bipartitions (TCA) under '
            'uniform bipartition adjustment for this tree:')
        self.RTCA_UNIBI_LABEL = Literal(
            'Relative tree certainty including all conflicting bipartitions (TCA) '
            'under uniform bipartition adjustment for this tree:')

        self.tc_stochbi = Suppress(SkipTo(self.TC_STOCHBI_LABEL)) + Suppress(
            self.TC_STOCHBI_LABEL) + FLOAT
        self.rtc_stochbi = Suppress(SkipTo(self.RTC_STOCHBI_LABEL)) + Suppress(
            self.RTC_STOCHBI_LABEL) + FLOAT
        self.tca_stochbi = Suppress(SkipTo(self.TCA_STOCHBI_LABEL)) + Suppress(
            self.TCA_STOCHBI_LABEL) + FLOAT
        self.rtca_stochbi = Suppress(
            SkipTo(self.RTCA_STOCHBI_LABEL)) + Suppress(
            self.RTCA_STOCHBI_LABEL) + FLOAT
        self.tc_unibi = Suppress(SkipTo(self.TC_UNIBI_LABEL)) + Suppress(
            self.TC_UNIBI_LABEL) + FLOAT
        self.rtc_unibi = Suppress(SkipTo(self.RTC_UNIBI_LABEL)) + Suppress(
            self.RTC_UNIBI_LABEL) + FLOAT
        self.tca_unibi = Suppress(SkipTo(self.TCA_UNIBI_LABEL)) + Suppress(
            self.TCA_UNIBI_LABEL) + FLOAT
        self.rtca_unibi = Suppress(SkipTo(self.RTCA_UNIBI_LABEL)) + Suppress(
            self.RTCA_UNIBI_LABEL) + FLOAT

        # Use these for flag 'a' option
        self.boot_likelihood = boot_likelihood
        self.freqs = freqs
        self.rates = rates
        self.alpha = alpha
        self.name = description
        self.treelen = treelen

        self._dash_f_e_parser = (Group(OneOrMore(self.model)) +
                                 likelihood +
                                 Group(OneOrMore(Group(
                                     description + alpha + Suppress(
                                         TREELENGTH_LEADIN) +
                                     Suppress(FLOAT) + Group(
                                         OneOrMore(rates)) + Group(
                                         OneOrMore(freqs))))))

    def parse(self, filename, f_flag=None):
        with open(filename) as fl:
            s = fl.read()
        if f_flag == None:
            try:
                alphas = self.alpha.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                alphas = [None]
            try:
                freqs = self.frq.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                freqs = [None]
            try:
                names = self.names.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                names = [None]
            try:
                rates = self.rates.parseString(s).asList()
            except ParseException:
                rates = None
            try:
                lnl = self.lnl.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                lnl = [0]

            return alphas, freqs, names, rates, lnl
        elif f_flag == 'i':
            try:
                tc_stochbi = self.tc_stochbi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                tc_stochbi = [None]
            try:
                rtc_stochbi = self.rtc_stochbi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                rtc_stochbi = [None]
            try:
                tca_stochbi = self.tca_stochbi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                tca_stochbi = [None]
            try:
                rtca_stochbi = self.rtca_stochbi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                rtca_stochbi = [None]
            try:
                tc_unibi = self.tc_unibi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                tc_unibi = [None]
            try:
                rtc_unibi = self.rtc_unibi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                rtc_unibi = [None]
            try:
                tca_unibi = self.tca_unibi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                tca_unibi = [None]
            try:
                rtca_unibi = self.rtca_unibi.parseString(s).asList()[0]
            except ParseException as err:
                logger.error(err)
                rtca_unibi = [None]

            return tc_unibi, rtc_unibi, tca_unibi, rtca_unibi, tc_stochbi, \
                   rtc_stochbi, tca_stochbi, rtca_stochbi

        elif f_flag == 'a':
            try:
                model = self.model.parseString(s).asList()
            except ParseException:
                model = None
            try:
                alphas = self.alpha.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                alphas = [None]
            try:
                freqs = self.freqs.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                freqs = [None]
            try:
                name = self.name.parseString(s).asList()
            except ParseException as err:
                logger.error(err)
                name = [None]
            try:
                rates = self.rates.parseString(s).asList()
            except ParseException:
                rates = None
            try:
                likelihood = self.boot_likelihood.parseString(s).asList()
            except ParseException:
                likelihood = None
            try:
                treelength = self.treelen.parseString(s).asList()
            except ParseException:
                treelength = None
            return model, treelength, alphas, freqs, name, rates, likelihood

    def _dash_f_e_to_dict(self, info_filename, tree_filename):
        """
        Raxml provides an option to fit model params to a tree,
        selected with -f e.
        The output is different and needs a different parser.
        """
        with open(info_filename) as fl:
            models, likelihood, partition_params = self._dash_f_e_parser.parseFile(
                fl).asList()

        with open(tree_filename) as fl:
            nwk_tree = fl.read().rstrip()
            tree = dpy.Tree.get(data=nwk_tree, schema='newick')

        d = {'likelihood': likelihood, 'tree': tree, 'partitions': {}}

        for model, params in zip(models, partition_params):
            subdict = {}
            index, name, _, alpha, rates, freqs = params
            subdict['alpha'] = alpha
            subdict['name'] = name
            subdict['rates'] = rates
            subdict['frequencies'] = freqs
            subdict['model'] = model
            d['partitions'][index] = subdict

        return d

    def _dash_f_i_to_dict(self, info_filename, tree_filename):
        """
        Raxml provides an option to fit model params to a tree,
        selected with -f i.
        The output is different and needs a different parser.
        """
        tc_unibi, rtc_unibi, tca_unibi, rtca_unibi, tc_stochbi, rtc_stochbi, tca_stochbi, rtca_stochbi = self.parse(
            info_filename, 'i')

        with open(tree_filename) as fl:
            nwk_tree = fl.read().rstrip()
            tree = dpy.Tree.get(data=nwk_tree, schema='newick')

        d = {'tree': tree, 'tc_unibi': tc_unibi, 'rtc_unibi': rtc_unibi,
             'tca_unibi': tca_unibi,
             'rtca_unibi': rtca_unibi, 'tc_stochbi': tc_stochbi,
             'rtc_stochbi': rtc_stochbi, 'tca_stochbi': tca_stochbi,
             'rtca_stochbi': rtca_stochbi}

        return d

    def _dash_f_a_to_dict(self, info_filename, tree_filename):
        """
        Raxml provides an option to fit model params to a tree,
        selected with -f a.
        The output is different and needs a different parser.
        """
        model, treelength, alphas, freqs, name, rates, likelihood = self.parse(
            info_filename, f_flag='a')

        bs_tree_filename = tree_filename.replace('bestTree',
                                                 'bipartitionsBranchLabels')
        try:
            with open(bs_tree_filename) as fl:
                nwk_tree = fl.read().rstrip()
                tree = dpy.Tree.get(data=nwk_tree, schema='newick')
        except IOError as err:
            logger.error('No tree file - raxml analysis failed')
            return

        result = {'likelihood': likelihood,
                  'model': model,
                  'treelength': treelength,
                  'part_name': name[1],
                  'alpha': alphas,
                  'rates': rates,
                  'frequences': freqs,
                  'bs_tree': nwk_tree,
                  'tree': tree}

        return result

    def to_dict(self, info_filename, tree_filename, dash_f=None):
        """
        Parse raxml output and return a dict
        Option dash_f_e=True will parse the output of a raxml -f e run,
        which has different output
        """
        if dash_f == 'e':
            return self._dash_f_e_to_dict(info_filename, tree_filename)
        elif dash_f == 'i':
            return self._dash_f_i_to_dict(info_filename, tree_filename)
        elif dash_f == 'a':
            return self._dash_f_a_to_dict(info_filename, tree_filename)
        else:
            return self._to_dict(info_filename, tree_filename)

    def _to_dict(self, info_filename, tree_filename):
        alpha, freqs, names, rates, lnl = self.parse(info_filename)
        try:
            with open(tree_filename) as fl:
                nwk_tree = fl.read().rstrip()
                tree = dpy.Tree.get(data=nwk_tree, schema='newick')
        except IOError as err:
            logger.error('No tree file - raxml analysis failed')
            return
        n_parts = len(alpha)
        assert len(freqs) == n_parts
        assert len(names) == n_parts
        if rates != None:
            assert len(rates) == n_parts

        result = {'likelihood': lnl[0],
                  'partitions': {},
                  'tree': tree}

        for i in range(n_parts):
            subdict = {'alpha': alpha[i], 'frequencies': freqs[i],
                       'name': names[i]}
            if rates != None:
                subdict['rates'] = rates[i]
            result['partitions'][i] = subdict

        return result

class IqtreeParser(object):
    def __init__(self):
        """
        Put stuff here (regex etc.)
        """

    def parse(self, filename):
        """
        Extract based on strings defined in __init__
        """

    def to_dict(self, tree_filename):

        """
        Add stuff for results dictionary here
        """

        try:
            with open(tree_filename) as fl:
                tree = fl.read()
        except IOError as err:
            logger.error(err)
            return

        tree = tree.rstrip()
        result = {'tree': tree}

        return result

class FasttreeParser(object):
    def __init__(self):
        """
        Put stuff here (regex etc.)
        """
        self.tree = None
        self.likelihood = None

    def parse(self, tree, other):
        """
        Extract based on strings defined in __init__
        """
        self.tree = dpy.Tree.get(data=tree.rstrip(), schema='newick')
        for line in other.split('\n'):
            if 'Optimize all lengths:' in line:
                self.likelihood = float(line.split()[-3])

    def to_dict(self):

        """
        Add stuff for results dictionary here
        """
        result = {'tree': self.tree,
                  'likelihood': self.likelihood}

        return result

class GuenomuParser(object):
    def __init__(self):
        self.best_topology = None
        self.species_trees = None

    def parse(self, filename):
        """
        Extract based on strings defined in __init__
        """
        try:
            self.species_trees = dpy.TreeList.get(path=filename[0],
                                                  schema='nexus')
        except IOError as err:
            logger.error("problem reading species tree file\n" + err)
            return
        try:
            self.best_topology = \
            dpy.TreeList.get(path=filename[1], schema='nexus')[
                0]  # first tree is most frequent (unrooted)
        except IOError as err:
            logger.error("problem reading unrooted species tree file\n" + err)
            return

    def to_dict(self, filenames):
        """
        Add stuff for results dictionary here
        """
        self.parse(filenames)
        result = {
            'best_topology': self.best_topology,
            'species_trees': self.species_trees,
        }
        return result


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('slide', '0002_slide_image'),
    ]

    operations = [
        migrations.AlterField(
            model_name='slide',
            name='image',
            field=models.ImageField(height_field=b'height', upload_to=b'', width_field=b'width', verbose_name='\u56fe\u7247\u6587\u4ef6'),
        ),
    ]