Function Reference¶
Processing Functions¶
ECG Cleaning¶
- ecgdatakit.processing.clean_ecg(lead, method='default', *, fs=None, **kwargs)[source]
Clean an ECG lead signal.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Input ECG lead or raw signal array.method (
str) – Cleaning method:"default","biosppy","neurokit2","combined", or"deepfade".fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.**kwargs –
Extra arguments forwarded to the selected backend:
device(str): PyTorch device for"deepfade"(default"cpu").weights_path(str | Path): Override the bundled DeepFADE weights.batch_size(int): Inference batch size for"deepfade"(default 32).
- Returns:
Cleaned lead (new object, original unchanged).
- Return type:
Filters¶
- ecgdatakit.processing.lowpass(lead, cutoff, order=4, *, fs=None)[source]
Apply a Butterworth low-pass filter.
- ecgdatakit.processing.highpass(lead, cutoff, order=4, *, fs=None)[source]
Apply a Butterworth high-pass filter.
- ecgdatakit.processing.bandpass(lead, low, high, order=4, *, fs=None)[source]
Apply a Butterworth band-pass filter.
- Parameters:
- Return type:
- ecgdatakit.processing.notch(lead, freq=50.0, quality=30.0, *, fs=None)[source]
Apply an IIR notch (band-stop) filter.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Input ECG lead or raw signal array.freq (
float) – Center frequency to remove (default 50 Hz for mains hum).quality (
float) – Quality factor — higher means narrower notch (default 30).fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.
- Return type:
- ecgdatakit.processing.remove_baseline(lead, cutoff=0.5, order=2, *, fs=None)[source]
Remove baseline wander using a high-pass filter.
- ecgdatakit.processing.diagnostic_filter(lead, notch_freq=50.0, *, fs=None)[source]
Apply AHA diagnostic-grade filtering: 0.05–150 Hz bandpass + notch.
Suitable for diagnostic ECG interpretation where full morphology (including ST segment) must be preserved.
- ecgdatakit.processing.monitoring_filter(lead, notch_freq=50.0, *, fs=None)[source]
Apply monitoring-grade filtering: 0.67–40 Hz bandpass + notch.
Suitable for arrhythmia monitoring where baseline stability is more important than preserving fine morphology.
Resampling¶
- ecgdatakit.processing.resample(lead, target_rate, *, fs=None)[source]
Resample a lead to a different sample rate.
Uses polyphase rational resampling (
scipy.signal.resample_poly) which preserves signal content up to the Nyquist frequency of the lower sample rate.
Normalization¶
- ecgdatakit.processing.normalize_minmax(data)[source]
- Overloads:
data (Lead) → Lead
data (list[Lead]) → list[Lead]
data (ECGRecord) → ECGRecord
data (list[ECGRecord]) → list[ECGRecord]
data (NDArray[np.float64]) → NDArray[np.float64]
- Parameters:
data (Lead | list[Lead] | ECGRecord | list[ECGRecord] | ndarray[tuple[Any, ...], dtype[float64]])
- Return type:
Lead | list[Lead] | ECGRecord | list[ECGRecord] | ndarray[tuple[Any, …], dtype[float64]]
Scale signal to the [-1, 1] range.
Normalization is applied per lead, per ECG.
Accepted inputs:
Lead— single lead.list[Lead]— multiple leads (e.g. a 12-lead ECG).ECGRecord— all leads and median beats in the record.list[ECGRecord]— multiple records.3-D numpy array
(n_ecgs, n_leads, n_samples)— raw multi-ECG data.
Returns the same type as the input.
- ecgdatakit.processing.normalize_zscore(data)[source]
- Overloads:
data (Lead) → Lead
data (list[Lead]) → list[Lead]
data (ECGRecord) → ECGRecord
data (list[ECGRecord]) → list[ECGRecord]
data (NDArray[np.float64]) → NDArray[np.float64]
- Parameters:
data (Lead | list[Lead] | ECGRecord | list[ECGRecord] | ndarray[tuple[Any, ...], dtype[float64]])
- Return type:
Lead | list[Lead] | ECGRecord | list[ECGRecord] | ndarray[tuple[Any, …], dtype[float64]]
Normalize signal to zero mean and unit variance (z-score).
Normalization is applied per lead, per ECG.
Accepted inputs:
Lead— single lead.list[Lead]— multiple leads (e.g. a 12-lead ECG).ECGRecord— all leads and median beats in the record.list[ECGRecord]— multiple records.3-D numpy array
(n_ecgs, n_leads, n_samples)— raw multi-ECG data.
Returns the same type as the input.
- ecgdatakit.processing.normalize_amplitude(data, target_mv=1.0)[source]
- Overloads:
data (Lead), target_mv (float) → Lead
data (list[Lead]), target_mv (float) → list[Lead]
data (ECGRecord), target_mv (float) → ECGRecord
data (list[ECGRecord]), target_mv (float) → list[ECGRecord]
data (NDArray[np.float64]), target_mv (float) → NDArray[np.float64]
- Parameters:
- Return type:
Lead | list[Lead] | ECGRecord | list[ECGRecord] | ndarray[tuple[Any, …], dtype[float64]]
Scale signal so that its maximum absolute amplitude equals target_mv.
Normalization is applied per lead, per ECG.
Accepted inputs:
Lead— single lead.list[Lead]— multiple leads (e.g. a 12-lead ECG).ECGRecord— all leads and median beats in the record.list[ECGRecord]— multiple records.3-D numpy array
(n_ecgs, n_leads, n_samples)— raw multi-ECG data.
Returns the same type as the input.
R-Peak Detection¶
- ecgdatakit.processing.detect_r_peaks(lead, method='pan_tompkins', *, fs=None)[source]
Detect R-peak locations in an ECG lead.
- Parameters:
- Returns:
Array of sample indices where R-peaks were detected.
- Return type:
- ecgdatakit.processing.heart_rate(lead, peaks=None, *, fs=None)[source]
Compute average heart rate in beats per minute.
- Parameters:
- Return type:
- ecgdatakit.processing.rr_intervals(lead, peaks=None, *, fs=None)[source]
Compute RR intervals in milliseconds.
- Parameters:
- Return type:
- ecgdatakit.processing.instantaneous_heart_rate(lead, peaks=None, *, fs=None)[source]
Compute instantaneous heart rate at each beat in bpm.
- Parameters:
- Return type:
Heart Rate Variability¶
- ecgdatakit.processing.time_domain(rr_ms)[source]
Compute time-domain HRV metrics from RR intervals.
- ecgdatakit.processing.frequency_domain(rr_ms, method='welch', interp_fs=4.0)[source]
Compute frequency-domain HRV metrics from RR intervals.
RR intervals are interpolated to a uniform time series at interp_fs Hz, then the power spectral density is estimated.
Transforms & Segmentation¶
- ecgdatakit.processing.power_spectrum(lead, method='welch', nperseg=None, *, fs=None)[source]
Compute the power spectral density of an ECG lead.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Input ECG lead or raw signal array.method (
str) –"welch"(default) for Welch’s method.nperseg (
int|None) – Segment length for Welch’s method. Defaults tomin(256, len(samples)).fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.
- Returns:
(frequencies, power)arrays.- Return type:
tuple[ndarray[tuple[Any,...],dtype[double]],ndarray[tuple[Any,...],dtype[double]]]
- ecgdatakit.processing.fft(lead, *, fs=None)[source]
Compute the single-sided FFT magnitude spectrum.
- ecgdatakit.processing.segment_beats(lead, peaks=None, before=0.2, after=0.4, *, fs=None)[source]
Segment individual heartbeats around R-peaks.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Input ECG lead or raw signal array.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices. Detected automatically ifNone.before (
float) – Seconds before R-peak to include (default 0.2).after (
float) – Seconds after R-peak to include (default 0.4).fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.
- Returns:
One Lead per beat, labelled
"{label}_beat_{i}".- Return type:
- ecgdatakit.processing.average_beat(lead, peaks=None, before=0.2, after=0.4, *, fs=None)[source]
Compute the ensemble-averaged heartbeat (template).
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Input ECG lead or raw signal array.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices. Detected automatically ifNone.before (
float) – Seconds before R-peak (default 0.2).after (
float) – Seconds after R-peak (default 0.4).fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.
- Returns:
Averaged beat labelled
"{label}_avg".- Return type:
Signal Quality¶
- ecgdatakit.processing.signal_quality_index(lead, *, fs=None)[source]
Compute a composite signal quality index (SQI) in the range [0, 1].
Combines four sub-metrics: kurtosis SQI, power-ratio SQI, R-peak regularity SQI, and baseline stability SQI.
- ecgdatakit.processing.classify_quality(lead, *, fs=None)[source]
Classify signal quality as a human-readable category.
- ecgdatakit.processing.snr_estimate(lead, *, fs=None)[source]
Estimate signal-to-noise ratio in dB.
Uses a frequency-domain approach: signal power in 1–40 Hz vs noise power above 100 Hz (up to Nyquist).
Lead Derivation¶
- ecgdatakit.processing.derive_lead_iii(lead_i, lead_ii, *, fs=None)[source]
Derive Lead III from Leads I and II (Einthoven’s law: III = II - I).
- ecgdatakit.processing.derive_augmented(lead_i, lead_ii, *, fs=None)[source]
Derive augmented limb leads aVR, aVL, aVF from Leads I and II.
- ecgdatakit.processing.derive_standard_12(lead_i, lead_ii, v1, v2, v3, v4, v5, v6, *, fs=None)[source]
Assemble a full 12-lead ECG, deriving III, aVR, aVL, aVF.
- Parameters:
- Returns:
12 leads in standard order: I, II, III, aVR, aVL, aVF, V1–V6.
- Return type:
Plotting Functions¶
Static Plots (matplotlib)¶
- ecgdatakit.plotting.plot_lead(lead, peaks=None, title=None, show_grid=False, figsize=(12, 3), ax=None, *, fs=None, show=True, x_axis='time')[source]
Plot a single ECG lead waveform.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – ECG lead or raw signal array to plot.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – Optional R-peak indices to mark.title (
str|None) – Figure title. Defaults to the lead label.show_grid (
bool) – Draw ECG paper-style grid (defaultTrue).figsize (
tuple[float,float]) – Figure size in inches (default(12, 3)).ax (
Axes|None) – Existing axes to draw on. A new figure is created ifNone.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue). Set toFalseto return the figure without displaying.x_axis (
str) –"time"for seconds (default) or"samples"for sample indices (1, 2, …, N).
- Return type:
Figure
- ecgdatakit.plotting.plot_leads(leads, peaks_dict=None, title=None, show_grid=False, figsize=(12, None), share_x=True, *, fs=None, show=True, x_axis='time', rows=None, cols=None)[source]
Plot multiple leads in a grid layout (vertical stack by default).
- Parameters:
leads (
list[Lead] |ECGRecord|ndarray[tuple[Any,...],dtype[double]] |list[ndarray[tuple[Any,...],dtype[double]]]) – Leads to plot. Also accepts a 2-D numpy array (n_leads × n_samples) or a list of 1-D numpy arrays.peaks_dict (
dict[str,ndarray[tuple[Any,...],dtype[int_]]] |None) –{label: peaks_array}for per-lead R-peak markers.show_grid (
bool) – Draw ECG paper-style grid.figsize (
tuple[float,float|None]) – Width is fixed; height is auto-calculated (2 in per row) whenNone.share_x (
bool) – Share the x-axis across all subplots (defaultTrue).fs (
int|None) – Sample rate in Hz. Required when leads is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.rows (
int|None) – Number of rows in the subplot grid. Derived from cols or defaults to one row per lead when neither is given.cols (
int|None) – Number of columns in the subplot grid (default1).
- Return type:
Figure
- ecgdatakit.plotting.plot_12lead(leads, record=None, title=None, show_grid=False, figsize=(12, None), share_x=True, *, fs=None, show=True, x_axis='time', rows=None, cols=None)[source]
Plot 12 leads with standard lead names (I, II, III, aVR, …, V6).
Unlike
plot_leads(), this function assigns the standard 12-lead names when the input contains unnamed leads (e.g. a raw numpy array). The full signal is plotted without cropping.- Parameters:
leads (
list[Lead] |ECGRecord|ndarray[tuple[Any,...],dtype[double]] |list[ndarray[tuple[Any,...],dtype[double]]]) – Leads (or full record) to plot. Also accepts a 2-D numpy array (n_leads × n_samples) or a list of 1-D numpy arrays.record (
ECGRecord|None) – If provided, a header with patient/device/measurement info is shown.show_grid (
bool) – Draw ECG paper-style grid (defaultTrue).figsize (
tuple[float,float|None]) – Width is fixed; height is auto-calculated (2 in per row) whenNone.share_x (
bool) – Share the x-axis across all subplots (defaultTrue).fs (
int|None) – Sample rate in Hz. Required when leads is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.rows (
int|None) – Number of rows in the subplot grid. Derived from cols or defaults to one row per lead when neither is given.cols (
int|None) – Number of columns in the subplot grid (default1).
- Return type:
Figure
- ecgdatakit.plotting.plot_peaks(lead, peaks=None, title=None, figsize=(12, 3), ax=None, *, fs=None, show=True, x_axis='time')[source]
Plot lead with R-peak markers and RR interval annotations.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – ECG lead or raw signal array to plot.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices. Auto-detected ifNone.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.title (str | None)
ax (Axes | None)
- Return type:
Figure
- ecgdatakit.plotting.plot_beats(lead, beats=None, peaks=None, overlay=True, figsize=(8, 5), ax=None, *, fs=None, show=True)[source]
Plot segmented heartbeats.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Source ECG lead or raw signal array.beats (
list[Lead] |None) – Pre-segmented beats. Segmented automatically ifNone.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices for segmentation.overlay (
bool) –True: overlay all beats;False: waterfall display.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).ax (Axes | None)
- Return type:
Figure
- ecgdatakit.plotting.plot_average_beat(lead, peaks=None, before=0.2, after=0.4, figsize=(6, 4), ax=None, *, fs=None, show=True)[source]
Plot ensemble-averaged beat with ±1 SD shading.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – Source ECG lead or raw signal array.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices.before (
float) – Seconds before R-peak.after (
float) – Seconds after R-peak.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).ax (Axes | None)
- Return type:
Figure
- ecgdatakit.plotting.plot_spectrum(lead, method='welch', figsize=(10, 4), ax=None, *, fs=None, show=True)[source]
Plot power spectral density or FFT magnitude spectrum.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – ECG lead or raw signal array to analyse.method (
str) –"welch"for PSD or"fft"for magnitude spectrum.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).ax (Axes | None)
- Return type:
Figure
- ecgdatakit.plotting.plot_spectrogram(lead, nperseg=256, figsize=(12, 4), ax=None, *, fs=None, show=True)[source]
Plot time-frequency spectrogram (STFT).
- Parameters:
- Return type:
Figure
- ecgdatakit.plotting.plot_rr_tachogram(rr_ms, figsize=(10, 3), ax=None, *, show=True)[source]
Plot RR interval tachogram.
- ecgdatakit.plotting.plot_poincare(rr_ms, figsize=(6, 6), ax=None, *, show=True)[source]
Poincaré plot: RR(n) vs RR(n+1) with SD1/SD2 ellipse.
- ecgdatakit.plotting.plot_hrv_summary(rr_ms, figsize=(14, 8), *, show=True)[source]
Combined HRV dashboard: tachogram, Poincaré, frequency bands, metrics.
- ecgdatakit.plotting.plot_quality(leads, figsize=(10, 5), *, fs=None, show=True)[source]
Signal quality dashboard: SQI bar chart per lead.
- Parameters:
leads (
list[Lead] |ECGRecord|ndarray[tuple[Any,...],dtype[double]] |list[ndarray[tuple[Any,...],dtype[double]]]) – Leads to assess. Also accepts a 2-D numpy array (n_leads × n_samples) or a list of 1-D numpy arrays.fs (
int|None) – Sample rate in Hz. Required when leads is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).
- Return type:
Figure
- ecgdatakit.plotting.plot_report(record, figsize=(16, 20), *, show=True)[source]
Comprehensive ECG report page.
Includes header with patient/device info, 12-lead grid, rhythm strip, and quality indicators.
Interactive Plots (plotly)¶
- ecgdatakit.plotting.iplot_lead(lead, peaks=None, title=None, height=300, *, fs=None, show=True, x_axis='time')[source]
Interactive single lead with hover showing time/amplitude.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – ECG lead or raw signal array to plot.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – Optional R-peak indices to mark.height (
int) – Figure height in pixels.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.
- Return type:
Figure
- ecgdatakit.plotting.iplot_leads(leads, peaks_dict=None, title=None, height=None, *, fs=None, show=True, x_axis='time', rows=None, cols=None)[source]
Interactive leads in a grid layout (vertical stack by default).
- Parameters:
leads (
list[Lead] |ECGRecord|ndarray[tuple[Any,...],dtype[double]] |list[ndarray[tuple[Any,...],dtype[double]]]) – Leads to plot. Also accepts a 2-D numpy array (n_leads × n_samples) or a list of 1-D numpy arrays.peaks_dict (
dict[str,ndarray[tuple[Any,...],dtype[int_]]] |None) –{label: peaks_array}for per-lead peak markers.height (
int|None) – Figure height (auto-calculated ifNone).fs (
int|None) – Sample rate in Hz. Required when leads is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.rows (
int|None) – Number of rows in the subplot grid. Derived from cols or defaults to one row per lead when neither is given.cols (
int|None) – Number of columns in the subplot grid (default1).
- Return type:
Figure
- ecgdatakit.plotting.iplot_12lead(leads, record=None, title=None, height=None, *, fs=None, show=True, x_axis='time', rows=None, cols=None)[source]
Interactive 12-lead plot with standard lead names.
Unlike
iplot_leads(), this function assigns the standard 12-lead names (I, II, III, aVR, …, V6) when the input contains unnamed leads. The full signal is plotted without cropping.- Parameters:
leads (
list[Lead] |ECGRecord|ndarray[tuple[Any,...],dtype[double]] |list[ndarray[tuple[Any,...],dtype[double]]]) – Leads (or record) to plot. Also accepts a 2-D numpy array (n_leads × n_samples) or a list of 1-D numpy arrays.record (
ECGRecord|None) – Optional record for header annotations.height (
int|None) – Figure height in pixels (auto-calculated ifNone).fs (
int|None) – Sample rate in Hz. Required when leads is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.rows (
int|None) – Number of rows in the subplot grid. Derived from cols or defaults to one row per lead when neither is given.cols (
int|None) – Number of columns in the subplot grid (default1).
- Return type:
Figure
- ecgdatakit.plotting.iplot_peaks(lead, peaks=None, title=None, height=350, *, fs=None, show=True, x_axis='time')[source]
Interactive lead with R-peak markers.
Hover on peaks shows peak index, RR interval, and instantaneous HR.
- Parameters:
lead (
Lead|ndarray[tuple[Any,...],dtype[double]]) – ECG lead or raw signal array to plot.peaks (
ndarray[tuple[Any,...],dtype[int_]] |None) – R-peak indices. Auto-detected ifNone.fs (
int|None) – Sample rate in Hz. Required when lead is a numpy array.show (
bool) – Display the plot immediately (defaultTrue).x_axis (
str) –"time"for seconds (default) or"samples"for sample indices.title (str | None)
height (int)
- Return type:
Figure
- ecgdatakit.plotting.iplot_spectrum(lead, method='welch', height=400, *, fs=None, show=True)[source]
Interactive spectrum with frequency band highlighting.
- Parameters:
- Return type:
Figure
- ecgdatakit.plotting.iplot_rr_tachogram(rr_ms, height=300, *, show=True)[source]
Interactive RR interval tachogram.
- ecgdatakit.plotting.iplot_poincare(rr_ms, height=500, *, show=True)[source]
Interactive Poincaré plot with SD1/SD2 ellipse.
- ecgdatakit.plotting.iplot_report(record, height=1200, *, show=True, x_axis='time')[source]
Interactive full ECG report.