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 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
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()