コード例 #1
0
ファイル: discrete.py プロジェクト: tcsvn/pyadlml
def heatmap_cross_correlation(df_dev, figsize=(10, 8)):

    title = 'Devices cross-correlation'
    cmap = get_diverging_color()
    cbarlabel = 'similarity'

    ct = cross_correlation(df_dev)
    vals = ct.values.T.astype(np.float64)
    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)
    annotate_heatmap(im,
                     textcolors=("black", "white"),
                     valfmt="{x:.2f}",
                     threshold=0.6)
    ax.set_title(title)
    fig.tight_layout()
    plt.show()
コード例 #2
0
ファイル: raw.py プロジェクト: longdongchuan/pyadlml
def corr_devices_01(rep, figsize=(19,14)):
    """ correlation between the on and off states of every device.
    Parameters
    ----------
    rep: pd.DataFrame
        A dataframe where columns are devices and rows are a binary representation
        e.g raw, changepoint, lastfired representation
    
    """
    df = rep.iloc[:,:-1] # get raw without activities
    df = df.reset_index(drop=True)
    df = df.astype(int)

    for device in df.columns:
        mask1 = df[[device]] == 1
        col_off = device + ' Off'
        df[[col_off]] = df[[device]].astype(bool)
        df[[col_off]] = ~df[[col_off]]
        df[[col_off]] = df[[col_off]].astype(int)
        df = df.rename(columns={device: device + ' On'})
        
    dev_count = int(len(df.columns)/2)
    
    ct = df.corr()
    ct_on_on = ct.iloc[:dev_count, :dev_count]
    ct_on_on_values = ct_on_on.values.T
    ct_on_on_devs = list(ct_on_on.index)
    
    ct_on_off = ct.iloc[dev_count:, :dev_count]
    ct_on_off_values = ct_on_off.values.T
    ct_on_off_rows = list(ct_on_off.index)
    ct_on_off_cols = list(ct_on_off.columns)

    fig, (ax1, ax2) = plt.subplots(1,2, figsize=figsize)
    
    # plot on-on corr
    im, cbar = heatmap(ct_on_on_values, ct_on_on_devs, ct_on_on_devs, 
                       ax=ax1, cmap='PuOr', cbarlabel='counts',
                       vmin=-1, vmax=1)

    texts = annotate_heatmap(im, textcolors=("black", "white"), valfmt="{x:.2f}")
    
    # plot on-off corr
    im, cbar = heatmap(ct_on_off_values, ct_on_off_rows, ct_on_off_cols, 
                       ax=ax2, cmap='PuOr', cbarlabel='counts',
                       vmin=-1, vmax=1)
    
    texts = annotate_heatmap(im, textcolors=("black", "white"), valfmt="{x:.2f}")
    
    ax1.set_title("Correlation of on-on signals")
    ax2.set_title("Correlation of on-off signals")
    fig.tight_layout()
    plt.show()
コード例 #3
0
ファイル: activities.py プロジェクト: longdongchuan/pyadlml
def heatmap_transitions(df_act, z_scale=None, figsize=(8, 6), idle=False):
    """    """
    assert z_scale in [None,
                       'log'], 'z-scale has to be either of type None or log'

    title = 'Activity transitions'
    z_label = 'count'

    df_act = add_idle(df_act) if idle else df_act

    # get the list of cross tabulations per t_window
    df = activities_transitions(df_act)
    act_lst = list(df.columns)
    x_labels = act_lst
    y_labels = act_lst
    values = df.values

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

    # begin plotting
    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap_square(values,
                              y_labels,
                              x_labels,
                              log=log,
                              ax=ax,
                              cbarlabel=z_label)
    texts = annotate_heatmap(im,
                             textcolors=("white", "black"),
                             log=log,
                             valfmt=valfmt)
    ax.set_title(title)

    return fig
コード例 #4
0
def heatmap_contingency_triggers(df_dev_rep3,
                                 df_act,
                                 figsize=(12, 10),
                                 idle=False,
                                 z_scale=None):
    """
    plot a heatmap TODO write docu
    """

    title = 'Triggers'
    cbarlabel = 'counts'

    tmp = contingency_table_triggers(df_dev_rep3, df_act, idle=idle)
    vals = tmp.values.T
    acts = tmp.columns
    devs = tmp.index

    fig, ax = plt.subplots(figsize=figsize)
    if z_scale == 'log':
        log = True
    else:
        log = False

    im, cbar = heatmap(vals, acts, devs, ax=ax, log=log, cbarlabel=cbarlabel)

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

    ax.set_title(title)
    fig.tight_layout()
    plt.show()
コード例 #5
0
ファイル: devices.py プロジェクト: longdongchuan/pyadlml
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()
コード例 #6
0
ファイル: devices.py プロジェクト: longdongchuan/pyadlml
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()
コード例 #7
0
ファイル: raw.py プロジェクト: longdongchuan/pyadlml
def heatmap_cross_correlation(df_dev, figsize=(10,8)):
    ct = cross_correlation(df_dev)
    vals = ct.values.T.astype(np.float64)
    devs = list(ct.index)
    
    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap_square(vals, devs, devs, ax=ax, cmap='BrBG', cbarlabel='counts',
                       vmin=-1, vmax=1)

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

    ax.set_title("Cross-correlation of signals")
    fig.tight_layout()
    plt.show()
コード例 #8
0
ファイル: raw.py プロジェクト: longdongchuan/pyadlml
def heatmap_contingency_01(X, y, rep='', z_scale=None, figsize=(16,12)):
    """ plots the contingency between features and labels of data
    Parameters
    ----------
    X: pd.DataFrame
        one of the representation raw, lastfired, changepoint
    y: pd.DataFrame
        a series of labels
    rep: string
        the name of the representation to add to the title
    """
    
    cbarlabel = 'counts'
    title = "On/Off contingency for representation " + rep

    df_con = contingency_table_01(X, y)
    
    vals = df_con.values.T
    acts = df_con.columns
    devs = list(df_con.index)
    
    # format x labels
    for i in range(0,len(devs)):
        if i % 2 == 0:
            tmp = devs[i][:-3]
            devs[i] = tmp + 'Off'
        else:
            devs[i] = 'On'

    if z_scale == 'log':
        log = True
    else:
        log = False

    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap(vals, acts, devs, ax=ax, log=log, cbarlabel=cbarlabel)
    
    texts = annotate_heatmap(im, log=log, textcolors=("white", "black"), valfmt="{x:.0f}")
    
    # create grid for heatmap into every pair
    tcks = np.arange((vals.shape[1])/2)*2 + 1.5
    ax.set_xticks(tcks, minor=True)
    ax.grid(which="minor", color="w", linestyle='-', linewidth=2)
    ax.tick_params(which="minor", bottom=False, left=False) 
    
    ax.set_title(title)
    fig.tight_layout()
    return fig
コード例 #9
0
def heatmap_contingency_triggers_01(df_dev_rep3,
                                    df_act,
                                    figsize=(12, 10),
                                    idle=True,
                                    z_scale=None):
    """
    """
    df_con = contingency_table_triggers_01(df_dev_rep3, df_act, idle=idle)
    tmp = df_con.reset_index()
    tmp['index'] = tmp['device'] + ' ' + tmp['val'].astype(str)
    tmp = tmp.set_index('index')
    tmp = tmp.drop(['device', 'val'], axis=1)

    vals = tmp.values.T
    acts = tmp.columns
    devs = list(tmp.index)

    # format x labels
    for i in range(0, len(devs)):
        if i % 2 == 0:
            tmp = devs[i][:-5]
            devs[i] = tmp + 'Off'
        else:
            devs[i] = 'On'

    if z_scale == 'log':
        log = True
    else:
        log = False

    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap(vals, acts, devs, ax=ax, log=log, cbarlabel='counts')

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

    # create grid for heatmap into every pair
    tcks = np.arange((vals.shape[1]) / 2) * 2 + 1.5
    ax.set_xticks(tcks, minor=True)
    ax.grid(which="minor", color="w", linestyle='-', linewidth=1)
    ax.tick_params(which="minor", bottom=False, left=False)

    ax.set_title("0-1 triggers")
    fig.tight_layout()
    return fig
コード例 #10
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
コード例 #11
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
コード例 #12
0
ファイル: activities.py プロジェクト: reviasprila/pyadlml
def heatmap_transitions(df_acts=None,
                        lst_acts=None,
                        df_trans=None,
                        z_scale="linear",
                        figsize=None,
                        idle=False,
                        numbers=True,
                        grid=True,
                        cmap=None,
                        file_path=None):
    """
    Parameters
    ----------
    df_acts : pd.DataFrame, optional
        recorded activities from a dataset. Fore more information refer to the
        :ref:`user guide<activity_dataframe>`.
    lst_acts : lst of str, optional
        A list of activities that are included in the statistic. The list can be a
        subset of the recorded activities or contain activities that are not recorded.
    df_trans : pd.DataFrame
        A precomputed transition table. If the *df_trans* parameter is given, parameters
        *df_acts* and *lst_acts* are ignored. The transition table can be computed
        in :ref:`stats <stats_acts_trans>`.
    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.
    idle : bool, default: False
        Determines whether gaps between activities should be assigned
        the activity *idle* or be ignored.
    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'.
    grid : bool, default: True
        determines whether to display a white grid, seperating the 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_activity_hm_transitions
    >>> plot_activity_hm_transitions(data.df_activities)

    .. image:: ../_static/images/plots/act_hm_trans.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 z_scale in [None,
                       'log'], 'z-scale has to be either of type None or log'
    assert not (df_acts is None and df_trans is None)

    title = 'Activity transitions'
    z_label = 'count'

    if df_trans is None:
        df_acts = add_idle(df_acts) if idle else df_acts
        df = activities_transitions(df_acts, lst_acts=lst_acts)
    else:
        df = df_trans

    # get the list of cross tabulations per t_window
    act_lst = list(df.columns)

    num_act = len(act_lst)
    figsize = (_num_items_2_heatmap_square_figsize(num_act)
               if figsize is None else figsize)
    cmap = (get_sequential_color() if cmap is None else cmap)

    x_labels = act_lst
    y_labels = act_lst
    values = df.values

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

    # begin plotting
    fig, ax = plt.subplots(figsize=figsize)
    im, cbar = heatmap_square(values,
                              y_labels,
                              x_labels,
                              log=log,
                              cmap=cmap,
                              ax=ax,
                              cbarlabel=z_label,
                              grid=grid)
    if numbers:
        texts = annotate_heatmap(im,
                                 textcolors=("white", "black"),
                                 log=log,
                                 valfmt=valfmt)
    ax.set_title(title)

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