def compute_and_measure_graphs(p): graph = nx.DiGraph() t_triples, t_cardinality = hdt.search_triples("", p, "") print("amount of triples: ", t_cardinality) for (l, p, r) in t_triples: if p == "http://www.w3.org/2000/01/rdf-schema#subClassOf": graph.add_edge(r, l) # avc = nx.average_clustering(graph) # average clustering coefficient # trans = nx.transitivity(graph) # pearson = nx.degree_pearson_correlation_coefficient(graph) reaching = nx.global_reaching_centrality(graph) return reaching # compute SCC # mydict = {} # for n in graph.nodes: # collect_succssor = [] # for s in graph.successors(n): # collect_succssor.append(s) # mydict[n] = collect_succssor # sccs = tarjan(mydict) # filter_scc = [x for x in sccs if len(x)>1] # # compute the counter for ths filtered SCC # ct = Counter() # for f in filter_scc: # ct[len(f)] += 1 # print ('SCC info: ', ct) # # obtain the corresponding scc graphs # scc_graphs = [] # for s in filter_scc: # g = graph.subgraph(s).copy() # scc_graphs.append(g) return filter_scc, scc_graphs
def func(G): """ The function being optimized by composite graphs. """ n = G.number_of_nodes() return (logpaths(G) / n)**0.5 - 2 * nx.global_reaching_centrality( G) - nx.average_clustering(G)
def draw_graph(g): weight = [g[u][v]['weight'] / 5 for u, v in g.edges()] # pos = nx.kamada_kawai_layout(g) pos = nx.spring_layout(g) # plt.subplot(111) # nx.draw(g, pos, with_labels=True, width=weight) # plt.show() connectivity = nx.algebraic_connectivity(g) centrality = nx.global_reaching_centrality(g) # print(f'density = {nx.density(g):.4f}') return connectivity, centrality
def erdos_renyi_grc(G): grc_test = [] N = nx.number_of_nodes(G) E = nx.number_of_edges(G) prob_edges = ((N * (N - 1)) / 2) / E for i in range(500): er_graph = nx.erdos_renyi_graph( N, prob_edges, seed=10) #number of nodes, probability for edge creation grc = nx.global_reaching_centrality(er_graph) grc_test.append(grc) print grc_test
def obtenerValores(dirigido, noDirigido): # variables locales datos = [] m = 0 c = 0 dm = 0 com = 0 # 1; orden - ambas #print("orden") datos.append(str(dirigido.order())) # 2; tamaño - dirigida #print("tamaño") datos.append(str(dirigido.size())) # 3; densidad, dirigida #print("densidad") datos.append(str(nx.density(dirigido))) # 4; grado promedio - dirigido #print("grado promedio") datos.append(str((dirigido.size()) / (dirigido.order()))) # 5; diametro - no dirigido #print("diametro") datos.append(str(nx.diameter(noDirigido))) # 6; radio - no dirigido #print("radio") datos.append(str(nx.radius(noDirigido))) # 7; tamaño de clique mas grande - no dirigida #print("clique mas grande") datos.append(str(nx.graph_clique_number(noDirigido))) # 8; numero de cliques maximales - no dirigida #print("cliques maximales") datos.append(str(nx.graph_number_of_cliques(noDirigido))) # 9; global reaching centrality - dirigido #print("reachability") datos.append(str(nx.global_reaching_centrality(dirigido))) # 10; clustering coefficient - dirigida #print("clustering") datos.append(str(nx.average_clustering(dirigido))) # 11; transitividad - dirigida #print("transitivity") datos.append(str(nx.transitivity(dirigido))) # 12; 13; 14; datos MODC: modularidad, dependencia minima, total de comunidades - no dirigido #print("MODC") (m, dm, com) = MODC(noDirigido, True) datos.append(str(m)) datos.append(str(dm)) datos.append(str(com)) # fin de funcion return (datos)
def features_vector(file): #self base_list = name_base(path_pdb, file) for i in range(len(base_list)): print base_list[i] G = input_nx(path_data, base_list[i]) L = laplacian_walk(G) er_lap = erdos_renyi_lap(G) H = load_hydrophobic(path_data, base_list[i]) C = load_charged(path_data, base_list[i]) A = alpha_content(path_pdb, base_list[i]) B = beta_content(path_pdb, base_list[i]) with open((feat_path + ('features_' + base_list[i] + '.txt')), 'w') as w: #w.write(str(float(nx.number_of_edges(G))/float(nx.number_of_nodes(G)))+'\n') #removed avg degree try: w.write( str( nx.average_shortest_path_length(G) / (((math.log(float(nx.number_of_nodes(G)))) - 0.5772) / ((math.log(float(nx.number_of_edges(G))) / (float(nx.number_of_nodes(G)))) + 0.5))) + '\n') #avg shortest path length w.write( str( nx.diameter(G) / (math.log((float(nx.number_of_nodes(G)))) / (math.log( float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G)))))) + '\n') #diameter w.write( str(nx.radius(G) / nx.average_shortest_path_length(G)) + '\n') #radius (min. shortest path) w.write( str( nx.average_clustering(G) / ((float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G))) / float(nx.number_of_nodes(G)))) + '\n') #clustering coefficient of random graph #***Number of quasi-rigid domains w.write(str(nx.degree_assortativity_coefficient(G)) + '\n') #assortativity coefficient w.write(str(nx.global_reaching_centrality(G)) + '\n') #nx.degree_centrality(G) #***Residue intrinsic dimensionality (may be used to compute 6.?) w.write( str(L / er_lap) + '\n' ) #Normalized laplacian walk from function, N = D^{-1/2} L D^{-1/2} as well with ER w.write(str(float(A) / (float(A) + float(B))) + '\n') #alpha content w.write(str(float(B) / (float(A) + float(B))) + '\n') #beta content w.write( str((float(nx.number_of_edges(H)) / float(nx.number_of_nodes(H))) / (float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G)))) + '\n' ) #***Average degree of hydrophobic residues (F,M,W,I,V,L,P,A) norm w.write( str((float(nx.average_clustering(G)) / ((float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G))) / float(nx.number_of_nodes(G)))) / (float(nx.average_clustering(H)) / ((float(nx.number_of_edges(H)) / float(nx.number_of_nodes(H))) / float(nx.number_of_nodes(H))))) + '\n' ) #clustering coefficient of random graph - hydrophobic residues norm w.write( str(nx.global_reaching_centrality(H)) + '\n' ) #global, Average local reaching centrality of hydrophobic residues w.write( str((float(nx.number_of_edges(C)) / float(nx.number_of_nodes(C))) / (float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G)))) + '\n' ) #***Average degree of charged residues (R,D,E,H,K) norm try: w.write( str((float(nx.average_clustering(G)) / ((float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G))) / float(nx.number_of_nodes(G)))) / (float(nx.average_clustering(C)) / ((float(nx.number_of_edges(C)) / float(nx.number_of_nodes(C))) / float(nx.number_of_nodes(C))))) + '\n' ) #clustering coefficient of random graph - charged residues norm except ZeroDivisionError: w.write('0.0' + '\n') w.write( str(nx.global_reaching_centrality(C)) + '\n' ) #global, Average local reaching centrality of charged residues p = (float(nx.number_of_edges(G)) / float(nx.number_of_nodes(G))) / ( float(nx.number_of_nodes(G)) - 1) X = [x[1] for x in G.degree()] w.write( str( np.var(X, dtype=np.float64) / (float(nx.number_of_nodes(G)) * p * (1 - p))) + '\n') #variance w.write( str( skew(X, bias=False) / ((1 - (2 * p)) / (math.sqrt( ((float(nx.number_of_nodes(G)) - 1) * p) * (1 - p))))) + '\n') #skewness except nx.exception.NetworkXError: pass #graph is not connected print base_list[ i], ' *.txt file has been saved! (original PDB, hydrophobic, charged)'
} fun_assortativity_measures = { "average_neighbor_degree": nx.average_neighbor_degree } fun_global_measures = { "global_density": lambda G: dict(zip(G.nodes, [nx.density(G.to_undirected())] * len(G))), "global_average_shortest_path_length": lambda G: dict( zip(G.nodes, [nx.average_shortest_path_length(G.to_undirected())] * len(G))), "global_reaching_centrality": lambda G: dict( zip(G.nodes, [nx.global_reaching_centrality(G.to_undirected())] * len( G))), "global_degree_assortativity_coefficient": lambda G: dict( zip(G.nodes, [nx.degree_assortativity_coefficient(G.to_undirected())] * len(G))), "global_degree_pearson_correlation_coefficient": lambda G: dict( zip(G.nodes, [ nx.degree_pearson_correlation_coefficient(G.to_undirected()) ] * len(G))), "global_transitivity": lambda G: dict(zip(G.nodes, [nx.transitivity(G.to_undirected())] * len(G)) ), "global_average_clustering": lambda G: dict(
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 test_non_positive_weights(self): with pytest.raises(nx.NetworkXError): G = nx.DiGraph() nx.global_reaching_centrality(G, weight="weight")
def test_cycle_directed_weighted(self): G = nx.DiGraph() G.add_weighted_edges_from([(1, 2, 1), (2, 1, 1)]) assert nx.global_reaching_centrality(G) == 0
def test_cycle_undirected_unweighted(self): G = nx.Graph() G.add_edge(1, 2) assert nx.global_reaching_centrality(G, weight=None) == 0
def test_negatively_weighted(self): with pytest.raises(nx.NetworkXError): G = nx.Graph() G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)]) nx.global_reaching_centrality(G, weight="weight")