Beispiel #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)
Beispiel #2
0
def heatmap_trigger_time(df_dev, t_window='5s', figsize=(8, 8), z_scale=None):
    color = 'trigger count'
    cbarlabel = 'counts'

    # get the list of cross tabulations per t_window
    df = device_tcorr(df_dev, t_window)[0]
    vals = df.astype(int).values.T
    devs = list(df.index)

    fig, ax = plt.subplots(figsize=figsize)

    log = True if z_scale == 'log' else False
    valfmt = "{x:.0f}"

    im, cbar = heatmap_square(
        vals,
        devs,
        devs,
        ax=ax,  #cmap='viridis', 
        cbarlabel=cbarlabel,
        log=log)  #, cbar_kw=cbar_kw)

    texts = annotate_heatmap(im,
                             textcolors=("white", "black"),
                             log=log,
                             valfmt=valfmt)

    ax.set_title("Triggercount with sliding window of " + t_window)
    fig.tight_layout()
    plt.show()
Beispiel #3
0
def heatmap_trigger_time(df_devs=None,
                         lst_devs=None,
                         df_tcorr=None,
                         t_window='5s',
                         figsize=None,
                         z_scale="linear",
                         cmap=None,
                         numbers=None,
                         file_path=None):
    """
    Plot todo

    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_tcorr : 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>`.
    t_window : str of {'[1-12]h', default='1h'
        the resolution, time_bins in hours. The number are
    figsize : (float, float), default: None
        width, height in inches. If not provided, the figsize is inferred by automatically.
    z_scale : {"log", "linear"}, default: None
        The axis scale type to apply.
    numbers : bool, default: True
        Whether to display numbers inside the heatmaps fields or not.
    cmap : str or Colormap, optional
        The Colormap instance or registered colormap name used to map scalar
        data to colors. This parameter is ignored for RGB(A) data.
        Defaults 'viridis'.
    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_hm_time_trigger
    >>> plot_device_hm_time_trigger(data.df_devices, t_res='1h')

    .. image:: ../_static/images/plots/dev_hm_trigger_one_day.png
       :height: 300px
       :width: 500 px
       :scale: 100 %
       :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_tcorr is None)
    title = "Triggercount with sliding window of " + t_window

    color = 'trigger count'
    cbarlabel = 'counts'

    if df_tcorr is None:
        df = device_tcorr(df_devs, lst_devs=lst_devs, t_window=t_window)
    else:
        df = df_tcorr

    # get the list of cross tabulations per t_window
    vals = df.astype(int).values.T
    devs = list(df.index)

    num_dev = len(devs)
    figsize = (_num_items_2_heatmap_square_figsize_ver2(num_dev)
               if figsize is None else figsize)
    cmap = (get_sequential_color() if cmap is None else cmap)

    fig, ax = plt.subplots(figsize=figsize)

    log = True if z_scale == 'log' else False
    valfmt = "{x:.0f}"

    im, cbar = heatmap_square(vals,
                              devs,
                              devs,
                              ax=ax,
                              cmap=cmap,
                              cbarlabel=cbarlabel,
                              log=log)  #, cbar_kw=cbar_kw)

    # show numbers for small sizes
    if numbers is None:
        if num_dev < 20:
            texts = annotate_heatmap(im,
                                     textcolors=("white", "black"),
                                     log=log,
                                     valfmt=valfmt)
    elif numbers:
        texts = annotate_heatmap(im,
                                 textcolors=("white", "black"),
                                 log=log,
                                 valfmt=valfmt)

    ax.set_title(title)
    fig.tight_layout()

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