torch_ecg.utils.masks_to_waveforms#

torch_ecg.utils.masks_to_waveforms(masks: ndarray, class_map: Dict[str, int], fs: Real, mask_format: str = 'channel_first', leads: Sequence[str] | None = None) Dict[str, List[ECGWaveForm]][source]#

Convert masks into lists of ECGWaveForm for each lead.

Parameters:
  • masks (numpy.ndarray) – Wave delineation in the form of masks, of shape (n_leads, seq_len), or (seq_len,).

  • class_map (dict) – Class map, mapping names to waves to numbers from 0 to n_classes-1, the keys should contain “pwave”, “qrs”, “twave”.

  • fs (numbers.Real) – Sampling frequency of the signal corresponding to the masks, used to compute the duration of each waveform.

  • mask_format (str, default "channel_first") – Format of the mask, used only when masks.ndim = 2, can be “channel_last” (alias “lead_last”), or “channel_first” (alias “lead_first”), case insensitive.

  • leads (str or List[str], optional) – Names of leads corresponding to the channels of the masks.

Returns:

Each item value is a list containing the ECGWaveForm corr. to the lead. Each item key is from leads if leads is set, otherwise would be "lead_1", "lead_2", ..., "lead_n".

Return type:

dict

Examples

>>> class_map = {
...     "pwave": 1,
...     "qrs": 2,
...     "twave": 3,
... }
>>> masks = np.zeros((2, 500), dtype=int)  # 2 leads, 5000 samples
>>> masks[:, 100:150] = 1
>>> masks[:, 160:205] = 2
>>> masks[:, 250:340] = 3
>>> waveforms = masks_to_waveforms(masks, class_map=class_map, fs=500, leads=["III", "aVR"])
>>> waveforms["III"][0]
ECGWaveForm(name='pwave', onset=100, offset=150, peak=nan, duration=100.0)