import pandas import nimfa import numpy as np from scipy.cluster.hierarchy import linkage, leaves_list from matplotlib.pyplot import savefig, imshow, set_cmap
def reorder(C): """ Reorder consensus matrix.
:param C: Consensus matrix. :type C: `numpy.matrix` http://nimfa.biolab.si/nimfa.examples.all_aml.html """ c_vec = np.array([C[i,j] for i in xrange(C.shape[0] - 1) for j in xrange(i + 1, C.shape[1])]) # convert similarities to distances Y = 1 - c_vec Z = linkage(Y, method = 'average') # get node ids as they appear in the tree from left to right(corresponding to observation vector idx) ivl = leaves_list(Z) ivl = ivl[::-1] return C[:, ivl][ivl, :],ivl