def test_interventions_date_cumsum(): chat = WhatsAppChat.from_source(filename) counts = get_interventions_count(chat=chat, date_mode='date', msg_length=False, cumulative=True) assert (isinstance(counts, pd.DataFrame)) # Asswert chat df and counts df have same users assert (set(chat.users) == set(counts.columns)) assert (len(chat.users) == counts.shape[1]) # Assert chat df and counts df have same date window assert (chat.df.index.max().date() == counts.index.max().date()) assert (chat.df.index.min().date() == counts.index.min().date()) # TO BE DEPRECATED counts = get_interventions_count(chat=chat, date_mode='date', msg_length=False, cummulative=True) assert (isinstance(counts, pd.DataFrame)) # Asswert chat df and counts df have same users assert (set(chat.users) == set(counts.columns)) assert (len(chat.users) == counts.shape[1]) # Assert chat df and counts df have same date window assert (chat.df.index.max().date() == counts.index.max().date()) assert (chat.df.index.min().date() == counts.index.min().date())
def test_interventions_error_1(): chat = WhatsAppChat.from_source(filename) with pytest.raises(ValueError): _ = get_interventions_count(chat=chat, date_mode='error', msg_length=False) with pytest.raises(ValueError): _ = get_interventions_count(chat=chat, date_mode='error', msg_length=True)
def test_interventions_hour_msg_length(): chat = WhatsAppChat.from_source(filename) counts = get_interventions_count(chat=chat, date_mode='hour', msg_length=True) assert (isinstance(counts, pd.DataFrame)) # Asswert chat df and counts df have same users assert (set(chat.users) == set(counts.columns)) assert (len(chat.users) == counts.shape[1]) # Check range hours assert (counts.index.max() == chat.df.index.hour.max()) assert (counts.index.min() == chat.df.index.hour.min())
def test_interventions_date_all(): chat = WhatsAppChat.from_source(filename) counts = get_interventions_count(chat=chat, date_mode='date', msg_length=False, all_users=True) assert (isinstance(counts, pd.DataFrame)) # Asswert chat df and counts df have same users assert (len(counts.columns) == 1) assert (counts.columns == ['interventions count']) # Assert chat df and counts df have same date window assert (chat.df.index.max().date() == counts.index.max().date()) assert (chat.df.index.min().date() == counts.index.min().date())
def user_interventions_count_linechart(self, date_mode='date', msg_length=False, cumulative=False, all_users=False, title="User interventions count", xlabel="Date/Time", cummulative=None): """Plot number of user interventions over time. Args: date_mode (str, optional): Choose mode to group interventions by. Defaults to ``'date'``. Available modes are: - ``'date'``: Grouped by particular date (year, month and day). - ``'hour'``: Grouped by hours. - ``'month'``: Grouped by months. - ``'weekday'``: Grouped by weekday (i.e. monday, tuesday, ..., sunday). - ``'hourweekday'``: Grouped by weekday and hour. msg_length (bool, optional): Set to True to count the number of characters instead of number of messages sent. cumulative (bool, optional): Set to True to obtain commulative counts. all_users (bool, optional): Obtain number of interventions of all users combined. Defaults to False. title (str, optional): Title for plot. Defaults to "User interventions count". xlabel (str, optional): x-axis label title. Defaults to "Date/Time". cummulative (bool, optional): Deprecated, use cumulative. Returns: plotly.graph_objs.Figure: Plotly Figure. .. seealso:: * :func:`get_interventions_count <whatstk.analysis.get_interventions_count>` * :func:`fig_scatter_time <whatstk.graph.figures.scatter.fig_scatter_time>` 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_interventions_count_linechart(cumulative=True) >>> plot(fig) """ counts = get_interventions_count(df=self.df, date_mode=date_mode, msg_length=msg_length, cumulative=cumulative, all_users=all_users, cummulative=cummulative) if all_users: fig = fig_scatter_time(user_data=counts, title=title, xlabel=xlabel) else: fig = fig_scatter_time(user_data=counts, username_to_color=self.user_color_mapping, title=title, xlabel=xlabel) return fig
def test_interventions_error_2(): with pytest.raises(ValueError): _ = get_interventions_count(date_mode='hour', msg_length=False)