This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsimulate.py
77 lines (56 loc) · 2.11 KB
/
simulate.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
import argparse
import datetime as dt
import json
import time
from river import datasets
from river import metrics
from river import stream
import requests
class colors:
GREEN = '\033[92m'
BLUE = '\033[94m'
ENDC = '\033[0m'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('speed_up', type=int, nargs='?', default=1)
args = parser.parse_args()
def sleep(td: dt.timedelta):
if td.seconds >= 0:
time.sleep(td.seconds / args.speed_up)
# Use the first trip's departure time as a reference time
taxis = datasets.Taxis()
now = next(iter(taxis))[0]['pickup_datetime']
mae = metrics.MAE()
host = 'http://localhost:5000'
predictions = {}
for trip_no, trip, duration in stream.simulate_qa(
taxis,
moment='pickup_datetime',
delay=lambda _, duration: dt.timedelta(seconds=duration)
):
trip_no = str(trip_no).zfill(len(str(taxis.n_samples)))
# Taxi trip starts
if duration is None:
# Wait
sleep(trip['pickup_datetime'] - now)
now = trip['pickup_datetime']
# Ask chantilly to make a prediction
r = requests.post(host + '/api/predict', json={
'id': trip_no,
'features': {**trip, 'pickup_datetime': trip['pickup_datetime'].isoformat()}
})
# Store the prediction
predictions[trip_no] = r.json()['prediction']
print(colors.GREEN + f'#{trip_no} departs at {now}' + colors.ENDC)
continue
# Taxi trip ends
# Wait
arrival_time = trip['pickup_datetime'] + dt.timedelta(seconds=duration)
sleep(arrival_time - now)
now = arrival_time
# Ask chantilly to update the model
requests.post(host + '/api/learn', json={'id': trip_no, 'ground_truth': duration})
# Update the metric
mae.update(y_true=duration, y_pred=predictions.pop(trip_no))
msg = f'#{trip_no} arrives at {now} - average error: {dt.timedelta(seconds=mae.get())}'
print(colors.BLUE + msg + colors.ENDC)