File size: 14,645 Bytes
eeb0f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
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