def display_handler(self, *_):

        if len(self.data) == 0:
            self.alert("No data!")
            return

        self._output.clear_output()
        with self._output:
            if self.output_format.lower() == 'chart':
                top_tokens: pd.DataFrame = self.data.assign(
                    xs=self.data.index, ys=self.data.weight)
                p = plot_topic_word_distribution(
                    top_tokens,
                    plot_width=1200,
                    plot_height=500,
                    title='',
                    tools='box_zoom,wheel_zoom,pan,reset')
                bokeh.plotting.show(p)
            else:
                if self.output_format.lower() in ('xlsx', 'csv', 'clipboard'):
                    utility.ts_store(data=self.data,
                                     extension=self.output_format.lower(),
                                     basename='topic_word_distribution')
                g: gu.TableWidget = gu.table_widget(self.data)
                display(g)
        self.alert("✅")
def default_displayer(opts: "TopicsTokenNetworkGUI") -> None:

    if opts.model.top_topic_tokens is None:
        return

    topics_tokens = opts.model.get_topics_tokens(opts.topics_ids,
                                                 opts.top_count)

    if opts.output_format == "network":
        network = create_network(topics_tokens)
        opts.network = network
        opts.set_layout()
        css_style = css_styles(topics_tokens.topic_id.unique(),
                               opts.custom_styles)
        network.set_style(css_style)
        display(network)

    if opts.output_format.lower() in ('table', 'xlsx', 'csv', 'clipboard',
                                      'gephi'):

        if opts.output_format == "gephi":
            topics_tokens = topics_tokens[['topic', 'token', 'weight']]
            topics_tokens.columns = ['Source', 'Target', 'Weight']

        if opts.output_format != "table":
            utility.ts_store(data=topics_tokens,
                             extension=opts.output_format.lower(),
                             basename='topics_token_network')

        g = gu.table_widget(topics_tokens)
        display(g)
    def update_handler(self, *_):

        self.alert("⌛ Computing...")
        self._output.clear_output()

        try:
            with self._output:

                try:
                    weights: pd.DataFrame = self.update()
                except pu.EmptyDataError:
                    weights = None

                if weights is None:
                    self.alert("😡 No data, please change filters..")
                elif self.output_format in ('xlsx', 'csv', 'clipboard'):
                    pu.ts_store(data=weights,
                                extension=self.output_format,
                                basename='heatmap_weights')
                elif self.output_format == "table":
                    g = gu.table_widget(weights)
                    display(g)
                else:
                    display_heatmap(
                        weights,
                        self.titles,
                        flip_axis=self._flip_axis.value,
                        aggregate=self.aggregate,
                        output_format=self.output_format,
                    )
            self.alert("✅")
        except Exception as ex:
            self.warn(f"😡 {ex}")
示例#4
0
def display_wordcloud(
    inferred_topics: tm.InferredTopicsData,
    topic_id: int = 0,
    n_words: int = 100,
    output_format: str = 'Wordcloud',
):

    tokens = inferred_topics.get_topic_title(topic_id, n_tokens=n_words)

    if len(tokens) == 0:
        raise ValueError("No data! Please change selection.")

    if output_format == 'Wordcloud':
        plot_utility.plot_wordcloud(inferred_topics.get_topic_tokens(topic_id),
                                    token='token',
                                    weight='weight',
                                    max_words=n_words,
                                    **PLOT_OPTS)
    else:
        top_tokens: pd.DataFrame = inferred_topics.get_topic_top_tokens(
            topic_id=topic_id, n_tokens=n_words)
        g = gu.table_widget(top_tokens)
        display(g)
        if output_format.lower() in ('xlsx', 'csv', 'clipboard'):
            utility.ts_store(data=top_tokens,
                             extension=output_format.lower(),
                             basename='topic_top_tokens')
示例#5
0
def display_heatmap(
    weights: pd.DataFrame,
    titles: pd.DataFrame,
    key: str = 'max',  # pylint: disable=unused-argument
    flip_axis: bool = False,
    glyph: str = 'Circle',  # pylint: disable=unused-argument
    aggregate: str = None,
    output_format: str = None,
):
    ''' Display aggregate value grouped by year  '''
    try:

        weights['weight'] = weights[aggregate]
        weights['year'] = weights.year.astype(str)
        weights['topic_id'] = weights.topic_id.astype(str)

        if len(weights) == 0:
            print("No data! Please change selection.")
            return

        if output_format.lower() == 'heatmap':

            p = plot_topic_relevance_by_year(
                weights,
                xs='year',
                ys='topic_id',
                flip_axis=flip_axis,
                titles=titles,
                element_id='topic_relevance',
                **HEATMAP_FIGOPTS,
            )

            bokeh.plotting.show(p)

        elif output_format.lower() in ('xlsx', 'csv', 'clipboard'):
            pu.ts_store(data=weights,
                        extension=output_format.lower(),
                        basename='heatmap_weights')
        else:
            g = gu.table_widget(weights)
            display(g)

    except Exception as ex:
        # raise
        logger.error(ex)
示例#6
0
 def display_handler(self, *_):
     self._output.clear_output()
     try:
         with self._output:
             if self.yearly_topic_weights is None:
                 self.alert("😡 No data, please change filters..")
             elif self.output_format in ('xlsx', 'csv', 'clipboard'):
                 pu.ts_store(data=self.yearly_topic_weights,
                             extension=self.output_format,
                             basename='heatmap_weights')
             elif self.output_format == "pandas":
                 with pd.option_context("display.max_rows", None,
                                        "display.max_columns", None):
                     display(self.yearly_topic_weights)
             elif self.output_format == "table":
                 g = gu.table_widget(self.yearly_topic_weights,
                                     handler=self.click_handler)
                 display(g)
             else:
                 self.topic_changed()
         self.alert("✅")
     except Exception as ex:
         self.warn(f"😡 {ex}")
    def display_handler(self, *_):

        self._output.clear_output()

        with self._output:

            if self.network_data is None:

                self.alert("😡 No data, please change filters..")

            elif self.output_format in ('xlsx', 'csv', 'clipboard', 'table',
                                        'gephi'):

                data: pd.DataFrame = self.network_data

                if self.output_format == "gephi":
                    data = data[['topic_id', "title", 'weight']]
                    data.columns = ['Source', 'Target', 'Weight']

                if self.output_format != "table":
                    pu.ts_store(data=data,
                                extension=self.output_format,
                                basename='heatmap_weights')

                g: gu.TableWidget = gu.table_widget(data)
                display(g)
            else:
                nx.plot_highlighted_bipartite_dataframe(
                    network_data=self.network_data,
                    network_layout=self.network_layout,
                    highlight_topic_ids=self.highlight_ids,
                    titles=self.titles,
                    scale=self.scale,
                    source_name="title",  # FIXME:self.picked_pivot_name
                    target_name="topic_id",
                    element_id=self.text_id,
                )
    def display_handler(self, *_):

        self._output.clear_output()

        with self._output:

            if self.network_data is None:

                self.alert("😡 No data, please change filters..")

            elif self.output_format in ('xlsx', 'csv', 'clipboard', 'table', 'gephi'):

                data: pd.DataFrame = self.network_data

                if self.output_format == "gephi":
                    data = data[['topic_id', "title", 'weight']]
                    data.columns = ['Source', 'Target', 'Weight']

                if self.output_format != "table":
                    pu.ts_store(data=data, extension=self.output_format, basename='heatmap_weights')

                g: gu.TableWidget = gu.table_widget(data)
                display(g)

            else:

                display_topic_topic_network(
                    data=self.network_data,
                    layout=self._network_layout.value,
                    titles=self.titles,
                    scale=self._scale.value,
                    element_id=self.text_id,
                    node_range=self._node_range.value,
                    edge_range=self._edge_range.value,
                    topic_proportions=self.topic_proportions,
                )