[ec103b]: / app.py

Download this file

536 lines (393 with data), 23.5 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
from flask import Flask, request, render_template, url_for
from werkzeug.utils import secure_filename, redirect
import pandas as pd
import pickle
import csv
import os
import rule_based_model as rule_based_model
# Declare a Flask app
app = Flask(__name__)
path = os.path.dirname(__file__)
@app.route('/', methods=['GET', 'POST'])
def index():
# If a form is submitted
if request.method == "POST":
req = ""
for filename, file in request.files.items():
req = request.files[filename].name
print(req)
if req == "model1":
clf = pickle.load(open('model.pkl', 'rb'))
f = request.files[req]
f.save(secure_filename(f.filename))
data = []
with open(f.filename) as file:
reader = csv.DictReader(file)
[data.append(dict(row)) for row in reader]
input_df = pd.DataFrame(data)
input_df['Combined Diagnosis'] = input_df['Diagnosis'] + input_df['Gross Description'] + input_df[
'Microscopic Description']
input_df = input_df[['Combined Diagnosis']]
# Get prediction
predictions, output = clf.predict(input_df['Combined Diagnosis'].values.tolist())
prediction = pd.DataFrame(predictions, columns=['Cancerous?'])
prediction = pd.concat([input_df, prediction], axis=1)
prediction[["accepted-rejected", "comments"]] = ""
print(prediction.head())
print(len(prediction))
pred_csv = prediction.to_csv(os.path.join(path, r"preds.csv"))
return redirect(url_for('cancerprediction'))
elif req == "model2":
clf = pickle.load(open('model2.pkl', 'rb'))
f = request.files[req]
f.save(secure_filename(f.filename))
data = []
with open(f.filename) as file:
reader = csv.DictReader(file)
[data.append(dict(row)) for row in reader]
input_df = pd.DataFrame(data)
input_df['Combined Diagnosis'] = input_df['Diagnosis'] + input_df['Gross Description'] + input_df[
'Microscopic Description']
input_df = input_df[['Combined Diagnosis']]
columns = ('AMPULLA', 'ANAL CANAL', 'ANORECTAL JUNCTION', 'ANTERIOR SEGMENT', 'ANUS', 'APPENDIX', 'ASCENDING)', 'AXILLA (LEFT)', 'BLADDER', 'BREAST', 'BREAST (LEFT)', 'BREAST (RIGHT)', 'CAECUM', 'CAECUM/ILEOCAECAL JUNCTION', 'CERVIX', 'CHEST WALL', 'CHEST WALL (LEFT)', 'COLON', 'COLON (ANASTOMOTIC SITE)', 'COLON (ASCENDING)', 'COLON (CAECUM', 'COLON (CAECUM)', 'COLON (DESCENDING)', 'COLON (DISTAL TRANSVERSE)', 'COLON (HEPATIC FLEXURE)', 'COLON (PROXIMAL SIGMOID)', 'COLON (RECTOSIGMOID)', 'COLON (RECTUM)', 'COLON (RIGHT)', 'COLON (SIGMOID)', 'COLON (SPLENIC FLEXURE)', 'COLON (TRANSVERSE)', 'COLON (UPPER RECTUM)', 'COLORECTAL', 'COLORECTAL (PRIMARY)', 'DUODENUM', 'ENDOMETRIUM', 'ESOPHAGUS', 'FALLOPIAN TUBE (LEFT)', 'FALLOPIAN TUBE (RIGHT)', 'FOOT', 'GASTRIC', 'HEPATIC FLEXURE', 'KIDNEY', 'KIDNEY (LEFT)', 'LARGE BOWEL', 'LIVER', 'LIVER (LEFT LOBE)', 'LIVER (SEGMENT II)', 'LIVER SEGMENT 7/8 NODULE', 'LIVER SEGMENT 8 NODULE', 'LIVER SEGMENT II', 'LUNG', 'LUNG (LEFT LOWER LOBE)', 'LUNG (RIGHT LOWER LOBE)', 'LUNG (RIGHT UPPER LOBE)', 'LUNG (RIGHT)', 'MANDIBLE', 'NASOPHARYNX', 'OVARY', 'PARASTERNAL (LEFT)', 'PAROTID (LEFT)', 'PERITONEUM', 'PROSTATE', 'RECTAL', 'RECTOSIGMOID', 'RECTUM', 'RENAL (LEFT)', 'RENAL (RIGHT)', 'RIGHT LOWER LOBE', 'RIGHT UPPER LOBE', 'SALIVARY GLAND', 'SIGMOID', 'SPLENIC FLEXURE', 'THIGH', 'THYMUS', 'TONGUE', 'TONSIL', 'UNKNOWN', 'UTERUS')
# Get prediction
predictions, output = clf.predict(input_df['Combined Diagnosis'].values.tolist())
prediction = pd.DataFrame(predictions,
columns=['AMPULLA', 'ANAL CANAL', 'ANORECTAL JUNCTION', 'ANTERIOR SEGMENT', 'ANUS', 'APPENDIX', 'ASCENDING)', 'AXILLA (LEFT)', 'BLADDER', 'BREAST', 'BREAST (LEFT)', 'BREAST (RIGHT)', 'CAECUM', 'CAECUM/ILEOCAECAL JUNCTION', 'CERVIX', 'CHEST WALL', 'CHEST WALL (LEFT)', 'COLON', 'COLON (ANASTOMOTIC SITE)', 'COLON (ASCENDING)', 'COLON (CAECUM', 'COLON (CAECUM)', 'COLON (DESCENDING)', 'COLON (DISTAL TRANSVERSE)', 'COLON (HEPATIC FLEXURE)', 'COLON (PROXIMAL SIGMOID)', 'COLON (RECTOSIGMOID)', 'COLON (RECTUM)', 'COLON (RIGHT)', 'COLON (SIGMOID)', 'COLON (SPLENIC FLEXURE)', 'COLON (TRANSVERSE)', 'COLON (UPPER RECTUM)', 'COLORECTAL', 'COLORECTAL (PRIMARY)', 'DUODENUM', 'ENDOMETRIUM', 'ESOPHAGUS', 'FALLOPIAN TUBE (LEFT)', 'FALLOPIAN TUBE (RIGHT)', 'FOOT', 'GASTRIC', 'HEPATIC FLEXURE', 'KIDNEY', 'KIDNEY (LEFT)', 'LARGE BOWEL', 'LIVER', 'LIVER (LEFT LOBE)', 'LIVER (SEGMENT II)', 'LIVER SEGMENT 7/8 NODULE', 'LIVER SEGMENT 8 NODULE', 'LIVER SEGMENT II', 'LUNG', 'LUNG (LEFT LOWER LOBE)', 'LUNG (RIGHT LOWER LOBE)', 'LUNG (RIGHT UPPER LOBE)', 'LUNG (RIGHT)', 'MANDIBLE', 'NASOPHARYNX', 'OVARY', 'PARASTERNAL (LEFT)', 'PAROTID (LEFT)', 'PERITONEUM', 'PROSTATE', 'RECTAL', 'RECTOSIGMOID', 'RECTUM', 'RENAL (LEFT)', 'RENAL (RIGHT)', 'RIGHT LOWER LOBE', 'RIGHT UPPER LOBE', 'SALIVARY GLAND', 'SIGMOID', 'SPLENIC FLEXURE', 'THIGH', 'THYMUS', 'TONGUE', 'TONSIL', 'UNKNOWN', 'UTERUS'])
prediction = pd.concat([input_df, prediction], axis=1)
prediction[["Predicted Primary Site(s)"]] = ""
for index, row in prediction.iterrows():
sites = ""
for organ in columns:
if row[organ] == 1:
sites += ", " + str(organ)
if sites == "":
sites = "None Predicted"
prediction.iat[index, 81] = sites
prediction[["accepted-rejected", "comments"]] = ""
print(prediction.head())
print(len(prediction))
pred_csv = prediction.to_csv(os.path.join(path, r"preds.csv"))
return redirect(url_for('primarysitedetection'))
elif req == "model3":
# get the file uploaded
f = request.files[req]
f.save(secure_filename(f.filename))
# link to regex model
# from rule_based_model code
df_initial = pd.read_csv(f.filename)
df_initial.columns = [x.upper() for x in df_initial.columns]
df_initial = df_initial[["SCM GUIDE", "DIAGNOSIS", "MICROSCOPIC DESCRIPTION",
"GRADE(1, 2, 3, MILDLY OR WELL = 1, MODERATELY = 2, POORLY = 3)"]]
print(df_initial)
df_initial.rename(columns={'GRADE(1, 2, 3, MILDLY OR WELL = 1, MODERATELY = 2, POORLY = 3)': 'grades'},
inplace=True)
print(df_initial)
df_initial['grades'] = df_initial['grades'].fillna(
0) # this changes all NaN values in the grade column to 0
print(df_initial)
# Step 1: Convert DF to show ID, TEXT and GRADES
df = rule_based_model.convert_df(df_initial)
# Step 2: Find text matches to the word 'grade' and 'differentiated' and store in list (+ MATCHES)
df = rule_based_model.find_matches(df)
# Step 3: Determine the list of grades from the list of matches (+ DETERMINED)
df = rule_based_model.determine_grade(df)
# Step 4: Evaulate if determined grade is "Correct" or "Wrong" and calculate overall accuracy score (+ RESULT)
df, accuracy_score = rule_based_model.evaluate_accuracy(df)
# create the new columns
df[["accepted-rejected", "comments"]] = ""
pred_csv = df.to_csv(os.path.join(path, r'preds.csv'))
return redirect(url_for('rulebasedmodelwebpage'))
else:
pass
else:
prediction = ""
return render_template("index.html")
dataset_location = os.path.join(path, r"preds.csv")
@app.route('/cancerprediction', methods=['GET', 'POST'])
def cancerprediction():
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
print(data)
# print(data)
row_number = 0 # initialise row number to zero
number_of_reports = len(data) # for the total number of reports in the html
if request.method == "POST":
if request.values.get("report-number-input"):
row_number = int(request.values.get("report-number-input"))
if request.values.get("accept-button"):
# print(request.values.get("accept-button")) to see output values
row_number = int(
request.values.get("accept-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 3] = "Accepted"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("reject-button"):
# print(request.values.get("reject-button")) to see output values
row_number = int(
request.values.get("reject-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 3] = "Rejected"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("comments-given-input"):
# print(request.values.get("comments-given-input")) # to see output values
comment = request.values.get("comments-given-input")
row_number = int(request.values.get(
"comment-submit-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) # to debug
# print(comment) # to check if row number is updated
# print(row_number) # to check if row number is updated
df.iat[row_number, 4] = comment
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code, could be stored in a function
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
if row_number >= number_of_reports or row_number < 0:
row_number = 0
# print(row_number) console print for debugging
# render HTML page dynamically
return render_template("cancerpredictionmodel.html", data=data, list=list, len=len, str=str, row_number=row_number,
number_of_reports=number_of_reports)
@app.route('/primarysitedetection', methods=['GET', 'POST'])
def primarysitedetection():
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
print(data)
# print(data)
row_number = 0 # initialise row number to zero
number_of_reports = len(data) # for the total number of reports in the html
if request.method == "POST":
if request.values.get("report-number-input"):
row_number = int(request.values.get("report-number-input"))
if request.values.get("accept-button"):
# print(request.values.get("accept-button")) to see output values
row_number = int(
request.values.get("accept-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 83] = "Accepted"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("reject-button"):
# print(request.values.get("reject-button")) to see output values
row_number = int(
request.values.get("reject-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 83] = "Rejected"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("comments-given-input"):
# print(request.values.get("comments-given-input")) # to see output values
comment = request.values.get("comments-given-input")
row_number = int(request.values.get(
"comment-submit-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) # to debug
# print(comment) # to check if row number is updated
# print(row_number) # to check if row number is updated
df.iat[row_number, 84] = comment
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code, could be stored in a function
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
if row_number >= number_of_reports or row_number < 0:
row_number = 0
# print(row_number) console print for debugging
# render HTML page dynamically
return render_template("primarysitepredictionmodel.html", data=data, list=list, len=len, str=str, row_number=row_number,
number_of_reports=number_of_reports)
@app.route('/rulebasedmodelwebpage', methods=["POST", "GET"])
def rulebasedmodelwebpage():
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
row_number = 0 # initialise row number to zero
number_of_reports = len(data) # for the total number of reports in the html
if request.method == "POST":
if request.values.get("report-number-input"):
row_number = int(request.values.get("report-number-input"))
if request.values.get("accept-button"):
# print(request.values.get("accept-button")) to see output values
row_number = int(
request.values.get("accept-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 7] = "Accepted"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("reject-button"):
# print(request.values.get("reject-button")) to see output values
row_number = int(
request.values.get("reject-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) to debug
# print(row_number) # to check if row number is updated
# edit cell based on cell value row, column
# https://re-thought.com/how-to-change-or-update-a-cell-value-in-python-pandas-dataframe/
df.iat[row_number, 7] = "Rejected"
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
elif request.values.get("comments-given-input"):
# print(request.values.get("comments-given-input")) # to see output values
comment = request.values.get("comments-given-input")
row_number = int(request.values.get(
"comment-submit-button")) # stored row number in the button value so can access it
# Read csv into dataframe
df = pd.read_csv(dataset_location)
# print(df) # to debug
# print(comment) # to check if row number is updated
# print(row_number) # to check if row number is updated
df.iat[row_number, 8] = comment
# write output
df.to_csv(dataset_location, index=False)
# to read the csv, repeated code, could be stored in a function
# variable to hold CSV data
data = []
# read data from CSV file
with open(dataset_location) as f:
# create CSV dictionary reader instance
reader = csv.DictReader(f)
# init CSV dataset
[data.append(dict(row)) for row in reader]
# print(data)
number_of_reports = len(data) # for the total number of reports in the html
if row_number >= number_of_reports or row_number < 0:
row_number = 0
# print(row_number) console print for debugging
# render HTML page dynamically
return render_template("cancergradepredictionmodel.html", data=data, list=list, len=len, str=str, row_number=row_number,
number_of_reports=number_of_reports)
# Running the app
if __name__ == '__main__':
app.run(debug=True)