Skip to content

Commit d4f5203

Browse files
author
mounika
committed
update
1 parent 5d7f12c commit d4f5203

3 files changed

Lines changed: 130 additions & 79 deletions

File tree

.env

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
FHIR_USERNAME=
2-
FHIR_PASSWORD=
1+
FHIR_SERVER_BASE_URL=http://pwebmedcit.services.brown.edu:9091/fhir
2+
FHIR_USERNAME=bcbifhir
3+
FHIR_PASSWORD=fancy-company-attraction-p3rson
34
FHIR_PORT=5000

src/app.py

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,118 @@
1-
import requests
21
import os
2+
import requests
33
from flask import Flask, request, render_template
44
from dotenv import load_dotenv
55

6-
76
app = Flask(__name__)
8-
9-
FHIR_SERVER_BASE_URL="http://pwebmedcit.services.brown.edu:9091/fhir"
10-
117
load_dotenv()
128

9+
FHIR_SERVER_BASE_URL = os.getenv("FHIR_SERVER_BASE_URL")
1310
username = os.getenv("FHIR_USERNAME")
1411
password = os.getenv("FHIR_PASSWORD")
1512

16-
1713
def 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'])
5052
def 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

64116
if __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)

src/templates/index.html

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
<!DOCTYPE html>
2-
32
<html lang="en">
43
<head>
54
<meta charset="UTF-8">
6-
<title>Patient Finder for a given condition_id</title>
5+
<title>ASCVD Risk Calculator</title>
76
</head>
87
<body>
9-
<div class=container>
10-
<h1>Patient Finder for a given condition_id</h1>
11-
12-
<form method="post">
13-
<label for="condition_id">Enter a condition ID:</label>
14-
<input type="text" name="condition_id" id="condition_id">
15-
<input type="submit" value="Search">
16-
</form>
17-
18-
{% if result %}
19-
<h3>Total Patients: {{ result['total_patients'] }}</h3>
20-
<h3>Results:</h3>
21-
<ul>
22-
{% for patient in result['unique_patients'] %}
23-
<li>
24-
<p>Patient ID: {{ patient[0] }}</p>
25-
<p>First Name: {{ patient[1] }}</p>
26-
<p>Last Name: {{ patient[2] }}</p>
27-
<p>Gender: {{ patient[3] }}</p>
28-
<p>Birth Date: {{ patient[4] }}</p>
29-
</li>
30-
{% endfor %}
31-
</ul>
32-
{% endif %}
33-
34-
35-
</div>
36-
</body>
37-
</html>
38-
39-
40-
<style>
41-
.container {
42-
display: grid;
43-
place-items: center;
44-
}
45-
</style>
8+
<div class="container">
9+
<h1>ASCVD Risk Calculator</h1>
10+
<form method="post">
11+
<label for="patient_id">Patient ID:</label>
12+
<input type="text" name="patient_id" id="patient_id"><br>
13+
14+
<label for="age">Current Age:</label>
15+
<input type="number" name="age" id="age" value="{{ patient_data.get('age', '') }}" required><br>
16+
17+
<label for="gender">Sex:</label>
18+
<select name="gender" id="gender" required>
19+
<option value="male" {% if patient_data.get('gender') == 'male' %}selected{% endif %}>Male</option>
20+
<option value="female" {% if patient_data.get('gender') == 'female' %}selected{% endif %}>Female</option>
21+
</select><br>
22+
23+
<label for="race">Race:</label>
24+
<select name="race" id="race" required>
25+
<option value="white" {% if patient_data.get('race') == 'white' %}selected{% endif %}>White</option>
26+
<option value="black" {% if patient_data.get('race') == 'black' %}selected{% endif %}>Black</option>
27+
<option value="asian" {% if patient_data.get('race') == 'asian' %}selected{% endif %}>Asian</option>
28+
<option value="other" {% if patient_data.get('race') == 'other' %}selected{% endif %}>Other</option>
29+
</select><br>
30+
31+
<label for="systolic_bp">Systolic Blood Pressure (mm Hg):</label>
32+
<input type="number" name="systolic_bp" id="systolic_bp" value="{{ patient_data.get('systolic_bp', '') }}" required><br>
33+
34+
<label for="diastolic_bp">Diastolic Blood Pressure (mm Hg):</label>
35+
<input type="number" name="diastolic_bp" id="diastolic_bp" value="{{ patient_data.get('diastolic_bp', '') }}" required><br>
36+
37+
<label for="total_cholesterol">Total Cholesterol (mg/dL):</label>
38+
<input type="number" name="total_cholesterol" id="total_cholesterol" value="{{ patient_data.get('total_cholesterol', '') }}" required><br>
39+
40+
<label for="hdl_cholesterol">HDL Cholesterol (mg/dL):</label>
41+
<input type="number" name="hdl_cholesterol" id="hdl_cholesterol" value="{{ patient_data.get('hdl_cholesterol', '') }}" required><br>
42+
43+
<label for="ldl_cholesterol">LDL Cholesterol (mg/dL):</label>
44+
<input type="number" name="ldl_cholesterol"

0 commit comments

Comments
 (0)