Note
Go to the end to download the full example code.
Process an EIT recording#
In this tutorial, we process one EIT recording from the file to the results. We load the file, filter the signal, find the breaths and save the results. At the end, you have the figure below and a folder with result files.
You need M3Resp with the EIT integration (python -m pip install -e ".[eit]").
Load the recording#
See The session for what a session holds.
A session holds the recordings and all the results of one analysis. We make an empty session and load the EIT file into it. The file is a Dräger .bin file, so we set vendor="draeger".
import os
import tempfile
from m3resp import M3Session
# The example recordings are in the tests/data folder of the M3Resp source code.
data_dir = os.environ.get("M3RESP_EXAMPLE_DATA", os.path.join("tests", "data"))
session = M3Session()
session.load_eit(
os.path.join(data_dir, "m3resp_multimodal_1_eit_draeger.bin"),
vendor="draeger",
)
The session now has the raw EIT data. The global impedance is the sum of all pixels in each EIT image.
global_impedance = session.raw["eit"].global_impedance
print("Sampling rate:", global_impedance.sample_frequency, "Hz")
print("Number of samples:", len(global_impedance.values))
Sampling rate: 20.0 Hz
Number of samples: 1200
The recording is 60 s long, with 20 EIT images per second.
Filter the signal#
See Signals for how M3Resp stores a signal.
preprocess_eit() finds the respiratory rate and the heart rate. Then it removes the heart signal from the EIT data with an MDN filter (multiple digital notch filter). It also calculates the tidal impedance variation (TIV) and the end-expiratory lung impedance (EELI) of each breath.
session.preprocess_eit()
for signal in session.signals:
print(f"{signal.name}: {signal.sample_frequency} Hz, unit {signal.unit}")
Global impedance (raw): 20.0 Hz, unit a.u.
Global impedance (mdn_filtered): 20.0 Hz, unit a.u.
The session now has two signals: the raw global impedance and the filtered global impedance. EIT impedance has no physical unit, so the unit is a.u. (arbitrary units).
The rates are parameters of the recording. M3Resp stores rates in Hz. Multiply by 60 to get breaths or beats per minute.
for parameter in session.parameter_results:
per_minute = 60 * parameter.value
rate = f"{parameter.value:.3f} Hz = {per_minute:.1f} per minute"
print(f"{parameter.name}: {rate}")
respiratory_rate: 0.231 Hz = 13.9 per minute
heart_rate: 1.596 Hz = 95.8 per minute
Find the breaths#
See Events, intervals and breaths for what a breath holds.
detect_eit_breaths() finds each breath in the filtered global impedance. Each breath has a start time, an extremum time and an end time, in seconds.
breaths = session.detect_eit_breaths()
print("Number of breaths:", len(breaths))
for breath in breaths[:3]:
print(
f"start {breath.start_time:5.2f} s, "
f"extremum {breath.extremum_time:5.2f} s, "
f"end {breath.end_time:5.2f} s"
)
Number of breaths: 13
start 4.05 s, extremum 5.15 s, end 8.00 s
start 8.00 s, extremum 9.35 s, end 12.10 s
start 12.10 s, extremum 13.80 s, end 16.95 s
The session also stores the breaths, under session.events["eit_breaths"].
Each breath has a TIV and an EELI value. M3Resp keeps these values per breath in session.interval_data.
tiv = session.interval_data.for_name("continuous_tivs")[0]
first_values = [round(float(value), 1) for value in tiv.values[:3]]
print("TIV of the first three breaths:", first_values)
TIV of the first three breaths: [120.5, 145.9, 160.3]
Look at the results#
plot_eit_processing_summary() shows the filtered global impedance with the breaths, the TIV and EELI of each breath, and the mean TIV of each pixel.
With backend="plotly", the figure is interactive. Drag over a part of a panel to zoom in, double-click to zoom out, and move the mouse over a line to see its values. fig.show() shows the figure in a notebook, or in your web browser when you run a Python script. Without backend="plotly", you get a static matplotlib figure.
from m3resp.visualization import plot_eit_processing_summary
fig = plot_eit_processing_summary(session, backend="plotly")
fig.show()
Save the results#
See How to export the results of a session for the list of files and their contents.
export_summary() writes all results of the session to a folder. Here we use a temporary folder. In your own analysis, give the path of your results folder.
output_dir = tempfile.mkdtemp()
session.export_summary(output_dir)
for file_name in sorted(os.listdir(output_dir)):
print(file_name)
eit_breaths.csv
interval_data.csv
interval_data_arrays.npz
interval_data_metadata.json
parameter_results.csv
processing_history.json
quality_flags.csv
session_metadata.json
signals_manifest.csv
summary.json
Recap#
In this tutorial, we:
loaded an EIT recording into a session with
load_eit(),filtered it and calculated the rates, TIV and EELI with
preprocess_eit(),found 13 breaths with
detect_eit_breaths(),plotted the results and wrote them to files with
export_summary().
The "eit" preset runs steps 2 and 3 with one call: session.run_preset("eit").
Next#
Process an EMG recording in the next tutorial.
Change the filter or the results that M3Resp calculates: How to change the EIT processing settings.
Total running time of the script: (0 minutes 4.041 seconds)