-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
317 lines (261 loc) · 10.7 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
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
from flask import Flask, render_template, request, jsonify, Response
from flask.json import JSONEncoder
from werkzeug.exceptions import BadRequestKeyError
from datetime import date, datetime
import psycopg2
import psycopg2.extras
import json
import io
import os
import re
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
# Database login credentials
DATABASE = "weight_watcher"
USER = "weight_watcher"
PASSWORD = "letmewatch"
HOST = "127.0.0.1"
PORT = "5432"
# Mapping of formal table headers to column names on database
DASH_COLUMNS = [
("Patient ID", "patientid"),
("Lastname", "lastname"),
("Firstname", "firstname"),
("Sex", "sex"),
("Age", "age"),
("Treatment", "treatment"),
("Last Appointment", "last_appointment"),
("Last Weight", "last_weight"),
("Current Weight", "current_weight"),
("Change in Weight", "change_weight"),
("Last Submission", "last_submit")
]
SYMP_COLUMNS = [
("Nausea", "nausea"),
("Skin Irritation", "skin_irritate"),
("Difficulty Swallowing", "diff_swallow"),
("Difficulty Breathing", "diff_breath")
]
IMAG_COLUMNS = ['ct_path', 'ctcont_path', 'rt_path']
# Global variables for sorting functionality
DEF_SORT_KEY = "change_weight"
LAST_SORT_KEY = ""
REV_ORDER = False
# Weight threshold for color coding
WEIGHT_THRESH = [-3, 0]
# Datetime formats
DATETIME_FORMAT_LIVE = "%a %b/%d/%Y %H:%M:%S"
DATETIME_FORMAT_TABLE = "%a %b/%d/%Y"
# Modifiable global variable for recorded weights
REC_WEIGHTS = []
# Excess path information
PATH_EXCESS = os.path.join('/home/skadi/Documents/MSc/W20/Instr_and_Comp/',
'Project', 'patient-weight-watch')
# JSON encoder to keep dates in the format yyyy-mm-dd
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, date):
return obj.strftime(DATETIME_FORMAT_TABLE) # %H:%M:%S")
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
app = Flask(__name__, template_folder='templates', static_url_path='/static')
app.json_encoder = CustomJSONEncoder
app.config["DEBUG"] = True
# Function that handles requests to the home page (dashboard)
@app.route("/")
@app.route("/home")
@app.route("/index")
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
try:
sort_key = request.form["sort_key"]
update_last_sort(sort_key)
except BadRequestKeyError:
print('No POST form received')
update_last_sort(DEF_SORT_KEY)
global REV_ORDER
REV_ORDER = False
finally:
response = update_dash()
jsonDat = json.loads(response.get_data().decode("utf-8"))
tabData = jsonDat["tab_dat"]
lastUpdate = jsonDat["time"]
sortStatus = jsonDat["sort"]
return render_template('dashboard.html', tabData=tabData,
colNames=DASH_COLUMNS, lastUpdate=lastUpdate,
sortStatus=sortStatus, threshold=WEIGHT_THRESH)
# Function that sends time, sort, and database info as json to "/update_dash"
@app.route('/update_dash')
def update_dash():
command_query_db = """SELECT * FROM PATIENT_INFO"""
response = exec_db_command(command_query_db, 1) # 1 returns fetchall
rows = build_weights(response)
if LAST_SORT_KEY != "":
# use last sort settings for live update
sort_key, rev_order = LAST_SORT_KEY, REV_ORDER
else:
sort_key, rev_order = DEF_SORT_KEY, False
rows.sort(key=lambda k: k.get(sort_key), reverse=rev_order)
sort_key_formal = get_formalized(sort_key)
if rev_order:
sort_key_formal += ' (reverse)'
now = datetime.now().strftime(DATETIME_FORMAT_LIVE)
return jsonify(time=now, sort=sort_key_formal, tab_dat=rows)
# Function that sends RT info, symptoms, and weight history to "/RT_symptoms"
@app.route('/RT_symptoms', methods=['POST'])
def get_RTsymptoms():
p_id = int(request.form["p_id"])
# query PATIENT_INFO using p_id
command_query_info_db = """SELECT * FROM PATIENT_INFO
WHERE PATIENTID = %i""" % p_id
response = exec_db_command(command_query_info_db, 1)
row_info = build_weights(response)[0] # just take the only element in list
rec_weights_arr = [float(x) for x in row_info['rec_weights'].split(', ')]
# update global REC_WEIGHTS
global REC_WEIGHTS
REC_WEIGHTS = rec_weights_arr
# query PATIENT_SYMPTOMS using p_id
command_query_symp_db = """SELECT * FROM PATIENT_SYMPTOMS
WHERE PATIENTID = %i""" % p_id
row_symp = exec_db_command(command_query_symp_db, 1)[0]
# query PATIENT_IMAGES using p_id
command_query_imag_db = """SELECT * FROM PATIENT_IMAGES
WHERE PATIENTID = %i""" % p_id
response = exec_db_command(command_query_imag_db, 1)[0]
row_imag = reformat_path_dict(response)
assert all_dict_have_same_keys(row_imag)
t_keys = [(i+1, x) for i, x in enumerate(row_imag[IMAG_COLUMNS[0]].keys())]
return render_template('RT_symptoms.html', p_id=p_id, defKey=DEF_SORT_KEY,
row_info=row_info, row_symp=row_symp,
row_imag=row_imag, t_keys=t_keys,
colNames=DASH_COLUMNS, colNamesSymp=SYMP_COLUMNS)
# Function called upon submission of symptom webform (db for database)
@app.route('/update_db', methods=['POST'])
def update_db():
p_id = int(request.form["p_id"])
p_weight = request.form["p_weight"]
p_nausea = int(request.form["p_nausea"])
p_skinirr = int(request.form["p_skinirr"])
p_diffswall = int(request.form["p_diffswall"])
p_diffbreath = int(request.form["p_diffbreath"])
# update PATIENT_SYMPTOMS
command_update_symp = """UPDATE PATIENT_SYMPTOMS \
SET NAUSEA = %i, SKIN_IRRITATE = %i, \
DIFF_SWALLOW = %i, DIFF_BREATH = %i \
WHERE PATIENTID = %i \
""" % (p_nausea, p_skinirr, p_diffswall,
p_diffbreath, p_id)
exec_db_command(command_update_symp, 0)
# update rec_weights and last_submit on PATIENT_INFO
command_update_info = """UPDATE PATIENT_INFO \
SET REC_WEIGHTS = REC_WEIGHTS || '%s', \
LAST_SUBMIT = '%s' \
WHERE PATIENTID = %i \
""" % (', '+str(p_weight), datetime.now(), p_id)
exec_db_command(command_update_info, 0)
return render_template("submitted.html", p_weight=p_weight,
p_nausea=p_nausea, p_skinirr=p_skinirr,
p_diffswall=p_diffswall, p_diffbreath=p_diffbreath)
# Function that posts plot of recorded weights to '/weights_plot.png'
@app.route('/weights_plot.png')
def plot_weights():
fig = create_figure(REC_WEIGHTS)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')
# Helper function that plots weights trend of chosen patient
def create_figure(rec_weights):
# TODO: beautify plot
fig = Figure()
ax = fig.add_subplot(1, 1, 1)
x_vals = [x+1 for x in range(len(rec_weights))]
ax.plot(x_vals, rec_weights)
ax.scatter(x_vals, rec_weights)
ax.set_xlabel('Day Number', fontsize=18)
ax.set_ylabel('Weights [kg]', fontsize=18)
ax.set_xticks(x_vals)
ax.grid()
fig.tight_layout()
return fig
# Helper function to extract and format weight info from rec_weights column
def build_weights(p_list):
new_list = []
for dictionary in p_list:
p_dict = dictionary.copy()
weights = p_dict["rec_weights"].split(", ")
last_weight = float(weights[-2])
current_weight = float(weights[-1])
change_weight = current_weight - last_weight
p_dict["last_weight"] = float('%.2f' % last_weight)
p_dict["current_weight"] = float('%.2f' % current_weight)
p_dict["change_weight"] = float('%.2f' % change_weight)
# print(p_dict)
new_list.append(p_dict)
return new_list
# Helper function that executes given MySQL string command
def exec_db_command(command, withval):
con = psycopg2.connect(database=DATABASE, user=USER, password=PASSWORD,
host=HOST, port=PORT)
cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute(command)
con.commit()
if withval: # if withval has value, return fetchall data
return cur.fetchall()
# Helper function to get formal name of database column name
def get_formalized(db_col_name):
for pair in DASH_COLUMNS:
if pair[1] == db_col_name:
return pair[0]
return "Undefined column name"
# Helper function that updates global sort info
def update_last_sort(sort_key):
global LAST_SORT_KEY, REV_ORDER
if LAST_SORT_KEY == sort_key:
# just inverse sort order
REV_ORDER = not REV_ORDER
else:
# update sort key and reset sort order
LAST_SORT_KEY = sort_key
REV_ORDER = False
# print("LAST_SORT_KEY updated to:", LAST_SORT_KEY)
# print("REV_ORDER updated to:", REV_ORDER)
# Helper function to reformat paths dictionary to hand treatment days
def reformat_path_dict(paths_dict):
paths_dict_new = {}
for key in IMAG_COLUMNS:
# remove excess string
str_new = re.sub(PATH_EXCESS, '', paths_dict[key])
paths_list = str_new.split(', ')
paths_dict_new[key] = build_treatment_dict(paths_list)
return paths_dict_new
# Helper function to build dictionary for treatment days
def build_treatment_dict(paths_list):
treatment_dict_new = {}
ctr = 1 # assumes that paths in list are arranged by treatment number
for path in sorted(paths_list):
while(True):
str_pattern = '/t' + str(ctr) + '/'
key = 't' + str(ctr)
if str_pattern in path:
if key not in treatment_dict_new.keys():
treatment_dict_new[key] = [path]
else:
treatment_dict_new[key].append(path)
break
else:
ctr += 1
return treatment_dict_new
# Helper function to check if all dictionaries have the same keys
def all_dict_have_same_keys(root_dict):
ref = len(root_dict[IMAG_COLUMNS[0]].keys())
for i in range(1, len(IMAG_COLUMNS)):
if len(root_dict[IMAG_COLUMNS[0]].keys()) != ref:
return False
return True
if __name__ == '__main__':
app.run()