1- import requests
21import os
2+ import requests
33from flask import Flask , request , render_template
44from dotenv import load_dotenv
55
6-
76app = Flask (__name__ )
8-
9- FHIR_SERVER_BASE_URL = "http://pwebmedcit.services.brown.edu:9091/fhir"
10-
117load_dotenv ()
128
9+ FHIR_SERVER_BASE_URL = os .getenv ("FHIR_SERVER_BASE_URL" )
1310username = os .getenv ("FHIR_USERNAME" )
1411password = os .getenv ("FHIR_PASSWORD" )
1512
16-
1713def request_patient (patient_id , credentials ):
18-
19- req = requests .get (FHIR_SERVER_BASE_URL + "/Patient/" + str (patient_id ), auth = credentials )
20-
21- print (f"Requests status: { req .status_code } " )
22-
23- response = req .json ()
24- print (response .keys ())
25-
26- return response
27-
28- def search_patients_by_condition (condition_id , credentials ):
29- # Search for patients with a specific condition
30- search_url = f"{ FHIR_SERVER_BASE_URL } /Condition?code={ condition_id } "
31- req = requests .get (search_url , auth = credentials )
32-
14+ req = requests .get (FHIR_SERVER_BASE_URL + "/Patient/" + str (patient_id ), auth = credentials )
3315 if req .status_code == 200 :
34- conditions = req .json ()['entry' ]
35- patient_ids = [entry ['resource' ]['subject' ]['reference' ].split ('/' )[- 1 ] for entry in conditions ]
36- patients = [request_patient (patient_id , credentials ) for patient_id in patient_ids ]
37- # Convert patient details into set of tuples to ensure uniqueness
38- unique_patients = set ((patient ['id' ], patient ['name' ][0 ]['given' ][0 ], patient ['name' ][0 ]['family' ], patient ['gender' ], patient ['birthDate' ]) for patient in patients )
39-
40- total_patients = len (unique_patients )
41- return {'unique_patients' : unique_patients , 'total_patients' : total_patients }
16+ return req .json ()
4217 else :
4318 return None
4419
20+ def request_observations (patient_id , credentials ):
21+ req = requests .get (FHIR_SERVER_BASE_URL + f"/Observation?patient={ patient_id } " , auth = credentials )
22+ if req .status_code == 200 :
23+ return req .json ()
24+ else :
25+ return None
4526
46-
47-
27+ def extract_patient_data (patient , observations ):
28+ data = {}
29+ data ['age' ] = 2024 - int (patient ['birthDate' ][:4 ]) # Simplified age calculation
30+ data ['gender' ] = patient ['gender' ]
31+ data ['race' ] = 'unknown' # Default value
32+
33+ for entry in observations .get ('entry' , []):
34+ resource = entry ['resource' ]
35+ code = resource ['code' ]['coding' ][0 ]['code' ]
36+ value = resource ['valueQuantity' ]['value' ]
37+
38+ if code == '2093-3' : # Total Cholesterol
39+ data ['total_cholesterol' ] = value
40+ elif code == '2085-9' : # HDL Cholesterol
41+ data ['hdl_cholesterol' ] = value
42+ elif code == '18262-6' : # LDL Cholesterol
43+ data ['ldl_cholesterol' ] = value
44+ elif code == '8480-6' : # Systolic Blood Pressure
45+ data ['systolic_bp' ] = value
46+ elif code == '8462-4' : # Diastolic Blood Pressure
47+ data ['diastolic_bp' ] = value
48+
49+ return data
4850
4951@app .route ('/' , methods = ['GET' , 'POST' ])
5052def index ():
5153 result = None
54+ patient_data = {}
5255 credentials = (username , password )
5356
5457 if request .method == 'POST' :
5558 try :
56- condition_id = request .form ['condition_id' ]
57- result = search_patients_by_condition (condition_id , credentials )
59+ patient_id = request .form .get ('patient_id' )
60+ if patient_id :
61+ patient = request_patient (patient_id , credentials )
62+ observations = request_observations (patient_id , credentials )
63+
64+ if patient and observations :
65+ patient_data = extract_patient_data (patient , observations )
66+
67+ age = request .form .get ('age' , type = int )
68+ gender = request .form .get ('gender' )
69+ race = request .form .get ('race' )
70+ systolic_bp = request .form .get ('systolic_bp' , type = int )
71+ diastolic_bp = request .form .get ('diastolic_bp' , type = int )
72+ total_cholesterol = request .form .get ('total_cholesterol' , type = int )
73+ hdl_cholesterol = request .form .get ('hdl_cholesterol' , type = int )
74+ ldl_cholesterol = request .form .get ('ldl_cholesterol' , type = int )
75+ diabetes = request .form .get ('diabetes' ) == 'yes'
76+ smoker = request .form .get ('smoker' ) == 'yes'
77+ on_bp_meds = request .form .get ('on_bp_meds' ) == 'yes'
78+ on_statin = request .form .get ('on_statin' ) == 'yes'
79+ on_aspirin = request .form .get ('on_aspirin' ) == 'yes'
80+ refine_risk = request .form .get ('refine_risk' ) == 'yes'
81+
82+ # Use the fetched data if available, otherwise use user input
83+ age = patient_data .get ('age' , age )
84+ gender = patient_data .get ('gender' , gender )
85+ race = patient_data .get ('race' , race )
86+ systolic_bp = patient_data .get ('systolic_bp' , systolic_bp )
87+ diastolic_bp = patient_data .get ('diastolic_bp' , diastolic_bp )
88+ total_cholesterol = patient_data .get ('total_cholesterol' , total_cholesterol )
89+ hdl_cholesterol = patient_data .get ('hdl_cholesterol' , hdl_cholesterol )
90+ ldl_cholesterol = patient_data .get ('ldl_cholesterol' , ldl_cholesterol )
91+
92+ if None in [age , gender , race , systolic_bp , diastolic_bp , total_cholesterol , hdl_cholesterol , ldl_cholesterol ]:
93+ result = 'Incomplete input. Please enter all required fields.'
94+ else :
95+ risk_score = calculate_ascvd_risk (
96+ age , gender , race , systolic_bp , diastolic_bp , total_cholesterol , hdl_cholesterol , ldl_cholesterol ,
97+ diabetes , smoker , on_bp_meds , on_statin , on_aspirin , refine_risk
98+ )
99+ result = f"Estimated 10-year ASCVD Risk: { risk_score :.2f} %"
58100 except ValueError :
59- result = 'Invalid input. Please enter a valid condition ID .'
101+ result = 'Invalid input. Please enter valid values for all fields .'
60102
61- return render_template ('index.html' , result = result )
103+ return render_template ('index.html' , result = result , patient_data = patient_data )
62104
105+ def calculate_ascvd_risk (age , gender , race , systolic_bp , diastolic_bp , total_cholesterol , hdl_cholesterol , ldl_cholesterol ,
106+ diabetes , smoker , on_bp_meds , on_statin , on_aspirin , refine_risk ):
107+ # Simplified calculation for demonstration purposes
108+ risk_score = (age * 0.1 + total_cholesterol * 0.2 + hdl_cholesterol * - 0.1 +
109+ systolic_bp * 0.2 + diastolic_bp * 0.1 +
110+ (10 if on_bp_meds else 0 ) + (10 if on_statin else 0 ) + (5 if on_aspirin else 0 ) +
111+ (20 if smoker else 0 ) + (30 if diabetes else 0 ))
112+ if refine_risk :
113+ risk_score *= 0.9 # Example refinement factor
114+ return risk_score
63115
64116if __name__ == '__main__' :
65- port_str = os .environ ['FHIR_PORT' ]
66- port_int = int (port_str )
67- app .run (debug = True , port = port_int )
117+ port = int (os .getenv ('PORT' , 5000 ))
118+ app .run (debug = True , port = port )
0 commit comments