Esempio n. 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)
Esempio n. 2
0
def heatmap_cross_correlation(df_dev, figsize=(10, 8)):
    """ plots the cross correlation between the device signals
    Parameters
    ----------
    df_dev: pd.DataFrame 
        devices in representation 1
    """
    cmap = 'BrBG'
    cbarlabel = 'counts'
    title = 'Cross-correlation of signals'

    ct = duration_correlation(df_dev)
    vals = ct.values.T
    devs = list(ct.index)
    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap_square(vals,
                              devs,
                              devs,
                              ax=ax,
                              cmap=cmap,
                              cbarlabel=cbarlabel,
                              vmin=-1,
                              vmax=1)

    texts = annotate_heatmap(im,
                             textcolors=("black", "white"),
                             threshold=0.5,
                             valfmt="{x:.2f}")

    ax.set_title(title)
    fig.tight_layout()
    plt.show()
Esempio n. 3
0
def heatmap_cross_correlation(df_devs=None,
                              lst_devs=None,
                              df_dur_corr=None,
                              figsize=None,
                              numbers=None,
                              file_path=None):
    """
    Plots the cross correlation between the device signals

    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>`.
    figsize : (float, float), default: None
        width, height in inches. If not provided, the figsize is inferred by automatically.
    numbers : bool, default: True
        Whether to display numbers inside the heatmaps fields or not.
    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_dev_hm_similarity
    >>> plot_dev_hm_similarity(data.df_devices)

    .. image:: ../_static/images/plots/dev_hm_dur_cor.png
       :height: 400px
       :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_dur_corr is None)

    title = 'Devices cross-correlation'
    cmap = 'RdBu'
    cbarlabel = 'similarity'

    if df_dur_corr is None:
        ct = duration_correlation(df_devs, lst_devs=lst_devs)
    else:
        ct = df_dur_corr
    ct = ct.replace(pd.NA, np.inf)
    vals = ct.values.T
    devs = list(ct.index)

    num_dev = len(devs)
    figsize = (_num_items_2_heatmap_square_figsize_ver2(num_dev)
               if figsize is None else figsize)
    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap_square(vals,
                              devs,
                              devs,
                              ax=ax,
                              cmap=cmap,
                              cbarlabel=cbarlabel,
                              vmin=-1,
                              vmax=1)
    if numbers is None:
        if num_dev < 15:
            valfmt = "{x:.2f}"
            texts = annotate_heatmap(im,
                                     textcolors=("black", "white"),
                                     threshold=0.5,
                                     valfmt=valfmt)
        elif num_dev < 30:
            valfmt = "{x:.1f}"
            texts = annotate_heatmap(im,
                                     textcolors=("black", "white"),
                                     threshold=0.5,
                                     valfmt=valfmt)
    if numbers:
        texts = annotate_heatmap(im,
                                 textcolors=("black", "white"),
                                 threshold=0.5,
                                 valfmt="{x:.2f}")
    ax.set_title(title)
    fig.tight_layout()
    if file_path is not None:
        savefig(fig, file_path)
        return
    else:
        return fig