Пример #1
0
class PingPong:
    __game = False
    i1 = 0
    i2 = 0
    __x = 0.0
    __y = 0.0
    __w = 0.0
    __h = 0.0
    __ball = None
    __gamer1 = None
    __gamer2 = None
    NNCTR = None
    gui = None
    game_gui = None
    win = None
    space = None
    inp = None
    outp = None
    network = None
    test = None
    pp_settings = None

    def __init__(self, x=0.0, y=0.0, w=480, h=768, ball=None, gamer1=None, gamer2=None, interval=0.02):
        self.__x = x
        self.__y = y
        self.__w = w
        self.__h = h
        self.interval = interval

        self.NNCTR = NNCTR()
        self.gui = GUI(self)
        self.game_gui = PingPongGUI()

        self.pp_settings = self.gui.new_window("Ping Pong Settings", 70.0, 100.0, 240, 350)
        self.win = self.gui.new_window("Ping Pong", x, y, w, h)

        self.pp_settings.iconbitmap('./favicon/game.ico')
        self.win.iconbitmap('./favicon/game.ico')

        self.space = self.game_gui.new_gamespace(self.win, x=x, y=y, w=w, h=h)

        self.__ball = self.create_ball(ball)
        self.__gamer1 = self.create_gamer(gamer1, id=1)
        self.__gamer2 = self.create_gamer(gamer2, id=2)

        self.win.bind('<Key>', self.key_press)
        self.win.bind('<KeyRelease>', self.key_release)

        frame = self.gui.frame(self.pp_settings, x=0, y=0, w=240, h=360, bind=None, bg="#77AA77")
        frame_control = self.gui.frame(frame, x=10, y=0, w=220, h=70, bind=None, bg="#669966")
        frame_settings = self.gui.frame(frame, x=10, y=80, w=220, h=270, bind=None, bg="#669966")

        self.gui.text(frame_control, x=60, y=5, w=100, h=20, text=self.NNCTR.localization("game_control_title"),
                      bg="#669966", font='Arial 14')

        self.gui.button(frame_control, x=20, y=30, w=80, h=30, text=self.NNCTR.localization("game_start_button"),
                        bind=("<Button-1>", lambda event:  self.start()))

        self.gui.button(frame_control, x=120, y=30, w=80, h=30, text=self.NNCTR.localization("game_stop_button"),
                        bind=("<Button-1>", lambda event:  self.stop()))

        self.gui.text(frame_settings, x=60, y=0, w=100, h=20, text=self.NNCTR.localization("game_settings_title"),
                      bg="#669966", font='Arial 14')

        self.gui.text(frame_settings, x=10, y=40, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_ball"),
                      bg="#669966")

        x_speed_ball = self.gui.input(frame_settings, x=120, y=40, w=40, h=20)

        self.gui.set_text_input(x_speed_ball, "")

        y_speed_ball = self.gui.input(frame_settings, x=170, y=40, w=40, h=20)

        self.gui.set_text_input(y_speed_ball, "")

        self.gui.text(frame_settings, x=10, y=70, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p1"),
                      bg="#669966")

        x_speed_g1 = self.gui.input(frame_settings, x=170, y=70, w=40, h=20)

        self.gui.set_text_input(x_speed_g1, "")

        self.gui.text(frame_settings, x=10, y=100, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p2"),
                      bg="#669966")

        x_speed_g2 = self.gui.input(frame_settings, x=170, y=100, w=40, h=20)

        self.gui.set_text_input(x_speed_g2, "")

        self.gui.text(frame_settings, x=10, y=130, w=110, h=20, text=self.NNCTR.localization("game_settings_radius"),
                      bg="#669966")

        r_ball = self.gui.input(frame_settings, x=170, y=130, w=40, h=20)

        self.gui.set_text_input(r_ball, "10")

        self.gui.text(frame_settings, x=10, y=160, w=130, h=20, text=self.NNCTR.localization("game_settings_collision"),
                      bg="#669966")

        k_ball = self.gui.input(frame_settings, x=170, y=160, w=40, h=20)

        self.gui.set_text_input(k_ball, "1.2")

        self.gui.text(frame_settings, x=10, y=190, w=110, h=20, text=self.NNCTR.localization("game_settings_cords"),
                      bg="#669966")

        x_ball = self.gui.input(frame_settings, x=120, y=190, w=40, h=20)

        self.gui.set_text_input(x_ball, "")

        y_ball = self.gui.input(frame_settings, x=170, y=190, w=40, h=20)

        self.gui.set_text_input(y_ball, "")

        self.gui.button(frame_settings, x=20, y=230, w=80, h=30, text=self.NNCTR.localization("game_get_button"),
                        bind=("<Button-1>", lambda event: self.get_settings(x_speed_ball, y_speed_ball,
                                                                            x_speed_g1, x_speed_g2, x_ball, y_ball)))

        self.gui.button(frame_settings, x=120, y=230, w=80, h=30, text=self.NNCTR.localization("game_set_button"),
                        bind=("<Button-1>", lambda event:
                        self.update_settings(x_speed_ball.get(),
                                             y_speed_ball.get(), x_speed_g1.get(),
                                             x_speed_g2.get(), r_ball.get(), k_ball.get(),
                                             x_ball.get(), y_ball.get())))

    def __getattr__(self, item):
        if item == "x":
            return self.__x
        elif item == "y":
            return self.__y
        elif item == "w":
            return self.__w
        elif item == "h":
            return self.__h
        elif item == "g1":
            return self.__gamer1
        elif item == "g2":
            return self.__gamer2
        elif item == "g":
            return self.__game

    def update_settings(self, x_speed_ball, y_speed_ball, x_speed_g1, x_speed_g2, r_ball, k_ball, x_ball, y_ball):
        try:
            x_speed_ball = float(x_speed_ball)
            y_speed_ball = float(y_speed_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_ball"))
            return None

        try:
            x_speed_g1 = float(x_speed_g1)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_of_p1"))
            return None

        try:
            x_speed_g2 = float(x_speed_g2)
        except ValueError:
            self.gui.message("warning", "Warning!",self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_of_p2"))
            return None

        try:
            r_ball = float(r_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_radius"))
            return None

        try:
            k_ball = float(k_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_collision"))
            return None

        try:
            x_ball = float(x_ball)
            y_ball = float(y_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_cords"))
            return None

        self.__ball.set_speed(x_speed_ball, y_speed_ball)
        self.__gamer1.set_speed(x_speed_g1)
        self.__gamer2.set_speed(x_speed_g2)
        self.__ball.set_r(float(r_ball), float(k_ball))
        self.__ball.set_cords(x_ball, y_ball)
        self.__game = True
        self.paint()
        self.__game = False

    def get_settings(self, x_speed_ball, y_speed_ball, x_speed_g1, x_speed_g2, x_ball, y_ball):
        x, y = self.__ball.get_speed()
        self.gui.set_text_input(x_speed_ball, round(x, 2))
        self.gui.set_text_input(y_speed_ball, round(y, 2))
        x = self.__gamer1.get_speed()
        self.gui.set_text_input(x_speed_g1, round(x, 2))
        x = self.__gamer2.get_speed()
        self.gui.set_text_input(x_speed_g2, round(x, 2))
        x, y = self.__ball.get_cords()
        self.gui.set_text_input(x_ball, round(x, 2))
        self.gui.set_text_input(y_ball, round(y, 2))

    def create_ball(self, ball):
        pass
        return Ball(self, ball)

    def create_gamer(self, gamer, id):
        pass
        return Gamer(self, gamer, id=id)

    def game_loop(self):
        self.__game = True
        while self.__game:
            self.__ball.move()
            self.__gamer1.move()
            self.__gamer2.move()
            sleep(self.interval)
            self.space.update()

    def stop_all(self):
        self.__game = False
        self.NNCTR.stop_all()

    def stop(self):
        self.__game = False

    def start(self):
        if self.NNCTR.network is None:
            self.gui.message("warning", "Warning!", "Network is not loaded!")
            return None
        if not self.__game:
            self.game_loop()

    def paint(self):
        if self.__game:
            self.__ball.paint()
            self.__gamer1.paint()
            self.__gamer2.paint()

    def key_press(self, event):
        if event.keycode == (65 if os.name == "nt" else 38):
            self.__gamer1.mul_speed(-1)
        elif event.keycode == (68 if os.name == "nt" else 40):
            self.__gamer1.mul_speed(1)
        elif event.keycode == (37 if os.name == "nt" else 113):
            self.__gamer2.mul_speed(-1)
        elif event.keycode == (39 if os.name == "nt" else 114):
            self.__gamer2.mul_speed(1)
        elif event.keycode == (32 if os.name == "nt" else 65):
            self.stop() if self.__game else self.start()

    def key_release(self, event):
        if event.keycode == (65 if os.name == "nt" else 38) or event.keycode == (68 if os.name == "nt" else 40):
            self.__gamer1.mul_speed(0)
        elif event.keycode == (37 if os.name == "nt" else 113) or event.keycode == (39 if os.name == "nt" else 114):
            self.__gamer2.mul_speed(0)
Пример #2
0
class NNConstructor:

    gui = None
    win = None
    winNN = None
    t_stop_train = {"next": False, "_next": False}
    __train = None
    samples = []
    test_samples = []
    p_samples = []
    inp = None
    outp = None
    gui_samples = None
    gui_samples_list = None
    gui_network_graph = None
    gui_graph = None
    frame_neurons_graph = None
    gui_graph_colors = None
    __visible_edit_samples = None
    network = None
    analysis_process = None

    def __init__(self, x=450.0, y=0.0):
        self.gui = GUI(self)
        self.winNN = self.gui.new_window("Constructor of Neural Networks", x, y, 960, h=720)
        self.winNN.iconbitmap('./favicon/constructor.ico')
        try:
            words = json.load(open("./language.json", "r"))
        except IOError:
            words = None
        except ValueError:
            words = None
        self.localization = Localization(words).localization


        frame = self.gui.frame(self.winNN, x=0, y=0, w=1046, h=768, bind=None, bg="#77AA77")

        frame_excel = self.gui.frame(frame, x=10, y=0, w=200, h=350, bind=None, bg="#669966")

        frame_newnet = self.gui.frame(frame, x=10, y=360, w=200, h=170, bind=None, bg="#669966")
        frame_train = self.gui.frame(frame, x=10, y=540, w=200, h=170, bind=None, bg="#669966")
        frame_samples = self.gui.frame(frame, x=220, y=0, w=300, h=370, bind=None, bg="#669966")
        self.frame_neurons_graph = self.gui.frame(frame, x=530, y=0, w=420, h=370, bind=None, bg="#669966")
        frame_neurons_control = self.gui.frame(frame, x=615, y=380, w=335, h=360, bind=None, bg="#669966")
        self.frame_train_graph = self.gui.frame(frame, x=220, y=380, w=385, h=350, bind=None, bg="#669966")

        self.gui_network_graph = self.gui.canvas(self.frame_neurons_graph, x=10, y=10, w=400, h=350, bg="#FFFFFF")
        self.new_graph()
        self.gui.button(self.frame_train_graph, x=10, y=310, w=367, h=25, text=self.localization("graph_button"),
                        bind=("<Button-1>",
                              lambda event:
                              self.new_graph()))

        self.new_network_graph(self.frame_neurons_graph)

        self.gui.text(frame_samples, x=50, y=5, w=200, h=20, text=self.localization("sample_editor_title"),
                      bg="#669966", font='Arial 13')

        self.gui_samples = self.gui.textarea(frame_samples, x=10, y=30, w=280, h=260)
        self.gui_samples_list = self.gui.listbox(frame_samples, x=10, y=30, w=280, h=260, bind=("<<ListboxSelect>>",
                                                                                                lambda event:
                                                                                                self.refresh_st_samples(
                                                                                                )))
        self.gui.button(frame_samples, x=10, y=300, w=280, h=30, text=self.localization("load_test_button"),
                        bind=("<Button-1>",
                              lambda event:
                              self.load_tsamples()))

        self.gui.button(frame_samples, x=10, y=335, w=67, h=30, text=self.localization("edit_samples_button"),
                        bind=("<Button-1>",
                              lambda event:  self.edit_samples()))

        self.gui.button(frame_samples, x=81, y=335, w=67, h=30, text=self.localization("clear_samples_button"),
                        bind=("<Button-1>",
                              lambda event: self.clear_samples()))

        self.gui.button(frame_samples, x=152, y=335, w=67, h=30, text=self.localization("load_samples_button"),
                        bind=("<Button-1>",
                              lambda event:  self.load_samples()))

        self.gui.button(frame_samples, x=223, y=335, w=67, h=30, text=self.localization("save_samples_button"),
                        bind=("<Button-1>",
                              lambda event:  self.save_samples()))

        self.gui.text(frame_newnet, x=10, y=0, w=180, h=20, text=self.localization("menu_net_title"),
                      bg="#669966", font='Arial 13')

        self.gui.text(frame_newnet, x=10, y=30, w=50, h=20, text=self.localization("menu_net_layers"), bg="#669966")
        layers = self.gui.input(frame_newnet, x=80, y=30, w=110, h=20)
        self.gui.set_text_input(layers, "")

        bias = self.gui.Tkinter.IntVar()

        self.gui.checkbox(frame_newnet, x=10, y=60, w=180, h=20, variable=bias,
                          text=self.localization("check_box_bias"))

        self.gui.button(frame_newnet, x=10, y=130, w=85, h=30, text=self.localization("load_network_button"),
                        bind=("<Button-1>",
                              lambda event: self.load_network()))

        self.gui.button(frame_newnet, x=105, y=130, w=85, h=30, text=self.localization("save_network_button"),
                        bind=("<Button-1>", lambda event: self.save_network()))

        self.gui.button(frame_newnet, x=10, y=90, w=180, h=30, text=self.localization("new_network_button"),
                        bind=("<Button-1>", lambda event: self.new_network(layers.get(), bias.get())))

        self.gui.text(frame_train, x=10, y=0, w=180, h=20, text=self.localization("menu_train_title"),
                      bg="#669966", font='Arial 13')

        self.gui.text(frame_train, x=10, y=25, w=110, h=20, text=self.localization("menu_train_speed"), bg="#669966")

        speed = self.gui.input(frame_train, x=120, y=25, w=70, h=20)

        self.gui.set_text_input(speed, "")

        self.gui.text(frame_train, x=10, y=50, w=110, h=20, text=self.localization("menu_train_iterations"),
                      bg="#669966")

        iterations = self.gui.input(frame_train, x=120, y=50, w=70, h=20)

        self.gui.set_text_input(iterations, "")

        checkbox = self.gui.Tkinter.IntVar()
        self.gui.checkbox(frame_train, x=10, y=75, w=180, h=20, variable=checkbox,
                          text=self.localization("check_box_change_speed"))

        self.gui.button(frame_train, x=10, y=100, w=180, h=30, text=self.localization("train_button"),
                        bind=("<Button-1>", lambda event: self.train_network(
                            iterations.get(),
                            speed.get(),
                            checkbox.get()
                        )))

        self.gui.button(frame_train, x=10, y=132, w=180, h=30, text=self.localization("stop_train_button"),
                        bind=("<Button-1>", lambda event: self.stop_train()))

        self.gui.text(frame_neurons_control, x=5, y=20, w=240, h=20, text=self.localization("status_sample_error"),
                      bg="#669966", font='Arial 12')

        self.smErr = self.gui.input(frame_neurons_control, x=250, y=20, w=70, h=20)

        self.gui.text(frame_neurons_control, x=5, y=60, w=240, h=20, text=self.localization("status_max_error"),
                      bg="#669966", font='Arial 12')

        self.mxErr = self.gui.input(frame_neurons_control, x=250, y=60, w=70, h=20)

        self.gui.text(frame_neurons_control, x=5, y=100, w=240, h=20, text=self.localization("status_av_error"),
                      bg="#669966", font='Arial 12')

        self.avErr = self.gui.input(frame_neurons_control, x=250, y=100, w=70, h=20)

        self.gui.text(frame_neurons_control, x=5, y=140, w=240, h=20, text=self.localization("status_max_test_error"),
                      bg="#669966", font='Arial 12')

        self.tmxErr = self.gui.input(frame_neurons_control, x=250, y=140, w=70, h=20)

        self.gui.text(frame_neurons_control, x=5, y=180, w=240, h=20, text=self.localization("status_av_test_error"),
                      bg="#669966", font='Arial 12')

        self.ttErr = self.gui.input(frame_neurons_control, x=250, y=180, w=70, h=20)

        self.gui.button(frame_neurons_control, x=30, y=250, w=80, h=30, text=self.localization("module_1_button"),
                        bind=("<Button-1>", lambda event: self.load_module("_module_1")))

        self.gui.button(frame_neurons_control, x=120, y=250, w=80, h=30, text=self.localization("module_2_button"),
                        bind=("<Button-1>", lambda event: self.load_module("_module_2")))

        self.gui.button(frame_neurons_control, x=210, y=250, w=80, h=30, text=self.localization("module_3_button"),
                        bind=("<Button-1>", lambda event: self.load_module("_module_3")))

        self.gui.text(frame_excel, x=50, y=5, w=100, h=20, text=self.localization("analysis_title"),
                      bg="#669966", font='Arial 13')

        self.gui.text(frame_excel, x=5, y=35, w=120, h=20, text=self.localization("analysis_name_of_file"),
                      bg="#669966")

        book_name = self.gui.input(frame_excel, x=120, y=35, w=70, h=20)

        self.gui.text(frame_excel, x=5, y=70, w=120, h=20, text=self.localization("analysis_layers"), bg="#669966")

        layers_pat = self.gui.input(frame_excel, x=120, y=70, w=70, h=20)

        self.gui.text(frame_excel, x=5, y=100, w=120, h=20, text=self.localization("analysis_ch_speed"), bg="#669966")

        ch_layers = self.gui.input(frame_excel, x=120, y=100, w=70, h=20)

        self.gui.text(frame_excel, x=5, y=135, w=120, h=20, text=self.localization("analysis_iterations"), bg="#669966")

        iter = self.gui.input(frame_excel, x=120, y=135, w=70, h=20)

        self.gui.text(frame_excel, x=5, y=170, w=120, h=20, text=self.localization("analysis_data_size"), bg="#669966")

        data = self.gui.input(frame_excel, x=120, y=170, w=70, h=20)

        self.gui.text(frame_excel, x=5, y=205, w=120, h=20, text=self.localization("analysis_speed"), bg="#669966")

        speed_cof = self.gui.input(frame_excel, x=120, y=205, w=70, h=20)

        checkbox = self.gui.Tkinter.IntVar()
        self.gui.checkbox(frame_excel, x=10, y=240, w=180, h=20, variable=checkbox,
                          text=self.localization("check_box_ch_speed"))

        progressbar = self.gui.progressbar(frame_excel, x=10, y=310, w=180, h=30,
                                           orient='horizontal', mode='indeterminate')

        self.gui.button(frame_excel, x=10, y=270, w=85, h=30, text=self.localization("start_analysis_button"),
                        bind=("<Button-1>", lambda event: self.analysis_start(book_name.get(),
                                                                              layers_pat.get(),
                                                                              ch_layers.get(),
                                                                              iter.get(),
                                                                              data.get(),
                                                                              speed_cof.get(),
                                                                              checkbox.get(),
                                                                              progressbar)))

        self.gui.button(frame_excel, x=105, y=270, w=85, h=30, text=self.localization("stop_analysis_button"),
                        bind=("<Button-1>", lambda event: self.analysis_stop(progressbar)))

    def open(self):
        self.gui.mainloop()

    def close(self):
        self.gui.close()

    def stop_all(self):
        self.stop_train()
        time.sleep(0.5)

    def load_module(self, name):
        try:
            module = __import__(name)
            if "main" in dir(module):
                module.main(self)
            else:
                self.gui.message("warning", "Warning!", self.localization("fminf_error"))
        except ImportError:
            self.gui.message("error", "Error!", self.localization("minf_error") % name)

    def analysis_start(self, fn, layers, ch_layers, iter, data, speed, ch_speed, pb):

        def parse_str(s):
            arr_s = s.replace(" ", "").split(",")
            if arr_s[0] == '':
                arr_s = []
            arr = []
            if len(arr_s) == 1:
                arr_s = arr_s[0].split(":")
            elif len(arr_s) > 1:
                arr = [float(i) for i in arr_s]
                arr_s = []

            if len(arr_s) == 1:
                arr = [float(arr_s[0])]
            elif len(arr_s) == 3:
                i = float(arr_s[0])
                while i <= float(arr_s[1]):
                    arr.append(i)
                    i += float(arr_s[2])

            return arr

        if len(fn) < 1:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("analysis_name_of_file")))
            return None

        for i in ['\\', '/', ':', '*', '.', '?', '"', '<', '>', '|']:
            if i in fn:
                self.gui.message("warning", "Warning!", self.localization("fcncc_error"))
                return None

        layers = layers.replace(" ", "").split(",")
        if layers[0] == '':
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("analysis_layers")))
            return None

        try:
            ch_layers = parse_str(ch_layers)
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % self.localization("analysis_ch_speed"))
            return None

        try:
            iter = parse_str(iter)
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("analysis_iterations")))
            return None

        try:
            data = parse_str(data)
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("analysis_data_size")))
            return None

        try:
            speed = parse_str(speed)
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("analysis_speed")))
            return None

        samples = self.samples[:]
        test_samples = self.test_samples[:] if len(self.test_samples) > 0 else samples

        len_samples = len(samples)
        len_test_samples = len(test_samples)

        if len_samples < 1:
            self.gui.message("warning", "Warning!", self.localization("sinl_error"))
            return None

        def main():

            wb = openpyxl.Workbook()
            test_wb = openpyxl.Workbook() if not (samples == test_samples) else None

            self.analysis_process = True
            pb.start(5)

            column = 1
            for i in ch_layers:
                row = 1
                for j in iter:
                    for k in data:
                        sample = samples[:int(k)] if (int(k) > 0) and (int(k) < len_samples) else samples
                        test_sample = test_samples[:int(k)] \
                            if (int(k) > 0) and (int(k) < len_test_samples) else test_samples

                        for z in speed:
                            if self.analysis_process is not True:
                                return None
                            nn = analysis.NN([int(i) if t == 'x' else int(t) for t in layers])

                            nn.train(sample, int(j), z, z/100 if ch_speed else z)
                            err = nn.get_error(sample, test_sample)
                            del nn
                            sheet = "col_data "+str(int(k))+" speed "+str(z)
                            if not (sheet in wb):
                                wb.create_sheet(1, sheet)
                            wb[sheet].cell(row=row, column=column).value = err[0]
                            if test_wb is not None:
                                if not (sheet in test_wb):
                                    test_wb.create_sheet(1, sheet)
                                test_wb[sheet].cell(row=row, column=column).value = err[1]

                        row += 1
                    column += 1

                pb.stop()

            wb.save("./sheets/%s.xlsx" % fn)
            if test_wb is not None:
                test_wb.save("./sheets/%s_test.xlsx" % fn)

        threading.Thread(target=main).start()

    def analysis_stop(self, pb):
        self.analysis_process = False
        pb.stop()

    def new_network(self, layers, bias=0):
        try:
            arr = layers.split(",")
            if len(arr) < 1:
                raise ValueError
            layers = []
            for i in arr:
                if int(i) > 0:
                    layers.append(int(i))
                else:
                    raise ValueError
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error") % (self.localization("menu_net_layers")))
            return None

        funcs = [[f_activation, df_activation]]*len(layers)
        funcs[0] = None

        self.network = Net(layers, funcs, bias)
        self.new_network_graph(self.frame_neurons_graph)
        self.refresh_st_samples()

    def train_network(self, iterations, speed, dspeed):
        if self.__train is not None and self.__train.isAlive():
            self.gui.message("warning", "Warning!", self.localization("ptsb_error"))
            return None

        if self.network is None:
            self.gui.message("warning", "Warning!", self.localization("ninl_error"))
            return None

        if len(self.samples) < 1:
            self.gui.message("warning", "Warning!", self.localization("sinl_error"))
            return None

        if not self.network.check_sample(self.samples):
            self.gui.message("warning", "Warning!", self.localization("sinl_error"))
            return None

        if not self.network.check_sample(self.test_samples) and self.test_samples != [] and self.test_samples != self.samples:
            self.gui.message("warning", "Warning!", self.localization("tsinl_error"))
            return None

        try:
            iterations = int(iterations)
            if iterations < 1:
                raise ValueError
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("menu_train_iterations")))
            return None

        try:
            speed = float(speed)
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("ii_error")
                             % (self.localization("menu_train_speed")))
            return None

        self.graph[3] = iterations
        if len(self.gui_graph_colors) == 0:
            self.new_graph()
        color = self.gui_graph_colors.pop()
        start = time.time()

        self.__train = threading.Thread(target=lambda: self.network.train(self.samples, iterations, speed, dspeed,
                                                                          func=lambda i, error, terror:
                                                                          self.picture_graph(
                                                                              i=i,
                                                                              error=error,
                                                                              terror=terror,
                                                                              iterations=iterations,
                                                                              color=color
                                                                          ),
                                                                          test=self.test_samples,
                                                                          stop=self.t_stop_train,
                                                                          callback=lambda: self.new_network_graph(
                                                                              self.frame_neurons_graph
                                                                          ) or self.refresh_st_samples(st=True)))
        self.__train.start()

    def stop_train(self):
        self.t_stop_train["_next"] = True
        self.t_stop_train["next"] = True

    def save_network(self):
        if self.network is None:
            return None
        self.set_samples()
        f = self.gui.save_file(self.winNN, filetypes=[("JSON file", ".json")])
        if not f:
            return None
        try:
            json.dump([self.network.n, self.network.w], f)
        except ValueError:
            self.gui.message("error", "Error!", self.localization("io_error"))
            return None

    def load_network(self):
        f = self.gui.open_file(self.winNN, filetypes=[("JSON file", ".json")])
        if not f:
            return None

        def check_network(ntw):
            if len(ntw) < 2 or type(ntw) is not list or len(ntw[0]) != len(ntw[1])+1:
                return False
            for i in range(len(ntw[0])-1):
                if ntw[0][i] != len(ntw[1][i]):
                    return False
                for j in range(len(ntw[1][i])):
                    if ntw[0][i+1] != len(ntw[1][i][j]):
                        return False

            return True

        try:
            network = json.load(f)
            if not check_network(network):
                raise ValueError
        except ValueError:
            self.gui.message("error", "Error!", self.localization("tinn_error"))
            return None
        funcs = [[f_activation, df_activation]]*len(network[0])
        funcs[0] = None
        self.network = Net(network[0], funcs)
        self.network.w = network[1]
        self.new_network_graph(self.frame_neurons_graph)
        self.refresh_st_samples()

    def new_graph(self):
        if self.gui_graph is not None:
            self.gui_graph.space.destroy()
            del self.gui_graph

        self.gui_graph = self.gui.canvas(self.frame_train_graph, x=10, y=10, w=365, h=300, bg="#99BB99")
        self.graph = [None, (40.0, 270.0, 300.0, 260.0, 0.2), None, 1]
        self.gui_graph.line(int(self.graph[1][0]),
                            int(self.graph[1][1]),
                            int(self.graph[1][0]),
                            int(self.graph[1][1]-self.graph[1][3]),
                            arrow=self.gui.Tkinter.LAST)
        self.gui_graph.line(int(self.graph[1][0]),
                            int(self.graph[1][1]),
                            int(self.graph[1][0]+self.graph[1][2]),
                            int(self.graph[1][1]),
                            arrow=self.gui.Tkinter.LAST)

        self.gui_graph_colors = ["red", "blue", "black", "orange", "purple"]
        text_cords = self.gui_graph.text(x=0, y=0, text="")
        self.gui_graph.space.bind("<B1-Motion>", lambda event: self.gui_graph.space.coords(text_cords, event.x-(
            -40 if event.x < self.graph[1][2]/2 else 40), event.y+7) or self.gui_graph.space.itemconfigure(
            text_cords, text=str(int((event.x-self.graph[1][0])/self.graph[1][2]*self.graph[3]))+" / "+str(
                round((self.graph[1][1]-event.y)/self.graph[1][3]*self.graph[1][4], 2))))

        self.gui_graph.text(x=self.graph[1][0]-20, y=self.graph[1][1]-self.graph[1][3]+10, text="Q")
        self.gui_graph.text(x=self.graph[1][0]+self.graph[1][2]-20, y=self.graph[1][1]+20, text="N")

        self.gui_graph.space.bind("<B1-ButtonRelease>", lambda e: self.gui_graph.space.coords(text_cords, -10, -10))
        cx, cy = 5, 5
        for i in range(1, cy):
            self.gui_graph.line(self.graph[1][0]-5, self.graph[1][1]-self.graph[1][3]/cy*i,
                                self.graph[1][0]+5, self.graph[1][1]-self.graph[1][3]/cy*i,
                                fill="red")
        for i in range(1, cx):
            self.gui_graph.space.create_line(self.graph[1][0]+self.graph[1][2]/cx*i,
                                             self.graph[1][1]+5, self.graph[1][0]+self.graph[1][2]/cx*i,
                                             self.graph[1][1]-5, width=1, fill="red")

    def picture_graph(self, i=0, error=0, terror=0, iterations=0, color=None):

        if not color:
            return None
        if self.graph[0] is None or i < 5:
            self.graph[0] = (i*1.0, error)
            self.graph[2] = (i*1.0, terror)
        else:
            self.gui_graph.space.create_line(
                self.graph[1][0]+i*1.0/iterations*self.graph[1][2],
                self.graph[1][1]-error/self.graph[1][4]*self.graph[1][3],
                self.graph[1][0]+self.graph[0][0]/iterations*self.graph[1][2],
                self.graph[1][1]-self.graph[0][1]/self.graph[1][4]*self.graph[1][3],
                width=2, fill=color
            )
            if terror:
                self.gui_graph.space.create_oval(
                    self.graph[1][0]+i*1.0/iterations*self.graph[1][2]-2,
                    self.graph[1][1]-terror/self.graph[1][4]*self.graph[1][3]-2,
                    self.graph[1][0]+i*1.0/iterations*self.graph[1][2]+2,
                    self.graph[1][1]-terror/self.graph[1][4]*self.graph[1][3]+2,
                    fill=color
                )
            self.graph[0] = (i*1.0, error)
            self.graph[2] = (i*1.0, terror)

    def new_network_graph(self, frame_neurons_graph):
        if not self.network:
            return None
        if self.gui_network_graph is not None:
            self.gui_network_graph.space.destroy()
            del self.gui_network_graph
        self.gui_network_graph = self.gui.canvas(frame_neurons_graph, x=10, y=10, w=400, h=350, bg="#FFFFFF")
        n = self.network.n

        for i in range(len(n)-1):
            for j in range(n[i]):
                for k in range(n[i+1]):
                        self.gui_network_graph.line(400/(len(n)+1)*(i+1), 350/(n[i]+1)*(j+1),
                                                    400/(len(n)+1)*(i+2), 350/(n[i+1]+1)*(k+1),
                                                    fill="#%x%x%x" % (15/(1+math.exp(
                                                        -self.network.w[i][j][k]*5)), 0, 15/(
                                                        1+math.exp(self.network.w[i][j][k]*5))))
        for i in range(len(n)):
            for j in range(n[i]):
                    self.gui_network_graph.circle(400/(len(n)+1)*(i+1), 350/(n[i]+1)*(j+1), 4,
                                                  tags=("neurons", "neuron-%d-%d" % (i, j)), fill="green")

    def refresh_st_samples(self, st=False):
        if self.network is None or (self.__train is not None and self.__train.isAlive() and not st) \
                or len(self.samples) < 1:
            return None

        if not self.network.check_sample(self.samples):
            return None

        if not self.network.check_sample(self.test_samples):
            return None

        smp_cl = int(self.gui_samples_list.curselection()[0]) if len(self.gui_samples_list.curselection()) > 0 else 0
        err = 0.0

        if len(self.samples) > smp_cl:
            sample = self.samples[smp_cl]

            self.network.update(sample[0])

            for k in range(len(sample[1])):
                err += abs(sample[1][k]-self.network.a[-1][k])
            err /= len(sample[1])

            self.gui.set_text_input(self.smErr, str(round(err, 5)))

        averr = 0.0
        mxerr = 0.0
        for p in self.samples:
            self.network.update(p[0])
            _averr = 0.0
            for k in range(len(p[1])):
                _averr += abs(p[1][k]-self.network.a[-1][k])
            _averr /= len(p[1])
            averr += _averr
            if _averr > mxerr:
                mxerr = _averr
        averr /= len(self.samples)

        t_averr = 0.0
        t_mxerr = 0.0

        for p in self.test_samples:
            self.network.update(p[0])
            _averr = 0.0
            for k in range(len(p[1])):
                _averr += abs(p[1][k]-self.network.a[-1][k])
            _averr /= len(p[1])
            t_averr += _averr
            if _averr > t_mxerr:
                t_mxerr = _averr

        t_averr /= len(self.test_samples) or 1

        self.gui.set_text_input(self.avErr, str(round(averr, 5)))
        self.gui.set_text_input(self.mxErr, str(round(mxerr, 5)))
        self.gui.set_text_input(self.ttErr, str(round(t_averr, 5)))
        self.gui.set_text_input(self.tmxErr, str(round(t_mxerr, 5)))

        return (err, averr, mxerr, t_averr, t_mxerr)

    def edit_samples(self):
        if self.__visible_edit_samples is None:
            self.__visible_edit_samples = 30
            self.gui_samples_list.place(y=-500)
        else:
            try:
                if self.set_samples() is not False:
                    self.get_samples()
                    self.gui_samples_list.place(y=self.__visible_edit_samples)
                    self.__visible_edit_samples = None
                    self.gui_samples_list.focus()

            except IndexError:
                self.gui.message("warning", "Warning!", self.localization("tsinc_error"))
                return None

    def clear_samples(self):
        self.samples = []
        self.get_samples()
        self.refresh_st_samples()

    def get_samples(self):
        self.gui.set_text_textarea(self.gui_samples, data_samples(self.samples))
        samples, k = "", 0
        for i in self.samples:
            samples += "%2d:        " % k
            k += 1
            for j in i[0]:
                samples += "% .2f  " % round(j, 2)
            samples += "  ->  "
            for j in i[1]:
                samples += "% .2f  " % round(j, 2)
            samples += "\n"

        self.gui.set_text_listbox(self.gui_samples_list, samples)

    def set_samples(self):
        try:
            self.samples = parse_samples(self.gui.get_text(self.gui_samples))
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("tsinc_error"))
            return False
        self.refresh_st_samples()

    def load_samples(self):
        f = self.gui.open_file(self.winNN, filetypes=[("JSON file", ".json")])
        if not f:
            return 0
        try:
            self.samples = json.load(f)
            if type(self.samples) is not list:
                raise ValueError
            self.refresh_st_samples()
            self.get_samples()
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("tins_error"))
            return None
        except TypeError:
            self.gui.message("warning", "Warning!", self.localization("tins_error"))
            return None

    def load_tsamples(self):
        f = self.gui.open_file(self.winNN, filetypes=[("JSON file", ".json")])
        if not f:
            return 0
        try:
            self.test_samples = json.load(f)
            if type(self.test_samples) is not list:
                raise ValueError
            self.refresh_st_samples()
            self.get_samples()
        except ValueError:
            self.gui.message("warning", "Warning!", self.localization("tints_error"))
            return None
        except TypeError:
            self.gui.message("warning", "Warning!", self.localization("tints_error"))
            return None

    def save_samples(self):
        self.set_samples()
        f = self.gui.save_file(self.winNN, filetypes=[("JSON file", ".json")])
        if not f:
            return 0
        try:
            json.dump(self.samples, f)
        except ValueError:
            self.gui.message("error", "Error!", self.localization("io_error"))
            return None