File size: 7,235 Bytes
79daedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import date, datetime, timedelta
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import pandas as pd
import plotly.graph_objects as go

def hour_rounder(t):
    if int(t.minute)>= 30:
        time_1 = str(int(t.hour)+1)
        if len(time_1) == 1:
            return "0"+time_1+":00"
        else:
            return str(time_1)+":00"
    else:
        if len(str(t.hour)) == 1:
            return "0"+str(t.hour)+":00"
        else:
            return str(t.hour)+":00"
        
def peak_hours(t):
    peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
    if t in peak:
        return 1
    else:
        return 0
    
def weekend(w):
    end = ['Saturday', 'Sunday']
    if w in end:
        return 1
    else:
        return 0
    
def vehicle_cat(v):
    if v >= 0 and v < 20:
        return 0
    elif v >= 20 and v < 50:
        return 1
    elif v >= 50 and v < 80:
        return 2
    elif v >= 80 and v < 120:
        return 3
    else:
        return 4 
    
def data_split(final_table):
    X = final_table.loc[:,['day', 'hour','view']]
    Y = final_table.loc[:,'cat']

    X = pd.get_dummies(X)
    X.loc[:,['peak', 'weekend']] = final_table.loc[:,['peak', 'weekend']]



    x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7,
                                                                test_size=0.3,
                                                                shuffle=True, random_state=13)
    
    return x_train, x_test, y_train, y_test 
        
def convert_date(date):
    return datetime.strptime(date, "%Y-%m-%d").strftime('%A') 

def create_row(x_train, date_d, hour, view):
    if date_d is None:
        date_d = "2023-04-11"
    if hour is None:
        hour = "09:00"
    if view is None:
        view = "Johor-Tuas"
    
    features = x_train.columns
    d_dict = {}
    day = datetime.strptime(date_d, "%Y-%m-%d").strftime('%A')
    hour = str(hour)
    view = str(view)
    col_day = "day_" + day
    col_hour = 'hour_'+ hour
    col_view = 'view_'+view
    
    for i in features:
        if i == col_day or i == col_hour or i == col_view:
            d_dict[i] = [1]
        else:
            d_dict[i] = [0]
    end = ['Saturday', 'Sunday']
    peak = ['07:00', "08:00", '09:00', "17:00", "18:00", "19:00"]
    
    if day in end:
        d_dict['weekend'] = 1
    if hour in peak:
        d_dict['peak'] = 1   
    result = pd.DataFrame.from_dict(d_dict, orient='columns')
    for i in features:
        result[i] = result[i].astype('category')
    return result

def prep_data_pred_plot(df):
    df = df.sort_values(by=['date']).reset_index(drop=True)
    df['date'] = pd.to_datetime(df['date'], format = "%Y-%m-%d")
    df['day'] = df['date'].dt.day_name()
    df.drop(columns=['motorcycle'], axis=1, inplace=True)
    df['vehicle'] = df['car'] + df['large_vehicle']

    transfer = {"View_from_Second_Link_at_Tuas_to_sg": 'Johor-Tuas', 
            "View_from_Second_Link_at_Tuas_to_jh": 'Tuas-Johor',
            "View_from_Tuas_Checkpoint_to_sg": 'Johor-Tuas',
            "View_from_Tuas_Checkpoint_to_jh": 'Tuas-Johor',
            "View_from_Woodlands_Causeway_Towards_Johor_to_sg": 'Johor-Woodlands',
            "View_from_Woodlands_Causeway_Towards_Johor_to_jh": 'Woodlands-Johor',
            "View_from_Woodlands_Checkpoint_Towards_BKE_to_sg": 'Johor-Woodlands',
            "View_from_Woodlands_Checkpoint_Towards_BKE_to_jh": 'Woodlands-Johor'}

    new_table = df.replace({'view':transfer})
    options = ['Johor-Woodlands','Woodlands-Johor','Johor-Tuas','Tuas-Johor']
    final_df = new_table[new_table['view'].isin(options)]
    final_df.loc[:, 'time'] = pd.to_datetime(final_df.loc[:,'time'], format='%H:%M:%S')
    final_df.loc[:,'hour'] = final_df.loc[:,'time'].apply(hour_rounder)

    final_table = final_df.groupby(['view', 'day', 'hour']).sum().reset_index().loc[:,['day', 'hour','view', 'vehicle']]

    final_table.loc[:,'peak'] = final_table.loc[:,'hour'].apply(peak_hours)
    final_table.loc[:,'peak'] = final_table.loc[:,'peak'].astype('category')
    final_table.loc[:,'weekend'] = final_table.loc[:,'day'].apply(weekend)
    final_table.loc[:,'weekend'] = final_table.loc[:,'weekend'].astype('category')
    final_table.loc[:,'cat'] = final_table.loc[:,'vehicle'].apply(vehicle_cat)
    final_table.loc[:,'cat'] = final_table.loc[:,'cat'].astype('category')

    return final_table

def gen_fig():
    figs = []

    for i in range(5):
        midway = [15, 40, 70, 110, 150]
        cat = ['No Traffic', 'Minimal Traffic', 'Mild Traffic', 'Moderate Traffic', 'Peak Traffic']
        
        figure = go.Figure(go.Indicator(
                            mode = "gauge",
                            value = midway[i],
                            domain = {'x': [0, 1], 'y': [0, 1]},
                            title = {'text': cat[i], 'font': {'size': 24}},
                            gauge = {
                                'axis': {'range': [None, 156], 'tickwidth': 1, 'tickcolor': "darkblue"},
                                'bar': {'color': "blue"},
                                'bgcolor': "white",
                                'borderwidth': 2,
                                'bordercolor': "gray",
                                'steps': [
                                    {'range': [0, 19], 'color': 'darkgreen'},
                                    {'range': [20, 49], 'color': 'green'},
                                    {'range': [50, 79], 'color': 'yellow'},
                                    {'range': [80, 119], 'color': 'orange'},
                                {'range': [120, 160], 'color': 'red'}],
                                'threshold': {
                                    'line': {'color': "red", 'width': 4},
                                    'thickness': 0.75,
                                    'value': 490}}))

        figure.update_layout(paper_bgcolor = "lavender", font = {'color': "darkblue", 'family': "Arial"})

        figs.append(figure)

    return figs
    
def predicted_figure(clf, x, figs):
    
    result = create_row(x[0], x[1], x[2], x[3])

    pred_val = clf.predict(result)[0]

    return figs[pred_val]

def get_today():
    t = str(date.today()).split('-')
    today = []

    for i in t:
        if t[0] =='0':
            today.append(int(t[1:]))
        else:
            today.append(int(i))
    return today

def update_output(date_value):
    string_prefix = 'Travel Day: '
    if date_value is not None:
        date_string = convert_date(date_value)
        return string_prefix + date_string

def update_final_output_hour(starter_variables, my_date_picker_single, hours_dropdown_id, direction_id):
    # starter_variables = [clf, str(date.today()), "07:00", "Tuas-Johor"]
    starter_variables[1] = str(my_date_picker_single)
    starter_variables[2] = str(hours_dropdown_id)
    starter_variables[3] = str(direction_id)
    fig = predicted_figure(starter_variables)
    return fig

def train_model(x_train, y_train):
    clf = MLPClassifier(solver='lbfgs', alpha=3, hidden_layer_sizes=(5,4), random_state=2, max_iter=3000)
    clf.fit(x_train, y_train)

    return clf