Spaces:
Sleeping
Sleeping
tappyness1
commited on
Commit
·
2399098
1
Parent(s):
d7aa6c5
predictive app
Browse files- app.py +20 -15
- notebooks/Causin_Final_Notebook.ipynb +0 -0
- src/pred_plot.py +101 -35
app.py
CHANGED
@@ -8,7 +8,7 @@ from src.map_viz import calling_map_viz
|
|
8 |
from src.data_ingestion import daily_average
|
9 |
from src.heatmap import HeatMap
|
10 |
from src.data_ingestion import remove_previous_view, merge_volumes
|
11 |
-
from src.pred_plot import prep_data_pred_plot, data_split, train_model, predicted_figure, get_today,
|
12 |
from datetime import date
|
13 |
|
14 |
def fetch_data():
|
@@ -97,22 +97,27 @@ def main():
|
|
97 |
final_table = prep_data_pred_plot(pred_df)
|
98 |
x_train, _, y_train, _ = data_split(final_table)
|
99 |
clf = train_model(x_train, y_train)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
113 |
|
114 |
starter_variables = [x_train, str(d), pred_hour_choice, pred_view_choice]
|
115 |
st.plotly_chart(predicted_figure(clf, starter_variables, figs))
|
116 |
-
|
|
|
117 |
if __name__ == "__main__":
|
118 |
main()
|
|
|
8 |
from src.data_ingestion import daily_average
|
9 |
from src.heatmap import HeatMap
|
10 |
from src.data_ingestion import remove_previous_view, merge_volumes
|
11 |
+
from src.pred_plot import prep_data_pred_plot, data_split, train_model, predicted_figure, get_today, gen_fig, pred_bars
|
12 |
from datetime import date
|
13 |
|
14 |
def fetch_data():
|
|
|
97 |
final_table = prep_data_pred_plot(pred_df)
|
98 |
x_train, _, y_train, _ = data_split(final_table)
|
99 |
clf = train_model(x_train, y_train)
|
100 |
+
col1, col2, col3 = st.columns(3)
|
101 |
+
with col1:
|
102 |
+
pred_hour_choice = st.selectbox(
|
103 |
+
"Choose Your Planned Hour",
|
104 |
+
options= hours,
|
105 |
+
key = "pred_hour", index = hours.index("08:00")
|
106 |
+
)
|
107 |
+
with col2:
|
108 |
+
pred_view_choice = st.selectbox(
|
109 |
+
"Choose View",
|
110 |
+
options= ['Johor-Tuas','Johor-Woodlands', 'Tuas-Johor', 'Woodlands-Johor'],
|
111 |
+
key = "pred_view"
|
112 |
+
)
|
113 |
+
with col3:
|
114 |
+
d = st.date_input(
|
115 |
+
"Choose Your Planned Date",
|
116 |
+
date(today[0],today[1], today[2]))
|
117 |
|
118 |
starter_variables = [x_train, str(d), pred_hour_choice, pred_view_choice]
|
119 |
st.plotly_chart(predicted_figure(clf, starter_variables, figs))
|
120 |
+
st.plotly_chart(pred_bars(d, final_table))
|
121 |
+
|
122 |
if __name__ == "__main__":
|
123 |
main()
|
notebooks/Causin_Final_Notebook.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
src/pred_plot.py
CHANGED
@@ -3,6 +3,8 @@ from sklearn.model_selection import train_test_split
|
|
3 |
from sklearn.neural_network import MLPClassifier
|
4 |
import pandas as pd
|
5 |
import plotly.graph_objects as go
|
|
|
|
|
6 |
|
7 |
def hour_rounder(t):
|
8 |
if int(t.minute)>= 30:
|
@@ -32,13 +34,13 @@ def weekend(w):
|
|
32 |
return 0
|
33 |
|
34 |
def vehicle_cat(v):
|
35 |
-
if v >= 0 and v <
|
36 |
return 0
|
37 |
-
elif v >=
|
38 |
return 1
|
39 |
-
elif v >=
|
40 |
return 2
|
41 |
-
elif v >=
|
42 |
return 3
|
43 |
else:
|
44 |
return 4
|
@@ -117,8 +119,8 @@ def prep_data_pred_plot(df):
|
|
117 |
final_df.loc[:, 'time'] = pd.to_datetime(final_df.loc[:,'time'], format='%H:%M:%S')
|
118 |
final_df.loc[:,'hour'] = final_df.loc[:,'time'].apply(hour_rounder)
|
119 |
|
120 |
-
final_table = final_df.groupby(['view', 'day', 'hour']).
|
121 |
-
|
122 |
final_table.loc[:,'peak'] = final_table.loc[:,'hour'].apply(peak_hours)
|
123 |
final_table.loc[:,'peak'] = final_table.loc[:,'peak'].astype('category')
|
124 |
final_table.loc[:,'weekend'] = final_table.loc[:,'day'].apply(weekend)
|
@@ -129,37 +131,63 @@ def prep_data_pred_plot(df):
|
|
129 |
return final_table
|
130 |
|
131 |
def gen_fig():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
figs = []
|
|
|
|
|
133 |
|
134 |
for i in range(5):
|
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 |
return figs
|
165 |
|
@@ -200,4 +228,42 @@ def train_model(x_train, y_train):
|
|
200 |
clf = MLPClassifier(solver='lbfgs', alpha=3, hidden_layer_sizes=(5,4), random_state=2, max_iter=3000)
|
201 |
clf.fit(x_train, y_train)
|
202 |
|
203 |
-
return clf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from sklearn.neural_network import MLPClassifier
|
4 |
import pandas as pd
|
5 |
import plotly.graph_objects as go
|
6 |
+
import streamlit as st
|
7 |
+
from plotly.subplots import make_subplots
|
8 |
|
9 |
def hour_rounder(t):
|
10 |
if int(t.minute)>= 30:
|
|
|
34 |
return 0
|
35 |
|
36 |
def vehicle_cat(v):
|
37 |
+
if v >= 0 and v < 2:
|
38 |
return 0
|
39 |
+
elif v >= 2 and v < 4:
|
40 |
return 1
|
41 |
+
elif v >= 4 and v < 6:
|
42 |
return 2
|
43 |
+
elif v >= 6 and v < 8:
|
44 |
return 3
|
45 |
else:
|
46 |
return 4
|
|
|
119 |
final_df.loc[:, 'time'] = pd.to_datetime(final_df.loc[:,'time'], format='%H:%M:%S')
|
120 |
final_df.loc[:,'hour'] = final_df.loc[:,'time'].apply(hour_rounder)
|
121 |
|
122 |
+
final_table = final_df.groupby(['view', 'day', 'hour']).mean().reset_index().loc[:,['day', 'hour','view', 'vehicle']]
|
123 |
+
final_table['vehicle'] = final_table['vehicle'].apply(lambda x: round(x))
|
124 |
final_table.loc[:,'peak'] = final_table.loc[:,'hour'].apply(peak_hours)
|
125 |
final_table.loc[:,'peak'] = final_table.loc[:,'peak'].astype('category')
|
126 |
final_table.loc[:,'weekend'] = final_table.loc[:,'day'].apply(weekend)
|
|
|
131 |
return final_table
|
132 |
|
133 |
def gen_fig():
|
134 |
+
|
135 |
+
paths = ["M 0.2 0.35 L 0.48 0.52 L 0.52 0.50",
|
136 |
+
"M 0.25 0.75 L 0.475 0.52 L 0.52 0.52",
|
137 |
+
"M 0.5 0.9 L 0.485 0.52 L 0.515 0.52",
|
138 |
+
"M 0.75 0.75 L 0.485 0.52 L 0.52 0.51",
|
139 |
+
"M 0.8 0.35 L 0.48 0.50 L 0.52 0.52"]
|
140 |
+
|
141 |
figs = []
|
142 |
+
values_ = ["No Traffic on Johor-Singapore Causeway", "Low Traffic on Johor-Singapore Causeway", "Johor-Singapore Causeway Slightly Busy",
|
143 |
+
"Johor-Singapore Causeway Moderately Busy", "Busiest Time to Travel on Johor-Singapore Causeway"]
|
144 |
|
145 |
for i in range(5):
|
146 |
+
plot_bgcolor = "#def"
|
147 |
+
colors = ["#f25829", "#f2a529", "#eff229", "#85e043", "#2bad4e","rgba(0,0,0,0)"]
|
148 |
+
quadrant_text = ["<b>Heavy</b>", "<b>Moderate</b>", "<b>Mild</b>", "<b>Low</b>", "<b>None</b>",""]
|
149 |
+
n_quadrants = len(colors) - 1
|
150 |
+
figure_1 = go.Figure(
|
151 |
+
data=[
|
152 |
+
go.Pie(
|
153 |
+
values=[14,14,14,14,14,30],
|
154 |
+
rotation=130,
|
155 |
+
hole=0.75,
|
156 |
+
marker_colors=colors,
|
157 |
+
marker_line={"width":2, "color":"white"},
|
158 |
+
textinfo="none",
|
159 |
+
text=quadrant_text,
|
160 |
+
hoverinfo="text"
|
161 |
+
),
|
162 |
+
],
|
163 |
+
layout=go.Layout(
|
164 |
+
showlegend=False,
|
165 |
+
margin=dict(b=0,t=30,l=10,r=10),
|
166 |
+
width=500,
|
167 |
+
height=350,
|
168 |
+
paper_bgcolor="rgba(0,0,0,0)",
|
169 |
+
annotations=[
|
170 |
+
go.layout.Annotation(
|
171 |
+
text=f"<b>{values_[i]}</b>",
|
172 |
+
x=0.5, xanchor="center", xref="paper",
|
173 |
+
y= 0.1, yanchor="bottom", yref="paper",
|
174 |
+
showarrow=False,
|
175 |
+
font= {"size":15, "color":"#333"}
|
176 |
+
)
|
177 |
+
]
|
178 |
+
)
|
179 |
+
)
|
180 |
+
figure_1.update_layout(shapes=[dict(type='path',
|
181 |
+
path=paths[i],
|
182 |
+
fillcolor="#333"),
|
183 |
+
go.layout.Shape(
|
184 |
+
type="circle",
|
185 |
+
x0=0.48, x1=0.52,
|
186 |
+
y0=0.48, y1=0.54,
|
187 |
+
fillcolor="#333",
|
188 |
+
line_color="#333",
|
189 |
+
)])
|
190 |
+
figs.append(figure_1)
|
191 |
|
192 |
return figs
|
193 |
|
|
|
228 |
clf = MLPClassifier(solver='lbfgs', alpha=3, hidden_layer_sizes=(5,4), random_state=2, max_iter=3000)
|
229 |
clf.fit(x_train, y_train)
|
230 |
|
231 |
+
return clf
|
232 |
+
|
233 |
+
def pred_bars(my_date_picker_single, final_table):
|
234 |
+
day_today = convert_date(str(my_date_picker_single))
|
235 |
+
df_filter = final_table[final_table['day']==day_today]
|
236 |
+
|
237 |
+
color_map = {0:"#2bad4e", 1:"#85e043", 2:"#eff229", 3:"#f2a529", 4:"#f25829"}
|
238 |
+
|
239 |
+
|
240 |
+
bar_day = make_subplots(shared_yaxes="all", rows=2, cols=2, start_cell="bottom-left", subplot_titles=("Johor-Tuas",
|
241 |
+
"Tuas-Johor",
|
242 |
+
"Johor-Woodlands",
|
243 |
+
"Johor-Woodlands"))
|
244 |
+
f1 = df_filter[df_filter['view']=='Johor-Tuas']
|
245 |
+
c1 = pd.Series(f1['cat']).map(color_map)
|
246 |
+
bar_day.add_trace(go.Bar(x=f1['hour'], y=f1['vehicle'], name='Johor-Tuas', showlegend=False, marker={'color':c1}),
|
247 |
+
row=1, col=1)
|
248 |
+
|
249 |
+
f2 = df_filter[df_filter['view']=='Tuas-Johor']
|
250 |
+
c2 = pd.Series(f2['cat']).map(color_map)
|
251 |
+
bar_day.add_trace(go.Bar(x=f2['hour'], y=f2['vehicle'], name='Tuas-Johor', showlegend=False, marker={'color':c2}),
|
252 |
+
row=1, col=2)
|
253 |
+
f3 = df_filter[df_filter['view']=='Johor-Woodlands']
|
254 |
+
c3 = pd.Series(f3['cat']).map(color_map)
|
255 |
+
bar_day.add_trace(go.Bar(x=f3['hour'], y=f3['vehicle'], name='Johor-Woodlands', showlegend=False, marker={'color':c3}),
|
256 |
+
row=2, col=1)
|
257 |
+
f4 = df_filter[df_filter['view']=='Woodlands-Johor']
|
258 |
+
c4 = pd.Series(f4['cat']).map(color_map)
|
259 |
+
bar_day.add_trace(go.Bar(x=f4['hour'], y=f4['vehicle'], name='Johor-Woodlands', showlegend=False, marker={'color':c4}),
|
260 |
+
row=2, col=2)
|
261 |
+
|
262 |
+
val_d = date.today().strftime("%d %B, %Y")
|
263 |
+
day_d = date.today().strftime("%A")
|
264 |
+
tex = "Predicted 24 Hour Traffic Trend on: " + day_d + ", " + str(val_d)
|
265 |
+
|
266 |
+
|
267 |
+
bar_day.update_layout(title_text=tex, paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)")
|
268 |
+
bar_day.update_xaxes(tickangle=45)
|
269 |
+
return bar_day
|