示例#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_trigger_time_diff(df_devs=None,
                           x=None,
                           n_bins=50,
                           figsize=(10, 6),
                           color=None,
                           file_path=None):
    """
    plots

    Parameters
    ----------
    df_devs : pd.DataFrame, optional
        recorded devices from a dataset. Fore more information refer to the
        :ref:`user guide<device_dataframe>`.
    x : ndarray, optional
        array of time deltas used to plot the histogram
    n_bins : int, default=50
        the number of bins for the histogram.
    color : str, optional
        sets the color of the plot. When not set, the primary theming color is used.
        Learn more about theming in the :ref:`user guide <theming>`
    figsize : (float, float), default: None
        width, height in inches. If not provided, the figsize is inferred by automatically.
    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.plot import plot_trigger_time_dev
    >>> plot_trigger_time_dev_todo(data.df_devices)

    .. image:: ../_static/images/plots/dev_hist_trigger_td.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 x is None)
    title = 'Time difference between succeeding device'
    log_sec_col = 'total_log_secs'
    sec_col = 'total_secs'
    ylabel = 'count'
    ax2label = 'cummulative percentage'
    ax1label = 'timedeltas count '
    xlabel = 'log seconds'
    color = (get_primary_color() if color is None else color)
    color2 = get_secondary_color()

    if x is None:
        X = trigger_time_diff(df_devs.copy())
    else:
        X = x

    # make equal bin size from max to min
    bins = np.logspace(min(np.log10(X)), max(np.log10(X)), n_bins)

    # make data ready for hist
    hist, _ = np.histogram(X, bins=bins)
    cum_percentage = hist.cumsum() / hist.sum()
    cum_percentage = np.concatenate(
        ([0], cum_percentage))  # let the array start with 0

    # plots
    fig, ax = plt.subplots(figsize=figsize)
    plt.xscale('log')
    ax.hist(X, bins=bins, label=ax1label, color=color)
    ax.set_ylabel(ylabel)
    ax.set_xlabel(xlabel)

    # create axis for line
    ax2 = ax.twinx()
    ax2.plot(bins, cum_percentage, 'r', label=ax2label, color=color2)
    ax2.set_ylabel('%')
    ax2.set_xscale('log')

    ax_top = ax.secondary_xaxis('top', functions=(lambda x: x, lambda x: x))
    ax_top.xaxis.set_major_formatter(
        ticker.FuncFormatter(func_formatter_seconds2time))

    # plot single legend for multiple axis
    h1, l1 = ax.get_legend_handles_labels()
    h2, l2 = ax2.get_legend_handles_labels()
    ax.legend(h1 + h2, l1 + l2, loc='center right')

    plt.title(title, y=1.08)

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