-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetAboutVRTrains.py
226 lines (182 loc) · 7.06 KB
/
tweetAboutVRTrains.py
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
import pandas as pd
import numpy as np
import datetime
import time
from dateutil.relativedelta import relativedelta
from sklearn import linear_model, model_selection, pipeline, preprocessing
from fetchTrainData import fetch_train_data
from fetchWeatherData import fetch_weather_data, save_weather_data, open_weather_data
from twitterBot import tweet_station_congested
weather_data = {}
def get_weather(stop_):
tag = stop_['station.shortCode']
if type(tag) == str:
lat = stop_['station.location'][0]
lon = stop_['station.location'][1]
if tag not in weather_data:
print("FETCHING DATA FOR: " + tag, end = "\r")
weather_data[tag] = fetch_weather_data(unix_time, lat, lon)['list'][0]['main']['temp']
def extract_time_tables(train_stops_):
print("TRAIN NUMBER " + str(train_stops_.name) + ", " + str(float(train_stops_.name) / number_of_trains), end = "\r")
train_stops = pd.json_normalize(train_stops_)
to_return = train_stops['differenceInMinutes'].sum()
train_stops = train_stops.drop(['differenceInMinutes'], axis=1)
train_stops.apply(get_weather, axis=1)
for stop in train_stops['station.shortCode'].unique():
if type(stop) == str:
if stop not in df.columns:
df[stop] = np.nan
df[stop][train_stops_.name] = weather_data[stop]
return to_return
tweet = True # Should results be tweeted about, or just printed
loops = 12 # How many months data we want in the dataframe, apparently we can't get over 12 months data
number_of_trains = 750 # How many trains to fetch per month. Seems to be softlocked at about 1000, 500 is safe
all_data_frames = []
all_late_amounts = []
for i in range(loops):
date = datetime.date.today() - relativedelta(months = i)
unix_time = time.mktime(date.timetuple())
print()
print("LOOP " + str(i) + ", DATE: " + str(date))
q = (
"""
{
"""
f' trainsByDepartureDate(departureDate: \"{date}\",'
"""
"""
f' take: {number_of_trains},'
"""
where: {
and: [
{or: [{deleted: {unequals: null}},
{deleted: {equals: false}}]},
{cancelled: {equals: false}},
{operator: {shortCode: {equals: "vr"}}}
]
}
)
{
trainNumber
departureDate
timeTableRows {
differenceInMinutes
station {
shortCode
location
}
}
}
}
"""
)
result = fetch_train_data(q)
recs = result['data']['trainsByDepartureDate']
df = pd.json_normalize(recs)
all_stops = pd.json_normalize(df['timeTableRows'])
df = df.drop(['timeTableRows'], axis=1)
weather_data = open_weather_data(date)
how_much_late = all_stops.apply(extract_time_tables, axis=1)
save_weather_data(date, weather_data)
df['departureDate'] = date.month
all_data_frames.append(df)
all_late_amounts.append(how_much_late)
combined_train_data = pd.concat(all_data_frames).fillna(0).reset_index().drop(['index'], axis=1)
combined_late_amounts = pd.concat(all_late_amounts).fillna(0).reset_index().drop(['index'], axis=1)
combined_late_amounts.rename(columns={0: "total_late"}, inplace=True)
combined_late_amounts = combined_late_amounts["total_late"].apply(lambda x: int(x > 4))
print()
print("DONE MAKING DATAFRAME")
best_score = 0
best_reg = linear_model.SGDClassifier(max_iter=1000)
for i in range(50):
print(i, end = "\r")
training_data, test_data, train_target, test_target = model_selection.train_test_split(combined_train_data, combined_late_amounts, train_size=0.8)
train_target = np.ravel(train_target)
test_target = np.ravel(test_target)
reg = linear_model.SGDClassifier(max_iter=1000)
reg.fit(training_data, train_target)
reg_score = reg.score(test_data, test_target)
if reg_score > best_score:
best_score = reg_score
best_reg = reg
print("NEW BEST SCORE: " + str(best_score))
print(best_reg.score(test_data, test_target))
def extract_time_tables_station(train_stops_):
train_stops = pd.json_normalize(train_stops_)
train_stops.apply(get_weather, axis=1)
for stop in train_stops['station.shortCode'].unique():
if type(stop) == str:
if stop not in df.columns:
df[stop] = np.nan
df[stop][train_stops_.name] = weather_data[stop]
# List here stations that are checked, and possible tweeted about
stations_to_check = [('HKI', 'Helsinki'), ('PSL', 'Pasila'), ('TKL', 'Tikkurila'), ('KE', 'Kerava'),
('JNS', 'Joensuu'), ('TPE', 'Tampere'), ('TKU', 'Turku'), ('HL', 'Hämeenlinna'),
('OL', 'Oulu'), ('SK', 'Seinäjoki'), ('JY', 'Jyväskylä'), ('KV', 'Kouvola')]
results = []
for station in stations_to_check:
date = datetime.date.today()
unix_time = time.mktime(date.timetuple())
q = (
"""
{
"""
f' trainsByStationAndQuantity(station: "{station[0]}",'
"""
"""
f' take: {200},'
"""
where: {
and: [
{or: [{deleted: {unequals: null}},
{deleted: {equals: false}}]},
{cancelled: {equals: false}},
{operator: {shortCode: {equals: "vr"}}}
]
}
)
{
trainNumber
departureDate
timeTableRows {
station {
shortCode
location
}
}
}
}
"""
)
result = fetch_train_data(q)
recs = result['data']['trainsByStationAndQuantity']
df = pd.json_normalize(recs)
all_stops = pd.json_normalize(df['timeTableRows'])
df = df.drop(['timeTableRows'], axis=1)
weather_data = open_weather_data(date)
all_stops.apply(extract_time_tables_station, axis=1)
save_weather_data(date, weather_data)
df['departureDate'] = date.month
df.fillna(0, inplace=True)
for station in best_reg.feature_names_in_:
if station not in df.columns:
df[station] = 0.0
for station in df.columns:
if station not in best_reg.feature_names_in_:
df.drop([station], inplace=True, axis=1)
how_many_late = 0
predictions = best_reg.predict(df)
for prediction in predictions:
if prediction == 1:
how_many_late = how_many_late + 1
results.append(round((how_many_late / len(predictions)) * best_score, 2)) # What percent of trains does the algorithm think are going to be late
if tweet:
for i, val in enumerate(stations_to_check):
if results[i] > 0.65:
tweet_station_congested(val[1], results[i])
print(f"TWEETED ABOUT {val[1]} BEING CONGESTED")
else:
print(stations_to_check)
print(results)
print("DONE!!")