/
app.py
250 lines (195 loc) · 7.31 KB
/
app.py
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
import dash
import dash_html_components as html
from elements import Elements
from dash.dependencies import Input, Output, State
from data import Data
import plotly.graph_objs as go
from predictor import Predictor
app = dash.Dash(__name__)
server = app.server
app.title = "Sydney Solar | Exposure predictor"
external_css = ["https://fonts.googleapis.com/css?family=Product+Sans:400,400i,700,700i",
"https://rawgit.com/JalalElwazze/sydneysolar/master/app.css"]
for css in external_css:
app.css.append_css({"external_url": css})
app.layout = html.Div(
[
Elements.header(),
Elements.graph_block(),
Elements.scatter_spear_block(),
Elements.prediction_block(),
Elements.convergence_block()
])
dataset = Data.merged_norm()
# Dropdown --> Time series
@app.callback(
Output("raw_data_graph", "figure"), [Input("raw_data_select", "value")]
)
def update_raw_data_plot(values):
new_data = []
colors = ['#4f9982', '#ba554f', "#f49b41", '#458fd3']
for index, value in enumerate(values):
new_data.append(
dict(
x=dataset['Time'],
y=dataset[value],
name=value,
type='line',
line=dict(color=colors[index], width=2),
)
)
layout = go.Layout(
title="Raw Time Series Data",
margin={'l': 50, 'b': 50, 't': 70, 'r': 0},
)
return {'data': new_data, 'layout': layout}
# Dropdon --> Scatter
@app.callback(
Output("scatter_plot", "figure"), [Input("scatter_select", "value")]
)
def update_scatter_plot(values):
new_data = []
colors = ['#4f9982', '#ba554f', "#f49b41", '#458fd3']
for index, value in enumerate(values):
new_data.append(
go.Scatter(
x=dataset['Exposure'],
y=dataset[value],
name=value,
mode='markers', opacity=0.7,
marker={
'size': 15,
'line': {'width': 0.5, 'color': 'white'},
'color': colors[index]
},
)
)
layout = go.Layout(
title="Scatter Vs Solar Exposure",
margin={'l': 50, 'b': 50, 't': 100, 'r': 50},
)
return {'data': new_data, 'layout': layout}
# Dropdown --> Pie
@app.callback(
Output("spearman_pie", "figure"), [Input("scatter_select", "value")]
)
def update_pie(values):
new_values = []
new_colors = []
colors = ['#4f9982', '#ba554f', "#f49b41", '#458fd3']
for index, value in enumerate(values):
new_values.append(Data.compute_rank(value))
new_colors.append(colors[index])
new_data = go.Pie(
labels=values, values=new_values, hoverinfo='label+value', textinfo='percent', textfont=dict(size=15),
showlegend=False, opacity=0.9,
marker=dict(colors=new_colors, line=dict(color='white', width=2)))
layout = go.Layout(
title="Spearman Ranks",
margin={'l': 50, 'b': 50, 't': 100, 'r': 50},
)
return {'data': [new_data], 'layout': layout}
# Prediction tools --> Train Model
@app.callback(
Output("prediction_graph", "style"), [Input('train', 'n_clicks')],
[State("select_inputs", "value"), State("select_model", "value")]
)
def train_model(static, inputs, models):
if (inputs is not None) and (models is not None):
# Filter inputs
file = models
inputs = sorted(inputs)
# Make filename
for parameter in inputs:
temp = parameter.strip(" ")
file += "_" + temp
# Initialise model
model = Predictor(dataset, inputs=inputs, model=models, filename=file)
# Train Model
model.train(100)
# Prediction tools --> Visualise on Main Plot
@app.callback(
Output("prediction_graph", "figure"), [Input('run', 'n_clicks')],
[State("select_inputs", "value"), State("select_model", "value")]
)
def run_model(static, inputs, models):
if (inputs is not None) and (models is not None):
# Filter inputs
file = models
inputs = sorted(inputs)
# Make filename
for parameter in inputs:
temp = parameter.strip(" ")
file += "_" + temp
# Initialise model
model = Predictor(dataset, inputs=inputs, model=models, filename=file)
# Run Model
result = model.predict()
# Visualise
error = 0.10
upper_bound = go.Scatter(
name='Upper Error', x=result['Time'], y=result['Predictions']*(1 + error),
mode='lines', marker=dict(color="444"), line=dict(width=0), fillcolor='rgba(62, 120, 214, 0.3)',
fill='tonexty')
trace = go.Scatter(
name='Predicted', x=result['Time'], y=result['Predictions'],
mode='lines', line=dict(color='rgba(62, 120, 214, 0.7)'),
fillcolor='rgba(62, 120, 214, 0.3)', fill='tonexty',)
lower_bound = go.Scatter(
name='Lower Error', x=result['Time'], y=result['Predictions']*(1 - error),
marker=dict(color="444"), line=dict(width=0), mode='lines')
new_data = [{'x': dataset.Time, 'y': dataset["Exposure"], 'name': "True Value",
'line': dict(color='rgba(68, 68, 68, 0.4)', width=2)}, lower_bound, trace, upper_bound]
return {'data': new_data}
# Prediction Tools --> Status Update color
@app.callback(
Output("status", "style"), [Input('train', 'n_clicks'), Input('run', 'n_clicks')],
)
def button_color(static, clicks_run):
if clicks_run is not None:
if (clicks_run % 2) == 0:
color = 'rgba(79, 153, 130, 0.7)'
else:
color = 'rgba(244, 155, 65, 0.7)'
return {'width': '150px', 'margin-right': '10px', 'margin-top': '10px',
'background-color': color}
# Run Button --> Fit Convergence
@app.callback(
Output("fit_convergence_plot", "figure"), [Input('run', 'n_clicks')],
[State("select_inputs", "value"), State("select_model", "value")],
)
def update_converge(static, inputs, models):
if (inputs is not None) and (models is not None):
# Filter inputs
file = models
inputs = sorted(inputs)
# Make filename
for parameter in inputs:
temp = parameter.strip(" ")
file += "_" + temp
# Initialise model
model = Predictor(dataset, inputs=inputs, model=models, filename=file)
# Run
results = model.check()
return {'data': [{'x': results[-1], 'y': results[0], 'line': dict(color='grey')}]}
# Run Button --> Prediction Convergence
@app.callback(
Output("pred_convergence_plot", "figure"), [Input('run', 'n_clicks')],
[State("select_inputs", "value"), State("select_model", "value")],
)
def update_prediction_convergence(static, inputs, models):
if (inputs is not None) and (models is not None):
# Filter inputs
file = models
inputs = sorted(inputs)
# Make filename
for parameter in inputs:
temp = parameter.strip(" ")
file += "_" + temp
# Initialise model
model = Predictor(dataset, inputs=inputs, model=models, filename=file)
# Run
results = model.check()
return {'data': [{'x': results[-1], 'y': results[1], 'line': dict(color='black')}]}
if __name__ == '__main__':
app.run_server(debug=True)