def compute_singlevalued_measures(ntwk, weighted=True, calculate_cliques=False): """ Returns a single value per network """ iflogger.info("Computing single valued measures:") measures = {} iflogger.info("...Computing degree assortativity (pearson number) ...") try: measures["degree_pearsonr"] = nx.degree_pearsonr(ntwk) except AttributeError: # For NetworkX 1.6 measures["degree_pearsonr"] = nx.degree_pearson_correlation_coefficient(ntwk) iflogger.info("...Computing degree assortativity...") try: measures["degree_assortativity"] = nx.degree_assortativity(ntwk) except AttributeError: measures["degree_assortativity"] = nx.degree_assortativity_coefficient(ntwk) iflogger.info("...Computing transitivity...") measures["transitivity"] = nx.transitivity(ntwk) iflogger.info("...Computing number of connected_components...") measures["number_connected_components"] = nx.number_connected_components(ntwk) iflogger.info("...Computing average clustering...") measures["average_clustering"] = nx.average_clustering(ntwk) if nx.is_connected(ntwk): iflogger.info("...Calculating average shortest path length...") measures["average_shortest_path_length"] = nx.average_shortest_path_length(ntwk, weighted) if calculate_cliques: iflogger.info("...Computing graph clique number...") measures["graph_clique_number"] = nx.graph_clique_number(ntwk) # out of memory error return measures
def degree_assortativity(G): #this wrapper helps avoid error due to change in interface name if hasattr(nx, 'degree_assortativity_coefficient'): return nx.degree_assortativity_coefficient(G) elif hasattr(nx, 'degree_assortativity'): return nx.degree_assortativity(G) else: raise ValueError, 'Cannot compute degree assortativity: method not available'
def compute_singlevalued_measures(ntwk, weighted=True, calculate_cliques=False): """ Returns a single value per network """ iflogger.info("Computing single valued measures:") measures = {} iflogger.info("...Computing degree assortativity (pearson number) ...") try: measures["degree_pearsonr"] = nx.degree_pearsonr(ntwk) except AttributeError: # For NetworkX 1.6 measures[ "degree_pearsonr"] = nx.degree_pearson_correlation_coefficient( ntwk) iflogger.info("...Computing degree assortativity...") try: measures["degree_assortativity"] = nx.degree_assortativity(ntwk) except AttributeError: measures["degree_assortativity"] = nx.degree_assortativity_coefficient( ntwk) iflogger.info("...Computing transitivity...") measures["transitivity"] = nx.transitivity(ntwk) iflogger.info("...Computing number of connected_components...") measures["number_connected_components"] = nx.number_connected_components( ntwk) iflogger.info("...Computing graph density...") measures["graph_density"] = nx.density(ntwk) iflogger.info("...Recording number of edges...") measures["number_of_edges"] = nx.number_of_edges(ntwk) iflogger.info("...Recording number of nodes...") measures["number_of_nodes"] = nx.number_of_nodes(ntwk) iflogger.info("...Computing average clustering...") measures["average_clustering"] = nx.average_clustering(ntwk) if nx.is_connected(ntwk): iflogger.info("...Calculating average shortest path length...") measures[ "average_shortest_path_length"] = nx.average_shortest_path_length( ntwk, weighted) else: iflogger.info("...Calculating average shortest path length...") measures[ "average_shortest_path_length"] = nx.average_shortest_path_length( nx.connected_component_subgraphs(ntwk)[0], weighted) if calculate_cliques: iflogger.info("...Computing graph clique number...") measures["graph_clique_number"] = nx.graph_clique_number( ntwk) # out of memory error return measures
def compute_singlevalued_measures(ntwk, weighted=True, calculate_cliques=False): """ Returns a single value per network """ iflogger.info('Computing single valued measures:') measures = {} iflogger.info('...Computing degree assortativity (pearson number) ...') try: measures['degree_pearsonr'] = nx.degree_pearsonr(ntwk) except AttributeError: # For NetworkX 1.6 measures[ 'degree_pearsonr'] = nx.degree_pearson_correlation_coefficient( ntwk) iflogger.info('...Computing degree assortativity...') try: measures['degree_assortativity'] = nx.degree_assortativity(ntwk) except AttributeError: measures['degree_assortativity'] = nx.degree_assortativity_coefficient( ntwk) iflogger.info('...Computing transitivity...') measures['transitivity'] = nx.transitivity(ntwk) iflogger.info('...Computing number of connected_components...') measures['number_connected_components'] = nx.number_connected_components( ntwk) iflogger.info('...Computing graph density...') measures['graph_density'] = nx.density(ntwk) iflogger.info('...Recording number of edges...') measures['number_of_edges'] = nx.number_of_edges(ntwk) iflogger.info('...Recording number of nodes...') measures['number_of_nodes'] = nx.number_of_nodes(ntwk) iflogger.info('...Computing average clustering...') measures['average_clustering'] = nx.average_clustering(ntwk) if nx.is_connected(ntwk): iflogger.info('...Calculating average shortest path length...') measures[ 'average_shortest_path_length'] = nx.average_shortest_path_length( ntwk, weighted) else: iflogger.info('...Calculating average shortest path length...') measures[ 'average_shortest_path_length'] = nx.average_shortest_path_length( nx.connected_component_subgraphs(ntwk)[0], weighted) if calculate_cliques: iflogger.info('...Computing graph clique number...') measures['graph_clique_number'] = nx.graph_clique_number( ntwk) # out of memory error return measures
def compute_singlevalued_measures(ntwk, weighted=True, calculate_cliques=False): """ Returns a single value per network """ iflogger.info('Computing single valued measures:') measures = {} iflogger.info('...Computing degree assortativity (pearson number) ...') try: measures['degree_pearsonr'] = nx.degree_pearsonr(ntwk) except AttributeError: # For NetworkX 1.6 measures[ 'degree_pearsonr'] = nx.degree_pearson_correlation_coefficient( ntwk) iflogger.info('...Computing degree assortativity...') try: measures['degree_assortativity'] = nx.degree_assortativity(ntwk) except AttributeError: measures['degree_assortativity'] = nx.degree_assortativity_coefficient( ntwk) iflogger.info('...Computing transitivity...') measures['transitivity'] = nx.transitivity(ntwk) iflogger.info('...Computing number of connected_components...') measures['number_connected_components'] = nx.number_connected_components( ntwk) iflogger.info('...Computing average clustering...') measures['average_clustering'] = nx.average_clustering(ntwk) if nx.is_connected(ntwk): iflogger.info('...Calculating average shortest path length...') measures[ 'average_shortest_path_length'] = nx.average_shortest_path_length( ntwk, weighted) if calculate_cliques: iflogger.info('...Computing graph clique number...') measures['graph_clique_number'] = nx.graph_clique_number( ntwk) #out of memory error return measures
print G.degree(0) #返回某个节点的度 print G.degree() #返回所有节点的度 print nx.degree_histogram(G) #返回图中所有节点的度分布序列(从1至最大度的出现频次) #~~~~~~~~~~~~~~~~~~~~~~draw the degree distribution~~~~~~~~~~~~~~~# degree = nx.degree_histogram(G) #返回图中所有节点的度分布序列 x = range(len(degree)) #生成x轴序列,从1到最大度 y = [z / float(sum(degree)) for z in degree] #将频次转换为频率,这用到Python的一个小技巧:列表内涵,Python的确很方便:) plt.loglog(x,y,color="blue",linewidth=2) #在双对数坐标轴上绘制度分布曲线 plt.show() #显示图表 #~~~~~~~~~~~~~~~~~~~~~~~~~~output the CC AND PATH~~~~~~~~~~~~~~~~~~# nx.average_clustering(G)#OUTPUT the cluster coefficient #nx.clustering(G) 则可以计算各个节点的群聚系数。 nx.diameter(G) #返回图G的直径(最长最短路径的长度) nx.average_shortest_path_length(G)#返回图G所有节点间平均最短路径长度 nx.degree_assortativity(G) #计算一个图的度匹配性 #~~~~~~~~~~~~~Degree centrality measures~~~~~~~~~~~~~~~~~~~~~~~~~~~# degree_centrality(G) #Compute the degree centrality for nodes. in_degree_centrality(G) #Compute the in-degree centrality for nodes. out_degree_centrality(G) #Compute the out-degree centrality for nodes. closeness_centrality(G[, v, weighted_edges]) # Compute closeness centrality for nodes. betweenness_centrality(G[, normalized, ...]) # Compute betweenness centrality for nodes. edge_betweenness_centrality(G[, normalized, ...]) # Compute betweenness centrality for edges. current_flow_closeness_centrality(G[, ...]) # Compute current-flow closeness centrality for nodes. current_flow_betweenness_centrality(G[, ...]) # Compute current-flow betweenness centrality for nodes. edge_current_flow_betweenness_centrality(G) # Compute current-flow betweenness centrality for edges.
def output_graphmetrics(pathadd,paths,file_name,data_dir): ''' output_graphmetrics() Calculates graph theory metrics from package NetworkX, stores in file and outputs in .csv file. ''' # Graph package #print outputGraphMetrics #if outputGraphMetrics: try: import networkx as nx except ImportError: raise ImportError, "NetworkX required." pathG = nx.Graph() # Get nrows and columns nrows = len(pathadd) ncols = len(pathadd[0]) # Now loop through pathadd rows for irow in xrange(nrows): # Begin loop through pathadd columns for icol in xrange(ncols): # Skip -9999. values if pathadd[irow][icol] != -9999.0: # Get spot node number nodenumber = ncols*irow+icol # Add node to pathG pathG.add_node(nodenumber) # Check neighbors for edges all 8 conditions # Count 0,1,2,3,4,5,6,7 in counter-clockwise # around center cell starting 0 in lower # left corner # Left top corner: if irow == 0 and icol == 0: # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 2 if pathadd[irow+1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Right top corner elif irow == 0 and icol == ncols-1: # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 0 if pathadd[irow+1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Left bottom corner elif irow == nrows-1 and icol == 0: # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 4 if pathadd[irow-1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Right bottom corner elif irow == nrows-1 and icol == ncols-1: # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 6 if pathadd[irow-1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Top side elif irow == 0 and icol != 0 and icol != ncols-1: # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 0 if pathadd[irow+1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 2 if pathadd[irow+1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Left side elif icol == 0 and irow != 0 and irow != nrows-1: # Get neighbors -spots 1,2,3,4,5 # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 2 if pathadd[irow+1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 4 if pathadd[irow-1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Right side elif icol == ncols-1 and irow != 0 and irow != nrows-1: # Get neighbors - spots 0,1,5,6,7 # Get neighbors: spot 0 if pathadd[irow+1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 6 if pathadd[irow-1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Bottom side: elif irow == nrows-1 and icol != 0 and icol != ncols-1: # Get neighbors - spots 3,4,5,6,7 # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 4 if pathadd[irow-1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 6 if pathadd[irow-1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Everything else: else: # Get neighbors: spot 0 if pathadd[irow+1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 1 if pathadd[irow+1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 2 if pathadd[irow+1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow+1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 3 if pathadd[irow][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 4 if pathadd[irow-1][icol+1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol+1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 5 if pathadd[irow-1][icol] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+icol # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 6 if pathadd[irow-1][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow-1)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Get neighbors: spot 7 if pathadd[irow][icol-1] != -9999.: # Then get egde number edgenumber = ncols*(irow)+(icol-1) # Then add edge to pathG pathG.add_edge(nodenumber,edgenumber) # Calculate properties from path lengths: min, max, average pathlen = [] for i in xrange(len(paths)): pathlen.append(paths[i][2]) # Create file to write info to try: fout = open(data_dir+file_name, 'w') except(IOerror,OSerror) as e: print("UNICOROutputs %s, error%s"(filename,e)) sys.exit(-1) # Write header information fout.write('Minimum Path Length,') fout.write(str(min(pathlen))+'\n') fout.write('Maximum Path Length,') fout.write(str(max(pathlen))+'\n') fout.write('Average Path Length,') fout.write(str(sum(pathlen)/len(paths))+'\n') fout.write('Density of Graph,') fout.write(str(nx.density(pathG))+'\n') fout.write('Number of nodes,') fout.write(str(nx.number_of_nodes(pathG))+'\n') fout.write('Number of edges,') fout.write(str(nx.number_of_edges(pathG))+'\n') fout.write('Is the graph a bipartite,') fout.write(str(nx.is_bipartite(pathG))+'\n') fout.write('Size of the largest clique,') fout.write(str(nx.graph_clique_number(pathG))+'\n') fout.write('Number of maximal cliques,') fout.write(str(nx.graph_number_of_cliques(pathG))+'\n') fout.write('Transitivity,') fout.write(str(nx.transitivity(pathG))+'\n') fout.write('Average clustering coefficient,') fout.write(str(nx.average_clustering(pathG))+'\n') fout.write('Test graph connectivity,') fout.write(str(nx.is_connected(pathG))+'\n') fout.write('Number of connected components,') fout.write(str(nx.number_connected_components(pathG))+'\n') fout.write('Consists of a single attracting component,') fout.write(str(nx.is_attracting_component(pathG))+'\n') if nx.is_attracting_component(pathG) == True: fout.write('Number of attracting components,') fout.write(str(nx.number_attracting_components(pathG))+'\n') if nx.is_connected(pathG): fout.write('Center,') fout.write(str(nx.center(pathG))+'\n') fout.write('Diameter,') fout.write(str(nx.diameter(pathG))+'\n') #fout.write('Eccentricity,') #fout.write(str(nx.eccentricity(pathG))+'\n') fout.write('Periphery,') fout.write(str(nx.periphery(pathG))+'\n') fout.write('Radius,') fout.write(str(nx.radius(pathG))+'\n') fout.write('Degree assortativity,') fout.write(str(nx.degree_assortativity(pathG))+'\n') fout.write('Degree assortativity Pearsons r,') fout.write(str(nx.degree_pearsonr(pathG))+'\n') # Close file fout.close del(pathG) # End::output_graphmetrics()
print(ave(degree)) def p(degree): x = list(range(len(degree))) y = [i for i in degree] plt.bar(x, y, align='center') #plot、loglog plt.ylim(0, 300) plt.title('Distribution of Nodes') plt.xlabel('Degree') plt.ylabel('Number of Nodes') for a, b in zip(x, y): plt.text(a, b + 2, '%.0f' % b, ha='center') #plt.savefig("degree.png") plt.show() p(degree) # 群聚系数 print(nx.average_clustering(G)) # 平均群聚系数的计算 print(nx.clustering(G)) # 计算各个节点的群聚系数 # 直径和平均距离 print(nx.diameter(G)) # 返回图G的直径(最长的最短路径的长度) print(nx.average_shortest_path_length(G)) #返回图G所有节点间平均最短路径长度 # 匹配性 print(nx.degree_assortativity(G)) # 图的度匹配性 # 中心性 # .........