Spaces:
Sleeping
Sleeping
tappyness1 commited on
Commit ·
79daedf
1
Parent(s): 3d19079
predictive app
Browse files- app.py +70 -43
- notebooks/Compiled_final.ipynb +0 -0
- poc.ipynb → notebooks/poc.ipynb +0 -0
- notebooks/pred_final.ipynb +321 -0
- src/pred_plot.py +203 -0
app.py
CHANGED
|
@@ -8,6 +8,8 @@ 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 |
|
| 12 |
def fetch_data():
|
| 13 |
# comment out for local testing, but be sure to include after testing
|
|
@@ -23,6 +25,7 @@ def fetch_data():
|
|
| 23 |
|
| 24 |
def main():
|
| 25 |
counts_df = fetch_data()
|
|
|
|
| 26 |
counts_df = remove_previous_view(counts_df)
|
| 27 |
counts_df = merge_volumes(counts_df)
|
| 28 |
# st.set_page_config(layout="wide")
|
|
@@ -34,56 +37,80 @@ def main():
|
|
| 34 |
</style> """,
|
| 35 |
unsafe_allow_html=True
|
| 36 |
)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Select Plot Option
|
| 39 |
-
st.sidebar.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
|
| 71 |
-
"18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
|
| 72 |
-
]
|
| 73 |
-
hour_choice = st.selectbox(
|
| 74 |
-
"Choose Hour",
|
| 75 |
options= hours,
|
| 76 |
-
key = "
|
| 77 |
)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
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, convert_date, gen_fig
|
| 12 |
+
from datetime import date
|
| 13 |
|
| 14 |
def fetch_data():
|
| 15 |
# comment out for local testing, but be sure to include after testing
|
|
|
|
| 25 |
|
| 26 |
def main():
|
| 27 |
counts_df = fetch_data()
|
| 28 |
+
pred_df = counts_df.copy()
|
| 29 |
counts_df = remove_previous_view(counts_df)
|
| 30 |
counts_df = merge_volumes(counts_df)
|
| 31 |
# st.set_page_config(layout="wide")
|
|
|
|
| 37 |
</style> """,
|
| 38 |
unsafe_allow_html=True
|
| 39 |
)
|
| 40 |
+
hours = [
|
| 41 |
+
"00:00", "01:00", "02:00", "03:00", "04:00", "05:00",
|
| 42 |
+
"06:00", "07:00", "08:00", "09:00", "10:00", "11:00",
|
| 43 |
+
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
|
| 44 |
+
"18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
|
| 45 |
+
]
|
| 46 |
# Select Plot Option
|
| 47 |
+
plot_type = st.sidebar.selectbox("Choose Plot Type", options = ['Historical', 'Predictive'])
|
| 48 |
+
if plot_type == 'Historical':
|
| 49 |
+
st.sidebar.markdown("Select Plots to show")
|
| 50 |
+
checkbox_one = st.sidebar.checkbox('Overall Traffic', value = True) # rename as necessary
|
| 51 |
+
checkbox_two = st.sidebar.checkbox('Traffic Map', value = True)
|
| 52 |
+
checkbox_three = st.sidebar.checkbox('Heat Map', value = True)
|
| 53 |
+
view_options = list(counts_df["view"].unique())
|
| 54 |
+
view_options.append('All')
|
| 55 |
+
view = st.sidebar.selectbox("Choose View", options=view_options, index = view_options.index("Woodlands - to Johor"))
|
| 56 |
+
if view != 'All':
|
| 57 |
+
st.header(f"Showing Traffic for {view}")
|
| 58 |
+
counts_df = counts_df[counts_df['view'] == view]
|
| 59 |
+
|
| 60 |
+
if checkbox_one:
|
| 61 |
+
st.subheader("Overall Traffic")
|
| 62 |
+
plot = st.selectbox("Choose Plot", options=["Day", "Hour", "Raw"], index = 0)
|
| 63 |
+
st.plotly_chart(basic_chart(counts_df, plot = plot),use_container_width=True)
|
| 64 |
+
|
| 65 |
+
if checkbox_two:
|
| 66 |
+
st.subheader("Traffic Map")
|
| 67 |
+
st.pyplot(calling_map_viz(counts_df))
|
| 68 |
+
|
| 69 |
+
if checkbox_three:
|
| 70 |
|
| 71 |
+
heatmap = HeatMap(counts_df)
|
| 72 |
+
|
| 73 |
+
# st.header("Mean Vehicle Count by Day of Week")
|
| 74 |
+
# st.plotly_chart(heatmap.vehicle_count_bar())
|
| 75 |
+
st.subheader("Heatmap")
|
| 76 |
+
st.plotly_chart(heatmap.heatmap())
|
| 77 |
|
| 78 |
+
hour_choice = st.selectbox(
|
| 79 |
+
"Choose Hour",
|
| 80 |
+
options= hours,
|
| 81 |
+
key = "hour", index = hours.index("08:00")
|
| 82 |
+
)
|
| 83 |
+
st.subheader(f"Traffic Volume of Each Day at {hour_choice}")
|
| 84 |
+
st.plotly_chart(heatmap.update_hour_bar_chart(hour_choice))
|
| 85 |
+
days = ["Monday", "Tuesday", "Wednesday",
|
| 86 |
+
"Thursday", "Friday","Saturday", "Sunday"]
|
| 87 |
+
day_choice = st.selectbox("Choose Day of the Week", options = days, key = "day", index = days.index("Saturday"))
|
| 88 |
+
st.subheader(f"Traffic Volume of Each Hour on {day_choice}")
|
| 89 |
+
st.plotly_chart(heatmap.update_day_bar_chart(day_choice))
|
| 90 |
|
| 91 |
+
else:
|
| 92 |
|
| 93 |
+
figs = gen_fig()
|
| 94 |
+
today = get_today()
|
| 95 |
+
final_table = prep_data_pred_plot(pred_df)
|
| 96 |
+
x_train, _, y_train, _ = data_split(final_table)
|
| 97 |
+
clf = train_model(x_train, y_train)
|
| 98 |
+
pred_hour_choice = st.selectbox(
|
| 99 |
+
"Choose Your Planned Hour",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
options= hours,
|
| 101 |
+
key = "pred_hour", index = hours.index("08:00")
|
| 102 |
)
|
| 103 |
+
pred_view_choice = st.selectbox(
|
| 104 |
+
"Choose View",
|
| 105 |
+
options= ['Johor-Tuas','Johor-Woodlands', 'Tuas-Johor', 'Woodlands-Johor'],
|
| 106 |
+
key = "pred_view"
|
| 107 |
+
)
|
| 108 |
+
d = st.date_input(
|
| 109 |
+
"Choose Your Planned Date",
|
| 110 |
+
date(today[0],today[1], today[2]))
|
| 111 |
+
|
| 112 |
+
starter_variables = [x_train, str(d), pred_hour_choice, pred_view_choice]
|
| 113 |
+
st.plotly_chart(predicted_figure(clf, starter_variables, figs))
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
main()
|
notebooks/Compiled_final.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
poc.ipynb → notebooks/poc.ipynb
RENAMED
|
File without changes
|
notebooks/pred_final.ipynb
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 6,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stderr",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"Using custom data configuration tappyness1--causion-f0c7d8cb97d796fc\n",
|
| 13 |
+
"Found cached dataset parquet (C:/Users/neoce/.cache/huggingface/datasets/tappyness1___parquet/tappyness1--causion-f0c7d8cb97d796fc/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec)\n",
|
| 14 |
+
"100%|██████████| 1/1 [00:00<00:00, 144.77it/s]\n"
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
],
|
| 18 |
+
"source": [
|
| 19 |
+
"from datasets import load_dataset\n",
|
| 20 |
+
"import pandas as pd\n",
|
| 21 |
+
"import os\n",
|
| 22 |
+
"import yaml\n",
|
| 23 |
+
"import sys\n",
|
| 24 |
+
"sys.path.append(\"..\")\n",
|
| 25 |
+
"from src.pred_plot import prep_data_pred_plot, data_split\n",
|
| 26 |
+
"%load_ext autoreload\n",
|
| 27 |
+
"%autoreload 2\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"token_file = open(\"../token_secret.yaml\")\n",
|
| 30 |
+
"token_obj = yaml.load(token_file, Loader=yaml.FullLoader)\n",
|
| 31 |
+
"dataset = load_dataset(\"tappyness1/causion\", use_auth_token=token_obj['TOKEN'])\n",
|
| 32 |
+
"counts_df = pd.DataFrame(dataset['train'])"
|
| 33 |
+
]
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"cell_type": "code",
|
| 37 |
+
"execution_count": 5,
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [
|
| 40 |
+
{
|
| 41 |
+
"data": {
|
| 42 |
+
"text/html": [
|
| 43 |
+
"<div>\n",
|
| 44 |
+
"<style scoped>\n",
|
| 45 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 46 |
+
" vertical-align: middle;\n",
|
| 47 |
+
" }\n",
|
| 48 |
+
"\n",
|
| 49 |
+
" .dataframe tbody tr th {\n",
|
| 50 |
+
" vertical-align: top;\n",
|
| 51 |
+
" }\n",
|
| 52 |
+
"\n",
|
| 53 |
+
" .dataframe thead th {\n",
|
| 54 |
+
" text-align: right;\n",
|
| 55 |
+
" }\n",
|
| 56 |
+
"</style>\n",
|
| 57 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 58 |
+
" <thead>\n",
|
| 59 |
+
" <tr style=\"text-align: right;\">\n",
|
| 60 |
+
" <th></th>\n",
|
| 61 |
+
" <th>date</th>\n",
|
| 62 |
+
" <th>time</th>\n",
|
| 63 |
+
" <th>view</th>\n",
|
| 64 |
+
" <th>car</th>\n",
|
| 65 |
+
" <th>motorcycle</th>\n",
|
| 66 |
+
" <th>large_vehicle</th>\n",
|
| 67 |
+
" </tr>\n",
|
| 68 |
+
" </thead>\n",
|
| 69 |
+
" <tbody>\n",
|
| 70 |
+
" <tr>\n",
|
| 71 |
+
" <th>0</th>\n",
|
| 72 |
+
" <td>2023-02-14</td>\n",
|
| 73 |
+
" <td>22:36:03</td>\n",
|
| 74 |
+
" <td>View_from_Second_Link_at_Tuas</td>\n",
|
| 75 |
+
" <td>0</td>\n",
|
| 76 |
+
" <td>0</td>\n",
|
| 77 |
+
" <td>1</td>\n",
|
| 78 |
+
" </tr>\n",
|
| 79 |
+
" <tr>\n",
|
| 80 |
+
" <th>1</th>\n",
|
| 81 |
+
" <td>2023-02-14</td>\n",
|
| 82 |
+
" <td>22:36:03</td>\n",
|
| 83 |
+
" <td>View_from_Tuas_Checkpoint</td>\n",
|
| 84 |
+
" <td>2</td>\n",
|
| 85 |
+
" <td>0</td>\n",
|
| 86 |
+
" <td>0</td>\n",
|
| 87 |
+
" </tr>\n",
|
| 88 |
+
" <tr>\n",
|
| 89 |
+
" <th>2</th>\n",
|
| 90 |
+
" <td>2023-02-14</td>\n",
|
| 91 |
+
" <td>22:36:03</td>\n",
|
| 92 |
+
" <td>View_from_Woodlands_Causeway_Towards_Johor</td>\n",
|
| 93 |
+
" <td>2</td>\n",
|
| 94 |
+
" <td>0</td>\n",
|
| 95 |
+
" <td>0</td>\n",
|
| 96 |
+
" </tr>\n",
|
| 97 |
+
" <tr>\n",
|
| 98 |
+
" <th>3</th>\n",
|
| 99 |
+
" <td>2023-02-14</td>\n",
|
| 100 |
+
" <td>22:36:03</td>\n",
|
| 101 |
+
" <td>View_from_Woodlands_Checkpoint_Towards_BKE</td>\n",
|
| 102 |
+
" <td>3</td>\n",
|
| 103 |
+
" <td>0</td>\n",
|
| 104 |
+
" <td>1</td>\n",
|
| 105 |
+
" </tr>\n",
|
| 106 |
+
" <tr>\n",
|
| 107 |
+
" <th>4</th>\n",
|
| 108 |
+
" <td>2023-02-14</td>\n",
|
| 109 |
+
" <td>23:14:34</td>\n",
|
| 110 |
+
" <td>View_from_Second_Link_at_Tuas</td>\n",
|
| 111 |
+
" <td>0</td>\n",
|
| 112 |
+
" <td>0</td>\n",
|
| 113 |
+
" <td>6</td>\n",
|
| 114 |
+
" </tr>\n",
|
| 115 |
+
" </tbody>\n",
|
| 116 |
+
"</table>\n",
|
| 117 |
+
"</div>"
|
| 118 |
+
],
|
| 119 |
+
"text/plain": [
|
| 120 |
+
" date time view car \\\n",
|
| 121 |
+
"0 2023-02-14 22:36:03 View_from_Second_Link_at_Tuas 0 \n",
|
| 122 |
+
"1 2023-02-14 22:36:03 View_from_Tuas_Checkpoint 2 \n",
|
| 123 |
+
"2 2023-02-14 22:36:03 View_from_Woodlands_Causeway_Towards_Johor 2 \n",
|
| 124 |
+
"3 2023-02-14 22:36:03 View_from_Woodlands_Checkpoint_Towards_BKE 3 \n",
|
| 125 |
+
"4 2023-02-14 23:14:34 View_from_Second_Link_at_Tuas 0 \n",
|
| 126 |
+
"\n",
|
| 127 |
+
" motorcycle large_vehicle \n",
|
| 128 |
+
"0 0 1 \n",
|
| 129 |
+
"1 0 0 \n",
|
| 130 |
+
"2 0 0 \n",
|
| 131 |
+
"3 0 1 \n",
|
| 132 |
+
"4 0 6 "
|
| 133 |
+
]
|
| 134 |
+
},
|
| 135 |
+
"execution_count": 5,
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"output_type": "execute_result"
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"source": [
|
| 141 |
+
"counts_df.head()"
|
| 142 |
+
]
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"cell_type": "code",
|
| 146 |
+
"execution_count": 23,
|
| 147 |
+
"metadata": {},
|
| 148 |
+
"outputs": [
|
| 149 |
+
{
|
| 150 |
+
"name": "stderr",
|
| 151 |
+
"output_type": "stream",
|
| 152 |
+
"text": [
|
| 153 |
+
"g:\\My Drive\\DataScience\\OMSA GaTech\\08_CSE6242\\Project\\Org_Space\\Causion\\notebooks\\..\\src\\pred_plot.py:117: SettingWithCopyWarning:\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
|
| 157 |
+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
|
| 158 |
+
"\n",
|
| 159 |
+
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"g:\\My Drive\\DataScience\\OMSA GaTech\\08_CSE6242\\Project\\Org_Space\\Causion\\notebooks\\..\\src\\pred_plot.py:118: SettingWithCopyWarning:\n",
|
| 162 |
+
"\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
|
| 165 |
+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
|
| 166 |
+
"\n",
|
| 167 |
+
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
|
| 168 |
+
"\n",
|
| 169 |
+
"g:\\My Drive\\DataScience\\OMSA GaTech\\08_CSE6242\\Project\\Org_Space\\Causion\\notebooks\\..\\src\\pred_plot.py:120: FutureWarning:\n",
|
| 170 |
+
"\n",
|
| 171 |
+
"The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.\n",
|
| 172 |
+
"\n"
|
| 173 |
+
]
|
| 174 |
+
}
|
| 175 |
+
],
|
| 176 |
+
"source": [
|
| 177 |
+
"from src.pred_plot import prep_data_pred_plot, data_split, train_model, predicted_figure, get_today, convert_date, gen_fig\n",
|
| 178 |
+
"from datetime import date, datetime, timedelta\n",
|
| 179 |
+
"from jupyter_dash import JupyterDash\n",
|
| 180 |
+
"import dash\n",
|
| 181 |
+
"from dash import Dash, html, dcc, Input, Output\n",
|
| 182 |
+
"final_table = prep_data_pred_plot(counts_df)\n",
|
| 183 |
+
"x_train, x_test, y_train, y_test = data_split(final_table)\n",
|
| 184 |
+
"clf = train_model(x_train, y_train)"
|
| 185 |
+
]
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"cell_type": "code",
|
| 189 |
+
"execution_count": 30,
|
| 190 |
+
"metadata": {},
|
| 191 |
+
"outputs": [],
|
| 192 |
+
"source": [
|
| 193 |
+
"starter_variables = [x_train, str(date.today()), \"07:00\", \"Tuas-Johor\"]\n",
|
| 194 |
+
"figs = gen_fig()\n",
|
| 195 |
+
"fig = predicted_figure(clf, starter_variables, figs)\n",
|
| 196 |
+
"today = get_today()"
|
| 197 |
+
]
|
| 198 |
+
},
|
| 199 |
+
{
|
| 200 |
+
"cell_type": "code",
|
| 201 |
+
"execution_count": 33,
|
| 202 |
+
"metadata": {},
|
| 203 |
+
"outputs": [],
|
| 204 |
+
"source": [
|
| 205 |
+
"app = JupyterDash(__name__)\n",
|
| 206 |
+
"\n",
|
| 207 |
+
"app.title = 'CSE6242 Dashboard'\n",
|
| 208 |
+
"app.layout = html.Div([\n",
|
| 209 |
+
" html.Div([\n",
|
| 210 |
+
" dcc.DatePickerSingle(\n",
|
| 211 |
+
" id='my_date_picker_single',\n",
|
| 212 |
+
" min_date_allowed=date(2023, 4, 11),\n",
|
| 213 |
+
" max_date_allowed=date(2024, 4, 11),\n",
|
| 214 |
+
" initial_visible_month=date(today[0],today[1], today[2]),\n",
|
| 215 |
+
" date=date(today[0],today[1], today[2])\n",
|
| 216 |
+
" ),\n",
|
| 217 |
+
" html.Div(id='output-container-date-picker-single')],\n",
|
| 218 |
+
" style={'width':'20%','height':'0px', 'padding-left':'2%',\n",
|
| 219 |
+
" 'display':'inline-block'}),\n",
|
| 220 |
+
" html.Div([dcc.Dropdown(id='hours_dropdown_id',\n",
|
| 221 |
+
" options=['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', \n",
|
| 222 |
+
" '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00','19:00',\n",
|
| 223 |
+
" '20:00', '21:00', '22:00', '23:00'],\n",
|
| 224 |
+
" value='07:00', clearable=False),html.Label(\"Estimated Leave Time\")],\n",
|
| 225 |
+
" style={'width':'20%','height':'60px', 'padding-left':'2%',\n",
|
| 226 |
+
" 'display':'inline-block'}),\n",
|
| 227 |
+
" html.Div([dcc.Dropdown(id='direction_id',\n",
|
| 228 |
+
" options=['Johor-Tuas','Johor-Woodlands',\n",
|
| 229 |
+
" 'Tuas-Johor', 'Woodlands-Johor'],\n",
|
| 230 |
+
" value=\"Tuas-Johor\", clearable=False),html.Label(\"Direction\")],\n",
|
| 231 |
+
" style={'width':'20%','height':'60px', 'padding-left':'2%',\n",
|
| 232 |
+
" 'display':'inline-block'}),\n",
|
| 233 |
+
" html.Div(dcc.Graph(id='final_output', figure=fig))])\n",
|
| 234 |
+
"\n",
|
| 235 |
+
"\n",
|
| 236 |
+
"@app.callback(Output('output-container-date-picker-single', 'children'),\n",
|
| 237 |
+
" Input('my_date_picker_single', 'date'))\n",
|
| 238 |
+
"def update_output(date_value):\n",
|
| 239 |
+
" string_prefix = 'Travel Day: '\n",
|
| 240 |
+
" if date_value is not None:\n",
|
| 241 |
+
" date_object = date.fromisoformat(date_value)\n",
|
| 242 |
+
" date_string = convert_date(date_value)\n",
|
| 243 |
+
" return string_prefix + date_string\n",
|
| 244 |
+
" \n",
|
| 245 |
+
"@app.callback(Output('final_output', \"figure\"),\n",
|
| 246 |
+
" Input('my_date_picker_single', 'date'),\n",
|
| 247 |
+
" Input('hours_dropdown_id', 'value'),\n",
|
| 248 |
+
" Input('direction_id', 'value'))\n",
|
| 249 |
+
"def update_final_output_hour(my_date_picker_single, hours_dropdown_id, direction_id):\n",
|
| 250 |
+
" starter_variables[0] = x_train\n",
|
| 251 |
+
" starter_variables[1] = str(my_date_picker_single)\n",
|
| 252 |
+
" starter_variables[2] = str(hours_dropdown_id)\n",
|
| 253 |
+
" starter_variables[3] = str(direction_id)\n",
|
| 254 |
+
" fig = predicted_figure(clf, starter_variables, figs)\n",
|
| 255 |
+
" return fig"
|
| 256 |
+
]
|
| 257 |
+
},
|
| 258 |
+
{
|
| 259 |
+
"cell_type": "code",
|
| 260 |
+
"execution_count": 34,
|
| 261 |
+
"metadata": {},
|
| 262 |
+
"outputs": [
|
| 263 |
+
{
|
| 264 |
+
"name": "stdout",
|
| 265 |
+
"output_type": "stream",
|
| 266 |
+
"text": [
|
| 267 |
+
"Dash is running on http://127.0.0.1:8050/\n",
|
| 268 |
+
"\n"
|
| 269 |
+
]
|
| 270 |
+
}
|
| 271 |
+
],
|
| 272 |
+
"source": [
|
| 273 |
+
"app.run_server(mode='inline')"
|
| 274 |
+
]
|
| 275 |
+
},
|
| 276 |
+
{
|
| 277 |
+
"cell_type": "code",
|
| 278 |
+
"execution_count": 2,
|
| 279 |
+
"metadata": {},
|
| 280 |
+
"outputs": [
|
| 281 |
+
{
|
| 282 |
+
"data": {
|
| 283 |
+
"text/plain": [
|
| 284 |
+
"'2023-04-14'"
|
| 285 |
+
]
|
| 286 |
+
},
|
| 287 |
+
"execution_count": 2,
|
| 288 |
+
"metadata": {},
|
| 289 |
+
"output_type": "execute_result"
|
| 290 |
+
}
|
| 291 |
+
],
|
| 292 |
+
"source": [
|
| 293 |
+
"from datetime import date\n",
|
| 294 |
+
"\n",
|
| 295 |
+
"str(date.today())"
|
| 296 |
+
]
|
| 297 |
+
}
|
| 298 |
+
],
|
| 299 |
+
"metadata": {
|
| 300 |
+
"kernelspec": {
|
| 301 |
+
"display_name": "causion",
|
| 302 |
+
"language": "python",
|
| 303 |
+
"name": "python3"
|
| 304 |
+
},
|
| 305 |
+
"language_info": {
|
| 306 |
+
"codemirror_mode": {
|
| 307 |
+
"name": "ipython",
|
| 308 |
+
"version": 3
|
| 309 |
+
},
|
| 310 |
+
"file_extension": ".py",
|
| 311 |
+
"mimetype": "text/x-python",
|
| 312 |
+
"name": "python",
|
| 313 |
+
"nbconvert_exporter": "python",
|
| 314 |
+
"pygments_lexer": "ipython3",
|
| 315 |
+
"version": "3.8.16"
|
| 316 |
+
},
|
| 317 |
+
"orig_nbformat": 4
|
| 318 |
+
},
|
| 319 |
+
"nbformat": 4,
|
| 320 |
+
"nbformat_minor": 2
|
| 321 |
+
}
|
src/pred_plot.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import date, datetime, timedelta
|
| 2 |
+
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:
|
| 9 |
+
time_1 = str(int(t.hour)+1)
|
| 10 |
+
if len(time_1) == 1:
|
| 11 |
+
return "0"+time_1+":00"
|
| 12 |
+
else:
|
| 13 |
+
return str(time_1)+":00"
|
| 14 |
+
else:
|
| 15 |
+
if len(str(t.hour)) == 1:
|
| 16 |
+
return "0"+str(t.hour)+":00"
|
| 17 |
+
else:
|
| 18 |
+
return str(t.hour)+":00"
|
| 19 |
+
|
| 20 |
+
def peak_hours(t):
|
| 21 |
+
peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
|
| 22 |
+
if t in peak:
|
| 23 |
+
return 1
|
| 24 |
+
else:
|
| 25 |
+
return 0
|
| 26 |
+
|
| 27 |
+
def weekend(w):
|
| 28 |
+
end = ['Saturday', 'Sunday']
|
| 29 |
+
if w in end:
|
| 30 |
+
return 1
|
| 31 |
+
else:
|
| 32 |
+
return 0
|
| 33 |
+
|
| 34 |
+
def vehicle_cat(v):
|
| 35 |
+
if v >= 0 and v < 20:
|
| 36 |
+
return 0
|
| 37 |
+
elif v >= 20 and v < 50:
|
| 38 |
+
return 1
|
| 39 |
+
elif v >= 50 and v < 80:
|
| 40 |
+
return 2
|
| 41 |
+
elif v >= 80 and v < 120:
|
| 42 |
+
return 3
|
| 43 |
+
else:
|
| 44 |
+
return 4
|
| 45 |
+
|
| 46 |
+
def data_split(final_table):
|
| 47 |
+
X = final_table.loc[:,['day', 'hour','view']]
|
| 48 |
+
Y = final_table.loc[:,'cat']
|
| 49 |
+
|
| 50 |
+
X = pd.get_dummies(X)
|
| 51 |
+
X.loc[:,['peak', 'weekend']] = final_table.loc[:,['peak', 'weekend']]
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7,
|
| 56 |
+
test_size=0.3,
|
| 57 |
+
shuffle=True, random_state=13)
|
| 58 |
+
|
| 59 |
+
return x_train, x_test, y_train, y_test
|
| 60 |
+
|
| 61 |
+
def convert_date(date):
|
| 62 |
+
return datetime.strptime(date, "%Y-%m-%d").strftime('%A')
|
| 63 |
+
|
| 64 |
+
def create_row(x_train, date_d, hour, view):
|
| 65 |
+
if date_d is None:
|
| 66 |
+
date_d = "2023-04-11"
|
| 67 |
+
if hour is None:
|
| 68 |
+
hour = "09:00"
|
| 69 |
+
if view is None:
|
| 70 |
+
view = "Johor-Tuas"
|
| 71 |
+
|
| 72 |
+
features = x_train.columns
|
| 73 |
+
d_dict = {}
|
| 74 |
+
day = datetime.strptime(date_d, "%Y-%m-%d").strftime('%A')
|
| 75 |
+
hour = str(hour)
|
| 76 |
+
view = str(view)
|
| 77 |
+
col_day = "day_" + day
|
| 78 |
+
col_hour = 'hour_'+ hour
|
| 79 |
+
col_view = 'view_'+view
|
| 80 |
+
|
| 81 |
+
for i in features:
|
| 82 |
+
if i == col_day or i == col_hour or i == col_view:
|
| 83 |
+
d_dict[i] = [1]
|
| 84 |
+
else:
|
| 85 |
+
d_dict[i] = [0]
|
| 86 |
+
end = ['Saturday', 'Sunday']
|
| 87 |
+
peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
|
| 88 |
+
|
| 89 |
+
if day in end:
|
| 90 |
+
d_dict['weekend'] = 1
|
| 91 |
+
if hour in peak:
|
| 92 |
+
d_dict['peak'] = 1
|
| 93 |
+
result = pd.DataFrame.from_dict(d_dict, orient='columns')
|
| 94 |
+
for i in features:
|
| 95 |
+
result[i] = result[i].astype('category')
|
| 96 |
+
return result
|
| 97 |
+
|
| 98 |
+
def prep_data_pred_plot(df):
|
| 99 |
+
df = df.sort_values(by=['date']).reset_index(drop=True)
|
| 100 |
+
df['date'] = pd.to_datetime(df['date'], format = "%Y-%m-%d")
|
| 101 |
+
df['day'] = df['date'].dt.day_name()
|
| 102 |
+
df.drop(columns=['motorcycle'], axis=1, inplace=True)
|
| 103 |
+
df['vehicle'] = df['car'] + df['large_vehicle']
|
| 104 |
+
|
| 105 |
+
transfer = {"View_from_Second_Link_at_Tuas_to_sg": 'Johor-Tuas',
|
| 106 |
+
"View_from_Second_Link_at_Tuas_to_jh": 'Tuas-Johor',
|
| 107 |
+
"View_from_Tuas_Checkpoint_to_sg": 'Johor-Tuas',
|
| 108 |
+
"View_from_Tuas_Checkpoint_to_jh": 'Tuas-Johor',
|
| 109 |
+
"View_from_Woodlands_Causeway_Towards_Johor_to_sg": 'Johor-Woodlands',
|
| 110 |
+
"View_from_Woodlands_Causeway_Towards_Johor_to_jh": 'Woodlands-Johor',
|
| 111 |
+
"View_from_Woodlands_Checkpoint_Towards_BKE_to_sg": 'Johor-Woodlands',
|
| 112 |
+
"View_from_Woodlands_Checkpoint_Towards_BKE_to_jh": 'Woodlands-Johor'}
|
| 113 |
+
|
| 114 |
+
new_table = df.replace({'view':transfer})
|
| 115 |
+
options = ['Johor-Woodlands','Woodlands-Johor','Johor-Tuas','Tuas-Johor']
|
| 116 |
+
final_df = new_table[new_table['view'].isin(options)]
|
| 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']).sum().reset_index().loc[:,['day', 'hour','view', 'vehicle']]
|
| 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)
|
| 125 |
+
final_table.loc[:,'weekend'] = final_table.loc[:,'weekend'].astype('category')
|
| 126 |
+
final_table.loc[:,'cat'] = final_table.loc[:,'vehicle'].apply(vehicle_cat)
|
| 127 |
+
final_table.loc[:,'cat'] = final_table.loc[:,'cat'].astype('category')
|
| 128 |
+
|
| 129 |
+
return final_table
|
| 130 |
+
|
| 131 |
+
def gen_fig():
|
| 132 |
+
figs = []
|
| 133 |
+
|
| 134 |
+
for i in range(5):
|
| 135 |
+
midway = [15, 40, 70, 110, 150]
|
| 136 |
+
cat = ['No Traffic', 'Minimal Traffic', 'Mild Traffic', 'Moderate Traffic', 'Peak Traffic']
|
| 137 |
+
|
| 138 |
+
figure = go.Figure(go.Indicator(
|
| 139 |
+
mode = "gauge",
|
| 140 |
+
value = midway[i],
|
| 141 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 142 |
+
title = {'text': cat[i], 'font': {'size': 24}},
|
| 143 |
+
gauge = {
|
| 144 |
+
'axis': {'range': [None, 156], 'tickwidth': 1, 'tickcolor': "darkblue"},
|
| 145 |
+
'bar': {'color': "blue"},
|
| 146 |
+
'bgcolor': "white",
|
| 147 |
+
'borderwidth': 2,
|
| 148 |
+
'bordercolor': "gray",
|
| 149 |
+
'steps': [
|
| 150 |
+
{'range': [0, 19], 'color': 'darkgreen'},
|
| 151 |
+
{'range': [20, 49], 'color': 'green'},
|
| 152 |
+
{'range': [50, 79], 'color': 'yellow'},
|
| 153 |
+
{'range': [80, 119], 'color': 'orange'},
|
| 154 |
+
{'range': [120, 160], 'color': 'red'}],
|
| 155 |
+
'threshold': {
|
| 156 |
+
'line': {'color': "red", 'width': 4},
|
| 157 |
+
'thickness': 0.75,
|
| 158 |
+
'value': 490}}))
|
| 159 |
+
|
| 160 |
+
figure.update_layout(paper_bgcolor = "lavender", font = {'color': "darkblue", 'family': "Arial"})
|
| 161 |
+
|
| 162 |
+
figs.append(figure)
|
| 163 |
+
|
| 164 |
+
return figs
|
| 165 |
+
|
| 166 |
+
def predicted_figure(clf, x, figs):
|
| 167 |
+
|
| 168 |
+
result = create_row(x[0], x[1], x[2], x[3])
|
| 169 |
+
|
| 170 |
+
pred_val = clf.predict(result)[0]
|
| 171 |
+
|
| 172 |
+
return figs[pred_val]
|
| 173 |
+
|
| 174 |
+
def get_today():
|
| 175 |
+
t = str(date.today()).split('-')
|
| 176 |
+
today = []
|
| 177 |
+
|
| 178 |
+
for i in t:
|
| 179 |
+
if t[0] =='0':
|
| 180 |
+
today.append(int(t[1:]))
|
| 181 |
+
else:
|
| 182 |
+
today.append(int(i))
|
| 183 |
+
return today
|
| 184 |
+
|
| 185 |
+
def update_output(date_value):
|
| 186 |
+
string_prefix = 'Travel Day: '
|
| 187 |
+
if date_value is not None:
|
| 188 |
+
date_string = convert_date(date_value)
|
| 189 |
+
return string_prefix + date_string
|
| 190 |
+
|
| 191 |
+
def update_final_output_hour(starter_variables, my_date_picker_single, hours_dropdown_id, direction_id):
|
| 192 |
+
# starter_variables = [clf, str(date.today()), "07:00", "Tuas-Johor"]
|
| 193 |
+
starter_variables[1] = str(my_date_picker_single)
|
| 194 |
+
starter_variables[2] = str(hours_dropdown_id)
|
| 195 |
+
starter_variables[3] = str(direction_id)
|
| 196 |
+
fig = predicted_figure(starter_variables)
|
| 197 |
+
return fig
|
| 198 |
+
|
| 199 |
+
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
|