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()
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
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()
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()
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()
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
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
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