This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_data.py
231 lines (198 loc) · 6.95 KB
/
visualize_data.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
227
228
229
230
231
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from dash import Dash, dcc, html
import plotly.express as px
import csv
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import matplotlib.pyplot as plt
import cv2
from util import *
from plots import *
LIVE = False
# code and plot setups
# settings
# pd.options.plotting.backend = "plotly"
paths_translink = list(Path("output/translink").iterdir())
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div(
[
html.H1("Live Translink"),
dcc.Interval(
id="interval-component",
interval=180 * 1000, # in milliseconds
n_intervals=0,
),
dbc.Row(
[
dbc.Col(dcc.Graph(id="graph")),
dbc.Col(
[
"Select route number: ",
dcc.Input(
id="input",
),
dcc.Graph(id="graph2"),
dcc.Slider(
0,
len(paths_translink),
1,
value=0,
id="slider",
marks=None,
),
]
),
]
),
dbc.Row([dbc.Col(dcc.Graph(id="graph5"))]),
dbc.Row([dbc.Col(dcc.Graph(id="graph3")), dbc.Col(dcc.Graph(id="graph4"))]),
dbc.Row([dbc.Col(dcc.Graph(id="graph6")), dbc.Col(dcc.Graph(id="graph7"))]),
]
)
df_stops = pd.read_csv("data/stops.txt")
df_routes = pd.read_csv("data/routes.txt")
fig5, fig6, fig7 = None, None, None
# CAUTION might take a long time if a lot of data is collected in ./output
# df_aggregate = aggregate_csvs()
quantiles = [0.05, 0.95]
# df_aggregate = add_suburbs(df_aggregate)
# fig5 = get_choropleth(df_aggregate)
# fig6 = get_delay_histogram(df_aggregate, quantiles)
# fig5 = get_rain_delay_plot(df_aggregate, quantiles)
# Define callback to update graph
@app.callback(
[
Output("graph", "figure"),
Output("graph2", "figure"),
Output("graph3", "figure"),
Output("graph4", "figure"),
Output("graph5", "figure"),
Output("graph6", "figure"),
Output("graph7", "figure"),
],
[
Input("interval-component", "n_intervals"),
Input("input", "value"),
Input("slider", "value"),
],
)
def streamFig(value, input, slider):
if LIVE:
df = get_rt_vehicle_df()
df_combine = df.merge(df_routes, on="route_id")
df_combine = df_combine.merge(df_stops, on="stop_id")
df_combine = filter_lat_lon(df_combine)
else:
df_combine = csv_to_df(paths_translink[slider])
df_combine = add_suburbs(df_combine)
df_combine["rain_dbz"] = df_combine["rain_dbz"].astype(str)
fig = go.Figure()
if input != None:
if LIVE:
df_route_updates = get_route_updates()
df_selection = df_combine.loc[df_combine["route_short_name"] == input]
if LIVE:
df_selection = df_selection.merge(df_route_updates, on="trip_id")
# scatter live vehicle location
fig.add_scattermapbox(
mode="markers",
lat=df_selection["lat"],
lon=df_selection["lon"],
marker=dict(size=16),
name="vehicles",
text=df_selection["vehicle_label"]
+ " | "
+ df_selection["route_long_name"]
+ " to: "
+ df_selection["stop_name"]
+ " | in: "
+ df_selection["LOC_NAME"]
+ " | rain: "
+ df_selection["rain_dbz"],
)
# scatter upcoming stops for selected route
for row_num, row in enumerate(df_selection.iterrows()):
upcoming_stops = row[1]["upcoming_stops"].merge(df_stops, on="stop_id")
fig.add_scattermapbox(
mode="lines+markers",
lat=upcoming_stops["stop_lat"],
lon=upcoming_stops["stop_lon"],
name="vehicle " + row[1]["vehicle_label"] + " destination",
marker=dict(size=13),
text=upcoming_stops["stop_name"]
+ " delay: "
+ str(upcoming_stops["arrival_delay"].item()),
)
fig.update_mapboxes(
center={
"lat": df_selection["lat"].mean(),
"lon": df_selection["lon"].mean(),
},
zoom=11,
)
else:
route_type_mapper_dict = {
"Tram": "yellow",
"": "cyan",
"Train": "green",
"Bus": "tomato",
"Boat": "aqua",
}
for route_type_name, df_by_route_type in df_combine.groupby("route_type"):
fig.add_scattermapbox(
lat=df_by_route_type["lat"],
lon=df_by_route_type["lon"],
name=route_type_name,
fillcolor=route_type_mapper_dict[route_type_name],
text=df_by_route_type["route_short_name"]
+ " | "
+ df_by_route_type["route_long_name"]
+ " to: "
+ df_by_route_type["stop_name"]
+ " | suburb: "
+ df_by_route_type["LOC_NAME"]
+ "| rain: "
+ df_by_route_type["rain_dbz"],
)
fig.update_mapboxes(
center={"lat": df_combine["lat"].mean(), "lon": df_combine["lon"].mean()},
zoom=11,
)
# rain radar -----------------------------------------
# print(f"output/weather/radar_{df_combine['timestamp_radar'].iloc[0].item()}.jpg")
image = cv2.imread(
f"output/weather/radar_{df_combine['timestamp_radar'].iloc[0].item()}.jpg"
)[:, :, ::-1]
image = convert_radar_colormap(image)
image_base = cv2.imread("data/base_observationwindow.png")[:, :, ::-1]
image_base = cv2.resize(image_base, (512, 512), interpolation=cv2.INTER_LINEAR)
image = cv2.addWeighted(image_base, 0.3, image, 0.6, 0)
fig2 = px.imshow(image[:300, 50:350, :], title="rain radar")
# delay plots -----------------------------------------
fig3 = get_delay_histogram(df_combine, quantiles)
fig4 = get_delay_boxplot(df_combine)
fig5 = get_choropleth(df_combine)
# fig6 = get_rain_delay_plot(df_combine)
# ----------------------------------------
fig.update_layout(
mapbox_style="carto-positron",
)
fig.update_layout(legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01
))
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
print("figures updated - start creating dash website")
return fig, fig2, fig3, fig4, fig5, fig6, fig7
app.run_server(
# mode="external",
port=8069,
dev_tools_ui=True, # debug=True,
dev_tools_hot_reload=True,
threaded=True,
)