-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
577 lines (520 loc) · 25.2 KB
/
Copy pathapp.py
File metadata and controls
577 lines (520 loc) · 25.2 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
from flask import Flask, jsonify, request
from flask_cors import CORS
import json
import os
import logging
from utils.db_pool import get_db_cursor, close_pool
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# In-memory cache for frequently accessed data
patient_data = None
health_metrics = None
medications = None
users = None
integrations = None
features = None
# Register close_pool function to be called when app shuts down
@app.teardown_appcontext
def shutdown_session(exception=None):
close_pool()
def load_patient_data():
global patient_data
try:
data_path = os.path.join('data', 'sample_patient.json')
if os.path.exists(data_path):
with open(data_path, 'r') as f:
patient_data = json.load(f)
else:
# Initialize with sample data if file doesn't exist
patient_data = {
"patient_id": "PT12345",
"name": "John Doe",
"age": 45,
"gender": "Male",
"medical_history": {
"conditions": ["Epilepsy", "Hypertension"],
"allergies": ["Penicillin"],
"neurological_history": {
"seizure_history": [
{"date": "2023-01-15", "type": "Focal", "duration_seconds": 120, "triggers": ["Stress", "Sleep deprivation"]},
{"date": "2023-03-22", "type": "Generalized", "duration_seconds": 180, "triggers": ["Missed medication"]},
{"date": "2023-06-10", "type": "Focal", "duration_seconds": 90, "triggers": ["Flashing lights"]}
],
"eeg_results": [
{"date": "2023-02-01", "findings": "Abnormal electrical activity in the temporal lobe", "severity": "Moderate"},
{"date": "2023-07-15", "findings": "Improved electrical patterns, some abnormalities persist", "severity": "Mild"}
],
"imaging_studies": [
{"date": "2023-01-20", "type": "MRI", "findings": "Small lesion in right temporal lobe", "recommendation": "Follow-up in 6 months"},
{"date": "2023-07-20", "type": "MRI", "findings": "No change in lesion size", "recommendation": "Continue current treatment"}
],
"cognitive_assessment": {
"date": "2023-05-10",
"memory_score": 85,
"attention_score": 78,
"processing_speed": 82,
"overall_cognitive_status": "Mild impairment"
}
}
},
"medications": [
{"name": "Levetiracetam", "dosage": "500mg", "frequency": "Twice daily", "purpose": "Seizure control"},
{"name": "Lisinopril", "dosage": "10mg", "frequency": "Once daily", "purpose": "Blood pressure control"}
],
"treatment_plan": {
"goals": ["Reduce seizure frequency", "Maintain blood pressure control", "Improve sleep quality"],
"interventions": [
{"type": "Medication", "details": "Continue current antiepileptic regimen"},
{"type": "Lifestyle", "details": "Stress reduction techniques, sleep hygiene"},
{"type": "Follow-up", "details": "Neurology appointment in 3 months"}
],
"progress_notes": [
{"date": "2023-04-15", "note": "Patient reports 30% reduction in seizure frequency"},
{"date": "2023-07-20", "note": "Blood pressure well-controlled, seizure frequency stable"}
]
},
"diagnostic_tests": [
{"date": "2023-01-05", "test": "Complete Blood Count", "result": "Normal", "reference_range": "N/A", "flag": False},
{"date": "2023-01-05", "test": "Comprehensive Metabolic Panel", "result": "Normal", "reference_range": "N/A", "flag": False},
{"date": "2023-01-05", "test": "Antiepileptic Drug Level", "result": "8.5 μg/mL", "reference_range": "4.0-12.0 μg/mL", "flag": False},
{"date": "2023-03-10", "test": "Antiepileptic Drug Level", "result": "14.2 μg/mL", "reference_range": "4.0-12.0 μg/mL", "flag": True},
{"date": "2023-07-01", "test": "Antiepileptic Drug Level", "result": "10.1 μg/mL", "reference_range": "4.0-12.0 μg/mL", "flag": False}
],
"neural_profile": {
"brain_regions_affected": ["Temporal lobe", "Hippocampus"],
"neural_network_analysis": {
"connectivity_strength": 72,
"abnormal_patterns": ["Spike-wave discharges", "Theta rhythm abnormalities"],
"network_stability": "Moderate"
},
"neurotransmitter_levels": [
{"name": "GABA", "status": "Low", "clinical_significance": "May contribute to seizure susceptibility"},
{"name": "Glutamate", "status": "Elevated", "clinical_significance": "Potential excitotoxicity"}
],
"neural_biomarkers": [
{"name": "Inflammatory markers", "value": "Slightly elevated", "interpretation": "Low-grade neuroinflammation"},
{"name": "Oxidative stress markers", "value": "Elevated", "interpretation": "Increased neuronal vulnerability"}
],
"neural_treatment_response": {
"current_medication_efficacy": 65,
"neural_adaptation": "Positive response to therapy",
"side_effect_profile": "Minimal cognitive impact"
}
}
}
# Save the sample data
with open(data_path, 'w') as f:
json.dump(patient_data, f, indent=2)
except Exception as e:
print(f"Error loading patient data: {e}")
patient_data = {"error": "Failed to load patient data"}
def load_health_metrics():
global health_metrics
health_metrics = {
"heart_rate": [{"date": "2023-07-01", "value": 72}, {"date": "2023-07-02", "value": 75}],
"blood_pressure": [{"date": "2023-07-01", "value": "120/80"}, {"date": "2023-07-02", "value": "118/79"}],
"sleep": [{"date": "2023-07-01", "value": 7.5}, {"date": "2023-07-02", "value": 8}],
"steps": [{"date": "2023-07-01", "value": 8500}, {"date": "2023-07-02", "value": 10200}]
}
def load_medications():
global medications
medications = [
{"id": 1, "name": "Levetiracetam", "dosage": "500mg", "frequency": "Twice daily", "purpose": "Seizure control"},
{"id": 2, "name": "Lisinopril", "dosage": "10mg", "frequency": "Once daily", "purpose": "Blood pressure control"}
]
def load_users():
global users
users = [
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"role": "patient",
"health_profile": {
"height": 180,
"weight": 80,
"conditions": ["Epilepsy", "Hypertension"]
}
}
]
def load_integrations():
global integrations
integrations = [
{"id": 1, "name": "Apple Health", "status": "connected", "last_sync": "2023-07-02T14:30:00Z"},
{"id": 2, "name": "Fitbit", "status": "disconnected", "last_sync": None},
{"id": 3, "name": "Google Fit", "status": "disconnected", "last_sync": None}
]
def load_features():
global features
features = [
{"id": 1, "name": "Medication Tracking", "enabled": True},
{"id": 2, "name": "Symptom Journal", "enabled": True},
{"id": 3, "name": "Health Data Integration", "enabled": True},
{"id": 4, "name": "Neural Profile Analysis", "enabled": True},
{"id": 5, "name": "Doctor Appointment Scheduling", "enabled": False}
]
# Check database connection on startup
try:
with get_db_cursor() as cursor:
cursor.execute("SELECT 1")
logger.info("Database connection successful")
except Exception as e:
logger.error(f"Database connection failed: {e}")
logger.info("Falling back to in-memory data")
# Load all data on startup
load_patient_data()
load_health_metrics()
load_medications()
load_users()
load_integrations()
load_features()
@app.route('/api/patient', methods=['GET'])
def get_patient_data():
# Try to get data from database first, fall back to in-memory cache
try:
with get_db_cursor() as cursor:
# Get primary patient data
cursor.execute("""
SELECT p.id, p.display_name, p.age_at_record,
p.physical_description, p.record_date
FROM patients p
LIMIT 1
""")
patient_row = cursor.fetchone()
if patient_row and patient_row[0]:
patient_id = patient_row[0]
patient = {
"id": patient_id,
"patient_id": f"PT{patient_id}",
"name": patient_row[1] or "John Doe",
"age": patient_row[2] or 45,
"gender": "Male", # Assuming default since we don't have this column
"physical_description": patient_row[3] or ""
}
# Get medical history
cursor.execute("""
SELECT id, allergies, conditions, surgeries, family_history
FROM medical_history
WHERE patient_id = %s
""", (patient_id,))
med_history_row = cursor.fetchone()
if med_history_row:
patient["medical_history"] = {
"conditions": med_history_row[2] if med_history_row[2] else ["Epilepsy", "Hypertension"],
"allergies": med_history_row[1] if med_history_row[1] else ["Penicillin"],
"surgeries": med_history_row[3] if med_history_row[3] else []
}
else:
patient["medical_history"] = {
"conditions": ["Epilepsy", "Hypertension"],
"allergies": ["Penicillin"]
}
# Get seizure events for neurological history
cursor.execute("""
SELECT id, date, type, duration_seconds, triggers
FROM seizure_events
WHERE patient_id = %s
ORDER BY date DESC
""", (patient_id,))
seizure_rows = cursor.fetchall()
# Get EEG results
cursor.execute("""
SELECT id, date, findings, severity
FROM eeg_results
WHERE patient_id = %s
ORDER BY date DESC
""", (patient_id,))
eeg_rows = cursor.fetchall()
# Get imaging studies
cursor.execute("""
SELECT id, date, type, findings, recommendations
FROM imaging_studies
WHERE patient_id = %s
ORDER BY date DESC
""", (patient_id,))
imaging_rows = cursor.fetchall()
# Get cognitive assessment
cursor.execute("""
SELECT id, date, memory_score, attention_score, processing_speed, overall_status
FROM cognitive_assessments
WHERE patient_id = %s
ORDER BY date DESC
LIMIT 1
""", (patient_id,))
cognitive_row = cursor.fetchone()
# Construct neurological history
neuro_history = {}
if seizure_rows:
neuro_history["seizure_history"] = [
{
"date": row[1].strftime("%Y-%m-%d") if row[1] else "2023-01-15",
"type": row[2] or "Focal",
"duration_seconds": row[3] or 120,
"triggers": row[4] if row[4] else ["Stress", "Sleep deprivation"]
}
for row in seizure_rows
]
if eeg_rows:
neuro_history["eeg_results"] = [
{
"date": row[1].strftime("%Y-%m-%d") if row[1] else "2023-02-01",
"findings": row[2] or "Abnormal electrical activity",
"severity": row[3] or "Moderate"
}
for row in eeg_rows
]
if imaging_rows:
neuro_history["imaging_studies"] = [
{
"date": row[1].strftime("%Y-%m-%d") if row[1] else "2023-01-20",
"type": row[2] or "MRI",
"findings": row[3] or "Small lesion in right temporal lobe",
"recommendation": row[4] or "Follow-up in 6 months"
}
for row in imaging_rows
]
if cognitive_row:
neuro_history["cognitive_assessment"] = {
"date": cognitive_row[1].strftime("%Y-%m-%d") if cognitive_row[1] else "2023-05-10",
"memory_score": cognitive_row[2] or 85,
"attention_score": cognitive_row[3] or 78,
"processing_speed": cognitive_row[4] or 82,
"overall_cognitive_status": cognitive_row[5] or "Mild impairment"
}
# Add neurological history to medical history
if neuro_history and isinstance(patient["medical_history"], dict):
patient["medical_history"] = {
**patient["medical_history"],
"neurological_history": neuro_history
}
# Get medications from treatment plan
cursor.execute("""
SELECT tp.id
FROM treatment_plans tp
WHERE tp.patient_id = %s
ORDER BY tp.created_at DESC
LIMIT 1
""", (patient_id,))
treatment_plan_row = cursor.fetchone()
if treatment_plan_row:
treatment_plan_id = treatment_plan_row[0]
cursor.execute("""
SELECT id, name, dosage, purpose
FROM medications
WHERE treatment_plan_id = %s
""", (treatment_plan_id,))
med_rows = cursor.fetchall()
if med_rows:
patient["medications"] = [
{
"id": row[0],
"name": row[1] or "Medication",
"dosage": row[2] or "500mg",
"frequency": "Twice daily", # Default value since we don't have this column
"purpose": row[3] or "Treatment"
}
for row in med_rows
]
# Get treatment plan details
cursor.execute("""
SELECT id, goals, interventions, progress_notes
FROM treatment_plans
WHERE patient_id = %s
ORDER BY created_at DESC
LIMIT 1
""", (patient_id,))
tp_row = cursor.fetchone()
if tp_row:
patient["treatment_plan"] = {
"goals": tp_row[1] if tp_row[1] else ["Reduce seizure frequency", "Maintain blood pressure control"],
"interventions": tp_row[2] if tp_row[2] else [
{"type": "Medication", "details": "Continue current antiepileptic regimen"},
{"type": "Lifestyle", "details": "Stress reduction techniques, sleep hygiene"}
],
"progress_notes": tp_row[3] if tp_row[3] else [
{"date": "2023-04-15", "note": "Patient reports 30% reduction in seizure frequency"}
]
}
# Get diagnostic tests
cursor.execute("""
SELECT id, test_date, test_type, test_results
FROM diagnostic_tests
WHERE patient_id = %s
ORDER BY test_date DESC
""", (patient_id,))
test_rows = cursor.fetchall()
if test_rows:
patient["diagnostic_tests"] = [
{
"date": row[1].strftime("%Y-%m-%d") if row[1] else "2023-01-05",
"test": row[2] or "Complete Blood Count",
"result": row[3] or "Normal",
"reference_range": "N/A",
"flag": False
}
for row in test_rows
]
# Add neural profile data (simplified for now)
patient["neural_profile"] = {
"brain_regions_affected": ["Temporal lobe", "Hippocampus"],
"neural_network_analysis": {
"connectivity_strength": 72,
"abnormal_patterns": ["Spike-wave discharges", "Theta rhythm abnormalities"],
"network_stability": "Moderate"
},
"neurotransmitter_levels": [
{"name": "GABA", "status": "Low", "clinical_significance": "May contribute to seizure susceptibility"},
{"name": "Glutamate", "status": "Elevated", "clinical_significance": "Potential excitotoxicity"}
]
}
# Update cache
global patient_data
patient_data = patient
return jsonify(patient)
except Exception as e:
logger.error(f"Error fetching patient data from database: {e}")
# If error occurs, use cached data
return jsonify(patient_data)
@app.route('/api/health-metrics', methods=['GET'])
def get_health_metrics():
return jsonify(health_metrics)
@app.route('/api/medications', methods=['GET'])
def get_medications():
# Try to get data from database first, fall back to in-memory cache
try:
with get_db_cursor() as cursor:
cursor.execute("""
SELECT m.id, m.name, m.dosage, m.purpose, tp.patient_id
FROM medications m
LEFT JOIN treatment_plans tp ON m.treatment_plan_id = tp.id
""")
rows = cursor.fetchall()
if rows:
result = [
{
"id": row[0],
"name": row[1] or "Unknown Medication",
"dosage": row[2] or "Standard dose",
"frequency": "As directed", # This field isn't in our DB schema
"purpose": row[3] or "Treatment",
"patient_id": row[4]
}
for row in rows
]
# Update cache
global medications
medications = result
return jsonify(result)
except Exception as e:
logger.error(f"Error fetching medications from database: {e}")
# If error occurs, use cached data
return jsonify(medications)
@app.route('/api/health-summary', methods=['GET'])
def get_health_summary():
try:
with get_db_cursor() as cursor:
# Get a patient ID for reference
cursor.execute("SELECT id FROM patients LIMIT 1")
patient_id_row = cursor.fetchone()
if not patient_id_row:
raise Exception("No patients found in database")
patient_id = patient_id_row[0]
# Get seizure count from database
cursor.execute("""
SELECT COUNT(*)
FROM seizure_events
WHERE patient_id = %s
""", (patient_id,))
seizure_count = cursor.fetchone()[0] or 0
# Get medication count
cursor.execute("""
SELECT COUNT(*)
FROM medications m
JOIN treatment_plans tp ON m.treatment_plan_id = tp.id
WHERE tp.patient_id = %s
""", (patient_id,))
medication_count = cursor.fetchone()[0] or 0
# Get latest diagnostic test
cursor.execute("""
SELECT test_date, test_type, test_results
FROM diagnostic_tests
WHERE patient_id = %s
ORDER BY test_date DESC
LIMIT 1
""", (patient_id,))
latest_test_row = cursor.fetchone()
latest_test = None
if latest_test_row:
latest_test = {
"date": latest_test_row[0].strftime("%Y-%m-%d") if latest_test_row[0] else None,
"test": latest_test_row[1] or "Comprehensive Metabolic Panel",
"result": latest_test_row[2] or "Normal",
"reference_range": "N/A",
"flag": False
}
summary = {
"seizure_count": seizure_count,
"medication_count": medication_count,
"latest_test": latest_test,
"overall_status": "Stable" if seizure_count < 3 else "Needs Attention"
}
return jsonify(summary)
except Exception as e:
logger.error(f"Error fetching health summary from database: {e}")
# Fall back to calculating from cache if necessary
# Use a more defensive approach with the potentially new structure
seizure_count = 0
if isinstance(patient_data, dict):
med_history = patient_data.get('medical_history', {})
if isinstance(med_history, dict):
neuro_history = med_history.get('neurological_history', {})
if isinstance(neuro_history, dict):
seizure_history = neuro_history.get('seizure_history', [])
seizure_count = len(seizure_history) if isinstance(seizure_history, list) else 0
medication_count = 0
if isinstance(patient_data, dict):
medications_list = patient_data.get('medications', [])
medication_count = len(medications_list) if isinstance(medications_list, list) else 0
latest_test = None
if isinstance(patient_data, dict):
diagnostic_tests = patient_data.get('diagnostic_tests', [])
if isinstance(diagnostic_tests, list) and diagnostic_tests:
latest_test = diagnostic_tests[-1]
summary = {
"seizure_count": seizure_count,
"medication_count": medication_count,
"latest_test": latest_test,
"overall_status": "Stable" if seizure_count < 3 else "Needs Attention"
}
return jsonify(summary)
@app.route('/api/check-symptoms', methods=['POST'])
def check_symptoms():
data = request.json
if not data or 'symptoms' not in data:
return jsonify({"error": "No symptoms provided"}), 400
symptoms = data['symptoms']
# In a real application, this would be a more sophisticated analysis
risk_level = "Low"
if len(symptoms) > 3:
risk_level = "Medium"
if "seizure" in symptoms or "severe headache" in symptoms:
risk_level = "High"
return jsonify({
"risk_level": risk_level,
"recommendation": "Consult your neurologist" if risk_level == "High" else "Monitor your symptoms"
})
@app.route('/api/users', methods=['GET'])
def get_users():
# Just return the in-memory cache as we don't have a users table in the database
return jsonify(users)
@app.route('/api/integrations', methods=['GET'])
def get_integrations():
return jsonify(integrations)
@app.route('/api/features', methods=['GET'])
def get_features():
return jsonify(features)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)