%matplotlib inline
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
territories = {
"Alaska": 0,
"Alberta": 0,
"Central America": 0,
"Eastern United States": 0,
"Greenland": 0,
"Northwest Territory": 0,
"Ontario": 0,
"Eastern Canada": 0,
"Western United States": 0,
"Argentina": 1,
"Brazil": 1,
"Peru": 1,
"Venezuela": 1,
"Great Britain": 5,
"Iceland": 5,
"Northern Europe": 5,
"Scandinavia": 5,
"Southern Europe": 5,
"Ukraine": 5,
"Western Europe": 5,
"Congo": 2,
"East Africa": 2,
"Egypt": 2,
"Madagascar": 2,
"North Africa": 2,
"South Africa": 2,
"Afghanistan": 3,
"China": 3,
"India": 3,
"Irkutsk": 3,
"Japan": 3,
"Kamchatka": 3,
"Middle East": 3,
"Mongolia": 3,
"Siam": 3,
"Siberia": 3,
"Ural": 3,
"Yakutsk": 3,
"Eastern Australia": 4,
"Indonesia": 4,
"New Guinea": 4,
"Western Australia": 4,
}
regions = {
"North America": 0,
"South America": 1,
"Africa": 2,
"Asia": 3,
"Australia": 4,
"Europe": 5,
}
g = nx.Graph()
for t, c_label in territories.items():
g.add_node(t, region=c_label)
edges = [
("Alaska", "Northwest Territory"),
("Alaska", "Alberta"),
("Alaska", "Kamchatka"),
("Northwest Territory", "Alberta"),
("Northwest Territory", "Greenland"),
("Northwest Territory", "Ontario"),
("Greenland", "Iceland"),
("Greenland", "Ontario"),
("Greenland", "Eastern Canada"),
("Eastern Canada", "Eastern United States"),
("Eastern Canada", "Ontario"),
("Ontario", "Alberta"),
("Ontario", "Western United States"),
("Alberta", "Western United States"),
("Western United States", "Eastern United States"),
("Western United States", "Central America"),
("Eastern United States", "Central America"),
("Central America", "Venezuela"),
("Venezuela", "Brazil"),
("Venezuela", "Peru"),
("Peru", "Argentina"),
("Peru", "Brazil"),
("Argentina", "Brazil"),
("Brazil", "North Africa"),
("North Africa", "Egypt"),
("North Africa", "Western Europe"),
("North Africa", "Southern Europe"),
("North Africa", "East Africa"),
("North Africa", "Congo"),
("Congo", "South Africa"),
("Congo", "East Africa"),
("South Africa", "Madagascar"),
("South Africa", "East Africa"),
("Madagascar", "East Africa"),
("East Africa", "Egypt"),
("East Africa", "Middle East"),
("Egypt", "Southern Europe"),
("Egypt", "Middle East"),
("Middle East", "India"),
("Middle East", "Afghanistan"),
("Middle East", "Ukraine"),
("Middle East", "Southern Europe"),
("India", "Afghanistan"),
("India", "China"),
("India", "Siam"),
("Siam", "China"),
("Siam", "Indonesia"),
("China", "Mongolia"),
("China", "Siberia"),
("China", "Ural"),
("China", "Afghanistan"),
("Mongolia", "Japan"),
("Mongolia", "Kamchatka"),
("Mongolia", "Irkutsk"),
("Mongolia", "Siberia"),
("Irkutsk", "Kamchatka"),
("Irkutsk", "Yakutsk"),
("Irkutsk", "Siberia"),
("Japan", "Kamchatka"),
("Yakutsk", "Siberia"),
("Yakutsk", "Kamchatka"),
("Siberia", "Ural"),
("Ural", "Afghanistan"),
("Ural", "Ukraine"),
("Ukraine", "Afghanistan"),
("Ukraine", "Scandinavia"),
("Ukraine", "Northern Europe"),
("Ukraine", "Southern Europe"),
("Scandinavia", "Northern Europe"),
("Scandinavia", "Iceland"),
("Scandinavia", "Great Britain"),
("Northern Europe", "Southern Europe"),
("Northern Europe", "Western Europe"),
("Northern Europe", "Great Britain"),
("Western Europe", "Great Britain"),
("Great Britain", "Iceland"),
("Indonesia", "New Guinea"),
("Indonesia", "Western Australia"),
("New Guinea", "Western Australia"),
("New Guinea", "Eastern Australia"),
("Eastern Australia", "Western Australia"),
]
for c in set([c for pair in edges for c in pair]):
if c not in territories:
print("Error on:", c)
for e1, e2 in edges:
g.add_edge(e1, e2)
print(len(g.nodes()), len(territories))
nx.write_edgelist(g, "risk.adj", delimiter="\t")
len(g.edges())
sorted([v for v in g.degree()], key=lambda x: x[1], reverse=True)
print("Density:", nx.density(g))
print("Mean Cluster Coef:", np.mean([x[1] for x in nx.clustering(g).items()]))
nx.draw(g)
A = nx.nx_agraph.to_agraph(g)
A.layout(prog='fdp')
A.draw('risk_graph.png')
nx.write_graphml(g, "risk.graphml")
ax = plt.gca()
ax.set_xlim((-1.5,1.5))
target = "Argentina"
degree = 1.5
subg = nx.ego_graph(g, target, degree)
subg.remove_node(target)
if ( degree == 1 ):
for e in dict(subg.edges):
if ( target not in e ):
subg.remove_edge(e[0], e[1])
nx.draw_networkx(subg, with_labels=True, ax=ax)
ax.axis('off')
plt.show()
print("Clustering Coefficient:", nx.clustering(g, target))
print("1.5-Degree Ego Density:", nx.density(subg))
print("Global Coefficient of Subg:", nx.transitivity(subg))
for t in territories:
subg = nx.ego_graph(g, t)
subg.remove_node(t)
cluster_coef = nx.clustering(g, t)
ego_dens = nx.density(subg)
print(t, cluster_coef)
if ( cluster_coef != ego_dens ):
print("Fail", t, cluster_coef, ego_dens)
local_cluster_coeffs = []
for n in g.nodes():
local_cluster_coeffs.append(nx.clustering(g, n))
print("Mean Local Clustering Coeff:", np.mean(local_cluster_coeffs))
print("Mean Local Clustering Coeff':", nx.average_clustering(g))
print("Global Clustering Coeff':", nx.transitivity(g))
num_triangles = np.sum([x[1] for x in nx.triangles(g).items()])
print("Triangles:", num_triangles/3)
number_of_triads = num_triangles/nx.transitivity(g)
print("Triads:", number_of_triads)
nx.node_connectivity(subg)
for reg, reg_idx in regions.items():
reg_nodes = [x[0] for x in nx.get_node_attributes(g, "region").items() if x[1] == reg_idx]
subg = g.subgraph(reg_nodes)
print(reg, nx.average_clustering(subg))
country_bc = nx.degree_centrality(g)
nx.set_node_attributes(g, country_bc, "d_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
dc_list = [x[1] for x in country_bc.items()]
m = np.max(dc_list)
np.sum([m-x for x in dc_list])
country_bc = nx.closeness_centrality(g)
nx.set_node_attributes(g, country_bc, "c_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
country_bc = nx.betweenness_centrality(g)
nx.set_node_attributes(g, country_bc, "b_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
nx.write_graphml(g, "risk.graphml")