def make_hierarch_cluster_figs(model):
def make_cat_cluster_fig(cat, bottom_off=False, num_probes_limit=20):
"""
Returns fig showing hierarchical clustering of probes in single category
"""
start = time.time()
sns.set_style('white')
# load data
cat_prototypes_df = model.get_single_cat_acts_df(cat)
probes_in_cat = [model.probe_store.probe_set[probe_id] for probe_id in cat_prototypes_df.index.tolist()]
num_probes_in_cat = len(probes_in_cat)
if num_probes_limit and num_probes_in_cat > num_probes_limit:
ids = np.random.choice(range(num_probes_in_cat), num_probes_limit, replace=False)
cat_prototypes_df = cat_prototypes_df.iloc[ids]
probes_in_cat = [probes_in_cat[id] for id in ids]
# fig
rcParams['lines.linewidth'] = 2.0
fig, ax = plt.subplots(figsize=(FigsConfigs.MAX_FIG_WIDTH, 4), dpi=FigsConfigs.DPI)
# dendrogram
dist_matrix = pdist(cat_prototypes_df.values, 'euclidean')
linkages = linkage(dist_matrix, method='complete')
dendrogram(linkages,
ax=ax,
leaf_label_func=lambda x: probes_in_cat[x],
orientation='right',
leaf_font_size=8)
ax.set_title(cat)
ax.set_xlim([0, FigsConfigs.CAT_CLUSTER_XLIM])
ax.tick_params(axis='both', which='both', top='off', right='off', left='off')
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
if bottom_off:
ax.xaxis.set_ticklabels([]) # hides ticklabels
ax.tick_params(axis='both', which='both', bottom='off')
ax.spines['bottom'].set_visible(False)
fig.tight_layout()
print('{} completed in {:.1f} secs'.format(sys._getframe().f_code.co_name, time.time() - start))
return fig
figs = [make_cat_cluster_fig(cat) for cat in model.probe_store.cat_set]
return figs
评论列表
文章目录