-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial,py
More file actions
196 lines (163 loc) · 7.59 KB
/
Copy pathtutorial,py
File metadata and controls
196 lines (163 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import csv
import matplotlib.pyplot as plt
import networkx as nx
import urllib.request
def save_data(url="https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat",
output="airports_db.dat"):
response = urllib.request.urlopen(url)
data = response.read()
with open(output, "wb") as file:
file.write(data)
# 1. Data Import
# save_data()
# save_data(url="https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat",
# output="routes_db.dat")
f = open("airports_db.dat", encoding = "utf8")
airport_db = []
errors = 0
for airport in csv.reader(f, delimiter=','):
current_record = []
try:
#each slots containing information about an airport
current_record.append(int(airport[0])) #airport ID
current_record.append(airport[1])
current_record.append(airport[2])
current_record.append(airport[3])
current_record.append(airport[4])
current_record.append(airport[5])
current_record.append(float(airport[6]))
current_record.append(float(airport[7]))
current_record.append(float(airport[8]))
current_record.append(float(airport[9]))
current_record.append(airport[10])
current_record.append(airport[11])
current_record.append(airport[12])
current_record.append(airport[13])
except :
errors += 1
else:
airport_db.append(current_record)
print("Total Airport Imported : ", len(airport_db),
"# of Errors : ", errors)
f = open("routes_db.dat", encoding="utf8")
route_db = []
errors = 0
for route in csv.reader(f, delimiter=','):
current_record = []
try:
current_record.append(route[0])
current_record.append(int(route[1]))
current_record.append(int(route[3]))
current_record.append(int(route[5]))
current_record.append(int(route[7]))
current_record.append(route[8])
except:
errors += 1
else:
route_db.append(current_record)
print("Total Routes Imported : ", len(route_db), "# of Errors : ", errors)
# 2. Data Manipulation
network = nx.Graph()
# Nodes: airports
for airport in airport_db:
network.add_node(airport[0], id=airport[0], name=airport[1], city=airport[2],
country=airport[3], iata=airport[4],
icao=airport[5],
lat=airport[6],
long=airport[7],
alt=airport[8], offset=airport[9],
daylight=airport[10], timezone=airport[11],
type=airport[12], source=airport[13])
# Edges: routes
for route in route_db:
if route[2] in network.nodes() and route[3] in network.nodes:
network.add_edge(route[2], route[3], airline=route[0],
airline_id=route[1], stops=route[4],
equipment=route[5])
# network.nodes[507]
# network.edges[507,100]
# network.nodes[100]
# for i in list(network.neighbors(2650)):
# print(network.nodes[i]['id'], " - ", network.nodes[i]["name"], " - ", network.nodes[i]["country"])
# print(nx.info(network, 2650))
# Density of network
# nx.density(network)
# fully connected graph ?
# nx.is_connected(network)
# Centrality: measure of the importance of each node in the network
degree_centrality = nx.degree_centrality(network)
# add the centrality values to our nodes
for airport, centrality_value in degree_centrality.items(): network.nodes[airport]['degree_centrality'] = centrality_value
network.nodes[507]['degree_centrality']
# path between two nodes
# nx.has_path(network, source= 507, target = 100)
# nx.has_path(network, source= 507, target = 20)
# network.degree()
# remove nodes without edges
for airport, degree in list(network.degree()):
if degree == 0:
network.remove_node(airport)
nx.info(network)
# nx.is_connected(network)
# Evaluate the subgraphs within our graph
# nx.number_connected_components(network)
print([len(e) for e in nx.connected_components(network)])
# By going that, we can see that there is a subnetwork containing the bulk of our airports (3077) and small
# subnetworks that are not connected to the main one. This is maybe due to the fact that they link so many
# regional airports within a country or something similar to that. Since they are not a lot, we can easily
# disregard those values. So let’s go ahead and save the subgraph 3077 in a new variable that will be used
# to display the network
main_subgraph = max(nx.connected_components(network), key=len)
network_main = network.subgraph(main_subgraph)
# nx.info(network_main)
# 3. Network Visualization
for airport in network_main.nodes():
network_main.nodes[airport]['coordinates'] = (network_main.nodes[airport]['long'],
network_main.nodes[airport]['lat'])
# nx.draw(network_main, nx.get_node_attributes(network_main, 'coordinates'), node_shape='.', node_size=3)
# plt.show()
# plt.figure(figsize = (20, 15))
# nx.draw_networkx_nodes(network_main, nx.get_node_attributes(network_main, 'coordinates'), node_shape='.', node_size=6)
# nx.draw_networkx_edges(network_main, nx.get_node_attributes(network_main, 'coordinates'), width=0.3, alpha=0.5)
# plt.show()
maximum_centrality = max(degree_centrality.values())
for airport in network_main.nodes():
if network_main.nodes[airport]['timezone'].find('Europe') == 0:
network_main.nodes[airport]['color'] = 'blue'
elif network_main.nodes[airport]['timezone'].find('Asia') == 0:
network_main.nodes[airport]['color'] = 'red'
elif network_main.nodes[airport]['timezone'].find('Africa') == 0:
network_main.nodes[airport]['color'] = 'yellow'
elif network_main.nodes[airport]['timezone'].find('America') == 0:
network_main.nodes[airport]['color'] = 'green'
elif network_main.nodes[airport]['timezone'].find('Australia') == 0:
network_main.nodes[airport]['color'] = 'orange'
elif network_main.nodes[airport]['timezone'].find('Pacific') == 0:
network_main.nodes[airport]['color'] = 'purple'
else:
network_main.nodes[airport]['color'] = 'grey'
if network_main.nodes[airport]['degree_centrality'] >= maximum_centrality * .9:
network_main.nodes[airport]['importance'] = 400
elif network_main.nodes[airport]['degree_centrality'] >= maximum_centrality * .5:
network_main.nodes[airport]['importance'] = 150
else:
network_main.nodes[airport]['importance'] = 10
for route in network_main.edges():
if (network_main[route[0]][route[1]]['airline'] == "TK"):
network_main[route[0]][route[1]]['color'] = 'red'
network_main[route[0]][route[1]]['size'] = 1.5
elif (network_main[route[0]][route[1]]['airline'] == "US"):
network_main[route[0]][route[1]]['color'] = 'blue'
network_main[route[0]][route[1]]['size'] = 1.5
else:
network_main[route[0]][route[1]]['color'] = 'grey'
network_main[route[0]][route[1]]['size'] = 0.2
plt.figure(figsize=(20, 15))
nx.draw_networkx_nodes(network_main, nx.get_node_attributes(network_main, 'coordinates'), node_shape = '.' ,
node_size=[importance for importance in nx.get_node_attributes(network_main, 'importance').values()],
node_color=[color for color in nx.get_node_attributes(network_main, 'color').values()])
nx.draw_networkx_edges(network_main, nx.get_node_attributes(network_main, 'coordinates'),
width=[size for size in nx.get_edge_attributes(network_main, 'size').values()],
edge_color=[color for color in nx.get_edge_attributes(network_main, 'color').values()],
alpha=0.1)
plt.show()