Data API¶
Auto-generated from docstrings.
ecgen.types¶
Data contracts shared by datasets, conditioners, and generative families.
ECGBatch
dataclass
¶
A collated batch of ECGs and their condition fields.
Attributes:
| Name | Type | Description |
|---|---|---|
signal |
Tensor
|
|
lead_names |
List[str]
|
Names of the |
fs |
int
|
Sampling rate in Hz. |
cond |
Dict[str, Tensor]
|
Raw condition fields by name (e.g. |
cond_missing |
Dict[str, Tensor]
|
Per-field missing masks, |
obs_mask |
Optional[Tensor]
|
|
meta |
Dict[str, Any]
|
Record identifiers etc.; never fed to a model. |
Source code in src/ecgen/types.py
ConditionOutput
dataclass
¶
Encoded conditions, in the slots a backbone can consume.
Attributes:
| Name | Type | Description |
|---|---|---|
vector |
Optional[Tensor]
|
|
tokens |
Optional[Tensor]
|
|
tokens_mask |
Optional[Tensor]
|
|
dense |
Optional[Tensor]
|
|
Source code in src/ecgen/types.py
get_signal
¶
Return the [B, C, T] ECG tensor from any supported batch format.
Accepts an :class:ECGBatch, a dict with "signal" or the legacy
"ecg_signals" key, or a legacy (ecg, features) tuple.
Source code in src/ecgen/types.py
ecgen.data.datasets.base¶
Dataset contract.
BaseECGDataset
¶
Bases: Dataset, ABC
Base class for ECG datasets.
__getitem__ returns a dict::
{
"signal": FloatTensor [C, T],
"cond": {field_name: Tensor}, # may be empty
"cond_missing": {field_name: BoolTensor},
"meta": {...}, # ids; not fed to models
}
and the dataset exposes :attr:lead_names and :attr:fs, which
:class:ecgen.data.collate.ECGCollate attaches to every batch.
Source code in src/ecgen/data/datasets/base.py
field_normalizers
property
¶
Normalizers of numeric cond fields, by field name.
Saved in checkpoints so a model can normalize raw values at sampling
time. Default: :attr:feature_normalizer for the measurements field.
scale_signal
¶
Apply the dataset-level amplitude scaling to a [C, T] record.
Source code in src/ecgen/data/datasets/base.py
ecgen.data.datasets.mimic¶
MIMIC-IV-ECG dataset (dict samples).
Layout under root:
files/p{XXXX}/p{subject_id}/s{study_id}/{study_id}.hea/.dat(WFDB, 12 leads, 500 Hz, 10 s)machine_measurements.csv(one row per study; interval/axis measurements and report lines)
Records are stored in the lead order I, II, III, aVR, aVF, aVL, V1..V6 — note
aVF before aVL, unlike the standard clinical order.
MIMICIVECG
¶
Bases: BaseECGDataset
MIMIC-IV-ECG records with machine measurements as a numeric condition field.
Samples are dicts (see :class:~ecgen.data.datasets.base.BaseECGDataset) with
cond["measurements"] = the normalized measurements columns.
The train/val/test split is by subject and seeded, and matches the legacy
MIMICIVECGDataset when missing="drop" (the default). The measurement
normalizer is always fit on the train split, whichever split is loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str
|
MIMIC-IV-ECG directory (contains |
required |
split
|
str
|
|
'train'
|
val_split
|
float
|
Fraction of subjects in the validation split. |
0.1
|
test_split
|
float
|
Fraction of subjects in the test split. |
0.1
|
seed
|
int
|
Split seed. |
42
|
max_samples
|
Optional[int]
|
Keep only the first N rows of the split (for quick runs). |
None
|
skip_missing_check
|
bool
|
Skip the check that each record's |
False
|
leads
|
LeadSpec
|
|
None
|
num_leads
|
int
|
Channel count when |
12
|
seq_length
|
int
|
Samples per record (padded/truncated). |
5000
|
scale
|
Scale
|
|
'zscore'
|
measurements
|
Optional[Sequence[str]]
|
Columns for |
None
|
missing
|
str
|
|
'drop'
|
feature_normalizer
|
Optional[FeatureNormalizer | Dict[str, Any]]
|
Use these statistics instead of fitting on train (e.g. those stored in a checkpoint). |
None
|
report
|
bool
|
Add |
False
|
Source code in src/ecgen/data/datasets/mimic.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
load_ecg
¶
Raw [C, T] signal in mV, with the configured leads and length.
Source code in src/ecgen/data/datasets/mimic.py
ecgen.data.datasets.synthetic¶
Synthetic ECG-like dataset for tests and smoke runs (no data download needed).
SyntheticECG
¶
Bases: BaseECGDataset
Deterministic periodic "beats" with per-record heart rate and amplitude.
Every record carries the ground truth that produced it, so conditional models can be tested against known targets:
cond["measurements"]:[heart_rate (bpm), amplitude (mV)], normalized with :attr:feature_normalizer(fixed statistics of the sampling ranges);cond["rhythm"]: class 0 = brady (< 60 bpm), 1 = normal, 2 = tachy (> 100 bpm);cond["labels"]: multi-hot[brady, tachy, high_amplitude (> 1.25 mV)];cond["report"]: a templated text, e.g."sinus tachycardia, high voltage".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split
|
str
|
|
'train'
|
num_samples
|
int
|
Records in this split. |
64
|
leads
|
LeadSpec
|
Lead spec (see :func: |
None
|
seq_length
|
int
|
Samples per record. |
5000
|
fs
|
int
|
Sampling rate in Hz. |
500
|
scale
|
Scale
|
As in :func: |
'none'
|
seed
|
int
|
Base seed; record |
0
|
Source code in src/ecgen/data/datasets/synthetic.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
render
classmethod
¶
The [C, T] signal for a heart rate (bpm) and R amplitude (mV).
Each beat has P, Q, R, S and T waves with fixed widths in seconds (the QT interval shortens with the RR interval, Bazett-style); every lead is the same beat train times a random gain and sign, plus white noise.
Source code in src/ecgen/data/datasets/synthetic.py
ecgen.data.leads¶
Lead names and lead selection.
resolve_leads
¶
Turn a lead spec into a list of lead names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
LeadSpec
|
|
required |
available
|
Sequence[str]
|
Leads the dataset stores, in file order. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
Lead names in output channel order. |
Source code in src/ecgen/data/leads.py
lead_indices
¶
Indices into available for each name in names.
maybe_lead_names
¶
names if given, else ["lead0", "lead1", ...].
derive_standard12
¶
Complete 8 independent leads (I, II, V1–V6, any order) to the standard 12 using Einthoven / Goldberger: III = II − I, aVR = −(I + II)/2, aVL = I − II/2, aVF = II − I/2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
|
required |
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in src/ecgen/data/leads.py
ecgen.data.normalizers¶
Normalization of numeric condition fields.
FeatureNormalizer
dataclass
¶
Per-column standardization, (x - mean) / std, with missing values.
Fit it on the training split only and reuse it for validation, test, and
sampling; :meth:to_dict / :meth:from_dict make it storable in a checkpoint.
Missing values (NaN) are ignored when fitting and come out of
:meth:transform as 0 with missing=True.
Source code in src/ecgen/data/normalizers.py
transform
¶
Normalize one row of raw values (ordered as :attr:columns).
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
|
Source code in src/ecgen/data/normalizers.py
ecgen.data.collate¶
Collation of dict samples into :class:ecgen.types.ECGBatch.
ECGCollate
¶
collate_fn that stacks dict samples into an :class:ECGBatch.
Tensors under signal, cond, cond_missing and obs_mask are
stacked; meta values are gathered into lists. With a mask_generator,
each sample without its own obs_mask gets one drawn from it.
Source code in src/ecgen/data/collate.py
ecgen.data.datamodule¶
Generic LightningDataModule over any registered dataset.
ECGDataModule
¶
Bases: LightningDataModule
Builds train/val/test splits of one dataset and yields :class:~ecgen.types.ECGBatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Mapping[str, Any]
|
Dataset spec, |
required |
batch_size
|
int
|
Batch size for all splits. |
32
|
num_workers
|
int
|
DataLoader workers. |
4
|
pin_memory
|
Optional[bool]
|
Defaults to |
None
|
split_params
|
Optional[Mapping[str, Mapping[str, Any]]]
|
Per-split overrides of |
None
|
masks
|
Optional[Mapping[str, Any]]
|
Observation-mask spec (see :mod: |
None
|
The train split's feature_normalizer (if any) is passed to the val/test
datasets that accept one, and saved in checkpoints via :meth:state_dict.
Source code in src/ecgen/data/datamodule.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
reference_dataset
property
¶
Any built split (train preferred), for lead names, fs, etc.