-
Notifications
You must be signed in to change notification settings - Fork 11
/
pipes.ipynb.txt
228 lines (228 loc) · 6.33 KB
/
pipes.ipynb.txt
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pipeline-based processing in pypillometry"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.insert(0,\"..\")\n",
"import pypillometry as pp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`pypillometry` implements a pipeline-like approach where each operation executed on a `PupilData`-object returns a copy of the (modified) object. This enables the \"chaining\" of commands as follows:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"d=pp.PupilData.from_file(\"../data/test.pd\")\\\n",
" .blinks_detect()\\\n",
" .blinks_merge()\\\n",
" .lowpass_filter(3)\\\n",
" .downsample(50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This command loads a data-file (`test.pd`), applies a 3Hz low-pass filter to it, downsamples the signal to 50 Hz, detects blinks in the signal and merges short, successive blinks together. The final result of this processing-pipeline is stored in object `d`. \n",
"\n",
"Here, for better visibility, we put each operation in a separate line. For that to work, we need to tell Python that the line has not yet ended at the end of the statement which we achieve by putting a backslash `\\` at the end of each (non-final) line.\n",
"\n",
"We can get a useful summary of the dataset and the operations applied to it by simply printing it:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PupilData(test_ro_ka_si_hu_re_vu_vi_be, 331.3KiB):\n",
" n : 6001\n",
" nmiss : 117.2\n",
" perc_miss : 1.9530078320279955\n",
" nevents : 56\n",
" nblinks : 24\n",
" ninterpolated : 0.0\n",
" blinks_per_min : 11.998000333277787\n",
" fs : 50\n",
" duration_minutes : 2.0003333333333333\n",
" start_min : 4.00015\n",
" end_min : 6.0\n",
" baseline_estimated: False\n",
" response_estimated: False\n",
" History:\n",
" *\n",
" └ reset_time()\n",
" └ blinks_detect()\n",
" └ sub_slice(4,6,units=min)\n",
" └ drop_original()\n",
" └ blinks_detect()\n",
" └ blinks_merge()\n",
" └ lowpass_filter(3)\n",
" └ downsample(50)\n",
"\n"
]
}
],
"source": [
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that sampling rate, number of datapoints and more is automatically printed along with the history of all operations applied to the dataset. This information can also be retrieved separately and in a form useful for further processing the function `summary()` which returns the information in the form of a `dict`:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'test_ro_ka_si_hu_re_vu_vi_be',\n",
" 'n': 6001,\n",
" 'nmiss': 117.2,\n",
" 'perc_miss': 1.9530078320279955,\n",
" 'nevents': 56,\n",
" 'nblinks': 24,\n",
" 'ninterpolated': 0.0,\n",
" 'blinks_per_min': 11.998000333277787,\n",
" 'fs': 50,\n",
" 'duration_minutes': 2.0003333333333333,\n",
" 'start_min': 4.00015,\n",
" 'end_min': 6.0,\n",
" 'baseline_estimated': False,\n",
" 'response_estimated': False}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The history is internally stored in `PupilData`'s `history` member and can be applied to another object for convenience. That way, a pipeline can be developed on a single dataset and later be transferred to a whole folder of other (similar) datasets.\n",
"\n",
"As an example, we create several \"fake\" datasets representing data from several subjects (each with 10 trials):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"nsubj=10 # number of subjects\n",
"data={k:pp.create_fake_pupildata(ntrials=10, fs=500) for k in range(1,nsubj+1)}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `dict` `data` now contains ten `PupilData` datasets. We will now use the data from the first subject to create a pipeline of processing operations:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* fake_bomitime_ni_fu\n",
"└ lowpass_filter(5)\n",
" └ downsample(100)\n"
]
}
],
"source": [
"template=data[1].lowpass_filter(5).downsample(100)\n",
"template.print_history()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have stored the result of these operations in a new dataset `template` which contains a record of these operations. We can now easily apply identical operations on all the datasets using the `apply_history()` function:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* fake_kowelale_wu_ni\n",
"└ lowpass_filter(5)\n",
" └ downsample(100)\n"
]
}
],
"source": [
"preproc_data={k:template.apply_history(d) for k,d in data.items()}\n",
"preproc_data[5].print_history()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}