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()
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()
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
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
def heatmap_trigger_one_day(df_dev, t_res='1h', figsize=(10, 6)): """ computes the heatmap for one day where all the device triggers are showed """ df = device_triggers_one_day(df_dev.copy(), t_res) x_labels = list(df.index) y_labels = df.columns dat = df.values.T # begin plotting fig, ax = plt.subplots(figsize=figsize) im, cbar = heatmap(dat, y_labels, x_labels, ax=ax, cmap='viridis', cbarlabel='counts') ax.set_title("Device triggers cummulative over one day") ax.set_xlabel('time') # format the x-axis def func(x, p): if True: if int(x / k) < 10: return '0{}:00'.format(int(x / k) + 1) else: return '{}:00'.format(int(x / k) + 1) # calculate the tick positions a, b = ax.get_xlim() k = (b - a) / 23 tcks_pos = np.arange(0, 23) * k + (-0.5 + k) x_locator = ticker.FixedLocator(tcks_pos) ax.xaxis.set_major_formatter(ticker.FuncFormatter(func)) ax.xaxis.set_major_locator(x_locator) ax.set_aspect(aspect='auto') return fig
def heatmap_trigger_one_day(df_devs=None, lst_devs=None, df_tod=None, t_res='1h', figsize=None, cmap=None, file_path=None): """ Plots the heatmap for one day where all the device triggers are shown 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_tod : 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>`. t_res : 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. 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_tod is None) title = "Device triggers cummulative over one day" xlabel = 'time' df = (device_triggers_one_day(df_devs.copy(), lst_devs=lst_devs, t_res=t_res) if df_tod is None else df_tod) num_dev = len(list(df.columns)) figsize = (_num_items_2_heatmap_one_day_figsize(num_dev) if figsize is None else figsize) cmap = (get_sequential_color() if cmap is None else cmap) x_labels = list(df.index) y_labels = df.columns dat = df.values.T # begin plotting fig, ax = plt.subplots(figsize=figsize) im, cbar = heatmap(dat, y_labels, x_labels, ax=ax, cmap=cmap, cbarlabel='counts') ax.set_title(title) ax.set_xlabel(xlabel) # format the x-axis def func(x, p): if True: if int(x / k) < 10: return '0{}:00'.format(int(x / k) + 1) else: return '{}:00'.format(int(x / k) + 1) # calculate the tick positions a, b = ax.get_xlim() k = (b - a) / 24 tcks_pos = np.arange(0, 23) * k + (-0.5 + k) x_locator = ticker.FixedLocator(tcks_pos) ax.xaxis.set_major_formatter(ticker.FuncFormatter(func)) ax.xaxis.set_major_locator(x_locator) ax.set_aspect(aspect='auto') if file_path is not None: savefig(fig, file_path) return else: return fig