def test_modularity_spectrum(self): "Modularity eigenvalues" evals = numpy.array([-1.5, 0., 0.]) e = sorted(nx.modularity_spectrum(self.P)) npt.assert_almost_equal(e, evals) # Directed modularity eigenvalues evals = numpy.array([-0.5, 0., 0.]) e = sorted(nx.modularity_spectrum(self.DG)) npt.assert_almost_equal(e, evals)
def test_modularity_spectrum(self): "Modularity eigenvalues" evals=numpy.array([-1.5, 0., 0.]) e=sorted(nx.modularity_spectrum(self.P)) assert_almost_equal(e,evals) # Directed modularity eigenvalues evals=numpy.array([-0.5, 0., 0.]) e=sorted(nx.modularity_spectrum(self.DG)) assert_almost_equal(e,evals)
# reference: https://brilliant.org/wiki/bellman-ford-algorithm/ print(startBlue + '\nBellman Ford path from Los Angeles:\n' + endColor, nx.bellman_ford_predecessor_and_distance(G, 'Los Angeles')) # Linear Algebra (Eigenvalues): # reference: https://networkx.github.io/documentation/stable/reference/linalg.html # defined: Using scaler multiplication (matrix multiplication = scaler multiplication) to create a new figure, # utilizing Eigenvalues and Eigenvectors # reference: https://www.youtube.com/watch?v=vs2sRvSzA3o # Real world use-case: To scale a model to a real-world dataset or graph # Reference: http://barabasi.com/f/94.pdf # Appears that utilizing the modularity spectrum, groups can be assigned into clusters/networks # reference: https://en.wikipedia.org/wiki/Modularity_(networks) print( startBlue + '\nThe Modularity Spectrum that returns eigenvalues of the modularity matrix of G:\n' + endColor, nx.modularity_spectrum(G)) print( startCyan + '\n----------------------------------------- Transforming Graph into Eulerian Graph' + '-----------------------------------------' + endColor, '\n') # reference: https://networkx.github.io/documentation/stable/reference/algorithms/euler.html # Use-Case example: The purpose of the proposed new roads is to make the town mailman-friendly. In graph theory terms, # we want to change the graph so it contains an Euler circuit. This is also referred to as Eulerizing a graph. The # most mailman-friendly graph is the one with an Euler circuit since it takes the mailman back to the starting point. # This means that the mailman can leave his car at one intersection, walk the route hitting all the streets just once, # and end up where he began. There is no backtracking or walking of streets twice. This saves him time. # reference: https://study.com/academy/lesson/eulerizing-graphs-in-math.html # reference for below weighted graph code: # https://networkx.github.io/documentation/networkx-1.10/examples/drawing/weighted_graph.html
def features_part1(info): """ second set of features. """ G = info['G'] n = info['num_nodes'] num_units = info['num_units'] edges = info['edges'] nedges = len(edges) res = dict() res['num_nodes'] = n res['num_edges'] = nedges res['reduce_frac'] = num_units / n - 1 res['edges_per_node'] = nedges / n res['density'] = nx.density(G) res['transitivity'] = nx.transitivity(G) res['average_clustering'] = nx.average_clustering(G) res['average_node_connectivity'] = nx.average_node_connectivity(G) res['average_shortest_path_length'] = nx.average_shortest_path_length(G) res['s_metric_norm'] = np.sqrt(nx.s_metric(G, normalized=False) / nedges) res['global_reaching_centrality'] = nx.global_reaching_centrality(G) res['edge_connectivity'] = nx.edge_connectivity(G, 0, n - 1) res['modularity_trace'] = np.real(np.sum(nx.modularity_spectrum(G))) stages = info['stages'] stagedict = get_stage_dict(n, stages) edges_diff = np.array([stagedict[j] - stagedict[i] for (i, j) in edges]) n0 = np.sum(edges_diff == 0) n1 = np.sum(edges_diff == 1) n2 = np.sum(edges_diff == 2) res['intrastage'] = n0 / nedges res['interstage'] = n1 / nedges res['hops_per_node'] = n2 / n degrees = np.array(nx.degree(G))[:, 1] res['mean_degree'] = np.mean(degrees) res['std_degree'] = np.std(degrees) res['span_degree'] = np.amax(degrees) / np.amin(degrees) triadic = nx.triadic_census(G) res['021D'] = triadic['021D'] / nedges res['021U'] = triadic['021U'] / nedges res['021C'] = triadic['021C'] / nedges res['030T'] = triadic['030T'] / nedges paths_nums = pathhist(G) ns = np.arange(1, n + 1) paths_total = np.sum(paths_nums) mean_path = np.sum(ns * paths_nums) / paths_total mean_pathsqr = np.sum(ns**2 * paths_nums) / paths_total std_path = np.sqrt(mean_pathsqr - mean_path**2) nonz = np.nonzero(paths_nums)[0] shortest_path = nonz[0] + 1 longest_path = nonz[-1] + 1 res['log_paths'] = np.log(paths_total) res['mean_path'] = mean_path res['std_paths'] = std_path res['min_path'] = shortest_path res['max_path'] = longest_path res['span_path'] = longest_path / shortest_path return res