my-gradio-app / health_analysis /health_analyzer.py
Nguyen Trong Lap
Recreate history without binary blobs
eeb0f9c
"""
Health Analyzer - Comprehensive health analysis and disease risk prediction
Analyzes user health data to provide insights and predictions
"""
from typing import List, Dict, Any, Optional
from datetime import datetime, timedelta
import math
from health_data import HealthContext
class HealthAnalyzer:
"""
Comprehensive health analysis and disease risk prediction
Provides health scoring, risk assessment, and preventive recommendations
"""
def __init__(self, health_context: HealthContext):
self.health_context = health_context
self.user_id = health_context.user_id
# ===== Health Status Analysis =====
def analyze_health_status(self) -> Dict[str, Any]:
"""Comprehensive health status analysis"""
profile = self.health_context.get_user_profile()
analysis = {
'timestamp': datetime.now().isoformat(),
'bmi_status': self._analyze_bmi(profile),
'activity_status': self._analyze_activity(),
'symptom_status': self._analyze_symptoms(),
'nutrition_status': self._analyze_nutrition(),
'mental_health_status': self._analyze_mental_health(),
'overall_health_score': 0.0
}
# Calculate overall health score
scores = [
analysis['bmi_status'].get('score', 0.5),
analysis['activity_status'].get('score', 0.5),
analysis['symptom_status'].get('score', 0.5),
analysis['nutrition_status'].get('score', 0.5),
analysis['mental_health_status'].get('score', 0.5)
]
analysis['overall_health_score'] = round(sum(scores) / len(scores), 2)
return analysis
def _analyze_bmi(self, profile) -> Dict[str, Any]:
"""Analyze BMI status"""
if not profile.bmi:
return {'status': 'unknown', 'score': 0.5, 'recommendation': 'Calculate BMI first'}
bmi = profile.bmi
if bmi < 18.5:
return {
'status': 'underweight',
'score': 0.6,
'bmi': bmi,
'recommendation': 'Consider healthy weight gain with proper nutrition'
}
elif bmi < 25:
return {
'status': 'normal',
'score': 1.0,
'bmi': bmi,
'recommendation': 'Maintain current weight with balanced diet and exercise'
}
elif bmi < 30:
return {
'status': 'overweight',
'score': 0.7,
'bmi': bmi,
'recommendation': 'Gradual weight loss through diet and exercise'
}
else:
return {
'status': 'obese',
'score': 0.4,
'bmi': bmi,
'recommendation': 'Consult healthcare provider for weight management plan'
}
def _analyze_activity(self) -> Dict[str, Any]:
"""Analyze physical activity status"""
fitness_history = self.health_context.get_fitness_history(days=30)
adherence = self.health_context.get_workout_adherence(days=30)
if not fitness_history:
return {
'status': 'sedentary',
'score': 0.3,
'workouts_30d': 0,
'recommendation': 'Start with 150 minutes of moderate activity per week'
}
total_minutes = sum(f.duration_minutes for f in fitness_history)
if adherence > 0.8 and total_minutes > 150:
return {
'status': 'active',
'score': 1.0,
'workouts_30d': len(fitness_history),
'total_minutes': total_minutes,
'adherence': adherence,
'recommendation': 'Excellent! Maintain current activity level'
}
elif adherence > 0.5:
return {
'status': 'moderately_active',
'score': 0.7,
'workouts_30d': len(fitness_history),
'total_minutes': total_minutes,
'adherence': adherence,
'recommendation': 'Good progress! Try to increase frequency'
}
else:
return {
'status': 'low_activity',
'score': 0.4,
'workouts_30d': len(fitness_history),
'total_minutes': total_minutes,
'adherence': adherence,
'recommendation': 'Increase physical activity gradually'
}
def _analyze_symptoms(self) -> Dict[str, Any]:
"""Analyze symptom patterns"""
symptom_records = self.health_context.get_records_by_type('symptom')
if not symptom_records:
return {
'status': 'no_symptoms',
'score': 1.0,
'recommendation': 'Continue monitoring health'
}
# Count symptoms in last 30 days
recent_symptoms = [r for r in symptom_records if r.timestamp > datetime.now() - timedelta(days=30)]
if len(recent_symptoms) > 5:
return {
'status': 'frequent_symptoms',
'score': 0.4,
'recent_symptoms': len(recent_symptoms),
'recommendation': 'Consult healthcare provider for evaluation'
}
elif len(recent_symptoms) > 0:
return {
'status': 'occasional_symptoms',
'score': 0.7,
'recent_symptoms': len(recent_symptoms),
'recommendation': 'Monitor symptoms and maintain healthy lifestyle'
}
else:
return {
'status': 'no_recent_symptoms',
'score': 0.9,
'recommendation': 'Good health status'
}
def _analyze_nutrition(self) -> Dict[str, Any]:
"""Analyze nutrition status"""
nutrition_records = self.health_context.get_records_by_type('nutrition')
if not nutrition_records:
return {
'status': 'unknown',
'score': 0.5,
'recommendation': 'Share your nutrition habits for personalized advice'
}
# Check adherence to nutrition plans
adherence = len(nutrition_records) / max(1, (30 / 7)) # Expected ~1 per week
if adherence > 0.8:
return {
'status': 'good_adherence',
'score': 0.9,
'adherence': min(adherence, 1.0),
'recommendation': 'Excellent nutrition tracking!'
}
else:
return {
'status': 'low_adherence',
'score': 0.5,
'adherence': adherence,
'recommendation': 'Improve nutrition tracking and consistency'
}
def _analyze_mental_health(self) -> Dict[str, Any]:
"""Analyze mental health status"""
mental_records = self.health_context.get_records_by_type('mental_health')
if not mental_records:
return {
'status': 'unknown',
'score': 0.5,
'recommendation': 'Share your mental health concerns for support'
}
# Check for stress/anxiety mentions
stress_count = sum(1 for r in mental_records if 'stress' in str(r.data).lower())
if stress_count > 3:
return {
'status': 'high_stress',
'score': 0.4,
'stress_indicators': stress_count,
'recommendation': 'Consider stress management techniques and professional support'
}
else:
return {
'status': 'stable',
'score': 0.8,
'recommendation': 'Continue mental health practices'
}
def calculate_health_score(self) -> float:
"""Calculate overall health score (0-100)"""
analysis = self.analyze_health_status()
return round(analysis['overall_health_score'] * 100, 1)
# ===== Risk Prediction =====
def identify_health_risks(self) -> List[Dict[str, Any]]:
"""Identify potential health risks"""
risks = []
profile = self.health_context.get_user_profile()
# BMI-related risks
if profile.bmi and profile.bmi > 30:
risks.append({
'risk_type': 'obesity',
'severity': 'high',
'description': 'Elevated BMI increases risk of cardiovascular disease and diabetes',
'recommendation': 'Consult healthcare provider for weight management'
})
# Sedentary lifestyle risk
fitness_history = self.health_context.get_fitness_history(days=30)
if len(fitness_history) < 2:
risks.append({
'risk_type': 'sedentary_lifestyle',
'severity': 'medium',
'description': 'Low physical activity increases health risks',
'recommendation': 'Start with 30 minutes of moderate activity daily'
})
# Chronic condition risks
if profile.health_conditions:
for condition in profile.health_conditions:
risks.append({
'risk_type': f'chronic_{condition}',
'severity': 'medium',
'description': f'Existing condition: {condition}',
'recommendation': 'Follow medical advice and monitor regularly'
})
return risks
def predict_disease_risk(self) -> List[Dict[str, Any]]:
"""Predict disease risk based on health data"""
predictions = []
profile = self.health_context.get_user_profile()
# Cardiovascular disease risk
cv_risk_score = self._calculate_cv_risk(profile)
if cv_risk_score > 0.6:
predictions.append({
'disease': 'cardiovascular_disease',
'risk_score': cv_risk_score,
'risk_level': 'high' if cv_risk_score > 0.8 else 'medium',
'factors': ['high_bmi', 'low_activity', 'age'],
'recommendation': 'Regular cardiovascular screening recommended'
})
# Type 2 Diabetes risk
diabetes_risk = self._calculate_diabetes_risk(profile)
if diabetes_risk > 0.6:
predictions.append({
'disease': 'type_2_diabetes',
'risk_score': diabetes_risk,
'risk_level': 'high' if diabetes_risk > 0.8 else 'medium',
'factors': ['high_bmi', 'sedentary', 'age'],
'recommendation': 'Blood glucose screening recommended'
})
return predictions
def _calculate_cv_risk(self, profile) -> float:
"""Calculate cardiovascular disease risk (0-1)"""
risk = 0.3 # Base risk
# Age factor
if profile.age and profile.age > 50:
risk += 0.2
# BMI factor
if profile.bmi and profile.bmi > 30:
risk += 0.2
# Activity factor
fitness_history = self.health_context.get_fitness_history(days=30)
if len(fitness_history) < 2:
risk += 0.15
# Health conditions
if profile.health_conditions:
risk += 0.1
return min(risk, 1.0)
def _calculate_diabetes_risk(self, profile) -> float:
"""Calculate type 2 diabetes risk (0-1)"""
risk = 0.2 # Base risk
# BMI factor (strongest predictor)
if profile.bmi and profile.bmi > 25:
risk += 0.3
# Age factor
if profile.age and profile.age > 45:
risk += 0.15
# Activity factor
fitness_history = self.health_context.get_fitness_history(days=30)
if len(fitness_history) < 2:
risk += 0.2
return min(risk, 1.0)
def recommend_preventive_measures(self) -> List[str]:
"""Recommend preventive health measures"""
recommendations = []
profile = self.health_context.get_user_profile()
# Weight management
if profile.bmi and profile.bmi > 25:
recommendations.append("Implement gradual weight loss through balanced diet and exercise")
# Physical activity
fitness_history = self.health_context.get_fitness_history(days=30)
if len(fitness_history) < 3:
recommendations.append("Aim for 150 minutes of moderate-intensity exercise per week")
# Nutrition
recommendations.append("Maintain balanced diet with whole grains, fruits, and vegetables")
# Stress management
recommendations.append("Practice stress management techniques like meditation or yoga")
# Regular checkups
recommendations.append("Schedule regular health checkups with your healthcare provider")
# Sleep
recommendations.append("Maintain 7-9 hours of quality sleep per night")
return recommendations
def generate_health_report(self) -> str:
"""Generate comprehensive health report"""
analysis = self.analyze_health_status()
risks = self.identify_health_risks()
predictions = self.predict_disease_risk()
recommendations = self.recommend_preventive_measures()
report = f"""
# Health Analysis Report
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## Overall Health Score: {analysis['overall_health_score']}/1.0
### Health Status
- BMI Status: {analysis['bmi_status']['status']}
- Activity Level: {analysis['activity_status']['status']}
- Symptom Status: {analysis['symptom_status']['status']}
- Nutrition Status: {analysis['nutrition_status']['status']}
- Mental Health: {analysis['mental_health_status']['status']}
### Identified Risks
{chr(10).join([f"- {r['risk_type']}: {r['description']}" for r in risks]) if risks else "No significant risks identified"}
### Disease Risk Predictions
{chr(10).join([f"- {p['disease']}: {p['risk_level']} risk ({p['risk_score']:.1%})" for p in predictions]) if predictions else "Low disease risk"}
### Preventive Recommendations
{chr(10).join([f"- {r}" for r in recommendations])}
"""
return report