예제 #1
0
 def buildTab(self):
     self.background_image = "images/gui_elements/tab_darkblue.png"
     with open("config/blocks.json") as json_data:
         blocks_config = json.load(json_data)
         for tab in blocks_config["tabs"]:
             #newTab = TabbedPanelItem(text = str(tab["id"]))
             block_type = str(tab["id"])[0]
             newTab = TabbedPanelItem()
             color_tab = []
             for color in tab["rgba"]:
                 color_tab.append(color)
             newTab.background_color = color_tab
             newLayout = StackLayout(spacing=10)
             for block in tab["blocks"]:
                 newBlock = Block(block_type, str(block["id"]),
                                  str(block["source"]))
                 newLayout.add_widget(newBlock)
             newTab.content = newLayout
             self.add_widget(newTab)
예제 #2
0
    def __init__(self, df, security_name, **kwargs):
        super(Application, self).__init__(**kwargs)
        self.orientation = 'vertical'
        tab_panel = TabbedPanel()
        tab_panel.do_default_tab = False
        tab_panel.background_color = (7 / 255, 0, 13 / 255, 1)
        tab_menu = TabbedPanelItem(text="Menu")
        tab_menu.background_color = (62 / 255, 142 / 255, 222 / 255, 1)
        tab_chart = TabbedPanelItem(text='Chart')
        tab_chart.background_color = (62 / 255, 142 / 255, 222 / 255, 1)
        tab_training = TabbedPanelItem(text='Training')
        tab_training.background_color = (62 / 255, 142 / 255, 222 / 255, 1)
        tab_validate = TabbedPanelItem(text='Validate')
        tab_validate.background_color = (62 / 255, 142 / 255, 222 / 255, 1)
        tab_future = TabbedPanelItem(text='Prediction')
        tab_future.background_color = (62 / 255, 142 / 255, 222 / 255, 1)

        tab_panel.add_widget(tab_menu)
        tab_panel.add_widget(tab_chart)
        tab_panel.add_widget(tab_training)
        tab_panel.add_widget(tab_validate)
        tab_panel.add_widget(tab_future)

        tab_menu.bind(on_press=lambda x: change_screen('menu'))

        chart_layout = FloatLayout()
        fig = plot_data(df, security_name.upper(), 30, 200)
        canvas = fig.canvas
        chart_layout.add_widget(canvas)
        tab_chart.add_widget(chart_layout)

        predict_frame = FloatLayout(opacity=1)
        predict_btn = Button(text='Run Prediction',
                             background_color=(62 / 255, 142 / 255, 222 / 255,
                                               1),
                             font_size=20,
                             pos_hint={
                                 "center_x": 0.5,
                                 "bottom": 0
                             },
                             size_hint=(0.3, 0.075))

        predict_btn.bind(on_press=lambda x: start_predict(df, security_name))
        predict_frame.add_widget(predict_btn)
        chart_layout.add_widget(predict_frame)

        def start_predict(df, security_name):
            que = queue.Queue()
            par_modeling = partial(modeling, security_name=security_name)
            process = threading.Thread(
                target=lambda q, arg1: q.put(par_modeling(arg1)),
                args=(que, df),
                daemon=True)
            process.start()

            Clock.schedule_once(
                lambda *args: tab_panel.switch_to(tab_training))

            train_fig = plt.figure(facecolor='#07000d')
            train_canvas = train_fig.canvas
            train_layout = FloatLayout()
            train_layout.add_widget(train_canvas)
            tab_training.add_widget(train_layout)

            if os.path.exists('training.csv'):
                os.remove('training.csv')

            def update_plot(fig):
                train_canvas = fig.canvas
                train_layout.clear_widgets()
                train_layout.add_widget(train_canvas)
                plt.close(fig)
                # for child in train_layout.children[:1]:
                #     train_layout.remove_widget(child)
                # Clock.schedule_once(lambda *args: tab_panel.switch_to(tab_training))

            def read_training(self):
                if os.path.exists('training.csv'):
                    loss_df = None
                    try:
                        loss_df = pd.read_csv('training.csv')
                    except Exception as e:
                        print(e)
                        pass
                    if loss_df is not None:
                        train_fig = plot_loss(loss_df)
                        update_plot(train_fig)
                if not process.is_alive():
                    Clock.unschedule(read_training)
                    val_fig, future_fig = que.get()
                    val_canvas = val_fig.canvas
                    val_layout = FloatLayout()
                    val_layout.add_widget(val_canvas)
                    tab_validate.add_widget(val_layout)

                    future_canvas = future_fig.canvas
                    future_layout = FloatLayout()
                    future_layout.add_widget(future_canvas)
                    tab_future.add_widget(future_layout)

                    Clock.schedule_once(
                        lambda *args: tab_panel.switch_to(tab_validate))

            Clock.schedule_interval(read_training, 0.1)

        Clock.schedule_once(lambda *args: tab_panel.switch_to(tab_chart))

        self.add_widget(tab_panel)