Ejemplo n.º 1
0
def test_get_response_matrix_2():
    chat = WhatsAppChat.from_source(filename)
    df_resp = get_response_matrix(chat=chat, zero_own=False)

    # Check shape and colnames of returned dataframe
    n_users = len(chat.users)
    assert(df_resp.shape == (n_users, n_users))
    assert(set(chat.users) == set(df_resp.columns))
Ejemplo n.º 2
0
def test_get_response_matrix_1():
    chat = WhatsAppChat.from_source(filename)
    df_resp = get_response_matrix(chat=chat, zero_own=True)

    # Check shape and colnames of returned dataframe
    n_users = len(chat.users)
    assert(df_resp.shape == (n_users, n_users))
    assert(set(chat.users) == set(df_resp.columns))

    # Check diagonal of returned dataframe is zero
    assert(all([df_resp.loc[user, user] == 0 for user in df_resp.columns]))
Ejemplo n.º 3
0
def test_get_response_matrix_5():
    chat = WhatsAppChat.from_source(filename)
    df_resp = get_response_matrix(chat=chat, norm='receiver')

    # Check shape and colnames of returned dataframe
    n_users = len(chat.users)
    assert(df_resp.shape == (n_users, n_users))
    assert(set(chat.users) == set(df_resp.columns))

    # Check scaling has been done correct
    assert(all([math.isclose(x, 1) for x in df_resp.sum(axis=0)]))
Ejemplo n.º 4
0
def test_get_response_matrix_3():
    chat = WhatsAppChat.from_source(filename)
    df_resp = get_response_matrix(chat=chat, norm='joint')

    # Check shape and colnames of returned dataframe
    n_users = len(chat.users)
    assert(df_resp.shape == (n_users, n_users))
    assert(set(chat.users) == set(df_resp.columns))

    # Check scaling has been done correct
    assert(math.isclose(df_resp.sum().sum(), 1))
Ejemplo n.º 5
0
    def user_message_responses_flow(self, title="Message flow"):
        """Get the flow of message responses.

        A response from user X to user Y happens if user X sends a message right after a message from user Y.

        Uses a Sankey diagram.

        Args:
            title (str, optional): Title for plot. Defaults to "Message flow".

        Returns:
            plotly.graph_objs.Figure: Plotly Figure.

        ..  seealso::

            * :func:`get_response_matrix <whatstk.analysis.get_response_matrix>`
            * :func:`fig_sankey <whatstk.graph.figures.sankey.fig_sankey>`

        Example:
            ..  code-block:: python

                >>> from whatstk import WhatsAppChat
                >>> from whatstk.graph import plot, FigureBuilder
                >>> from whatstk.data import whatsapp_urls
                >>> chat = WhatsAppChat.from_source(filepath=whatsapp_urls.LOREM)
                >>> fig = FigureBuilder(chat=chat).user_message_responses_flow()
                >>> plot(fig)

        """
        # Get response matrix
        responses = get_response_matrix(self.df)

        # Node lists
        label = self.usernames * 2
        color = list(self.user_color_mapping.values()) * 2
        # Link lists
        n_users = len(self.usernames)
        source = np.repeat(np.arange(n_users), n_users).tolist()
        target = np.arange(n_users, 2 * n_users).tolist() * n_users
        value = responses.values.flatten().tolist()

        # Get figure
        fig = fig_sankey(label=label,
                         color=color,
                         source=source,
                         target=target,
                         value=value,
                         title=title)
        return fig
Ejemplo n.º 6
0
    def user_message_responses_heatmap(self,
                                       norm=NORMS.ABSOLUTE,
                                       title="Response matrix"):
        """Get the response matrix heatmap.

        A response from user X to user Y happens if user X sends a message right after a message from user Y.

        Args:
            norm (str, optional): Specifies the type of normalization used for reponse count. Can be:

                                - ``'absolute'``: Absolute count of messages.
                                - ``'joint'``: Normalized by total number of messages sent by all users.
                                - ``'sender'``: Normalized per sender by total number of messages sent by user.
                                - ``'receiver'``: Normalized per receiver by total number of messages sent by user.
            title (str, optional): Title for plot. Defaults to "Response matrix".

        Returns:
            plotly.graph_objs.Figure: Plotly Figure.

        ..  seealso::

            * :func:`get_response_matrix <whatstk.analysis.get_response_matrix>`
            * :func:`fig_heatmap <whatstk.graph.figures.heatmap.fig_heatmap>`

        Example:
            ..  code-block:: python

                >>> from whatstk import WhatsAppChat
                >>> from whatstk.graph import plot, FigureBuilder
                >>> from whatstk.data import whatsapp_urls
                >>> chat = WhatsAppChat.from_source(filepath=whatsapp_urls.LOREM)
                >>> fig = FigureBuilder(chat=chat).user_message_responses_heatmap()
                >>> plot(fig)

        """
        # Get response matrix
        responses = get_response_matrix(self.df, norm=norm)

        # Get figure
        fig = fig_heatmap(df_matrix=responses, title=title)
        return fig
Ejemplo n.º 7
0
def test_get_response_matrix_error():
    chat = WhatsAppChat.from_source(filename)
    with pytest.raises(ValueError):
        _ = get_response_matrix(chat=chat, norm='error')