Spaces:
Sleeping
Sleeping
predictive app
Browse files- app.py +21 -0
- src/map_viz_pred.py +108 -0
app.py
CHANGED
@@ -5,6 +5,7 @@ from datasets import load_dataset
|
|
5 |
import os
|
6 |
from src.basic_plot import basic_chart
|
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
|
@@ -91,6 +92,9 @@ def main():
|
|
91 |
st.plotly_chart(heatmap.update_day_bar_chart(day_choice))
|
92 |
|
93 |
else:
|
|
|
|
|
|
|
94 |
|
95 |
figs = gen_fig()
|
96 |
today = get_today()
|
@@ -119,5 +123,22 @@ def main():
|
|
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()
|
|
|
5 |
import os
|
6 |
from src.basic_plot import basic_chart
|
7 |
from src.map_viz import calling_map_viz
|
8 |
+
from src.map_viz_pred import calling_pred_map_viz
|
9 |
from src.data_ingestion import daily_average
|
10 |
from src.heatmap import HeatMap
|
11 |
from src.data_ingestion import remove_previous_view, merge_volumes
|
|
|
92 |
st.plotly_chart(heatmap.update_day_bar_chart(day_choice))
|
93 |
|
94 |
else:
|
95 |
+
|
96 |
+
st.sidebar.markdown("Select Plots to show")
|
97 |
+
checkbox_two_pred = st.sidebar.checkbox('Predictive Traffic Map', value = True)
|
98 |
|
99 |
figs = gen_fig()
|
100 |
today = get_today()
|
|
|
123 |
st.plotly_chart(predicted_figure(clf, starter_variables, figs))
|
124 |
st.plotly_chart(pred_bars(d, final_table))
|
125 |
|
126 |
+
|
127 |
+
if checkbox_two_pred:
|
128 |
+
st.subheader("Predictive Traffic Map")
|
129 |
+
|
130 |
+
|
131 |
+
hour_choice = st.selectbox(
|
132 |
+
"Choose Hour",
|
133 |
+
options= hours,
|
134 |
+
key = "hour", index = hours.index("08:00")
|
135 |
+
)
|
136 |
+
|
137 |
+
days = ["Monday", "Tuesday", "Wednesday",
|
138 |
+
"Thursday", "Friday","Saturday", "Sunday"]
|
139 |
+
day_choice = st.selectbox("Choose Day of the Week", options = days, key = "day", index = days.index("Saturday"))
|
140 |
+
|
141 |
+
st.pyplot(calling_pred_map_viz(counts_df, day_choice, hour_choice))
|
142 |
+
|
143 |
if __name__ == "__main__":
|
144 |
main()
|
src/map_viz_pred.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import division, print_function
|
2 |
+
from six import StringIO
|
3 |
+
from svgpath2mpl import parse_path
|
4 |
+
from collections import defaultdict
|
5 |
+
from src.pred_plot import hour_rounder
|
6 |
+
import xml.etree.ElementTree as etree
|
7 |
+
import re
|
8 |
+
import matplotlib as mpl
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import numpy as np
|
11 |
+
import requests
|
12 |
+
import pandas as pd
|
13 |
+
import datetime
|
14 |
+
|
15 |
+
import warnings
|
16 |
+
warnings.filterwarnings("ignore")
|
17 |
+
|
18 |
+
def calling_pred_map_viz(counts_df1, input_day = None, input_hour = None):
|
19 |
+
r = "svg/snazzy-image-01.svg"
|
20 |
+
tree = etree.parse(r)
|
21 |
+
root = tree.getroot()
|
22 |
+
path_elems = root.findall('.//{http://www.w3.org/2000/svg}path')
|
23 |
+
|
24 |
+
paths = [parse_path(elem.attrib['d']) for elem in path_elems]
|
25 |
+
facecolors = []
|
26 |
+
edgecolors = []
|
27 |
+
linewidths = []
|
28 |
+
for elem in path_elems:
|
29 |
+
facecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("fill", "none"))
|
30 |
+
edgecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke", "none"))
|
31 |
+
linewidths.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke-width", "none").replace("px", ""))
|
32 |
+
|
33 |
+
path_id = defaultdict(int)
|
34 |
+
for i, elem in enumerate(path_elems):
|
35 |
+
try:
|
36 |
+
#print(i, elem.attrib['id'])
|
37 |
+
path_id[elem.attrib['id']] = i
|
38 |
+
except:
|
39 |
+
continue
|
40 |
+
|
41 |
+
counts_df1['total'] = counts_df1['car'] + counts_df1['motorcycle'] + counts_df1['large_vehicle']
|
42 |
+
counts_df1.loc[:,'date_time'] = pd.to_datetime(counts_df1.loc[:,'date'] + " "+ counts_df1.loc[:,'time'], format='%Y-%m-%d %H:%M:%S')
|
43 |
+
counts_df1.loc[:,'hour'] = counts_df1.loc[:,'date_time'].apply(hour_rounder)
|
44 |
+
counts_df1.loc[:,'day_name'] = counts_df1.loc[:,'date_time'].dt.day_name()
|
45 |
+
|
46 |
+
if (input_day != None) & (input_hour != None):
|
47 |
+
filtered_day = input_day
|
48 |
+
filtered_hour = input_hour
|
49 |
+
else:
|
50 |
+
filtered_date = counts_df1.iloc[-1]['date']
|
51 |
+
filtered_time = counts_df1.iloc[-1]['time']
|
52 |
+
filtered_day = counts_df1.iloc[-1]['day_name']
|
53 |
+
filtered_hour = counts_df1.iloc[-1]['hour']
|
54 |
+
|
55 |
+
|
56 |
+
# filtered_date = counts_df1.iloc[-1]['date']
|
57 |
+
# filtered_time = counts_df1.iloc[-1]['time']
|
58 |
+
# filtered_day = counts_df1.iloc[-1]['day_name']
|
59 |
+
# filtered_hour = counts_df1.iloc[-1]['hour']
|
60 |
+
|
61 |
+
|
62 |
+
day_hour_view_group = counts_df1.groupby(by=['view', 'day_name', 'hour'])['total'].mean().reset_index()
|
63 |
+
count_max = day_hour_view_group['total'].max()
|
64 |
+
count_min = day_hour_view_group['total'].min()
|
65 |
+
|
66 |
+
|
67 |
+
count_dict = {"woodlands_to_sg" :day_hour_view_group.loc[(day_hour_view_group['view'] == 'Woodlands - to SG') & (day_hour_view_group['day_name'] == filtered_day) & (day_hour_view_group['hour'] == filtered_hour), "total" ].iloc[0],
|
68 |
+
"woodlands_to_jh" :day_hour_view_group.loc[(day_hour_view_group['view'] == 'Woodlands - to Johor') & (day_hour_view_group['day_name'] == filtered_day) & (day_hour_view_group['hour'] == filtered_hour), "total" ].iloc[0],
|
69 |
+
"tuas_to_sg" :day_hour_view_group.loc[(day_hour_view_group['view'] == 'Tuas - to SG') & (day_hour_view_group['day_name'] == filtered_day) & (day_hour_view_group['hour'] == filtered_hour), "total" ].iloc[0],
|
70 |
+
"tuas_to_jh" :day_hour_view_group.loc[(day_hour_view_group['view'] == 'Tuas - to Johor') & (day_hour_view_group['day_name'] == filtered_day) & (day_hour_view_group['hour'] == filtered_hour), "total" ].iloc[0]
|
71 |
+
}
|
72 |
+
|
73 |
+
values = np.array([0., 0.25, 1.])
|
74 |
+
values = np.sort(np.array(values))
|
75 |
+
values = np.interp(values, (values.min(), values.max()), (0., 1.))
|
76 |
+
colors = ["#539f6b", "#ffc835", "#bf0000"]
|
77 |
+
cmap = mpl.colors.LinearSegmentedColormap.from_list("custom", list(zip(values, colors)))
|
78 |
+
|
79 |
+
norm = mpl.colors.Normalize(vmin=count_min, vmax=count_max)
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
hex_dict = {k: mpl.colors.to_hex(cmap(norm(v))) for k, v in count_dict.items()}
|
84 |
+
color_dict = defaultdict(str)
|
85 |
+
|
86 |
+
for k, i in path_id.items():
|
87 |
+
#print(k, i)
|
88 |
+
color_dict[i] = hex_dict[k]
|
89 |
+
|
90 |
+
for k, i in color_dict.items():
|
91 |
+
#print(k,i)
|
92 |
+
facecolors[k] = i
|
93 |
+
|
94 |
+
collection = mpl.collections.PathCollection(paths,
|
95 |
+
edgecolors=edgecolors,
|
96 |
+
linewidths=[int(i)/100 for i in linewidths if i != 'none'],
|
97 |
+
facecolors=[i.strip() for i in facecolors])
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
fig = plt.figure(figsize=(10,10))
|
102 |
+
ax = fig.add_subplot(111)
|
103 |
+
collection.set_transform(ax.transData)
|
104 |
+
ax.add_artist(collection)
|
105 |
+
ax.set_xlim([100, 1900])
|
106 |
+
ax.set_ylim([1800, 0])
|
107 |
+
ax.set_title(filtered_day+ " | " + filtered_hour + " SGT", fontname = 'Georgia')
|
108 |
+
return fig
|