Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Claude-generated health tracker demo #311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions HealthTracker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Dependency directories
node_modules/

# Build outputs
dist/
build/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# OS-specific files
.DS_Store
Thumbs.db

# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Editor directories and files
.idea/
.vscode/
*.swp
*.swo
63 changes: 63 additions & 0 deletions HealthTracker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# HealthTracker

A TypeScript library with useful functions for health and fitness tracking applications.

## Features

This library provides essential functions for processing health and fitness data:

- Heart rate variability (HRV) calculation
- Resting heart rate determination
- Calorie burn estimation
- VO2 Max estimation
- Sleep quality scoring
- Training load calculation
- Abnormal heart rhythm detection
- Recovery score calculation

## Installation

```bash
npm install
```

## Usage

Build the library:

```bash
npm run build
```

Import functions in your project:

```typescript
import {
calculateHRV,
calculateRestingHeartRate,
calculateCaloriesBurned
} from 'healthtracker';

// Calculate HRV from RR intervals
const hrvValue = calculateHRV([800, 820, 790, 810, 800]);

// Calculate resting heart rate from heart rate data
const restingHeartRate = calculateRestingHeartRate([65, 70, 68, 72, 80, 85, 78]);

// Calculate calories burned during exercise
const caloriesBurned = calculateCaloriesBurned(140, 35, 70, 'male', 45);
```

## Development

To run in watch mode during development:

```bash
npm run dev
```

To check for type errors:

```bash
npm run lint
```
27 changes: 27 additions & 0 deletions HealthTracker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "healthtracker",
"version": "1.0.0",
"description": "A library of useful functions for health tracking applications",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "tsc --noEmit"
},
"keywords": [
"health",
"fitness",
"tracking",
"hrv",
"heartrate"
],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^22.13.9",
"ts-node": "^10.9.2",
"typescript": "^5.8.2"
}
}
223 changes: 223 additions & 0 deletions HealthTracker/src/healthFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/**
* Calculates heart rate variability (HRV) from an array of RR intervals (in ms)
* @param rrIntervals Array of R-R intervals in milliseconds
* @returns The HRV in milliseconds (RMSSD - Root Mean Square of Successive Differences)
*/
export function calculateHRV(rrIntervals: number[]): number {
if (rrIntervals.length < 2) {
return 0;
}

// Calculate successive differences
const differences: number[] = [];
for (let i = 1; i < rrIntervals.length; i++) {
differences.push(Math.pow(rrIntervals[i] - rrIntervals[i - 1], 2));
}

// Calculate mean of squared differences
const meanSquaredDiff = differences.reduce((sum, diff) => sum + diff, 0) / differences.length;

// Return RMSSD (Root Mean Square of Successive Differences)
return Math.sqrt(meanSquaredDiff);
}

/**
* Calculates resting heart rate based on the bottom quartile of heart rate data
* @param heartRates Array of heart rate measurements in bpm
* @returns Resting heart rate in bpm
*/
export function calculateRestingHeartRate(heartRates: number[]): number {
if (heartRates.length === 0) {
return 0;
}

// Sort heart rates
const sortedRates = [...heartRates].sort((a, b) => a - b);

// Take bottom 25% of measurements
const quartileSize = Math.max(1, Math.floor(sortedRates.length * 0.25));
const bottomQuartile = sortedRates.slice(0, quartileSize);

// Calculate average of bottom quartile
return bottomQuartile.reduce((sum, rate) => sum + rate, 0) / bottomQuartile.length;
}

/**
* Calculates calories burned based on heart rate, age, weight, gender and duration
* @param averageHeartRate Average heart rate during activity (bpm)
* @param age Age in years
* @param weightKg Weight in kilograms
* @param gender 'male' or 'female'
* @param durationMinutes Duration of activity in minutes
* @returns Estimated calories burned
*/
export function calculateCaloriesBurned(
averageHeartRate: number,
age: number,
weightKg: number,
gender: 'male' | 'female',
durationMinutes: number
): number {
// Using Keytel formula for calorie estimation
const genderFactor = gender === 'male' ? 1 : 0;

// Keytel formula
const caloriesPerMinute =
((-55.0969 + (0.6309 * averageHeartRate) + (0.1988 * weightKg) + (0.2017 * age)) / 4.184) *
(genderFactor + 1);

return caloriesPerMinute * durationMinutes;
}

/**
* Calculates VO2 Max estimate using the Uth–Sørensen–Overgaard–Pedersen estimation
* @param restingHeartRate Resting heart rate in bpm
* @param maxHeartRate Maximum heart rate in bpm
* @returns Estimated VO2 Max in ml/kg/min
*/
export function estimateVO2Max(restingHeartRate: number, maxHeartRate: number): number {
// Uth–Sørensen–Overgaard–Pedersen estimation
return 15.3 * (maxHeartRate / restingHeartRate);
}

/**
* Calculates sleep quality score based on various sleep metrics
* @param totalSleepHours Total sleep duration in hours
* @param deepSleepPercentage Percentage of deep sleep (0-100)
* @param remSleepPercentage Percentage of REM sleep (0-100)
* @param wakeupCount Number of times woken up during sleep
* @returns Sleep quality score (0-100)
*/
export function calculateSleepScore(
totalSleepHours: number,
deepSleepPercentage: number,
remSleepPercentage: number,
wakeupCount: number
): number {
// Factors and weights
const durationFactor = Math.min(totalSleepHours / 8, 1) * 40; // 40% weight for duration
const deepSleepFactor = (deepSleepPercentage / 25) * 25; // 25% weight for deep sleep (ideal is ~25%)
const remSleepFactor = (remSleepPercentage / 23) * 25; // 25% weight for REM sleep (ideal is ~23%)
const wakeupFactor = Math.max(0, 10 - wakeupCount) / 10 * 10; // 10% weight for wakeups

// Calculate total score
let score = durationFactor + deepSleepFactor + remSleepFactor + wakeupFactor;

// Ensure score is between 0-100
return Math.min(100, Math.max(0, score));
}

/**
* Calculates training load based on heart rate data and duration
* @param averageHeartRate Average heart rate during exercise in bpm
* @param durationMinutes Duration of exercise in minutes
* @param maxHeartRate Maximum heart rate of the individual in bpm
* @returns Training load score in arbitrary units
*/
export function calculateTrainingLoad(
averageHeartRate: number,
durationMinutes: number,
maxHeartRate: number
): number {
// Calculate heart rate reserve percentage
const hrReservePercent = (averageHeartRate / maxHeartRate) * 100;

// Calculate training impulse (TRIMP) score
// Using a simplified version of the Banister TRIMP formula
const intensity = 0.64 * Math.exp(1.92 * (hrReservePercent / 100));

return intensity * durationMinutes;
}

/**
* Detects potential abnormal heart rhythm
* @param rrIntervals Array of R-R intervals in milliseconds
* @returns Object containing abnormality assessment
*/
export function detectAbnormalHeartRhythm(rrIntervals: number[]): {
isAbnormal: boolean;
irregularityScore: number;
possibleIssue: string | null;
} {
if (rrIntervals.length < 10) {
return {
isAbnormal: false,
irregularityScore: 0,
possibleIssue: null
};
}

// Calculate average RR interval
const avgRR = rrIntervals.reduce((sum, rr) => sum + rr, 0) / rrIntervals.length;

// Calculate variation coefficient (standard deviation / mean)
const stdDev = Math.sqrt(
rrIntervals.reduce((sum, rr) => sum + Math.pow(rr - avgRR, 2), 0) / rrIntervals.length
);
const variationCoefficient = (stdDev / avgRR) * 100;

// Count significant deviations (>20% from average)
const significantDeviations = rrIntervals.filter(
rr => Math.abs(rr - avgRR) > 0.2 * avgRR
).length;

const deviationPercentage = (significantDeviations / rrIntervals.length) * 100;
const irregularityScore = (variationCoefficient + deviationPercentage) / 2;

// Assess potential issues
let possibleIssue: string | null = null;
if (irregularityScore > 25) {
possibleIssue = "Possible arrhythmia detected";
} else if (variationCoefficient > 20) {
possibleIssue = "High heart rate variability";
} else if (variationCoefficient < 5 && rrIntervals.length > 100) {
possibleIssue = "Unusually low heart rate variability";
}

return {
isAbnormal: irregularityScore > 15,
irregularityScore,
possibleIssue
};
}

/**
* Calculates recovery score based on HRV, resting heart rate, and sleep quality
* @param currentHRV Current HRV measurement (ms)
* @param baselineHRV Baseline/average HRV measurement (ms)
* @param currentRHR Current resting heart rate (bpm)
* @param baselineRHR Baseline/average resting heart rate (bpm)
* @param sleepScore Sleep quality score (0-100)
* @returns Recovery score (0-100)
*/
export function calculateRecoveryScore(
currentHRV: number,
baselineHRV: number,
currentRHR: number,
baselineRHR: number,
sleepScore: number
): number {
// Calculate HRV ratio (current vs baseline)
const hrvRatio = currentHRV / baselineHRV;

// Calculate RHR ratio (baseline vs current, inverted because lower RHR is better)
const rhrRatio = baselineRHR / currentRHR;

// Weight factors
const hrvWeight = 0.4; // 40%
const rhrWeight = 0.3; // 30%
const sleepWeight = 0.3; // 30%

// Calculate weighted score
// HRV and RHR ratios are scaled to 0-100
const hrvScore = Math.min(100, hrvRatio * 50);
const rhrScore = Math.min(100, rhrRatio * 50);

const recoveryScore =
(hrvWeight * hrvScore) +
(rhrWeight * rhrScore) +
(sleepWeight * sleepScore);

// Ensure score is between 0-100
return Math.min(100, Math.max(0, recoveryScore));
}
1 change: 1 addition & 0 deletions HealthTracker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './healthFunctions';
15 changes: 15 additions & 0 deletions HealthTracker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": ["es2020"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "dist",
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}