In [ ]:
%matplotlib inline

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
In [ ]:
 
In [ ]:
stations = [
    "Shady Grove",
    "Glenmont",
    "Greenbelt",
    "Branch Avenue",
    "New Carrolton",
    "Vienna",
    "Wiehle-Reston East",
    "Largo Town Center",
    "Franconia-Springfield",
    "Huntington",
    "Fort Totten",
    "Gallery Place",
    "Metro Center",
    "L'Enfant Plaza",
    "Stadium Armory",
    "Pentagon",
    "King St-Old Town",
    "Rosslyn",
    "East Falls Church"
]
print("Station Count:", len(stations))

lines = {
    0: "Red Line",
    1: "Green Line",
    2: "Silver Line",
    3: "Blue Line",
    4: "Orange line",
    5: "Yellow Line",
}
print("Line Count:", len(lines))
In [ ]:
 
In [ ]:
stations_alpha = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S"
]

sa_map = dict(zip(stations, stations_alpha))
In [ ]:
 
In [ ]:
g = nx.Graph()

for s in stations:
    g.add_node(s)
In [ ]:
edges = [
    ("Shady Grove", "Metro Center", 0),
    ("Gallery Place", "Metro Center", 0),
    ("Gallery Place", "Fort Totten", 0),
    ("Glenmont", "Fort Totten", 0),
    ("Greenbelt", "Fort Totten", 1),
    ("Gallery Place", "Fort Totten", 1),
    ("Gallery Place", "Fort Totten", 5),
    ("Gallery Place", "L'Enfant Plaza", 1),
    ("Branch Avenue", "L'Enfant Plaza", 1),
    ("Gallery Place", "L'Enfant Plaza", 5),
    ("Pentagon", "L'Enfant Plaza", 5),
    ("Pentagon", "L'Enfant Plaza", 3),
    ("Pentagon", "King St-Old Town", 3),
    ("Pentagon", "King St-Old Town", 5),
    ("Huntington", "King St-Old Town", 5),
    ("Franconia-Springfield", "King St-Old Town", 3),
    ("Pentagon", "Rosslyn", 3),
    ("Rosslyn", "East Falls Church", 2),
    ("Rosslyn", "East Falls Church", 4),
    ("Vienna", "East Falls Church", 4),
    ("Wiehle-Reston East", "East Falls Church", 2),
    ("Rosslyn", "Metro Center", 2),
    ("Rosslyn", "Metro Center", 3),
    ("Rosslyn", "Metro Center", 4),
    ("L'Enfant Plaza", "Metro Center", 2),
    ("L'Enfant Plaza", "Metro Center", 3),
    ("L'Enfant Plaza", "Metro Center", 4),
    ("L'Enfant Plaza", "Stadium Armory", 2),
    ("L'Enfant Plaza", "Stadium Armory", 3),
    ("L'Enfant Plaza", "Stadium Armory", 4),
    ("Largo Town Center", "Stadium Armory", 2),
    ("Largo Town Center", "Stadium Armory", 3),
    ("New Carrolton", "Stadium Armory", 4),
]
In [ ]:
for c in set([c for tup in edges for c in tup[:1]]):
    if c not in stations:
        print("Error on:", c)
In [ ]:
for e1, e2, lidx in edges:
    g.add_edge(e1, e2, line=lines[lidx])
In [ ]:
print(len(g.nodes()), len(stations))
In [ ]:
 
In [ ]:
for v, d in sorted([v for v in g.degree()], key=lambda x: x[1], reverse=True):
    print(v, sa_map[v], d)
In [ ]:
 
In [ ]:
nx.draw(g)
In [ ]:
ax = plt.gca()
nx.draw_networkx(g, with_labels=True, ax=ax)

ax.axis('off')

plt.show()
In [ ]:
A = nx.nx_agraph.to_agraph(g)
A.layout(prog='fdp')
A.draw('metro_graph.png')

graph

In [ ]:
 
In [ ]:
nx.write_graphml(nx.Graph(g), "metro.graphml")
In [ ]:
 
In [ ]:
ax = plt.gca()

subg = nx.ego_graph(g, "Fort Totten")
subg.remove_node("Fort Totten")
nx.draw_networkx(subg, with_labels=True, ax=ax)

ax.axis('off')

print("Clustering Coefficient:", nx.clustering(g, "Iceland"))
print("1.5-Degree Ego Density:", nx.density(subg))
In [ ]:
 
In [ ]:
for s in stations:

    subg = nx.ego_graph(g, s)
    subg.remove_node(s)
    
    cluster_coef = nx.clustering(g, s)
    ego_dens = nx.density(subg)

    if ( cluster_coef != ego_dens ):
        print("Fail", t, cluster_coef, ego_dens)
In [ ]:
 
In [ ]:
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))
In [ ]:
 
In [ ]:
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)]
In [ ]:
 
In [ ]:
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)]
In [ ]:
 
In [ ]:
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)]
In [ ]:
 
In [ ]:
dc_vs = [x[1] for x in nx.degree_centrality(g).items()]
plt.hist(dc_vs)

plt.xlabel("Degree Centrality")
plt.ylabel("Frequency")
plt.grid()
plt.show()

dcm = np.max(dc_vs)
central_score = np.sum([dcm-x for x in dc_vs])

print(dcm)
print(central_score)
In [ ]:
g1 = nx.star_graph(int(len(stations))-1)
print(len(g1.nodes))
nx.draw(g1)

np.mean([x[1] for x in nx.clustering(g1).items()])

plt.show()

dc_vs = [x[1] for x in nx.degree_centrality(g1).items()]
plt.hist(dc_vs)

plt.xlabel("Degree Centrality")
plt.ylabel("Frequency")
plt.grid()
plt.show()

dcm = np.max(dc_vs)
star_central_score = np.sum([dcm-x for x in dc_vs])

print(star_central_score)
In [ ]:
central_score / star_central_score
In [ ]:
3.056 / 17
In [ ]: