-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneurosky_dash.py
283 lines (255 loc) · 9.51 KB
/
neurosky_dash.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
import dash
import dash_bootstrap_components as dbc
from dash.dependencies import Output, Input, State
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import threading
from data_extraction import extract_data
import numpy as np
import multiprocessing
from spectr_extraction import extract_spectr
import pickle
import time
#Глобальные переменные для работы с гарнитурой
jobs = []
process_job = multiprocessing.Queue()
file = "data.csv"
duration = 15
num_spec = 4
segmentation = 4
#Инициализация Dash приложения
app = dash.Dash(__name__,
external_stylesheets=[dbc.themes.MINTY],
meta_tags=[{'name': 'viewport',
'content': 'width=device-width, initial-scale=1.0'}],
update_title=None)
app.config.suppress_callback_exceptions = True
#Обертка приложения
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
dbc.Nav([
dbc.NavItem(dbc.Button("Start", id="button", disabled=False, n_clicks=0, color="primary", className="mr-1",
style={"padding": "10px 40px","font-size": "20px", "margin": "10px", "margin-top": "20px"})),
dbc.NavItem(dbc.NavLink(id="live-text", style={ "color": "#ffffff",
"font-size": "20px",
"margin": "10px",
"padding-top": "20px"}))
])
]),
], style={"background-color": "#3c3f50",}),
dbc.Row([
dbc.Col([
dcc.Graph(id="live-graph"),
dcc.Interval(
id="interval-component",
interval=100,
n_intervals=0
)
], style={"margin-top": "10px"}, width={"size": 6}),
dbc.Col([
dcc.Graph(id="live-heatmap"),
dcc.Interval(
id="interval-heatmap",
interval=1500,
n_intervals=0
)
], style={"margin-top": "10px"}, width={"size": 3}),
], justify="center"),
dbc.Row([
dbc.Col([
dcc.Graph(id="live-svm"),
dcc.Interval(
id="interval-stats",
interval=1500,
n_intervals=0
)
], style={"margin-top": "10px"}, width={"size": 3}),
dbc.Col([
dcc.Graph(id="live-xgboost"),
], style={"margin-top": "10px"}, width={"size": 3}),
dbc.Col([
dcc.Graph(id="live-percentage"),
], style={"margin-top": "10px"}, width={"size": 3}),
html.P(id="live-prediction", style={"display": "none"})
], justify="center")
], fluid=True)
#Словарь с состояниями работы программы
state = {
"prediction": [],
"button": False,
"process_prediction": 0,
"process_extraction": 0,
"jobs": "Data exctraction isn't done",
"disabled": False,
"labels_svm": [],
"values_svm": [],
"labels_xg": [],
"values_xg": []
}
#Mетоды Dash приложения для взаимодействия гарнитуры с интерфейсом
@app.callback([Output("button", "disabled"),
Output("button", "n_clicks")],
[Input("button", "n_clicks"),
Input("interval-heatmap", "n_intervals")])
def button_live(clicks, intervals):
if clicks > 0:
state["process_extraction"] = 0
state["process_prediction"] = 0
extract_thread = threading.Thread(target=extract_data, kwargs={"jobs": jobs})
extract_thread.start()
# extract_data(jobs=jobs)
state["button"] = True
state["disabled"] = False
state['jobs'] = "Data exctraction isn't done"
return True, 0
else:
return state["button"], 0
@app.callback([Output("live-text", "children"),
Output("interval-component", "disabled")],
[Input("interval-heatmap", "n_intervals")])
def update_jobs(n):
if jobs:
if state["process_extraction"] == 0:
#Обработка сетрограмм вызвана отдельным процессом, потому
#что matplotlib не может быть вызван в дочернем треде
multiprocessing.Process(target=extract_spectr, args=(file, num_spec, 128 * 3, process_job)).start()
state["process_extraction"] = 1
state["button"] = False
time.sleep(0.1)
state["jobs"] = jobs[0]
jobs.clear()
state["disabled"] = True
return [html.P(f"{state['jobs']}")], state["disabled"]
else:
return [html.P(state['jobs'])], state["disabled"]
@app.callback(Output("live-prediction", "children"),
[Input("interval-heatmap", "n_intervals")])
def update_pred(n):
if state["process_prediction"] == 0:
if not process_job.empty():
model_xgboost = pickle.load(open("xgboost_model.sav", 'rb'))
model_svm = pickle.load(open("svm_model.sav", 'rb'))
dataset = process_job.get()
state["prediction"] = [list(model_svm.predict(dataset))]
state["prediction"].append(list(model_xgboost.predict(dataset)))
state["labels_svm"] = list(set(state["prediction"][0]))
state["values_svm"] = [state["prediction"][0].count(x) for x in state["labels_svm"]]
state["labels_xg"] = list(set(state["prediction"][1]))
state["values_xg"] = [state["prediction"][1].count(x) for x in state["labels_xg"]]
state["process_prediction"] = 1
return True
else:
return False
return False
@app.callback(Output("live-graph", "figure"),
[Input("interval-component", "n_intervals")])
def update_graph_scatter(n):
try:
df = pd.read_csv("data.csv")
rawEeg = df["rawEeg"]
X = np.arange(0, rawEeg.size, 1)/128
Y = df["rawEeg"]
fig = go.Figure(go.Scatter(
x=X,
y=Y,
name="Scatter",
mode="lines",
line={"color": "#20c997"}
))
fig.update_layout(title="Voltage versus time plot",
paper_bgcolor="#dbe9f0")
fig.update_yaxes(title_text="Voltage, mV")
fig.update_xaxes(title_text="Time, s")
return fig
except Exception as e:
with open("errors.txt","a") as f:
f.write(str(e))
f.write("\n")
@app.callback(Output("live-heatmap", "figure"),
[Input("interval-heatmap", "n_intervals")])
def update_heatmap(n):
if state["process_extraction"] != 0:
data_plot = pickle.load(open("data_plot", "rb"))
fig = go.Figure()
fig.set_subplots(rows=2, cols=2, horizontal_spacing=0.1)
fig.add_trace(
go.Heatmap(z=data_plot[0], showscale=False, colorscale = 'Viridis'),
row=1, col=1
)
fig.add_trace(
go.Heatmap(z=data_plot[1], showscale=False, colorscale = 'Viridis'),
row=1, col=2
)
fig.add_trace(
go.Heatmap(z=data_plot[2], showscale=False, colorscale = 'Viridis'),
row=2, col=1
)
fig.add_trace(
go.Heatmap(z=data_plot[3], colorscale = 'Viridis'),
row=2, col=2
)
fig.update_layout(title="Spectrogram of a signal",
paper_bgcolor="#dbe9f0")
return fig
else:
fig = go.Figure()
fig.update_layout(title="Spectrogram of a signal",
paper_bgcolor="#dbe9f0")
return fig
@app.callback(Output("live-svm", "figure"),
[Input("interval-stats", "n_intervals")])
def update_svm(n):
if state["process_extraction"] != 0:
fig = go.Figure(
go.Pie(
labels=state["labels_svm"],
values=state["values_svm"]
)
)
fig.update_layout(title="SVM Prediction",
paper_bgcolor="#dbe9f0")
return fig
else:
fig = go.Figure()
fig.update_layout(title="SVM Prediction",
paper_bgcolor="#dbe9f0")
return fig
@app.callback(Output("live-xgboost", "figure"),
[Input("interval-stats", "n_intervals")])
def update_xgboost(n):
if state["process_extraction"] != 0:
fig = go.Figure(
go.Pie(
labels=state["labels_xg"],
values=state["values_xg"]
)
)
fig.update_layout(title="XGBoost Prediction",
paper_bgcolor="#dbe9f0")
return fig
else:
fig = go.Figure()
fig.update_layout(title="XGBoost Prediction",
paper_bgcolor="#dbe9f0")
return fig
@app.callback(Output("live-percentage", "figure"),
[Input("interval-stats", "n_intervals")])
def update_percentage(n):
if state["process_extraction"] != 0:
fig = go.Figure(
# go.Pie(
# )
)
fig.update_layout(title="Total statistics",
paper_bgcolor="#dbe9f0")
return fig
else:
fig = go.Figure()
fig.update_layout(title="Total statistics",
paper_bgcolor="#dbe9f0")
return fig
if __name__ == "__main__":
app.run_server(debug=False)