Debian Codegraph @

EEG/ERPWindow.cpp

raw symbols SCIP
1 /* ERPWindow.cpp
2 *
3 * Copyright (C) 2012-2022 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ERPWindow.h"
20 #include "EditorM.h"
21 #include "Preferences.h"
22
23 Thing_implement (ERPWindow, FunctionEditor, 0);
24
25 #include "Prefs_define.h"
26 #include "ERPWindow_prefs.h"
27 #include "Prefs_install.h"
28 #include "ERPWindow_prefs.h"
29 #include "Prefs_copyToInstance.h"
30 #include "ERPWindow_prefs.h"
31
32 typedef struct { int inclination, azimuth; double topX, topY; } BiosemiLocationData;
33
34 static BiosemiLocationData biosemiCapCoordinates64 [1+64] =
35 {
36 /*
37 * BioSemi says:
38 * "Spherical coordinates in degrees,
39 * by inclination (from Cz, pos is right hemisphere, neg is left hemisphere),
40 * and azimuth (from T7 for left hemisphere, and from T8 for the right hemisphere, pos is anti-clockwise, neg is clockwise)"
41 */
42 {0,0,0.0,0.0},
43 { -92, -72, 0.0, 0.0 }, // 1 Fp1
44 { -92, -54, 0.0, 0.0 }, // 2 AF7
45 { -74, -65, 0.0, 0.0 }, // 3 AF3
46 { -50, -68, 0.0, 0.0 }, // 4 F1
47 { -60, -51, 0.0, 0.0 }, // 5 F3
48 { -75, -41, 0.0, 0.0 }, // 6 F5
49 { -92, -36, 0.0, 0.0 }, // 7 F7
50 { -92, -18, 0.0, 0.0 }, // 8 FT7
51 { -72, -21, 0.0, 0.0 }, // 9 FC5
52 { -50, -28, 0.0, 0.0 }, // 10 FC3
53 { -32, -45, 0.0, 0.0 }, // 11 FC1
54 { -23, 0, 0.0, 0.0 }, // 12 C1
55 { -46, 0, 0.0, 0.0 }, // 13 C3
56 { -69, 0, 0.0, 0.0 }, // 14 C5
57 { -92, 0, 0.0, 0.0 }, // 15 T7
58 { -92, 18, 0.0, 0.0 }, // 16 TP7
59 { -72, 21, 0.0, 0.0 }, // 17 CP5
60 { -50, 28, 0.0, 0.0 }, // 18 CP3
61 { -32, 45, 0.0, 0.0 }, // 19 CP1
62 { -50, 68, 0.0, 0.0 }, // 20 P1
63 { -60, 51, 0.0, 0.0 }, // 21 P3
64 { -75, 41, 0.0, 0.0 }, // 22 P5
65 { -92, 36, 0.0, 0.0 }, // 23 P7
66 {-115, 40, 0.0, 0.0 }, // 24 P9
67 { -92, 54, 0.0, 0.0 }, // 25 PO7
68 { -74, 65, 0.0, 0.0 }, // 26 PO3
69 { -92, 72, 0.0, 0.0 }, // 27 O1
70 { 115, -90, 0.0, 0.0 }, // 28 Iz
71 { 92, -90, 0.0, 0.0 }, // 29 Oz
72 { 69, -90, 0.0, 0.0 }, // 30 POz
73 { 46, -90, 0.0, 0.0 }, // 31 Pz
74 { 23, -90, 0.0, 0.0 }, // 32 CPz
75 { 92, 90, 0.0, 0.0 }, // 33 Fpz
76 { 92, 72, 0.0, 0.0 }, // 34 Fp2
77 { 92, 54, 0.0, 0.0 }, // 35 AF8
78 { 74, 65, 0.0, 0.0 }, // 36 AF4
79 { 69, 90, 0.0, 0.0 }, // 37 AFz
80 { 46, 90, 0.0, 0.0 }, // 38 Fz
81 { 50, 68, 0.0, 0.0 }, // 39 F2
82 { 60, 51, 0.0, 0.0 }, // 40 F4
83 { 75, 41, 0.0, 0.0 }, // 41 F6
84 { 92, 36, 0.0, 0.0 }, // 42 F8
85 { 92, 18, 0.0, 0.0 }, // 43 FT8
86 { 72, 21, 0.0, 0.0 }, // 44 FC6
87 { 50, 28, 0.0, 0.0 }, // 45 FC4
88 { 32, 45, 0.0, 0.0 }, // 46 FC2
89 { 23, 90, 0.0, 0.0 }, // 47 FCz
90 { 0, 0, 0.0, 0.0 }, // 48 Cz
91 { 23, 0, 0.0, 0.0 }, // 49 C2
92 { 46, 0, 0.0, 0.0 }, // 50 C4
93 { 69, 0, 0.0, 0.0 }, // 51 C6
94 { 92, 0, 0.0, 0.0 }, // 52 T8
95 { 92, -18, 0.0, 0.0 }, // 53 TP8
96 { 72, -21, 0.0, 0.0 }, // 54 CP6
97 { 50, -28, 0.0, 0.0 }, // 55 CP4
98 { 32, -45, 0.0, 0.0 }, // 56 CP2
99 { 50, -68, 0.0, 0.0 }, // 57 P2
100 { 60, -51, 0.0, 0.0 }, // 58 P4
101 { 75, -41, 0.0, 0.0 }, // 59 P6
102 { 92, -36, 0.0, 0.0 }, // 60 P8
103 { 115, -40, 0.0, 0.0 }, // 61 P10
104 { 92, -54, 0.0, 0.0 }, // 62 PO8
105 { 74, -65, 0.0, 0.0 }, // 63 PO4
106 { 92, -72, 0.0, 0.0 } // 64 O2
107 };
108
109 static BiosemiLocationData biosemiCapCoordinates32 [1+32] =
110 {
111 /*
112 * BioSemi says:
113 * "Spherical coordinates in degrees,
114 * by inclination (from Cz, pos is right hemisphere, neg is left hemisphere),
115 * and azimuth (from T7 for left hemisphere, and from T8 for the right hemisphere, pos is anti-clockwise, neg is clockwise)"
116 */
117 {0,0,0.0,0.0},
118 { -92, -72, 0.0, 0.0 }, // 1 Fp1
119 { -74, -65, 0.0, 0.0 }, // 2 AF3
120 { -92, -36, 0.0, 0.0 }, // 3 F7
121 { -60, -51, 0.0, 0.0 }, // 4 F3
122 { -32, -45, 0.0, 0.0 }, // 5 FC1
123 { -72, -21, 0.0, 0.0 }, // 6 FC5
124 { -92, 0, 0.0, 0.0 }, // 7 T7
125 { -46, 0, 0.0, 0.0 }, // 8 C3
126 { -32, 45, 0.0, 0.0 }, // 9 CP1
127 { -72, 21, 0.0, 0.0 }, // 10 CP5
128 { -92, 36, 0.0, 0.0 }, // 11 P7
129 { -60, 51, 0.0, 0.0 }, // 12 P3
130 { 46, -90, 0.0, 0.0 }, // 13 Pz
131 { -74, 65, 0.0, 0.0 }, // 14 PO3
132 { -92, 72, 0.0, 0.0 }, // 15 O1
133 { 92, -90, 0.0, 0.0 }, // 16 Oz
134 { 92, -72, 0.0, 0.0 }, // 17 O2
135 { 74, -65, 0.0, 0.0 }, // 18 PO4
136 { 60, -51, 0.0, 0.0 }, // 19 P4
137 { 92, -36, 0.0, 0.0 }, // 20 P8
138 { 72, -21, 0.0, 0.0 }, // 21 CP6
139 { 32, -45, 0.0, 0.0 }, // 22 CP2
140 { 46, 0, 0.0, 0.0 }, // 23 C4
141 { 92, 0, 0.0, 0.0 }, // 24 T8
142 { 72, 21, 0.0, 0.0 }, // 25 FC6
143 { 32, 45, 0.0, 0.0 }, // 26 FC2
144 { 60, 51, 0.0, 0.0 }, // 27 F4
145 { 92, 36, 0.0, 0.0 }, // 28 F8
146 { 74, 65, 0.0, 0.0 }, // 29 AF4
147 { 92, 72, 0.0, 0.0 }, // 30 Fp2
148 { 46, 90, 0.0, 0.0 }, // 31 Fz
149 { 0, 0, 0.0, 0.0 }, // 32 Cz
150 };
151
152 void ERP_drawScalp_garnish (Graphics graphics, double vmin, double vmax, enum kGraphics_colourScale colourScale) {
153 const integer n = 201;
154 autoMAT legend = raw_MAT (n, 2);
155 for (integer irow = 1; irow <= n; irow ++)
156 for (integer icol = 1; icol <= 2; icol ++)
157 legend [irow] [icol] = (irow - 1) / (n - 1.0);
158 Graphics_setColourScale (graphics, colourScale);
159 Graphics_image (graphics, legend.all(), 0.85, 0.98, -0.8, +0.8, 0.0, 1.0);
160 Graphics_setColourScale (graphics, kGraphics_colourScale::GREY);
161 Graphics_rectangle (graphics, 0.85, 0.98, -0.8, +0.8);
162 Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_TOP);
163 Graphics_text (graphics, 1.0, -0.8, vmin * 1e6, U" μV");
164 Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_BOTTOM);
165 Graphics_text (graphics, 1.0, +0.8, vmax * 1e6, U" μV");
166 }
167
168 void ERP_drawScalp (ERP me, Graphics graphics, double tmin, double tmax, double vmin, double vmax, enum kGraphics_colourScale colourScale, bool garnish) {
169 Graphics_setInner (graphics);
170 Graphics_setWindow (graphics, -1.0, 1.0, -1.0, 1.0);
171 //Graphics_setGrey (graphics, 1.0);
172 //Graphics_fillRectangle (graphics, -1.1, 1.1, -1.01, 1.19);
173 //Graphics_setColour (graphics, Melder_BLACK);
174 const integer numberOfDrawableChannels =
175 my ny >= 64 && Melder_equ (my channelNames [64].get(), U"O2") ? 64 :
176 my ny >= 32 && Melder_equ (my channelNames [32].get(), U"Cz") ? 32 :
177 0;
178 BiosemiLocationData *biosemiLocationData = numberOfDrawableChannels == 64 ? biosemiCapCoordinates64 : numberOfDrawableChannels == 32 ? biosemiCapCoordinates32 : 0;
179 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++) {
180 const double inclination = (double) biosemiLocationData [ichan]. inclination;
181 const double azimuth = (double) biosemiLocationData [ichan]. azimuth;
182 const bool rightHemisphere = ( inclination >= 0.0 );
183 const double r = fabs (inclination / 115.0);
184 const double theta = rightHemisphere ? azimuth * (NUMpi / 180.0) : (azimuth + 180.0) * (NUMpi / 180.0);
185 biosemiLocationData [ichan]. topX = r * cos (theta);
186 biosemiLocationData [ichan]. topY = r * sin (theta);
187 }
188 const integer n = 201;
189 const double d = 2.0 / (n - 1);
190 autoVEC mean = raw_VEC (numberOfDrawableChannels);
191 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++)
192 mean [ichan] = ( tmin == tmax ? Sampled_getValueAtX (me, tmin, ichan, 0, true) : Vector_getMean (me, tmin, tmax, ichan) );
193 autoMAT image = raw_MAT (n, n);
194 for (integer irow = 1; irow <= n; irow ++) {
195 const double y = -1.0 + (irow - 1) * d;
196 for (integer icol = 1; icol <= n; icol ++) {
197 const double x = -1.0 + (icol - 1) * d;
198 if (x * x + y * y <= 1.0) {
199 double value = undefined;
200 longdouble sum = 0.0, weight = 0.0;
201 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++) {
202 const double dx = x - biosemiLocationData [ichan]. topX;
203 const double dy = y - biosemiLocationData [ichan]. topY;
204 double distance = hypot (dx, dy);
205 if (distance < 1e-12) {
206 value = mean [ichan];
207 break;
208 }
209 distance = distance * distance * distance * distance * distance * distance;
210 sum += mean [ichan] / distance;
211 weight += 1.0 / distance;
212 }
213 if (isundef (value))
214 value = ( sum == 0.0 ? 0.0 : double (sum / weight) );
215 image [irow] [icol] = value;
216 }
217 }
218 }
219 const double whiteValue = ( colourScale == kGraphics_colourScale::BLUE_TO_RED ? 0.5 * (vmin + vmax) : vmin );
220 Graphics_setColourScale (graphics, colourScale);
221 for (integer irow = 1; irow <= n; irow ++) {
222 const double y = -1.0 + (irow - 1) * d;
223 for (integer icol = 1; icol <= n; icol ++) {
224 const double x = -1.0 + (icol - 1) * d;
225 if (x * x + y * y > 1.0)
226 image [irow] [icol] = whiteValue;
227 }
228 }
229 Graphics_image (graphics, image.all(), -1.0-0.5/n, 1.0+0.5/n, -1.0-0.5/n, 1.0+0.5/n, vmin, vmax);
230 Graphics_setColourScale (graphics, kGraphics_colourScale::GREY);
231 Graphics_setLineWidth (graphics, 2.0);
232 /*
233 Nose.
234 */
235 Graphics_setGrey (graphics, colourScale == kGraphics_colourScale::BLUE_TO_RED ? 1.0 : 0.5);
236 {// scope
237 const double x [3] = { -0.08, 0.0, 0.08 }, y [3] = { 0.99, 1.18, 0.99 };
238 Graphics_fillArea (graphics, 3, x, y);
239 }
240 Graphics_setColour (graphics, Melder_BLACK);
241 Graphics_line (graphics, -0.08, 0.99, 0.0, 1.18);
242 Graphics_line (graphics, 0.08, 0.99, 0.0, 1.18);
243 /*
244 Ears.
245 */
246 Graphics_setGrey (graphics, colourScale == kGraphics_colourScale::BLUE_TO_RED ? 1.0 : 0.5);
247 Graphics_fillRectangle (graphics, -1.09, -1.00, -0.08, 0.08);
248 Graphics_fillRectangle (graphics, 1.09, 1.00, -0.08, 0.08);
249 Graphics_setColour (graphics, Melder_BLACK);
250 Graphics_line (graphics, -0.99, 0.08, -1.09, 0.08);
251 Graphics_line (graphics, -1.09, 0.08, -1.09, -0.08);
252 Graphics_line (graphics, -1.09, -0.08, -0.99, -0.08);
253 Graphics_line (graphics, 0.99, 0.08, 1.09, 0.08);
254 Graphics_line (graphics, 1.09, 0.08, 1.09, -0.08);
255 Graphics_line (graphics, 1.09, -0.08, 0.99, -0.08);
256 /*
257 Scalp.
258 */
259 Graphics_ellipse (graphics, -1.0, 1.0, -1.0, 1.0);
260 Graphics_setLineWidth (graphics, 1.0);
261 Graphics_unsetInner (graphics);
262 if (garnish)
263 ERP_drawScalp_garnish (graphics, vmin, vmax, colourScale);
264 }
265
266 void structERPWindow :: v_drawSelectionViewer () {
267 Graphics_setWindow (our graphics.get(), -1.1, 1.1, -1.01, 1.19);
268 Graphics_setColour (our graphics.get(), DataGuiColour_WINDOW_BACKGROUND);
269 Graphics_fillRectangle (our graphics.get(), -1.1, 1.1, -1.01, 1.19);
270 Graphics_setColour (our graphics.get(), Melder_BLACK);
271 const integer numberOfDrawableChannels =
272 our erpArea() -> erp() -> ny >= 64 && Melder_equ (our erpArea() -> erp() -> channelNames [64].get(), U"O2") ? 64 :
273 our erpArea() -> erp() -> ny >= 32 && Melder_equ (our erpArea() -> erp() -> channelNames [32].get(), U"Cz") ? 32 :
274 0;
275 BiosemiLocationData *biosemiLocationData = numberOfDrawableChannels == 64 ? biosemiCapCoordinates64 : numberOfDrawableChannels == 32 ? biosemiCapCoordinates32 : 0;
276 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++) {
277 const double inclination = (double) biosemiLocationData [ichan]. inclination;
278 const double azimuth = (double) biosemiLocationData [ichan]. azimuth;
279 const bool rightHemisphere = inclination >= 0.0;
280 const double r = fabs (inclination / 115.0);
281 const double theta = rightHemisphere ? azimuth * (NUMpi / 180.0) : (azimuth + 180.0) * (NUMpi / 180.0);
282 biosemiLocationData [ichan]. topX = r * cos (theta);
283 biosemiLocationData [ichan]. topY = r * sin (theta);
284 }
285 const integer n = 201;
286 const double d = 2.0 / (n - 1);
287 autoVEC means = raw_VEC (numberOfDrawableChannels);
288 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++)
289 means [ichan] =
290 our startSelection == our endSelection ?
291 Sampled_getValueAtX (our erpArea() -> erp(), our startSelection, ichan, 0, true) :
292 Vector_getMean (our erpArea() -> erp(), our startSelection, our endSelection, ichan);
293 autoMAT image = zero_MAT (n, n);
294 for (integer irow = 1; irow <= n; irow ++) {
295 const double y = -1.0 + (irow - 1) * d;
296 for (integer icol = 1; icol <= n; icol ++) {
297 const double x = -1.0 + (icol - 1) * d;
298 if (x * x + y * y <= 1.0) {
299 double value = undefined;
300 longdouble sum = 0.0, weight = 0.0;
301 for (integer ichan = 1; ichan <= numberOfDrawableChannels; ichan ++) {
302 const double dx = x - biosemiLocationData [ichan]. topX;
303 const double dy = y - biosemiLocationData [ichan]. topY;
304 double distance = sqrt (dx * dx + dy * dy);
305 if (distance < 1e-12) {
306 value = means [ichan];
307 break;
308 }
309 distance = distance * distance * distance * distance * distance * distance;
310 sum += means [ichan] / distance;
311 weight += 1.0 / distance;
312 }
313 if (isundef (value))
314 value = ( sum == 0.0 ? 0.0 : double (sum / weight) );
315 image [irow] [icol] = value;
316 }
317 }
318 }
319 double minimum = 0.0, maximum = 0.0;
320 for (integer irow = 1; irow <= n; irow ++) {
321 for (integer icol = 1; icol <= n; icol ++) {
322 const double value = image [irow] [icol];
323 if (value < minimum) minimum = value;
324 else if (value > maximum) maximum = value;
325 }
326 }
327 const double absoluteExtremum = std::max (- minimum, maximum);
328 if (our erpArea() -> instancePref_scalingStrategy() == kSoundArea_scalingStrategy::FIXED_RANGE) {
329 minimum = our erpArea() -> instancePref_scaling_minimum();
330 maximum = our erpArea() -> instancePref_scaling_maximum();
331 } else if (our erpArea() -> instancePref_scalingStrategy() == kSoundArea_scalingStrategy::FIXED_HEIGHT) {
332 const double mean = 0.5 * (minimum + maximum);
333 minimum = mean - 0.5 * our erpArea() -> instancePref_scaling_height();
334 maximum = mean + 0.5 * our erpArea() -> instancePref_scaling_height();
335 } else {
336 minimum = - absoluteExtremum;
337 maximum = absoluteExtremum;
338 }
339 for (integer irow = 1; irow <= n; irow ++) {
340 const double y = -1.0 + (irow - 1) * d;
341 for (integer icol = 1; icol <= n; icol ++) {
342 const double x = -1.0 + (icol - 1) * d;
343 if (x * x + y * y > 1.0) {
344 image [irow] [icol] = minimum +
345 ( our instancePref_scalp_colourScale() == kGraphics_colourScale::BLUE_TO_RED ? 0.46 : 0.1875 ) * (maximum - minimum);
346 // -0.625 * absoluteExtremum;
347 }
348 }
349 }
350 Graphics_setColourScale (our graphics.get(), our instancePref_scalp_colourScale());
351 Graphics_image (our graphics.get(), image.all(), -1.0-0.5/n, 1.0+0.5/n, -1.0-0.5/n, 1.0+0.5/n, minimum, maximum);
352 Graphics_setColourScale (our graphics.get(), kGraphics_colourScale::GREY);
353 Graphics_setLineWidth (our graphics.get(), 2.0);
354 /*
355 Nose.
356 */
357 Graphics_setGrey (our graphics.get(), our instancePref_scalp_colourScale() == kGraphics_colourScale::BLUE_TO_RED ? 1.0 : 0.5);
358 {// scope
359 const double x [3] = { -0.08, 0.0, 0.08 }, y [3] = { 0.99, 1.18, 0.99 };
360 Graphics_fillArea (our graphics.get(), 3, x, y);
361 }
362 Graphics_setColour (our graphics.get(), Melder_BLACK);
363 Graphics_line (our graphics.get(), -0.08, 0.99, 0.0, 1.18);
364 Graphics_line (our graphics.get(), 0.08, 0.99, 0.0, 1.18);
365 /*
366 Ears.
367 */
368 Graphics_setGrey (our graphics.get(), our instancePref_scalp_colourScale() == kGraphics_colourScale::BLUE_TO_RED ? 1.0 : 0.5);
369 Graphics_fillRectangle (our graphics.get(), -1.09, -1.00, -0.08, 0.08);
370 Graphics_fillRectangle (our graphics.get(), 1.09, 1.00, -0.08, 0.08);
371 Graphics_setColour (our graphics.get(), Melder_BLACK);
372 Graphics_line (our graphics.get(), -0.99, 0.08, -1.09, 0.08);
373 Graphics_line (our graphics.get(), -1.09, 0.08, -1.09, -0.08);
374 Graphics_line (our graphics.get(), -1.09, -0.08, -0.99, -0.08);
375 Graphics_line (our graphics.get(), 0.99, 0.08, 1.09, 0.08);
376 Graphics_line (our graphics.get(), 1.09, 0.08, 1.09, -0.08);
377 Graphics_line (our graphics.get(), 1.09, -0.08, 0.99, -0.08);
378 /*
379 Scalp.
380 */
381 Graphics_ellipse (our graphics.get(), -1.0, 1.0, -1.0, 1.0);
382 Graphics_setLineWidth (our graphics.get(), 1.0);
383 }
384
385 OPTIONMENU_ENUM_VARIABLE (kGraphics_colourScale, v_prefs__scalpColourSpace)
386 void structERPWindow :: v_prefs_addFields (EditorCommand cmd) {
387 OPTIONMENU_ENUM_FIELD (kGraphics_colourScale, v_prefs__scalpColourSpace,
388 U"Scalp colour space", kGraphics_colourScale::BLUE_TO_RED)
389 }
390 void structERPWindow :: v_prefs_setValues (EditorCommand cmd) {
391 SET_ENUM (v_prefs__scalpColourSpace, kGraphics_colourScale, instancePref_scalp_colourScale())
392 }
393 void structERPWindow :: v_prefs_getValues (EditorCommand /* cmd */) {
394 setInstancePref_scalp_colourScale (v_prefs__scalpColourSpace);
395 }
396
397 autoERPWindow ERPWindow_create (conststring32 title, ERP erp) {
398 Melder_assert (erp);
399 try {
400 autoERPWindow me = Thing_new (ERPWindow);
401 my erpArea() = ERPArea_create (true, nullptr, me.get());
402 FunctionEditor_init (me.get(), title, erp);
403 return me;
404 } catch (MelderError) {
405 Melder_throw (U"ERP window not created.");
406 }
407 }
408
409 /* End of file ERPWindow.cpp */