Download task_repo/solution.py from LianeMarilin/ECG-EdgeBench: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/datasets/LianeMarilin/ECG-EdgeBench/resolve/4a8de96917a9ac51ec5233bfa15968e85050c22e/task_repo/solution.py
- Command line
-
hf download hf://datasets/LianeMarilin/ECG-EdgeBench@4a8de96917a9ac51ec5233bfa15968e85050c22e/task_repo/solution.py
-
curl -L -o solution.py https://huggingface.co/datasets/LianeMarilin/ECG-EdgeBench/resolve/4a8de96917a9ac51ec5233bfa15968e85050c22e/task_repo/solution.py
2.7 kB
| #!/usr/bin/env python3 | |
| """Deliberately modest deterministic baseline; agents should improve it.""" | |
| from __future__ import annotations | |
| import argparse | |
| from pathlib import Path | |
| import sys | |
| import numpy as np | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.pipeline import make_pipeline | |
| from sklearn.preprocessing import StandardScaler | |
| def _features(signal: np.ndarray, rr: np.ndarray) -> np.ndarray: | |
| # Coarse morphology intentionally leaves substantial room for better signal models. | |
| n, channels, width = signal.shape | |
| bins = 18 | |
| trimmed = signal[:, :, : (width // bins) * bins] | |
| chunks = trimmed.reshape(n, channels, bins, -1) | |
| means = chunks.mean(axis=3).reshape(n, -1) | |
| stds = chunks.std(axis=3).reshape(n, -1) | |
| extrema = np.concatenate( | |
| [signal.min(axis=2), signal.max(axis=2), np.ptp(signal, axis=2)], axis=1 | |
| ) | |
| return np.concatenate([rr, means, stds, extrema], axis=1).astype(np.float32) | |
| def run(train_path: Path, test_path: Path, output_path: Path) -> None: | |
| with np.load(train_path, allow_pickle=False) as train: | |
| x_train = _features(train["signal"], train["rr"]) | |
| y_train = train["y"].astype(np.int64) | |
| with np.load(test_path, allow_pickle=False) as test: | |
| x_test = _features(test["signal"], test["rr"]) | |
| keep = y_train < 4 # Q is too sparse for the baseline. | |
| model = make_pipeline( | |
| StandardScaler(), | |
| LogisticRegression( | |
| C=0.7, | |
| class_weight="balanced", | |
| max_iter=250, | |
| random_state=20260920, | |
| solver="lbfgs", | |
| ), | |
| ) | |
| model.fit(x_train[keep], y_train[keep]) | |
| pred = model.predict(x_test).astype(np.int16) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| np.save(output_path, pred, allow_pickle=False) | |
| def self_test() -> None: | |
| rng = np.random.default_rng(7) | |
| sig = rng.normal(size=(12, 2, 252)).astype(np.float32) | |
| rr = rng.normal(size=(12, 5)).astype(np.float32) | |
| got = _features(sig, rr) | |
| assert got.shape == (12, 83), got.shape | |
| assert np.isfinite(got).all() | |
| print("self-test passed") | |
| def main() -> int: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--train", type=Path) | |
| parser.add_argument("--test", type=Path) | |
| parser.add_argument("--output", type=Path) | |
| parser.add_argument("--self-test", action="store_true") | |
| args = parser.parse_args() | |
| if args.self_test: | |
| self_test() | |
| return 0 | |
| if not (args.train and args.test and args.output): | |
| parser.error("--train, --test and --output are required") | |
| run(args.train, args.test, args.output) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |