Package curating cohesive training & inference pipelines for ECG analysis.
Project description
ecg-transform
Installation
pip install ecg-transform
Example
Here is an example of defining an input schema and transforms,
from ecg_transform.input import ECGInputSchema
from ecg_transform.transforms.common import (
LinearResample,
MinMaxNormalize,
Pad,
ReorderLeads,
Segment,
)
LEAD_ORDER = ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6']
SAMPLE_RATE = 500
N_SAMPLES = 5000
SCHEMA = ECGInputSchema(
sampling_rate=SAMPLE_RATE,
expected_lead_order=LEAD_ORDER,
required_num_samples=N_SAMPLES,
)
TRANSFORMS = [
ReorderLeads(
expected_order=LEAD_ORDER,
missing_lead_strategy='raise',
),
LinearResample(desired_sampling_rate=SAMPLE_RATE),
MinMaxNormalize(),
Segment(segment_length=N_SAMPLES),
Pad(pad_to_num_samples=N_SAMPLES, value=0)
]
Here is an example of how ecg-transform could be used in PyTorch (which we do not require to minimize dependencies),
from typing import List
from itertools import chain
from scipy.io import loadmat
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data.dataloader import DataLoader
from ecg_transform.inp import ECGInput, ECGInputSchema
from ecg_transform.transforms.base import ECGTransform
from ecg_transform.sample import ECGMetadata, ECGSample
class ECGDataset(Dataset):
def __init__(
self,
schema,
transforms,
file_paths,
):
self.schema = schema
self.transforms = transforms
self.file_paths = file_paths
def __len__(self):
return len(self.file_paths)
def __getitem__(self, idx):
mat = loadmat(self.file_paths[idx])
metadata = ECGMetadata(
sample_rate=int(mat['org_sample_rate'][0, 0]),
num_samples=mat['feats'].shape[1],
lead_names=['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'],
unit=None,
input_start=0,
input_end=mat['feats'].shape[1],
)
inp = ECGInput(mat['feats'], metadata)
sample = ECGSample(
inp,
self.schema,
self.transforms,
)
return torch.from_numpy(sample.out).float(), self.file_paths[idx]
def collate_fn(inps):
sample_ids = list(
chain.from_iterable([[inp[1]]*inp[0].shape[0] for inp in inps])
)
return torch.concatenate([inp[0] for inp in inps]), sample_ids
def file_paths_to_loader(
file_paths: List[str],
schema: ECGInputSchema,
transforms: List[ECGTransform],
batch_size = 64,
num_workers = 7,
):
dataset = ECGDataset(
schema,
transforms,
file_paths,
)
return DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
sampler=None,
shuffle=False,
collate_fn=collate_fn,
drop_last=False,
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ecg_transform-0.1.0.tar.gz
(9.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ecg_transform-0.1.0.tar.gz.
File metadata
- Download URL: ecg_transform-0.1.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a114755787306241fb25e60df88d7ae31b2ed31902f247dbab46eff7c476f0d
|
|
| MD5 |
f4cddf32973ee7a8e9f571e8ab5b6282
|
|
| BLAKE2b-256 |
c8c9a69114f4c49155f3b918cfd5484f304b1554d2ea4bf0872d1c2cd0598eac
|
File details
Details for the file ecg_transform-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ecg_transform-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a1bed2e52bb05a8b50b9e73d111a784c6274844800f513ca3dc8af7c81fb701
|
|
| MD5 |
700a69f0f019f96a1de22f6f0d68d258
|
|
| BLAKE2b-256 |
1bc77274431bc91edb885bb74439c746a4cad2b514aaa843ad859956efac7d86
|