[ec103b]: / rule_based_model.py

Download this file

275 lines (217 with data), 9.4 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
"""
Moving toward front end html webpage
# just need the functions, no need to run anything, all commented out
"""
import pandas as pd
import re
import copy
import pprint
import os
path = os.path.dirname(__file__)
'''
# for flaskapp integration: put this into app.py
dataset_location = os.path.join(path, r"14122021 NLP Histo 3032 Reports (Classify Cancerous and Non-cancerous Reports).xlsx")
df_initial = pd.read_excel(dataset_location, usecols="D, F, H, P")
df_initial.rename(columns={"Grade(1, 2, 3, mildly or well = 1, moderately = 2, poorly = 3)": "grades"}, inplace=True)
df_initial['grades'] = df_initial['grades'].fillna(0) # this changes all NaN values in the grade column to 0
'''
###################################
####### ESSENTIAL FUNCTIONS #######
###################################
# STEP 1 FUNCTION
def convert_df(data):
id_list = []
text_list = []
grades_list = []
for index, row in data.iterrows():
id = row['SCM GUIDE'].lower()
text = (row['DIAGNOSIS'] + " " + row['MICROSCOPIC DESCRIPTION']).lower()
grades = re.findall('\d+', str(row['grades']))
# if there are no grades assigned, it is 'Unknown' --> treat as '0' (no grade)
if len(grades) == 0:
grades = ['0']
id_list.append(id)
text_list.append(text)
grades_list.append(grades)
converted_df = pd.DataFrame({'id': id_list, 'text': text_list, 'grades': grades_list})
return converted_df
# STEP 2 FUNCTION
def find_matches(data):
matches_list = []
for index, row in data.iterrows():
pattern = re.compile(r"[\s\S]{25}grade[\s\S]{25}|[\s\S]{25}differentiated[\s\S]{25}")
matches = pattern.findall(row['text'])
matches_list.append(matches)
data["matches"] = matches_list
return data
# STEP 3 FUNCTION
def determine_grade(data):
# for id in determined_data.keys():
# determined_data[id]["matches"] = None # set "matches" = None for all rows in determined_data first
determined_list = []
# Edit patterns for the respective grades here
pattern1 = re.compile(r"low|grade\s1|grade\si|well")
pattern2 = re.compile(r"intermediate|grade\s2|grade\sii|moderately")
pattern3 = re.compile(r"high|grade\s3|grade\siii|poorly")
pattern_false = re.compile(r"dcis|dysplasia") # removed 'nuclear' for now.
for index, row in data.iterrows():
grades_list = []
for match_text in row["matches"]:
grade1_match = pattern1.findall(match_text)
grade2_match = pattern2.findall(match_text)
grade3_match = pattern3.findall(match_text)
matches_false = pattern_false.findall(match_text)
if not matches_false:
if grade1_match:
grades_list.append('1')
if grade2_match:
grades_list.append('2')
if grade3_match:
grades_list.append('3')
# grades_list.append('9')
# If all matches in a given row does not result in any determined grade, set 'determined' = ['0']
if len(grades_list) == 0:
grades_list.append('0')
determined_list.append(grades_list)
data["determined"] = determined_list
# determined_data[id]["determined_grades"] = determined_grades
# determined_data[id]["matches"] = all_matches[id]
return data
# STEP 4 FUNCTION
def evaluate_accuracy(data):
correct_counter = 0
total = len(data)
result_list = [] # holds a string "Correct" or "Wrong" for each row after comparing "grades" with "determined"
for index, row in data.iterrows():
grades = row["grades"]
determined_grades = row["determined"]
if determined_grades == grades: # if determined grades matches actual grades exacty, it is correct
correct_counter += 1
result = "Correct"
else:
result = "Wrong"
result_list.append(result)
data["result"] = result_list
score = correct_counter / total
return data, score
###################################
##### MISCELLANEOUS FUNCTIONS #####
###################################
def wrong_gradings(data):
id_list = []
text_list = []
grades_list = []
matches_list = []
determined_list = []
result_list = []
for index, row in data.iterrows():
if row['result'] == 'Wrong':
id_list.append(row['id'])
text_list.append(row['text'])
grades_list.append(row['grades'])
matches_list.append(row['matches'])
determined_list.append(row['determined'])
result_list.append(row['result'])
wrong_df = pd.DataFrame({'id': id_list, 'text': text_list, 'grades': grades_list, 'matches': matches_list, 'determined': determined_list, 'result': result_list})
return wrong_df
def correct_gradings(data):
id_list = []
text_list = []
grades_list = []
matches_list = []
determined_list = []
result_list = []
for index, row in data.iterrows():
if row['result'] == 'Correct':
id_list.append(row['id'])
text_list.append(row['text'])
grades_list.append(row['grades'])
matches_list.append(row['matches'])
determined_list.append(row['determined'])
result_list.append(row['result'])
correct_df = pd.DataFrame({'id': id_list, 'text': text_list, 'grades': grades_list, 'matches': matches_list, 'determined': determined_list, 'result': result_list})
return correct_df
def false_positives(data):
id_list = []
text_list = []
grades_list = []
matches_list = []
determined_list = []
result_list = []
for index, row in data.iterrows():
# if there are detected matches, yet there are not supposed to be any grades at all (grades = ['0'])
if len(row['matches']) != 0 and row['grades'] == ['0']:
id_list.append(row['id'])
text_list.append(row['text'])
grades_list.append(row['grades'])
matches_list.append(row['matches'])
determined_list.append(row['determined'])
result_list.append(row['result'])
false_positives_df = pd.DataFrame({'id': id_list, 'text': text_list, 'grades': grades_list, 'matches': matches_list, 'determined': determined_list, 'result': result_list})
return false_positives_df
def false_negatives(data):
id_list = []
text_list = []
grades_list = []
matches_list = []
determined_list = []
result_list = []
for index, row in data.iterrows():
# if there are NO detected matches, yet there are supposed to be some grade(s) assigned to the report.
if len(row['matches']) == 0 and row['grades'] != ['0']:
id_list.append(row['id'])
text_list.append(row['text'])
grades_list.append(row['grades'])
matches_list.append(row['matches'])
determined_list.append(row['determined'])
result_list.append(row['result'])
false_negatives_df = pd.DataFrame({'id': id_list, 'text': text_list, 'grades': grades_list, 'matches': matches_list, 'determined': determined_list, 'result': result_list})
return false_negatives_df
'''
# for flaskapp integration: don't need to run anything, will do in app.py
# This section of the code utlises all the essential functions defined above.
# The naming of varaibles will start with 'df' and then state the columns in the
# corresponding dataframe, separated by a '_' (e.g. df_id_text_grades_matches
# means that the dataframe has 4 columns: id, text, grades, matches)
# The steps that the dataframe goes through are as such:
# Step 1: Convert DF to show ID, TEXT and GRADES
df = convert_df(df_initial)
# Step 2: Find text matches to the word 'grade' and 'differentiated' and store in list (+ MATCHES)
df = find_matches(df)
# Step 3: Determine the list of grades from the list of matches (+ DETERMINED)
df = determine_grade(df)
# Step 4: Evaulate if determined grade is "Correct" or "Wrong" and calculate overall accuracy score (+ RESULT)
df, accuracy_score = evaluate_accuracy(df)
# RESULTANT DATAFRAME AFTER ALL STEPS (6 COLUMNS)
print("The total number of REPORTS is: " + str(len(df)))
print("\n")
print("The accuracy of determine_grade function is: " + str(accuracy_score))
print("\n")
df
# dataframe for wrong gradings
wrong_df = wrong_gradings(df)
print("The number of wrongly graded rows is: " + str(len(wrong_df)))
wrong_df
# dataframe for correct gradings
correct_df = correct_gradings(df)
print("The number of correctly graded reports is: " + str(len(correct_df)))
correct_df
# dataframe for false positives
false_positives_df = false_positives(df)
print("The number of false_positives is: " + str(len(false_positives_df)))
false_positives_df
# dataframe for false negatives
false_negatives_df = false_negatives(df)
print("The number of false_negatives is: " + str(len(false_negatives_df)))
false_negatives_df
# exporting dataframes
try:
os.mkdir(os.path.join(path,r'csvfiles')) # create directory to store csv files
except OSError as error:
print(error)
df.to_csv(os.path.join(path, r'csvfiles\df.csv')) # export to to csv file
wrong_df.to_csv(os.path.join(path, r'csvfiles\wrong_df.csv')) # export to to csv file
correct_df.to_csv(os.path.join(path, r'csvfiles\correct_df.csv')) # export to to csv file
false_positives_df.to_csv(os.path.join(path, r'csvfiles\false_positives_df.csv')) # export to to csv file
false_negatives_df.to_csv(os.path.join(path, r'csvfiles\false_negatives_df.csv')) # export to to csv file
'''