/
app.py
127 lines (114 loc) · 5.07 KB
/
app.py
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
from flask import Flask, render_template
from fhir_parser import FHIR
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def index():
fhir = FHIR()
patients = fhir.get_all_patients()
return render_template('main.html', patients=patients)
@app.route('/patientObs/<string:id>')
def observations(id):
fhir = FHIR()
patient = fhir.get_patient(id)
observations = fhir.get_patient_observations(id)
spreadSheet = {
'bodyWeight' : [],
'BMI' : [],
'dBP' : [],
'sBP': [],
'pain':[],
'latestObs' : {
'uuid': "",
'latestDate': "",
'type': "",
'status': "",
'weight': "",
'sBP': "",
'BMI': ""
}
}
for i in observations:
components = i.components
for i2 in components:
value = i2.value
if value == None: continue
date = i.effective_datetime
dateStr = str(date.day) + "/" + str(date.month) + "/" + str(date.year)
display = i2.display
if display == "Body Weight": spreadSheet['bodyWeight'].append((dateStr,value))
elif display == "Diastolic Blood Pressure": spreadSheet['dBP'].append((dateStr,value))
elif display == "Systolic Blood Pressure": spreadSheet['sBP'].append((dateStr,value))
elif display == "Body Mass Index": spreadSheet['BMI'].append((dateStr,value))
elif "Pain severity" in display: spreadSheet['pain'].append((dateStr,value))
dictItemsToBeSorted = [spreadSheet['bodyWeight'], spreadSheet['BMI'], spreadSheet['dBP'], spreadSheet['sBP'], spreadSheet['pain']]
sortTime(dictItemsToBeSorted)
spreadSheet['latestObs']['uuid'] = observations[-1].uuid
tmp = observations[-1].effective_datetime
spreadSheet['latestObs']['latestDate'] = str(tmp.day) + "/" + str(tmp.month) + "/" + str(tmp.year)
spreadSheet['latestObs']['type'] = observations[-1].type
spreadSheet['latestObs']['status'] = observations[-1].status
spreadSheet['latestObs']['weight'] = spreadSheet['bodyWeight'][-1][-1]
spreadSheet['latestObs']['sBP'] = spreadSheet['sBP'][-1][-1]
spreadSheet['latestObs']['BMI'] = spreadSheet['BMI'][-1][-1]
return render_template("observations.html", obsData = spreadSheet, patient = patient)
@app.route('/demographics')
def demographicGroups():
fhir=FHIR()
patients = fhir.get_all_patients()
statistics = {
'age' : {'0 to 9':0,
'10 to 19':0,
'20 to 29':0,
'30 to 39':0,
'40 to 49':0,
'50 to 59':0,
'60 to 69':0,
'70 to 79':0,
'80 to 100':0,
'> 100':0},
'marital' : {'Never Married':0,
"Single":0,
'Married':0},
'gender' : {'male':0,
'female':0},
'location' : {},
'race' : {},
'language': {}
}
for patient in patients:
age = abs(datetime.today().year - patient.birth_date.year)
marital = str(patient.marital_status)
gender = str(patient.gender)
locations = [str(i.state) for i in patient.addresses]
race = str(patient.get_extension('us-core-race'))
languages = [str(i) for i in patient.communications.languages]
if age <= 9: statistics['age']['0 to 9'] += 1
elif age >= 10 and age <= 19: statistics['age']['10 to 19'] += 1
elif age >= 20 and age <= 29: statistics['age']['20 to 29'] += 1
elif age >= 30 and age <= 39: statistics['age']['30 to 39'] += 1
elif age >= 40 and age <= 49: statistics['age']['40 to 49'] += 1
elif age >= 50 and age <= 59: statistics['age']['50 to 59'] += 1
elif age >= 60 and age <= 69: statistics['age']['60 to 69'] += 1
elif age >= 70 and age <= 79: statistics['age']['70 to 79'] += 1
elif age >= 80 and age <= 100: statistics['age']['80 to 100'] += 1
else: statistics['age']['> 100'] += 1
statistics['marital'][marital] += 1
statistics['gender'][gender] += 1
for i in locations:
if statistics['location'].get(i) == None: statistics['location'][i] = 1
else: statistics['location'][i] += 1
if statistics['race'].get(race) == None: statistics['race'][race] = 1
else: statistics['race'][race] += 1
for i in languages:
if statistics['language'].get(i) == None: statistics['language'][i] = 1
else: statistics['language'][i] += 1
statistics['age'] = statistics['age'].values()
statistics['marital'] = statistics['marital'].values()
statistics['gender'] = statistics['gender'].values()
return render_template("demographic.html", stats = statistics)
def sortTime(inputList):
for i in inputList:
i.sort(key = lambda x : datetime.strptime(x[0], "%d/%m/%Y"))
if __name__ == "__main__":
app.run(debug=True)