def test_smetric(): g = nx.Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(2, 4) g.add_edge(1, 4) sm = nx.s_metric(g, normalized=False) assert_equal(sm, 19.0) smNorm = nx.s_metric(g, normalized=True)
def test_smetric(): g = nx.Graph() g.add_edge(1,2) g.add_edge(2,3) g.add_edge(2,4) g.add_edge(1,4) sm = nx.s_metric(g,normalized=False) assert_equal(sm, 19.0) smNorm = nx.s_metric(g,normalized=True)
def test_li_smax(): G = networkx.barabasi_albert_graph(25,1) #Any old graph Gdegseq = G.degree().values() #degree sequence Gdegseq.sort(reverse=True) assert_true(not (sum(Gdegseq)%2)) #Tests the 'unconstrained version' Gmax = networkx.li_smax_graph(Gdegseq) Gmaxdegseq = Gmax.degree().values() Gmaxdegseq.sort(reverse=True) assert_equal(G.order(),Gmax.order()) #Sanity Check on the nodes assert_equal(Gdegseq,Gmaxdegseq) #make sure both graphs have the same degree sequence assert_true(networkx.s_metric(G) <= networkx.s_metric(Gmax)) #make sure the smax graph is actually bigger
def test_li_smax(): G = networkx.barabasi_albert_graph(25, 1) #Any old graph Gdegseq = sorted(G.degree().values(), reverse=True) #degree sequence # Tests the 'unconstrained version' assert_true(not (sum(Gdegseq) % 2)) Gmax = networkx.li_smax_graph(Gdegseq) Gmaxdegseq = sorted(Gmax.degree().values(), reverse=True) assert_equal(G.order(), Gmax.order()) #Sanity Check on the nodes # make sure both graphs have the same degree sequence assert_equal(Gdegseq, Gmaxdegseq) # make sure the smax graph is actually bigger assert_true(networkx.s_metric(G) <= networkx.s_metric(Gmax))
def networkxReport(networkxObj,reportFileName=None): import collections report = {} report['density'] = networkx.density(networkxObj) report['degDist'] = { 'decription' : "degree distribution", 'value': collections.Counter([d for n, d in networkxObj.degree()]) } report['RCC'] = { 'decription': "For each degree k, the rich-club coefficient is the ratio of the number of actual to the number of potential edges for nodes with degree greater than k", 'value': networkx.rich_club_coefficient(networkx.Graph(networkxObj), normalized=True) } report['s-Metric'] = { 'decription': "The s-metric is defined as the sum of the products deg(u)*deg(v) for every edge (u,v) in G", 'value' : networkx.s_metric(networkxObj, normalized=False) } report['degree-centrality'] = { 'decription':"The degree centrality for a node v is the fraction of nodes it is connected to", 'value' : networkx.degree_centrality(networkxObj) } if reportFileName: f = open(reportFileName, "w") f.write(json.dumps(report)) f.close() return report
def compute_model_graph_metrics(model_name, dataset, root_dir, epoch, model_location): model = torch.load(model_location) if dataset == 'mnist': input_dim = (1, 1, 28, 28) elif dataset == 'cifar10': input_dim = (1, 3, 32, 32) param_info = get_model_param_info(model_name, dataset) architecture = model_name + "_" + dataset if (architecture not in model_graph_dict) or (epoch == 0): print(("Architecture: {} not found, creating").format(architecture)) NNG = nn_graph.NNGraph() NNG.parameter_graph(model, param_info, input_dim, ignore_zeros=True) s_metric = nx.s_metric(NNG.G, normalized=False) degree_assortativity_coefficient = nx.degree_assortativity_coefficient( NNG.G) diameter = nx.diameter(NNG.G) global_efficiency = nx.global_efficiency(NNG.G) return [ s_metric, degree_assortativity_coefficient, diameter, global_efficiency ]
def test_smetric(): g = nx.Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(2, 4) g.add_edge(1, 4) sm = nx.s_metric(g) assert_equal(sm, 19)
def test_smetric(): g = nx.Graph() g.add_edge(1,2) g.add_edge(2,3) g.add_edge(2,4) g.add_edge(1,4) sm = nx.s_metric(g) assert_equal(sm, 19)
def test_smetric(): g = nx.Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(2, 4) g.add_edge(1, 4) sm = nx.s_metric(g, normalized=False) assert sm == 19.0
def compute_features(self): # s metric self.add_feature( "s_metric", lambda graph: nx.s_metric(graph, normalized=False), "The s-metric is defined as the sum of the products deg(u)*deg(v) for every edge (u,v) in G", InterpretabilityScore(4), )
def GetGlobalGraphDescriptors(UndirectedGraph): cG = UndirectedGraph container = [] featuresList = [nx.density, nx.diameter, nx.radius, GAI, ABC, ECI] for feat in featuresList: container.append(feat(cG)) container.append(nx.s_metric(cG, normalized=False)) return container
def circuit_analysis(depth, trail, varying_param): if gdv_name == "TFL": folder = "BNTF" depth_string = "{:02}".format(depth) name_end = "TFL" if nr_qubits == 54: name_end = "QSE" elif gdv_name == "QSE": folder = "BSS" depth_string = "{:03}".format(depth) name_end = "QSE" # if nr_qubits==54: # gdv_name = "QSE" qasm_file_name = "_private_benchmark/{}/{}QBT_{}CYC_{}_{}.qasm".format( folder, nr_qubits, depth_string, name_end, trail) solution_file_name = "_private_benchmark/meta/{}QBT_{}CYC_{}_{}_solution.csv".format( nr_qubits, depth_string, name_end, trail) # print("qiskit", depth) # input qasm file as circuit test_circuit = qiskit.QuantumCircuit.from_qasm_file(qasm_file_name) """ NetworkX analysis data """ dag_circ = circuit_to_dag(test_circuit) undirect = dag_circ._multi_graph.to_undirected(as_view=True) weighted = convert_to_weighted_graph(dag_circ._multi_graph) # import matplotlib.pyplot as plt # # nx.draw_networkx(undirect, with_labels=False, node_size=10) # nx.draw_networkx(dag_circ._multi_graph, with_labels=False, node_size=10) # plt.show() return max(nx.pagerank(weighted).values()),\ nx.number_connected_components(undirect),\ undirect.number_of_edges(),\ undirect.number_of_nodes(), \ nx.global_efficiency(weighted), \ nx.s_metric(dag_circ._multi_graph, False)
def a_s_metric(G): return nx.s_metric(G, normalized=False)
def test_normalized(): sm = nx.s_metric(nx.Graph(), normalized=True)
def test_normalized(): with pytest.raises(nx.NetworkXError): sm = nx.s_metric(nx.Graph(), normalized=True)
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
def s_metric(G, posG, stages, stage=None): """ Computes the s-metric (not normalized). """ Graph = G if stage is None else gu.get_subgraph(G, posG, stages, stage) return nx.s_metric(Graph, normalized=False)
def s_metric(graph): """Returns the s-metric of graph """ return nx.s_metric(graph.graph, normalized=False)
def test_normalized(): sm = nx.s_metric(nx.Graph(),normalized=True)