-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
333 lines (297 loc) · 16.1 KB
/
main.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
# # VERSION 03 ###
# #################
import base64
import datetime
import dash_bootstrap_components as dbc
from dash import dash, html, Patch
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from data import categorize_inputdata, data_prep, marker_apply, create_standarddf_of_markers_summary, \
saveas_standard_csv_in_data_dir, marker_rename, marker_add, dataprep_laeq, dataprep_la95
from definitions import file_is_from_invalid_folder, project_folder_and_path
from audio import update_audio_source
from plot import create_fig_time_vs_db, dct_timeannotationlayout, fig_add_annotation, \
fig_patch_updated_marker, domain_get_start_end, fig_patch_renamed_marker, fig_patch_added_marker, \
create_fig_spectrum
from components import c_total_layout
folder_root, folder_data = project_folder_and_path()
# ######################################################################################
# # ######### BUILD DASHBOARD #########
# ######################################################################################
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = c_total_layout()
# ######################################################################################
# ######### CALLBACKS server-side #########
# ######################################################################################
# --------------------------------------------------------------------------------------
# ------------ TIME SERIES - audio ------------
# --------------------------------------------------------------------------------------
# audio file selection is pumped into the audioplayer
@app.callback(Output('cl_audiofile', 'children'), Output('cl_begintime', 'children'), Output('cl_audioplayer', 'src'),
Input('cl_drop_audiotimeandfile', 'value'),
State('cl_drop_audiotimeandfile', 'options'),
prevent_initial_call=True)
def update_audiosource(dropdownval, dropdownoptions):
dropdownval, o_datetime, s = update_audio_source(dropdownval, dropdownoptions)
return dropdownval, o_datetime, s # send the audio string to cl_audioplayer
# annotation of the actual audio-timestamp is patched on the graph
# allow duplicate is needed because figure is also updated from marker manipulations below
@app.callback(Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
Input('cl_ann', 'children'),
State('cl_hlp_figure', 'children'),
prevent_initial_call=True)
def add_ann_to_fig(actualtimevalue, figurestatus):
if figurestatus != "figure loaded": # if the file/figure is not loaded yet, nothing can be patched
raise PreventUpdate
patched_figure = Patch()
patched_figure["layout"]["annotations"].clear()
patched_figure["layout"]["annotations"].extend([dct_timeannotationlayout(actualtimevalue)])
return patched_figure
# --------------------------------------------------------------------------------------
# ------------ TIME SERIES - selection rectangle on fig ------------
# --------------------------------------------------------------------------------------
@app.callback(
Output('cl_selectbegin', 'children'), Output('cl_selectend', 'children'),
Input('cl_fig_timeseries', 'relayoutData'),
prevent_initial_call=True)
def selectiondomain(relayoutdata):
begin, einde = domain_get_start_end(relayoutdata)
return begin, einde # json.dumps(relayoutData, indent=2)
# --------------------------------------------------------------------------------------
# ------------ TIME SERIES - marker manipulations # ------------
# --------------------------------------------------------------------------------------
@app.callback(Output('cl_markererase', 'children'),
Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
Output('cl_store_df', 'data', allow_duplicate=True),
Input('cl_marker_btnerase', 'n_clicks'),
State('cl_store_df', 'data'),
State('cl_fig_timeseries', 'figure'),
State('cl_markers_used', 'value'),
State('cl_selectbegin', 'children'),
State('cl_selectend', 'children'),
prevent_initial_call=True)
def markers_erase(n_clicks, dct_df, fig, marker, starttime, endtime):
if marker is None:
raise PreventUpdate
else:
# change data
dct_df = marker_apply(dct_df, marker, starttime, endtime, 0)
# patch new data into figure
patched_figure = fig_patch_updated_marker(fig, marker, dct_df)
return n_clicks, patched_figure, dct_df
@app.callback(Output('cl_markerdraw', 'children'),
Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
Output('cl_store_df', 'data', allow_duplicate=True),
Input('cl_marker_btndraw', 'n_clicks'),
State('cl_store_df', 'data'),
State('cl_fig_timeseries', 'figure'),
State('cl_markers_used', 'value'),
State('cl_selectbegin', 'children'),
State('cl_selectend', 'children'),
prevent_initial_call=True)
def markers_draw(n_clicks, dct_df, fig, marker, starttime, endtime):
if marker is None:
raise PreventUpdate
else:
# change data
dct_df = marker_apply(dct_df, marker, starttime, endtime, 1)
# patch new data into figure
patched_figure = fig_patch_updated_marker(fig, marker, dct_df)
return n_clicks, patched_figure, dct_df
@app.callback(Output('cl_div_addandrenamesection', 'hidden', allow_duplicate=True),
Input('cl_marker_btnedit', 'n_clicks'),
prevent_initial_call=True)
def marker_editsection_setvisible(n_clicks):
return False
@app.callback(Output('cl_div_addandrenamesection', 'hidden', allow_duplicate=True),
Input('cl_marker_btncancel', 'n_clicks'),
prevent_initial_call=True)
def marker_editsection_setinvisible(n_clicks):
return True
@app.callback(Output('cl_div_addandrenamesection', 'hidden', allow_duplicate=True),
Output('cl_store_df', 'data', allow_duplicate=True),
Output("cl_store_c_markers", 'data', allow_duplicate=True),
# Output("cl_markers_used","options", allow_duplicate=True),
Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
State('cl_store_df', 'data'),
State("cl_store_c_markers", 'data'),
State('cl_markers_used', 'value'),
State('cl_inp_marker_add_or_rename', 'value'),
State('cl_fig_timeseries', 'figure'),
Input('cl_marker_btnrename', 'n_clicks'),
prevent_initial_call=True)
def marker_renaming(dct_df, dct_markers, oldmarkername, newmarkername, fig, n_clicks):
# change data when valid
valid, dct_df, dct_markers = marker_rename(dct_df, oldmarkername, newmarkername, dct_markers)
if not valid:
raise PreventUpdate
else:
# patch new data into figure
patched_figure = fig_patch_renamed_marker(fig, newmarkername, oldmarkername)
return True, dct_df, dct_markers, patched_figure
@app.callback(Output('cl_div_addandrenamesection', 'hidden', allow_duplicate=True),
Output('cl_store_df', 'data', allow_duplicate=True),
Output("cl_store_c_markers", 'data', allow_duplicate=True),
# Output("cl_markers_used","options", allow_duplicate=True),
Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
State('cl_store_df', 'data'),
State("cl_store_c_markers", 'data'),
State('cl_inp_marker_add_or_rename', 'value'),
State('cl_fig_timeseries', 'figure'),
Input('cl_marker_btnadd', 'n_clicks'),
prevent_initial_call=True)
def marker_adding(dct_df, dct_markers, newmarkername, fig, n_clicks):
# change data when valid
valid, dct_df, dct_markers = marker_add(dct_df, newmarkername, dct_markers)
if not valid:
raise PreventUpdate
else:
# patch new data into figure
patched_figure = fig_patch_added_marker(fig, newmarkername)
return True, dct_df, dct_markers, patched_figure
@app.callback(Output('cl_drp_markers_spec', 'options'),
Output('cl_markers_used', 'options'),
Input('cl_store_c_markers', 'data'),
prevent_intial_call=True)
def refresh(dct_markers):
return dct_markers, dct_markers
# --------------------------------------------------------------------------------------
# ------------ STATISTICS refresh ------------
# --------------------------------------------------------------------------------------
@app.callback(Output('cl_statsrefresh', 'children'),
Output("cl_tbl_markersummary", 'data'),
Input('cl_btnstatrefresh', 'n_clicks'),
State("cl_store_df", 'data'),
State("cl_store_c_markers", 'data'),
prevent_initial_call=True)
def refreshstatistics(n_clicks, dct_summary, dct_markers):
# update data of the summary statistics dataframe
dct_dfsummary = create_standarddf_of_markers_summary(dct_summary, dct_markers)
return n_clicks, dct_dfsummary
# --------------------------------------------------------------------------------------
# ------------ PLOT SPECTRUM ------------
# --------------------------------------------------------------------------------------
@app.callback(Output('cl_fig_spect', 'figure'),
Input('cl_btn_plotspec', 'n_clicks'),
State('cl_drp_markers_spec', 'value'),
State('cl_drp_LnLeq_spec', 'value'),
State("cl_store_df", 'data'),
prevent_initial_call=True)
def plotspectrum(n_clicks, marker, parameter, dct_df):
# dataprep
if parameter == 'Leq':
df = dataprep_laeq(dct_df, marker)
titel = marker + ' ' + parameter
else:
df = dataprep_la95(dct_df, marker)
titel = marker + ' ' + parameter
# plot
fig = create_fig_spectrum(df, titel)
return fig
# --------------------------------------------------------------------------------------
# ------------ SAVE DATA after editing ------------
# --------------------------------------------------------------------------------------
@app.callback(Output("cl_hlp_save", 'children'),
Input('cl_btn_save', 'n_clicks'),
State("cl_store_df", 'data'),
State('cl_hlp_filename', 'children'),
State('cl_store_c_always', 'data'),
State('cl_store_c_markers', 'data'),
State('cl_hlp_columnorder', 'children'),
prevent_initial_call=True)
def save(n_clicks, dct_df, filename, col_always, col_markers, col_order):
saveas_standard_csv_in_data_dir(dct_df, folder_data, filename, col_always, col_markers, col_order)
return n_clicks
# --------------------------------------------------------------------------------------
# ------------ INITIAL DATA LOAD into dash app ------------
# --------------------------------------------------------------------------------------
@app.callback(
Output('cl_filestatus', 'children'),
Output('cl_hlp_filename', 'children'),
Output('cl_hlp_figure', 'children'),
Output('cl_begintime', 'children', allow_duplicate=True),
Output("cl_store_df", 'data'),
Output("cl_store_c_always", 'data'),
Output("cl_store_c_markers", 'data'),
Output("cl_fig_timeseries", 'figure', allow_duplicate=True),
Output("cl_drop_audiotimeandfile", "options"),
Output("cl_spectstatus", "children"),
Output("cl_hlp_columnorder", "children"),
Input('cl_upload01', 'contents'),
State('cl_upload01', 'filename'),
prevent_initial_call=True
)
def load_data_into_layout(strcontent, f):
# initialize empty dictionaries, lists and dummies
dfdict, dfsummarydict, fig = dict(), dict(), dict()
lst_flds_a, lst_flds_m_used, lstsound, kolomvolgorde = [], [], [], []
figurestatus = "figure not loaded yet"
begintime = "1976-07-02 23:30:00" # my dummy birthday
spectralinfo = "there is no spectral info"
# check if file is dropped from the datafolder, if not from datafolder: stop
invalid, status = file_is_from_invalid_folder(f, folder_data)
if not invalid: # check sonometer-type
# decode inputstring of dropped file
content_type, content_string = strcontent.split(',') # split content string from dcc
decoded = base64.b64decode(content_string)
# check sonometer type, based on decoded string
invalid, status = categorize_inputdata(decoded)
# data preparation only if sonometer-type is known
if not invalid:
lst_flds_a, lst_flds_st, lst_flds_m_used, begintime, df, lstsound, spectralinfo = \
data_prep(slmtype=status, decoded=decoded, filename=f)
# store column-order for saving (dictionaries don't preserve this order)
kolomvolgorde = df.columns.to_list()
# put the dataframe in dcc store as a dict for later use
dfdict = df.to_dict("records")
fig = create_fig_time_vs_db(df, lst_flds_a, lst_flds_m_used)
fig_add_annotation(fig, begintime)
figurestatus = "figure loaded"
return status, f, figurestatus, begintime, \
dfdict, lst_flds_a, lst_flds_m_used, \
fig, lstsound, spectralinfo, kolomvolgorde
# ######################################################################################
# ######### CALLBACK client-side (audio annotation on figure) #########
# ######################################################################################
app.clientside_callback(
"""
function TrackCurrentTime(jsbegintime, jsinterval)
// Get the acutal time from cl_audioplayer based on :
// the begintime of the graph,
// added with
// the elapsed time of the audioplayer
// Parameters:
// jsbegintime: begintime from html component needed to calculate annotation position
// jsinterval: interval which states the update-speed (ms) of the new value of the audio element
// Return: txt_ann: a textual annotation with the actual timestamp
{
// HELP-FUNCTIONS --->
// Help-function to add seconds to a given date returning new date
function addSeconds(date, seconds) {date.setSeconds(date.getSeconds() + seconds);return date;}
// Help-function to get iso-string of the local - time object
// (function "toISOString" gives string of time object at Greenwich, and here is not Greenwich)
function getLocalISOString(date) {
const offset = date.getTimezoneOffset()
const offsetAbs = Math.abs(offset)
const isoString = new Date(date.getTime() - offset * 60 * 1000).toISOString()
return `${isoString.slice(0, -1)}${offset > 0 ? '-' : '+'}
${String(Math.floor(offsetAbs / 60)).padStart(2, '0')}:${String(offsetAbs % 60).padStart(2, '0')}`
}
// <--- HELP-FUNCTIONS
// MAIN - FUNCTION --->
// get value of audioplayer and calculate datetime-object
const myaudio = document.getElementById("cl_audioplayer");
const time_cur_s = Math.round(myaudio.currentTime);
const o_time_start = new Date(jsbegintime);
const o_ann = addSeconds(o_time_start, time_cur_s);
const txt_ann = getLocalISOString(o_ann).substring(0, 19);
// <--- MAIN - FUNCTION
return txt_ann;
}
""",
Output('cl_ann', 'children'),
Input('cl_begintime', 'children'),
Input('cl_interval', 'n_intervals'), # every dcc.interval a new value is taken from audio component
)
if __name__ == '__main__':
app.run_server(debug=True)