#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.22
#  in conjunction with Tcl version 8.6
#    Apr 25, 2019 09:29:00 PM CAT  platform: Windows NT

import sys
from tkinter import filedialog
import numpy as np
from tkinter import messagebox



try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import form_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    form_support.set_Tk_var()
    top = Toplevel1 (root)
    form_support.init(root, top)
    root.mainloop()

w = None
def create_Toplevel1(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = tk.Toplevel (root)
    form_support.set_Tk_var()
    top = Toplevel1 (w)
    form_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:

    def __init__(self, top=None):
        import project
        import table
        import popup
        import perceptron
        import hebb
        import weight_matrix
        import perceptron_matrix
        self.final_x=[[]]
        self.final_y=[[]]
        self.train_x=[]
        self.test_x=[]
        self.train_y=[]
        self.test_y=[]
        self.set_al=1
        self.train_error=-1
        self.test_error=-1
        self.predicted_vals=[]
        self.last_weights=[]
        self.active_processing=0;

        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("834x668+216+10")
        top.title("Neural Networks")
        top.configure(background="#d9d9d9")
                      
          
        def filter_path(st):
            c=""
            counter=1
            allow=False
            for i in range(len(st)):
                if st[i]=='\'' and counter==2:
                    break
                elif st[i]=='\'' or allow==True:
                    c+=st[i]
                    allow=True
                    counter=2
            return(c[1:])

        self.Label1 = tk.Label(top)
        self.Label1.place(relx=0.288, rely=0.03, height=21, width=334)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(font="-family {Bookman Old Style} -size 20 -weight bold -slant italic")
        self.Label1.configure(foreground="#db5ab9")
        self.Label1.configure(text='''Neural Networks''')
        self.Label1.configure(width=334)


        self.DataSet = tk.LabelFrame(top)
        self.DataSet.place(relx=0.023, rely=0.061, relheight=0.299
                , relwidth=0.947)
        self.DataSet.configure(relief='groove')
        self.DataSet.configure(font="-family {Segoe UI} -size 14 -weight bold")
        self.DataSet.configure(foreground="black")
        self.DataSet.configure(text='''DataSet''')
        self.DataSet.configure(background="#d9d9d9")
        self.DataSet.configure(width=790)
        
        
        """-----------------------------------------------"""
        
        self.data_entry = tk.LabelFrame(self.DataSet)
        self.data_entry.place(relx=0.013, rely=0.110, relheight=0.385
                , relwidth=0.722, bordermode='ignore')
        self.data_entry.configure(relief='groove')
        self.data_entry.configure(font="-family {Segoe UI} -size 8 -weight bold")
        self.data_entry.configure(foreground="black")
        self.data_entry.configure(text='''Data Entry''')
        self.data_entry.configure(background="#d9d9d9")
        self.data_entry.configure(width=570)
        
        self.target_lb = tk.Label(self.data_entry)
        self.target_lb.place(relx=0.0, rely=0.600, height=21, width=173
                , bordermode='ignore')
        self.target_lb.configure(background="#d9d9d9")
        self.target_lb.configure(disabledforeground="#a3a3a3")
        self.target_lb.configure(font="-family {Segoe UI} -size 11")
        self.target_lb.configure(foreground="#000000")
        self.target_lb.configure(text='''Targets'Numbers''')
        self.target_lb.configure(width=173)

        self.target_en = tk.Entry(self.data_entry)
        self.target_en.place(relx=0.266, rely=0.600, height=20, relwidth=0.195
                , bordermode='ignore')
        self.target_en.configure(background="white")
        self.target_en.configure(disabledforeground="#a3a3a3")
        self.target_en.configure(font="TkFixedFont")
        self.target_en.configure(foreground="#000000")
        self.target_en.configure(insertbackground="black")
        self.target_en.configure(width=154)
        
        """-------------event------------------------------------"""
        def split_vars():
            if(self.Entry1.get()==""):
                messagebox.showwarning("Warning!!","Upload DataSet At First")
            else:
                num_target=int(self.target_en.get())
                self.set_al=num_target
                set_al()
                return num_target
        
        
        self.Label8 = tk.Label(self.DataSet)
        self.Label8.place(relx=0.038, rely=0.700, height=21, width=84
                , bordermode='ignore')
        self.Label8.configure(background="#d9d9d9")
        self.Label8.configure(disabledforeground="#a3a3a3")
        self.Label8.configure(font="-family {Segoe UI} -size 11")
        self.Label8.configure(foreground="#000000")
        self.Label8.configure(text='''Type of Test''')
        self.Label8.configure(width=84)

        self.test_type = ttk.Combobox(self.DataSet)
        self.test_type.place(relx=0.266, rely=0.690, relheight=0.120
                , relwidth=0.194, bordermode='ignore')
        self.value_list = ['Random','Kfold','LOO',]
        self.test_type.configure(values=self.value_list)
        self.test_type.configure(width=153)
        self.test_type.configure(takefocus="")

        self.test_rate_lb = tk.Label(self.DataSet)
        self.test_rate_lb.place(relx=0.0, rely=0.830, height=21, width=114
                , bordermode='ignore')
        self.test_rate_lb.configure(activebackground="#f9f9f9")
        self.test_rate_lb.configure(activeforeground="black")
        self.test_rate_lb.configure(background="#d9d9d9")
        self.test_rate_lb.configure(disabledforeground="#a3a3a3")
        self.test_rate_lb.configure(font="-family {Segoe UI} -size 10")
        self.test_rate_lb.configure(foreground="#000000")
        self.test_rate_lb.configure(highlightbackground="#d9d9d9")
        self.test_rate_lb.configure(highlightcolor="black")
        self.test_rate_lb.configure(text='''Test Rate''')
        
        
        self.random_state_lb = tk.Label(self.DataSet)
        self.random_state_lb.place(relx=0.245, rely=0.830, height=21, width=114
                , bordermode='ignore')
        self.random_state_lb.configure(activebackground="#f9f9f9")
        self.random_state_lb.configure(activeforeground="black")
        self.random_state_lb.configure(background="#d9d9d9")
        self.random_state_lb.configure(disabledforeground="#a3a3a3")
        self.random_state_lb.configure(font="-family {Segoe UI} -size 10")
        self.random_state_lb.configure(foreground="#000000")
        self.random_state_lb.configure(highlightbackground="#d9d9d9")
        self.random_state_lb.configure(highlightcolor="black")
        self.random_state_lb.configure(text='''Random State''')
        
        self.test_rate = tk.Entry(self.DataSet)
        self.test_rate.place(relx=0.120, rely=0.830, height=20, relwidth=0.10
                , bordermode='ignore')
        self.test_rate.configure(background="white")
        self.test_rate.configure(disabledforeground="#a3a3a3")
        self.test_rate.configure(font="TkFixedFont")
        self.test_rate.configure(foreground="#000000")
        self.test_rate.configure(highlightbackground="#d9d9d9")
        self.test_rate.configure(highlightcolor="black")
        self.test_rate.configure(insertbackground="black")
        self.test_rate.configure(selectbackground="#c4c4c4")
        self.test_rate.configure(selectforeground="black")
        self.test_rate.configure(width=400)
        
        self.random_state = tk.Entry(self.DataSet)
        self.random_state.place(relx=0.380, rely=0.830, height=20, relwidth=0.08
                , bordermode='ignore')
        self.random_state.configure(background="white")
        self.random_state.configure(disabledforeground="#a3a3a3")
        self.random_state.configure(font="TkFixedFont")
        self.random_state.configure(foreground="#000000")
        self.random_state.configure(highlightbackground="#d9d9d9")
        self.random_state.configure(highlightcolor="black")
        self.random_state.configure(insertbackground="black")
        self.random_state.configure(selectbackground="#c4c4c4")
        self.random_state.configure(selectforeground="black")
        self.random_state.configure(width=400)



        """-----------------------event------------------------"""
        
        def random_train_test(x,y,size,state):
            from sklearn.model_selection import train_test_split
            self.train_x, self.test_x, self.train_y, self.test_y = train_test_split(x, y, test_size = size, random_state = state)

        def kfold_train_test(X,Y,num):
            
            from sklearn.model_selection import KFold
            kfold = KFold(n_splits=num, shuffle=False, random_state=None)
            #k = kfold.get_n_splits(X)
            for train_index, test_index in kfold.split(X):
                self.train_x.append(np.asarray((X[train_index])))
                self.test_x.append(X[test_index])
                self.train_y.append(Y[train_index])
                self.test_y.append(Y[test_index])
            
        
        def leave_train_test(x,y):
            from sklearn.model_selection import LeaveOneOut
            LOO=LeaveOneOut()
            for train, test in LOO.split(x):
                self.train_x.append(x[train])
                self.test_x.append(x[test])
                self.train_y.append(y[train])
                self.test_y.append(y[test])
            
        
        
        def split_to_train_test():
            if(self.active_processing==0):
                messagebox.showinfo("Warning","Clean The Data At First (preprocessing)")
            else:
                test_ttp=self.test_type.get()
                
                self.train_x=[]
                self.train_y=[]
                self.test_x=[]
                self.test_y=[]
                
                
                if(test_ttp=="Random"):
                    if(self.test_rate.get()=="" or self.random_state.get()==""):
                        messagebox.showwarning("Warning!!","Test Size and Random State are required")
                    else:
                        random_train_test(self.final_x,self.final_y,float(self.test_rate.get()),int(self.random_state.get()))
                elif(test_ttp=="Kfold"):
                    if(self.test_rate.get()==""):
                        messagebox.showwarning("Warning!!","K is Required :Test Rate")
                    else:
                        kfold_train_test(self.final_x,self.final_y,int(self.test_rate.get()))
                elif(test_ttp=="LOO"):
                    leave_train_test(self.final_x,self.final_y)
                else:
                    self.train_x=self.final_x
                    self.train_y=self.final_y
                    self.test_x=self.final_x
                    self.test_y=self.final_y
                    
                print("train x:",self.train_x)
                print("test x : ",self.test_x)
                print("train y : ",self.train_y)
                print("test y: ",self.test_y)
            
            

            
        self.lab48_but45 = tk.Button(self.DataSet)
        self.lab48_but45.place(relx=0.506, rely=0.774, height=24, width=177
                , bordermode='ignore')
        self.lab48_but45.configure(activebackground="#ececec")
        self.lab48_but45.configure(activeforeground="#000000")
        self.lab48_but45.configure(background="#d9d9d9")
        self.lab48_but45.configure(disabledforeground="#a3a3a3")
        self.lab48_but45.configure(foreground="#000000")
        self.lab48_but45.configure(highlightbackground="#d9d9d9")
        self.lab48_but45.configure(highlightcolor="black")
        self.lab48_but45.configure(pady="0")
        self.lab48_but45.configure(text='''Split Data''')
        self.lab48_but45.configure(command=split_to_train_test)
            
        
        """----------------------------------------------------"""


        self.split_var = tk.Button(self.data_entry)
        self.split_var.place(relx=0.506, rely=0.600, height=24, width=177
                , bordermode='ignore')
        self.split_var.configure(activebackground="#ececec")
        self.split_var.configure(activeforeground="#000000")
        self.split_var.configure(background="#d9d9d9")
        self.split_var.configure(disabledforeground="#a3a3a3")
        self.split_var.configure(foreground="#000000")
        self.split_var.configure(highlightbackground="#d9d9d9")
        self.split_var.configure(highlightcolor="black")
        self.split_var.configure(pady="0")
        self.split_var.configure(text='''Split Variables''')
        self.split_var.configure(width=177)
        self.split_var.configure(command=split_vars)
        
        """---------------------preprocessing-----------------------"""
        self.Labelframe1 = tk.LabelFrame(self.DataSet)
        self.Labelframe1.place(relx=0.759, rely=0.150, relheight=0.800
                , relwidth=0.228, bordermode='ignore')
        self.Labelframe1.configure(relief='groove')
        self.Labelframe1.configure(font="-family {Segoe UI} -size 8 -weight bold")
        self.Labelframe1.configure(foreground="black")
        self.Labelframe1.configure(text='''Data Preprocessing''')
        self.Labelframe1.configure(background="#d9d9d9")
        self.Labelframe1.configure(width=180)

        self.Label6 = tk.Label(self.Labelframe1)
        self.Label6.place(relx=0.0, rely=0.15, height=21, width=74
                , bordermode='ignore')
        self.Label6.configure(background="#d9d9d9")
        self.Label6.configure(disabledforeground="#a3a3a3")
        self.Label6.configure(foreground="#000000")
        self.Label6.configure(text='''Missing Data''')

        self.Label7 = tk.Label(self.Labelframe1)
        self.Label7.place(relx=0.0, rely=0.35, height=21, width=44
                , bordermode='ignore')
        self.Label7.configure(background="#d9d9d9")
        self.Label7.configure(disabledforeground="#a3a3a3")
        self.Label7.configure(foreground="#000000")
        self.Label7.configure(text='''Scaling''')
        
        self.missing = ttk.Combobox(self.Labelframe1)
        self.missing.place(relx=0.444, rely=0.15, relheight=0.150, relwidth=0.517
                , bordermode='ignore')
        self.value_list = ['Mean','Median','Most frequent',]
        self.missing.configure(values=self.value_list)
        self.missing.configure(width=93)
        self.missing.configure(takefocus="")

        self.scaling = ttk.Combobox(self.Labelframe1)
        self.scaling.place(relx=0.444, rely=0.35, relheight=0.150, relwidth=0.517
                , bordermode='ignore')
        self.value_list = ['Standardization','Normalization','MinMax','No',]
        self.scaling.configure(values=self.value_list)
        self.scaling.configure(width=93)
        self.scaling.configure(takefocus="")
        
        
        self.ordinal = tk.Button(self.Labelframe1)
        self.ordinal.place(relx=0.167, rely=0.55, height=24, width=127
                , bordermode='ignore')
        self.ordinal.configure(activebackground="#ececec")
        self.ordinal.configure(activeforeground="#000000")
        self.ordinal.configure(background="#d9d9d9")
        self.ordinal.configure(disabledforeground="#a3a3a3")
        self.ordinal.configure(foreground="#000000")
        self.ordinal.configure(highlightbackground="#d9d9d9")
        self.ordinal.configure(highlightcolor="black")
        self.ordinal.configure(pady="0")
        self.ordinal.configure(text='''ADD Ordinal Features ''')
        self.ordinal.configure(width=127)
        self.ordinal.configure(command=popup.popup_bonus)

        
        

        """--------------------------event--------------------------------"""
        def missing_data(X,way):
            from sklearn.preprocessing import Imputer
            imputer = Imputer(missing_values = 'NaN', strategy = way, axis = 0)
            
            increase=1
            for i in range(len(X[0])):
                if not(isinstance(X[0][i],str)):
                    imputer = imputer.fit(X[:,i:increase])
                    X[:,i:increase] = imputer.transform(X[:,i:increase])
                increase+=1
            return X
        
        
        def ordinal_val(dataset,X,test):
            from sklearn.preprocessing import LabelEncoder, OneHotEncoder
            labelencoder_X = LabelEncoder()
            x=popup.ordinal
            
            def ret_vals(vals):
                dict2={}
                counter=1
                vals="".join(vals).split(',')
                for i in vals:
                    dict2.update({i:counter})
                    counter+=1
                return dict2
                        
            def find(num,x):
                for i in x:
                    if num in i:
                        return True,i.values()
                return False,0
            
            categ=[]
            for i in range(len(X[0])):
                if isinstance(X[0][i],str):
                    check,vals=find(i,x)
                    if check:
                        dataset.iloc[:,i] = dataset.iloc[:,i].map(ret_vals(vals))
                        X[:,i] = dataset.iloc[:,i].values
                       
                    else:
                        X[:, i] = labelencoder_X.fit_transform(X[:, i])
                        categ.append(i)
            if not(categ==[]):
                if not(test):           
                    one = OneHotEncoder(categorical_features = categ)
                    X = one.fit_transform(X).toarray()
            
            return X
        
        def scaling_data(x,way):
            from sklearn.preprocessing import StandardScaler,Normalizer,MinMaxScaler
            if way=="Standardization":
                standardization = StandardScaler()
                x_scaled = standardization.fit_transform(x)
            elif way=="Normalization":
                normailzation = Normalizer()
                x_scaled = normailzation.fit_transform(x)
            elif way=="MinMax":
                minMaxScaled_rescaling = MinMaxScaler()
                x_scaled = minMaxScaled_rescaling.fit_transform(x)
            elif way=="No":
                x_scaled=x
            return x_scaled
        
 
        def process_data():
            if(self.target_en.get()==""):
                messagebox.showinfo("Warning!!","Split Variables Of Data At First")
            else:
                self.active_processing=1;
                missing_val=self.missing.get()
                scaling_val=self.scaling.get()
                num=split_vars()
                dataset=project.set_dataset(self.Entry1.get())
                x = dataset.iloc[:,:-num].values
                y = dataset.iloc[:,-num:].values
                
                if not(missing_val==""):
                    x=missing_data(x,missing_val.replace(' ','_').lower())
                    
                x=ordinal_val(dataset,x,False)
                y=ordinal_val(dataset,y,True)
                
                if not(scaling_val==""):
                    x=scaling_data(x,scaling_val)
                
                self.final_x=x
                
                self.final_y=y
                
        def ret_x():
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                table.Main(self.final_x,'Independent Variables')
        
        def ret_y():
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                table.Main(self.final_y,'Dependent Variables')
            
             


        self.preprocessing = tk.Button(self.Labelframe1)
        self.preprocessing.place(relx=0.167, rely=0.72, height=24, width=127
                , bordermode='ignore')
        self.preprocessing.configure(activebackground="#ececec")
        self.preprocessing.configure(activeforeground="#000000")
        self.preprocessing.configure(background="#d9d9d9")
        self.preprocessing.configure(disabledforeground="#a3a3a3")
        self.preprocessing.configure(foreground="#000000")
        self.preprocessing.configure(highlightbackground="#d9d9d9")
        self.preprocessing.configure(highlightcolor="black")
        self.preprocessing.configure(pady="0")
        self.preprocessing.configure(text='''Preprocessing''')
        self.preprocessing.configure(width=127)
        self.preprocessing.configure(command=process_data)
        



        

        self.Entry1 = tk.Entry(self.data_entry)
        self.Entry1.place(relx=0.013, rely=0.220, height=20, relwidth=0.450
                , bordermode='ignore')
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font="TkFixedFont")
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(width=364)

        self.Button1 = tk.Button(self.data_entry)
        self.Button1.place(relx=0.506, rely=0.220, height=24, width=177
                , bordermode='ignore')
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Upload Dataset''')
        self.Button1.configure(width=177)

        def open_file():
            self.Entry1.delete(0,'end')
            self.target_en.delete(0,'end')
            self.active_processing=0;
            state_normal()
            self.test_rate.delete(0,'end')
            self.random_state.delete(0,'end')
            result=filedialog.askopenfile(initialdir="/desktop/Project_Data",title="select Dataset file")
            self.Entry1.insert(0,filter_path(str(result)))
            
           
            
       
        
        self.Button1.configure(command=open_file)
        
        self.Button2 = tk.Button(self.DataSet)
        self.Button2.place(relx=0.025, rely=0.516, height=24, width=137
                , bordermode='ignore')
        self.Button2.configure(activebackground="#ececec")
        self.Button2.configure(activeforeground="#000000")
        self.Button2.configure(background="#d9d9d9")
        self.Button2.configure(disabledforeground="#a3a3a3")
        self.Button2.configure(foreground="#000000")
        self.Button2.configure(highlightbackground="#d9d9d9")
        self.Button2.configure(highlightcolor="black")
        self.Button2.configure(pady="0")
        self.Button2.configure(text='''Show DataSet''')
        self.Button2.configure(width=137)
 
    
        def show_dataset():
            if(self.Entry1.get()==""):
                messagebox.showwarning("Warning!!","Upload DataSet At First")
            else:
                table.Main(project.set_dataset(self.Entry1.get()).iloc[:,:].values,'Dataset')
            
        
            
    
        self.Button2.configure(command=show_dataset)
           


        self.Button3 = tk.Button(self.DataSet)
        self.Button3.place(relx=0.266, rely=0.516, height=24, width=157
                , bordermode='ignore')
        self.Button3.configure(activebackground="#ececec")
        self.Button3.configure(activeforeground="#000000")
        self.Button3.configure(background="#d9d9d9")
        self.Button3.configure(disabledforeground="#a3a3a3")
        self.Button3.configure(foreground="#000000")
        self.Button3.configure(highlightbackground="#d9d9d9")
        self.Button3.configure(highlightcolor="black")
        self.Button3.configure(pady="0")
        self.Button3.configure(text='''Independent Variables''')
        self.Button3.configure(width=157)
        self.Button3.configure(command=ret_x)
        

        self.Button4 = tk.Button(self.DataSet)
        self.Button4.place(relx=0.506, rely=0.516, height=24, width=177
                , bordermode='ignore')
        self.Button4.configure(activebackground="#ececec")
        self.Button4.configure(activeforeground="#000000")
        self.Button4.configure(background="#d9d9d9")
        self.Button4.configure(disabledforeground="#a3a3a3")
        self.Button4.configure(foreground="#000000")
        self.Button4.configure(highlightbackground="#d9d9d9")
        self.Button4.configure(highlightcolor="black")
        self.Button4.configure(pady="0")
        self.Button4.configure(text='''Dependent Variables''')
        self.Button4.configure(width=177)
        self.Button4.configure(command=ret_y)
        

        

        """-------------------------------------------------------------------"""

        """-------------------------------------------------------"""
        
        
        
        

        self.Labelframe2 = tk.LabelFrame(top)
        self.Labelframe2.place(relx=0.024, rely=0.359, relheight=0.367
                , relwidth=0.947)
        self.Labelframe2.configure(relief='groove')
        self.Labelframe2.configure(font="-family {Segoe UI} -size 13 -weight bold")
        self.Labelframe2.configure(foreground="black")
        self.Labelframe2.configure(text='''Training Phase''')
        self.Labelframe2.configure(background="#d9d9d9")
        self.Labelframe2.configure(width=790)


        self.Label4 = tk.Label(self.Labelframe2)
        self.Label4.place(relx=0.013, rely=0.163, height=21, width=164
                , bordermode='ignore')
        self.Label4.configure(background="#d9d9d9")
        self.Label4.configure(disabledforeground="#a3a3a3")
        self.Label4.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.Label4.configure(foreground="#000000")
        self.Label4.configure(text='''Select the Algorithm''')
        self.Label4.configure(width=164)

        self.TCombobox1 = ttk.Combobox(self.Labelframe2)
        self.TCombobox1.place(relx=0.228, rely=0.163, relheight=0.086
                , relwidth=0.257, bordermode='ignore')
        self.value_list1 = ['Hebbian','Perceptron',]
        
      
        """-----------------------test----------------------------"""
        
        def set_normal(event):
            state_normal()
            clear_list()
            if self.set_al!=1:
                self.value_list2 = ['Weight Matrix','Perceptron Matrix',]
                self.TCombobox1.configure(values=self.value_list2)
            else:
                self.value_list2 = ['Hebbian','Perceptron',]
                self.TCombobox1.configure(values=self.value_list2)
                
        
        self.TCombobox1.configure(values=self.value_list1)
        self.TCombobox1.configure(width=203)
        self.TCombobox1.configure(takefocus="")
        self.TCombobox1.bind("<<ComboboxSelected>>",set_normal)
        
        
        def set_al():
            if(self.set_al!=1):
                self.value_list2 = ['Weight Matrix','Perceptron Matrix',]
                self.TCombobox1.configure(values=self.value_list2)

        self.Scrolledlistbox1 = ScrolledListBox(self.Labelframe2)
        self.Scrolledlistbox1.place(relx=0.595, rely=0.163, relheight=0.306
                , relwidth=0.356, bordermode='ignore')
        self.Scrolledlistbox1.configure(background="white")
        self.Scrolledlistbox1.configure(disabledforeground="#a3a3a3")
        self.Scrolledlistbox1.configure(font="TkFixedFont")
        self.Scrolledlistbox1.configure(foreground="black")
        self.Scrolledlistbox1.configure(highlightbackground="#d9d9d9")
        self.Scrolledlistbox1.configure(highlightcolor="#d9d9d9")
        self.Scrolledlistbox1.configure(selectbackground="#c4c4c4")
        self.Scrolledlistbox1.configure(selectforeground="black")
        self.Scrolledlistbox1.configure(width=10)

        self.Label4_1 = tk.Label(self.Labelframe2)
        self.Label4_1.place(relx=0.013, rely=0.327, height=21, width=164
                , bordermode='ignore')
        self.Label4_1.configure(activebackground="#f9f9f9")
        self.Label4_1.configure(activeforeground="black")
        self.Label4_1.configure(background="#d9d9d9")
        self.Label4_1.configure(disabledforeground="#a3a3a3")
        self.Label4_1.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.Label4_1.configure(foreground="#000000")
        self.Label4_1.configure(highlightbackground="#d9d9d9")
        self.Label4_1.configure(highlightcolor="black")
        self.Label4_1.configure(text='''The number of epochs''')
        self.Label4_1.configure(width=164)

        self.Label4_2 = tk.Label(self.Labelframe2)
        self.Label4_2.place(relx=0.62, rely=0.082, height=21, width=244
                , bordermode='ignore')
        self.Label4_2.configure(activebackground="#f9f9f9")
        self.Label4_2.configure(activeforeground="black")
        self.Label4_2.configure(background="#d9d9d9")
        self.Label4_2.configure(disabledforeground="#a3a3a3")
        self.Label4_2.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.Label4_2.configure(foreground="#000000")
        self.Label4_2.configure(highlightbackground="#d9d9d9")
        self.Label4_2.configure(highlightcolor="black")
        self.Label4_2.configure(text='''Weights in each epoch''')
        self.Label4_2.configure(width=244)

        self.Entry2 = tk.Entry(self.Labelframe2)
        self.Entry2.place(relx=0.228, rely=0.327, height=20, relwidth=0.258
                , bordermode='ignore')
        self.Entry2.configure(background="white")
        self.Entry2.configure(disabledforeground="#a3a3a3")
        self.Entry2.configure(font="TkFixedFont")
        self.Entry2.configure(foreground="#000000")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(width=204)

        self.Scrolledlistbox1_4 = ScrolledListBox(self.Labelframe2)
        self.Scrolledlistbox1_4.place(relx=0.595, rely=0.612, relheight=0.347
                , relwidth=0.356, bordermode='ignore')
        self.Scrolledlistbox1_4.configure(background="white")
        self.Scrolledlistbox1_4.configure(disabledforeground="#a3a3a3")
        self.Scrolledlistbox1_4.configure(font="TkFixedFont")
        self.Scrolledlistbox1_4.configure(foreground="black")
        self.Scrolledlistbox1_4.configure(highlightbackground="#d9d9d9")
        self.Scrolledlistbox1_4.configure(highlightcolor="#d9d9d9")
        self.Scrolledlistbox1_4.configure(selectbackground="#c4c4c4")
        self.Scrolledlistbox1_4.configure(selectforeground="black")
        self.Scrolledlistbox1_4.configure(width=10)

        self.Label4_3 = tk.Label(self.Labelframe2)
        self.Label4_3.place(relx=0.62, rely=0.49, height=21, width=244
                , bordermode='ignore')
        self.Label4_3.configure(activebackground="#f9f9f9")
        self.Label4_3.configure(activeforeground="black")
        self.Label4_3.configure(background="#d9d9d9")
        self.Label4_3.configure(disabledforeground="#a3a3a3")
        self.Label4_3.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.Label4_3.configure(foreground="#000000")
        self.Label4_3.configure(highlightbackground="#d9d9d9")
        self.Label4_3.configure(highlightcolor="black")
        self.Label4_3.configure(text='''Error rate in each epoch''')

        self.Label4_2 = tk.Label(self.Labelframe2)
        self.Label4_2.place(relx=0.013, rely=0.49, height=21, width=154
                , bordermode='ignore')
        self.Label4_2.configure(activebackground="#f9f9f9")
        self.Label4_2.configure(activeforeground="black")
        self.Label4_2.configure(background="#d9d9d9")
        self.Label4_2.configure(disabledforeground="#a3a3a3")
        self.Label4_2.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.Label4_2.configure(foreground="#000000")
        self.Label4_2.configure(highlightbackground="#d9d9d9")
        self.Label4_2.configure(highlightcolor="black")
        self.Label4_2.configure(text='''Learning Rate''')
        self.Label4_2.configure(width=154)
        
        
        
        self.error_rt = tk.Label(self.Labelframe2)
        self.error_rt.place(relx=0.013, rely=0.65, height=21, width=154
                , bordermode='ignore')
        self.error_rt.configure(activebackground="#f9f9f9")
        self.error_rt.configure(activeforeground="black")
        self.error_rt.configure(background="#d9d9d9")
        self.error_rt.configure(disabledforeground="#a3a3a3")
        self.error_rt.configure(font="-family {Trebuchet MS} -size 12 -slant italic")
        self.error_rt.configure(foreground="#000000")
        self.error_rt.configure(highlightbackground="#d9d9d9")
        self.error_rt.configure(highlightcolor="black")
        self.error_rt.configure(text='''Error Rate''')
        self.error_rt.configure(width=154)
        
        
        
        
        self.Entry2_3 = tk.Entry(self.Labelframe2)
        self.Entry2_3.place(relx=0.228, rely=0.49, height=20, relwidth=0.258
                , bordermode='ignore')
        self.Entry2_3.configure(background="white")
        self.Entry2_3.configure(disabledforeground="#a3a3a3")
        self.Entry2_3.configure(font="TkFixedFont")
        self.Entry2_3.configure(foreground="#000000")
        self.Entry2_3.configure(highlightbackground="#d9d9d9")
        self.Entry2_3.configure(highlightcolor="black")
        self.Entry2_3.configure(insertbackground="black")
        self.Entry2_3.configure(selectbackground="#c4c4c4")
        self.Entry2_3.configure(selectforeground="black")
        
        self.error_en = tk.Entry(self.Labelframe2)
        self.error_en.place(relx=0.228, rely=0.65, height=20, relwidth=0.258
                , bordermode='ignore')
        self.error_en.configure(background="white")
        self.error_en.configure(disabledforeground="#a3a3a3")
        self.error_en.configure(font="TkFixedFont")
        self.error_en.configure(foreground="#000000")
        self.error_en.configure(highlightbackground="#d9d9d9")
        self.error_en.configure(highlightcolor="black")
        self.error_en.configure(insertbackground="black")
        self.error_en.configure(selectbackground="#c4c4c4")
        self.error_en.configure(selectforeground="black")


        """-------------------------------events----------------------"""
        
        def clear_list():
            self.Scrolledlistbox1.delete(0,'end')
            self.Scrolledlistbox1_4.delete(0,'end')
            
        def state_normal():
            
            self.Entry2.configure(state="normal")
            self.Entry2_3.configure(state="normal")
            self.error_en.configure(state="normal")
            self.Entry3_5.configure(state="normal")
            self.Entry2.delete(0,'end')
            self.Entry2_3.delete(0,'end')
            self.error_en.delete(0,'end')
            self.Entry3_5.delete(0,'end')
            self.Scrolledlistbox1.delete(0,'end')
            self.Scrolledlistbox1_4.delete(0,'end')
            
            
   
        def state_disabled():
                                
            self.Entry2.configure(state="disabled")
            self.Entry2_3.configure(state="disabled")
        
        def train_model():
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                self.last_weights=[]
                alg=self.TCombobox1.get()
                initial=list(np.zeros(len(self.final_x[0])))
            
                if(self.test_type.get()=="Kfold"or self.test_type.get()=="LOO"): 
                    if(alg=="Perceptron"):
                        num_epochs=int(self.Entry2.get())
                        learning_rt=float(self.Entry2_3.get())
        
                        counter=0
                        sum2=[]
                        self.last_weights=[]
                        for k in range(len(self.train_x)):
                            new_="------- train<"+str(k+1)+">-------"
                            self.Scrolledlistbox1.insert(counter,new_)
                            self.Scrolledlistbox1_4.insert(counter,new_)
                            counter+=1
                            p=perceptron.perceptron(self.final_x,self.final_y,1,0,initial,learning_rt,num_epochs)
                            p.call(self.train_x[k],self.train_y[k],self.test_x[k],self.test_y[k])
                            weights=p.ret_weights()
                            
                            we,bi=p.ret_last_weights()
                            wt=[we]
                            wt.append(bi)
                            self.last_weights.append(wt)
                            
                            #print(weights)
                            error_epoch=p.ret_errors_epochs()
                            sum2.append(error_epoch.sum()/len(weights))
                            print(error_epoch)
        
                            for i in range(len(weights)):
                                st="------ epoch<"+(str(i+1))+">-------"
                                self.Scrolledlistbox1.insert(counter,st)
                                self.Scrolledlistbox1_4.insert(counter,st)
                                counter+=1
                                self.Scrolledlistbox1.insert(counter,weights[i])
                                self.Scrolledlistbox1_4.insert(counter,"error : "+str(error_epoch[i]))
                                counter+=1
                        self.train_error=(np.array(sum2).sum())/len(self.train_x)
    
                    elif(alg=="Hebbian"):
                        state_disabled()
                           
                        counter=0
                        sum2=[]
                        self.last_weights=[]
                        for k in range(len(self.train_x)):
                            new_="------- train<"+str(k+1)+">-------"
                            self.Scrolledlistbox1.insert(counter,new_)
                            self.Scrolledlistbox1_4.insert(counter,new_)
                            counter+=1
                            h=hebb.hebb(self.final_x,self.final_y,1,0,initial)
                            h.call(self.train_x[k],self.train_y[k])
                            weights=h.ret_weights()
                            
                            we,bi=h.ret_final_weight()
                            wt=[we]
                            wt.append(bi)
                            self.last_weights.append(wt)
                            
                            #print(weights)
                            error_samples=h.ret_training_error()
                            sum2.append(np.array(error_samples).sum()/len(error_samples))
                            j=0
                            for i in range(1,len(weights)):
                                st="------ training sample<"+(str(i))+">-------"
                                self.Scrolledlistbox1.insert(counter,st)
                                self.Scrolledlistbox1_4.insert(counter,st)
                                counter+=1
                                self.Scrolledlistbox1.insert(counter,weights[i])
                                self.Scrolledlistbox1_4.insert(counter,"error : "+str(error_samples[j]))
                                counter+=1
                                j+=1
                                
                            
                        self.train_error=(np.array(sum2).sum())/len(self.train_x)
                        print(self.train_error)
                    
                    elif(alg=="Weight Matrix"):
                        state_disabled()
                        counter=0
                        j=0
                        total=[]
                        for k in range(len(self.train_x)):
                            w=weight_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y))
                            w.training(np.matrix(self.train_x[k]),np.matrix(self.train_y[k]))
                            self.last_weights.append(w.ret_final_weight())
                            new_="------- train<"+str(k+1)+">-------"
                            self.Scrolledlistbox1.insert(counter,new_)
                            self.Scrolledlistbox1_4.insert(j,new_)
                            j+=1
                            counter+=1
                            self.Scrolledlistbox1.insert(counter,'------weight matrix-----')
                            counter+=1
                            self.Scrolledlistbox1.insert(counter,str(w.ret_final_weight()))
                            sample_error=w.ret_train_error()
                            counter+=1
                            sum2=[]
                            
                            for i in range(len(sample_error)):
                                st="-------- training sample <"+(str(i+1))+">-------"
                                self.Scrolledlistbox1_4.insert(j,st)
                                j+=1
                                self.Scrolledlistbox1_4.insert(j,"error : "+str(sample_error[i]))
                                j+=1
                                sum2.append(np.array(sample_error[i]).sum()/len(sample_error[i]))
                                
                            total.append(np.array(sum2).sum()/len(sum2))
                        self.train_error=np.array(total).sum()/len(total)
                    
                        
                    if(alg=="Perceptron Matrix"):
                        """-------------------------------------------------"""
                        num_epochs=int(self.Entry2.get())
                        learning_rt=float(self.Entry2_3.get())
                        matrix=np.random.rand(np.matrix(self.final_x).shape[1],np.matrix(self.final_y).shape[1])
    
                        counter=0
                        sum2=[]
                        self.last_weights=[]
                        for k in range(len(self.train_x)):
                            new_="------- train<"+str(k+1)+">-------"
                            self.Scrolledlistbox1.insert(counter,new_)
                            self.Scrolledlistbox1_4.insert(counter,new_)
                            counter+=1
                            p=perceptron_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y),learning_rt,num_epochs,matrix)
                            p.loop(np.matrix(self.train_x[k]),np.matrix(self.train_y[k]))
                            self.last_weights.append(p.ret_final_weight())
                            weights=p.ret_epoch_weights()
                            #print(weights)
                            error_epoch=p.ret_train_error()
                            sum2.append(np.array(error_epoch).sum()/len(error_epoch))
                            print(error_epoch)
        
                            for i in range(len(weights)):
                                st="------ epoch<"+(str(i+1))+">-------"
                                self.Scrolledlistbox1.insert(counter,st)
                                self.Scrolledlistbox1_4.insert(counter,st)
                                counter+=1
                                self.Scrolledlistbox1.insert(counter,weights[i])
                                self.Scrolledlistbox1_4.insert(counter,"error : "+str(error_epoch[i]))
                                counter+=1
                        self.train_error=(np.array(sum2).sum())/len(self.train_x)
                        

                        
                        
                        """-------------------------------------------------"""
                        
                        
       
                    
                    
                    self.error_en.configure(state="normal")
                    self.error_en.delete(0,'end')
                    self.error_en.insert(0,str(self.train_error))
                    self.error_en.configure(state="disabled")
                            
                        
                else:
                    if(alg=="Perceptron"):
                        num_epochs=int(self.Entry2.get())
                        learning_rt=float(self.Entry2_3.get())
                        self.Scrolledlistbox1.delete(0,'end')
                        self.Scrolledlistbox1_4.delete(0,'end')
                        self.last_weights=[]
                        counter=0
                        p=perceptron.perceptron(self.final_x,self.final_y,1,0,initial,learning_rt,num_epochs)
                        p.call(self.train_x,self.train_y,self.test_x,self.test_y)
                        weights=p.ret_weights()
                        #print(weights)
                        error_epoch=p.ret_errors_epochs()
                        self.train_error=error_epoch.sum()/len(weights)
                        
                        we,bi=p.ret_last_weights()
                        wt=[we]
                        wt.append(bi)
                        self.last_weights.append(wt)
                        
                        for i in range(len(weights)):
                                
                            st="------ epoch<"+(str(i+1))+">-------"
                            self.Scrolledlistbox1.insert(counter,st)
                            self.Scrolledlistbox1_4.insert(counter,st)
                            counter+=1
                            self.Scrolledlistbox1.insert(counter,weights[i])
                            self.Scrolledlistbox1_4.insert(counter,"error : "+str(error_epoch[i]))
                            counter+=1
                    
                    
                    elif(alg=="Hebbian"):
                        state_disabled()
                        
                        h=hebb.hebb(self.final_x,self.final_y,1,0,initial)
                        h.call(self.train_x,self.train_y)
                        weights=h.ret_weights()
                        sample_error=h.ret_training_error()
                        self.train_error=np.array(sample_error).sum()/len(sample_error)
                        
                        we,bi=h.ret_final_weight()
                        wt=[we]
                        wt.append(bi)
                        self.last_weights.append(wt)
                        counter=0
                        j=0
                        for i in range(1,len(weights)):
                            st="------ training sample <"+(str(i))+">-------"
                            self.Scrolledlistbox1.insert(counter,st)
                            self.Scrolledlistbox1_4.insert(counter,st)
                            counter+=1
                            self.Scrolledlistbox1.insert(counter,weights[i])
                            self.Scrolledlistbox1_4.insert(counter,"error : "+str(sample_error[j]))
                            counter+=1
                            j+=1
                           
                        
                    elif(alg=="Weight Matrix"):
                        state_disabled()
                        w=weight_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y))
                        w.training(np.matrix(self.train_x),np.matrix(self.train_y))
                        self.Scrolledlistbox1.insert(0,'---------weight matrix-----')
                        self.Scrolledlistbox1.insert(1,str(w.ret_final_weight()))
                        sample_error=w.ret_train_error()
                        counter=0
                        sum2=[]
                        self.last_weights.append(w.ret_final_weight())
                        for i in range(len(sample_error)):
                            st="------ training sample <"+(str(i+1))+">-------"
                
                            self.Scrolledlistbox1_4.insert(counter,st)
                            counter+=1
                            self.Scrolledlistbox1_4.insert(counter,"error : "+str(sample_error[i]))
                            counter+=1
                            sum2.append(np.array(sample_error[i]).sum()/len(sample_error[i]))
                        self.train_error=np.array(sum2).sum()/len(sum2)   
                        
                    elif(alg=="Perceptron Matrix"):
                        epochs=int(self.Entry2.get())
                        learning_rate=float(self.Entry2_3.get())
                        matrix=np.random.rand(np.matrix(self.final_x).shape[1],np.matrix(self.final_y).shape[1])
                        p=perceptron_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y),learning_rate,epochs,matrix)
                        self.last_weights=[]
                        counter=0
                        p.loop(np.matrix(self.train_x),np.matrix(self.train_y))
                        weights=p.ret_epoch_weights()
                        #print(weights)
                        error_epoch=p.ret_train_error()
                        self.train_error=np.array(error_epoch).sum()/len(error_epoch)
                        self.last_weights.append(p.ret_final_weight())
                        
                        for i in range(len(weights)):
                                
                            st="------ epoch<"+(str(i+1))+">-------"
                            self.Scrolledlistbox1.insert(counter,st)
                            self.Scrolledlistbox1_4.insert(counter,st)
                            counter+=1
                            self.Scrolledlistbox1.insert(counter,str(weights[i]))
                            self.Scrolledlistbox1_4.insert(counter,"error : "+str(error_epoch[i]))
                            counter+=1
        
                    self.error_en.configure(state="normal")
                    self.error_en.delete(0,'end')
                    self.error_en.insert(0,str(self.train_error))
                    self.error_en.configure(state="disabled")
                        
                    
                    

        self.Button5 = tk.Button(self.Labelframe2)
        self.Button5.place(relx=0.025, rely=0.776, height=24, width=367
                , bordermode='ignore')
        self.Button5.configure(activebackground="#ececec")
        self.Button5.configure(activeforeground="#000000")
        self.Button5.configure(background="#d9d9d9")
        self.Button5.configure(disabledforeground="#a3a3a3")
        self.Button5.configure(foreground="#000000")
        self.Button5.configure(highlightbackground="#d9d9d9")
        self.Button5.configure(highlightcolor="black")
        self.Button5.configure(pady="0")
        self.Button5.configure(text='''Training The Model''')
        self.Button5.configure(width=367)
        self.Button5.configure(command=train_model)
        
        

        self.Labelframe3 = tk.LabelFrame(top)
        self.Labelframe3.place(relx=0.024, rely=0.749, relheight=0.142
                , relwidth=0.947)
        self.Labelframe3.configure(relief='groove')
        self.Labelframe3.configure(font="-family {Segoe UI} -size 14 -weight bold")
        self.Labelframe3.configure(foreground="black")
        self.Labelframe3.configure(text='''Testing Phase''')
        self.Labelframe3.configure(background="#d9d9d9")
        self.Labelframe3.configure(width=790)


        self.Label5_4 = tk.Label(self.Labelframe3)
        self.Label5_4.place(relx=0.0, rely=0.632, height=21, width=114
                , bordermode='ignore')
        self.Label5_4.configure(activebackground="#f9f9f9")
        self.Label5_4.configure(activeforeground="black")
        self.Label5_4.configure(background="#d9d9d9")
        self.Label5_4.configure(disabledforeground="#a3a3a3")
        self.Label5_4.configure(font="-family {Segoe UI} -size 12")
        self.Label5_4.configure(foreground="#000000")
        self.Label5_4.configure(highlightbackground="#d9d9d9")
        self.Label5_4.configure(highlightcolor="black")
        self.Label5_4.configure(text='''Error Rate''')

        self.Entry3_5 = tk.Entry(self.Labelframe3)
        self.Entry3_5.place(relx=0.165, rely=0.632, height=20, relwidth=0.208
                , bordermode='ignore')
        self.Entry3_5.configure(background="white")
        self.Entry3_5.configure(disabledforeground="#a3a3a3")
        self.Entry3_5.configure(font="TkFixedFont")
        self.Entry3_5.configure(foreground="#000000")
        self.Entry3_5.configure(highlightbackground="#d9d9d9")
        self.Entry3_5.configure(highlightcolor="black")
        self.Entry3_5.configure(insertbackground="black")
        self.Entry3_5.configure(selectbackground="#c4c4c4")
        self.Entry3_5.configure(selectforeground="black")
        
        
        
        
        """-----------------event----------------------------"""
        def test_model():
            self.predicted_vals=[]
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                alg=self.TCombobox1.get()
                if(self.test_type.get()=="Kfold" or self.test_type.get()=="LOO"):
                    sum2=[]
                    self.predicted_vals=[]
                    initial=list(np.zeros(len(self.final_x[0])))
                    if(alg=="Perceptron"):
                        num_epochs=int(self.Entry2.get())
                        learning_rt=float(self.Entry2_3.get())
                        print(self.last_weights)
                        for k in range(len(self.test_x)):
                            p=perceptron.perceptron(self.final_x,self.final_y,1,self.last_weights[k][1],self.last_weights[k][0],learning_rt,num_epochs)
                            predicted_vals=p.predicted_vals(self.test_x[k],self.test_y[k])
                            self.predicted_vals.append(np.full((len(predicted_vals),1),predicted_vals))
                            #test=np.full((len(self.predicted_vals[k]),1),self.predicted_vals[k])
    
                            sum2.append(p.ret_test_errors())
                           
                        self.test_error=(np.array(sum2)).sum()/len(self.test_x)
                    elif(alg=="Hebbian"):
    
                        print(self.last_weights)
                        for k in range(len(self.test_x)):
                            h=hebb.hebb(self.final_x,self.final_y,1,self.last_weights[k][1],self.last_weights[k][0])
                            #self.predicted_vals.append(h.ret_predicted_vals(self.test_x[k],self.test_y[k]))
                            predicted_vals=h.ret_predicted_vals(self.test_x[k],self.test_y[k])
                            self.predicted_vals.append(np.full((len(predicted_vals),1),predicted_vals))
                            sum2.append(h.ret_test_error())
                           
                        self.test_error=(np.array(sum2)).sum()/len(self.test_x)
                        
                    elif(alg=="Weight Matrix"):
                        
                        app=[]
                        for k in range(len(self.test_x)):
                            sum2=[]
                            m=weight_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y))
                            predicted_vals=m.ret_predicted_vals(np.matrix(self.test_x[k]),np.matrix(self.test_y[k]),self.last_weights[k])
                            self.predicted_vals.append(predicted_vals)
                            print('vals:::',self.predicted_vals)
                            test_error=m.ret_test_error()
                            print('--=====',self.predicted_vals)
                            
                        
                            for i in range(len(test_error)):
                                sum2.append(np.array(test_error[i]).sum()/len(test_error[i]))
                            app.append(np.array(sum2).sum()/len(sum2))
                            
                        self.test_error=np.array(app).sum()/len(app)
                        
                    elif(alg=="Perceptron Matrix"):
                        
                        num_epochs=int(self.Entry2.get())
                        learning_rt=float(self.Entry2_3.get())
                        print(self.last_weights)
                        print(self.last_weights[0])
                        
                        for k in range(len(self.test_x)):
                            p=perceptron_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y),learning_rt,num_epochs,self.last_weights[k])
                            predicted_vals=p.ret_predicted_vals(np.matrix(self.test_x[k]),np.matrix(self.test_y[k]))
                            self.predicted_vals.append(predicted_vals)
                            #test=np.full((len(self.predicted_vals[k]),1),self.predicted_vals[k])
    
                            sum2.append(p.ret_test_error())
                           
                        self.test_error=(np.array(sum2)).sum()/len(self.test_x)
                        
                            
                        
                        
                    self.Entry3_5.configure(state="normal")   
                    self.Entry3_5.delete(0,'end')
                    self.Entry3_5.insert(0,self.test_error)
                    self.Entry3_5.configure(state="disabled")
                    
     
                else:
                    if(alg=="Perceptron"):
                         num_epochs=int(self.Entry2.get())
                         learning_rt=float(self.Entry2_3.get())
                         p=perceptron.perceptron(self.final_x,self.final_y,1,self.last_weights[0][1],self.last_weights[0][0],learning_rt,num_epochs)
                         self.predicted_vals.append(p.predicted_vals(self.test_x,self.test_y))
                         print(p.ret_test_errors())
                         self.test_error=p.ret_test_errors()
                         
                    elif(alg=="Hebbian"):
                         h=hebb.hebb(self.final_x,self.final_y,1,self.last_weights[0][1],self.last_weights[0][0])
                         self.predicted_vals.append(h.ret_predicted_vals(self.test_x,self.test_y))
                         print(h.ret_test_error)
                         self.test_error=h.ret_test_error()
                    elif(alg=="Weight Matrix"):
                        sum2=[]
                        m=weight_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y))
                        predicted_vals=m.ret_predicted_vals(np.matrix(self.test_x),np.matrix(self.test_y),self.last_weights[0])
                        self.predicted_vals=np.full((len(self.test_x),len(self.final_y[0])),predicted_vals).T
                        print("predicted:: ",self.predicted_vals)
                        
                        test_error=m.ret_test_error()
                        print("--------------")
                        print(test_error)
                        for i in range(len(test_error)):
                            sum2.append(np.array(test_error[i]).sum()/len(test_error[i]))
                        print(sum2)
                        self.test_error=np.array(sum2).sum()/len(sum2)
                        
                    elif(alg=="Perceptron Matrix"):
                         num_epochs=int(self.Entry2.get())
                         learning_rt=float(self.Entry2_3.get())
                         p=perceptron_matrix.weight_matrix(np.matrix(self.final_x),np.matrix(self.final_y),learning_rt,num_epochs,self.last_weights[0])
                         predicted_vals=p.ret_predicted_vals(np.matrix(self.test_x),np.matrix(self.test_y))
                         self.predicted_vals=np.full((len(self.test_x),len(self.final_y[0])),predicted_vals).T
    
                         print(self.predicted_vals)
                         test_error=p.ret_test_error()
                         print(test_error)
                         sum2=[]
                         for i in range(len(test_error)):
                            sum2.append(np.array(test_error[i]).sum()/len(test_error[i]))
                         print(sum2)
                         self.test_error=np.array(sum2).sum()/len(sum2)
                        
                        
                        
                        
                  
                    self.Entry3_5.configure(state="normal")   
                    self.Entry3_5.delete(0,'end')
                    self.Entry3_5.insert(0,self.test_error)
                    self.Entry3_5.configure(state="disabled")
                

        self.Button8 = tk.Button(self.Labelframe3)
        self.Button8.place(relx=0.443, rely=0.737, height=24, width=427
                , bordermode='ignore')
        self.Button8.configure(activebackground="#ececec")
        self.Button8.configure(activeforeground="#000000")
        self.Button8.configure(background="#d9d9d9")
        self.Button8.configure(disabledforeground="#a3a3a3")
        self.Button8.configure(foreground="#000000")
        self.Button8.configure(highlightbackground="#d9d9d9")
        self.Button8.configure(highlightcolor="black")
        self.Button8.configure(pady="0")
        self.Button8.configure(text='''Testing The Model''')
        self.Button8.configure(width=427)
        self.Button8.configure(command=test_model)
        """................................................................"""
        """--------------------------event--------------------------------"""
        def show_actual():
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                if(self.test_type.get()=="Kfold" or self.test_type.get()=="LOO"):
                    #for k in range(len(self.test_y)):
                        title='Actual Values for testing of each test'
                        table.Main(self.test_y,title)
                else:
                    table.Main(self.test_y,'Actual Values')
                
        def show_predicted():
            if(self.active_processing==0):
                messagebox.showwarning("Warning!!","Preprocess Data")
            else:
                if(self.test_type.get()=="Kfold" or self.test_type.get()=="LOO"):
                    #for k in range(len(self.predicted_vals)):   
                        #test=np.full((len(self.predicted_vals[k]),1),self.predicted_vals[k])
                        title='Predicted Values for testing of each test '
                        table.Main(self.predicted_vals,title)
                else:
                    table.Main(np.array(self.predicted_vals).T,'Predicted Values')



        self.Button6 = tk.Button(self.Labelframe3)
        self.Button6.place(relx=0.443, rely=0.316, height=24, width=207
                , bordermode='ignore')
        self.Button6.configure(activebackground="#ececec")
        self.Button6.configure(activeforeground="#000000")
        self.Button6.configure(background="#d9d9d9")
        self.Button6.configure(disabledforeground="#a3a3a3")
        self.Button6.configure(foreground="#000000")
        self.Button6.configure(highlightbackground="#d9d9d9")
        self.Button6.configure(highlightcolor="black")
        self.Button6.configure(pady="0")
        self.Button6.configure(text='''Actual Values''')
        self.Button6.configure(width=207)
        self.Button6.configure(command=show_actual)

        self.Button7 = tk.Button(self.Labelframe3)
        self.Button7.place(relx=0.747, rely=0.316, height=24, width=187
                , bordermode='ignore')
        self.Button7.configure(activebackground="#ececec")
        self.Button7.configure(activeforeground="#000000")
        self.Button7.configure(background="#d9d9d9")
        self.Button7.configure(disabledforeground="#a3a3a3")
        self.Button7.configure(foreground="#000000")
        self.Button7.configure(highlightbackground="#d9d9d9")
        self.Button7.configure(highlightcolor="black")
        self.Button7.configure(pady="0")
        self.Button7.configure(text='''Predicted Values''')
        self.Button7.configure(width=187)
        self.Button7.configure(command=show_predicted)


       








# The following code is added to facilitate the Scrolled widgets you specified.
class AutoScroll(object):
    '''Configure the scrollbars for a widget.'''

    def __init__(self, master):
        #  Rozen. Added the try-except clauses so that this class
        #  could be used for scrolled entry widget for which vertical
        #  scrolling is not supported. 5/7/14.
        try:
            vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
        except:
            pass
        hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)

        #self.configure(yscrollcommand=_autoscroll(vsb),
        #    xscrollcommand=_autoscroll(hsb))
        try:
            self.configure(yscrollcommand=self._autoscroll(vsb))
        except:
            pass
        self.configure(xscrollcommand=self._autoscroll(hsb))

        self.grid(column=0, row=0, sticky='nsew')
        try:
            vsb.grid(column=1, row=0, sticky='ns')
        except:
            pass
        hsb.grid(column=0, row=1, sticky='ew')

        master.grid_columnconfigure(0, weight=1)
        master.grid_rowconfigure(0, weight=1)

        # Copy geometry methods of master  (taken from ScrolledText.py)
        if py3:
            methods = tk.Pack.__dict__.keys() | tk.Grid.__dict__.keys() \
                  | tk.Place.__dict__.keys()
        else:
            methods = tk.Pack.__dict__.keys() + tk.Grid.__dict__.keys() \
                  + tk.Place.__dict__.keys()

        for meth in methods:
            if meth[0] != '_' and meth not in ('config', 'configure'):
                setattr(self, meth, getattr(master, meth))

    @staticmethod
    def _autoscroll(sbar):
        '''Hide and show scrollbar as needed.'''
        def wrapped(first, last):
            first, last = float(first), float(last)
            if first <= 0 and last >= 1:
                sbar.grid_remove()
            else:
                sbar.grid()
            sbar.set(first, last)
        return wrapped

    def __str__(self):
        return str(self.master)

def _create_container(func):
    '''Creates a ttk Frame with a given master, and use this new frame to
    place the scrollbars and the widget.'''
    def wrapped(cls, master, **kw):
        container = ttk.Frame(master)
        container.bind('<Enter>', lambda e: _bound_to_mousewheel(e, container))
        container.bind('<Leave>', lambda e: _unbound_to_mousewheel(e, container))
        return func(cls, container, **kw)
    return wrapped

class ScrolledListBox(AutoScroll, tk.Listbox):
    '''A standard Tkinter Text widget with scrollbars that will
    automatically show/hide as needed.'''
    @_create_container
    def __init__(self, master, **kw):
        tk.Listbox.__init__(self, master, **kw)
        AutoScroll.__init__(self, master)

import platform
def _bound_to_mousewheel(event, widget):
    child = widget.winfo_children()[0]
    if platform.system() == 'Windows' or platform.system() == 'Darwin':
        child.bind_all('<MouseWheel>', lambda e: _on_mousewheel(e, child))
        child.bind_all('<Shift-MouseWheel>', lambda e: _on_shiftmouse(e, child))
    else:
        child.bind_all('<Button-4>', lambda e: _on_mousewheel(e, child))
        child.bind_all('<Button-5>', lambda e: _on_mousewheel(e, child))
        child.bind_all('<Shift-Button-4>', lambda e: _on_shiftmouse(e, child))
        child.bind_all('<Shift-Button-5>', lambda e: _on_shiftmouse(e, child))

def _unbound_to_mousewheel(event, widget):
    if platform.system() == 'Windows' or platform.system() == 'Darwin':
        widget.unbind_all('<MouseWheel>')
        widget.unbind_all('<Shift-MouseWheel>')
    else:
        widget.unbind_all('<Button-4>')
        widget.unbind_all('<Button-5>')
        widget.unbind_all('<Shift-Button-4>')
        widget.unbind_all('<Shift-Button-5>')

def _on_mousewheel(event, widget):
    if platform.system() == 'Windows':
        widget.yview_scroll(-1*int(event.delta/120),'units')
    elif platform.system() == 'Darwin':
        widget.yview_scroll(-1*int(event.delta),'units')
    else:
        if event.num == 4:
            widget.yview_scroll(-1, 'units')
        elif event.num == 5:
            widget.yview_scroll(1, 'units')

def _on_shiftmouse(event, widget):
    if platform.system() == 'Windows':
        widget.xview_scroll(-1*int(event.delta/120), 'units')
    elif platform.system() == 'Darwin':
        widget.xview_scroll(-1*int(event.delta), 'units')
    else:
        if event.num == 4:
            widget.xview_scroll(-1, 'units')
        elif event.num == 5:
            widget.xview_scroll(1, 'units')

if __name__ == '__main__':
    vp_start_gui()





