Skip to content

Commit

Permalink
Merge pull request #13 from mat-kie/ext_algos
Browse files Browse the repository at this point in the history
migrate to external algorithm crate, add DFA1a analysis
  • Loading branch information
mat-kie authored Dec 31, 2024
2 parents b261b24 + 98ce3b1 commit 11794e9
Show file tree
Hide file tree
Showing 16 changed files with 1,068 additions and 725 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ log = { version = "0.4.22", features = [] }
serde = { version = "1.0.215", features = ["derive", "serde_derive"] }
serde_json = "1.0.133"
async-trait = "0.1.83"
mockall = "0.13.1"
rand = "0.8.5"
typetag = "0.2.18"
anyhow = { version = "1.0.94", features = ["backtrace"] }
rust-fsm = "0.7.0"
event_bridge = "0.3.1"
hrv-algos={ version = "0.4.2", features = ["serde"] }
rayon = "1.10.0"
[dev-dependencies]
mockall = "0.13.1"
tempdir = "0.3.7"
criterion = { version = "0.5", features = ["html_reports"] }
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# Hrv-rs

[![Pipeline Status](https://github.com/mat-kie/hrv-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/mat-kie/hrv-rs/actions/workflows/rust.yml)
[![Coverage](https://codecov.io/gh/mat-kie/hrv-rs/branch/main/graph/badge.svg?token=YOUR_CODECOV_TOKEN)](https://codecov.io/gh/mat-kie/hrv-rs)


**Hrv-rs** is a Rust-based application designed to analyze Heart Rate Variability (HRV) using Bluetooth Low Energy (BLE) chest straps.

## Disclaimer

**This project is in a very early stage and is not intended for any medical applications.**

## Features

- **Bluetooth Connectivity**:
- Scan and connect to BLE chest straps that provide R-R interval data.
- **HRV Analysis**:
- Compute HRV metrics such as RMSSD, SDRR, SD1, SD2, and Poincaré plots.
- Visualize HRV statistics in real-time.

## HRV Metrics

- **RMSSD**: Root Mean Square of Successive Differences between R-R intervals.
- **SDRR**: Standard Deviation of R-R intervals.
- **SD1/SD2**: Short- and long-term HRV metrics derived from Poincaré plots.
Expand All @@ -25,10 +27,12 @@
## Getting Started

### Prerequisites

- A BLE-compatible chest strap for HRV measurement.
- A system with BLE support.

### Installation

1. Clone the repository:
```bash
git clone https://github.com/mat-kie/hrv-rs.git
Expand All @@ -48,6 +52,7 @@
## Code Structure

### Architecture

The project uses a modular, event-driven MVC architecture.

### Modules
Expand Down
4 changes: 4 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage:
range: 50..70
status:
project: false
21 changes: 6 additions & 15 deletions src/api/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use anyhow::Result;
use async_trait::async_trait;
use btleplug::api::Central;
use std::{path::PathBuf, sync::Arc};
use time::Duration;
use tokio::sync::RwLock;

use super::model::{BluetoothModelApi, MeasurementModelApi};
Expand Down Expand Up @@ -60,16 +59,6 @@ pub trait StorageEventApi {
///
/// * `path` - A `PathBuf` representing the file path to which to store data.
async fn store_to_file(&mut self, path: PathBuf) -> Result<()>;

/// Store the recorded measurement.
///
/// This method handles the storage of a new measurement.
async fn new_measurement(&mut self) -> Result<()>;

/// Store the recorded measurement.
///
/// This method handles the storage of the recorded measurement.
async fn store_recorded_measurement(&mut self) -> Result<()>;
}

/// StorageApi trait
Expand All @@ -84,8 +73,10 @@ pub trait StorageEventApi {
pub trait StorageApi<MT: MeasurementModelApi> {
/// Get the active measurement.
///
/// This method returns a reference to the active measurement, if any.
fn get_active_measurement(&mut self) -> &Option<Arc<RwLock<MT>>>;
/// This method returns a reference to the measurement at index.
fn get_measurement(&self, index: usize) -> Result<Arc<RwLock<MT>>>;

fn store_measurement(&mut self, measurement: Arc<RwLock<MT>>) -> Result<()>;
}

/// MeasurementApi trait
Expand All @@ -101,8 +92,8 @@ pub trait MeasurementApi: MeasurementModelApi {
///
/// # Arguments
///
/// * `window` - A `Duration` representing the length of the statistics window.
async fn set_stats_window(&mut self, window: Duration) -> Result<()>;
/// * `window` - Number of samples to consider for statistics
async fn set_stats_window(&mut self, window: usize) -> Result<()>;

/// Set the outlier filter.
///
Expand Down
38 changes: 20 additions & 18 deletions src/api/model.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! This module defines the read only API for interacting with various models.
//! It provides interfaces for accessing data related to HRV measurements,
//! Bluetooth adapters, and stored acquisitions.
use crate::model::{
bluetooth::{AdapterDescriptor, DeviceDescriptor, HeartrateMessage},
hrv::PoincarePoints,
};
use anyhow::Result;
use btleplug::api::BDAddr;
use std::{fmt::Debug, sync::Arc};
use time::{Duration, OffsetDateTime};
use tokio::sync::RwLock;

use crate::model::{
bluetooth::{AdapterDescriptor, DeviceDescriptor, HeartrateMessage},
hrv::{HrvSessionData, HrvStatistics},
};

/// `MeasurementModelApi` trait.
///
/// Defines the interface for managing measurement-related data, including runtime measurements,
Expand All @@ -28,17 +28,25 @@ pub trait MeasurementModelApi: Debug + Send + Sync {
/// An optional `HeartrateMessage` representing the most recent measurement.
fn get_last_msg(&self) -> Option<&HeartrateMessage>;

/// Retrieves the current HRV statistics.
///
/// # Returns
/// A reference to an optional `HrvStatistics` containing computed HRV data.
fn get_hrv_stats(&self) -> Option<&HrvStatistics>;
fn get_rmssd(&self) -> Option<f64>;
fn get_sdrr(&self) -> Option<f64>;
fn get_sd1(&self) -> Option<f64>;
fn get_sd2(&self) -> Option<f64>;
fn get_hr(&self) -> Option<f64>;
fn get_dfa1a(&self) -> Option<f64>;

fn get_rmssd_ts(&self) -> Vec<[f64; 2]>;
fn get_sdrr_ts(&self) -> Vec<[f64; 2]>;
fn get_sd1_ts(&self) -> Vec<[f64; 2]>;
fn get_sd2_ts(&self) -> Vec<[f64; 2]>;
fn get_hr_ts(&self) -> Vec<[f64; 2]>;
fn get_dfa1a_ts(&self) -> Vec<[f64; 2]>;

/// Retrieves the configured statistics window.
///
/// # Returns
/// A reference to an optional `Duration` representing the analysis window size.
fn get_stats_window(&self) -> Option<&Duration>;
fn get_stats_window(&self) -> Option<usize>;

/// Getter for the filter parameter value (fraction of std. dev).
///
Expand All @@ -50,13 +58,7 @@ pub trait MeasurementModelApi: Debug + Send + Sync {
///
/// # Returns
/// A vector of `[f64; 2]` pairs representing the Poincare points.
fn get_poincare_points(&self) -> Vec<[f64; 2]>;

/// Retrieves the session data.
///
/// # Returns
/// A reference to the `HrvSessionData`.
fn get_session_data(&self) -> &HrvSessionData;
fn get_poincare_points(&self) -> Result<PoincarePoints>;

/// Retrieves the elapsed time since the start of the acquisition.
///
Expand Down
Loading

0 comments on commit 11794e9

Please sign in to comment.