-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathExperiment.js
158 lines (135 loc) · 4.72 KB
/
Experiment.js
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
"use strict";
/*
This script encodes the experiement
*/
var Experiment = (function(){
// experiment settings:
const _settings = {
faceDetectedThreshold: 0.5, // between 0 (easy detection) and 1 (hard detection)
nIterations: 25, // number of iterations black -> white
delay: 2000, // delay between 2 luminosity changes in ms
resamplePeriod: 20 // used for measures time resampling (we need to resample the time to average values). In ms
};
// private vars:
let _domButton = null, _domScreen = null;
let _isRunning = false, _cyclesCounter = 0, _detectedState = null;
// private funcs:
function cycle(){
if (!_isRunning) return;
if (_cyclesCounter === 2*_settings.nIterations){ // experience is over
complete();
return;
}
if (_cyclesCounter%2 === 0){ // black screen
_domScreen.style.backgroundColor = 'black';
ExperimentRecorder.addEvent('BLACK');
} else { // white screen
_domScreen.style.backgroundColor = 'white';
ExperimentRecorder.addEvent('WHITE');
}
++_cyclesCounter;
setTimeout(cycle, _settings.delay);
}
function setCSSdisplay(domId, val){
const domElt = document.getElementById(domId);
domElt.style.display = val;
}
function complete(){ // experience is complete (not aborted or canceled)
console.log('INFO in Experiment.js: experiment is complete :)');
that.stop();
ExperimentRecorder.plot(); // trace RAW RESULTS
// compute and trace AVG RESULTS:
const groupedValues = ExperimentRecorder.group_byEventLabels(['WHITE', 'BLACK']);
const avgs = {};
['WHITE', 'BLACK'].forEach(function(groupLabel){
groupedValues[groupLabel] = groupedValues[groupLabel].map(function(sample){
ExperimentRecorder.filter_hampel(sample, 0.5, 2);
const sampleNormalized = ExperimentRecorder.normalize_byFirstValue(sample);
return ExperimentRecorder.resample(sampleNormalized, _settings.delay, _settings.resamplePeriod);
});
const averageValues = ExperimentRecorder.average_resampleds(groupedValues[groupLabel]);
avgs[groupLabel] = averageValues;
});
// plot average:
ExperimentRecorder.plot_averages(avgs);
// Some CSS & UI stuffs:
TabManager.open('tabLink-results', 'tabContent-results');
setCSSdisplay('results-noResults', 'none');
setCSSdisplay('results-caption', 'block');
setCSSdisplay('results-plot', 'inline-block');
setCSSdisplay('resultsAvg-noResults', 'none');
setCSSdisplay('resultsAvg-caption', 'block');
setCSSdisplay('resultsAvg-plot', 'inline-block');
}
function addValue(){
ExperimentRecorder.addValue({
pupilLeftRadius: _detectedState.pupilLeftRadius,
pupilRightRadius: _detectedState.pupilRightRadius
});
}
function callbackTrack(detectedState){
_detectedState = detectedState;
if (!_isRunning){
return;
}
const isFaceDetected = (detectedState.detected>_settings.faceDetectedThreshold);
if (0 && !isFaceDetected){
that.stop();
alert('ERROR: the face is not detected. Please take a look in the debug view. The experiment has been aborted.');
return;
}
addValue();
return;
}
// public methods:
const that = {
init: function(){ // entry point. Called by body onload method
// initialize Jeeliz pupillometry:
JEELIZPUPILLOMETRY.init({
canvasId: 'jeePupilCanvas',
NNCPath: '../../dist/',
callbackReady: function(err){
if (err){
console.log('AN ERROR HAPPENS. ERR =', err);
return;
}
console.log('INFO: JEELIZPUPILLOMETRY IS READY');
},
callbackTrack: callbackTrack
});
_domButton = document.getElementById('experiment-stopStartButton');
_domScreen = document.getElementById('experiment-screen');
},
toggle: function(){
if (_isRunning){
that.stop();
} else {
that.start();
}
},
start: function(){
if (_isRunning){
console.log('WARNING in Experiment.js - start(): the experiment is running. Stop it before running this method.');
return;
}
_isRunning = true;
_domButton.innerHTML = 'STOP THE EXPERIMENT';
_domScreen.style.display = 'block';
_cyclesCounter = 0;
ExperimentRecorder.start();
addValue(); // add the first value
cycle();
},
stop: function(){
if (!_isRunning){
console.log('WARNING in Experiment.js - stop(): the experiment is not running. Start it before running this method.');
return;
}
_isRunning = false;
_domButton.innerHTML = 'START THE EXPERIMENT';
_domScreen.style.display = 'none';
ExperimentRecorder.end();
}
} //end that
return that;
})();