예제 #1
0
def displayGlobalButton():
    '''
    显示全局按钮
    :return: 
    '''
    config = Config(GLOBAL_SECTION)
    if not config.isReady():
        config.initConf(GLOBAL_SECTION)
        config.writeConf(GLOBAL_SECTION, 'ref', 'True')
        config.writeConf(GLOBAL_SECTION, 'debug', 'False')

    isDisplayRef = config.readConf(GLOBAL_SECTION, 'ref', 'True') == str(True)
    isDisplayDebug = config.readConf(GLOBAL_SECTION, 'debug', 'True') == str(True)

    btnRef = ToggleButton(description = '隐藏参考信息' if isDisplayRef else '显示参考信息', value = isDisplayRef)
    btnDebug = ToggleButton(description = '隐藏调试信息' if isDisplayDebug else '显示调试信息', value = isDisplayDebug)
    btnDebugClear = Button(description = '清除调试信息')

    debug.setCleaner(btnDebugClear)

    if isDisplayDebug:
        debug.enable()
    else:
        debug.disable()

    btnRef.observe(on_ref_change)
    btnDebug.observe(on_debug_change)
    btnDebugClear.on_click(on_debugclear_clicked)

    boxGlobal = Box([btnRef, btnDebug, btnDebugClear])
    display(boxGlobal)
예제 #2
0
    def setup_widget(self):
        ### grid widget construction:
        def items_callback (index, column):
            items = {}
            for tool,output in self.metadata.items():
                for row,col in output.get('Cell_errors', []):
                    if row == index and self.columns[col] ==  column:
                        items [f"info_{tool}"] = f"False : Detected By {tool}" 

            items ["label_False"] = 'Label as False'
            for action, text in zip(['label', 'update'], ['Labeld By', 'Updated By']):
                for name, data in self.collaborators.items():
                    df = data[action]
                    val = df.loc[index,column]

                    if name == self.user_id or pd.isna(val):
                        continue

                    key = f"{action}_{val}_{name}" 
                    items[key]= f"{val} : {text} {name}" 

            return items

        def click_callback(index, column, key):
            action, val = key.split('_')[0:2]
            
            if action == 'update':
                self.grid_widget.edit_cell(index,column, val)
            elif action == 'label':
                self.label.loc[index,column] = val
            

        context_menu = {
                    'items_callback' : items_callback,
                    'click_callback' : click_callback
                }
        grid_widget = qgrid.show_grid(self, context_menu = context_menu)
        def handle_cell_edited(event, grid_widget):
            index, column, new_value = event['index'], event['column'], event['new']
            self.loc[index, column] = new_value 

        grid_widget.on('cell_edited', handle_cell_edited)
        self.grid_widget = grid_widget
        

        #refresh button
        refresh_button = Button(description='Refresh')
        refresh_button.on_click(self.refresh)

        #commit button
        commit_button = Button(description='Commit')
        commit_button.on_click(self.commit)

        def get_widget():
            self.refresh()
            return VBox([
                        HBox([refresh_button,commit_button]),
                        grid_widget
                ])
        self.get_widget = get_widget
예제 #3
0
        def make_buttons(input_widget, inverse=False):
            button_layout = {'width': '20px'}
            factor = 0.5 if inverse else 2.0
            double_btn = Button(description="", button_style='warning', layout=button_layout)
            half_btn = Button(description="", button_style='success', layout=button_layout)

            def double_value(_):
                input_widget.value *= factor

            def half_value(_):
                input_widget.value /= factor

            widgets.jslink((double_btn, 'disabled'), (input_widget, 'disabled'))
            widgets.jslink((half_btn, 'disabled'), (input_widget, 'disabled'))
            double_btn.on_click(double_value)
            half_btn.on_click(half_value)
            return [half_btn, double_btn]
예제 #4
0
    def displayExecute(self):
        '''
        执行请求
        :return: 
        '''
        if not self.cf.isReady():
            print(Fore.RED + '配置项尚未初始化!')
        elif self.env.tester is not None:
            self.btnExecute = Button(description = '执行请求', button_style = 'primary')
            btnCopy = Button(description = '复制请求链接')

            self.btnExecute.on_click(self.on_execute_clicked)
            btnCopy.on_click(self.on_copy_clicked)

            self.htmExecute = HTML(value = '')
            # boxExecute = VBox([Box([self.btnExecute, btnCopy]), self.htmExecute])
            boxExecute = VBox([self.btnExecute, self.htmExecute])

            display(boxExecute)
예제 #5
0
def _create_notebook_form(entity_id, entity_type, message, responses,
                          credentials):
    check_package('ipywidgets')

    from ipywidgets.widgets import HTML, Layout, Button, GridspecLayout

    text = HTML(message)

    button_yes = Button(description='Yes',
                        button_style='info',
                        layout=Layout(height='32px', width='176px'))
    button_no = Button(description='No',
                       button_style='',
                       layout=Layout(height='32px', width='176px'))

    buttons = GridspecLayout(1, 5)
    buttons[0, 0] = button_yes
    buttons[0, 1] = button_no

    def disable_buttons():
        button_yes.disabled = True
        button_no.disabled = True

    def on_button_yes_clicked(b):
        disable_buttons()
        response = trigger_subscription(entity_id, entity_type, credentials)
        if response:
            display(HTML(responses.get('ok')))
        else:
            display(HTML(responses.get('error')))

    def on_button_no_clicked(b):
        disable_buttons()
        display(HTML(responses.get('cancel')))

    button_yes.on_click(on_button_yes_clicked)
    button_no.on_click(on_button_no_clicked)

    return (text, buttons)
예제 #6
0
class SoundDemo1Dashboard1:
    def __init__(self):
        """
        :param int i: 
        """

        self.select_training_data_options = None

        self.select_training_data = Dropdown(
            options={'Record training data': 0,
                     'Load training data files': 1
                     },
            value=0,
            description='Choose training data:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.select_training_data.observe(self.on_change)

        self.select_nseconds = Dropdown(
            options={
                '2s': 2,
                '3s': 3,
                '5s': 5
            },
            value=2,
            description='Choose recording length:',
            disabled=False,
            layout=Layout(width='400px', display='block'),
            style={'description_width': '160px'},
        )

        self.select_nclasses = Dropdown(
            options={'2': 2,
                     '3': 3,
                     '4': 4
                     },
            value=2,
            description='Choose number of classes:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.select_nfiles = Dropdown(
            options={'12': 12,
                     '8': 8,
                     '6': 6,
                     '4': 4
                     },
            value=12,
            description='Choose number of files:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.container = Output(
            value="",
        )

        self.submit_button = Button(
            value=False,
            description='Get data',
            disabled=False,
            button_style='success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='TRAIN: use recorded sounds or record new',
            icon=''
        )

        self.play_button = Button(
            value=False,
            description='Play the recording',
            disabled=False,
            button_style='info',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='TRAIN: play recorded or loaded sounds',
            icon=''
        )

        self.widget_box = VBox(
            (
                HBox(
                    (
                        VBox((
                            self.select_training_data,
                            self.select_nseconds,
                            self.select_nclasses,
                            self.select_nfiles
                        ), ),
                        VBox((
                            self.submit_button,
                            self.play_button
                        ), )
                    ),
                ),
                self.container
            ),
        )

        self.submit_button.on_click(self._run)
        self.play_button.on_click(self._playback)

    @property
    def start(self):
        return self.widget_box

    def on_change(self, change):
        if change['type'] == 'change' and change['name'] == 'value':
            if change["new"] != 0:
                self.select_nseconds.layout.display = 'none'
                self.select_nseconds.disabled = True
            else:
                self.select_nseconds.layout.display = 'block'
                self.select_nseconds.disabled = False
            # print("changed to {}".format(change['new']))

    def _playback(self, v):

        if self.data:
            # instantiate pyaudio
            p = pyaudio.PyAudio()
            i = 0
            for d in enumerate(self.data[0]):
                i = i + 1
                # instantiate stream
                stream = p.open(format=pyaudio.paFloat32, # float32 insteda of paInt16 because librosa loads in float32
                                     channels=1,
                                     rate=22050,
                                     output=True,
                                     )
                print("Playing example {}, label {} ...".format(i, self.data[1][i - 1]))
                time.sleep(1)
                try:
                    stream.write(self.data[0][i - 1], len(self.data[0][i - 1]))
                except Exception:
                    print("Error:", sys.exc_info()[0])
            stream.stop_stream()
            stream.close()
            p.terminate()
        else:
            print("No training data has been loaded. Please load the training data first.")

    def _run(self, v):
        self.prefix = "training"
        self.recorder = Recorder(n_classes=self.select_nclasses.value,
                                 n_files=self.select_nfiles.value,
                                 prefix=self.prefix,
                                 wav_dir='tmp')
        if self.select_training_data.value != 0:
            # Remove the data with the same prefix
            print("Loading the recorded data..")
            try:
                files = self.recorder.get_files()
                s = [True if self.prefix in file else False for file in files]
            except Exception:
                print("Loading failed. No files were found.")
                return
            if True in s:
                self.data = self.recorder.create_dataset()
            else:
                print("Loading failed. No relevant files were found.")
                return

        # If we choose to record the data, then we record it here
        if self.select_training_data.value == 0:
            print("Recording the training data..")
            # Remove the data with the same prefix
            files = self.recorder.get_files()
            for file in files:
                if self.prefix in file:
                    os.remove(file)
            # Start recording
            self.recorder.record(seconds=self.select_nseconds.value, clear_output=False)
            self.data = self.recorder.create_dataset()

        print("Data has been loaded.")

        return
from ipywidgets.widgets import Dropdown, Text, Button, HBox, VBox
from IPython.display import display, clear_output
import pandas as pd

from FileValidator.app import TheValidator


def get_clicked(b):
    clear_output()  #resets output every time you click the button
    csv_to_dataframe = pd.read_csv(FILE_PATH.value)
    validate_app = TheValidator(csv_to_dataframe, FUNCTION.value)
    output = validate_app.main()
    output.to_csv(FILE_PATH.value.replace('.csv', '_validated.csv'), sep=',')
    print('Validated! Please verify the output')


FILE_PATH = Text(placeholder='Path to file')
VALIDATE_BUTTON = Button(description="Validate!", button_style="primary")
FUNCTION = Dropdown(description="Select a File Type", options=['FileType1'])
VALIDATE_BUTTON.on_click(get_clicked)
FILE_PATH.layout.width = '75%'
display(FILE_PATH, FUNCTION)
display(VALIDATE_BUTTON)
class RSSWikiDashboard:
    def __init__(self, wikipedia: Wikipedia, rsspediainit: RsspediaInit):
        """
        :param Wikipedia wikipedia: wikipedia class object initialized with correct language
        :param RsspediaInit rsspediainit: rsspedia init object - prepares the data and embeddings
        """

        self.data_titles = []
        self.data_results = []
        self.rsspediainit = rsspediainit
        self.wikipedia = wikipedia

        self.select_feed = Dropdown(
            options={
                'Politiken.dk': 0,
                'DR.dk': 1,
                'BT.dk': 2,
                'Information.dk': 3,
                'Børsen.dk': 4,
                'Ekstrabladet.dk': 5
            },
            value=0,
            description='Vælg nyhedskilde:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.select_search = Dropdown(
            options={
                'Okapi BM-25': 0,
                'Explicit Semantic Analysis': 1,
                'FTN-a': 2,
                'FTN-b': 3
            },
            value=0,
            description='Vælg søgnings-algorytme:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.select_nmatches = Dropdown(
            options={
                '3': 3,
                '5': 5,
                '10': 10
            },
            value=3,
            description='Vælg antal matches:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.container = Output(value="", )

        self.submit_button = Button(
            value=False,
            description='Indlæs nyheder',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Få nyheder fra RSS-feed og find Wikipedia matches',
            icon='')

        self.widget_box = VBox(
            (self.select_feed, self.select_search, self.select_nmatches,
             self.submit_button, self.container), )

        self.submit_button.on_click(self._run)

    @property
    def start(self):
        return self.widget_box

    def display_beautifully(self):
        content_items = []
        for i in range(len(self.data_results)):
            news, titles, texts, urls, scores = self.data_results[i]
            #content_items.append(self.rsspedia_search.display_beautifully(titles, texts, urls, scores))
            content_items.append(
                HTML(value="{}".format(
                    self.rsspedia_search.display_beautifully(
                        titles, texts, urls, scores))))
        accordion = Accordion(children=(content_items), )

        for i in range(len(self.data_titles)):
            accordion.set_title(i, "{}. {}".format(i + 1, self.data_titles[i]))

        display(accordion)

    def _run(self, *args, **kwargs):
        selected_value = False
        self.data_results = []
        self.data_titles = []
        RSS_feeds = [
            ('Politiken.dk', 'http://politiken.dk/rss/senestenyt.rss'),
            ('DR.dk',
             'http://www.dr.dk/Forms/Published/rssNewsFeed.aspx?config=6b82610d-b898-49b2-80ef-85c5642519c3&rss=Yes&rssTitle=DR+Nyheder+Online&overskrift=Politik+-+seneste+20&Url=%2fnyheder%2f'
             ), ('BT.dk', 'https://www.bt.dk/bt/seneste/rss'),
            ('Information.dk', 'https://www.information.dk/feed'),
            ('Børsen.dk', 'https://borsen.dk/rss/'),
            ('Ekstrabladet.dk', 'http://ekstrabladet.dk/seneste10.rss')
        ]

        search_algorithms = [("okapibm25"), ("esa_relatedness"),
                             ("fasttext_a"), ("fasttext_b")]
        search_algorithm = search_algorithms[self.select_search.value]

        self.rsspedia_search = RsspediaSearch(rsspediainit=self.rsspediainit,
                                              wikipedia=self.wikipedia)

        if selected_value:
            feed = feedparser.parse(RSS_feeds[selected_value][1])
        else:
            feed = feedparser.parse(RSS_feeds[self.select_feed.value][1])

        # Get relevant objects from RSS feed ans store titles and scores
        for i in range(len(feed["entries"])):
            self.data_titles.append(feed["entries"][i]["title"])
            titles, texts, urls, scores = self.rsspedia_search.search_wiki(
                search_texts=[feed["entries"][i]["title"]],
                n_matches=self.select_nmatches.value,
                search_type=search_algorithm)
            self.data_results.append(
                [feed["entries"][i]["title"], titles, texts, urls, scores])

        return self.display_beautifully()
class ViewSentiment:
    def __init__(self):
        plt.close("all")
        self.backend = TextInputLoop(use_widget=True)
        self.highlighter = SentimentHighlighter(self.backend)
        self.backend.add_interface(self.highlighter)
        self.backend.start()

        self.cwd_label = Label(
            value="Working directory: {}".format(Path.cwd()),
            layout=dict(margin="2px 0px 0px 20px"),
        )
        self.save_path = Text(
            value=str(Path("saved_html.html")),
            description='Save path:',
            disabled=False,
            layout=dict(width="50%"),
        )
        self.save_button = Button(
            value=False,
            description='Save to HTML',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='',
            icon='',
            layout=Layout(width='18%', margin="2px 0px 0px 20px"),
        )
        self.progress_label = Label(
            value="",
            layout=dict(margin="2px 0px 0px 20px"),
        )

        self.full_contrast = Checkbox(value=False,
                                      description='Full Contrast',
                                      disabled=False)
        self.do_highlighting = Checkbox(value=True,
                                        description='Do Highlighting',
                                        disabled=False)

        box1 = HBox(
            (self.do_highlighting, self.full_contrast),
            layout=Layout(align_content="flex-start"),
        )

        box2 = HBox(
            (self.save_path, self.save_button),
            layout=Layout(align_content="flex-start"),
        )

        self.storage_dashboard = VBox(
            (box1, self.cwd_label, box2, self.progress_label))

        # Set observers
        self.save_button.on_click(self._save)
        self.full_contrast.observe(self._special_options)
        self.do_highlighting.observe(self._special_options)

        # noinspection PyTypeChecker
        display(self.storage_dashboard)

    def _special_options(self, _=None):
        self.highlighter.full_contrast = self.full_contrast.value
        self.highlighter.do_highlighting = self.do_highlighting.value
        self.highlighter.refresh()

    def _reset_progress(self):
        self.progress_label.value = ""

    def _save(self, _=None):
        if self.highlighter.c_modifiers is not None:
            # Convert to HTML
            html = modified_text_to_html(
                text=self.highlighter.c_text,
                modifiers=self.highlighter.c_modifiers)

            # Get save path
            path = Path(self.save_path.value)

            # Save HTML
            with path.open("w") as file:
                file.write(html)

            self.progress_label.value = "Saved!"
            timer = Timer(4.0, self._reset_progress)
            timer.start()
class RSSDashboard:
    def __init__(self):
        self.data_titles = []
        self.data_scores = []
        self.afinn = None

        self.select = Dropdown(
            options={
                'Politiken.dk': 0,
                'DR.dk': 1,
                'BT.dk': 2,
                'Information.dk': 3,
                'Børsen.dk': 4,
                'Ekstrabladet.dk': 5
            },
            value=0,
            description='Vælg nyhedskilde:',
            disabled=False,
            layout=Layout(width='300px'),
            style={'description_width': '130px'},
        )

        self.container = Output(value="", )

        self.submit_button = Button(
            value=False,
            description='Indlæs nyheder',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Få nyheder fra RSS-feed og lav sentiment-analyse',
            icon='')

        self.widget_box = VBox(
            (self.select, self.submit_button, self.container), )

        self.submit_button.on_click(self._do_sentiment_analysis)

    @property
    def start(self):
        return self.widget_box

    def _do_sentiment_analysis(self, selected_value=None):
        RSS_feeds = [
            ('Politiken.dk', 'http://politiken.dk/rss/senestenyt.rss'),
            ('DR.dk',
             'http://www.dr.dk/Forms/Published/rssNewsFeed.aspx?config=6b82610d-b898-49b2-80ef-85c5642519c3&rss=Yes&rssTitle=DR+Nyheder+Online&overskrift=Politik+-+seneste+20&Url=%2fnyheder%2f'
             ), ('BT.dk', 'https://www.bt.dk/bt/seneste/rss'),
            ('Information.dk', 'https://www.information.dk/feed'),
            ('Børsen.dk', 'https://borsen.dk/rss/'),
            ('Ekstrabladet.dk', 'http://ekstrabladet.dk/seneste10.rss')
        ]

        if selected_value:
            feed = feedparser.parse(RSS_feeds[selected_value][1])
        else:
            feed = feedparser.parse(RSS_feeds[self.select.value][1])

        self.afinn = Afinn(language="da")

        # Get relevant objects from RSS feed ans store titles and scores
        for i in range(len(feed["entries"])):
            self.data_titles.append(feed["entries"][i]["title"])
            self.data_scores.append(
                self.afinn.score(feed["entries"][i]["title"]))

        # Dataframe
        pd.set_option('display.max_colwidth',
                      -1)  # Used to display whole title (non-truncated)
        df = pd.DataFrame({
            "Title": self.data_titles,
            "Score": self.data_scores
        })  # Creating the data frame and populating it

        # Highlight the positive and negative sentiments
        def highlight(s):
            if s.Score > 0:
                return ['background-color: #AAFFAA'] * 2
            elif s.Score < 0:
                return ['background-color: #FFAAAA'] * 2
            else:
                return ['background-color: #FFFFFF'] * 2

        df = df.style.apply(highlight, axis=1)
        display(df)
예제 #11
0
class JupyterSearchInput(HBox):
    def __init__(self, model, *args, **kwargs):
        self.model = model
        self._fields = {field: Text() for field in self.model.fields}
        self.radio_button_group = RadioButtons(
            description="When:",
            options=[
                "All", "30 Days", "24 Hours", "1 Year", "1 Week", "1 Hour"
            ],
        )
        self.refresh_button = Button(description="Refresh")
        date_buttons = VBox([self.radio_button_group, self.refresh_button])
        self.since_widget = DatetimePicker()
        self.until_widget = DatetimePicker()
        date_range = HBox([
            Label(value="Date range:"),
            self.since_widget,
            Label(value="-"),
            self.until_widget,
        ])

        grid_children = []
        for field, text in self._fields.items():
            grid_children.append(Label(value=f"{field}:"))
            grid_children.append(text)

        text_grid = GridBox(children=grid_children,
                            layout=Layout(grid_template_columns="30% 70%"))

        text_input = VBox([date_range, text_grid])
        children = (date_buttons, text_input)
        if self.model.text_search_supported:
            full_text_label = Label("Full Text Search:")
            self.text_search_input = Text()
            text_grid.children += (full_text_label, self.text_search_input)
            self.text_search_input.observe(self._on_text_view_changed, "value")
            self.model.events.text.connect(self._on_text_model_changed)

        super().__init__(children, **kwargs)

        self.radio_button_group.observe(self._on_radio_button_changed, "value")
        self.refresh_button.on_click(self._on_reload_request)
        self.model.events.reload.connect(self._on_reload)
        self.model.events.query.connect(self._on_reload)

        self.since_widget.observe(self._on_since_view_changed, "value")
        self.model.events.since.connect(self._on_since_model_changed)
        self.until_widget.observe(self._on_until_view_changed, "value")
        self.model.events.until.connect(self._on_until_model_changed)

        # Set these values here so the model picks the values up
        self.model.since = GRACE_HOPPER_BIRTHDAY
        self.model.until = timedelta()
        self.radio_button_group.index = self.radio_button_group.options.index(
            "All")

        for field, text in zip(self.model.fields, self._fields.values()):

            def on_field_text_changed(change, field=field):
                self.model.field_search.update({field: change["new"]})

            text.observe(on_field_text_changed, "value")

        self.model.events.field_search_updated.connect(
            self._on_field_search_updated)

    def _on_radio_button_changed(self, change):
        if change["new"] == "30 Days":
            self.model.since = timedelta(days=-30)
            self.model.until = timedelta()
        elif change["new"] == "24 Hours":
            self.model.since = timedelta(days=-1)
            self.model.until = timedelta()
        elif change["new"] == "1 Year":
            self.model.since = timedelta(days=-365)
            self.model.until = timedelta()
        elif change["new"] == "1 Week":
            self.model.since = timedelta(days=-7)
            self.model.until = timedelta()
        elif change["new"] == "1 Hour":
            self.model.since = timedelta(hours=-1)
            self.model.until = timedelta()
        elif change["new"] == "All":
            self.model.since = GRACE_HOPPER_BIRTHDAY
            self.model.until = timedelta()

    def _on_reload_request(self, event):
        self.model.request_reload()

    def _on_reload(self, event):
        now = self._now()
        if isinstance(self.model.since, timedelta):
            with _blocked(self.since_widget,
                          self._on_since_view_changed,
                          names="value"):
                self.since_widget.value = now + self.model.since
        if isinstance(self.model.until, timedelta):
            with _blocked(self.until_widget,
                          self._on_until_view_changed,
                          names="value"):
                self.until_widget.value = now + self.model.until

    def _on_since_view_changed(self, change):
        self.model.since = change["new"]

    def _on_since_model_changed(self, event):
        now = self._now()
        if isinstance(event.date, timedelta):
            new_datetime = now + event.date
            if event.date == timedelta(hours=-1):
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "1 Hour")
            elif event.date == timedelta(days=-1):
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "24 Hours")
            elif event.date == timedelta(days=-7):
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "1 Week")
            elif event.date == timedelta(days=-30):
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "30 Days")
            elif event.date == timedelta(days=-365):
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "1 Year")
            else:
                pass
        else:
            if event.date == GRACE_HOPPER_BIRTHDAY:
                self.radio_button_group.index = self.radio_button_group.options.index(
                    "All")
            else:
                self.radio_button_group.index = None
            new_datetime = event.date
        with _blocked(self.since_widget,
                      self._on_since_view_changed,
                      names="value"):
            self.since_widget.value = new_datetime
        with _blocked(self.until_widget,
                      self._on_until_view_changed,
                      names="value"):
            self.until_widget.value = now

    def _on_until_view_changed(self, change):
        self.model.until = change["new"]

    def _on_until_model_changed(self, event):
        if not isinstance(event.date, timedelta):
            self.radio_button_group.index = None
            with _blocked(self.until_widget,
                          self._on_until_view_changed,
                          names="value"):
                self.until_widget.value = event.date
        else:
            with _blocked(self.until_widget,
                          self._on_until_view_changed,
                          names="value"):
                self.until_widget.value = event.date + self._now()

    def _on_text_view_changed(self, change):
        self.model.text = change["new"]

    def _on_text_model_changed(self, event):
        self.text_search_input.value = event.text

    def _on_field_search_updated(self, event):
        for k, v in event.update.items():
            text_widget = self._fields[k]
            text_widget.value = v

    @staticmethod
    def _now():
        return datetime.now(tz=LOCAL_TIMEZONE).replace(second=0, microsecond=0)
예제 #12
0
class QueryWindow:
    def __init__(self, qNo):
        self.qNo = qNo
        self.qOut = Output()
        self.queryArea = Textarea(
            value='',
            placeholder='Type your Query here',
            description='',
            disabled=False,
            #layout = Layout(max_width='30%')
        )
        self.execute = Button(
            description='Execute',
            disabled=False,
            button_style='',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Execute',
            #layout = Layout(max_width='20%')
        )
        self.resultMessage = Output()
        self.execute.on_click(self.executeQuery)
        self.yourOutput = Output(layout=Layout())
        self.expectedOutput = Output(layout=Layout())
        self.yourOut = Output()
        self.expectOut = Output()
        with self.yourOut:
            display(HTML('<b>Your Ouput:</b>'))
        with self.expectOut:
            display(HTML('<b>Expected Ouput:</b>'))
        self.disPlayWindow = VBox([HBox([self.qOut,self.queryArea,self.execute,self.resultMessage]),\
        VBox([VBox([self.expectOut,self.expectedOutput]\
                   ),VBox([self.yourOut,self.yourOutput])])]\
                                  ,layout = Layout(width='80%'))
        self.qset = pd.read_csv('questions.csv')
        self.questionData = self.qset.loc[self.qset.qNo == self.qNo]
        expected = self.getExpected()
        with self.expectedOutput:
            display(expected)
        with self.qOut:
            print(self.questionData.question.values[0])

    def getExpected(self):
        expected = pd.read_pickle('results/' +
                                  self.questionData.results.values[0])
        if self.questionData.result_type.values[0] != 'set':
            expected = expected.iloc[0][0]
        return expected

    def display(self):
        return self.disPlayWindow

    def executeQuery(self, event):
        self.resultMessage.clear_output()
        self.yourOutput.clear_output()
        db = databases.getDB(self.questionData.database.values[0])
        error = False
        try:
            result = pd.read_sql_query(self.queryArea.value, db)
        except:
            with self.resultMessage:
                display(
                    HTML(
                        '<span style="color:red">Error in processing your query. Please check syntax and retry</span>'
                    ))
            error = True
        if not error:
            match = False
            expected = self.getExpected()
            if self.questionData.result_type.values[0] != 'set':
                if result.shape == (1, 1):
                    result = result.iloc[0][0]
                    match = result == expected
            else:
                if not self.questionData.isOrder.values[0]:
                    #check all columns are same
                    if set(result.columns.values) == set(
                            expected.columns.values) == 0:
                        match=result.sort_values(by=[self.questionData.sort_key.values[0]])\
                        .reset_index(drop=True).equals(expected.sort_values(by=[self.questionData.sort_key.values[0]])\
                                                       .reset_index(drop=True))
                else:
                    match = result.reset_index(drop=True).equals(
                        expected.reset_index(drop=True))
            with self.yourOutput:
                display(result)
            msg = '<span style="color:green">Success!!!!!</span>'
            if not match:
                msg = '<span style="color:red">Sorry your query results doesnot match the expected result. Please try again</span>'
            with self.resultMessage:
                display(HTML(msg))
예제 #13
0
    def __menu(self, parameter_type, box, category_name=None):

        btn_add = Button(description='Add')
        ddn_add = Dropdown()

        if parameter_type == 'scalars':

            def fn_add(self):

                value = None

                if ddn_add.value == 'Integer':
                    value = 1
                elif ddn_add.value == 'Float':
                    value = 1.0
                elif ddn_add.value == 'Bool':
                    value = True

                name = f'{ddn_add.value}_{len(box.children)}'
                scalar = self.__lp.add_scalar(name,
                                              value,
                                              category=category_name)
                editor = make_scalar_editor(scalar)
                editor.observe(self.__on_editor_changed(scalar))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Integer', 'Float', 'Bool']

        elif parameter_type == 'materials':

            def fn_add(self):

                index = max(self.__lp.get_colors().keys()) + 1 if len(
                    self.__lp.get_colors().keys()) else 0

                self.__lp.set_color(
                    index, pgl.Material(ambient=(80, 80, 80), diffuse=1))
                editor = make_color_editor(self.__lp.get_color(index),
                                           index,
                                           no_name=True)
                editor.observe(
                    self.__on_editor_changed(self.__lp.get_color(index)))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Color']

        elif parameter_type == 'curves':

            def fn_add(self):

                name = f'{ddn_add.value}_{len(box.children)}'
                if ddn_add.value == 'Function':
                    curve = self.__lp.add_function(name,
                                                   category=category_name)
                else:
                    curve = self.__lp.add_curve(name, category=category_name)
                editor = make_curve_editor(curve)
                editor.observe(self.__on_editor_changed(curve))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Curve', 'Function']

        btn_add.on_click(lambda x: fn_add(self))

        return HBox((btn_add, ddn_add))
예제 #14
0
class TwoClassCameraDashboard:
    def __init__(self):
        self.start_button = Button(
            value=False,
            description='Start Camera',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Start the camera to take pictures for classifier ',
            icon='',
            layout=Layout(width='25%'),
        )

        self.save_button = Button(
            value=False,
            description='Save images',
            disabled=True,
            button_style=
            'danger',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save images to folder ',
            icon='',
            layout=Layout(width='18%', margin="2px 0px 0px 20px"),
        )
        self.load_button = Button(
            value=False,
            description='Load images',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Load images from folder ',
            icon='',
            layout=Layout(width='18%', margin="2px 0px 0px 20px"),
        )

        self.save_path = Text(
            value=str(Path("path", "to", "directory")),
            description='Save path:',
            disabled=False,
        )

        self.num_pictures = FloatText(
            value=12,
            description='#pictures:',
            disabled=False,
            layout=Layout(width='20%'),
        )

        self.use_augmentation = Checkbox(
            value=False,
            description='Augmentation',
            disabled=False,
            tooltip='Use Augmentation',
            icon='check',
        )

        self.progress_text = Label(
            value='',
            layout=Layout(margin="5px 0px 5px 20px"),
        )

        self.widget_box = VBox((
            HBox(
                (self.num_pictures, self.use_augmentation, self.start_button),
                layout=Layout(align_content="flex-start"),
            ),
            HBox(
                (self.progress_text, ),
                layout=Layout(align_content="flex-start"),
            ),
            HBox(
                (self.save_path, self.save_button, self.load_button),
                layout=Layout(align_content="flex-start"),
            ),
        ))

        self.start_button.on_click(self._start_video)
        self.save_button.on_click(self._save_images)
        self.load_button.on_click(self._load_images)

        self.collector = ImageCollector()

    def load_ml_data(self, random_permutation=True, train_split=0.9):
        return self.collector.load_ml_data(
            random_permutation=random_permutation, train_split=train_split)

    @property
    def start(self):
        return self.widget_box

    def _load_images(self, _=None):
        # Can no longer use camera or save images
        self.save_button.disabled = True
        self.start_button.disabled = True

        # Get path from widget
        load_path = Path(self.save_path.value)
        if load_path.name != images_dir_name:
            load_path = Path(load_path, images_dir_name)

        # Load data
        self.progress_text.value = "Loading images from: {}".format(
            load_path.resolve())
        self.collector.load_data_from_files(file_path=load_path)

        if self.collector.loaded:
            self.progress_text.value = "Images loaded from: {}".format(
                load_path.resolve())
        else:
            self.progress_text.value = "This directory does not seem to contain jpg-files: {}"\
                .format(load_path.resolve())

    def _save_images(self, _=None):
        # Get values from widgets
        use_augmentation = self.use_augmentation.value
        save_path = Path(self.save_path.value)

        self.progress_text.value = "Saving images to: {}".format(
            save_path.resolve())

        # Use collector to save images
        self.collector.save_images(save_path,
                                   use_augmentation=use_augmentation)

        self.progress_text.value = "Images saved to: {}".format(
            save_path.resolve())

    def _start_video(self, _):
        # Reset start-button and notify
        self.start_button.value = False
        self.progress_text.value = "Starting camera! (please wait)"

        # Disable controls
        self.start_button.disabled = True
        self.num_pictures.disabled = True
        self.use_augmentation.disabled = True
        self.save_path.disabled = True

        # Get settings
        self.collector.num_pictures = int(self.num_pictures.value)

        # Start video
        self.collector.run_collector(use_binary=True)

        # Re-enable controls
        self.start_button.disabled = False
        self.num_pictures.disabled = False
        self.save_button.disabled = False
        self.use_augmentation.disabled = False
        self.save_path.disabled = False

        self.progress_text.value = "Video done."
예제 #15
0
def mk_map_region_selector(height='600px', **kwargs):
    from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl
    from ipywidgets.widgets import Layout, Button, HTML
    from types import SimpleNamespace

    state = SimpleNamespace(selection=None, bounds=None, done=False)

    btn_done = Button(description='done', layout=Layout(width='5em'))
    btn_done.style.button_color = 'green'
    btn_done.disabled = True

    html_info = HTML(
        layout=Layout(flex='1 0 20em', width='20em', height='3em'))

    def update_info(txt):
        html_info.value = '<pre style="color:grey">' + txt + '</pre>'

    m = Map(**kwargs) if len(kwargs) else Map(zoom=2)
    m.scroll_wheel_zoom = True
    m.layout.height = height

    widgets = [
        WidgetControl(widget=btn_done, position='topright'),
        WidgetControl(widget=html_info, position='bottomleft'),
    ]
    for w in widgets:
        m.add_control(w)

    draw = DrawControl()

    draw.circle = {}
    draw.polyline = {}
    draw.circlemarker = {}

    shape_opts = {
        "fillColor": "#fca45d",
        "color": "#000000",
        "fillOpacity": 0.1
    }
    draw.rectangle = {"shapeOptions": shape_opts}
    poly_opts = {"shapeOptions": dict(**shape_opts)}
    poly_opts["shapeOptions"]["original"] = dict(**shape_opts)
    poly_opts["shapeOptions"]["editing"] = dict(**shape_opts)

    draw.polygon = poly_opts
    draw.edit = True
    draw.remove = True
    m.add_control(draw)
    m.add_control(FullScreenControl())

    def on_done(btn):
        state.done = True
        btn_done.disabled = True
        m.remove_control(draw)
        for w in widgets:
            m.remove_control(w)

    def bounds_handler(event):
        (lat1, lon1), (lat2, lon2) = event['new']
        txt = 'lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]'.format(
            lat1, lat2, lon1, lon2, n=4)
        update_info(txt)
        state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2))

    def on_draw(event):
        v = event['new']
        action = event['name']
        if action == 'last_draw':
            state.selection = v['geometry']
        elif action == 'last_action' and v == 'deleted':
            state.selection = None

        btn_done.disabled = state.selection is None

    draw.observe(on_draw)
    m.observe(bounds_handler, ('bounds', ))
    btn_done.on_click(on_done)

    return m, state
예제 #16
0
    def __init__(self, df: pd.DataFrame, col_name: str, df_name: str,
                 page_size: int):
        self._clusterer = Clusterer(df, col_name, df_name)
        self._clusterer.cluster("fingerprint")

        self._page_size = page_size

        # clustering dropdown and export code checkbox, used in the top row
        self._clustering_method_label = Label(
            " Clustering Method: ", layout=Layout(margin="2px 0 0 20px"))
        self._clustering_method_drop = Dropdown(
            options=[
                "fingerprint", "ngram-fingerprint", "phonetic-fingerprint",
                "levenshtein"
            ],
            layout=Layout(width="150px", margin="0 0 0 10px"),
        )
        self._clustering_method_drop.observe(self._cluster_method_change,
                                             names="value")
        self._export_code = Checkbox(
            value=True,
            description="export code",
            layout=Layout(width="165px", margin="0 0 0 482px"),
            style={"description_width": "initial"},
        )
        self._dropds = HBox(
            [
                self._clustering_method_label,
                self._clustering_method_drop,
                self._export_code,
            ],
            layout=Layout(height="35px", margin="10px 0 0 0"),
        )
        # text boxes for clustering parameters used in the top row
        self._ngram_text = Text(
            value=DEFAULT_NGRAM,
            description="n-gram",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._radius_text = Text(
            value=DEFAULT_RADIUS,
            description="Radius",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._block_chars_text = Text(
            value=DEFAULT_BLOCK_SIZE,
            description="Block Chars",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._ngram_text.observe(self._param_recluster, names="value")
        self._radius_text.observe(self._param_recluster, names="value")
        self._block_chars_text.observe(self._param_recluster, names="value")

        # create header labels, second row
        headers = HBox(
            [
                Label("Distinct values", layout=Layout(margin="0 0 0 10px")),
                Label("Total values", layout=Layout(margin="0 0 0 35px")),
                Label("Cluster values", layout=Layout(margin="0 0 0 95px")),
                Label("Merge?", layout=Layout(margin="0 0 0 295px")),
                Label("Representative value",
                      layout=Layout(margin="0 0 0 50px")),
            ],
            layout=Layout(margin="10px"),
        )

        # create buttons for bottom row
        self._sel_all = Checkbox(description="Select all",
                                 layout=Layout(width="165px"))
        self._sel_all.observe(self._select_all, names="value")

        merge_and_recluster = Button(description="Merge and Re-Cluster",
                                     layout=Layout(margin="0 0 0 466px",
                                                   width="150px"))
        merge_and_recluster.on_click(self._execute_merge)

        finish = Button(description="Finish",
                        layout=Layout(margin="0 0 0 10px"))
        finish.on_click(self._close)

        # next and previous page buttons
        self._next_button = Button(description="Next")
        self._next_button.on_click(self._next_page)

        self._prev_button = Button(description="Previous",
                                   layout=Layout(margin="0 0 0 20px"))
        self._prev_button.on_click(self._prev_page)

        # an index in the clusters Series indicating the start of the current page
        self._page_pos = 0
        # loading label, displayed when re-clustering or next page load
        self._loading_label = Label("Loading...",
                                    layout=Layout(margin="170px 0 0 440px"))
        # displayed when the user enters a non integer value into a clustering parameter text box
        self._invalid_param_label = Label(
            "Invalid clustering parameter, please enter an integer",
            layout=Layout(margin="170px 0 0 350px"),
        )

        self._reprs = [
            Text(layout=Layout(width="200px", margin="0 10px 0 40px"))
            for _ in range(self._page_size)
        ]
        self._checks = [
            Checkbox(indent=False,
                     layout=Layout(width="auto", margin="0 0 0 20px"))
            for _ in range(self._page_size)
        ]

        # VBox containing a VBox with all the clusters in the first row and an optional
        # second row containing next and previous page buttons
        self._cluster_and_next_prev = VBox()
        self._cluster_vbox = VBox(
            layout=Layout(height="450px", flex_flow="row wrap"))

        footer = HBox([self._sel_all, merge_and_recluster, finish])

        box_children = [
            self._dropds, headers, self._cluster_and_next_prev, footer
        ]

        box_layout = Layout(display="flex",
                            flex_flow="column",
                            align_items="stretch",
                            border="solid")
        self._box = Box(children=box_children, layout=box_layout)
        self._update_clusters()
예제 #17
0
class UserInterface:
    """
    A user interface used by the clean_duplication function.
    """

    # pylint: disable=too-many-instance-attributes

    _clusterer: Clusterer
    _page_size: int
    _clustering_method_label: Label
    _clustering_method_drop: Dropdown
    _export_code: Checkbox
    _sel_all: Checkbox
    _next_button: Button
    _prev_button: Button
    _page_pos: int
    _ngram_text: Text
    _radius_text: Text
    _block_chars_text: Text
    _dropds: HBox
    _reprs: List[Text]
    _checks: List[Checkbox]
    _cluster_vbox: VBox
    _box: Box
    _loading_label: Label
    _invalid_param_label: Label

    def __init__(self, df: pd.DataFrame, col_name: str, df_name: str,
                 page_size: int):
        self._clusterer = Clusterer(df, col_name, df_name)
        self._clusterer.cluster("fingerprint")

        self._page_size = page_size

        # clustering dropdown and export code checkbox, used in the top row
        self._clustering_method_label = Label(
            " Clustering Method: ", layout=Layout(margin="2px 0 0 20px"))
        self._clustering_method_drop = Dropdown(
            options=[
                "fingerprint", "ngram-fingerprint", "phonetic-fingerprint",
                "levenshtein"
            ],
            layout=Layout(width="150px", margin="0 0 0 10px"),
        )
        self._clustering_method_drop.observe(self._cluster_method_change,
                                             names="value")
        self._export_code = Checkbox(
            value=True,
            description="export code",
            layout=Layout(width="165px", margin="0 0 0 482px"),
            style={"description_width": "initial"},
        )
        self._dropds = HBox(
            [
                self._clustering_method_label,
                self._clustering_method_drop,
                self._export_code,
            ],
            layout=Layout(height="35px", margin="10px 0 0 0"),
        )
        # text boxes for clustering parameters used in the top row
        self._ngram_text = Text(
            value=DEFAULT_NGRAM,
            description="n-gram",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._radius_text = Text(
            value=DEFAULT_RADIUS,
            description="Radius",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._block_chars_text = Text(
            value=DEFAULT_BLOCK_SIZE,
            description="Block Chars",
            layout=Layout(width="130px"),
            continuous_update=False,
        )
        self._ngram_text.observe(self._param_recluster, names="value")
        self._radius_text.observe(self._param_recluster, names="value")
        self._block_chars_text.observe(self._param_recluster, names="value")

        # create header labels, second row
        headers = HBox(
            [
                Label("Distinct values", layout=Layout(margin="0 0 0 10px")),
                Label("Total values", layout=Layout(margin="0 0 0 35px")),
                Label("Cluster values", layout=Layout(margin="0 0 0 95px")),
                Label("Merge?", layout=Layout(margin="0 0 0 295px")),
                Label("Representative value",
                      layout=Layout(margin="0 0 0 50px")),
            ],
            layout=Layout(margin="10px"),
        )

        # create buttons for bottom row
        self._sel_all = Checkbox(description="Select all",
                                 layout=Layout(width="165px"))
        self._sel_all.observe(self._select_all, names="value")

        merge_and_recluster = Button(description="Merge and Re-Cluster",
                                     layout=Layout(margin="0 0 0 466px",
                                                   width="150px"))
        merge_and_recluster.on_click(self._execute_merge)

        finish = Button(description="Finish",
                        layout=Layout(margin="0 0 0 10px"))
        finish.on_click(self._close)

        # next and previous page buttons
        self._next_button = Button(description="Next")
        self._next_button.on_click(self._next_page)

        self._prev_button = Button(description="Previous",
                                   layout=Layout(margin="0 0 0 20px"))
        self._prev_button.on_click(self._prev_page)

        # an index in the clusters Series indicating the start of the current page
        self._page_pos = 0
        # loading label, displayed when re-clustering or next page load
        self._loading_label = Label("Loading...",
                                    layout=Layout(margin="170px 0 0 440px"))
        # displayed when the user enters a non integer value into a clustering parameter text box
        self._invalid_param_label = Label(
            "Invalid clustering parameter, please enter an integer",
            layout=Layout(margin="170px 0 0 350px"),
        )

        self._reprs = [
            Text(layout=Layout(width="200px", margin="0 10px 0 40px"))
            for _ in range(self._page_size)
        ]
        self._checks = [
            Checkbox(indent=False,
                     layout=Layout(width="auto", margin="0 0 0 20px"))
            for _ in range(self._page_size)
        ]

        # VBox containing a VBox with all the clusters in the first row and an optional
        # second row containing next and previous page buttons
        self._cluster_and_next_prev = VBox()
        self._cluster_vbox = VBox(
            layout=Layout(height="450px", flex_flow="row wrap"))

        footer = HBox([self._sel_all, merge_and_recluster, finish])

        box_children = [
            self._dropds, headers, self._cluster_and_next_prev, footer
        ]

        box_layout = Layout(display="flex",
                            flex_flow="column",
                            align_items="stretch",
                            border="solid")
        self._box = Box(children=box_children, layout=box_layout)
        self._update_clusters()

    def _update_clusters(self) -> None:
        """
        Updates the clusters currently being displayed.
        """
        line = HBox(
            children=[Label("-" * 186, layout=Layout(margin="0 0 0 18px"))])
        self._sel_all.value = False

        cluster_page = self._clusterer.get_page(
            self._page_pos, self._page_pos + self._page_size)

        label_layout = Layout(height="22px", width="360px")
        box_children = [line]
        for idx, cluster in enumerate(cluster_page):
            labels = []
            for cluster_val, cnt in cluster:
                if cnt > 1:
                    cluster_val += f" ({cnt} rows)"
                labels.append(Label(cluster_val, layout=label_layout))

            totals_vals = sum(cnt for _, cnt in cluster)
            distinct_vals = len(cluster)

            self._reprs[idx].value = cluster[0][0]
            self._checks[idx].value = False
            box_children.append(
                HBox([
                    Label(str(distinct_vals),
                          layout=Layout(width="60px", margin="0 0 0 60px")),
                    Label(str(totals_vals),
                          layout=Layout(width="60px", margin="0 0 0 50px")),
                    VBox(children=labels, layout=Layout(margin="0 0 0 80px")),
                    self._checks[idx],
                    self._reprs[idx],
                ]))

            box_children.append(line)

        # no clusters to display
        if len(cluster_page) == 0:
            box_children = [
                Label(
                    "No clusters, try a different clustering method",
                    layout=Layout(margin="170px 0 0 360px"),
                )
            ]

        self._cluster_vbox.children = box_children
        cluster_and_next_prev = [self._cluster_vbox]
        self._add_next_prev_button_row(cluster_and_next_prev)
        self._cluster_and_next_prev.children = cluster_and_next_prev

    def _update_dropds(self, clustering_method: str) -> None:
        """
        Update the dropdowns row of the UI to display the required text boxes for passing
        parameters needed for the given clustering method.
        """
        if clustering_method in ("fingerprint", "phonetic-fingerprint"):
            self._export_code.layout.margin = "0 0 0 482px"
            self._dropds.children = [
                self._clustering_method_label,
                self._clustering_method_drop,
                self._export_code,
            ]

        if clustering_method == "ngram-fingerprint":
            self._export_code.layout.margin = "0 0 0 348px"
            self._dropds.children = [
                self._clustering_method_label,
                self._clustering_method_drop,
                self._ngram_text,
                self._export_code,
            ]

        if clustering_method == "levenshtein":
            self._export_code.layout.margin = "0 0 0 214px"
            self._dropds.children = [
                self._clustering_method_label,
                self._clustering_method_drop,
                self._radius_text,
                self._block_chars_text,
                self._export_code,
            ]

    def _param_recluster(self, _: Dict[str, Any]) -> None:
        """
        Re-cluster the dataframe with the new clustering parameters.
        Triggered when the value in a clustering parameter textbox is changed.
        """
        self._display_message(self._loading_label)
        try:
            self._clusterer.set_cluster_params(*self._cluster_params())
            cluster_method = self._clustering_method_drop.value
            self._clusterer.cluster(cluster_method)
            self._page_pos = 0
            self._update_clusters()
        except ValueError:
            self._display_message(self._invalid_param_label)

    def _cluster_params(self) -> Tuple[int, int, int]:
        """
        Retrieve clustering parameters from their respective text boxes.
        """
        ngram = self._ngram_text.value if self._ngram_text.value else DEFAULT_NGRAM
        radius = self._radius_text.value if self._radius_text.value else DEFAULT_RADIUS
        block_size = self._block_chars_text.value if self._block_chars_text else DEFAULT_BLOCK_SIZE
        return int(ngram), int(radius), int(block_size)

    def _select_all(self, change: Dict[str, Any]) -> None:
        """
        Triggered when the select all checkbox is selected or unselected.
        Changes the value of the cluster checkboxes to match the state of the select all checkbox.
        """
        for check in self._checks:
            check.value = change["new"]

    def _cluster_method_change(self, change: Dict[str, Any]) -> None:
        """
        Triggered when the cluster method dropdown state is changed.
        Re-clusters the DataFrame with the new clustering method.
        """
        self._update_dropds(change["new"])
        self._display_message(self._loading_label)
        cluster_method = self._clustering_method_drop.value
        self._clusterer.cluster(cluster_method)
        self._page_pos = 0
        self._update_clusters()

    def _add_next_prev_button_row(
            self, box_children: List[Union[HBox, VBox]]) -> None:
        """
        Adds a next page or previous page button, if the operation is valid.
        """
        next_prev = []
        prev_is_valid = self._page_pos - self._page_size >= 0
        next_is_valid = self._page_pos + self._page_size < len(
            self._clusterer.clusters)

        if prev_is_valid and next_is_valid:
            self._next_button.layout.margin = "0 0 0 628px"
            next_prev.append(self._prev_button)
            next_prev.append(self._next_button)

        elif prev_is_valid:
            next_prev.append(self._prev_button)

        elif next_is_valid:
            self._next_button.layout.margin = "0 0 0 795px"
            next_prev.append(self._next_button)

        if next_is_valid or prev_is_valid:
            box_children.append(HBox(next_prev, layout={"height": "50px"}))

    def _next_page(self, _: Dict[str, Any]) -> None:
        """
        Display the next page of clusters by increasing the page position.
        """
        self._display_message(self._loading_label)
        self._page_pos += self._page_size
        self._update_clusters()
        self._sel_all_on_page()

    def _prev_page(self, _: Dict[str, Any]) -> None:
        """
        Display the previous page of clusters by decreasing the page position.
        """
        self._display_message(self._loading_label)
        self._page_pos -= self._page_size
        self._update_clusters()
        self._sel_all_on_page()

    def _display_message(self, message_label: Label) -> None:
        """
        Display a message to the user, used for the loading screen
        and invalid clustering parameter screen
        """
        self._cluster_vbox.children = [message_label]
        # don't display next and prev buttons
        self._cluster_and_next_prev.children = [
            self._cluster_and_next_prev.children[0]
        ]

    def _sel_all_on_page(self) -> None:
        """
        Select all checkboxes on the current page of clusters if the
        select all checkbox is selected.
        """
        if self._sel_all.value:
            for check in self._checks:
                check.value = True

    def _close(self, _: Dict[str, Any]) -> None:
        """
        Close the UI and display the final dataframe in the next jupyter notebook cell.
        """
        self._clusterer.final_df()
        self._box.close()

    def _execute_merge(self, _: Dict[str, Any]) -> None:
        """
        Merge the selected clusters and re-cluster the dataframe. If the export code checkbox is
        selected the required replace function calls and add them to the jupyter notebook cell.
        """
        self._display_message(self._loading_label)

        do_merge = [check.value for check in self._checks]
        new_values = [text.value for text in self._reprs]
        cluster_page = self._clusterer.get_page(
            self._page_pos, self._page_pos + self._page_size)

        if self._export_code.value:
            self._clusterer.live_export_code(cluster_page, do_merge,
                                             new_values)

        cluster_method = self._clustering_method_drop.value
        self._clusterer.execute_merge_code(cluster_page, do_merge, new_values)
        self._clusterer.cluster(cluster_method)
        self._update_page_if_empty()
        self._update_clusters()

    def _update_page_if_empty(self) -> None:
        """
        Decrease the page if the last page is empty.
        Needed for when all clusters on the last page are merged.
        """
        if self._page_pos >= len(self._clusterer.clusters):
            self._page_pos -= self._page_size

    def display(self) -> Box:
        """Display the UI."""
        return self._box
예제 #18
0
class VideoTakeDashboard:
    def __init__(self):

        self.start_button = Button(
            value=False,
            description='Start Camera',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Start the camera and the recognition algorithm.',
            icon='')

        self.select_network = Dropdown(
            options=KerasDetector.available_models,
            value=KerasDetector.available_models[0],
            description='Algorithm:',
            disabled=False,
        )

        self.label_names = Text(
            value='',
            placeholder='separated by commas',
            description='Labels',
            disabled=False,
        )

        self.num_pictures = IntText(value=2.0,
                                    description='#pictures',
                                    disabled=False,
                                    layout=Layout(width='18%'))

        self.text = Label(value='',
                          layout=Layout(justify_content='space-around', ))

        self.widget_box = VBox((HBox(
            (self.start_button, self.label_names, self.num_pictures,
             self.select_network),
            layout=Layout(justify_content="space-around")), self.text))

        # Initialize field
        self.collector = None

        self.start_button.on_click(self._start_video)

    @property
    def start(self):
        return self.widget_box

    def _start_video(self, _):
        # Reset start-button and notify
        self.start_button.value = False
        self.text.value = "Starting Video! (please wait)"

        # Disable controls
        self.start_button.disabled = True
        self.select_network.disabled = True

        # Get settings
        num_pictures = self.num_pictures.value
        labels = self.label_names.value.split(',')
        num_objects = len(labels)

        self.collector = ImageCollector(num_pictures=num_pictures,
                                        num_objects=num_objects)
        self.collector.run_collector(list_of_labels=labels)

        # Re-enable controls
        self.start_button.disabled = False
        self.select_network.disabled = False

        # Clear output
        self.text.value = ""
예제 #19
0
class Interface():
    '''
    接口类
    '''

    def __init__(self, default):
        self.default = default # 默认配置{catagory,interface}
        self.cf = Config(self.default['interface']) # 配置对象

    def __repr__(self):
        return '[Interface Object: catagoryname = %s , interfacename = %s]' % (self.catagoryname, self.interfacename)

    def on_catagory_change(self, change):
        '''
        更改类别
        :param change: 更改内容
        :return: 
        '''
        if change['type'] == 'change' and change['name'] == 'value':
            debug.out('on_catagory_change event')
            self.catagoryname = change['new']
            self.interfacename = None # 清空接口名称
            self.catagory = interfaces.get(self.catagoryname, None) if self.catagoryname is not None else None # 类别信息
            self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息
            self.interface = None # 清空接口信息
            # 写入配置项
            debug.out('on_catagory_change before write conf\ncatagoryname=%s\ninterfacename=%s' % (
                self.catagoryname, self.interfacename))
            self.cf.writeConf(self.default['interface'], 'catagoryname', self.catagoryname)
            self.cf.writeConf(self.default['interface'], 'interfacename', self.interfacename)
            # 修改界面
            debug.out('on_catagory_change before modify UI')
            self.htmInterface.value = self.choiceResult()
            self.dpInterface.options = [key for key in self.interfaces.keys()]

    def on_interface_change(self, change):
        '''
        更改接口
        :param change: 更改内容
        :return: 
        '''
        if change['type'] == 'change' and change['name'] == 'value':
            debug.out('on_interface_change event')
            self.interfacename = change['new']
            self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息
            self.interface = self.interfaces.get(self.interfacename,
                                                 None) if self.interfaces is not None and self.interfacename is not None else None # 接口信息
            # 写入配置项
            debug.out('on_interface_change before write conf\ninterfacename=%s' % self.interfacename)
            self.cf.writeConf(self.default['interface'], 'interfacename', self.interfacename)
            # 修改界面
            debug.out('on_interface_change before modify UI')
            self.htmInterface.value = self.choiceResult()
            self.dfParams = pd.DataFrame(self.interface['params'], index = [0]).T
            self.dfParams.columns = ['请求参数类型']
            self.gdParam.df = self.dfParams

            self.dfBody = pd.DataFrame(self.interface.get('body', ['无']))
            self.dfBody.columns = ['请求体参数名称']
            self.gdBody.df = self.dfBody

            self.packageWidgets()
            self.packageParams()

    def on_init_clicked(self, b):
        '''
        点击初始化按钮
        :param b: 按钮
        :return: 
        '''
        debug.out('on_init_clicked event')
        self.cf.initConf(self.default['interface'])
        self.cf.writeConf(self.default['interface'], 'catagoryname', self.default['catagory'])
        self.cf.writeConf(self.default['interface'], 'interfacename', self.default['interface'])

    def on_param_change(self, change):
        '''
        参数输入变化
        :param change: 新的参数值
        :return: 
        '''
        if change['type'] == 'change' and change['name'] == 'value':
            self.packageParams()

    def on_execute_clicked(self, b):
        '''
        点击执行请求按钮
        :param b: 按钮
        :return: 
        '''
        debug.out('on_execute_clicked event')
        import requests
        if self.interface['method'] == 'GET':
            self.r = requests.get('http://' + self.env.server + self.interface['url'], params = self.params)
            self.htmExecute.value = '<span style="color:#208FFB">[%s请求执行完成] --> %s</span>' % (
                self.interface['method'], self.r.url)
        elif self.interface['method'] == 'POST':
            if self.interface.get('body', False) != False:
                self.r = requests.post('http://' + self.env.server + self.interface['url'], params = self.params,
                                       data = self.data)
            else:
                self.r = requests.post('http://' + self.env.server + self.interface['url'], params = self.params)
            self.htmExecute.value = '<span style="color:#208FFB">[%s请求执行完成] --> %s</span>' % (
                self.interface['method'], self.r.url)
        else:
            self.htmExecute.value = '<span style="color:red">[%s接口方法填写错误,请求未执行]</span>' % self.interface['method']

        if self.gdResult is not None:
            import json
            output = json.loads(self.r.text)
            resultCode = output['resultCode']
            debug.out('resultCode = %s' % resultCode)
            if resultCode == 1:
                debug.out('response output: %s' % output)
                self.htmResult.value = '<span style="color:#208FFB">[请求成功] 返回数据:</span>'
                for field in self.interface['output']:
                    output = output[field]
                df = pd.DataFrame(output)
                self.gdResult.df = df
                self.gdResult.layout = Layout()
            else:
                self.htmResult.value = output['<span style="color:red">请求失败:%s</span>' % output['resultMsg']]
                self.gdResult.layout = Layout(display = 'none')

    def on_copy_clicked(self, b):
        # pyperclip.copy(self.r.url)
        pass

    def choiceResult(self):
        '''
        获取环境选择结果
        :return: 选择结果
        '''
        try:
            return '<span style="color:#208FFB">当前选择的接口是: [%s]<br>url = %s<br>method = %s<br>output = %s </span>' % (
                self.interface.get('desc', ''), self.interface.get('url', ''), self.interface.get('method', ''),
                self.interface.get('output', ''))
        except:
            return ''

    def packageWidgets(self):
        '''
        打包组件
        :return: 
        '''
        debug.out('package request widgets')
        self.paramWidgets = {}
        self.txtBody = None
        for param, ptype in self.interface['params'].items():
            value = self.cf.readConf(self.default['interface'], param)
            if ptype == 'int':
                pw = IntText(description = param + ':',
                             value = value if value is not None else 0,
                             layout = Layout(width = '90%'))
            elif ptype == 'date':
                pw = DatePicker(description = param + ':',
                                value = value if value is not None else '',
                                layout = Layout(width = '90%'))
            elif ptype == 'companyId':
                pw = Text(description = param + ':',
                          value = value if value is not None else self.env.tester['companyId'],
                          layout = Layout(width = '90%'))
            elif ptype == 'openId':
                pw = Text(description = param + ':',
                          value = value if value is not None else self.env.tester['openId'],
                          layout = Layout(width = '90%'))
            else:
                pw = Text(description = param + ':',
                          value = value if value is not None else '',
                          layout = Layout(width = '90%'))
            pw.observe(self.on_param_change)
            self.paramWidgets[param] = pw

        self.txtBody = Textarea(description = 'body data:', value = '')
        self.txtBody.layout = Layout(display = 'none')

        debug.out(self.interface.get('body', False))
        if self.interface.get('body', False) != False:
            debug.out('interface have body data to package widget')
            value = self.cf.readConf(self.default['interface'], 'body')
            self.txtBody.value = value if value is not None else ''
            self.txtBody.layout = Layout(width = '90%', height = '200px')

        tmpWds = [wd for wd in self.paramWidgets.values()]
        tmpWds.append(self.txtBody)
        self.boxParams.children = tmpWds

    def packageParams(self):
        '''
        打包数据
        :return: 
        '''
        debug.out('package request params')
        self.params = {}
        for param, widget in self.paramWidgets.items():
            self.params[param] = str(widget.value)
            self.cf.writeConf(self.env.default['interface'], param, str(widget.value))
        self.params['appKey'] = self.env.tester['api_key'][self.catagoryname]
        self.params['nonce'] = Tool.generate_nonce()
        self.params['time'] = Tool.generate_time()
        self.params['sign'] = Tool.generate_sign(self.params, self.env.tester['api_secret'][self.catagoryname])

        df = pd.DataFrame(self.params, index = [0]).T
        df.columns = ['参数值']
        self.gdParamValue.df = df

        debug.out(self.interface.get('body', False))
        if self.interface.get('body', False) != False:
            debug.out('interface have body data to package params')
            self.data = self.txtBody.value
            self.cf.writeConf(self.env.default['interface'], 'body', self.txtBody.value)

        else:
            self.data = None
            self.cf.writeConf(self.env.default['interface'], 'body', '')

    def displayInterface(self):
        '''
        显示组件
        :return: 
        '''
        if not self.cf.isReady():
            print(Fore.RED + '配置项尚未初始化!')
            self.btnInit = Button(description = '立即初始化', button_style = 'danger')
            self.btnInit.on_click(self.on_init_clicked)
            display(self.btnInit)
        else:
            self.catagoryname = self.cf.readConf(self.default['interface'], 'catagoryname') # 类别名称
            self.catagoryname = self.default[
                'catagory'] if self.catagoryname is None else self.catagoryname # 未设置时使用默认配置
            self.interfacename = self.cf.readConf(self.default['interface'], 'interfacename') # 接口名称
            self.interfacename = self.default[
                'interface'] if self.interfacename is None else self.interfacename # 未设置时使用默认配置
            self.catagory = interfaces.get(self.catagoryname, None) if self.catagoryname is not None else None # 类别信息
            self.interfaces = self.catagory.get('interface', None) if self.catagory is not None else None # 类别下所有接口信息
            self.interface = self.interfaces.get(self.interfacename,
                                                 None) if self.interfaces is not None and self.interfacename is not None else None # 接口信息
            # 组件初始化
            self.dpCatagory = Dropdown(
                options = [key for key in interfaces.keys()],
                value = self.catagoryname if self.catagoryname is not None else None,
                description = '类别:'
            )
            debug.out('interfacename = %s' % self.interfacename)
            tmpOptions = [key for key in
                          interfaces[self.dpCatagory.value]['interface']] if self.catagory is not None else []
            self.dpInterface = Dropdown(
                options = tmpOptions,
                value = self.interfacename if self.interfacename in tmpOptions else None,
                description = '接口:'
            )

            self.dpCatagory.observe(self.on_catagory_change)
            self.dpInterface.observe(self.on_interface_change)

            self.htmInterface = HTML(value = self.choiceResult())
            self.gdParam = qgrid.show_grid(pd.DataFrame(None))
            self.gdBody = qgrid.show_grid(pd.DataFrame(None))

            if self.interface is not None:
                self.dfParams = pd.DataFrame(self.interface.get('params', ['无']), index = [0]).T
                self.dfParams.columns = ['请求参数类型']
                self.gdParam = qgrid.show_grid(self.dfParams,
                                               grid_options = {'filterable': False, 'editable': False})
                self.gdParam.layout = Layout(width = '90%')

                self.dfBody = pd.DataFrame(self.interface.get('body', ['无']))
                self.dfBody.columns = ['请求体参数名称']
                self.gdBody = qgrid.show_grid(self.dfBody, grid_options = {'filterable': False, 'editable': False})
                self.gdBody.layout = Layout(width = '90%')

            debug.out('display from interface object=%s' % self)
            display(self.dpCatagory)
            display(self.dpInterface)

            boxRequest = Box([VBox([Label(value = '请求参数:'), self.gdParam],
                                   layout = Layout(flex = '1 1 0%', width = 'auto')),
                              VBox([Label(value = '请求体参数:'), self.gdBody],
                                   layout = Layout(flex = '1 1 0%', width = 'auto'))],
                             layout = Layout(flex_flow = 'row', display = 'flex'))
            boxRef = VBox([self.htmInterface, boxRequest])
            acRef = Accordion(children = [boxRef])
            acRef.set_title(0, '接口参考')
            toggleRefDisplay(acRef)
            display(acRef)
            ref.append(acRef)

    def displayPrepare(self, env):
        '''
        准备数据
        :param env: 当前环境
        :return: 
        '''
        self.env = env
        if not self.cf.isReady():
            print(Fore.RED + '配置项尚未初始化!')
        else:
            self.boxParams = VBox()
            self.boxParams.layout = Layout(flex_flow = 'column', display = 'flex')
            self.packageWidgets()
            display(self.boxParams)

            self.gdParamValue = qgrid.show_grid(pd.DataFrame([]),
                                                grid_options = {'filterable': False, 'autoHeight': True,
                                                                'editable': False})
            self.gdParamValue.layout = Layout(width = '90%')
            self.packageParams()
            self.txtBodyValue = Textarea(value = self.data if self.data is not None else '')
            self.txtBodyValue.layout = Layout(width = '90%', height = '200px', margin = '6px 2px 2px 2px')

            boxRequest = Box([
                VBox([Label(value = '请求参数值:'), self.gdParamValue],
                     layout = Layout(flex = '1 1 0%', width = 'auto')),
                VBox([Label(value = '请求体参数值:'), self.txtBodyValue],
                     layout = Layout(flex = '1 1 0%', width = 'auto'))],
                layout = Layout(flex_flow = 'row', display = 'flex'))
            acRef = Accordion(children = [boxRequest])
            acRef.set_title(0, '输入参考')
            toggleRefDisplay(acRef)
            display(acRef)
            ref.append(acRef)

    def displayExecute(self):
        '''
        执行请求
        :return: 
        '''
        if not self.cf.isReady():
            print(Fore.RED + '配置项尚未初始化!')
        elif self.env.tester is not None:
            self.btnExecute = Button(description = '执行请求', button_style = 'primary')
            btnCopy = Button(description = '复制请求链接')

            self.btnExecute.on_click(self.on_execute_clicked)
            btnCopy.on_click(self.on_copy_clicked)

            self.htmExecute = HTML(value = '')
            # boxExecute = VBox([Box([self.btnExecute, btnCopy]), self.htmExecute])
            boxExecute = VBox([self.btnExecute, self.htmExecute])

            display(boxExecute)

    def displayResponse(self):
        '''
        查看结果
        :return: 
        '''
        if not self.cf.isReady():
            print(Fore.RED + '配置项尚未初始化!')
        else:
            self.htmResult = HTML(value = '')
            self.gdResult = qgrid.show_grid(pd.DataFrame([]))
            boxResult = VBox([self.htmResult, self.gdResult])
            display(boxResult)
예제 #20
0
class ParameterEditor(VBox):
    """TODO: Add docstring here
    """

    filename = ''

    _model_name = Unicode('ParameterEditorModel').tag(sync=True)
    _model_module = Unicode(module_name).tag(sync=True)
    _model_module_version = Unicode(module_version).tag(sync=True)
    _view_name = Unicode('ParameterEditorView').tag(sync=True)
    _view_module = Unicode(module_name).tag(sync=True)
    _view_module_version = Unicode(module_version).tag(sync=True)

    __auto_apply = False
    __auto_save = False
    __accordion = None
    __auto_apply_cbx = None
    __auto_save_cbx = None
    __apply_btn = None
    __save_btn = None
    __add_category_btn = None
    __add_category_txt = None

    __lp = None

    def __init__(self, lp, filename='', **kwargs):

        self.__lp = lp
        self.filename = filename
        self.__accordion = Accordion()
        self.__auto_apply_cbx = Checkbox(description='Auto apply')
        self.__auto_save_cbx = Checkbox(description='Auto save')
        self.__apply_btn = Button(description='Apply changes')
        self.__save_btn = Button(description='Save changes')
        self.__add_category_btn = Button(description='Add category')
        self.__add_category_txt = Text(placeholder='category name')
        self.__initialize()

        super().__init__([
            VBox([
                HBox([
                    HBox((self.__apply_btn, self.__auto_apply_cbx)),
                    HBox((self.__save_btn, self.__auto_save_cbx)),
                    HBox((self.__add_category_btn, self.__add_category_txt))
                ],
                     layout=Layout(flex_flow='row wrap', margin='5px')),
                self.__accordion
            ],
                 layout=Layout(margin='5px'))
        ], **kwargs)

    def dumps(self):
        return self.__lp.dumps()

    def __initialize(self):
        if self.__lp.is_valid():
            children, titles = self.__build_gui()
            accordion = Accordion(children)
            for i, title in enumerate(titles):
                accordion.set_title(i, title)
            self.__accordion = accordion

            self.__auto_apply_cbx.observe(self.__on_auto_apply_cbx_change,
                                          names='value')
            self.__auto_save_cbx.observe(self.__on_auto_save_cbx_change,
                                         names='value')
            self.__apply_btn.on_click(
                lambda x: self.on_lpy_context_change(self.__lp.dumps()))
            self.__save_btn.on_click(lambda x: self.__save())
            self.__add_category_btn.on_click(lambda x: self.__add_category(
                self.__add_category_txt.value.strip()))

    def __add_category(self, name):
        if not name or name in self.__lp.get_category_names():
            return

        item_layout = Layout(margin='20px', flex_flow='row wrap')
        acc = self.__accordion
        box_scalars = HBox(layout=item_layout)
        box_curves = HBox(layout=item_layout)

        acc.children = (*self.__accordion.children,
                        VBox([
                            self.__menu('scalars', box_scalars, name),
                            box_scalars,
                            self.__menu('curves', box_curves, name), box_curves
                        ]))
        acc.set_title(len(acc.children) - 1, name)
        self.__lp.add_category(name)
        if self.__auto_save:
            self.__save()

    def __on_auto_apply_cbx_change(self, change):
        self.__auto_apply = change['owner'].value
        self.__apply_btn.disabled = self.__auto_apply
        if self.__auto_apply:
            self.on_lpy_context_change(self.__lp.dumps())

    def __on_auto_save_cbx_change(self, change):
        self.__auto_save = change['owner'].value
        self.__save_btn.disabled = self.__auto_save
        if self.__auto_save:
            self.__save()

    def on_lpy_context_change(self, context):
        pass

    def __build_gui(self):

        children = []
        titles = ['materials']
        item_layout = Layout(margin='20px', flex_flow='row wrap')

        if self.__lp:

            box_materials = HBox(layout=item_layout)

            for index, color in self.__lp.get_colors().items():
                editor = make_color_editor(color, index, no_name=True)
                if editor:
                    editor.observe(self.__on_editor_changed(color))
                    box_materials.children = (*box_materials.children, editor)

            children.append(
                VBox([self.__menu('materials', box_materials), box_materials]))

            for category_name in self.__lp.get_category_names():

                box_scalars = HBox(layout=item_layout)
                box_curves = HBox(layout=item_layout)

                for scalar in self.__lp.get_category_scalars(category_name):

                    editor = make_scalar_editor(scalar, False)
                    if editor:
                        editor.observe(self.__on_editor_changed(scalar))
                        box_scalars.children = (*box_scalars.children, editor)

                for param in self.__lp.get_category_graphicalparameters(
                        category_name):

                    editor = make_curve_editor(param, False)
                    if editor:
                        editor.observe(self.__on_editor_changed(param))
                        box_curves.children = (*box_curves.children, editor)

                children.append(
                    VBox([
                        self.__menu('scalars', box_scalars,
                                    category_name), box_scalars,
                        self.__menu('curves', box_curves, category_name),
                        box_curves
                    ]))
                titles.append(category_name)

        return (children, titles)

    def __menu(self, parameter_type, box, category_name=None):

        btn_add = Button(description='Add')
        ddn_add = Dropdown()

        if parameter_type == 'scalars':

            def fn_add(self):

                value = None

                if ddn_add.value == 'Integer':
                    value = 1
                elif ddn_add.value == 'Float':
                    value = 1.0
                elif ddn_add.value == 'Bool':
                    value = True

                name = f'{ddn_add.value}_{len(box.children)}'
                scalar = self.__lp.add_scalar(name,
                                              value,
                                              category=category_name)
                editor = make_scalar_editor(scalar)
                editor.observe(self.__on_editor_changed(scalar))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Integer', 'Float', 'Bool']

        elif parameter_type == 'materials':

            def fn_add(self):

                index = max(self.__lp.get_colors().keys()) + 1 if len(
                    self.__lp.get_colors().keys()) else 0

                self.__lp.set_color(
                    index, pgl.Material(ambient=(80, 80, 80), diffuse=1))
                editor = make_color_editor(self.__lp.get_color(index),
                                           index,
                                           no_name=True)
                editor.observe(
                    self.__on_editor_changed(self.__lp.get_color(index)))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Color']

        elif parameter_type == 'curves':

            def fn_add(self):

                name = f'{ddn_add.value}_{len(box.children)}'
                if ddn_add.value == 'Function':
                    curve = self.__lp.add_function(name,
                                                   category=category_name)
                else:
                    curve = self.__lp.add_curve(name, category=category_name)
                editor = make_curve_editor(curve)
                editor.observe(self.__on_editor_changed(curve))
                box.children = (*box.children, editor)

                if self.__auto_save:
                    self.__save()
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())

            ddn_add.options = ['Curve', 'Function']

        btn_add.on_click(lambda x: fn_add(self))

        return HBox((btn_add, ddn_add))

    def __validate_name(self, name):
        if not _property_name_regex.match(name):
            return False
        if name in self.lpy_context:
            return False
        for key in self.lpy_context.keys():
            if name in self.lpy_context[key]:
                return False
        return True

    def __save(self):
        if self.filename:
            params = self.__lp.dumps()
            with io.open(self.filename, 'w') as file:
                file.write(json.dumps(json.loads(params), indent=4))

    def __on_editor_changed(self, param):
        def on_param_changed(change):
            if 'new' in change:
                value = change['new']
                name = change['name']
                if isinstance(param, BaseScalar):
                    if name == 'name':
                        param.name = value
                    else:
                        param.value = value
                elif isinstance(param, tuple):
                    if name == 'value':
                        if isinstance(param[1],
                                      (pgl.NurbsCurve2D, pgl.BezierCurve2D)):
                            param[1].ctrlPointList = pgl.Point3Array(
                                [pgl.Vector3(p[0], p[1], 1) for p in value])
                        elif isinstance(param[1], pgl.Polyline2D):
                            param[1].pointList = pgl.Point2Array(
                                [pgl.Vector2(p[0], p[1]) for p in value])
                    else:
                        param[1].name = value
                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())
                if self.__auto_save:
                    self.__save()

        def on_material_changed(change):
            if 'new' in change:
                if change['name'] == 'index':
                    self.__lp.unset_color(change['old'])
                    self.__lp.set_color(change['new'], param)
                else:
                    setattr(param, change['name'], change['new'])

                if self.__auto_apply:
                    self.on_lpy_context_change(self.__lp.dumps())
                if self.__auto_save:
                    self.__save()

        return on_material_changed if isinstance(
            param, pgl.Material) else on_param_changed
예제 #21
0
class SoundDemo1Dashboard2:
    def __init__(self):
        """
        :param int i: 
        """
        self.test_sound = None
        self.test_data = None
        self.select_test_data_options = None

        self.select_test_data = Dropdown(
            options={'Record test data': 0,
                     'Load test data files': 1
                     },
            value=0,
            description='Choose training data:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '160px'},
        )

        self.select_test_data.observe(self.on_change)

        self.select_nseconds = Dropdown(
            options={
                '2s': 2,
                '3s': 3,
                '5s': 5
            },
            value=2,
            description='Choose recording length:',
            disabled=False,
            layout=Layout(width='400px', display='block'),
            style={'description_width': '160px'},
        )

        self.container = Output(
            value="",
        )

        self.submit_button = Button(
            value=False,
            description='Get test data',
            disabled=False,
            button_style='success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='TEST: use recorded sounds or record new',
            icon=''
        )

        self.play_button = Button(
            value=False,
            description='Play the recording',
            disabled=False,
            button_style='info',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='TEST: play recorded or loaded sounds',
            icon=''
        )

        self.widget_box = VBox(
            (
                HBox(
                    (
                        VBox((
                            self.select_test_data,
                            self.select_nseconds
                        ), ),
                        VBox((
                            self.submit_button,
                            self.play_button
                        ), )
                    ),
                ),
                self.container
            ),
        )

        self.submit_button.on_click(self._run)
        self.play_button.on_click(self._playback)

    @property
    def start(self):
        return self.widget_box

    def on_change(self, change):
        if change['type'] == 'change' and change['name'] == 'value':
            if change["new"] != 0:
                self.select_nseconds.layout.display = 'none'
                self.select_nseconds.disabled = True
            else:
                self.select_nseconds.layout.display = 'block'
                self.select_nseconds.disabled = False
            # print("changed to {}".format(change['new']))

    def _playback(self, v):
        if self.test_sound is not None:
            # Instantiate miniRecorder
            self.rec = miniRecorder(seconds=1.5)
            self.rec.data = self.test_data
            self.rec.sound = self.test_sound
            if self.test_data is None:
                self.rec.playback(format=pyaudio.paFloat32)
            else:
                self.rec.playback()
        else:
            print("No test sound has been loaded. Please load the test sound first.")

    def _run(self, v):
        self.prefix = "test"
        self.test_filename = "test.wav"
        self.test_sound = None
        self.test_data = None

        # If we choose to record the data, then we record it here
        if self.select_test_data.value == 0:

            # Remove the data with the same prefix
            file_ext = self.prefix + "*.wav"
            files = glob.glob(os.path.join(WAV_DIR, file_ext))
            for file in files:
                if self.prefix in file:
                    os.remove(file)
            # Instantiate miniRecorder
            self.rec = miniRecorder(seconds=1.5)
            # Start recording
            _ = self.rec.record()
            self.test_sound = self.rec.sound
            self.test_data = self.rec.data
            self.rec.write2file(fname=os.path.join(WAV_DIR, self.test_filename))
        else:
            file = glob.glob(os.path.join(WAV_DIR, self.test_filename))
            if file:
                try:
                    (sound_clip, fs) = librosa.load(os.path.join(WAV_DIR, self.test_filename), sr=22050)
                    self.test_sound = np.array(sound_clip)
                    print("File loaded successfully.")
                except Exception:
                    print("File loading has not succeeded.")
            else:
                print("No file named {} has been found in {}".format(self.test_filename, WAV_DIR))

        return
예제 #22
0
D_widget = IntSlider(min=10, max=100, step=1, value=50)
p_widget = FloatSlider(min=0.0, max=1.0, step=0.1, value=0.5)

P_widget = IntSlider(min=5, max=50, step=5, value=20)
A_widget = FloatSlider(min=0.0, max=1.0, step=0.1, value=0.5)

params = interact(set_params,
                  nn=nn_widget,
                  np=np_widget,
                  D=D_widget,
                  p=p_widget,
                  P=P_widget,
                  A=A_widget)

button_init = Button(description="Init", on_click=init)
button_init.on_click(init)
button_init

##########
"""
Button(
    description='Click me',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
    icon='check' # (FontAwesome names without the `fa-` prefix)
)
"""

#button_init = Button(description="Init")
#button_init.on_click(init)
예제 #23
0
def mk_map_region_selector(map=None, height="600px", **kwargs):
    from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl
    from ipywidgets.widgets import Layout, Button, HTML
    from types import SimpleNamespace

    state = SimpleNamespace(selection=None, bounds=None, done=False)

    btn_done = Button(description="done", layout=Layout(width="5em"))
    btn_done.style.button_color = "green"
    btn_done.disabled = True

    html_info = HTML(layout=Layout(flex="1 0 20em", width="20em", height="3em"))

    def update_info(txt):
        html_info.value = '<pre style="color:grey">' + txt + "</pre>"

    def render_bounds(bounds):
        (lat1, lon1), (lat2, lon2) = bounds
        txt = "lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]".format(
            lat1, lat2, lon1, lon2, n=4
        )
        update_info(txt)

    if map is None:
        m = Map(**kwargs) if len(kwargs) else Map(zoom=2)
        m.scroll_wheel_zoom = True
        m.layout.height = height
    else:
        m = map
        render_bounds(m.bounds)

    widgets = [
        WidgetControl(widget=btn_done, position="topright"),
        WidgetControl(widget=html_info, position="bottomleft"),
    ]
    for w in widgets:
        m.add_control(w)

    draw = DrawControl()

    draw.circle = {}
    draw.polyline = {}
    draw.circlemarker = {}

    shape_opts = {"fillColor": "#fca45d", "color": "#000000", "fillOpacity": 0.1}
    draw.rectangle = {"shapeOptions": shape_opts, "metric": ["km", "m"]}
    poly_opts = {"shapeOptions": dict(**shape_opts)}
    poly_opts["shapeOptions"]["original"] = dict(**shape_opts)
    poly_opts["shapeOptions"]["editing"] = dict(**shape_opts)

    draw.polygon = poly_opts
    draw.edit = True
    draw.remove = True
    m.add_control(draw)
    m.add_control(FullScreenControl())

    def on_done(btn):
        state.done = True
        btn_done.disabled = True
        m.remove_control(draw)
        for w in widgets:
            m.remove_control(w)

    def bounds_handler(event):
        bounds = event["new"]
        render_bounds(bounds)
        (lat1, lon1), (lat2, lon2) = bounds
        state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2))

    def on_draw(event):
        v = event["new"]
        action = event["name"]
        if action == "last_draw":
            state.selection = v["geometry"]
        elif action == "last_action" and v == "deleted":
            state.selection = None

        btn_done.disabled = state.selection is None

    draw.observe(on_draw)
    m.observe(bounds_handler, ("bounds",))
    btn_done.on_click(on_done)

    return m, state
class PrimeMinisterSpeechDashboard:
    def __init__(self):
        self.afinn = Afinn(language="da")
        self.speeches_names = [('2018 (Lars Løkke Rasmussen)', '2018'),
                               ('2017 (Lars Løkke Rasmussen)', '2017'),
                               ('2016 (Lars Løkke Rasmussen)', '2016'),
                               ('2015 (Lars Løkke Rasmussen)', '2015'),
                               ('2014 (Helle Thorning-Schmidt)', '2014'),
                               ('2013 (Helle Thorning-Schmidt)', '2013'),
                               ('2012 (Helle Thorning-Schmidt)', '2012'),
                               ('2011 (Helle Thorning-Schmidt)', '2011'),
                               ('2010 (Lars Løkke Rasmussen)', '2010'),
                               ('2009 (Lars Løkke Rasmussen)', '2009'),
                               ('2008 (Anders Fogh Rasmussen)', '2008'),
                               ('2007 (Anders Fogh Rasmussen)', '2007'),
                               ('2006 (Anders Fogh Rasmussen)', '2006'),
                               ('2005 (Anders Fogh Rasmussen)', '2005'),
                               ('2004 (Anders Fogh Rasmussen)', '2004'),
                               ('2003 (Anders Fogh Rasmussen)', '2003'),
                               ('2002 (Anders Fogh Rasmussen)', '2002'),
                               ('2001 (Poul Nyrup Rasmussen)', '2001'),
                               ('2000 (Poul Nyrup Rasmussen)', '2000'),
                               ('1999 (Poul Nyrup Rasmussen)', '1999'),
                               ('1998 (Poul Nyrup Rasmussen)', '1998'),
                               ('1997 (Poul Nyrup Rasmussen)', '1997')]
        self.speeches = {}
        self.speeches_sentiments = {}

        self.select = Dropdown(
            options={
                '2018 (Lars Løkke Rasmussen)': 0,
                '2017 (Lars Løkke Rasmussen)': 1,
                '2016 (Lars Løkke Rasmussen)': 2,
                '2015 (Lars Løkke Rasmussen)': 3,
                '2014 (Helle Thorning-Schmidt)': 4,
                '2013 (Helle Thorning-Schmidt)': 5,
                '2012 (Helle Thorning-Schmidt)': 6,
                '2011 (Helle Thorning-Schmidt)': 7,
                '2010 (Lars Løkke Rasmussen)': 8,
                '2009 (Lars Løkke Rasmussen)': 9,
                '2008 (Anders Fogh Rasmussen)': 10,
                '2007 (Anders Fogh Rasmussen)': 11,
                '2006 (Anders Fogh Rasmussen)': 12,
                '2005 (Anders Fogh Rasmussen)': 13,
                '2004 (Anders Fogh Rasmussen)': 14,
                '2003 (Anders Fogh Rasmussen)': 15,
                '2002 (Anders Fogh Rasmussen)': 16,
                '2001 (Poul Nyrup Rasmussen)': 17,
                '2000 (Poul Nyrup Rasmussen)': 18,
                '1999 (Poul Nyrup Rasmussen)': 19,
                '1998 (Poul Nyrup Rasmussen)': 20,
                '1997 (Poul Nyrup Rasmussen)': 21
            },
            value=0,
            description='Vælg talen:',
            disabled=False,
            layout=Layout(width='400px'),
            style={'description_width': '100px'},
        )

        self.container = Output(value="", )

        self.submit_button = Button(
            value=False,
            description='Indlæs talen',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Indlæs statsministerens tale og lav sentiment-analyse',
            icon='')

        self.widget_box = VBox(
            (self.select, self.submit_button, self.container), )

        self.submit_button.on_click(self._do_sentiment_analysis)

    @property
    def start(self):
        return self.widget_box

    def load_speeches(self):
        for filepath in glob.iglob('data/statsminister/*.txt'):
            self.speeches[os.path.basename(filepath).replace(".txt", "")] = [
                line.rstrip('\n')
                for line in open(filepath, mode="r", encoding="utf-8")
            ]
            current_sentiment = 0
            for line in self.speeches[os.path.basename(filepath).replace(
                    ".txt", "")]:
                current_sentiment += self.afinn.score(line)
            self.speeches_sentiments[os.path.basename(filepath).replace(
                ".txt", "")] = current_sentiment

    def _do_sentiment_analysis(self,
                               number_of_averaged_scores=10,
                               smoothing_constant=0.9,
                               use_exp_smoothing=True,
                               use_imputation=True,
                               speech_number=None):
        if speech_number:
            current_speech = self.speeches_names[speech_number][1]
            current_speech_title = self.speeches_names[speech_number][0]
        else:
            current_speech = self.speeches_names[self.select.value][1]
            current_speech_title = self.speeches_names[self.select.value][0]
        scores = []
        for i in range(len(self.speeches[current_speech])):
            scores.append(self.afinn.score(self.speeches[current_speech][i]))

        # Dataframe
        pd.set_option('display.max_colwidth',
                      -1)  # Used to display whole title (non-truncated)
        df = pd.DataFrame({
            "Line": self.speeches[current_speech],
            "Score": scores
        })  # Creating the data frame and populating it

        # Highlight the positive and negative sentiments
        def highlight(s):
            if s.Score > 0:
                return ['background-color: #AAFFAA'] * 2
            elif s.Score < 0:
                return ['background-color: #FFAAAA'] * 2
            else:
                return ['background-color: #FFFFFF'] * 2

        df = df.style.apply(highlight, axis=1)

        # Imputation - using previous nonzero score instead of zero
        running_score = 0
        imputed_scores = []
        for i in range(len(scores)):
            if scores[i] != 0:
                running_score = scores[i]
                imputed_scores.append(scores[i])
            else:
                imputed_scores.append(running_score)

        smoothed_scores = []

        if not use_imputation:
            imputed_scores = scores

        for i in range(len(imputed_scores)):
            if use_exp_smoothing:  # Exp smoothing
                if i == 0:
                    smoothed_scores.append(imputed_scores[i])
                else:
                    smoothed_scores.append(imputed_scores[i - 1] *
                                           (1 - smoothing_constant) +
                                           imputed_scores[i] *
                                           smoothing_constant)
            else:  # Arithmetic smoothing
                s = 0
                if i > number_of_averaged_scores:
                    n = number_of_averaged_scores
                else:
                    n = i + 1

                for j in range(n):
                    s = s + imputed_scores[i - j]
                smoothed_scores.append(s / n)

        # Data for plotting
        y = np.array(smoothed_scores)
        x = np.array(range(1, len(smoothed_scores) + 1))
        x_s = np.linspace(
            x.min(), x.max(), 1800
        )  #300 represents number of points to make between T.min and T.max
        y_s = spline(x, y, x_s)

        fig, ax = plt.subplots(figsize=(20, 10))
        ax.plot(x_s, y_s, color="black", linewidth=3)

        ax.tick_params(labelsize=13)
        ax.set_xlabel('Tid', fontsize=16)
        ax.set_ylabel('Sentiment', fontsize=15)
        ax.set_title(
            'Statsministeren\'s tale: {}'.format(current_speech_title),
            fontsize=18)

        # use xnew, ynew to plot filled-color graphs
        plt.fill_between(x_s, 0, y_s, where=(y_s - 1) < -1, color='#E7D1AC')
        plt.fill_between(x_s, 0, y_s, where=(y_s - 1) > -1, color='#A8D2D1')

        ax.grid()
        plt.show()

        display(df)
        return
예제 #25
0
class VideoRecognitionDashboard:
    def __init__(self):

        self.start_button = Button(
            value=False,
            description='Start Camera',
            disabled=False,
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Start the camera and the recognition algorithm.',
            icon='')

        self.use_recorded = RadioButtons(
            options=['Webcam', 'MP4'],
            value='Webcam',
            description='Input',
            disabled=False,
            layout=Layout(width='320px'),
            style={'description_width': '150px'},
        )

        self.video_path = Text(
            value='',
            placeholder=str(Path("path", "to", "video.mp4")),
            description='Video path:',
            disabled=True,
            layout=Layout(width='320px'),
            style={'description_width': '150px'},
        )

        self.select_network = Dropdown(
            options=KerasDetector.available_models,
            value=KerasDetector.available_models[0],
            description='Algorithm:',
            disabled=False,
            layout=Layout(width='220px'),
            style={'description_width': '100px'},
        )

        self.select_frontend = Dropdown(
            options=["opencv", "matplotlib"],
            value="opencv",
            description='Frontend:',
            disabled=False,
            layout=Layout(width='220px', padding='8px 0 0 0'),
            style={'description_width': '100px'},
        )

        self.video_length = FloatText(
            value=12.0,
            description='Video length [s]:',
            disabled=False,
            layout=Layout(width='250px'),
            style={'description_width': '130px'},
        )

        self.select_framerate = Dropdown(
            options=[3, 5, 10, 15, 24],
            value=15,
            description='Frame rate:',
            disabled=False,
            layout=Layout(width='250px', padding='8px 0 0 0'),
            style={'description_width': '130px'},
        )

        self.static_text = Label(
            value="Working directory: {}".format(Path.cwd()),
            layout=Layout(margin='30px 0 0 0'),
        )
        self.progress_text = Label(value='', )
        self.text_box = VBox((self.static_text, self.progress_text),
                             layout=Layout(justify_content="flex-start"))

        self.widget_box = VBox(
            (HBox((VBox((self.start_button, ),
                        layout=Layout(justify_content="space-around")),
                   VBox((self.use_recorded, self.video_path),
                        layout=Layout(justify_content="space-around")),
                   VBox((self.video_length, self.select_framerate),
                        layout=Layout(justify_content="space-around")),
                   VBox((self.select_network, self.select_frontend),
                        layout=Layout(justify_content="space-around"))), ),
             HBox((self.text_box, ),
                  layout=Layout(justify_content="flex-start"))), )

        self.start_button.on_click(self._start_video)
        self.use_recorded.observe(self.refresh_state)

    @property
    def start(self):
        return self.widget_box

    def refresh_state(self, _=None):
        use_recorded = self.use_recorded.value == "MP4"

        self.video_length.disabled = use_recorded
        self.video_path.disabled = not use_recorded
        self.video_length.disabled = use_recorded

    def _start_video(self, _):
        # Reset start-button and notify
        self.start_button.value = False
        self.progress_text.value = "Starting Video! (please wait)"

        # Disable controls
        self.start_button.disabled = True
        self.select_network.disabled = True
        self.video_length.disabled = True
        self.use_recorded.disabled = True
        self.video_path.disabled = True

        # Get settings
        model_name = self.select_network.value
        video_length = self.video_length.value
        selected_frontend = self.select_frontend.value
        selected_framerate = self.select_framerate.value

        # Make network
        net = KerasDetector(model_specification=model_name,
                            exlude_animals=True)

        # Start network
        video_path = None
        if self.use_recorded.value == "MP4":
            video_path = self.video_path.value
            video_length = 120
        #the_video = LabelledVideo(net, backend=selected_backend, frame_rate = selected_framerate, video_length=video_length, video_path=video_path)
        the_video = VideoLoop(model=net,
                              regime="object_detection",
                              frontend=selected_frontend,
                              frame_rate=selected_framerate,
                              video_length=video_length,
                              video_path=video_path)
        self.progress_text.value = "Video running!"
        the_video.start()

        # Re-enable controls
        self.start_button.disabled = False
        self.select_network.disabled = False
        self.video_length.disabled = False
        self.use_recorded.disabled = False
        self.video_path.disabled = False

        # Clear output
        self.progress_text.value = "Done."