def test_normalized_laplacian_spectrum(self): "Normalized Laplacian eigenvalues" evals = numpy.array([0, 0, 0.7712864461218, 1.5, 1.7287135538781]) e = sorted(nx.normalized_laplacian_spectrum(self.G)) npt.assert_almost_equal(e, evals) e = sorted(nx.normalized_laplacian_spectrum(self.WG, weight=None)) npt.assert_almost_equal(e, evals) e = sorted(nx.normalized_laplacian_spectrum(self.WG)) npt.assert_almost_equal(e, evals) e = sorted(nx.normalized_laplacian_spectrum(self.WG, weight='other')) npt.assert_almost_equal(e, evals)
def cut(graph): if (len(graph.nodes) < 2): return list(graph.nodes) laplacian = nx.normalized_laplacian_matrix(graph).toarray() eignvector = np.linalg.eigh(laplacian)[1][:, 1] eignvalues = nx.normalized_laplacian_spectrum(graph) #print("The second smallest eignvalue: {0}\nEignvector: {1}".format(result[0][1], eignvector)) print("Eignvalues {0}".format(eignvalues)) print("Eignvector {0}".format(eignvector)) histogram = np.histogram(eignvalues, bins=np.size(eignvalues), density=True) if len(eignvalues) > 2: ratio = eignvalues[2] - eignvalues[1] else: ratio = eignvalues[1] - eignvalues[1] print("Ratio {0:.2f}".format(ratio)) threshold = 0 #np.mean(eignvector) res = input("Divide?") if res == 'n': #(ratio < 0.06) :#((eignvector < threshold).all() or (eignvector >= threshold).all()) or (len(graph.nodes) <= 2): #len(graph.nodes) <= 2 return list(graph.nodes) else: sub1 = graph.copy() sub2 = graph.copy() sub1.remove_nodes_from( np.array(list(graph.nodes))[(eignvector < threshold)]) sub2.remove_nodes_from( np.array(list(graph.nodes))[(eignvector >= threshold)]) return [cut(sub1), cut(sub2)]
def computeFeatures(self, G, features_nx, features_nk): if("effective_graph_resistance" in features_nx or "nb_spanning_trees" in features_nx or "algebraic_connectivity" in features_nx): if(self.params["verbose"]): print("Computing laplacian_eigenvalues") s = time.time() self.eigenvalues["laplacian"] = np.real(nx.laplacian_spectrum(G)) if(self.params["verbose"]): print("Finish laplacian_eigenvalues (%s)" % (timeFormat(time.time()-s))) if("largest_eigenvalue" in features_nx or "symmetry_ratio" in features_nx or "natural_connectivity" in features_nx): if(self.params["verbose"]): print("Computing adjacency_eigenvalues") s = time.time() self.eigenvalues["adjacency"] = np.real(nx.adjacency_spectrum(G)) if(self.params["verbose"]): print("Finish adjacency_eigenvalues (%s)" % (timeFormat(time.time()-s))) if("weighted_spectrum_3" in features_nx or "weighted_spectrum_4" in features_nx): if(self.params["verbose"]): print("Computing normalized_laplacian_eigenvalues") s = time.time() self.eigenvalues["normalized_laplacian"] = np.real(nx.normalized_laplacian_spectrum(G)) if(self.params["verbose"]): print("Finish normalized_laplacian_eigenvalues (%s)" % (timeFormat(time.time()-s))) return(NodesFeatures.computeFeatures(self, G, features_nx, features_nk))
def weighted_spectrum(G, nodes, n=3, eigenvalues=None): normalized_laplacian_eigenvalues = None if(not eigenvalues is None): normalized_laplacian_eigenvalues = eigenvalues["normalized_laplacian"] if(normalized_laplacian_eigenvalues is None): normalized_laplacian_eigenvalues = np.real(nx.normalized_laplacian_spectrum(G)) ws = np.sum((1-normalized_laplacian_eigenvalues)**n) return(np.float64(ws))
N = 15 g = regular_2D_lattice(N) g2 = regular_2D_lattice_8_neighbors(N) #g2 = random_edge_suppression(N,5*N) G2 = g2.to_networkx().to_undirected() L2 = nx.normalized_laplacian_matrix(G2) e = numpy.linalg.eigvals(L2.A) fig, ax = plt.subplots() s = np.sort(e) l, = plt.plot( np.sort(nx.normalized_laplacian_spectrum(g.to_networkx().to_undirected()))) l, = plt.plot(np.sort(e), lw=2) ax = plt.axis([0, len(e), -0.5, np.max(e) * 1.5]) axamp = plt.axes([0.25, .03, 0.50, 0.02]) axamp2 = plt.axes([0.25, 0, 0.50, 0.02]) # Slider samp = Slider(axamp, ' + b on diag', -1.5, 1.5, valinit=0) samp2 = Slider(axamp2, 'Theta', -.5, 1.5, valinit=1) def update(val): # amp is the current value of the slider amp = samp.val
def laplacian_spectrum(self, ): return nx.normalized_laplacian_spectrum(self.undirected_g)
def normalized_laplacian_spectrum(play: dict): G = nx.Graph(big_umat_df(play)) res = nx.normalized_laplacian_spectrum(G) return res