def get_response_content(fs): np.set_printoptions(linewidth=200) n = len(fs.D) # create the distance matrix with the extra node added D_dup = get_duplicate_edm(fs.D) # get the principal axis projection from the distance dup matrix HSH = -(0.5) * MatrixUtil.double_centered(D_dup) X_w, X_v = EigUtil.principal_eigh(HSH) D_dup_x = X_v * math.sqrt(X_w) # get masses summing to one m = np.array([1]*(n-1) + [2], dtype=float) / (n+1) # get the principal axis projection using the weight formula M = np.diag(np.sqrt(m)) I = np.eye(n, dtype=float) E = I - np.outer(np.ones(n, dtype=float), m) ME = np.dot(M, E) Q = -(0.5) * np.dot(ME, np.dot(fs.D, ME.T)) Q_w, Q_v = EigUtil.principal_eigh(Q) Q_x = Q_v * math.sqrt(Q_w) / np.sqrt(m) # make the response out = StringIO() print >> out, 'distance matrix with exact duplicate node:' print >> out, D_dup print >> out print >> out, 'principal axis projection:' print >> out, D_dup_x print >> out print >> out, 'principal axis projection using the weight formula:' print >> out, Q_x return out.getvalue()
def get_response_content(fs): np.set_printoptions(linewidth=200) n = len(fs.D) # create the Laplacian matrix L = Euclid.edm_to_laplacian(fs.D) # create the Laplacian matrix with the extra node added L_dup = get_pseudoduplicate_laplacian(L, fs.strength) # get the principal axis projection from the Laplacian dup matrix X_w, X_v = EigUtil.principal_eigh(np.linalg.pinv(L_dup)) L_dup_x = X_v * math.sqrt(X_w) # get masses summing to one m = np.array([1] * (n - 1) + [2], dtype=float) / (n + 1) # get the principal axis projection using the weight formula M = np.diag(np.sqrt(m)) L_pinv = np.linalg.pinv(L) I = np.eye(n, dtype=float) E = I - np.outer(np.ones(n, dtype=float), m) ME = np.dot(M, E) Q = np.dot(ME, np.dot(L_pinv, ME.T)) Q_w, Q_v = EigUtil.principal_eigh(Q) Q_x = Q_v * math.sqrt(Q_w) / np.sqrt(m) # make the response out = StringIO() print >> out, 'Laplacian matrix with pseudo-duplicate node:' print >> out, L_dup print >> out print >> out, 'principal axis projection:' print >> out, L_dup_x print >> out print >> out, 'principal axis projection using the weight formula:' print >> out, Q_x return out.getvalue()
def get_response_content(fs): np.set_printoptions(linewidth=200) n = len(fs.D) # create the distance matrix with the extra node added D_dup = get_duplicate_edm(fs.D) # get the principal axis projection from the distance dup matrix HSH = -(0.5) * MatrixUtil.double_centered(D_dup) X_w, X_v = EigUtil.principal_eigh(HSH) D_dup_x = X_v * math.sqrt(X_w) # get masses summing to one m = np.array([1] * (n - 1) + [2], dtype=float) / (n + 1) # get the principal axis projection using the weight formula M = np.diag(np.sqrt(m)) I = np.eye(n, dtype=float) E = I - np.outer(np.ones(n, dtype=float), m) ME = np.dot(M, E) Q = -(0.5) * np.dot(ME, np.dot(fs.D, ME.T)) Q_w, Q_v = EigUtil.principal_eigh(Q) Q_x = Q_v * math.sqrt(Q_w) / np.sqrt(m) # make the response out = StringIO() print >> out, 'distance matrix with exact duplicate node:' print >> out, D_dup print >> out print >> out, 'principal axis projection:' print >> out, D_dup_x print >> out print >> out, 'principal axis projection using the weight formula:' print >> out, Q_x return out.getvalue()
def get_response_content(fs): np.set_printoptions(linewidth=200) n = len(fs.D) # create the Laplacian matrix L = Euclid.edm_to_laplacian(fs.D) # create the Laplacian matrix with the extra node added L_dup = get_pseudoduplicate_laplacian(L, fs.strength) # get the principal axis projection from the Laplacian dup matrix X_w, X_v = EigUtil.principal_eigh(np.linalg.pinv(L_dup)) L_dup_x = X_v * math.sqrt(X_w) # get masses summing to one m = np.array([1]*(n-1) + [2], dtype=float) / (n+1) # get the principal axis projection using the weight formula M = np.diag(np.sqrt(m)) L_pinv = np.linalg.pinv(L) I = np.eye(n, dtype=float) E = I - np.outer(np.ones(n, dtype=float), m) ME = np.dot(M, E) Q = np.dot(ME, np.dot(L_pinv, ME.T)) Q_w, Q_v = EigUtil.principal_eigh(Q) Q_x = Q_v * math.sqrt(Q_w) / np.sqrt(m) # make the response out = StringIO() print >> out, 'Laplacian matrix with pseudo-duplicate node:' print >> out, L_dup print >> out print >> out, 'principal axis projection:' print >> out, L_dup_x print >> out print >> out, 'principal axis projection using the weight formula:' print >> out, Q_x return out.getvalue()
def get_response_content(fs): # build the newick tree from the string tree = NewickIO.parse(fs.tree_string, FelTree.NewickTree) nvertices = len(list(tree.preorder())) nleaves = len(list(tree.gen_tips())) # get ordered ids with the leaves first ordered_ids = get_ordered_ids(tree) # get the adjacency matrix and the augmented adjacency matrix A = np.array(tree.get_affinity_matrix(ordered_ids)) A_aug = get_augmented_adjacency(A, nleaves, fs.ndups, fs.strength) # get the laplacian matrices L = Euclid.adjacency_to_laplacian(A) L_aug = Euclid.adjacency_to_laplacian(A_aug) # get the schur complement R = SchurAlgebra.mschur(L, set(range(nleaves, nvertices))) R_pinv = np.linalg.pinv(R) vals, vecs = EigUtil.eigh(R_pinv) # get the scaled Fiedler vector for the Schur complement w, v = EigUtil.principal_eigh(R_pinv) fiedler = v * math.sqrt(w) # get the eigendecomposition of the augmented Laplacian L_aug_pinv = np.linalg.pinv(L_aug) vals_aug, vecs_aug = EigUtil.eigh(L_aug_pinv) # get the scaled Fiedler vector for the augmented Laplacian w_aug, v_aug = EigUtil.principal_eigh(L_aug_pinv) fiedler_aug = v_aug * math.sqrt(w_aug) # report the results np.set_printoptions(linewidth=300) out = StringIO() print >> out, 'Laplacian matrix:' print >> out, L print >> out print >> out, 'Schur complement of Laplacian matrix:' print >> out, R print >> out print >> out, 'scaled Fiedler vector of Schur complement:' print >> out, fiedler print >> out print >> out, 'eigenvalues of pinv of Schur complement:' print >> out, vals print >> out print >> out, 'corresponding eigenvectors of pinv of Schur complement:' print >> out, np.array(vecs).T print >> out print >> out print >> out, 'augmented Laplacian matrix:' print >> out, L_aug print >> out print >> out, 'scaled Fiedler vector of augmented Laplacian:' print >> out, fiedler_aug print >> out print >> out, 'eigenvalues of pinv of augmented Laplacian:' print >> out, vals_aug print >> out print >> out, 'rows are eigenvectors of pinv of augmented Laplacian:' print >> out, np.array(vecs_aug) return out.getvalue()
def get_response_content(fs): # build the newick tree from the string tree = NewickIO.parse(fs.tree_string, FelTree.NewickTree) nvertices = len(list(tree.preorder())) nleaves = len(list(tree.gen_tips())) # get ordered ids with the leaves first ordered_ids = get_ordered_ids(tree) # get the distance matrix and the augmented distance matrix D = np.array(tree.get_partial_distance_matrix(ordered_ids)) D_aug = get_augmented_distance(D, nleaves, fs.ndups) # get the laplacian matrix L = Euclid.edm_to_laplacian(D) # get the schur complement R = SchurAlgebra.mschur(L, set(range(nleaves, nvertices))) R_pinv = np.linalg.pinv(R) vals, vecs = EigUtil.eigh(R_pinv) # get the scaled Fiedler vector for the Schur complement w, v = EigUtil.principal_eigh(R_pinv) fiedler = v * math.sqrt(w) # get the eigendecomposition of the centered augmented distance matrix L_aug_pinv = Euclid.edm_to_dccov(D_aug) vals_aug, vecs_aug = EigUtil.eigh(L_aug_pinv) # get the scaled Fiedler vector for the augmented Laplacian w_aug, v_aug = EigUtil.principal_eigh(L_aug_pinv) fiedler_aug = v_aug * math.sqrt(w_aug) # report the results np.set_printoptions(linewidth=300, threshold=10000) out = StringIO() print >> out, "Laplacian matrix:" print >> out, L print >> out print >> out, "Schur complement of Laplacian matrix:" print >> out, R print >> out print >> out, "scaled Fiedler vector of Schur complement:" print >> out, fiedler print >> out print >> out, "eigenvalues of pinv of Schur complement:" print >> out, vals print >> out print >> out, "corresponding eigenvectors of pinv of Schur complement:" print >> out, np.array(vecs).T print >> out print >> out print >> out, "augmented distance matrix:" print >> out, D_aug print >> out print >> out, "scaled Fiedler vector of augmented Laplacian limit:" print >> out, fiedler_aug print >> out print >> out, "eigenvalues of pinv of augmented Laplacian limit:" print >> out, vals_aug print >> out print >> out, "rows are eigenvectors of pinv of augmented Laplacian limit:" print >> out, np.array(vecs_aug) return out.getvalue()
def get_response_content(fs): # build the newick tree from the string tree = NewickIO.parse(fs.tree_string, FelTree.NewickTree) nvertices = len(list(tree.preorder())) nleaves = len(list(tree.gen_tips())) # get ordered ids with the leaves first ordered_ids = get_ordered_ids(tree) # get the adjacency matrix and the augmented adjacency matrix A = np.array(tree.get_affinity_matrix(ordered_ids)) A_aug = get_augmented_adjacency(A, nleaves, fs.strength) # get the laplacian matrices L = Euclid.adjacency_to_laplacian(A) L_aug = Euclid.adjacency_to_laplacian(A_aug) # get the schur complements R = SchurAlgebra.mschur(L, set(range(nleaves, nvertices))) R_aug = SchurAlgebra.mschur(L_aug, set(range(nleaves, nvertices))) # get the scaled Fiedler vectors w, v = EigUtil.principal_eigh(np.linalg.pinv(R)) fiedler = v * math.sqrt(w) w_aug, v_aug = EigUtil.principal_eigh(np.linalg.pinv(R_aug)) fiedler_aug = v_aug * math.sqrt(w_aug) # report the results np.set_printoptions(linewidth=200) out = StringIO() print >> out, 'Laplacian matrix:' print >> out, L print >> out print >> out, 'Schur complement of Laplacian matrix:' print >> out, R print >> out print >> out, 'scaled Fiedler vector:' print >> out, fiedler print >> out print >> out, 'augmented Laplacian matrix:' print >> out, L_aug print >> out print >> out, 'Schur complement of augmented Laplacian matrix:' print >> out, R_aug print >> out print >> out, 'scaled Fiedler vector of augmented matrix:' print >> out, fiedler_aug print >> out return out.getvalue()
def get_response_content(fs): # build the newick tree from the string tree = NewickIO.parse(fs.tree_string, FelTree.NewickTree) nvertices = len(list(tree.preorder())) nleaves = len(list(tree.gen_tips())) # get ordered ids with the leaves first ordered_ids = get_ordered_ids(tree) # get the adjacency matrix and the augmented adjacency matrix A = np.array(tree.get_affinity_matrix(ordered_ids)) A_aug = get_augmented_adjacency(A, nleaves, fs.strength) # get the laplacian matrices L = Euclid.adjacency_to_laplacian(A) L_aug = Euclid.adjacency_to_laplacian(A_aug) # get the schur complements R = SchurAlgebra.mschur(L, set(range(nleaves, nvertices))) R_aug = SchurAlgebra.mschur(L_aug, set(range(nleaves, nvertices))) # get the scaled Fiedler vectors w, v = EigUtil.principal_eigh(np.linalg.pinv(R)) fiedler = v * math.sqrt(w) w_aug, v_aug = EigUtil.principal_eigh(np.linalg.pinv(R_aug)) fiedler_aug = v_aug * math.sqrt(w_aug) # report the results np.set_printoptions(linewidth=200) out = StringIO() print >> out, "Laplacian matrix:" print >> out, L print >> out print >> out, "Schur complement of Laplacian matrix:" print >> out, R print >> out print >> out, "scaled Fiedler vector:" print >> out, fiedler print >> out print >> out, "augmented Laplacian matrix:" print >> out, L_aug print >> out print >> out, "Schur complement of augmented Laplacian matrix:" print >> out, R_aug print >> out print >> out, "scaled Fiedler vector of augmented matrix:" print >> out, fiedler_aug print >> out return out.getvalue()