-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
469 lines (407 loc) · 21.8 KB
/
app.py
File metadata and controls
469 lines (407 loc) · 21.8 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 9 10:26:09 2018
@author: Aveedibya Dey
"""
import dash_table_experiments as dte
from parse_contents import parse_contents
import forecast_example as fcst
import dash_core_components as dcc
import plotly.graph_objs as go
import pandas as pd
import dash
import dash_html_components as html
import calendar
import forecast_models
import adjustment_block
app = dash.Dash()
app.scripts.config.serve_locally = True
app.config['suppress_callback_exceptions']=True
app.title = 'Forecasting Tool'
server = app.server
def dropdown_dict(df):
date_dict = []
values_date_default = []
for month in df['Date'].dt.month.unique():
date_dict.append({'label': calendar.month_abbr[month], 'value': month})
values_date_default.append(month)
print('-----------------dropdown values processed----------------------')
return date_dict, values_date_default
#date_dict, values_date_default = dropdown_dict(df)
app.layout = html.Div(children=[
dcc.Markdown(children='''### Forecast Generator: '''),
html.Div([dcc.Markdown('''__Quick Description__: Forecast generator and viewer. Shows monthly and daily level views of forecast with interactive filters. ''')], style={'borderTop': 'thin lightgrey solid', 'borderBottom': 'thin lightgrey solid', 'padding': '5'}),
#Add filters for the data
html.Div([
html.Div([html.Label('Choose Data to Show:'),
dcc.RadioItems(id='dataselector', options=[
{'label': 'View Data Only', 'value': 1},
{'label': 'View Data and Forecast', 'value': 2},
{'label': 'View Forecast Only', 'value': 3}
], value=2, labelStyle={'display': 'inline-block'})
], style={'width': '49%', 'display': 'inline-block'}),
html.Div([html.Label('Choose Month:'),
dcc.Dropdown(id='date-dropdown', multi=True
#,options=date_dict, value=values_date_default, multi=True
)], style={'width': '49%', 'display': 'inline-block', 'float': 'right'})
], style={'borderBottom': 'thin lightgrey dotted', 'padding': '20px 5px'}),
#Graph
html.Div([html.Div([dcc.Graph(id='graph-daily')], style={'padding': '10 10', 'display': 'inline-block', 'width': '69%'}),
html.Div([html.Label('Adjust Forecast:'), #----Adjust Forecast Elements
html.Div([
html.Div([dcc.Input(id='adj-number', type='number')], style={'display': 'inline-flex'}),
html.Div([html.Button('Create Adj.', id='adj-creator')], style={'display': 'inline-flex'})
], style={'width': '50%'}),
html.Div(children=adjustment_block.add_adj_block(3)[0])], style={'display': 'inline-block', 'width': '29%', 'float': 'top', 'vertical-align': 'top', 'padding': '5'})
], style={'padding': '0'}),
#Choose Forecasting Model
html.Div([html.Label('Choose Forecasting Model:'),
dcc.Dropdown(id='model-dropdown',
options=[
{'label': 'ARIMA Time Series', 'value': 1},
{'label': 'FB Prophet', 'value': 2},
{'label': 'Moving Average', 'value': 3}], value=3)
], style={}),
#Take ARIMA Inputs
html.Div([
html.Div(dcc.Input(id='arima-p', type='number', placeholder='AR(p)='), style={'display': 'inline-block'}),
html.Div(dcc.Input(id='arima-d', type='number', placeholder='I(d)='), style={'display': 'inline-block'}),
html.Div(dcc.Input(id='arima-q', type='number', placeholder='MA(q)='), style={'display': 'inline-block'}),
html.Button('Submit ARIMA Parameters ', id='arima-submit', className='button-primary'),
html.Div(id='output-container-button',
children='Enter ARIMA parameters and click Submit to refresh forecast!')], id='arima-inputblock'),
#Take Moving Average Inputs
html.Div([
html.Div(dcc.Input(id='n_weeks', type='number', placeholder='#Week for averaging = 6'), style={'display': 'inline-block'}),
html.Div(dcc.Input(id='period', type='number', placeholder='Period = 7'), style={'display': 'inline-block'}),
html.Button('Update Moving Average Forecast ', id='movingavg-submit', className='button-primary'),
html.Div(id='output-container-button-ma',
children='Enter Moving Average parameters and click Submit to refresh forecast!')], id='movingavg-inputblock'),
#Stores df-to-json for a forecast method
html.Div(id='intermediate-value', style={'display': 'none'}),
#Stores uploded data converted to df-to-json
html.Div(id='upload-data-df', style={'display': 'none'}),
#Stores uploded data converted to df-to-json
html.Div(id='adj-forecast-df', style={'display': 'none'}),
#Data Table elements
html.Div([dcc.Upload(
id='upload-data',
children=html.Div(['Drag and Drop or ', html.A('Select Files')
]),
style={
'width': '30%', 'display':'inline-block', 'height': '90px', 'lineHeight': '90px', 'borderWidth': '0px',
'borderStyle': 'solid','borderRadius': '5px', 'textAlign': 'center', 'margin-top': '20px', 'marginRight': '20px',
'backgroundColor': '#ebebe0', 'verticalAlign': 'middle'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div([html.Label(dcc.Markdown('*__OR__ Select Built-in Sample Data:*')),
html.Div(dcc.Dropdown(id='select-dataset',
options=[{'label': 'Peyton Manning WikiPage Visits', 'value': 1},
{'label': 'Air Passenger Data', 'value': 2}],
placeholder='Built-in Dataset Not Selected',
value=''
),
style={}
)], style={'backgroundColor': '#ebebe0',
'margin-top': '20px', 'padding': '10', 'borderRadius': '5px',
'display': 'inline-block', 'width': '30%', 'verticalAlign': 'middle'})
]),
html.Div(id='output-data-upload'),
html.Div(dte.DataTable(rows=[{}]), style={'display': 'none'})
]
#, className='container' #If you want to show everything in a shrinked smaller central section
, style={'width': ''})
def return_a_textbox(value):
'''
'''
if value:
return html.Div([
html.Div(dcc.Input(id='input-box', type='text')),
html.Button('Submit', id='button'),
html.Div(id='output-container-button',
children='Enter a value and press submit')])
#--------------------------------------------------------
#Show ARIMA input parameters block
@app.callback(
dash.dependencies.Output('arima-inputblock', 'style'),
[dash.dependencies.Input('model-dropdown', 'value')])
def update_arimablock(value):
if value ==1:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
#--------------------------------------------------------
#Show ARIMA input parameters block
@app.callback(
dash.dependencies.Output('movingavg-inputblock', 'style'),
[dash.dependencies.Input('model-dropdown', 'value')])
def update_movingavgblock(value):
if value ==3:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
#--------------------------------------------------------
#Generate forecasts - Forecast dataframe is generated
@app.callback(
dash.dependencies.Output('intermediate-value', 'children'),
[dash.dependencies.Input('upload-data-df', 'children'),
dash.dependencies.Input('model-dropdown', 'value'),
dash.dependencies.Input('arima-submit', 'n_clicks'),
dash.dependencies.Input('movingavg-submit', 'n_clicks')],
[dash.dependencies.State('arima-p', 'value'),
dash.dependencies.State('arima-d', 'value'),
dash.dependencies.State('arima-q', 'value'),
dash.dependencies.State('n_weeks', 'value'),
dash.dependencies.State('period', 'value')])
def update_daily_view(uploaded_df, modelselected, arima_n_clicks, ma_n_clicks, arimap=1, arimad=0, arimaq=1,
n_weeks_ma=6, period_ma=7):
'''
'''
print("-----Printing Uploaded Data------")
print(uploaded_df)
df = pd.read_json(uploaded_df, orient='split').dropna()
#df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')
print(df)
if modelselected ==1: #1=ARIMA
df_final = fcst.fcst_wklyavg(df, stop_at_futuredates=1)
arima_fcst = forecast_models.forecast_ARIMA(df, p=arimap, d=arimad, q=arimaq)
df_final['Forecast'][df.shape[0]:] = arima_fcst[0][0]
elif modelselected == 2: #2= FB Prophet
df_final = fcst.fcst_wklyavg(df, stop_at_futuredates=1)
print('--------------Empty forecast dataframe generated-----------')
print(df_final.tail())
prophetfcst = forecast_models.forecast_FBProphet(df, futureperiod=90)
df_final['Forecast'][df.shape[0]:] = prophetfcst['yhat'][df.shape[0]:]
df_final = plot_error_range(df, df_final, prophetfcst)
#df_final = df_final.rename(columns={'ds':'Date', 'yhat':'Forecast'})
elif modelselected ==3: #3=Moving Average
df_final = fcst.fcst_wklyavg(df, n_week=n_weeks_ma, data_period=period_ma)
#print(df_final.tail())
return df_final.to_json(date_format='iso', orient='split')
#--------------------------------------------------------
def plot_error_range(df, df_final, df_fcst, modelselected=3):
'''
'''
df_final['Forecast_upper']=0.0
df_final['Forecast_lower']=0.0
if modelselected ==3: #FB Prophet Model
df_final['Forecast_upper'][df.shape[0]:] = df_fcst['yhat_upper'][df.shape[0]:]
df_final['Forecast_lower'][df.shape[0]:] = df_fcst['yhat_lower'][df.shape[0]:]
print("-----error range df returned------")
print(df_final.tail())
return df_final
#--------------------------------------------------------
#Update Dropdown Filter
@app.callback(
dash.dependencies.Output('date-dropdown', 'options'),
[dash.dependencies.Input('intermediate-value', 'children')])
def update_dropdown_options(json_intermediate_data):
'''
'''
print('-------------------updating dropdown filters: Options-------------------')
print(json_intermediate_data)
df_final = pd.read_json(json_intermediate_data, orient='split')
print(df_final)
date_dict, values_date_default = dropdown_dict(df_final)
return date_dict
#--------------------------------------------------------
#Update Dropdown Values
@app.callback(
dash.dependencies.Output('date-dropdown', 'value'),
[dash.dependencies.Input('intermediate-value', 'children')])
def update_dropdown_values(json_intermediate_data):
'''
'''
print('-------------------updating dropdown filters: Values-------------------')
print(json_intermediate_data)
df_final = pd.read_json(json_intermediate_data, orient='split')
date_dict, values_date_default = dropdown_dict(df_final)
return values_date_default
#--------------------------------------------------------
#Update graph based on filters
@app.callback(
dash.dependencies.Output('graph-daily', 'figure'),
[dash.dependencies.Input('date-dropdown', 'value'),
dash.dependencies.Input('dataselector', 'value'),
dash.dependencies.Input('intermediate-value', 'children')
,dash.dependencies.Input('adj-forecast-df', 'children')
])
def update_daily_viewfilters(selected_month, dataselected, json_intermediate_data, json_intermediate_adj):
'''
'''
#json_intermediate_adj=None
if json_intermediate_adj is not None:
print("-------->>> adjusting forecast")
df_final = pd.read_json(json_intermediate_adj, orient='split')
else:
df_final = pd.read_json(json_intermediate_data, orient='split')
print("-------->>> using original forecast")
#date_dict, values_date_default = dropdown_dict(df_final)
filtered_df = df_final[df_final['Date'].dt.month.isin(selected_month)]
traces = []
month_list = []
annotations=None
for i in selected_month:
month_list.append(calendar.month_abbr[i])
if dataselected !=3: #Option 3 is Forecast Only!
traces.append(go.Scatter(
x=filtered_df[filtered_df['Volume']>0]['Date'],
y=filtered_df[filtered_df['Volume']>0]['Volume'],
mode='lines', name='Actuals'# for: '+",".join(month_list)
))
if dataselected !=1: #Option 1 is Data Only!
traces.append(go.Scatter(
x=filtered_df[filtered_df['Forecast']>0]['Date'],
y=filtered_df[filtered_df['Forecast']>0]['Forecast'],
mode='lines', name='Forecast'# for: ' + ",".join(month_list)
))
if 'Adj_Forecast' in filtered_df.columns:
traces.append(go.Scatter(
x=filtered_df[filtered_df['Adj_Forecast']>0]['Date'],
y=filtered_df[filtered_df['Adj_Forecast']>0]['Adj_Forecast'],
mode='lines', name='Adj Forecast'# for: ' + ",".join(month_list)
))
filter_annotes = filtered_df[filtered_df['Adj_Annotation']!='']
annotations=[dict(
x=filter_annotes['Date'].tolist()[i], y=0,#filter_annotes['Adj_Forecast'].tolist()[0],
text=filter_annotes['Adj_Annotation'].tolist()[i], showarrow=True, arrowhead=7,
ax=0, ay=-20*(i+1)) for i in range(len(filter_annotes['Date'].tolist()))]
if 'Forecast_upper' in filtered_df.columns:
traces.append(go.Scatter(
x=filtered_df[filtered_df['Forecast']>0]['Date'].tolist() + filtered_df[filtered_df['Forecast']>0]['Date'].tolist()[::-1],
y=filtered_df[filtered_df['Forecast']>0]['Forecast_upper'].tolist() + filtered_df[filtered_df['Forecast']>0]['Forecast_lower'].tolist()[::-1],
fill='tozeroy', fillcolor='rgba(0,100,80,0.2)', line=dict(color='rgba(255,255,255,0)'), name='Forecast-errorzone'# for: ' + ",".join(month_list)
))
return {
'data': traces,
'layout': go.Layout(
xaxis={'title': 'Months Selected: ' + ", ".join(month_list)},
yaxis={'title': 'Volume'},
#margin={'l': 40, 'b': 30, 't': 40, 'r': 0},
#legend={'x': 0, 'y': 1},
hovermode='closest',
annotations=annotations
#plot_bgcolor='#e0e0eb',
#paper_bgcolor='#e0e0eb'
)
}
#--------------------------------------------------------
#Store the uploaded DF
@app.callback(dash.dependencies.Output('upload-data-df', 'children'),
[dash.dependencies.Input('upload-data', 'contents'),
dash.dependencies.Input('upload-data', 'filename'),
dash.dependencies.Input('upload-data', 'last_modified'),
dash.dependencies.Input('select-dataset', 'value')])
def update_output(list_of_contents, list_of_names, list_of_dates, dataset_selected):
if list_of_contents is not None:
children = [
parse_contents(c, n, d)[1] for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
print(children[0])
return children[0]
elif dataset_selected in [1,2]:
if dataset_selected == 1:
return pd.read_csv('sample_datasets/example_wp_log_peyton_manning.csv').dropna().to_json(date_format='iso', orient='split')
elif dataset_selected == 2:
return pd.read_csv('sample_datasets/example_air_passengers.csv').dropna().to_json(date_format='iso', orient='split')
#--------------------------------------------------------
#Data Table Output
@app.callback(dash.dependencies.Output('output-data-upload', 'children'),
[dash.dependencies.Input('upload-data', 'contents'),
dash.dependencies.Input('upload-data', 'filename'),
dash.dependencies.Input('upload-data', 'last_modified')])
def update_output_data(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d)[0] for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
#--------------------------------------------------------
#Forecast Adjustment Block
@app.callback(dash.dependencies.Output('adj-master-block-0', 'style'),
[dash.dependencies.Input('adj-creator', 'n_clicks')],
[dash.dependencies.State('adj-number', 'value')])
def update_adj_block0(creator_clicks, number_of_adj_blocks):
if number_of_adj_blocks >= 1:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
@app.callback(dash.dependencies.Output('adj-master-block-1', 'style'),
[dash.dependencies.Input('adj-creator', 'n_clicks')],
[dash.dependencies.State('adj-number', 'value')])
def update_adj_block1(creator_clicks, number_of_adj_blocks):
if number_of_adj_blocks >=2:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
@app.callback(dash.dependencies.Output('adj-master-block-2', 'style'),
[dash.dependencies.Input('adj-creator', 'n_clicks')],
[dash.dependencies.State('adj-number', 'value')])
def update_adj_block2(creator_clicks, number_of_adj_blocks):
if number_of_adj_blocks >=3:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
@app.callback(dash.dependencies.Output('adj-forecast', 'style'),
[dash.dependencies.Input('adj-creator', 'n_clicks')],
[dash.dependencies.State('adj-number', 'value')])
def update_adj_block_button(creator_clicks, number_of_adj_blocks):
if number_of_adj_blocks >=1:
return {'display': 'block', 'padding': '20 0'}
else:
return {'display': 'none'}
#-----------------------------------------------------
#Forecast Adjuster
@app.callback(
dash.dependencies.Output('adj-forecast-df', 'children'),
[dash.dependencies.Input('intermediate-value', 'children'),
dash.dependencies.Input('adj-forecast', 'n_clicks')],
[dash.dependencies.State('adj-input-0', 'value'),
dash.dependencies.State('datepicker-0', 'start_date'),
dash.dependencies.State('datepicker-0', 'end_date'),
dash.dependencies.State('adj-input-1', 'value'),
dash.dependencies.State('datepicker-1', 'start_date'),
dash.dependencies.State('datepicker-1', 'end_date'),
dash.dependencies.State('adj-input-2', 'value'),
dash.dependencies.State('datepicker-2', 'start_date'),
dash.dependencies.State('datepicker-2', 'end_date')])
def adj_forecast(json_intermediate_data, n_clicks,
input0, stdt0, enddt0,
input1, stdt1, enddt1,
input2, stdt2, enddt2):
'''
'''
print("--------->>> Entered forecast adjusting block")
df_final = pd.read_json(json_intermediate_data, orient='split')
#date_dict, values_date_default = dropdown_dict(df_final)
filtered_df = df_final
#clear existing columns
if 'Adj_Forecast' in filtered_df.columns:
filtered_df.drop('Adj_Forecast', axis=1)
if 'Adj_Annotation' in filtered_df.columns:
filtered_df.drop('Adj_Annotation', axis=1)
if input0 is not None or input1 is not None or input2 is not None:
filtered_df['Adj_Forecast'] = filtered_df['Forecast']
filtered_df['Adj_Annotation'] = ''
print(">>>>filtered_df processing...")
print(filtered_df.tail())
if stdt0 is not None and enddt0 is not None:
print("-->>-->>-->> processing 1st adjustment")
filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt0) & (filtered_df['Date'] <=enddt0)] = filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt0) & (filtered_df['Date'] <=enddt0)] + float(input0)
filtered_df['Adj_Annotation'][filtered_df['Date']==stdt0] = "Adj. #1 Applied from:\n" + str(stdt0) + " to " + str(enddt0)
if stdt1 is not None and enddt1 is not None:
print("-->>-->>-->> processing 2nd adjustment")
filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt1) & (filtered_df['Date'] <=enddt1)] = filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt1) & (filtered_df['Date'] <=enddt1)] + float(input1)
filtered_df['Adj_Annotation'][filtered_df['Date']==stdt1] = "Adj. #2 Applied from:\n" + str(stdt1) + " to " + str(enddt1)
if stdt2 is not None and enddt2 is not None:
print("-->>-->>-->> processing 3rd adjustment")
filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt2) & (filtered_df['Date'] <=enddt2)] = filtered_df['Adj_Forecast'][(filtered_df['Date']>=stdt2) & (filtered_df['Date'] <=enddt2)] + float(input2)
filtered_df['Adj_Annotation'][filtered_df['Date']==stdt2] = "Adj. #3 Applied from:\n" + str(stdt2) + " to " + str(enddt2)
print("--------->>> after forecast adjustment")
print(filtered_df.tail())
return filtered_df.to_json(date_format='iso', orient='split')
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
if __name__ == '__main__':
app.run_server(debug=True)