|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "## Demo Scripts for the wfdb-python package\n", |
| 7 | + "# Demo Scripts for the wfdb-python package\n", |
8 | 8 | "\n", |
9 | 9 | "Run this script from the base directory of the git repository to access the included demo files" |
10 | 10 | ] |
|
23 | 23 | "from IPython.display import display" |
24 | 24 | ] |
25 | 25 | }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": { |
| 30 | + "collapsed": true |
| 31 | + }, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "# See the help documentation for the read functions\n", |
| 35 | + "\n", |
| 36 | + "#help(wfdb.rdsamp)\n", |
| 37 | + "#help(wfdb.srdsamp)\n", |
| 38 | + "#help(wfdb.rdann)" |
| 39 | + ] |
| 40 | + }, |
26 | 41 | { |
27 | 42 | "cell_type": "markdown", |
28 | 43 | "metadata": {}, |
29 | 44 | "source": [ |
30 | | - "### Reading Records and Annotations" |
| 45 | + "## Reading Records and Annotations" |
31 | 46 | ] |
32 | 47 | }, |
33 | 48 | { |
|
169 | 184 | "cell_type": "markdown", |
170 | 185 | "metadata": {}, |
171 | 186 | "source": [ |
172 | | - "### Writing Records and Annotations" |
| 187 | + "### Multiple sample/frame examples\n", |
| 188 | + "\n", |
| 189 | + "Although there can only be one base sampling frequency per record, a single wfdb record can store multiple channels with different sampling frequencies, as long as their sampling frequencies can all be expressed by an integer multiple of a base value. This is done by using the `sampsperframe` attribute in each channel, which indicates the number of samples of each channel present in each frame.\n", |
| 190 | + "\n", |
| 191 | + "ie: To capture three signals with `fs = 120, 240, and 360 Hz` in a single record, they can be combined into a record with `fs = 120` and `sampsperframe = [1, 2, 3]`.\n", |
| 192 | + "\n", |
| 193 | + "#### Reading Options\n", |
| 194 | + "\n", |
| 195 | + "This package allows signals in records with multiple samples/frame to be read in two ways:\n", |
| 196 | + "1. smoothed - An uniform mxn numpy is returned as the d_signals or p_signals field. Channels with multiple samples/frame have their values averaged within each frame. This is like the behaviour of the `rdsamp` function of the original WFDB c package. Note that `wfdb.plotrec` only works if the record object has the `p_signals` field.\n", |
| 197 | + "2. expanded - A list of 1d numpy arrays is returned as the e_d_signals or e_p_signals field. All samples for each channel are returned in its respective numpy array. The arrays may have different lengths depending on their `sampsperframe` values. \n", |
| 198 | + "\n", |
| 199 | + "Set the `smoothframes` *(default=True)* option in `rdsamp` to return the desired signal type." |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "metadata": { |
| 206 | + "collapsed": false |
| 207 | + }, |
| 208 | + "outputs": [], |
| 209 | + "source": [ |
| 210 | + "# Demo 8 - Read a wfdb record in which one channel has multiple samples/frame. Return a smoothed uniform array.\n", |
| 211 | + "record = wfdb.rdsamp('sampledata/test01_00s_frame')\n", |
| 212 | + "wfdb.plotrec(record)" |
173 | 213 | ] |
174 | 214 | }, |
175 | 215 | { |
|
180 | 220 | }, |
181 | 221 | "outputs": [], |
182 | 222 | "source": [ |
183 | | - "# Demo 8 - Read a WFDB record's digital samples and create a copy via the wrsamp() instance method \n", |
| 223 | + "# Demo 9 - Read a wfdb record in which one channel has multiple samples/frame. Return a list of all the expanded samples.\n", |
| 224 | + "record = wfdb.rdsamp('sampledata/test01_00s_frame', smoothframes = False)\n", |
| 225 | + "\n", |
| 226 | + "display(record.e_p_signals)\n", |
| 227 | + "# Show that different channels have different lengths. Channel 1 has 2 samples/frame, hence has 2x as many samples.\n", |
| 228 | + "print([len(s) for s in record.e_p_signals])\n", |
| 229 | + "\n", |
| 230 | + "# wfdb.plotrec doesn't work because the Record object is missing its p_signals field." |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "markdown", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "## Writing Records and Annotations" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": { |
| 244 | + "collapsed": false |
| 245 | + }, |
| 246 | + "outputs": [], |
| 247 | + "source": [ |
| 248 | + "# Demo 10 - Read a WFDB record's digital samples and create a copy via the wrsamp() instance method \n", |
184 | 249 | "# of the Record object.\n", |
185 | 250 | "\n", |
186 | 251 | "# Read a record as a Record object.\n", |
|
202 | 267 | }, |
203 | 268 | "outputs": [], |
204 | 269 | "source": [ |
205 | | - "# Demo 9 - Write a WFDB record without using a Record object via the gateway wrsamp function.\n", |
| 270 | + "# Demo 11 - Write a WFDB record without using a Record object via the gateway wrsamp function.\n", |
206 | 271 | "# This is the basic way to write physical signals to a WFDB file. \n", |
207 | 272 | "\n", |
208 | 273 | "# Read part of a record from Physiobank\n", |
|
223 | 288 | }, |
224 | 289 | "outputs": [], |
225 | 290 | "source": [ |
226 | | - "# Demo 10 - Read a WFDB annotation file and create a copy via the wrann() instance method\n", |
| 291 | + "# Demo 12 - Write a WFDB record with multiple samples/frame in a channel\n", |
| 292 | + "\n", |
| 293 | + "# Read a record as a Record object.\n", |
| 294 | + "record = wfdb.rdsamp('sampledata/test01_00s_frame', physical = False, smoothframes=False)\n", |
| 295 | + "record.recordname = 'test01_00s_framex'\n", |
| 296 | + "\n", |
| 297 | + "# Call the instance method of the object with expanded=True to write the record using the e_d_signals field\n", |
| 298 | + "record.wrsamp(expanded=True)\n", |
| 299 | + "\n", |
| 300 | + "# The new file can be read\n", |
| 301 | + "recordx = wfdb.rdsamp('test01_00s_framex')" |
| 302 | + ] |
| 303 | + }, |
| 304 | + { |
| 305 | + "cell_type": "code", |
| 306 | + "execution_count": null, |
| 307 | + "metadata": { |
| 308 | + "collapsed": false |
| 309 | + }, |
| 310 | + "outputs": [], |
| 311 | + "source": [ |
| 312 | + "# Demo 13 - Read a WFDB annotation file and create a copy via the wrann() instance method\n", |
227 | 313 | "# of the Annotation object\n", |
228 | 314 | "\n", |
229 | 315 | "# Read an annotation from Physiobank\n", |
|
245 | 331 | }, |
246 | 332 | "outputs": [], |
247 | 333 | "source": [ |
248 | | - "# Demo 11 - Write a WFDB annotation file without using an Annotator object via the gateway wrann function.\n", |
| 334 | + "# Demo 14 - Write a WFDB annotation file without using an Annotator object via the gateway wrann function.\n", |
249 | 335 | "\n", |
250 | 336 | "# Read an annotation as an Annotation object\n", |
251 | 337 | "annotation = wfdb.rdann('b001', 'atr', pbdir='cebsdb')\n", |
|
265 | 351 | }, |
266 | 352 | "outputs": [], |
267 | 353 | "source": [ |
268 | | - "# Demo 12 - View what the 'anntype' symbols mean in the standard WFDB library\n", |
| 354 | + "# Demo 15 - View what the 'anntype' symbols mean in the standard WFDB library\n", |
269 | 355 | "wfdb.showanncodes()" |
270 | 356 | ] |
271 | 357 | }, |
272 | 358 | { |
273 | 359 | "cell_type": "markdown", |
274 | 360 | "metadata": {}, |
275 | 361 | "source": [ |
276 | | - "### Downloading Content from Physiobank\n", |
| 362 | + "## Downloading Content from Physiobank\n", |
277 | 363 | "\n", |
278 | 364 | "- The downloads are made via http\n", |
279 | 365 | "- See the above demos for examples on streaming WFDB files stored in Physiobank without downloading them to local disk\n", |
|
288 | 374 | }, |
289 | 375 | "outputs": [], |
290 | 376 | "source": [ |
291 | | - "# Demo 13 - List the Physiobank Databases\n", |
| 377 | + "# Demo 16 - List the Physiobank Databases\n", |
292 | 378 | "\n", |
293 | 379 | "dbs = wfdb.getdblist()\n", |
294 | 380 | "display(dbs)" |
|
302 | 388 | }, |
303 | 389 | "outputs": [], |
304 | 390 | "source": [ |
305 | | - "# Demo 14 - Download all the WFDB records and annotations from a small Physiobank Database\n", |
| 391 | + "# Demo 17 - Download all the WFDB records and annotations from a small Physiobank Database\n", |
306 | 392 | "\n", |
307 | 393 | "# Make a temporary download directory in your current working directory\n", |
308 | 394 | "cwd = os.getcwd()\n", |
|
326 | 412 | }, |
327 | 413 | "outputs": [], |
328 | 414 | "source": [ |
329 | | - "# Demo 15 - Download specified files from a Physiobank database\n", |
| 415 | + "# Demo 18 - Download specified files from a Physiobank database\n", |
330 | 416 | "\n", |
331 | 417 | "# The files to download\n", |
332 | 418 | "filelist = ['STAFF-Studies-bibliography-2016.pdf', 'data/001a.hea', 'data/001a.dat', 'data/001b.hea', 'data/001b.dat']\n", |
|
0 commit comments