-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataCharacterisation.py
More file actions
415 lines (347 loc) · 11.3 KB
/
Copy pathdataCharacterisation.py
File metadata and controls
415 lines (347 loc) · 11.3 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# Data Characterisation
# Work out what the data consists of, and understand which elements of the data are useful
import json
from pymongo import MongoClient
# Function to replace a space with a specified character
def replace_space(element, char):
if element != '':
return element
else:
return char
def check_status(mins, status):
if status == "LATE":
return -mins
else:
return mins
def get_operator(operator):
switcher = {
'23': "Arriva Trains Northern",
'71': "Transport for Wales",
'79': "c2c",
'35': "Caledonian Sleeper",
'74': "Chiltern Railway",
'27': "CrossCountry",
'34': "Devon and Cornwall Railways",
'28': "East Midlands Trains",
'06': "Eurostar",
'26': "First Capital Connect (defunct)",
'55': "First Hull Trains",
'81': "Gatwick Express",
'54': "GB Railfreight",
'88': "Govia Thameslink Railway / Southern",
'22': "Grand Central",
'25': "Great Western Railway",
'21': "Greater Anglia",
'24': "Heathrow Connect",
'86': "Heathrow Express",
'85': "Island Lines",
'29': "London Midlands",
'30': "London Overground",
'64': "Merseyrail",
'00': "Network Rail",
'56': "Nexus",
'51': "North Yorkshire Moors Railway",
'60': "ScotRail",
'84': "South Western Railway",
'19': "South Yorkshire Supertram",
'80': "Southeastern",
'33': "TFL Rail",
'20': "TransPennine Express",
'65': "Virgin Trains",
'61': "Virgin Trains East Coast",
'50': "West Coast Railway Co."
}
return switcher.get(operator, "Invalid Station")
def get_stanox(stanox):
switcher = {
'87031': "AHT",
'87021': "AON",
'86074': "ADV",
'87763': "AHS",
'86066': "BSK",
'86896': "BEU",
'86339': "BDH",
'87024': "BTY",
'86216': "BTE",
'87009': "BAW",
'86202': "BOE",
'86921': "BMH",
'86061': "BMY",
'86901': "BCU",
'86223': "BUO",
'86108': "CFR",
'86915': "CHR",
'86301': "CSA",
'86112': "DEN",
'86107': "DBG",
'86087': "ESL",
'86343': "EMS",
'86241': "FRM",
'86042': "FNB",
'87010': "FNN",
'87026': "FNH",
'86045': "FLE",
'86321': "FTN",
'86077': "GRT",
'86222': "HME",
'87062': "HSL",
'86341': "HAV",
'86201': "HDE",
'86332': "HLS",
'86913': "HNA",
'86049': "HOK",
'87064': "LIP",
'87065': "LIS",
'86908': "LYP",
'86907': "LYT",
'86081': "MIC",
'86527': "MBK",
'86219': "NTL",
'86911': "NWM",
'87012': "NCM",
'86070': "OVR",
'87066': "PTR",
'86917': "POK",
'86248': "PTC",
'86313': "PMS",
'86311': "PMH",
'86703': "RDB",
'86101': "ROM",
'87067': "RLN",
'86122': "SAL",
'86084': "SHW",
'86218': "SHO",
'86495': "SOA",
'86520': "SOU",
'86499': "SDN",
'86225': "SNW",
'86909': "SWY",
'86497': "SWG",
'86711': "TTN",
'86342': "WBL",
'86071': "WCH",
'86083': "WIN",
'86047': "WNF",
'86215': "WLS"
}
return switcher.get(stanox, "Invalid Stanox")
client = MongoClient()
dblist = client.list_database_names()
db = client["Third_Year_Project"]
real_time_data = db["Real_Time_Data_HANTS_2_2weeks"]
# Import Data
json_data = real_time_data.find({"header.msg_type": '0003'})
json_change_orig_data = real_time_data.find({"header.msg_type": '0006'})
json_cancel_data = real_time_data.find({"header.msg_type": "0002"})
data = []
change_orig_data = []
cancel_data = []
for x in json_data:
data.append([
x['body']['event_type'],
x['body']['planned_timestamp'],
int(x['body']['timetable_variation']), # Minutes variation from timetabled time
# x['body']['original_loc_timestamp'],
x['body']['current_train_id'], # Indicates train has been reassigned -> does this have an impact on lateness
int(replace_space(x['body']['next_report_run_time'], 0)), # Likely minutes until next stanox report
x['body']['actual_timestamp'],
x['body']['correction_ind'], # False if not a correction of a previous report, True if it is
x['body']['platform'],
x['body']['train_id'],
x['body']['variation_status'], # ON TIME, EARLY, LATE, OFF ROUTE
x['body']['train_service_code'],
x['body']['toc_id'],
x['body']['loc_stanox'],
x['body']['next_report_stanox']
])
for x in json_change_orig_data:
change_orig_data.append(list(x['body'].values()))
for x in json_cancel_data:
cancel_data.append(list(x['body'].values()))
# flatten_dict(data_body)
# print(data)
# print(change_orig_data)
# print(cancel_data)
# ANALYSIS data using graphs/general stats
# E.g. How many late trains, how many early trains etc
# Break down of number of service instances per service code
# Break down number of stops per train line etc
# find lines with greatest amount of data etc
# etc
early = 0
late = 0
ontime = 0
offroute = 0
for x in data:
if x[9] == "EARLY":
early += 1
elif x[9] == "LATE":
late += 1
elif x[9] == "ON TIME":
ontime += 1
else:
offroute += 1
print()
print("************************************* STATS ****************************************")
print()
print("EARLY: ", early, " (", round((early/len(data))*100), "%) | LATE: ", late, " (", round((late/len(data))*100), "%) | ON TIME: ", ontime, " (", round((ontime/len(data))*100), "%) | OFF ROUTE: ", offroute, " (", round((offroute/len(data))*100), "%)")
print()
dep = 0
arr = 0
for x in data:
if x[0] == "DEPARTURE":
dep += 1
elif x[0] == "ARRIVAL":
arr += 1
print("DEPARTURES: ", dep, " | ARRIVALS: ", arr)
print("// Interesting")
print()
# Average number of minutes late/early
# Neg_data is form with mins in negative if appropriate
neg_data = []
for x in data:
neg_data.append([
x[0],
x[1],
check_status(x[2], x[9]), # Minutes variation from timetabled time
x[3],
x[4],
x[5],
x[6],
x[7],
x[8],
x[9],
x[10],
x[11],
x[12],
x[13]
])
total = 0
count = 0
for x in neg_data:
total += x[2]
count += 1
average = total/count
print("Average Mins off Timetable: ", average)
print()
# Average delay based at station
stations = set()
stations_dict = dict()
late = 0
for x in neg_data:
stations.add(x[12])
print("Average Mins off Timetable / Percentage of trains which are late: ")
for y in stations:
temp_count = 0
temp_sum = 0
late = 0
for x in neg_data:
if x[12] == y:
temp_count += 1
temp_sum += x[2]
if x[9] == "LATE":
late += 1
print(get_stanox(y), ": ", round(temp_sum/temp_count, 2), "Mins off \t\t", round((late/temp_count)*100), "% Late")
stations_dict[y] = temp_sum / temp_count
print()
# Latest/Earliest (on average) station
earliest = ""
latest = ""
for key in stations_dict:
if stations_dict[key] == sorted(stations_dict.values())[0]:
latest = key
elif stations_dict[key] == sorted(stations_dict.values())[len(stations_dict)-1]:
earliest = key
print("On Average Station Most Late : ", get_stanox(latest), " Mins Delayed: ", sorted(stations_dict.values())[0], " On Average Station Most Early: ", get_stanox(earliest), " Mins Early:", sorted(stations_dict.values())[len(stations_dict)-1])
print()
# Average delay based on toc
tocs = set()
tocs_dict = dict()
for x in neg_data:
tocs.add(x[11])
print("Average Mins off Timetable: ")
for y in tocs:
temp_count = 0
temp_sum = 0
late = 0
for x in neg_data:
if x[11] == y:
temp_count += 1
temp_sum += x[2]
if x[9] == "LATE":
late += 1
print(get_operator(y), ": \t\t", round(temp_sum/temp_count, 2), "Mins off \t\t", round((late/temp_count)*100), "% Late")
tocs_dict[y] = temp_sum / temp_count
print()
# Latest/Earliest (on average) train operating company
earliest = ""
latest = ""
for key in tocs_dict:
if tocs_dict[key] == sorted(tocs_dict.values())[0]:
latest = key
elif tocs_dict[key] == sorted(tocs_dict.values())[len(tocs)-1]:
earliest = key
print("On Average Train Operator Most Late: ", get_operator(latest), " Mins Delayed: ", sorted(tocs_dict.values())[0], " On Average Train Operator Most Early: ", get_operator(earliest), " Mins Early:", sorted(tocs_dict.values())[len(tocs)-1])
print()
# Average delay based on Train Service Line
services = set()
services_dict = dict()
for x in neg_data:
services.add(x[10])
print("Average Mins off Timetable: ")
for y in services:
temp_count = 0
temp_sum = 0
late = 0
for x in neg_data:
if x[10] == y:
temp_count += 1
temp_sum += x[2]
if x[9] == "LATE":
late += 1
print(y, ": ", round(temp_sum/temp_count, 2), "Mins off \t\t", round((late/temp_count)*100), "% Late")
services_dict[y] = temp_sum/temp_count
print()
# Latest/Earliest (on average) service
earliest = ""
latest = ""
for key in services_dict:
if services_dict[key] == sorted(services_dict.values())[0]:
latest = key
elif services_dict[key] == sorted(services_dict.values())[len(services_dict)-1]:
earliest = key
print("On Average Service Most Late: ", latest, " Mins Delayed: ", sorted(services_dict.values())[0], " On Average Service Most Early: ", earliest, " Mins Early:", sorted(services_dict.values())[len(services_dict)-1])
print()
# Busiest Station
stations_count_dict = dict()
for y in stations:
temp_count = 0
for x in neg_data:
if x[12] == y:
temp_count += 1
stations_count_dict[y] = temp_count
most_busy = ""
least_busy = ""
for key in stations_count_dict:
if stations_count_dict[key] == sorted(stations_count_dict.values())[0]:
least_busy = key
elif stations_count_dict[key] == sorted(stations_count_dict.values())[len(stations_count_dict)-1]:
most_busy = key
print("On Average Station Most Busy: ", get_stanox(most_busy), " Visits: ", sorted(stations_count_dict.values())[0], " On Average Service Least Busy: ", get_stanox(least_busy), " Visits:", sorted(stations_count_dict.values())[len(stations_count_dict)-1])
print()
# Most Common Train Service
services_count_dict = dict()
for y in services:
temp_count = 0
for x in neg_data:
if x[10] == y:
temp_count += 1
services_count_dict[y] = temp_count
most_common = ""
least_common = ""
for key in services_count_dict:
if services_count_dict[key] == sorted(services_count_dict.values())[0]:
most_common = key
elif services_count_dict[key] == sorted(services_count_dict.values())[len(services_count_dict)-1]:
least_common = key
print("On Average Service Most Common: ", most_common, " Number of Services: ", sorted(services_count_dict.values())[0], " On Average Service Least Common: ", least_common, " Number of Services:", sorted(services_count_dict.values())[len(services_count_dict)-1])