-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
448 lines (431 loc) · 10.4 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
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
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State, ALL, MATCH
import utils.preprocessing as preprocessing
import utils.callbacks as callbacks
from models.ScanLine import ScanLine
from models.Impact import Impact
app = dash.Dash(
__name__,
meta_tags = [{
'name': 'viewport',
'content': 'width=device-width'
}],
suppress_callback_exceptions = True
)
server = app.server
# layout
app.layout = html.Div(
className = 'row twelve columns',
children = [
# left sidebar
html.Div(
# main instructions
className = 'four columns instruction',
children = [
html.H1('Impact Visualizer'),
html.P(
'''
- View source code (GitHub) by clicking "SOURCE"
- Upload impact files by clicking "UPLOAD DATA"
- Upload new impact files by clicking "UPLOAD DATA" again
- If your files are not named by their y-position, you must also include the MATLAB script (plot.m or Surface_Plot.m) containing the ystep variable.
''',
className = 'instruction-text'
),
# source and upload buttons
html.Div(
className = 'mobile-buttons',
children = [
# source button
html.Div(
className='mobile-button',
children = [
dcc.Link(
href = 'https://github.com/davidjtoomer/Impact-Visualizer',
target = '_blank',
children = [
html.Button(
'SOURCE',
className = 'src-button',
id = 'source-code'
)
]
),
]
),
# upload button
dcc.Upload(
id = 'upload-data',
multiple = True,
className = 'mobile-button',
children = [
html.Button(
'UPLOAD DATA',
className = 'upload-button',
id = 'upload'
)
]
)
]
),
# empty: impact toolbar
html.Div(
id = 'impact-toolbar',
className = 'impact-toolbar'
),
# empty: scanline toolbar
html.Div(
id = 'scanline-toolbar',
className = 'scanline-toolbar'
)
]
),
# right sidebar
html.Div(
className = 'eight columns graphs',
children = [
# impact graph
html.Div(id = 'impact-graph-container'),
# scanline graphs
html.Div(id = 'scanline-graph-container')
]
)
]
)
@app.callback(
[
Output('impact-graph-container', 'children'),
Output('impact-toolbar', 'children'),
Output('scanline-graph-container', 'children'),
Output('scanline-toolbar', 'children')
],
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
]
)
def load_data(contents, filenames):
if contents:
matlab_contents, _ = preprocessing.extract_matlab(contents, filenames)
ystep = preprocessing.extract_ystep(matlab_contents)
datafiles = preprocessing.parse_impact(contents)
global impact
global figure
impact = Impact(datafiles, filenames, ystep)
figure, impact_figure = callbacks.impact_figure(impact)
impact_toolbar = callbacks.impact_toolbar(impact)
scanline_toolbar = callbacks.scanline_toolbar(impact)
scanline_graphs = [
html.Div(
id = 'scanline-graph-all-display',
className = 'scanline-graph-all-display'
)
]
for i, _ in enumerate(impact.scanlines):
scanline_graphs.append(html.Div(
id = dict(
type = 'scanline-graph-display',
index = i
)
))
return impact_figure, impact_toolbar, scanline_graphs, scanline_toolbar
return None, None, None, None
@app.callback(
Output('scanline-graph-all-display', 'children'),
[
Input('scanline-view-all-button', 'value'),
]
)
def display_all_scanlines(value):
if value:
if value == 'hide': return callbacks.all_scanline_figure(impact, visible = 'legendonly')
return callbacks.all_scanline_figure(impact)
return None
@app.callback(
[
Output(
dict(
type = 'scanline-graph-display',
index = MATCH
),
'children'
),
Output(
dict(
type = 'scanline-view-button',
index = MATCH
),
'children'
),
Output(
dict(
type = 'scanline-regression-info',
index = MATCH
),
'style'
),
Output(
dict(
type = 'scanline-local-extrema',
index = MATCH
),
'style'
)
],
[
Input(
dict(
type = 'scanline-view-button',
index = MATCH
),
'n_clicks'
)
],
[
State(
dict(
type = 'scanline-view-button',
index = MATCH
),
'id'
),
State(
dict(
type = 'scanline-view-button',
index = MATCH
),
'children'
)
]
)
def display_scanline(n_clicks, scanline_id, curr_text):
if curr_text:
if n_clicks and n_clicks % 2:
return callbacks.scanline_figure(impact, scanline_id['index']), curr_text.replace('View', 'Hide'), {'display': 'block'}, {'display': 'block'}
return None, curr_text.replace('Hide', 'View'), {'display': 'none'}, {'display': 'none'}
return None, None, None
@app.callback(
Output('scanline-graph-all', 'figure'),
[
Input(
dict(
type = 'scanline-regression-slope',
index = ALL
),
'value'
),
Input(
dict(
type = 'scanline-regression-intercept',
index = ALL
),
'value'
)
],
[
State('scanline-graph-all', 'figure')
]
)
def update_global_scanline_figure(slopes, intercepts, fig):
if fig and dash.callback_context.triggered:
changed = dash.callback_context.triggered[0]['prop_id']
index = int(changed[changed.find(':') + 1 : changed.find(',')])
if slopes[index] and intercepts[index]:
scanline = impact.scanlines[index]
scanline.update_regression(slopes[index], intercepts[index])
new_data = scanline.data_corrected_smooth[:, 1]
fig['data'][index]['y'] = new_data
return fig
@app.callback(
Output(
dict(
type = 'scanline-graph',
index = MATCH
),
'figure'
),
[
Input(
dict(
type = 'scanline-regression-slope',
index = MATCH
),
'value'
),
Input(
dict(
type = 'scanline-regression-intercept',
index = MATCH
),
'value'
)
],
[
State(
dict(
type = 'scanline-graph',
index = MATCH
),
'figure'
),
State(
dict(
type = 'scanline-graph',
index = MATCH
),
'id'
)
]
)
def update_individual_scanline_regression(slope, intercept, fig, scanline_id):
if fig and dash.callback_context.triggered and slope and intercept:
scanline = impact.scanlines[scanline_id['index']]
scanline.update_regression(slope, intercept)
fig['data'][1]['y'] = scanline.data_corrected[:, 1]
fig['data'][2]['y'] = scanline.data_corrected_smooth[:, 1]
return fig
@app.callback(
[
Output(
dict(
type = 'scanline-local-minima',
index = MATCH
),
'children'
),
Output(
dict(
type = 'scanline-local-maxima',
index = MATCH
),
'children'
)
],
[
Input(
dict(
type = 'scanline-regression-slope',
index = MATCH
),
'value'
),
Input(
dict(
type = 'scanline-regression-intercept',
index = MATCH
),
'value'
)
],
[
State(
dict(
type = 'scanline-regression-slope',
index = MATCH
),
'id'
),
State(
dict(
type = 'scanline-local-minima',
index = MATCH
),
'children'
),
State(
dict(
type = 'scanline-local-maxima',
index = MATCH
),
'children'
)
]
)
def update_local_extrema(slope, intercept, scanline_id, minima, maxima):
if dash.callback_context.triggered and slope and intercept:
scanline = impact.scanlines[scanline_id['index']]
scanline.update_regression(slope, intercept)
minima = [html.P(f'({minima[0]:.5f}, {minima[1]:.5f})') for minima in scanline.minima[0]]
maxima = [html.P(f'({maxima[0]:.5f}, {maxima[1]:.5f})') for maxima in scanline.maxima[0]]
return minima, maxima
@app.callback(
Output('impact-graph', 'figure'),
[
Input('num-contours-slider', 'value'),
Input('colorscale-dropdown', 'value'),
Input('projection-dropdown', 'value'),
Input('view-dropdown', 'value'),
Input('x-range-slider', 'value'),
Input('y-range-slider', 'value'),
Input('z-scale-input', 'value'),
Input(
dict(
type = 'scanline-regression-slope',
index = ALL
),
'value'
),
Input(
dict(
type = 'scanline-regression-intercept',
index = ALL
),
'value'
)
]
)
def update_impact_graph(num_contours, colorscale, projection, view, x_range, y_range, z_scale, slopes, intercepts):
if dash.callback_context.triggered and slopes and intercepts:
for i in range(len(impact.scanlines)):
scanline = impact.scanlines[i]
scanline.update_regression(slopes[i], intercepts[i])
figure.data[0].z[i] = scanline.data_corrected_smooth[:, 1]
return callbacks.update_impact(impact, figure, num_contours, colorscale, projection, view, x_range, y_range, z_scale)
@app.callback(
Output('display-num-contours', 'children'),
[
Input('num-contours-slider', 'value')
]
)
def display_num_contours(num_contours):
return [
html.P(f'{num_contours}')
]
@app.callback(
[
Output('left-x-range-slider', 'children'),
Output('right-x-range-slider', 'children')
],
[
Input('x-range-slider', 'value')
]
)
def display_xrange(value):
return [
html.P(f'{value[0]:.3f}')
], [
html.P(f'{value[1]:.3f}')
]
@app.callback(
[
Output('left-y-range-slider', 'children'),
Output('right-y-range-slider', 'children')
],
[
Input('y-range-slider', 'value')
]
)
def display_yrange(value):
return [
html.P(f'{value[0]:.3f}')
], [
html.P(f'{value[1]:.3f}')
]
# run the server
if __name__ == '__main__':
app.run_server(debug = True)