def test_clustermap_annotation(self): g = mat.clustermap(self.df_norm, annot=True, fmt=".1f") for val, text in zip(np.asarray(g.data2d).flat, g.ax_heatmap.texts): assert text.get_text() == f"{val:.1f}" g = mat.clustermap(self.df_norm, annot=self.df_norm, fmt=".1f") for val, text in zip(np.asarray(g.data2d).flat, g.ax_heatmap.texts): assert text.get_text() == f"{val:.1f}"
def test_square_warning(self): kws = self.default_kws.copy() g1 = mat.clustermap(self.df_norm, **kws) with pytest.warns(UserWarning): kws["square"] = True g2 = mat.clustermap(self.df_norm, **kws) g1_shape = g1.ax_heatmap.get_position().get_points() g2_shape = g2.ax_heatmap.get_position().get_points() assert np.array_equal(g1_shape, g2_shape)
def test_required_scipy_errors(): x = np.random.normal(0, 1, (10, 10)) with pytest.raises(RuntimeError): mat.clustermap(x) with pytest.raises(RuntimeError): mat.ClusterGrid(x) with pytest.raises(RuntimeError): mat.dendrogram(x)
def test_cbar_pos(self): kws = self.default_kws.copy() kws["cbar_pos"] = (.2, .1, .4, .3) g = mat.clustermap(self.df_norm, **kws) pos = g.ax_cbar.get_position() assert pytest.approx(tuple(pos.p0)) == kws["cbar_pos"][:2] assert pytest.approx(pos.width) == kws["cbar_pos"][2] assert pytest.approx(pos.height) == kws["cbar_pos"][3] kws["cbar_pos"] = None g = mat.clustermap(self.df_norm, **kws) assert g.ax_cbar is None
def test_row_col_colors_raise_on_mixed_index_types(self): row_colors = pd.Series(list(self.row_colors), name="row_annot", index=self.df_norm.index) col_colors = pd.Series(list(self.col_colors), name="col_annot", index=self.df_norm.columns) with pytest.raises(TypeError): mat.clustermap(self.x_norm, row_colors=row_colors) with pytest.raises(TypeError): mat.clustermap(self.x_norm, col_colors=col_colors)
def test_row_col_colors_series_shuffled(self): # Tests if colors are properly matched, even if given in wrong order m, n = self.df_norm.shape shuffled_inds = [ self.df_norm.index[i] for i in list(range(0, m, 2)) + list(range(1, m, 2)) ] shuffled_cols = [ self.df_norm.columns[i] for i in list(range(0, n, 2)) + list(range(1, n, 2)) ] kws = self.default_kws.copy() row_colors = pd.Series(list(self.row_colors), name='row_annot', index=self.df_norm.index) kws['row_colors'] = row_colors.loc[shuffled_inds] col_colors = pd.Series(list(self.col_colors), name='col_annot', index=self.df_norm.columns) kws['col_colors'] = col_colors.loc[shuffled_cols] cm = mat.clustermap(self.df_norm, **kws) assert list(cm.col_colors) == list(self.col_colors) assert list(cm.row_colors) == list(self.row_colors)
def test_tree_kws(self): rgb = (1, .5, .2) g = mat.clustermap(self.df_norm, tree_kws=dict(color=rgb)) for ax in [g.ax_col_dendrogram, g.ax_row_dendrogram]: tree, = ax.collections assert tuple(tree.get_color().squeeze())[:3] == rgb
def test_row_col_colors_df(self): kws = self.default_kws.copy() kws['row_colors'] = pd.DataFrame( { 'row_1': list(self.row_colors), 'row_2': list(self.row_colors) }, index=self.df_norm.index, columns=['row_1', 'row_2']) kws['col_colors'] = pd.DataFrame( { 'col_1': list(self.col_colors), 'col_2': list(self.col_colors) }, index=self.df_norm.columns, columns=['col_1', 'col_2']) cm = mat.clustermap(self.df_norm, **kws) row_labels = [l.get_text() for l in cm.ax_row_colors.get_xticklabels()] assert cm.row_color_labels == ['row_1', 'row_2'] assert row_labels == cm.row_color_labels col_labels = [l.get_text() for l in cm.ax_col_colors.get_yticklabels()] assert cm.col_color_labels == ['col_1', 'col_2'] assert col_labels == cm.col_color_labels
def test_row_col_colors(self): kws = self.default_kws.copy() kws['row_colors'] = self.row_colors kws['col_colors'] = self.col_colors cm = mat.clustermap(self.df_norm, **kws) assert len(cm.ax_row_colors.collections) == 1 assert len(cm.ax_col_colors.collections) == 1
def test_plot_dendrograms(self): cm = mat.clustermap(self.df_norm, **self.default_kws) assert len(cm.ax_row_dendrogram.collections[0].get_paths()) == len( cm.dendrogram_row.independent_coord) assert len(cm.ax_col_dendrogram.collections[0].get_paths()) == len( cm.dendrogram_col.independent_coord) data2d = self.df_norm.iloc[cm.dendrogram_row.reordered_ind, cm.dendrogram_col.reordered_ind] pdt.assert_frame_equal(cm.data2d, data2d)
def test_noticklabels(self): kws = self.default_kws.copy() kws["xticklabels"] = False kws["yticklabels"] = False g = mat.clustermap(self.df_norm, **kws) xtl_actual = [t.get_text() for t in g.ax_heatmap.get_xticklabels()] ytl_actual = [t.get_text() for t in g.ax_heatmap.get_yticklabels()] assert xtl_actual == [] assert ytl_actual == []
def test_colors_input_custom_cmap(self): kws = self.default_kws.copy() kws['cmap'] = mpl.cm.PRGn kws['row_colors'] = self.row_colors kws['col_colors'] = self.col_colors cg = mat.clustermap(self.df_norm, **kws) npt.assert_array_equal(cg.row_colors, self.row_colors) npt.assert_array_equal(cg.col_colors, self.col_colors) assert len(cg.fig.axes) == 6
def test_mask_reorganization(self): kws = self.default_kws.copy() kws["mask"] = self.df_norm > 0 g = mat.clustermap(self.df_norm, **kws) npt.assert_array_equal(g.data2d.index, g.mask.index) npt.assert_array_equal(g.data2d.columns, g.mask.columns) npt.assert_array_equal( g.mask.index, self.df_norm.index[g.dendrogram_row.reordered_ind]) npt.assert_array_equal( g.mask.columns, self.df_norm.columns[g.dendrogram_col.reordered_ind])
def test_cluster_false(self): kws = self.default_kws.copy() kws['row_cluster'] = False kws['col_cluster'] = False cm = mat.clustermap(self.df_norm, **kws) assert len(cm.ax_row_dendrogram.lines) == 0 assert len(cm.ax_col_dendrogram.lines) == 0 assert len(cm.ax_row_dendrogram.get_xticks()) == 0 assert len(cm.ax_row_dendrogram.get_yticks()) == 0 assert len(cm.ax_col_dendrogram.get_xticks()) == 0 assert len(cm.ax_col_dendrogram.get_yticks()) == 0 pdt.assert_frame_equal(cm.data2d, self.df_norm)
def test_row_col_colors_df_one_axis(self): # Test case with only row annotation. kws1 = self.default_kws.copy() kws1['row_colors'] = pd.DataFrame( { 'row_1': list(self.row_colors), 'row_2': list(self.row_colors) }, index=self.df_norm.index, columns=['row_1', 'row_2']) cm1 = mat.clustermap(self.df_norm, **kws1) row_labels = [ l.get_text() for l in cm1.ax_row_colors.get_xticklabels() ] assert cm1.row_color_labels == ['row_1', 'row_2'] assert row_labels == cm1.row_color_labels # Test case with only col annotation. kws2 = self.default_kws.copy() kws2['col_colors'] = pd.DataFrame( { 'col_1': list(self.col_colors), 'col_2': list(self.col_colors) }, index=self.df_norm.columns, columns=['col_1', 'col_2']) cm2 = mat.clustermap(self.df_norm, **kws2) col_labels = [ l.get_text() for l in cm2.ax_col_colors.get_yticklabels() ] assert cm2.col_color_labels == ['col_1', 'col_2'] assert col_labels == cm2.col_color_labels
def test_row_col_colors_ignore_heatmap_kwargs(self): g = mat.clustermap(self.rs.uniform(0, 200, self.df_norm.shape), row_colors=self.row_colors, col_colors=self.col_colors, cmap="Spectral", norm=mpl.colors.LogNorm(), vmax=100) assert np.array_equal( np.array(self.row_colors)[g.dendrogram_row.reordered_ind], g.ax_row_colors.collections[0].get_facecolors()[:, :3]) assert np.array_equal( np.array(self.col_colors)[g.dendrogram_col.reordered_ind], g.ax_col_colors.collections[0].get_facecolors()[:, :3])
def test_row_col_colors_df_missing(self): kws = self.default_kws.copy() row_colors = pd.DataFrame({'row_annot': list(self.row_colors)}, index=self.df_norm.index) kws['row_colors'] = row_colors.drop(self.df_norm.index[0]) col_colors = pd.DataFrame({'col_annot': list(self.col_colors)}, index=self.df_norm.columns) kws['col_colors'] = col_colors.drop(self.df_norm.columns[0]) cm = mat.clustermap(self.df_norm, **kws) assert list( cm.col_colors)[0] == [(1.0, 1.0, 1.0)] + list(self.col_colors[1:]) assert list( cm.row_colors)[0] == [(1.0, 1.0, 1.0)] + list(self.row_colors[1:])
def test_row_col_colors_series_missing(self): kws = self.default_kws.copy() row_colors = pd.Series(list(self.row_colors), name='row_annot', index=self.df_norm.index) kws['row_colors'] = row_colors.drop(self.df_norm.index[0]) col_colors = pd.Series(list(self.col_colors), name='col_annot', index=self.df_norm.columns) kws['col_colors'] = col_colors.drop(self.df_norm.columns[0]) cm = mat.clustermap(self.df_norm, **kws) assert list( cm.col_colors) == [(1.0, 1.0, 1.0)] + list(self.col_colors[1:]) assert list( cm.row_colors) == [(1.0, 1.0, 1.0)] + list(self.row_colors[1:])
def test_row_col_colors_series(self): kws = self.default_kws.copy() kws['row_colors'] = pd.Series(list(self.row_colors), name='row_annot', index=self.df_norm.index) kws['col_colors'] = pd.Series(list(self.col_colors), name='col_annot', index=self.df_norm.columns) cm = mat.clustermap(self.df_norm, **kws) row_labels = [l.get_text() for l in cm.ax_row_colors.get_xticklabels()] assert cm.row_color_labels == ['row_annot'] assert row_labels == cm.row_color_labels col_labels = [l.get_text() for l in cm.ax_col_colors.get_yticklabels()] assert cm.col_color_labels == ['col_annot'] assert col_labels == cm.col_color_labels
def test_ticklabel_reorganization(self): kws = self.default_kws.copy() xtl = np.arange(self.df_norm.shape[1]) kws["xticklabels"] = list(xtl) ytl = self.letters.loc[:self.df_norm.shape[0]] kws["yticklabels"] = ytl g = mat.clustermap(self.df_norm, **kws) xtl_actual = [t.get_text() for t in g.ax_heatmap.get_xticklabels()] ytl_actual = [t.get_text() for t in g.ax_heatmap.get_yticklabels()] xtl_want = xtl[g.dendrogram_col.reordered_ind].astype("<U1") ytl_want = ytl[g.dendrogram_row.reordered_ind].astype("<U1") npt.assert_array_equal(xtl_actual, xtl_want) npt.assert_array_equal(ytl_actual, ytl_want)
def test_size_ratios(self): # The way that wspace/hspace work in GridSpec, the mapping from input # ratio to actual width/height of each axes is complicated, so this # test is just going to assert comparative relationships kws1 = self.default_kws.copy() kws1.update(dendrogram_ratio=.2, colors_ratio=.03, col_colors=self.col_colors, row_colors=self.row_colors) kws2 = kws1.copy() kws2.update(dendrogram_ratio=.3, colors_ratio=.05) g1 = mat.clustermap(self.df_norm, **kws1) g2 = mat.clustermap(self.df_norm, **kws2) assert (g2.ax_col_dendrogram.get_position().height > g1.ax_col_dendrogram.get_position().height) assert (g2.ax_col_colors.get_position().height > g1.ax_col_colors.get_position().height) assert (g2.ax_heatmap.get_position().height < g1.ax_heatmap.get_position().height) assert (g2.ax_row_dendrogram.get_position().width > g1.ax_row_dendrogram.get_position().width) assert (g2.ax_row_colors.get_position().width > g1.ax_row_colors.get_position().width) assert (g2.ax_heatmap.get_position().width < g1.ax_heatmap.get_position().width) kws1 = self.default_kws.copy() kws1.update(col_colors=self.col_colors) kws2 = kws1.copy() kws2.update(col_colors=[self.col_colors, self.col_colors]) g1 = mat.clustermap(self.df_norm, **kws1) g2 = mat.clustermap(self.df_norm, **kws2) assert (g2.ax_col_colors.get_position().height > g1.ax_col_colors.get_position().height) kws1 = self.default_kws.copy() kws1.update(dendrogram_ratio=(.2, .2)) kws2 = kws1.copy() kws2.update(dendrogram_ratio=(.2, .3)) g1 = mat.clustermap(self.df_norm, **kws1) g2 = mat.clustermap(self.df_norm, **kws2) # Fails on pinned matplotlib? # assert (g2.ax_row_dendrogram.get_position().width # == g1.ax_row_dendrogram.get_position().width) assert g1.gs.get_width_ratios() == g2.gs.get_width_ratios() assert (g2.ax_col_dendrogram.get_position().height > g1.ax_col_dendrogram.get_position().height)