DEV Community

Beck_Moulton
Beck_Moulton

Posted on

From Heartbeats to Insights: Real-time HRV Anomaly Detection with PyTorch Lightning & Transformers

Ever wondered why your smartwatch suddenly pings you to "take a breath" exactly two minutes before a stressful meeting? It’s not magicβ€”it’s the power of HRV anomaly detection.

Heart Rate Variability (HRV), specifically the RR intervals (the time between successive heartbeats), is a goldmine for understanding the autonomic nervous system. However, processing this high-frequency, noisy data requires more than just simple moving averages. To truly predict stress spikes and burnout, we need a Time-Series Transformer capable of capturing long-range dependencies in physiological data.

In this tutorial, we’ll build a high-performance pipeline using PyTorch Lightning for our model architecture and InfluxDB for handling the massive stream of wearable data. If you are looking for more production-ready patterns regarding health-tech integrations, I highly recommend checking out the advanced deep-dives over at Wellally's Engineering Blog.


πŸ—οΈ The Architecture

Before we dive into the tensors, let’s look at how the data flows from a wearable sensor to a real-time alert dashboard.

graph TD
    A[Wearable Device/Sensor] -->|High-freq RR Intervals| B(InfluxDB)
    B -->|Windowed Batching| C{PyTorch Lightning Transformer}
    C -->|Anomaly Score| D[Stress Threshold Logic]
    D -->|Alert/Metric| E(Grafana Dashboard)
    D -->|Feedback Loop| B
Enter fullscreen mode Exit fullscreen mode

This setup ensures that we aren't just doing "batch processing" once a day, but actually reacting to physiological shifts in near real-time.


πŸ› οΈ Prerequisites

To follow along, you'll need:

  • Python 3.9+
  • PyTorch & PyTorch Lightning: For the heavy lifting.
  • InfluxDB: Our time-series database of choice.
  • Numpy: For signal pre-processing.

🧠 Step 1: Defining the Time-Series Transformer

Standard Transformers are great for text, but for HRV data, we need to handle continuous numerical values. We'll use a PositionalEncoding layer to help the model understand the sequence of heartbeats.

import torch
import torch.nn as nn
import lightning as L

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, max_len=5000):
        super().__init__()
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-torch.log(torch.tensor(10000.0)) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe)

    def forward(self, x):
        return x + self.pe[:x.size(1), :]

class HRVTransformer(L.LightningModule):
    def __init__(self, input_dim=1, model_dim=64, n_heads=4, n_layers=3):
        super().__init__()
        self.save_hyperparameters()

        self.input_projection = nn.Linear(input_dim, model_dim)
        self.pos_encoder = PositionalEncoding(model_dim)

        encoder_layer = nn.TransformerEncoderLayer(
            d_model=model_dim, nhead=n_heads, batch_first=True
        )
        self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=n_layers)

        # Output is a single value: predicted next RR-interval or anomaly score
        self.regressor = nn.Linear(model_dim, 1)
        self.criterion = nn.MSELoss()

    def forward(self, x):
        # x shape: (batch, seq_len, 1)
        x = self.input_projection(x)
        x = self.pos_encoder(x)
        x = self.transformer(x)
        return self.regressor(x[:, -1, :]) # Predict the last point

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = self.criterion(y_hat, y)
        self.log("train_loss", loss, prog_bar=True)
        return loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=1e-3)
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Step 2: Data Ingestion with InfluxDB

HRV data is inherently time-series. Instead of messy CSVs, we use InfluxDB to query "windows" of heartbeats. Here is how we fetch the last 100 RR-intervals for inference:

from influxdb_client import InfluxDBClient

def fetch_hrv_window(bucket="health_metrics", window_size=100):
    client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
    query_api = client.query_api()

    query = f'''
    from(bucket: "{bucket}")
      |> range(start: -1h)
      |> filter(fn: (r) => r["_measurement"] == "rr_interval")
      |> last()
      |> limit(n: {window_size})
    '''
    # Process the result into a Numpy array...
    # (Pre-processing involves removing outliers > 2000ms and < 300ms)
    return data
Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 3: Deployment & Real-time Visualization

Once the model is trained with PyTorch Lightning, we export it via TorchScript or ONNX for high-speed inference. We then pipe the Anomaly Score (the difference between the predicted RR-interval and the actual one) back into InfluxDB.

The "Official" Way to Scale

While this tutorial covers the core ML logic, deploying this at scale for thousands of concurrent users requires robust data validation and privacy-first architecture. For detailed architectural patterns on HIPAA-compliant cloud setups and advanced time-series forecasting, check out the specialized guides at wellally.tech/blog. They cover the "production" side of things that usually follows a successful PoC.


πŸ“ˆ Visualizing in Grafana

The final step is connecting Grafana to your InfluxDB instance.

  1. Create a Graph Panel: Plot raw RR intervals.
  2. Add an Overlay: Plot the anomaly_score generated by our Transformer.
  3. Set Alerts: If anomaly_score > threshold for more than 30 seconds, trigger a Webhook to send a notification to the user's phone. πŸ“±

πŸ’‘ Conclusion

Building an HRV anomaly detector with a Time-Series Transformer allows us to look beyond simple heart rate and see the complex rhythms of human stress. By leveraging PyTorch Lightning, we keep our code modular and research-friendly, while InfluxDB provides the backbone for real-time data handling.

What's next?

  • Try adding multi-modal inputs like SPO2 or activity levels! πŸƒβ€β™‚οΈ
  • Experiment with "Attention Maps" to see which heartbeats the model finds most suspicious.

If you enjoyed this build, drop a comment below or follow for more "Learning in Public" deep dives!

Top comments (0)