def test_is_attracting_component(self): assert not nx.is_attracting_component(self.G1) assert not nx.is_attracting_component(self.G2) assert not nx.is_attracting_component(self.G3) g2 = self.G3.subgraph([1, 2]) assert nx.is_attracting_component(g2) assert not nx.is_attracting_component(self.G4)
def test_is_attracting_component(self): assert_false(nx.is_attracting_component(self.G1)) assert_false(nx.is_attracting_component(self.G2)) assert_false(nx.is_attracting_component(self.G3)) g2 = self.G3.subgraph([1, 2]) assert_true(nx.is_attracting_component(g2)) assert_false(nx.is_attracting_component(self.G4))
# clique #list(nx.find_cliques(G)) #list(nx.make_max_clique_graph(G)) #list(nx.make_clique_bipartite(G)) #nx.graph_clique_number(G) #nx.graph_number_of_cliques(G) # components nx.is_strongly_connected(G) nx.number_strongly_connected_components(G) scc = nx.strongly_connected_components(G) nx.strongly_connected_components_recursive(G) nx.condensation(G, scc) # attracting components nx.is_attracting_component(G) nx.number_attracting_components(G) nx.attracting_components(G) # directed acyclic graphs nx.is_directed_acyclic_graph(G) nx.is_aperiodic(G) # distance measure (all for connected graph) nx.center(Gcc) nx.diameter(Gcc) nx.eccentricity(Gcc) nx.periphery(Gcc) nx.radius(Gcc) # flows (seg fault currently)
pos = nx.spring_layout(G, k=0.15, iterations=10) # pos=nx.graphviz_layout(G) # pos=layout(G) G.remove_nodes_from(nx.isolates(G)) print str(" ") print 'ATTRACTING CONNECTEDNESS OF DIRECTED GRAPHS' print str(" ") print str(" ") print G.name print str(" ") print 'Does the graph G consist of a single attracting component?', nx.is_attracting_component( G) print 'The number of attracting components of G is:', nx.number_attracting_components( G) print str(" ") print 'List of attracting components:' print sorted(nx.attracting_components(G), key=len, reverse=True) print str(" ") lc = sorted(nx.strongly_connected_components(G), key=len, reverse=True) print 'List of strongly connected components:' print lc print str(" ") colors_list = ['c', 'b', 'g', 'y', 'k', 'm'] colors_to_select = list(colors_list)
pos=nx.spring_layout(G,k=0.15,iterations=10) # pos=nx.graphviz_layout(G) # pos=layout(G) G.remove_nodes_from(nx.isolates(G)) print str(" ") print 'ATTRACTING CONNECTEDNESS OF DIRECTED GRAPHS' print str(" ") print str(" ") print G.name print str(" ") print 'Does the graph G consist of a single attracting component?', nx.is_attracting_component(G) print 'The number of attracting components of G is:', nx.number_attracting_components(G) print str(" ") print 'List of attracting components:' print sorted(nx.attracting_components(G), key = len, reverse=True) print str(" ") lc=sorted(nx.strongly_connected_components(G), key = len, reverse=True) print 'List of strongly connected components:' print lc print str(" ") colors_list=['c','b','g','y','k','m'] colors_to_select=list(colors_list) graphs =sorted(nx.attracting_component_subgraphs(G), key = len, reverse=True)
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()