Utils

This module contains basic functions for computing graph metrics and properties.
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

This wrapper function spits out some basic graph properties of a NetworkX graph object. We can use it to conveniently explore the different graphs on which to compute hierarchy scores.


source

graph_properties

 graph_properties (G)

Return the properties of a NetworkX graph.

Type Details
G NetworkX graph
Returns str Concatenated string with number of edges and of nodes, the average degree and the graph’s density

Let’s test it on a simple directed graph.

First we wil define the graph and visualise it using Graphviz

G1 = nx.DiGraph([(1, 2), (2, 3), (1, 5), (2, 4), (4, 6), (5, 6), (3, 1)])
G 1 1 2 2 1->2 5 5 1->5 3 3 2->3 4 4 2->4 3->1 6 6 5->6 4->6

And now we can print the output of graph_properties

print(graph_properties(G1))

assert graph_properties(G1)=="Edges:7, Nodes:6, Avg Degree:2.3333333333333335, Density:0.23333333333333334"
Edges:7, Nodes:6, Avg Degree:2.3333333333333335, Density:0.23333333333333334

We can also check out some other hand-defined directed graphs, a digraph built from the OmniPath database or a NetworkX built-in graph.

G2 = nx.DiGraph([(1, 2), (2, 3), (1, 5), (2, 4), (4, 6), (5, 6)])
G3 = nx.DiGraph([(1, 2), (4, 6)])
# Directed Graph from OmniPath
#using the following link: https://omnipathdb.org/interactions?genesymbols=yes&fields=type&license=academic&directed=0
#Gets us all interactions, no matter type nor undirected/vs directed
omnidata = pd.read_csv("https://omnipathdb.org/interactions?genesymbols=yes&fields=type&license=academic&directed=1", sep="\t")

G4 = nx.from_pandas_edgelist(omnidata, source="source_genesymbol", target="target_genesymbol",edge_attr="type", create_using=nx.DiGraph)
# Graph from Krackhardt's work on centrality metrics (Krackhardt, David 1990)
G5 = nx.krackhardt_kite_graph()
print(graph_properties(G1))
print(graph_properties(G2))
print(graph_properties(G3))
print(graph_properties(G4))
print(graph_properties(G5))
Edges:7, Nodes:6, Avg Degree:2.3333333333333335, Density:0.23333333333333334
Edges:6, Nodes:6, Avg Degree:2.0, Density:0.2
Edges:2, Nodes:4, Avg Degree:1.0, Density:0.16666666666666666
Edges:91888, Nodes:9248, Avg Degree:19.8719723183391, Density:0.0010745091553119445
Edges:18, Nodes:10, Avg Degree:3.6, Density:0.4