-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouping_atc.py
285 lines (250 loc) · 14.5 KB
/
grouping_atc.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
import pandas as pd
from collections import OrderedDict
def atc_grouping_third(lookup_file, inferred_file, filename, choice="all"):
if choice=="all":
if lookup_file=="N/A" and inferred_file!="N/A":
df = pd.read_csv(inferred_file)
elif inferred_file=="N/A" and lookup_file!="N/A":
df = pd.read_csv(lookup_file)
elif lookup_file=="N/A" and inferred_file=="N/A":
print("Both Files empty")
return
else:
df_lookup = pd.read_csv(lookup_file)
df_inferred = pd.read_csv(inferred_file)
frames = [df_lookup, df_inferred]
df = pd.concat(frames)
elif choice=="lookup":
df = pd.read_csv(lookup_file)
elif choice=="inferred":
df = pd.read_csv(inferred_file)
df.to_csv(filename+'_Grouped.csv', index=False)
atc_groups = {}
check_rows= []
for idi, i in df.iterrows():
if isinstance(i['ATC_Code_3rd_Level'], float):
if "OTHER" not in atc_groups.keys():
atc_groups["OTHER"] = [1, [i["Drug_ID"]], [i["Drug_Name"]], [[i["sugeno_score"], i["Sugeno_Conf_Clin"],i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]]]
elif i["Drug_ID"] not in atc_groups["OTHER"][1]:
atc_groups["OTHER"][0] += 1
atc_groups["OTHER"][1].extend([i["Drug_ID"]])
atc_groups["OTHER"][2].extend([i["Drug_Name"]])
atc_groups["OTHER"][3].extend([[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]])
elif '[' in i['ATC_Code_3rd_Level']:
check_rows.append(i)
else:
key = i['ATC_Code_3rd_Level']
if key not in atc_groups.keys():
atc_groups[key] = [1, [i["Drug_ID"]], [i["Drug_Name"]], [[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]], i["ATC_Code_3rd_Level_Names"]]
elif i["Drug_ID"] not in atc_groups[key][1]:
atc_groups[key][0] += 1
atc_groups[key][1].extend([i["Drug_ID"]])
atc_groups[key][2].extend([i["Drug_Name"]])
atc_groups[key][3].extend([[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]])
for idi, i in enumerate(check_rows):
atc_codes_strings = i[4].rstrip(']').lstrip('[').split("', '")
atc_codes_strings = [j.lstrip("'").rstrip("'") for j in atc_codes_strings]
atc_codes_strings = list(OrderedDict.fromkeys(atc_codes_strings))
df_atc = pd.read_csv('ATC level 3.csv')
atc_codes_strings_names = []
for p in atc_codes_strings:
atc_codes_strings_names.append(df_atc.loc[df_atc['level3'] == p]['level3_description'].values[0])
# atc_codes_counts = [atc_groups[w][0] if w in atc_groups.keys() else 0 for w in atc_codes_strings]
# max_count, max_count_idx = max(atc_codes_counts), atc_codes_counts.index(max(atc_codes_counts))
#
# if max_count == 0:
# atc_groups[atc_codes_strings[max_count_idx]] = [1, [i[1]], [i[2]], [[i[6], i[7], i[8], i[9]]],
# atc_codes_strings_names[max_count_idx]]
# else:
# atc_groups[atc_codes_strings[max_count_idx]][0] += 1
# atc_groups[atc_codes_strings[max_count_idx]][1].extend([i[1]])
# atc_groups[atc_codes_strings[max_count_idx]][2].extend([i[2]])
# atc_groups[atc_codes_strings[max_count_idx]][3].extend([[i[6], i[7], i[8], i[9]]])
for idk, key in enumerate(atc_codes_strings):
if key not in atc_groups.keys():
# print(i)
# print(key, idk)
# print(atc_codes_strings)
# print(atc_codes_strings_names)
# print("\n")
atc_groups[key] = [1, [i[1]], [i[2]],[[i[6], i[13], i[7], i[8], i[9]]], atc_codes_strings_names[idk]]
elif i["Drug_ID"] not in atc_groups[key][1]:
atc_groups[key][0] += 1
atc_groups[key][1].extend([i[1]])
atc_groups[key][2].extend([i[2]])
atc_groups[key][3].extend([[i[6], i[13], i[7], i[8], i[9]]])
group_entries = []
for idc, c in enumerate(atc_groups.keys()):
scores = atc_groups[c][3]
if c=="OTHER":
atc_groups_name = "ATC_GROUP_NOT_FOUND"
else:
atc_groups_name = atc_groups[c][4]
max_score = [0,0,0,0]
max_score_drug_id = ""
max_score_drug_name = ""
for idd, d in enumerate(scores):
if (d[0]>max_score[0]) or (d[0]==max_score[0] and d[1]>max_score[1]) or (d[0]==max_score[0] and d[1]==max_score[1] and d[2]>max_score[2]) or (d[0]==max_score[0] and d[1]==max_score[1] and d[2]==max_score[2] and d[3]>max_score[3]):
max_score = d
max_score_drug_id = atc_groups[c][1][idd]
max_score_drug_name = atc_groups[c][2][idd]
for idd, d in enumerate(scores):
if idd == 0:
row = {
"ATC_CODE_THIRD_LEVEL": c,
"ATC_COD_THIRD_LEVEL_NAME": atc_groups_name,
"DRUG ID": atc_groups[c][1][idd],
"DRUG NAME": atc_groups[c][2][idd],
"DRUG SCORES": d,
"DRUG ID WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_id,
"DRUG NAME WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_name,
"HIGHEST SCORES FOR THIS CODE (WITHIN THE RESULTS) (ORDER: SUGENO, CONFIDENCE, NOVELTY, CLINICAL": max_score
}
else:
row = {
"ATC_CODE_THIRD_LEVEL": "",
"ATC_COD_THIRD_LEVEL_NAME": "",
"DRUG ID": atc_groups[c][1][idd],
"DRUG NAME": atc_groups[c][2][idd],
"DRUG SCORES": d,
"DRUG ID WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": "",
"DRUG NAME WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": "",
"HIGHEST SCORES FOR THIS CODE (WITHIN THE RESULTS) (ORDER: SUGENO, CONFIDENCE, NOVELTY, CLINICAL": ""
}
group_entries.append(row)
df_group = pd.DataFrame(group_entries)
df_group.to_csv(f"{filename}_Grouped_ATC_3rd.csv", index=False)
def atc_grouping_second(lookup_file, inferred_file, filename, choice="all"):
if choice == "all":
if lookup_file == "N/A" and inferred_file != "N/A":
df = pd.read_csv(inferred_file)
elif inferred_file == "N/A" and lookup_file != "N/A":
df = pd.read_csv(lookup_file)
elif lookup_file == "N/A" and inferred_file == "N/A":
print("Both Files empty")
return
else:
df_lookup = pd.read_csv(lookup_file)
df_inferred = pd.read_csv(inferred_file)
frames = [df_lookup, df_inferred]
df = pd.concat(frames)
elif choice == "lookup":
df = pd.read_csv(lookup_file)
elif choice == "inferred":
df = pd.read_csv(inferred_file)
df.to_csv(filename+'_Grouped.csv', index=False)
atc_groups = {}
# df = df.iloc[:, 1:]
check_rows= []
for idi, i in df.iterrows():
if isinstance(i['ATC_Code_2nd_Level'], float):
if "OTHER" not in atc_groups.keys():
atc_groups["OTHER"] = [1, [i["Drug_ID"]], [i["Drug_Name"]], [[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]]]
elif i["Drug_ID"] not in atc_groups["OTHER"][1]:
atc_groups["OTHER"][0] += 1
atc_groups["OTHER"][1].extend([i["Drug_ID"]])
atc_groups["OTHER"][2].extend([i["Drug_Name"]])
atc_groups["OTHER"][3].extend([[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]])
elif '[' in i['ATC_Code_2nd_Level']:
check_rows.append(i)
else:
key = i['ATC_Code_2nd_Level']
if key not in atc_groups.keys():
atc_groups[key] = [1, [i["Drug_ID"]], [i["Drug_Name"]], [[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]], i["ATC_Code_3rd_Level_Names"]]
elif i["Drug_ID"] not in atc_groups[key][1]:
atc_groups[key][0] += 1
atc_groups[key][1].extend([i["Drug_ID"]])
atc_groups[key][2].extend([i["Drug_Name"]])
atc_groups[key][3].extend([[i["sugeno_score"], i["Sugeno_Conf_Clin"], i["comp_confidence_score"], i["comp_novelty_score"], i["comp_clinical_evidence_score"]]])
for idi, i in enumerate(check_rows):
atc_codes_strings = i[10].rstrip(']').lstrip('[').split("', '")
atc_codes_strings = [j.lstrip("'").rstrip("'") for j in atc_codes_strings]
atc_codes_strings = list(OrderedDict.fromkeys(atc_codes_strings))
atc_codes_strings_names = i[11].rstrip(']').lstrip('[').split("', '")
atc_codes_strings_names = [j.lstrip("'").rstrip("'") for j in atc_codes_strings_names]
atc_codes_strings_names = list(OrderedDict.fromkeys(atc_codes_strings_names))
atc_codes_counts = [atc_groups[w][0] if w in atc_groups.keys() else 0 for w in atc_codes_strings]
max_count, max_count_idx = max(atc_codes_counts), atc_codes_counts.index(max(atc_codes_counts))
if max_count==0:
atc_groups[atc_codes_strings[max_count_idx]] = [1, [i[1]], [i[2]], [[i[6], i[14], i[7], i[8], i[9]]], atc_codes_strings_names[max_count_idx]]
else:
atc_groups[atc_codes_strings[max_count_idx]][0] += 1
atc_groups[atc_codes_strings[max_count_idx]][1].extend([i[1]])
atc_groups[atc_codes_strings[max_count_idx]][2].extend([i[2]])
atc_groups[atc_codes_strings[max_count_idx]][3].extend([[i[6], i[14], i[7], i[8], i[9]]])
# for idk, key in enumerate(atc_codes_strings):
# if key not in atc_groups.keys():
# # print(i)
# # print(key, idk)
# # print(atc_codes_strings)
# # print(atc_codes_strings_names)
# # print("\n")
# atc_groups[key] = [1, [i[3]], [i[4]],[[i[8], i[9], i[10], i[11]]], atc_codes_strings_names[idk]]
#
# elif i["Drug_ID"] not in atc_groups[key][1]:
# atc_groups[key][0] += 1
# atc_groups[key][1].extend([i[3]])
# atc_groups[key][2].extend([i[4]])
# atc_groups[key][3].extend([[i[8], i[9], i[10], i[11]]])
group_entries = []
group_entries_new = []
for c in atc_groups.keys():
scores = atc_groups[c][3]
if c=="OTHER":
atc_groups_name = "ATC_GROUP_NOT_FOUND"
else:
atc_groups_name = atc_groups[c][4]
max_score = [0,0,0,0,0]
max_score_drug_id = ""
max_score_drug_name = ""
for idd, d in enumerate(scores):
if (d[1]>max_score[1]) or (d[1]==max_score[1] and d[2]>max_score[2]) or (d[1]==max_score[1] and d[2]==max_score[2] and d[4]>max_score[4]):
max_score = d
max_score_drug_id = atc_groups[c][1][idd]
max_score_drug_name = atc_groups[c][2][idd]
row_new = {
"ATC_CODE_SECOND_LEVEL": c,
"ATC_COD_SECOND_LEVEL_NAME": atc_groups_name,
"DRUG ID WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_id,
"DRUG NAME WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_name,
"ORDERING SCORE (ONLY CLINICAL AND CONFIDENCE)": max_score[1],
"CONFIDENCE SCORE": max_score[2],
"CLINICAL SCORE": max_score[4]
}
group_entries_new.append(row_new)
for idd, d in enumerate(scores):
if idd == 0:
row = {
"ATC_CODE_SECOND_LEVEL": c,
"ATC_COD_SECOND_LEVEL_NAME": atc_groups_name,
"DRUG ID": atc_groups[c][1][idd],
"DRUG NAME": atc_groups[c][2][idd],
"DRUG ORDERING SCORE (ONLY CLINICAL AND CONFIDENCE)": d[1],
"DRUG CONFIDENCE SCORE": d[2],
"DRUG CLINICAL SCORE": d[4],
"DRUG ID WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_id,
"DRUG NAME WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": max_score_drug_name,
"ORDERING SCORE (ONLY CLINICAL AND CONFIDENCE) FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": max_score[1],
"CONFIDENCE SCORE FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": max_score[2],
"CLINICAL SCORE FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": max_score[4]
}
else:
row = {
"ATC_CODE_SECOND_LEVEL": "",
"ATC_COD_SECOND_LEVEL_NAME": "",
"DRUG ID": atc_groups[c][1][idd],
"DRUG NAME": atc_groups[c][2][idd],
"DRUG ORDERING SCORE (ONLY CLINICAL AND CONFIDENCE)": d[1],
"DRUG CONFIDENCE SCORE": d[2],
"DRUG CLINICAL SCORE": d[4],
"DRUG ID WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": "",
"DRUG NAME WITH HIGHEST SCORE FOR THIS CODE (WITHIN THE RESULTS)": "",
"ORDERING SCORE (ONLY CLINICAL AND CONFIDENCE) FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": "",
"CONFIDENCE SCORE FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": "",
"CLINICAL SCORE FOR DRUG WITH HIGHEST SCORE FOR THIS CODE": ""
}
group_entries.append(row)
df_group = pd.DataFrame(group_entries)
df_group_new = pd.DataFrame(group_entries_new)
df_group.to_csv(f"{filename}_Grouped_ATC_2nd.csv", index=False)
df_group_new.to_csv(f"{filename}_ATC_2nd_Only.csv", index=False)