How To Get Data From The MIT-BIH Arrhythmia Database - by Proto Bioengineering - Medium
How To Get Data From The MIT-BIH Arrhythmia Database - by Proto Bioengineering - Medium
2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Open in app
Search
Member-only story
Use Python to read the most famous heart rhythm database in the world.
The MIT-BIH Arrhythmia Database is a set of 30-minute heart rhythm recordings (AKA
“electrocardiograms” or “ECGs”) from 47 patients from 1975 to 1979. The data is from a range of
healthy to heart-diseased patients and is useful for practicing analyses of the heart using code.
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 1/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
However, downloading and using the data is not straightforward. If you go to the download
page, you’ll see a ZIP file and a bunch of .atr , .hea , and other obscure file types.
And if you try to open these in a text editor, you’ll get weird characters or hex data, because your
computer doesn’t automatically know how to read the files.
We will fix this by reading the ECG data with WFDB (Waveform Database), a waveform reading
library available for multiple languages, like C, Python, MATLAB, and more.
Overall Steps
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 2/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Then unzip the file. You’ll see 4 files per patient ( .atr , .dat , .hea , and .xws ). The patients are
numbered 100–234.
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 3/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
# Python 3
import wfdb
patient_record = wfdb.rdrecord("100")
Note that we leave off any filetypes in our code. It’s just "100" , rather than “100.dat” or
“100.hea” . This is because WFDB automatically tacks on .hea to our argument, then uses the
100.hea as an entry to point to then read the 100.dat file. It’s kind of odd, but that’s how it
works.
import wfdb
patient_record = wfdb.rdrecord("100")
wfdb.plot_wfdb(patient_record) # plots the ECG
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 4/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
To zoom in, click the magnifying glass, then click and drag on the graph to zoom into a small
section.
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 5/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Above are a bunch of little spikes. Each spike is a heart beat. The official name for these spikes
in medicine is a “QRS complex.”
This can be done by grabbing the data from the patient_record variable.
This is the same code as above, minus the graphing part, plus the data printing part:
import wfdb
patient_record = wfdb.rdrecord("100")
print(patient_record.__dict__)
This prints a truncated version of the dictionary that holds all of the ECG data.
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 6/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
some info about the patient ( comments such as 69 male and his prescribed medications, like
Inderal )
etc.
The middle chunk is where the actual ECG data is (the voltages of the leads — in this case, leads
MLII and V5).
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 7/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
And the last chunk of data tells us more about the voltage, units, data format, etc.
To get a basic CSV with two columns (one for each lead), we’ll grab the following from
patient_record :
record_name (patient #)
This data can also be accessed via dot notation ( patient_record.p_signal , etc.).
import wfdb
import csv
# Create CSV
filename = f"{patient_number}.csv"
outfile = open(filename, "w")
out_csv = csv.writer(outfile)
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 8/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
# Write CSV header with lead names
out_csv.writerow(leads)
The output will be a CSV with two columns, one for each ECG lead, and 65,000 rows:
This CSV can then be plugged back in to Matplotlib, SciPy, Pandas, and more for further analysis.
import wfdb
import csv
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 9/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
# Extract patient info, lead names, and ECG data
patient_number = patient_record.record_name
leads = patient_record.sig_name
ecg_data = patient_record.p_signal
# Create CSV
filename = f"{patient_number}.csv"
outfile = open(filename, "w")
out_csv = csv.writer(outfile)
An alternative method that uses mostly WFDB in Python is also available here.
Sources
Moody GB, Mark RG. The impact of the MIT-BIH Arrhythmia Database. IEEE Eng in Med and
Biol 20(3):45–50 (May-June 2001). (PMID: 11446209)
Physionet.org
View list
Follow
Learn to code for science. “Everything simple is false. Everything complex is unusable.” — Paul Valery
Proto Bioengineering
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 11/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Proto Bioengineering
76 1
Proto Bioengineering
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 12/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
How to Make a Live Map of the ISS’s Location with Python and Plotly Dash
See the latitude/longitude of the International Space Station every second.
Proto Bioengineering
How to Stream Data from a Movella DOT Wearable Sensor with a Mac and Python
Use Python on your MacBook to get human movement data from Movella DOT.
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 13/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Gigi Dattaradon
Abigail A Antenor
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 14/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
By Abigail Antenor, Sook-Yee Chong Data Scientists Artificial Intelligence and Innovation Center of
Excellence Aboitiz Data Innovation and…
55 1
Lists
ChatGPT
21 stories · 651 saves
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 15/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Alon Fliess
Article 5 of 5 — Completing the Journey — From ESP32 to the Cloud and Back
In this final article of the series, I present how to implement the device side of our MQTT system using an
ESP32 microcontroller. The…
18
Proto Bioengineering
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 16/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
1.6K 20
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 17/18
29.05.2024, 17:36 How to Get Data from the MIT-BIH Arrhythmia Database | by Proto Bioengineering | Medium
Varun Tyagi
A Deep Dive into Building a Weather Prediction Model using Neural Networks
In the ever-evolving field of machine learning, predicting weather patterns has become an intriguing
application. In this blog post, we’ll…
https://medium.com/@protobioengineering/how-to-get-heart-data-from-the-mit-bih-arrhythmia-database-e452d4bf7215 18/18