/
W_Focus.pde
572 lines (480 loc) · 20.7 KB
/
W_Focus.pde
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
////////////////////////////////////////////////////
// //
// W_focus.pde (ie "Focus Widget") //
// Enums can be found in FocusEnums.pde //
// //
// //
// Created by: Richard Waltman, March 2021 //
// //
////////////////////////////////////////////////////
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import brainflow.BoardIds;
import brainflow.BoardShim;
import brainflow.BrainFlowClassifiers;
import brainflow.BrainFlowInputParams;
import brainflow.BrainFlowMetrics;
import brainflow.BrainFlowModelParams;
import brainflow.DataFilter;
import brainflow.LogLevels;
import brainflow.MLModel;
class W_Focus extends Widget {
//to see all core variables/methods of the Widget class, refer to Widget.pde
//put your custom variables here...
//private ControlP5 focus_cp5;
//private Button widgetTemplateButton;
private ChannelSelect focusChanSelect;
private boolean prevChanSelectIsVisible = false;
private AuditoryNeurofeedback auditoryNeurofeedback;
private Grid dataGrid;
private final int NUM_TABLE_ROWS = 6;
private final int NUM_TABLE_COLUMNS = 2;
//private final int TABLE_WIDTH = 142;
private int tableHeight = 0;
private int cellHeight = 10;
private DecimalFormat df = new DecimalFormat("#.0000");
private final int PAD_FIVE = 5;
private final int PAD_TWO = 2;
private final int METRIC_DROPDOWN_W = 100;
private final int CLASSIFIER_DROPDOWN_W = 80;
private FocusBar focusBar;
private float focusBarHardYAxisLimit = 1.05f; //Provide slight "breathing room" to avoid GPlot error when metric value == 1.0
private FocusXLim xLimit = FocusXLim.TEN;
private FocusMetric focusMetric = FocusMetric.RELAXATION;
private FocusClassifier focusClassifier = FocusClassifier.REGRESSION;
private FocusThreshold focusThreshold = FocusThreshold.EIGHT_TENTHS;
private FocusColors focusColors = FocusColors.GREEN;
private int[] exgChannels;
private int channelCount;
private double[][] dataArray;
private MLModel mlModel;
private double metricPrediction = 0d;
private boolean predictionExceedsThreshold = false;
private float xc, yc, wc, hc; // status circle center xy, width and height
private int graphX, graphY, graphW, graphH;
private final int GRAPH_PADDING = 30;
private color cBack, cDark, cMark, cFocus, cWave, cPanel;
List<controlP5.Controller> cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
W_Focus(PApplet _parent) {
super(_parent); //calls the parent CONSTRUCTOR method of Widget (DON'T REMOVE)
//Add channel select dropdown to this widget
focusChanSelect = new ChannelSelect(pApplet, this, x, y, w, navH, "FocusChannelSelect");
focusChanSelect.activateAllButtons();
cp5ElementsToCheck.addAll(focusChanSelect.getCp5ElementsForOverlapCheck());
auditoryNeurofeedback = new AuditoryNeurofeedback(x + PAD_FIVE, y + PAD_FIVE, w/2 - PAD_FIVE*2, navBarHeight/2);
cp5ElementsToCheck.add((controlP5.Controller)auditoryNeurofeedback.startStopButton);
cp5ElementsToCheck.add((controlP5.Controller)auditoryNeurofeedback.modeButton);
exgChannels = currentBoard.getEXGChannels();
channelCount = currentBoard.getNumEXGChannels();
dataArray = new double[channelCount][];
// initialize graphics parameters
onColorChange();
//This is the protocol for setting up dropdowns.
dropdownWidth = 60; //Override the default dropdown width for this widget
addDropdown("focusMetricDropdown", "Metric", focusMetric.getEnumStringsAsList(), focusMetric.getIndex());
addDropdown("focusClassifierDropdown", "Classifier", focusClassifier.getEnumStringsAsList(), focusClassifier.getIndex());
addDropdown("focusThresholdDropdown", "Threshold", focusThreshold.getEnumStringsAsList(), focusThreshold.getIndex());
addDropdown("focusWindowDropdown", "Window", xLimit.getEnumStringsAsList(), xLimit.getIndex());
//Create data table
dataGrid = new Grid(NUM_TABLE_ROWS, NUM_TABLE_COLUMNS, cellHeight);
dataGrid.setTableFontAndSize(p5, 12);
dataGrid.setDrawTableBorder(true);
dataGrid.setString("Metric Value", 0, 0);
dataGrid.setString("Delta (1.5-4Hz)", 1, 0);
dataGrid.setString("Theta (4-8Hz)", 2, 0);
dataGrid.setString("Alpha (7.5-13Hz)", 3, 0);
dataGrid.setString("Beta (13-30Hz)", 4, 0);
dataGrid.setString("Gamma (30-45Hz)", 5, 0);
//Instantiate local cp5 for this box. This allows extra control of drawing cp5 elements specifically inside this class.
//focus_cp5 = new ControlP5(ourApplet);
//focus_cp5.setGraphics(ourApplet, 0,0);
//focus_cp5.setAutoDraw(false);
//create our focus graph
updateGraphDims();
focusBar = new FocusBar(_parent, xLimit.getValue(), focusBarHardYAxisLimit, graphX, graphY, graphW, graphH);
initBrainFlowMetric();
}
public void update() {
super.update(); //calls the parent update() method of Widget (DON'T REMOVE)
//Update channel checkboxes and active channels
focusChanSelect.update(x, y, w);
//Flex the Gplot graph when channel select dropdown is open/closed
if (focusChanSelect.isVisible() != prevChanSelectIsVisible) {
channelSelectFlexWidgetUI();
prevChanSelectIsVisible = focusChanSelect.isVisible();
}
if (currentBoard.isStreaming()) {
dataGrid.setString(df.format(metricPrediction), 0, 1);
focusBar.update(metricPrediction);
}
lockElementsOnOverlapCheck(cp5ElementsToCheck);
}
public void draw() {
super.draw(); //calls the parent draw() method of Widget (DON'T REMOVE)
//remember to refer to x,y,w,h which are the positioning variables of the Widget class
//Draw data table
dataGrid.draw();
drawStatusCircle();
if (false) {
//Draw some guides to help develop this widget faster
pushStyle();
stroke(OPENBCI_DARKBLUE);
//Main guides
line(x, y+(h/2), x+w, y+(h/2));
line(x+(w/2), y, x+(w/2), y+(h/2));
//Top left container center
line(x+(w/4), y, x+(w/4), y+(h/2));
line(x, y+(h/4), x+(w/2), y+(h/4));
popStyle();
}
//This draws all cp5 objects in the local instance
//focus_cp5.draw();
auditoryNeurofeedback.draw();
//Draw the graph
focusBar.draw();
focusChanSelect.draw();
}
public void screenResized() {
super.screenResized(); //calls the parent screenResized() method of Widget (DON'T REMOVE)
//Very important to allow users to interact with objects after app resize
//focus_cp5.setGraphics(ourApplet, 0, 0);
resizeTable();
//We need to set the position of our Cp5 object after the screen is resized
//widgetTemplateButton.setPosition(x + w/2 - widgetTemplateButton.getWidth()/2, y + h/2 - widgetTemplateButton.getHeight()/2);
updateStatusCircle();
updateAuditoryNeurofeedbackPosition();
updateGraphDims();
focusBar.screenResized(graphX, graphY, graphW, graphH);
focusChanSelect.screenResized(pApplet);
//Custom resize these dropdowns due to longer text strings as options
cp5_widget.get(ScrollableList.class, "focusMetricDropdown").setWidth(METRIC_DROPDOWN_W);
cp5_widget.get(ScrollableList.class, "focusMetricDropdown").setPosition(
x0 + w0 - (dropdownWidth*2) - METRIC_DROPDOWN_W - CLASSIFIER_DROPDOWN_W - (PAD_TWO*4),
navH + y0 + PAD_TWO
);
cp5_widget.get(ScrollableList.class, "focusClassifierDropdown").setWidth(CLASSIFIER_DROPDOWN_W);
cp5_widget.get(ScrollableList.class, "focusClassifierDropdown").setPosition(
x0 + w0 - (dropdownWidth*2) - CLASSIFIER_DROPDOWN_W - (PAD_TWO*3),
navH + y0 + PAD_TWO
);
}
void mousePressed() {
super.mousePressed(); //calls the parent mousePressed() method of Widget (DON'T REMOVE)
focusChanSelect.mousePressed(this.dropdownIsActive); //Calls channel select mousePressed and checks if clicked
}
private void resizeTable() {
int extraPadding = focusChanSelect.isVisible() ? navHeight : 0;
float upperLeftContainerW = w/2;
float upperLeftContainerH = h/2;
//float min = min(upperLeftContainerW, upperLeftContainerH);
int tx = x + int(upperLeftContainerW);
int ty = y + PAD_FIVE + extraPadding;
int tw = int(upperLeftContainerW) - PAD_FIVE*2;
//tableHeight = tw;
dataGrid.setDim(tx, ty, tw);
dataGrid.setTableHeight(int(upperLeftContainerH - PAD_FIVE*2));
dataGrid.dynamicallySetTextVerticalPadding(0, 0);
dataGrid.setHorizontalCenterTextInCells(true);
}
private void updateAuditoryNeurofeedbackPosition() {
int extraPadding = focusChanSelect.isVisible() ? navHeight : 0;
int subContainerMiddleX = x + w/4;
auditoryNeurofeedback.screenResized(subContainerMiddleX, (int)(y + h/2 - navHeight + extraPadding), w/2 - PAD_FIVE*2, navBarHeight/2);
}
private void updateStatusCircle() {
float upperLeftContainerW = w/2;
float upperLeftContainerH = h/2;
float min = min(upperLeftContainerW, upperLeftContainerH);
xc = x + w/4;
yc = y + h/4 - navHeight;
wc = min * (3f/5);
hc = wc;
}
private void updateGraphDims() {
graphW = int(w - PAD_FIVE*4);
graphH = int(h/2 - GRAPH_PADDING - PAD_FIVE*2);
graphX = x + PAD_FIVE*2;
graphY = int(y + h/2);
}
//Core method to fetch and process data
//Returns a metric value from 0. to 1. When there is an error, returns -1.
private double updateFocusState() {
try {
int windowSize = currentBoard.getSampleRate() * xLimit.getValue();
// getData in GUI returns data in shape ndatapoints x nchannels, in BrainFlow its transposed
List<double[]> currentData = currentBoard.getData(windowSize);
if (currentData.size() != windowSize || focusChanSelect.activeChan.size() <= 0) {
return -1.0;
}
for (int i = 0; i < channelCount; i++) {
dataArray[i] = new double[windowSize];
for (int j = 0; j < currentData.size(); j++) {
dataArray[i][j] = currentData.get(j)[exgChannels[i]];
}
}
int[] channelsInDataArray = ArrayUtils.toPrimitive(
focusChanSelect.activeChan.toArray(
new Integer[focusChanSelect.activeChan.size()]
));
//Full Source Code for this method: https://github.com/brainflow-dev/brainflow/blob/c5f0ad86683e6eab556e30965befb7c93e389a3b/src/data_handler/data_handler.cpp#L1115
Pair<double[], double[]> bands = DataFilter.get_avg_band_powers (dataArray, channelsInDataArray, currentBoard.getSampleRate(), true);
double[] featureVector = bands.getLeft ();
//Left array is Averages, right array is Standard Deviations. Update values using Averages.
updateBandPowerTableValues(bands.getLeft());
//Keep this here
double prediction = mlModel.predict(featureVector)[0];
//println("Concentration: " + prediction);
//Send band power and prediction data to AuditoryNeurofeedback class
auditoryNeurofeedback.update(bands.getLeft(), (float)prediction);
return prediction;
} catch (BrainFlowError e) {
e.printStackTrace();
println("Error updating focus state!");
return -1d;
}
}
private void updateBandPowerTableValues(double[] bandPowers) {
for (int i = 0; i < bandPowers.length; i++) {
dataGrid.setString(df.format(bandPowers[i]), 1 + i, 1);
}
}
private void drawStatusCircle() {
color fillColor;
color strokeColor;
StringBuilder sb = new StringBuilder("");
if (predictionExceedsThreshold) {
fillColor = cFocus;
strokeColor = cFocus;
} else {
fillColor = cDark;
strokeColor = cDark;
sb.append("Not ");
}
sb.append(focusMetric.getIdealStateString());
//Draw status graphic
pushStyle();
noStroke();
fill(fillColor);
stroke(strokeColor);
ellipseMode(CENTER);
ellipse(xc, yc, wc, hc);
noStroke();
textAlign(CENTER);
text(sb.toString(), xc, yc + hc/2 + 16);
popStyle();
}
private void initBrainFlowMetric() {
BrainFlowModelParams modelParams = new BrainFlowModelParams(
focusMetric.getMetric().get_code(),
focusClassifier.getClassifier().get_code()
);
mlModel = new MLModel (modelParams);
try {
mlModel.prepare();
} catch (BrainFlowError e) {
e.printStackTrace();
}
}
//Called on haltSystem() when GUI exits or session stops
public void endSession() {
try {
mlModel.release();
} catch (BrainFlowError e) {
e.printStackTrace();
}
}
private void onColorChange() {
switch(focusColors) {
case GREEN:
cBack = #ffffff; //white
cDark = #3068a6; //medium/dark blue
cMark = #4d91d9; //lighter blue
cFocus = #b8dc69; //theme green
cWave = #ffdd3a; //yellow
cPanel = #f5f5f5; //little grey
break;
case ORANGE:
cBack = #ffffff; //white
cDark = #377bc4; //medium/dark blue
cMark = #5e9ee2; //lighter blue
cFocus = #fcce51; //orange
cWave = #ffdd3a; //yellow
cPanel = #f5f5f5; //little grey
break;
case CYAN:
cBack = #ffffff; //white
cDark = #377bc4; //medium/dark blue
cMark = #5e9ee2; //lighter blue
cFocus = #91f4fc; //cyan
cWave = #ffdd3a; //yellow
cPanel = #f5f5f5; //little grey
break;
}
}
void channelSelectFlexWidgetUI() {
focusBar.setPlotPosAndOuterDim(focusChanSelect.isVisible());
int factor = focusChanSelect.isVisible() ? 1 : -1;
yc += navHeight * factor;
resizeTable();
updateAuditoryNeurofeedbackPosition();
}
public void setFocusHorizScale(int n) {
xLimit = xLimit.values()[n];
focusBar.adjustTimeAxis(xLimit.getValue());
}
public void setMetric(int n) {
focusMetric = focusMetric.values()[n];
endSession();
initBrainFlowMetric();
}
public void setClassifier(int n) {
focusClassifier = focusClassifier.values()[n];
endSession();
initBrainFlowMetric();
}
public void setThreshold(int n) {
focusThreshold = focusThreshold.values()[n];
}
public int getMetricExceedsThreshold() {
return predictionExceedsThreshold ? 1 : 0;
}
public void killAuditoryFeedback() {
auditoryNeurofeedback.killAudio();
}
//Called in DataProcessing.pde to update data even if widget is closed
public void updateFocusWidgetData() {
metricPrediction = updateFocusState();
predictionExceedsThreshold = metricPrediction > focusThreshold.getValue();
}
}; //end of class
//The following global functions are used by the Focus widget dropdowns. This method is the least amount of code.
public void focusWindowDropdown(int n) {
w_focus.setFocusHorizScale(n);
}
public void focusMetricDropdown(int n) {
w_focus.setMetric(n);
}
public void focusClassifierDropdown(int n) {
w_focus.setClassifier(n);
}
public void focusThresholdDropdown(int n) {
w_focus.setThreshold(n);
}
//This class contains the time series plot for the focus metric over time
class FocusBar {
int x, y, w, h;
int focusBarPadding = 30;
int xOffset;
final int nPoints = 30 * 1000;
GPlot plot; //the actual grafica-based GPlot that will be rendering the Time Series trace
LinkedList<Float> fifoList;
LinkedList<Float> fifoTimeList;
int numSeconds;
color channelColor; //color of plot trace
FocusBar(PApplet _parent, int xLimit, float yLimit, int _x, int _y, int _w, int _h) { //channel number, x/y location, height, width
x = _x;
y = _y;
w = _w;
h = _h;
if (eegDataSource == DATASOURCE_CYTON) {
xOffset = 22;
} else {
xOffset = 0;
}
numSeconds = xLimit;
plot = new GPlot(_parent);
plot.setPos(x + 36 + 4 + xOffset, y); //match Accelerometer plot position with Time Series
plot.setDim(w - 36 - 4 - xOffset, h);
plot.setMar(0f, 0f, 0f, 0f);
plot.setLineColor((int)channelColors[(NUM_ACCEL_DIMS)%8]);
plot.setXLim(-numSeconds,0); //set the horizontal scale
plot.setYLim(0, yLimit); //change this to adjust vertical scale
//plot.setPointSize(2);
plot.setPointColor(0);
plot.getXAxis().setAxisLabelText("Time (s)");
plot.getYAxis().setAxisLabelText("Metric Value");
plot.setAllFontProperties("Arial", 0, 14);
plot.getXAxis().getAxisLabel().setOffset(float(22));
plot.getYAxis().getAxisLabel().setOffset(float(focusBarPadding));
plot.getXAxis().setFontColor(OPENBCI_DARKBLUE);
plot.getXAxis().setLineColor(OPENBCI_DARKBLUE);
plot.getXAxis().getAxisLabel().setFontColor(OPENBCI_DARKBLUE);
plot.getYAxis().setFontColor(OPENBCI_DARKBLUE);
plot.getYAxis().setLineColor(OPENBCI_DARKBLUE);
plot.getYAxis().getAxisLabel().setFontColor(OPENBCI_DARKBLUE);
adjustTimeAxis(numSeconds);
initArrays();
//set the plot points for X, Y, and Z axes
plot.addLayer("layer 1", new GPointsArray(30));
plot.getLayer("layer 1").setLineColor(ACCEL_X_COLOR);
}
private void initArrays() {
fifoList = new LinkedList<Float>();
fifoTimeList = new LinkedList<Float>();
for (int i = 0; i < nPoints; i++) {
fifoList.add(0f);
fifoTimeList.add(0f);
}
}
public void update(double val) {
updateGPlotPoints(val);
}
public void draw() {
plot.beginDraw();
plot.drawBox(); //we won't draw this eventually ...
plot.drawGridLines(GPlot.BOTH);
plot.drawLines(); //Draw a Line graph!
//plot.drawPoints(); //Used to draw Points instead of Lines
plot.drawYAxis();
plot.drawXAxis();
plot.getXAxis().draw();
plot.endDraw();
}
public void adjustTimeAxis(int _newTimeSize) {
numSeconds = _newTimeSize;
plot.setXLim(-_newTimeSize,0);
initArrays();
//Set the number of axis divisions...
if (_newTimeSize > 1) {
plot.getXAxis().setNTicks(_newTimeSize);
}else{
plot.getXAxis().setNTicks(10);
}
}
//Used to update the Points within the graph
private void updateGPlotPoints(double val) {
float timerVal = (float)millis() / 1000.0;
fifoTimeList.removeFirst();
fifoTimeList.addLast(timerVal);
fifoList.removeFirst();
fifoList.addLast((float)val);
int stopId = 0;
for (stopId = nPoints - 1; stopId > 0; stopId--) {
if (timerVal - fifoTimeList.get(stopId) > numSeconds) {
break;
}
}
int size = nPoints - 1 - stopId;
GPointsArray focusPoints = new GPointsArray(size);
for (int i = 0; i < size; i++) {
focusPoints.set(i, fifoTimeList.get(i + stopId) - timerVal, fifoList.get(i + stopId), "");
}
plot.setPoints(focusPoints, "layer 1");
}
public void screenResized(int _x, int _y, int _w, int _h) {
x = _x;
y = _y;
w = _w;
h = _h;
//reposition & resize the plot
plot.setPos(x + 36 + 4 + xOffset, y);
plot.setDim(w - 36 - 4 - xOffset, h);
}
public void setPlotPosAndOuterDim(boolean chanSelectIsVisible) {
int _y = chanSelectIsVisible ? y + 22 : y;
int _h = chanSelectIsVisible ? h - 22 : h;
//reposition & resize the plot
plot.setPos(x + 36 + 4 + xOffset, _y);
plot.setDim(w - 36 - 4 - xOffset, _h);
}
}; //end of class