예제 #1
0
    def test_stats_devices(self):
        from pyadlml.dataset.stats.devices import device_tcorr, devices_trigger_count, \
            device_triggers_one_day, trigger_time_diff, devices_on_off_stats, duration_correlation

        df = self.data.df_devices
        lst = self.data.lst_devices

        # tcorr = device_tcorr(df)
        tcorr = device_tcorr(df, lst)
        assert tcorr.shape[0] == len(lst) and tcorr.shape[1] == len(lst)

        trigg_c = devices_trigger_count(df)
        assert len(trigg_c) == self.num_rec_devs
        trigg_c = devices_trigger_count(df, lst)
        assert len(trigg_c) == len(lst)

        trigg_od = device_triggers_one_day(df)
        assert len(trigg_od.columns) == self.num_rec_devs
        trigg_od = device_triggers_one_day(df,  lst)
        assert len(trigg_od.columns) == len(lst)

        trigg_td = trigger_time_diff(df)
        assert len(trigg_td) == len(df) - 1

        onoff = devices_on_off_stats(df)
        assert len(onoff) == self.num_rec_binary_devs
        onoff = devices_on_off_stats(df, lst)
        assert len(onoff) == len(lst)

        dc = duration_correlation(df)
        assert len(dc) == self.num_rec_binary_devs
        dc = duration_correlation(df, lst)
        assert len(dc) == len(lst)
예제 #2
0
def hist_counts(df_dev, figsize=(10, 6), y_scale=None):
    """
    plots the trigger count of each device 
    """
    df = devices_trigger_count(df_dev.copy())
    df.reset_index(level=0, inplace=True)

    title = 'Count of on/off activations per Device'
    col_label = 'trigger count'
    col_device = 'device'
    df.columns = ['device', col_label]

    df = df.sort_values(by=col_label, axis=0, ascending=True)

    # plot
    fig, ax = plt.subplots(figsize=figsize)
    plt.title(title)
    plt.xlabel(col_label)
    ax.barh(df['device'], df['trigger count'])
    if y_scale == 'log':
        ax.set_xscale('log')
    return fig
예제 #3
0
def hist_counts(df_devs=None,
                lst_devs=None,
                df_tc=None,
                figsize=None,
                y_scale=None,
                color=None,
                order='count',
                file_path=None):
    """
    bar chart displaying how often activities are occurring

    Parameters
    ----------
    df_devs : pd.DataFrame, optional
        recorded devices from a dataset. Fore more information refer to the
        :ref:`user guide<device_dataframe>`.
    lst_devs : lst of str, optional
        A list of devices that are included in the statistic. The list can be a
        subset of the recorded activities or contain activities that are not recorded.
    df_tc : pd.DataFrame
        A precomputed correlation table. If the *df_tcorr* parameter is given, parameters
        *df_devs* and *lst_devs* are ignored. The transition table can be computed
        in :ref:`stats <stats_devs_tcorr>`.
    y_scale : {"log", "linear"}, default: None
        The axis scale type to apply.
    figsize : (float, float), default: None
        width, height in inches. If not provided, the figsize is inferred by automatically.
    color : str, optional
        sets the primary color of the plot. When not set, the primary theming color is used.
        Learn more about theming in the :ref:`user guide <theming>`
    order : {'count', 'alphabetically', 'room'}, default='count'
        determines the order in which the devices are listed.
    file_path : str, optional
        If set, saves the plot under the given file path and return *None* instead
        of returning the figure.

    Examples
    --------
    >>> from pyadlml.plots import plot_device_bar_count
    >>> plot_device_bar_count(data.df_devices)

    .. image:: ../_static/images/plots/dev_bar_trigger.png
       :height: 300px
       :width: 500 px
       :scale: 90 %
       :alt: alternate text
       :align: center




    Returns
    -------
    res : fig or None
        Either a figure if file_path is not specified or nothing.
    """
    assert not (df_devs is None and df_tc is None)
    assert y_scale in ['log', 'linear']
    assert order in ['alphabetic', 'count', 'room']

    title = 'Device triggers'
    x_label = 'count'
    df_col = 'trigger_count'

    df = (devices_trigger_count(df_devs.copy(), lst_devs=lst_devs)
          if df_tc is None else df_tc)
    num_dev = len(df)
    figsize = (_num_bars_2_figsize(num_dev) if figsize is None else figsize)
    color = (get_primary_color() if color is None else color)

    if order == 'alphabetic':
        df = df.sort_values(by=[DEVICE], ascending=True)
    elif order == 'count':
        df = df.sort_values(by=[df_col])
    else:
        raise NotImplemented('the room order is going to be implemented')

    # plot
    fig, ax = plt.subplots(figsize=figsize)
    plt.title(title)
    plt.xlabel(x_label)
    ax.barh(df[DEVICE], df[df_col], color=color)

    if y_scale == 'log':
        ax.set_xscale('log')

    if file_path is not None:
        savefig(fig, file_path)
        return
    else:
        return fig