def plot_matrix(self, colorbar_kws, xind, yind, **kws): self.data2d = self.data2d.iloc[yind, xind] self.mask = self.mask.iloc[yind, xind] # Try to reorganize specified tick labels, if provided xtl = kws.pop("xticklabels", True) try: xtl = np.asarray(xtl)[xind] except (TypeError, IndexError): pass ytl = kws.pop("yticklabels", True) try: ytl = np.asarray(ytl)[yind] except (TypeError, IndexError): pass heatmap(self.data2d, ax=self.ax_heatmap, cbar=self.colorbar, cbar_ax=self.cax, cbar_kws=colorbar_kws, mask=self.mask, xticklabels=xtl, yticklabels=ytl, **kws) self.ax_heatmap.yaxis.set_ticks_position('right') self.ax_heatmap.yaxis.set_label_position('right')
def test_heatmap_ticklabel_rotation(self): f, ax = plt.subplots(figsize=(2, 2)) mat.heatmap(self.df_norm, xticklabels=1, yticklabels=1, ax=ax) for t in ax.get_xticklabels(): assert t.get_rotation() == 0 for t in ax.get_yticklabels(): assert t.get_rotation() == 90 plt.close(f) df = self.df_norm.copy() df.columns = [str(c) * 10 for c in df.columns] df.index = [i * 10 for i in df.index] f, ax = plt.subplots(figsize=(2, 2)) mat.heatmap(df, xticklabels=1, yticklabels=1, ax=ax) for t in ax.get_xticklabels(): assert t.get_rotation() == 90 for t in ax.get_yticklabels(): assert t.get_rotation() == 0 plt.close(f)
def test_cbar_ticks(self): f, (ax1, ax2) = plt.subplots(2) mat.heatmap(self.df_norm, ax=ax1, cbar_ax=ax2, cbar_kws=dict(drawedges=True)) assert len(ax2.collections) == 2
def test_heatmap_annotation_mesh_colors(self): ax = mat.heatmap(self.df_norm, annot=True) mesh = ax.collections[0] assert len(mesh.get_facecolors()) == self.df_norm.values.size plt.close("all")
def test_square_aspect(self): ax = mat.heatmap(self.df_norm, square=True) obs_aspect = ax.get_aspect() # mpl>3.3 returns 1 for setting "equal" aspect # so test for the two possible equal outcomes assert obs_aspect == "equal" or obs_aspect == 1
def test_custom_center_colors(self): vals = np.linspace(.2, 1, 9) cmap = mpl.cm.binary ax = mat.heatmap([vals], center=.5, cmap=cmap) fc = ax.collections[0].get_facecolors() npt.assert_array_almost_equal(fc, cmap(vals), 2)
def test_heatmap_inner_lines(self): c = (0, 0, 1, 1) ax = mat.heatmap(self.df_norm, linewidths=2, linecolor=c) mesh = ax.collections[0] assert mesh.get_linewidths()[0] == 2 assert tuple(mesh.get_edgecolor()[0]) == c
def test_heatmap_annotation_with_limited_ticklabels(self): ax = mat.heatmap(self.df_norm, fmt=".2f", annot=True, xticklabels=False, yticklabels=False) for val, text in zip(self.x_norm.flat, ax.texts): assert text.get_text() == f"{val:.2f}"
def test_default_colors(self): vals = np.linspace(.2, 1, 9) cmap = mpl.cm.binary ax = mat.heatmap([vals], cmap=cmap) fc = ax.collections[0].get_facecolors() cvals = np.linspace(0, 1, 9) npt.assert_array_almost_equal(fc, cmap(cvals), 2)
def test_heatmap_annotation(self): ax = mat.heatmap(self.df_norm, annot=True, fmt=".1f", annot_kws={"fontsize": 14}) for val, text in zip(self.x_norm.flat, ax.texts): assert text.get_text() == f"{val:.1f}" assert text.get_fontsize() == 14
def test_heatmap_annotation_other_data(self): annot_data = self.df_norm + 10 ax = mat.heatmap(self.df_norm, annot=annot_data, fmt=".1f", annot_kws={"fontsize": 14}) for val, text in zip(annot_data.values.flat, ax.texts): assert text.get_text() == f"{val:.1f}" assert text.get_fontsize() == 14
def test_heatmap_annotation_overwrite_kws(self): annot_kws = dict(color="0.3", va="bottom", ha="left") ax = mat.heatmap(self.df_norm, annot=True, fmt=".1f", annot_kws=annot_kws) for text in ax.texts: assert text.get_color() == "0.3" assert text.get_ha() == "left" assert text.get_va() == "bottom"
def test_heatmap_annotation_with_mask(self): df = pd.DataFrame(data={ 'a': [1, 1, 1], 'b': [2, np.nan, 2], 'c': [3, 3, np.nan] }) mask = np.isnan(df.values) df_masked = np.ma.masked_where(mask, df) ax = mat.heatmap(df, annot=True, fmt='.1f', mask=mask) assert len(df_masked.compressed()) == len(ax.texts) for val, text in zip(df_masked.compressed(), ax.texts): assert f"{val:.1f}" == text.get_text()
def test_heatmap_axes(self): ax = mat.heatmap(self.df_norm) xtl = [int(l.get_text()) for l in ax.get_xticklabels()] assert xtl == list(self.df_norm.columns) ytl = [l.get_text() for l in ax.get_yticklabels()] assert ytl == list(self.df_norm.index) assert ax.get_xlabel() == "" assert ax.get_ylabel() == "letters" assert ax.get_xlim() == (0, 8) assert ax.get_ylim() == (4, 0)
def test_heatmap_cbar(self): f = plt.figure() mat.heatmap(self.df_norm) assert len(f.axes) == 2 plt.close(f) f = plt.figure() mat.heatmap(self.df_norm, cbar=False) assert len(f.axes) == 1 plt.close(f) f, (ax1, ax2) = plt.subplots(2) mat.heatmap(self.df_norm, ax=ax1, cbar_ax=ax2) assert len(f.axes) == 2 plt.close(f)
def plot_colors(self, xind, yind, **kws): """Plots color labels between the dendrogram and the heatmap Parameters ---------- heatmap_kws : dict Keyword arguments heatmap """ # Remove any custom colormap and centering kws = kws.copy() kws.pop('cmap', None) kws.pop('center', None) kws.pop('vmin', None) kws.pop('vmax', None) kws.pop('xticklabels', None) kws.pop('yticklabels', None) if self.row_colors is not None: matrix, cmap = self.color_list_to_matrix_and_cmap(self.row_colors, yind, axis=0) # Get row_color labels if self.row_color_labels is not None: row_color_labels = self.row_color_labels else: row_color_labels = False heatmap(matrix, cmap=cmap, cbar=False, ax=self.ax_row_colors, xticklabels=row_color_labels, yticklabels=False, **kws) # Adjust rotation of labels if row_color_labels is not False: plt.setp(self.ax_row_colors.get_xticklabels(), rotation=90) if self.col_colors is not None: matrix, cmap = self.color_list_to_matrix_and_cmap(self.col_colors, xind, axis=1) # Get col_color labels if self.col_color_labels is not None: col_color_labels = self.col_color_labels else: col_color_labels = False heatmap(matrix, cmap=cmap, cbar=False, ax=self.ax_col_colors, xticklabels=False, yticklabels=col_color_labels, **kws) # Adjust rotation of labels, place on right side if col_color_labels is not False: self.ax_col_colors.yaxis.tick_right() plt.setp(self.ax_col_colors.get_yticklabels(), rotation=0)
def plot_dataset(absolut=None, n_images: List[int] = [1, 2, 3, 4], names: List[int] = ['COVID-19', 'Normal', 'Pneumonia'], path: Path = None, overwrite: bool = True): perc = normalize_confusion_matrix(absolut) if isinstance(n_images, list): for i in range(len(n_images)): dist_dataset(absolut[i], perc, names, n_images[i]) else: dist_dataset(absolut, perc, names, n_images) plt.show() # fig, ax = plt.subplots() # im = ax.imshow(absolut) # ax.set_xticks(np.arange(len(names))) # ax.set_yticks(np.arange(len(names))) # ax.set_xticklabels(names) # ax.set_yticklabels(names) # plt.setp(ax.get_xticklabels(), rotation=45, # ha='right', rotation_mode='anchor') # for i in range(len(names)): # for j in range(len(names)): # text = ax.text(j, i, absolut[i][j], # ha='center', va='center', color='w') # ax.set_title('Matriz Confusao') # fig.tight_layout() # plt.show() fig, ax = plt.subplots() im, cbar = heatmap(np.array(absolut), names, names, ax=ax, cmap='Blues', cbarlabel=None) texts = annotate_heatmap(im, valfmt="{x}") ax.set_title('Matriz de confusão') ax.set_xlabel('Rótulo Verdadeiro') ax.set_ylabel('Rótulo Predição') fig.tight_layout() # if path is not None: # fig_path = os.path.join(path,'matriz_confusao.png') # if overwrite: # i = 0 # while os.path.exists(fig_path): # fig_path = os.path.join(path,'matriz_confusao_{}.png'.format(i)) # i += 1 # plt.savefig(fig_path,dpi=fig.dpi) plt.show() mc_path = path / f'mc_{n_images}_pacotes.png' if mc_path.exists(): if overwrite: i = 0 mc_path = str(mc_path.absolute())[:-4] fig_path = f'{mc_path}_{i}.png' while os.path.exists(fig_path): i += 1 fig_path = f'{mc_path}_{i}.png' plt.savefig(fig_path, dpi=fig.dpi) return fig_path print(f'[PLOT] Arquivo já existe: {str(path)}') return mc_path plt.savefig(mc_path, dpi=fig.dpi)
def test_heatmap_annotation_different_shapes(self): annot_data = self.df_norm.iloc[:-1] with pytest.raises(ValueError): mat.heatmap(self.df_norm, annot=annot_data)
def make_confusion_matrix_plot( train_inputs: Tuple[np.ndarray], test_inputs: Tuple[np.ndarray], job_config: ht.config, save_dir: ht.pathlike, ): tc = job_config.train.clone() ac = job_config.apply.clone() score_cut = ac.cfg_confusion_matrix.dnn_cut save_dir = pathlib.Path(save_dir) / f"confusion_matrix" save_dir.mkdir(exist_ok=True, parents=True) # prepare output_bkg_node_names = tc.output_bkg_node_names output_bkg_node_names = tc.output_bkg_node_names all_nodes = ["sig"] + output_bkg_node_names y_train, y_train_pred, wt_train = train_inputs y_test, y_test_pred, wt_test = test_inputs # plot node by node row_label = ["Negative Class", "Positive Class"] col_label = ["Negative Prediction", "Positive Prediction"] for node_num, node in enumerate(all_nodes): # train dataset con_matrix_train = confusion_matrix( y_train[:, node_num], y_train_pred[:, node_num] > score_cut, sample_weight=wt_train, ) matrix_df = pd.DataFrame(con_matrix_train) matrix_df.columns = col_label matrix_df.index = row_label matrix_df["Total"] = matrix_df.sum(axis=1, numeric_only=True) matrix_df.loc["Total"] = matrix_df.sum(numeric_only=True) matrix_df.to_csv(save_dir / f"node_{node}_cut_{score_cut}_train.csv") fig, ax = plt.subplots() heatmap(con_matrix_train, cmap="coolwarm_r", annot=True, ax=ax) ax.set_title(f"node {node} - cut {score_cut} - train") ax.set_xlabel("Predicted Classes") ax.set_ylabel("Real Classes") fig.savefig(save_dir / f"node_{node}_cut_{score_cut}_train.png") with open(save_dir / f"node_{node}_cut_{score_cut}_train_report.txt" ) as report_file: report = classification_report( y_train, y_train_pred[:, node_num] > score_cut) print(report, file=report_file) # test dataset con_matrix_test = confusion_matrix( y_test[:, node_num], y_test_pred[:, node_num] > score_cut, sample_weight=wt_test, ) matrix_df = pd.DataFrame(con_matrix_test) matrix_df.columns = col_label matrix_df.index = row_label matrix_df["Total"] = matrix_df.sum(axis=1, numeric_only=True) matrix_df.loc["Total"] = matrix_df.sum(numeric_only=True) matrix_df.to_csv(save_dir / f"node_{node}_cut_{score_cut}_test.csv") fig, ax = plt.subplots() heatmap(con_matrix_test, cmap="coolwarm_r", annot=True, ax=ax) ax.set_title(f"node {node} - cut {score_cut} - test") ax.set_xlabel("Predicted Classes") ax.set_ylabel("Real Classes") fig.savefig(save_dir / f"node_{node}_cut_{score_cut}_test.png") with open( save_dir / f"node_{node}_cut_{score_cut}_test_report.txt") as report_file: report = classification_report( y_test, y_test_pred[:, node_num] > score_cut) print(report, file=report_file)