Skip to content

Latest commit

Β 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Heartbeat Library Logo

πŸ«€ Heartbeat Library

The world's first TimescaleDB-powered ECG similarity search engine

Live Demo Backend Database Portfolio

FastAPI React Python Vercel Render GitHub


⚑ What Is This?

"Shazam, but for hearts."

Heartbeat Library answers one question: given any ECG waveform, find the most similar cardiac patterns across a 99.6-million-sample clinical database β€” in under half a second.

It uses Cascade DTW filtering + in-database PL/pgSQL DTW on TimescaleDB hypertables to retrieve ECG matches with zero network round-trips between filtering and scoring. Built on the MIT-BIH Arrhythmia and PTB-XL datasets from PhysioNet β€” 2,821 real patients, 48 hours of annotated cardiac data.


πŸš€ Live Links

Service URL Status
🌐 Frontend heartbeat-library-wt5u.vercel.app 🟒 Live
βš™οΈ Backend API heartbeat-library-api.onrender.com 🟒 Live
🐘 Database Timescale Cloud · AP-SOUTH-1 🟒 Live
πŸ“ GitHub github.com/sarthakNaikare/heartbeat-library βœ… Public
πŸ§‘β€πŸ’» Portfolio sarthaknaikare.github.io 🟒 Live

πŸ“Š Benchmark β€” The Numbers

All benchmarks run against 99,600,000 ECG samples across 2,821 patients

Method Wall Time Filter Latency Notes
🐒 Naive DTW `53.4s` β€” Full scan baseline
⚑ Cascade v1 `15.3s` `84ms` 99.9% candidate elimination
πŸ” Cascade v2 `17.0s` `84ms` Batch fetch variant
πŸ† In-DB DTW `0.46s` `3.1ms` PL/pgSQL, zero network trips
πŸ” Elasticsearch `~1s` `18–125ms` Industry benchmark

πŸ… Key Stats

``` πŸ“¦ Compression: 9 GB β†’ 839 MB (91.2% Β· 11.3Γ— ratio) 🎯 Precision@10: 100% (PVC Β· Normal Β· Atrial beats) ⚑ Filter speedup: 40Γ— (3.1ms vs 125ms Elasticsearch) πŸš€ Total speedup: 116Γ— (Naive DTW β†’ In-DB DTW) ```


πŸ—οΈ Architecture

``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ CLIENT (React) β”‚ β”‚ Splash Β· Search Β· Dashboard Β· Benchmarks Β· Upload β”‚ β”‚ heartbeat-library-wt5u.vercel.app β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ HTTPS REST β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ BACKEND (FastAPI) β”‚ β”‚ /search Β· /upload-search Β· /stats Β· /generate-report β”‚ β”‚ heartbeat-library-api.onrender.com β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ psycopg2 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ DATABASE (TimescaleDB 2.26) β”‚ β”‚ Timescale Cloud Β· AP-SOUTH-1 β”‚ β”‚ β”‚ β”‚ ecg_samples ← 99.6M rows Β· hypertable Β· compressed β”‚ β”‚ ecg_recordings ← 2,821 patients β”‚ β”‚ ecg_beats ← 201,680 individual beats β”‚ β”‚ ecg_segments ← 18,366 segment-level stats β”‚ β”‚ dtw_distance() ← PL/pgSQL in-database DTW function β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ```


🧠 How It Works β€” Cascade DTW

``` Stage 1 β€” Statistical Pre-filter (SQL) └─ Filter by mean Β± Οƒ, amplitude range β†’ eliminates 99.9% of candidates

Stage 2 β€” Euclidean Distance (Python) └─ Fast L2 distance on surviving candidates β†’ narrows to top-k

Stage 3 β€” In-Database DTW (PL/pgSQL) └─ Full DTW on final candidates, computed inside Timescale β†’ Zero network round-trips, sub-500ms wall time ```


πŸ—„οΈ Database Schema

```sql -- Hypertable: 99.6M rows, chunked by time, 91.2% columnar compressed CREATE TABLE ecg_samples ( sample_id BIGSERIAL, recording_id INTEGER REFERENCES ecg_recordings(recording_id), sample_index INTEGER, timestamp TIMESTAMPTZ NOT NULL, signal_value FLOAT4, lead TEXT DEFAULT 'MLII' ); SELECT create_hypertable('ecg_samples', 'timestamp');

-- In-database DTW β€” zero network trips CREATE OR REPLACE FUNCTION dtw_distance( seq1 FLOAT4[], seq2 FLOAT4[] ) RETURNS FLOAT8 LANGUAGE plpgsql AS $$ DECLARE -- Dynamic Time Warping matrix computed in pure SQL ... $$; ```


πŸ› οΈ Tech Stack

Layer Technology Why
πŸ—„οΈ Database TimescaleDB 2.26 Β· Timescale Cloud Hypertables, columnar compression, time-series native
βš™οΈ Backend FastAPI Β· Python 3.11.9 Β· Render Async, typed, auto-docs
🌐 Frontend React · Vercel Component-driven, edge CDN
πŸ“‘ Uptime UptimeRobot (5-min pings) Keeps Render free tier awake 24/7
πŸ“Š Data MIT-BIH + PTB-XL Β· PhysioNet Gold-standard clinical ECG datasets
πŸ”Œ Ingestion wfdb + psycopg2 COPY protocol Bulk-load 99.6M rows efficiently

🌟 Features

πŸ” Search

  • Patient + beat type selector β€” query by specific arrhythmia class
  • Demo pills β€” one-click preset queries for PVC, Normal, Atrial
  • Live similarity results β€” DTW scores, waveform visualizations
  • Upload your own ECG β€” drag & drop CSV/TXT/DAT, get instant matches

πŸ“Š Dashboard

  • Live stats pulled from Timescale Cloud API
  • Compression ratios, row counts, chunk statistics

πŸ“ˆ Benchmarks

  • Bar charts comparing all methods
  • Precision@10 table across beat types
  • Compression comparison vs raw storage

πŸ“„ Medical Report

  • Download a professional HTML report with DTW scores, similarity bars, methodology, and disclaimer

πŸ“ Project Structure

``` heartbeat-library/ β”œβ”€β”€ gui/ β”‚ β”œβ”€β”€ frontend/ ← React app (Vercel) β”‚ β”‚ β”œβ”€β”€ src/ β”‚ β”‚ β”‚ β”œβ”€β”€ App.js ← React.createElement (no JSX) β”‚ β”‚ β”‚ β”œβ”€β”€ Search.js β”‚ β”‚ β”‚ β”œβ”€β”€ Dashboard.js β”‚ β”‚ β”‚ β”œβ”€β”€ Benchmarks.js β”‚ β”‚ β”‚ └── Upload.js β”‚ β”‚ └── .env ← REACT_APP_API_URL β”‚ └── backend/ ← FastAPI app (Render) β”‚ β”œβ”€β”€ main.py ← /search /upload-search /stats /generate-report β”‚ β”œβ”€β”€ dtw.py ← Cascade DTW pipeline β”‚ β”œβ”€β”€ requirements.txt β”‚ └── .python-version ← Pinned 3.11.9 β”œβ”€β”€ ingestion/ ← wfdb β†’ TimescaleDB pipeline └── README.md ```


🚦 API Endpoints

``` GET /health β†’ Uptime check (HEAD + GET, for UptimeRobot) GET /stats β†’ Live database statistics POST /search β†’ Cascade DTW search by patient/beat type POST /upload-search β†’ Upload ECG file β†’ normalize β†’ cascade β†’ results POST /generate-report β†’ Professional HTML medical report download ```


πŸ“¦ Data

Dataset Patients Source
MIT-BIH Arrhythmia Database 48 PhysioNet
PTB-XL ECG Dataset 2,773 PhysioNet
Total 2,821 β€”

πŸ§‘β€πŸ’» Author

Sarthak Naikare
CS Graduate Β· MIT ADT University, Pune Β· 2025

Portfolio GitHub LinkedIn


πŸ—ΊοΈ Roadmap

  • πŸ“ Publish research paper β€” Computers in Biology and Medicine + VLDB Industrial Track
  • πŸš€ Migrate database to Fly.io (post Timescale Cloud trial)
  • πŸŽ™οΈ Add voiceover to demo video
  • πŸ”’ Build Ghost + TimescaleDB real-time network intrusion detection SIEM

Built with πŸ«€ and TimescaleDB in Pune, India

Live Demo

About

World's first TimescaleDB powered ECG similarity search engine. Sub 500ms cardiac waveform retrieval across 99.6 million samples from 2821 patients using cascade DTW filtering and in database PL/pgSQL DTW.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages