Spaces:
Sleeping
Sleeping
tappyness1
commited on
Commit
•
471f224
1
Parent(s):
1c43051
new charts
Browse files- .gitignore +2 -1
- app.py +9 -15
- env.yml +2 -1
- requirements.txt +1 -0
- src/basic_plot.py +30 -0
- src/map_viz.py +83 -0
- svg/snazzy-image-01.svg +0 -0
.gitignore
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
*.jpg
|
2 |
-
*.csv
|
|
|
|
1 |
*.jpg
|
2 |
+
*.csv
|
3 |
+
__pycache__
|
app.py
CHANGED
@@ -3,16 +3,8 @@ import pandas as pd
|
|
3 |
import plotly.express as px
|
4 |
from datasets import load_dataset
|
5 |
import os
|
6 |
-
|
7 |
-
|
8 |
-
def bar_chart(counts_df):
|
9 |
-
fig = px.bar(counts_df, x = 'car', y = 'large_vehicle')
|
10 |
-
|
11 |
-
# fig_app_by_arc.update_layout(
|
12 |
-
# xaxis_title="Name",
|
13 |
-
# yaxis_title="",
|
14 |
-
# )
|
15 |
-
return fig
|
16 |
|
17 |
def daily_average(counts_df):
|
18 |
|
@@ -55,13 +47,15 @@ def main():
|
|
55 |
)
|
56 |
|
57 |
# Select Plot Option
|
58 |
-
st.sidebar.markdown("
|
59 |
-
checkbox_one = st.sidebar.checkbox('
|
60 |
-
checkbox_two = st.sidebar.checkbox('
|
61 |
|
62 |
if checkbox_one:
|
63 |
-
st.
|
64 |
-
|
|
|
|
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
main()
|
|
|
3 |
import plotly.express as px
|
4 |
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 |
|
9 |
def daily_average(counts_df):
|
10 |
|
|
|
47 |
)
|
48 |
|
49 |
# Select Plot Option
|
50 |
+
st.sidebar.markdown("Select Plots to show")
|
51 |
+
checkbox_one = st.sidebar.checkbox('Overall Traffic', value = True) # rename as necessary
|
52 |
+
checkbox_two = st.sidebar.checkbox('Traffic Map', value = True)
|
53 |
|
54 |
if checkbox_one:
|
55 |
+
st.plotly_chart(basic_chart(counts_df),use_container_width=True)
|
56 |
+
|
57 |
+
if checkbox_two:
|
58 |
+
st.pyplot(calling_map_viz(counts_df))
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
main()
|
env.yml
CHANGED
@@ -6,4 +6,5 @@ dependencies:
|
|
6 |
- datasets>=2.8.0
|
7 |
- pandas=1.5.3
|
8 |
- plotly=5.13.0
|
9 |
-
- streamlit=1.18.1
|
|
|
|
6 |
- datasets>=2.8.0
|
7 |
- pandas=1.5.3
|
8 |
- plotly=5.13.0
|
9 |
+
- streamlit=1.18.1
|
10 |
+
- svgpath2mpl=1.0.0
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
pandas
|
2 |
plotly
|
3 |
datasets
|
|
|
|
1 |
pandas
|
2 |
plotly
|
3 |
datasets
|
4 |
+
svgpath2mpl
|
src/basic_plot.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
from datasets import load_dataset
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
def basic_chart(counts_df):
|
9 |
+
# data processing
|
10 |
+
counts_df['traffic'] = counts_df['car'] + counts_df['motorcycle'] + counts_df['large_vehicle']
|
11 |
+
counts_df['datetime'] = pd.to_datetime(counts_df['date'] + ' ' + counts_df['time'])
|
12 |
+
counts_df['weekday'] = counts_df['datetime'].dt.strftime('%A')
|
13 |
+
counts_df['hour'] = counts_df['datetime'].dt.strftime('%H')
|
14 |
+
|
15 |
+
# plot types
|
16 |
+
plot = st.sidebar.selectbox('Choose Plot', options=['Day','Hour','Raw'])
|
17 |
+
|
18 |
+
# view types
|
19 |
+
view = st.sidebar.selectbox('Choose View', options=counts_df['view'].unique())
|
20 |
+
filtered_views = counts_df[counts_df['view'] == view]
|
21 |
+
|
22 |
+
# conditional views
|
23 |
+
if plot == 'Day':
|
24 |
+
fig = px.bar(filtered_views, x='weekday', y='traffic')
|
25 |
+
elif plot == 'Hour':
|
26 |
+
fig = px.bar(filtered_views, x='hour', y='traffic')
|
27 |
+
elif plot == 'Raw':
|
28 |
+
fig = px.bar(filtered_views, x='datetime', y='traffic')
|
29 |
+
|
30 |
+
return fig
|
src/map_viz.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import division, print_function
|
2 |
+
from six import StringIO
|
3 |
+
from svgpath2mpl import parse_path
|
4 |
+
from collections import defaultdict
|
5 |
+
import xml.etree.ElementTree as etree
|
6 |
+
import re
|
7 |
+
import matplotlib as mpl
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import numpy as np
|
10 |
+
import requests
|
11 |
+
import pandas as pd
|
12 |
+
|
13 |
+
|
14 |
+
def calling_map_viz(counts_df):
|
15 |
+
r = "svg/snazzy-image-01.svg"
|
16 |
+
tree = etree.parse(r)
|
17 |
+
root = tree.getroot()
|
18 |
+
path_elems = root.findall('.//{http://www.w3.org/2000/svg}path')
|
19 |
+
|
20 |
+
paths = [parse_path(elem.attrib['d']) for elem in path_elems]
|
21 |
+
facecolors = []
|
22 |
+
edgecolors = []
|
23 |
+
linewidths = []
|
24 |
+
for elem in path_elems:
|
25 |
+
facecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("fill", "none"))
|
26 |
+
edgecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke", "none"))
|
27 |
+
linewidths.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke-width", "none").replace("px", ""))
|
28 |
+
|
29 |
+
path_id = defaultdict(int)
|
30 |
+
for i, elem in enumerate(path_elems):
|
31 |
+
try:
|
32 |
+
#print(i, elem.attrib['id'])
|
33 |
+
path_id[elem.attrib['id']] = i
|
34 |
+
except:
|
35 |
+
continue
|
36 |
+
|
37 |
+
|
38 |
+
# counts_df = pd.read_csv("counts_dataset.csv")
|
39 |
+
counts_df['total'] = counts_df['car'] + counts_df['motorcycle'] + counts_df['large_vehicle']
|
40 |
+
count_max = counts_df['total'].max()
|
41 |
+
count_min = counts_df['total'].min()
|
42 |
+
last_date = counts_df.iloc[-1:,0].values[0]
|
43 |
+
last_time = counts_df.iloc[-1:,1].values[0]
|
44 |
+
|
45 |
+
|
46 |
+
count_dict = {"woodlands_to_sg" :counts_df.loc[counts_df['view'].str.contains(r'''Woodlands([a-zA-Z0-9_.+-]+)sg''') & (counts_df['date'] == last_date) & (counts_df['time'] == last_time), "total" ].sum(),
|
47 |
+
"woodlands_to_jh" :counts_df.loc[counts_df['view'].str.contains(r'''Woodlands([a-zA-Z0-9_.+-]+)jh''') & (counts_df['date'] == last_date) & (counts_df['time'] == last_time), "total" ].sum(),
|
48 |
+
"tuas_to_sg" :counts_df.loc[counts_df['view'].str.contains(r'''Tuas([a-zA-Z0-9_.+-]+)sg''') & (counts_df['date'] == last_date) & (counts_df['time'] == last_time), "total" ].sum(),
|
49 |
+
"tuas_to_jh" :counts_df.loc[counts_df['view'].str.contains(r'''Tuas([a-zA-Z0-9_.+-]+)jh''') & (counts_df['date'] == last_date) & (counts_df['time'] == last_time), "total" ].sum()
|
50 |
+
}
|
51 |
+
|
52 |
+
values = np.array([0., 0.5, 1.])
|
53 |
+
values = np.sort(np.array(values))
|
54 |
+
values = np.interp(values, (values.min(), values.max()), (0., 1.))
|
55 |
+
colors = ["#539f6b", "#ffc835", "#bf0000"]
|
56 |
+
cmap = mpl.colors.LinearSegmentedColormap.from_list("custom", list(zip(values, colors)))
|
57 |
+
|
58 |
+
norm = mpl.colors.Normalize(vmin=count_min, vmax=count_max)
|
59 |
+
|
60 |
+
hex_dict = {k: mpl.colors.to_hex(cmap(norm(v))) for k, v in count_dict.items()}
|
61 |
+
color_dict = defaultdict(str)
|
62 |
+
|
63 |
+
for k, i in path_id.items():
|
64 |
+
color_dict[i] = hex_dict[k]
|
65 |
+
|
66 |
+
for k, i in color_dict.items():
|
67 |
+
#print(k,i)
|
68 |
+
facecolors[k] = i
|
69 |
+
|
70 |
+
collection = mpl.collections.PathCollection(paths,
|
71 |
+
edgecolors=edgecolors,
|
72 |
+
linewidths=[int(i)/100 for i in linewidths if i != 'none'],
|
73 |
+
facecolors=[i.strip() for i in facecolors])
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
fig = plt.figure(figsize=(10,10))
|
78 |
+
ax = fig.add_subplot(111)
|
79 |
+
collection.set_transform(ax.transData)
|
80 |
+
ax.add_artist(collection)
|
81 |
+
ax.set_xlim([100, 1900])
|
82 |
+
ax.set_ylim([1800, 0])
|
83 |
+
return fig
|
svg/snazzy-image-01.svg
ADDED