Search code examples
pythondataset

load MIT-BIH Arrhythmia Database


what is the best file format to use and how can I load the database correctly and plot sgnals in python.

I am using a .dat file from https://physionet.org/physiobank/database/mitdb/

I used these code

import numpy as np 
import matplotlib.pyplot as plt 
data = np.genfromtxt('100.dat',delimiter=',')
print(data)

and I got this error :

*line 1867, in genfromtxt
    raise ValueError(errmsg)

ValueError: Some errors were detected !
Line #3 (got 2 columns instead of 1)
Line #6 (got 4 columns instead of 1)
Line #9 (got 3 columns instead of 1)
Line #11 (got 3 columns instead of 1)
Line #12 (got 2 columns instead of 1)
Line #16 (got 3 columns instead of 1)
Line #17 (got 2 columns instead of 1)
Line #18 (got 3 columns instead of 1)
Line #20 (got 2 columns instead of 1)
Line #23 (got 2 columns instead of 1)
Line #27 (got 2 columns instead of 1)
Line #36 (got 2 columns instead of 1)
Line #37 (got 2 columns instead of 1)
Line #66 (got 2 columns instead of 1)
Line #67 (got 2 columns instead of 1)
Line #100 (got 2 columns instead of 1)
Line #105 (got 2 columns instead of 1)
Line #106 (got 2 columns instead of 1)
Line #118 (got 2 columns instead of 1)
Line #120 (got 5 columns instead of 1)
Line #122 (got 3 columns instead of 1)
Line #123 (got 4 columns instead of 1)
Line #125 (got 2 columns instead of 1)
Line #126 (got 2 columns instead of 1)
Line #127 (got 3 columns instead of 1)
Line #128 (got 2 columns instead of 1)
Line #134 (got 2 columns instead of 1)
Line #135 (got 2 columns instead of 1)
Line #137 (got 2 columns instead of 1)
Line #141 (got 3 columns instead of 1)
Line #143 (got 3 columns instead of 1)
Line #146 (got 2 columns instead of 1)
Line #147 (got 2 columns instead of 1)
Line #148 (got 2 columns instead of 1)
Line #149 (got 2 columns instead of 1)
Line #152 (got 3 columns instead of 1)
Line #154 (got 2 columns instead of 1)
Line #156 (got 4 columns instead of 1)
Line #162 (got 2 columns instead of 1)
Line #163 (got 2 columns instead of 1)
Line #167 (got 3 columns instead of 1)
Line #168 (got 2 columns instead of 1)
Line #174 (got 2 columns instead of 1)
Line #175 (got 2 columns instead of 1)
Line #176 (got 3 columns instead of 1)
Line #177 (got 2 columns instead of 1)
Line #179 (got 2 columns instead of 1)
Line #186 (got 2 columns instead of 1)
Line #187 (got 3 columns instead of 1)
Line #188 (got 2 columns instead of 1)
Line #189 (got 3 columns instead of 1)
Line #191 (got 2 columns instead of 1)
Line #193 (got 2 columns instead of 1)
Line #196 (got 2 columns instead of 1)
Line #199 (got 2 columns instead of 1)
Line #202 (got 2 columns instead of 1)
Line #203 (got 2 columns instead of 1)
Line #204 (got 2 columns instead of 1)
Line #209 (got 4 columns instead of 1)
Line #210 (got 4 columns instead of 1)
Line #211 (got 2 columns instead of 1)
Line #213 (got 2 columns instead of 1)
Line #218 (got 2 columns instead of 1)
Line #222 (got 2 columns instead of 1)
Line #223 (got 3 columns instead of 1)
Line #228 (got 2 columns instead of 1)
Line #236 (got 2 columns instead of 1)
Line #240 (got 2 columns instead of 1)
Line #241 (got 2 columns instead of 1)
Line #244 (got 3 columns instead of 1)
Line #246 (got 2 columns instead of 1)
Line #255 (got 2 columns instead of 1)
Line #257 (got 2 columns instead of 1)
Line #268 (got 2 columns instead of 1)
Line #269 (got 2 columns instead of 1)
Line #271 (got 2 columns instead of 1)
Line #273 (got 4 columns instead of 1)
Line #280 (got 2 columns instead of 1)
Line #281 (got 2 columns instead of 1)
Line #291 (got 2 columns instead of 1)
Line #323 (got 2 columns instead of 1)
Line #325 (got 2 columns instead of 1)
Line #334 (got 2 columns instead of 1)
Line #340 (got 2 columns instead of 1)
Line #341 (got 2 columns instead of 1)
Line #342 (got 2 columns instead of 1)
Line #364 (got 2 columns instead of 1)
Line #372 (got 3 columns instead of 1)
Line #375 (got 2 columns instead of 1)
Line #378 (got 6 columns instead of 1)*

Solution

  • You should use the wfdb-python package. The documentation is really pretty good, and there are plenty of examples. To install it in your environment, you can simply do pip install wfdb.

    Assuming you have the data files (including the 100.dat file you mentioned above) in a local directory called ./mitdb, you can read a record with the following:

    import wfdb
    record = wfdb.rdsamp('mitdb/100', sampto=3000)
    annotation = wfdb.rdann('sampledata/100', 'atr', sampto=3000)
    

    You can then plot the data using the following function:

    wfdb.plotrec(record, annotation = annotation,
             title='Record 100 from MIT-BIH Arrhythmia Database',
             timeunits = 'seconds', figsize = (10,4), ecggrids = 'all')
    

    If you don't already have the data files locally, you can use wfdb to download them:

    import os
    wfdb.dldatabase('mitdb', os.path.join(os.getcwd(), 'mitdb'))
    

    This will download the records and save them in a local directory called mitdb