-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerLawAirlineRoutes.py
More file actions
147 lines (101 loc) · 3.98 KB
/
Copy pathpowerLawAirlineRoutes.py
File metadata and controls
147 lines (101 loc) · 3.98 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
import sys
import re
import matplotlib.pyplot as plt
import powerlaw
import numpy as np
# python powerLawAirlineRoutes.py ./Data/airports.dat
# ./Data/182980864_T_T100D_SEGMENT_ALL_CARRIER2.csv
def main():
args = sys.argv
airportsDataFile = args[1] # airports.dat
routeDataFile = args[2] # 182980864_T_T100D_SEGMENT_ALL_CARRIER2.csv
airportData = open(airportsDataFile, 'r')
routeData = open(routeDataFile, 'r')
outputFile = open('./Data/powerLawData.csv','w')
domesticAirports = dict()
# Iterate through each line in original airport file to find all domestic
# airports (nodes)
for line in airportData:
segmentedLine = re.split('"?,?"?',line) # split on the separators
# Save IATA code for all domestic airports
if segmentedLine[3] == "United States":
if segmentedLine[3] not in domesticAirports:
# Store number of routes, number of passengers flowing out,
# number of connected airports
domesticAirports[segmentedLine[4]] = [0,0,dict()]
routeData.readline() # throw away header
for line in routeData:
segmentedLine = re.split('"?,?"?',line) # split on the separators
# Look for IATA code in approvedAirports list
# Check to make sure the from and to airports are both domestic and that
# at least one person traveled between the cities
if segmentedLine[4] in domesticAirports and segmentedLine[6] in \
domesticAirports and float(segmentedLine[0]) > 0:
if segmentedLine[6] not in domesticAirports[segmentedLine[4]][2]:
domesticAirports[segmentedLine[4]][2][segmentedLine[6]] = 1
domesticAirports[segmentedLine[4]][0] += 1
domesticAirports[segmentedLine[4]][1] += float(segmentedLine[0])
# Find max num
maxNum = 0
for key, values in domesticAirports.items():
if values[0] > maxNum:
maxNum = values[0]
domesticAirportsList = [0] * (maxNum + 1)
# domesticAirportsList2 = dict()
for key, values in domesticAirports.items():
domesticAirportsList[values[0]] += 1
# for i in range(0,1000000000,1000000):
# if (values[1] <= i):
# if i not in domesticAirportsList2:
# domesticAirportsList2[i] = 0
# domesticAirportsList2[i] += 1
# break
#print (domesticAirports)
# print (domesticAirportsList)
#print (domesticAirportsList2)
# keys = domesticAirportsList2.keys()
# values = domesticAirportsList2.values()
#plt.loglog(list(keys), list(values), '*', basex=10)
#plt.show()
#print (keys, values)
xData = list()
yData = list()
for i in range(1,len(domesticAirportsList)):
if domesticAirportsList[i] > 0:
xData.append(i)
yData.append(domesticAirportsList[i])
lines = []
for i in range(len(xData)):
lines.append(str(xData[i]) + ", " + str(yData[i]) + "\n")
outputFile.writelines(lines)
# xTravelData = list()
# yTravelData = list()
#for i in range(1,len(domesticAirports))
# for i in range(1,maxNum+1):
# xData.append(i)
print ("xdata",xData)
print ("ydata",yData)
plt.loglog(xData, yData, '*', basex=10)
plt.xlabel("Route Degree")
plt.ylabel("Number of Routes")
plt.grid(True)
#plt.plot(xData,domesticAirportsList[1:])
plt.show()
# plt.plot(xData, np.poly1d(np.polyfit(xData, yData, 1))(xData))
# plt.show()
#fit = powerlaw.Fit(domesticAirportsList)
#print (fit.power_law.alpha)
#print (fit.power_law.xmin)
#fit.power_law.plot_pdf( color= 'b',linestyle='--',label='fit ccdf')
#fit.plot_pdf( color= 'b')
# Write to file
# count = 1
# for item in foundAirports:
# item.insert(0,str(count))
# count += 1
# outputFile.write(",".join(item))
airportData.close()
routeData.close()
outputFile.close()
if __name__ == '__main__':
main()