Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@afrendeiro
Last active September 30, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afrendeiro/b121ff53eeceae1482d5 to your computer and use it in GitHub Desktop.
Save afrendeiro/b121ff53eeceae1482d5 to your computer and use it in GitHub Desktop.
import networkx as nx
import matplotlib.pyplot as plt
import seaborn as sns
# Getting a specific node
G['PAX5']
# Geting a specific edge
G['PAX5']['NFKB1']
# Getting all edge weights
[G[u][v]['weight'] for u, v in G.edges()]
# get nodes regulating u
G.predecessors('PAX5')
# get nodes regulated by u
G.successors('PAX5')
# Shortest path between two nodes
nx.shortest_path(G, 'PAX5', 'NFKB1', weight='weight')
# GRAPH OPERATIONS
# Subseting
# only edges with certain weight
G2 = nx.DiGraph()
for u, v in G.edges_iter():
weight = G[u][v]['weight']
if weight > 5:
G2.add_edge(u, v, weight=weight)
# Drawing
# simple
nx.draw(G)
nx.draw_circular(G)
nx.draw_spring(G)
# with labels
nx.draw_networkx(G, alpha=.5)
# with specific positions
nx.draw_networkx(G, pos=nx.spring_layout(G), alpha=.5)
# with edge weights as colormap
nx.draw_networkx(
G2, pos=nx.spring_layout(G2), alpha=.5,
arrows=False,
edge_color=[G2[u][v]['weight'] for u, v in G2.edges()],
edge_cmap=plt.get_cmap('gray_r') # white to gray
)
# Graph description
## centrality
nx.degree_centrality(G)
nx.in_degree_centrality(G)
nx.out_degree_centrality(G)
### closest-path based
nx.closeness_centrality(G)
nx.betweenness_centrality(G)
nx.betweenness_centrality(G).items()[np.argmax(nx.betweenness_centrality(G).values())] # get most central node
sorted(nx.katz_centrality_numpy(G).items(), key=lambda x: x[1]) # get results sorted by most central
nx.edge_betweenness_centrality(G)
### current flow-based
#### not possible for DiGraphs!
### eigenvector-based
nx.eigenvector_centrality(G)
nx.eigenvector_centrality(G).items()[np.argmax(nx.eigenvector_centrality(G).values())] # get most central node
sorted(nx.eigenvector_centrality(G).items(), key=lambda x: x[1]) # get results sorted by most central
nx.katz_centrality_numpy(G)
nx.katz_centrality_numpy(G).items()[np.argmax(nx.katz_centrality_numpy(G).values())] # get most central node
sorted(nx.katz_centrality_numpy(G).items(), key=lambda x: x[1]) # get results sorted by most central
### communicability-based
#### not possible for DiGraphs!
### load-based
nx.load_centrality(G)
nx.load_centrality(G).items()[np.argmax(nx.load_centrality(G).values())] # get most central node
sorted(nx.load_centrality(G).items(), key=lambda x: x[1]) # get results sorted by most central
nx.edge_load(G)
sorted(nx.edge_load(G).items(), key=lambda x: x[1]) # get results sorted by most central
nx.dispersion(G) # find un/coordinated nodes
sorted([(i, x, y) for i, j in nx.dispersion(G).items() for x,y in j.items()], key=lambda x: x[2]) # get results sorted by more coordinated nodes (less is more coordination)
### average shortest path length
nx.average_shortest_path_length(G)
# Connectivity
nx.degree_assortativity_coefficient(G)
nx.degree_pearson_correlation_coefficient(G)
nx.attribute_assortativity_coefficient(G, 'count')
nx.numeric_assortativity_coefficient(G, 'count')
nx.average_neighbor_degree(G)
nx.average_degree_connectivity(G)
# plot degree vs connectivity
res = nx.average_degree_connectivity(G)
plt.plot(res.keys(), res.values(), 'o')
sns.regplot(np.array(res.keys(), dtype=np.float64), np.array(res.values(), dtype=np.float64)) # linear regression
res = nx.k_nearest_neighbors(G)
sns.regplot(np.array(res.keys(), dtype=np.float64), np.array(res.values(), dtype=np.float64)) # linear regression
# Drawing
# simple
nx.draw(G)
nx.draw_circular(G)
nx.draw_spring(G)
# with labels
nx.draw_networkx(G, alpha=.5)
# with specific positions
nx.draw_networkx(G, pos=nx.spring_layout(G), alpha=.5)
# with edge weights as colormap
nx.draw_networkx(
G, pos=nx.spring_layout(G), alpha=.5,
arrows=False,
edge_color=[G[u][v]['weight'] for u, v in G.edges()],
edge_cmap=plt.get_cmap('gray_r') # white to gray
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment