deepecgkit.features¶
Deep embedding extraction. Every registered model exposes extract_features and feature_dim;
this module runs that feature path over data, handling batching, devices, and label alignment.
extract_features
¶
extract_features(
data: FeatureInput,
model: Module | None = None,
checkpoint: str | Path | None = None,
weights: str | Path | None = None,
model_name: str | None = None,
input_channels: int | None = None,
output_size: int | None = None,
split: str = "test",
**kwargs: Any,
) -> FeatureSet
Extract deep embeddings in one call.
Exactly one weight source must be given: an already-built model, a checkpoint path, or pretrained weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FeatureInput
|
An array, tensor, file path, Dataset, DataLoader, or ECGDataModule |
required |
model
|
Module | None
|
An already-built model |
None
|
checkpoint
|
str | Path | None
|
Path to a checkpoint |
None
|
weights
|
str | Path | None
|
Registered pretrained weight name, or a path to a weights file |
None
|
model_name
|
str | None
|
Registered model name; optional for a checkpoint that records its own |
None
|
input_channels
|
int | None
|
Lead count, when it cannot be recovered from the weights |
None
|
output_size
|
int | None
|
Classifier width, when it cannot be recovered |
None
|
split
|
str
|
Which split to use when data is an ECGDataModule |
'test'
|
**kwargs
|
Any
|
Forwarded to FeatureExtractor |
{}
|
Returns:
| Type | Description |
|---|---|
FeatureSet
|
The embeddings, with labels when the input supplied them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If not exactly one weight source is given, or a checkpoint neither records its architecture nor is given a model name. |
Example
result = extract_features(signals, checkpoint="best.ckpt", model_name="kanres") result.features.shape (100, 64)
Source code in deepecgkit/features.py
FeatureExtractor
¶
Bases: TransformerMixin, BaseEstimator
Runs a model's feature path over data and returns the embeddings.
The extractor is a scikit-learn transformer, so it drops straight into a
Pipeline ahead of any downstream estimator. Note that sklearn's clone
deep-copies the wrapped model, which doubles its memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Any model exposing extract_features and feature_dim |
required |
model_name
|
str | None
|
Registry name recorded on the results, for provenance |
None
|
batch_size
|
int
|
Samples per forward pass, ignored for a supplied DataLoader |
32
|
device
|
str
|
Device name, or auto to prefer CUDA then MPS then CPU |
'auto'
|
dtype
|
str
|
Precision of the returned features, float32 or float16 |
'float32'
|
num_workers
|
int
|
Worker processes for loaders this extractor constructs |
0
|
progress
|
bool
|
Show a progress bar when tqdm is installed |
False
|
Example
extractor = FeatureExtractor.from_checkpoint("best.ckpt", "kanres") result = extractor.extract(signals) result.features.shape (100, 64)
Source code in deepecgkit/features.py
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
feature_dim
property
¶
Width of the embedding the wrapped model produces.
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the model does not declare feature_dim. |
input_channels
property
¶
Lead count recovered from the weights, or None when unrecoverable.
from_checkpoint
classmethod
¶
from_checkpoint(
checkpoint: str | Path,
model_name: str | None = None,
input_channels: int | None = None,
output_size: int | None = None,
**kwargs: Any,
) -> FeatureExtractor
Build an extractor from a saved checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
str | Path
|
Path to an ECGTrainer checkpoint, a bare state dict, or a legacy PyTorch Lightning checkpoint |
required |
model_name
|
str | None
|
Registered model name matching the weights; optional when the checkpoint records it |
None
|
input_channels
|
int | None
|
Lead count, required for architectures whose weights do not encode it |
None
|
output_size
|
int | None
|
Classifier width, normally recovered from the weights |
None
|
**kwargs
|
Any
|
Forwarded to the constructor |
{}
|
Returns:
| Type | Description |
|---|---|
FeatureExtractor
|
An extractor wrapping the loaded model. |
Source code in deepecgkit/features.py
from_pretrained
classmethod
¶
from_pretrained(
weights: str | Path,
model_name: str | None = None,
**kwargs: Any,
) -> FeatureExtractor
Build an extractor from registered pretrained weights or a weights file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
str | Path
|
Name of registered pretrained weights, or a path |
required |
model_name
|
str | None
|
Registered model name; inferred from the weight registry when the weights are registered |
None
|
**kwargs
|
Any
|
Forwarded to the constructor, except that recognised model constructor arguments are passed to the model instead |
{}
|
Returns:
| Type | Description |
|---|---|
FeatureExtractor
|
An extractor wrapping the loaded model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the model name is neither given nor inferable. |
Source code in deepecgkit/features.py
extract
¶
Run the model's feature path over the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FeatureInput
|
An array, tensor, file path, Dataset, DataLoader, or ECGDataModule |
required |
split
|
str
|
Which split to use when data is an ECGDataModule, one of train, val, test, or all |
'test'
|
Returns:
| Type | Description |
|---|---|
FeatureSet
|
The embeddings, with labels when the input supplied them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the split or dtype is unknown. |
RuntimeError
|
If the model returns a non-2D representation. |
Source code in deepecgkit/features.py
fit
¶
Return self; the extractor holds a trained model and learns nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
Ignored, present for scikit-learn compatibility |
None
|
y
|
Any
|
Ignored, present for scikit-learn compatibility |
None
|
Returns:
| Type | Description |
|---|---|
FeatureExtractor
|
This extractor. |
Source code in deepecgkit/features.py
transform
¶
Return the embeddings as a plain array, for use inside a Pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
FeatureInput
|
Any input accepted by extract |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (num_samples, feature_dim). |
Source code in deepecgkit/features.py
get_feature_names_out
¶
Return names for the embedding dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
Any | None
|
Ignored, present for scikit-learn compatibility |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of feature names. |
Source code in deepecgkit/features.py
FeatureSet
dataclass
¶
Embeddings extracted from a model, with any aligned labels.
Attributes:
| Name | Type | Description |
|---|---|---|
features |
ndarray
|
Array of shape (num_samples, feature_dim) |
labels |
ndarray | None
|
Aligned labels of shape (num_samples,) for single-label data or (num_samples, num_classes) for multi-label data, or None when the input carried no labels |
model_name |
str | None
|
Registry name of the model that produced the features |
Source code in deepecgkit/features.py
save
¶
Write the features to disk, choosing the format from the suffix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Destination ending in .npz, .npy, or .csv. The .npz form keeps the labels and metadata; .npy stores features only; .csv writes one column per feature plus a label column. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the suffix is not one of the three supported forms. |