-
Notifications
You must be signed in to change notification settings - Fork 11
/
blinks.html
595 lines (560 loc) · 35.6 KB
/
blinks.html
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blink preprocessing — pypillometry documentation</title>
<link rel="stylesheet" href="../_static/nature.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Plotting pupillometric data for exploration" href="plotting.html" />
<link rel="prev" title="Pipeline-based processing in pypillometry" href="pipes.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="plotting.html" title="Plotting pupillometric data for exploration"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="pipes.html" title="Pipeline-based processing in pypillometry"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">pypillometry documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="notebooks.html" accesskey="U">Tutorials/Example Notebooks</a> »</li>
<li class="nav-item nav-item-this"><a href="">Blink preprocessing</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<style>
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput.container,
div.nbinput.container div.prompt,
div.nbinput.container div.input_area,
div.nbinput.container div[class*=highlight],
div.nbinput.container div[class*=highlight] pre,
div.nboutput.container,
div.nboutput.container div.prompt,
div.nboutput.container div.output_area,
div.nboutput.container div[class*=highlight],
div.nboutput.container div[class*=highlight] pre {
background: none;
border: none;
padding: 0 0;
margin: 0;
box-shadow: none;
}
/* avoid gaps between output lines */
div.nboutput.container div[class*=highlight] pre {
line-height: normal;
}
/* input/output containers */
div.nbinput.container,
div.nboutput.container {
display: -webkit-flex;
display: flex;
align-items: flex-start;
margin: 0;
width: 100%;
}
@media (max-width: 540px) {
div.nbinput.container,
div.nboutput.container {
flex-direction: column;
}
}
/* input container */
div.nbinput.container {
padding-top: 5px;
}
/* last container */
div.nblast.container {
padding-bottom: 5px;
}
/* input prompt */
div.nbinput.container div.prompt pre {
color: #307FC1;
}
/* output prompt */
div.nboutput.container div.prompt pre {
color: #BF5B3D;
}
/* all prompts */
div.nbinput.container div.prompt,
div.nboutput.container div.prompt {
width: 4.5ex;
padding-top: 5px;
position: relative;
user-select: none;
}
div.nbinput.container div.prompt > div,
div.nboutput.container div.prompt > div {
position: absolute;
right: 0;
margin-right: 0.3ex;
}
@media (max-width: 540px) {
div.nbinput.container div.prompt,
div.nboutput.container div.prompt {
width: unset;
text-align: left;
padding: 0.4em;
}
div.nboutput.container div.prompt.empty {
padding: 0;
}
div.nbinput.container div.prompt > div,
div.nboutput.container div.prompt > div {
position: unset;
}
}
/* disable scrollbars on prompts */
div.nbinput.container div.prompt pre,
div.nboutput.container div.prompt pre {
overflow: hidden;
}
/* input/output area */
div.nbinput.container div.input_area,
div.nboutput.container div.output_area {
-webkit-flex: 1;
flex: 1;
overflow: auto;
}
@media (max-width: 540px) {
div.nbinput.container div.input_area,
div.nboutput.container div.output_area {
width: 100%;
}
}
/* input area */
div.nbinput.container div.input_area {
border: 1px solid #e0e0e0;
border-radius: 2px;
background: #f5f5f5;
}
/* override MathJax center alignment in output cells */
div.nboutput.container div[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.imgmath center alignment in output cells */
div.nboutput.container div.math p {
text-align: left;
}
/* standard error */
div.nboutput.container div.output_area.stderr {
background: #fdd;
}
/* ANSI colors */
.ansi-black-fg { color: #3E424D; }
.ansi-black-bg { background-color: #3E424D; }
.ansi-black-intense-fg { color: #282C36; }
.ansi-black-intense-bg { background-color: #282C36; }
.ansi-red-fg { color: #E75C58; }
.ansi-red-bg { background-color: #E75C58; }
.ansi-red-intense-fg { color: #B22B31; }
.ansi-red-intense-bg { background-color: #B22B31; }
.ansi-green-fg { color: #00A250; }
.ansi-green-bg { background-color: #00A250; }
.ansi-green-intense-fg { color: #007427; }
.ansi-green-intense-bg { background-color: #007427; }
.ansi-yellow-fg { color: #DDB62B; }
.ansi-yellow-bg { background-color: #DDB62B; }
.ansi-yellow-intense-fg { color: #B27D12; }
.ansi-yellow-intense-bg { background-color: #B27D12; }
.ansi-blue-fg { color: #208FFB; }
.ansi-blue-bg { background-color: #208FFB; }
.ansi-blue-intense-fg { color: #0065CA; }
.ansi-blue-intense-bg { background-color: #0065CA; }
.ansi-magenta-fg { color: #D160C4; }
.ansi-magenta-bg { background-color: #D160C4; }
.ansi-magenta-intense-fg { color: #A03196; }
.ansi-magenta-intense-bg { background-color: #A03196; }
.ansi-cyan-fg { color: #60C6C8; }
.ansi-cyan-bg { background-color: #60C6C8; }
.ansi-cyan-intense-fg { color: #258F8F; }
.ansi-cyan-intense-bg { background-color: #258F8F; }
.ansi-white-fg { color: #C5C1B4; }
.ansi-white-bg { background-color: #C5C1B4; }
.ansi-white-intense-fg { color: #A1A6B2; }
.ansi-white-intense-bg { background-color: #A1A6B2; }
.ansi-default-inverse-fg { color: #FFFFFF; }
.ansi-default-inverse-bg { background-color: #000000; }
.ansi-bold { font-weight: bold; }
.ansi-underline { text-decoration: underline; }
div.nbinput.container div.input_area div[class*=highlight] > pre,
div.nboutput.container div.output_area div[class*=highlight] > pre,
div.nboutput.container div.output_area div[class*=highlight].math,
div.nboutput.container div.output_area.rendered_html,
div.nboutput.container div.output_area > div.output_javascript,
div.nboutput.container div.output_area:not(.rendered_html) > img{
padding: 5px;
}
/* fix copybtn overflow problem in chromium (needed for 'sphinx_copybutton') */
div.nbinput.container div.input_area > div[class^='highlight'],
div.nboutput.container div.output_area > div[class^='highlight']{
overflow-y: hidden;
}
/* hide copybtn icon on prompts (needed for 'sphinx_copybutton') */
.prompt a.copybtn {
display: none;
}
/* Some additional styling taken form the Jupyter notebook CSS */
div.rendered_html table {
border: none;
border-collapse: collapse;
border-spacing: 0;
color: black;
font-size: 12px;
table-layout: fixed;
}
div.rendered_html thead {
border-bottom: 1px solid black;
vertical-align: bottom;
}
div.rendered_html tr,
div.rendered_html th,
div.rendered_html td {
text-align: right;
vertical-align: middle;
padding: 0.5em 0.5em;
line-height: normal;
white-space: normal;
max-width: none;
border: none;
}
div.rendered_html th {
font-weight: bold;
}
div.rendered_html tbody tr:nth-child(odd) {
background: #f5f5f5;
}
div.rendered_html tbody tr:hover {
background: rgba(66, 165, 245, 0.2);
}
</style>
<div class="admonition note">
This file was created from the following Jupyter-notebook: <a href="https://github.com/ihrke/pypillometry/tree/master/docs/blinks.ipynb">docs/blinks.ipynb</a>
<br>
Interactive version:
<a href="https://mybinder.org/v2/gh/ihrke/pypillometry/master?filepath=docs/blinks.ipynb"><img alt="Binder badge" src="https://mybinder.org/badge_logo.svg" style="vertical-align:text-bottom"></a>
</div><div class="section" id="Blink-preprocessing">
<h1>Blink preprocessing<a class="headerlink" href="#Blink-preprocessing" title="Permalink to this headline">¶</a></h1>
<p>One important step in the processing of pupillometric data is the handling of blinks.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="s2">".."</span><span class="p">)</span> <span class="c1"># this is not needed if you have installed pypillometry</span>
<span class="kn">import</span> <span class="nn">pypillometry</span> <span class="k">as</span> <span class="nn">pp</span>
<span class="kn">import</span> <span class="nn">pylab</span> <span class="k">as</span> <span class="nn">plt</span>
</pre></div>
</div>
</div>
<div class="section" id="Blink-detection">
<h2>Blink detection<a class="headerlink" href="#Blink-detection" title="Permalink to this headline">¶</a></h2>
<p>The first step is, of course, the identification of blinks. For this, we can use the <code class="docutils literal notranslate"><span class="pre">blinks_detect()</span></code> function:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[19]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">d</span><span class="o">=</span><span class="n">pp</span><span class="o">.</span><span class="n">PupilData</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s2">"../data/example_shortblinks.pd"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_4_0.png" src="../_images/docs_blinks_4_0.png" />
</div>
</div>
<p>Detected blinks are marked in red in the plot. In the current case, the blink-detection did a reasonable job with the default settings but the <code class="docutils literal notranslate"><span class="pre">blinks_detect()</span></code> function has many parameters that can be tweaked.</p>
<p>The function <code class="docutils literal notranslate"><span class="pre">blinks_detect()</span></code> looks blinks using several strategies:</p>
<ul class="simple">
<li><p>First, blinks are detected as consecutive sequence of 0 (or another placeholder value for missing data).</p></li>
<li><p>Second, blinks are defined as everything between two crossings of the velocity profile (from negative to positive).</p></li>
<li><p>Finally, detected blinks have to be at least <code class="docutils literal notranslate"><span class="pre">min_duration</span></code> duration (in customizable <code class="docutils literal notranslate"><span class="pre">units</span></code>).</p></li>
</ul>
<p>As a consequence, there are several parameters that can be adjusted (see the <a class="reference external" href="api.html">API-docs</a> for details). The most important ones are the following:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">min_duration</span></code>: minimum duration for a sequence of missing numbers to be treated as blink</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">vel_onset</span></code>: negative velocity that needs to be crossed; arbitrary units that depend on sampling rate etc</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">vel_offset</span></code>: same but for the offset of the blink</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">min_onset_len</span></code>: minimum number of consecutive samples that crossed threshold in the velocity profile to detect as onset (to avoid noise-induced changes)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">min_offset_len</span></code>: same for the offset</p></li>
</ul>
<p>In practice, it often makes sense to start with a default set of parameters and adjust them for each subject by visually inspecting the results (see <a class="reference external" href="preproc_example_pavlov.html">this example notebook for a fully worked example</a>).</p>
<p>In the next example, default parameters produce suboptimal results:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[25]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">d</span><span class="o">=</span><span class="n">pp</span><span class="o">.</span><span class="n">PupilData</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s2">"../data/test.pd"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span><span class="mf">4.3</span><span class="p">),</span> <span class="n">units</span><span class="o">=</span><span class="s2">"min"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_7_0.png" src="../_images/docs_blinks_7_0.png" />
</div>
</div>
<p>We see that a period of seemingly valid pupil-data (around times 4.05 and 4.15 min) was marked as blink because of a sudden downward change in the pupil signal which might resemble the onset of a blink (a likely source of these artifacts are sudden eye-movements but we would need eyetracking-data to corrobate this hypothesis).</p>
<p>This problem can be fixed by setting a stricter value for the velocity-profile based onset detection <code class="docutils literal notranslate"><span class="pre">vel_onset</span></code> or requiring a more prolonged onset-transient by increasing <code class="docutils literal notranslate"><span class="pre">min_onset_len</span></code>:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[48]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">9</span><span class="p">))</span>
<span class="n">d1</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">(</span><span class="n">min_onset_len</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
<span class="n">d2</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">(</span><span class="n">vel_onset</span><span class="o">=-</span><span class="mi">15</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="n">d1</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span><span class="mf">4.3</span><span class="p">),</span> <span class="n">units</span><span class="o">=</span><span class="s2">"min"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"min_onset_len=15"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="n">d2</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span><span class="mf">4.3</span><span class="p">),</span> <span class="n">units</span><span class="o">=</span><span class="s2">"min"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"vel_onset=-15"</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_9_0.png" src="../_images/docs_blinks_9_0.png" />
</div>
</div>
<p>The second method changing <code class="docutils literal notranslate"><span class="pre">vel_onset</span></code> seems to have worked better, as the last “false-positive” around time 4.25 is still present when increasing <code class="docutils literal notranslate"><span class="pre">min_onset_len</span></code>. However, we introduced a new problem, as the ultra-short blink at time 4.024 went undetected. As a consequence, we can decrease the minimum blink-duration required <code class="docutils literal notranslate"><span class="pre">min_duration</span></code>.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[54]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">(</span><span class="n">vel_onset</span><span class="o">=-</span><span class="mi">15</span><span class="p">,</span> <span class="n">min_duration</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span><span class="mf">4.3</span><span class="p">),</span> <span class="n">units</span><span class="o">=</span><span class="s2">"min"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_11_0.png" src="../_images/docs_blinks_11_0.png" />
</div>
</div>
</div>
<div class="section" id="Merging-blinks">
<h2>Merging blinks<a class="headerlink" href="#Merging-blinks" title="Permalink to this headline">¶</a></h2>
<p>Some subjects blink in quick succession so that the eye almost immediately starts closing again once it opens from the previous blink. In that case, it is very difficult to define the onset and offset of such blink sequences and interpolation is even more difficult.</p>
<p>We reuse the first example above, which is from a dataset recorded in our lab. We see a couple of “normal” blinks, as well as some fast sequences of sequential blinks (e.g., between seconds 131 and 134).</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[58]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">d</span><span class="o">=</span><span class="n">pp</span><span class="o">.</span><span class="n">PupilData</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s2">"../data/example_shortblinks.pd"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_14_0.png" src="../_images/docs_blinks_14_0.png" />
</div>
</div>
<p>If we would attempt a normal interpolation of these blinks separately, the interpolation would fail because the signal “neighbouring” the blink is contaminated by new blinks. Therefore, such sequences should be merged before interpolation using function <code class="docutils literal notranslate"><span class="pre">blinks_merge()</span></code>. We can specifiy how far apart two blinks need to be to be treated as separate blinks by setting the <code class="docutils literal notranslate"><span class="pre">distance=</span></code> argument (in units of ms).</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[93]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">blinks_detect</span><span class="p">()</span><span class="o">.</span><span class="n">blinks_merge</span><span class="p">(</span><span class="n">distance</span><span class="o">=</span><span class="mi">130</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_16_0.png" src="../_images/docs_blinks_16_0.png" />
</div>
</div>
<p>Here, we require 50 ms of clean signal between two blinks. As a consequence, we see that the rapid blink sequences have been merged together and can safely be interpolated.</p>
</div>
<div class="section" id="Interpolating-blinks">
<h2>Interpolating blinks<a class="headerlink" href="#Interpolating-blinks" title="Permalink to this headline">¶</a></h2>
<p>The final step in the treatment of blinks is to actually replace the contaminated parts of the signal, a procedure known as <em>interpolation</em>.</p>
<p>In <code class="docutils literal notranslate"><span class="pre">pypillometry</span></code>, there are currently two implementation functions available, <code class="docutils literal notranslate"><span class="pre">blinks_interpolate()</span></code> and <code class="docutils literal notranslate"><span class="pre">blinks_interp_mahot()</span></code>. The difference between the two functions is which datapoints are used for interpolation. While <code class="docutils literal notranslate"><span class="pre">blinks_interpolate()</span></code> uses all available datapoints, <code class="docutils literal notranslate"><span class="pre">blinks_interp_mahot()</span></code> uses only four points that are determined by a heuristic following a <a class="reference external" href="https://figshare.com/articles/A_simple_way_to_reconstruct_pupil_size_during_eye_blinks/688001">suggestion by Sebastiaan Mahot
(2013)</a>.</p>
<p>Both approaches can be tweaked to give good results but <code class="docutils literal notranslate"><span class="pre">blinks_interp_mahot()</span></code> seems to work better in general.</p>
<p>We will now use this approach and plot the interpolated signal on top of the original one:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[94]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">dinterp</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">blinks_interp_mahot</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">dinterp</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">highlight_blinks</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_19_0.png" src="../_images/docs_blinks_19_0.png" />
</div>
</div>
<p>While the resulting signal looks somewhat ok’ish, there are some worrying discontinuities in the reconstructed signal especially at the end of some blink periods, e.g., the one around 133 seconds. The reason for this is that the opening of the pupil was somewhat slow for this subject and therefore the upward-going transient at the end of the blinks is not yet fully recovered to the pre-blink baseline levels.</p>
<p>The solution for this dataset is to set the <code class="docutils literal notranslate"><span class="pre">margin</span></code>-parameter of the procedure to a larger value. Here, we set the <code class="docutils literal notranslate"><span class="pre">margin</span></code> parameter to (50,100) ms (50 ms for the onset and 100 ms for the offset of the transient). Plotting the two interpolations on top of each other, the one with appropriate settings of the <code class="docutils literal notranslate"><span class="pre">margin</span></code> parameter has fewer discontinuities.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[96]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">dinterp2</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">blinks_interp_mahot</span><span class="p">(</span><span class="n">margin</span><span class="o">=</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">100</span><span class="p">))</span><span class="c1">#, plot=True, plot_figsize=(25,15), plot_dim=(5,5));</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="n">dinterp</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">highlight_blinks</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">highlight_interpolated</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">dinterp2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">highlight_blinks</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_21_0.png" src="../_images/docs_blinks_21_0.png" />
</div>
</div>
<p>The procedure has more parameters that can be tweaked to make interpolation even better. Please refer to <a class="reference external" href="https://figshare.com/articles/A_simple_way_to_reconstruct_pupil_size_during_eye_blinks/688001">Mahot (2013)</a> for a short and readable summary of what these parameters do.</p>
<p>Finally, the <code class="docutils literal notranslate"><span class="pre">blinks_interp_mahot()</span></code> function supports an argument <code class="docutils literal notranslate"><span class="pre">plot=True</span></code> that visualizes the interpolation procedure and can help to detect errors and set the parameters appropriately:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[100]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">d</span><span class="o">.</span><span class="n">blinks_interp_mahot</span><span class="p">(</span><span class="n">margin</span><span class="o">=</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">100</span><span class="p">),</span> <span class="n">plot</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">plot_figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span><span class="mi">6</span><span class="p">),</span> <span class="n">plot_dim</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="../_images/docs_blinks_23_0.png" src="../_images/docs_blinks_23_0.png" />
</div>
</div>
</div>
</div>
<div class="admonition note">
This file was created from the following Jupyter-notebook: <a href="https://github.com/ihrke/pypillometry/tree/master/docs/blinks.ipynb">docs/blinks.ipynb</a>
<br>
Interactive version:
<a href="https://mybinder.org/v2/gh/ihrke/pypillometry/master?filepath=docs/blinks.ipynb"><img alt="Binder badge" src="https://mybinder.org/badge_logo.svg" style="vertical-align:text-bottom"></a>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/pypillometry_logo_200x200.png" alt="Logo"/>
</a></p>
<h3><a href="../index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Blink preprocessing</a><ul>
<li><a class="reference internal" href="#Blink-detection">Blink detection</a></li>
<li><a class="reference internal" href="#Merging-blinks">Merging blinks</a></li>
<li><a class="reference internal" href="#Interpolating-blinks">Interpolating blinks</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="pipes.html"
title="previous chapter">Pipeline-based processing in pypillometry</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="plotting.html"
title="next chapter">Plotting pupillometric data for exploration</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/docs/blinks.ipynb.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="plotting.html" title="Plotting pupillometric data for exploration"
>next</a> |</li>
<li class="right" >
<a href="pipes.html" title="Pipeline-based processing in pypillometry"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">pypillometry documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="notebooks.html" >Tutorials/Example Notebooks</a> »</li>
<li class="nav-item nav-item-this"><a href="">Blink preprocessing</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2020, Matthias Mittner.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.1.
</div>
</body>
</html>