In [44]:
%matplotlib inline
In [45]:
import networkx as nx
import pandas as pd
import projx as px
import matplotlib.pyplot as plt
In [46]:
plt.rcParams['figure.figsize'] = (12, 7)
In [47]:
def prob_dist(itrbl):
count = {}
for i in itrbl:
count.setdefault(i, 0)
count[i] += 1
sr = pd.Series(count)
prob = sr.apply(lambda x: float(x) / len(itrbl))
return prob
def basic_graph_stats(g):
stats = {
"num_nodes": len(g),
"num_edges": len(g.edges()),
"density": nx.density(g),
"diameter": nx.diameter(g),
"avg_short_path": nx.average_shortest_path_length(g),
"avg_clust": nx.average_clustering(g),
"transitivity": nx.transitivity(g)
}
return pd.Series(stats)
In [48]:
graph = nx.read_gexf("projections/ninety_percent_cut.gexf")
In [49]:
subgraphs = list(nx.connected_component_subgraphs(graph))
print([len(sub) for sub in subgraphs])
In [50]:
g = subgraphs[0]
g1 = subgraphs[1]
g2 = subgraphs[2]
g3 = subgraphs[3]
g4 = subgraphs[4]
In [51]:
nx.write_gexf(g, "projections/subgraph_ninety_cut.gexf")
In [52]:
basic_graph_stats(g)
Out[52]:
In [53]:
print(len(g), len(g.edges()), nx.density(g))
In [54]:
plt.rcParams['figure.figsize'] = (17, 12)
In [55]:
px.draw_simple_graph(g, edge_label_attr="")
In [56]:
px.draw_simple_graph(g1, edge_label_attr="")
In [57]:
px.draw_simple_graph(g2, edge_label_attr="")
In [58]:
px.draw_simple_graph(g3, edge_label_attr="")
In [59]:
px.draw_simple_graph(g4, edge_label_attr="")
In [60]:
bc = nx.betweenness_centrality(g, weight="weight")
ec = nx.eigenvector_centrality(g, weight="weight", max_iter=500)
cc = nx.closeness_centrality(g)
deg = nx.degree(g)
pr = nx.pagerank(g, max_iter=500, weight="weight")
In [61]:
cent_10_df = pd.DataFrame({
"bc": [(k, g.node[k]["label"], bc[k]) for k in sorted(bc, key=bc.get, reverse=True)[0:10]],
"ec": [(k, g.node[k]["label"], ec[k]) for k in sorted(ec, key=ec.get, reverse=True)[0:10]],
"cc": [(k, g.node[k]["label"], cc[k]) for k in sorted(cc, key=cc.get, reverse=True)[0:10]],
"dc": [(k, g.node[k]["label"], deg[k]) for k in sorted(deg, key=deg.get, reverse=True)[0:10]],
"pr": [(k, g.node[k]["label"], pr[k]) for k in sorted(pr, key=pr.get, reverse=True)[0:10]]
})
In [62]:
print(cent_10_df)
In [63]:
pd.Series(deg.values()).hist()
Out[63]:
In [64]:
deg_prob = prob_dist(deg.values())
plt.scatter(deg_prob.index, deg_prob)
Out[64]:
In [65]:
pd.Series(bc.values()).hist()
Out[65]:
In [66]:
pd.Series(cc.values()).hist()
Out[66]:
In [67]:
pd.Series(ec.values()).hist()
Out[67]:
In [68]:
pd.Series(pr.values()).hist()
Out[68]:
In [69]:
nx.degree_assortativity_coefficient(g)
Out[69]:
In [70]:
r = nx.degree_assortativity_coefficient(g)
print("%3.1f"%r)
In [71]:
nodes_by_deg = sorted(deg, key=deg.get, reverse=True)
mtrx = nx.to_numpy_matrix(g, nodelist=nodes_by_deg)
In [72]:
weight_sr = pd.Series([attrs["weight"] for s, t, attrs in g.edges(data=True)])
weight_sr.describe()
Out[72]:
In [73]:
quant = weight_sr.quantile(.75)
In [74]:
plt.rcParams['figure.figsize'] = (17, 12)
In [75]:
heatmap = plt.imshow(mtrx)
heatmap.set_clim(0.0, quant)
plt.colorbar()
Out[75]:
In [76]:
stripmtrx = mtrx[:, :35]
In [77]:
heatmap = plt.imshow(stripmtrx)
heatmap.set_clim(0.0, quant)
plt.colorbar()
Out[77]: