-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_codes.py
354 lines (303 loc) · 12.7 KB
/
process_codes.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
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
"""
By: Peyman Ghasemi
Please cite as:
Ghasemi P, Lee J
Unsupervised Feature Selection to Identify Important ICD-10 and ATC Codes for Machine Learning: A Case Study on a Coronary
Artery Disease Patient Cohort
JMIR Med Inform 2024;0:e0
URL: https://medinform.jmir.org/2024/0/e0/
doi: 10.2196/52896
"""
import simple_icd_10 as icd
import numpy as np
import pandas as pd
import os
import re
from datetime import datetime
import constants as cons
import argparse
def process_and_list_all_icd_codes(data_path: str):
"""
This function reads the dataset and lists all the unique ICD codes in the dataset. It also finds the ancestors of
each code and saves them in a csv file. It also checks if the code is a Canadian ICD code or not.
If you are not using the Canadian ICD-10 codes, you can replace the file path in the constants.py file with your own
local ICD-10 code list or leave it empty if you only want to use the WHO ICD-10 codes.
:param data_path: path to the dataset (csv file containing the ICD codes in the columns for each sample)
:return: None (it saves the list of ICD codes in the processed_data folder)
"""
# create required folders
os.makedirs(cons.PROCESSED_DIR, exist_ok=True)
os.makedirs(cons.SELECTED_FEATURES_DIR, exist_ok=True)
# read the dataset
df = pd.read_csv(data_path, dtype=str)
canadian_icd_10 = pd.read_csv(cons.CANADIAN_ICD10_LIST_PATH)
CODE_COLS = [
cons.CODE_COL_PREFIX + str(i) for i in range(1, cons.MAX_NUM_CODE_COLS + 1)
]
disease_df = df[CODE_COLS]
unique_codes = np.unique(disease_df.to_numpy().astype(str))
all_codes_list = pd.DataFrame(
columns=["code", "parent", "rank", "chapter", "description", "is_canadian"]
)
def get_canadian_codes_ancestors(icd_code: str):
"""
This code removes the least valued digit from the ICD code until finds its parent in the main ICD codes
It is useful to detect Canadian ICD codes. In cases that the code is related to the morphology of neoplasm, it
handles it differently.
:param icd_code:
:return:
"""
# check if the code consists of just numbers
pattern = re.compile(r"^\d+$")
if bool(pattern.match(icd_code)) and (len(icd_code) > 3):
return ["NEOPLASM"]
# General Cases
# drop last digit and check general ICD-10
new_icd_code = icd_code[:-1]
if icd.is_valid_item(new_icd_code):
ancestors = icd.get_ancestors(new_icd_code)
ancestors.insert(0, new_icd_code)
else:
if len(new_icd_code) != 0:
ancestors = get_canadian_codes_ancestors(new_icd_code)
# ancestors.insert(0, new_icd_code)
else:
ancestors = []
return ancestors
# find ancestors of the unique code and add them to the code list
def add_to_codes_list(icd_code):
nonlocal all_codes_list
if icd.is_valid_item(icd_code):
ancestors = icd.get_ancestors(icd_code)
if not (icd_code in all_codes_list["code"].values):
code_data = {
"code": icd_code,
"parent": (
None if len(ancestors) == 0 else ancestors[0]
), # to make sure the list is not empty
"rank": len(ancestors),
"chapter": (
icd_code if len(ancestors) == 0 else ancestors[-1]
), # to make sure the list is not empty
"description": icd.get_description(icd_code),
"is_canadian": False,
}
all_codes_list = pd.concat(
[all_codes_list, pd.DataFrame([code_data])], ignore_index=True
)
# Now do the same thing for each ancestor
for ancestor in ancestors:
add_to_codes_list(ancestor)
else:
# check if the code is in canadian list
if icd_code in canadian_icd_10["DX_CD"].values:
ancestors = get_canadian_codes_ancestors(icd_code)
if not (icd_code in all_codes_list["code"].values):
code_data = {
"code": icd_code,
"parent": (
None if len(ancestors) == 0 else ancestors[0]
), # to make sure the list is not empty
"rank": len(ancestors),
"chapter": (
icd_code if len(ancestors) == 0 else ancestors[-1]
), # to make sure the list is not empty
"description": canadian_icd_10.loc[
canadian_icd_10["DX_CD"] == icd_code, "DX_DESC"
].values[0],
"is_canadian": True,
}
all_codes_list = pd.concat(
[all_codes_list, pd.DataFrame([code_data])], ignore_index=True
)
# Now do the same thing for each ancestor
for ancestor in ancestors:
add_to_codes_list(ancestor)
for code in unique_codes:
add_to_codes_list(code)
# print(all_codes_list)
all_codes_list.to_csv(
os.path.join(cons.PROCESSED_DIR, "all_codes_list.csv"), index=False
)
print("Listing the ICD Codes Done...")
def find_atc_ancestors(atc_code: str, atc_code_list: list[str]) -> list[str]:
"""
Find all ancestors of a given ATC code. It includes the code itself.
:param atc_code: the ATC code. Case sensitive.
:param atc_code_list: the list containing all ATC codes.
:return: a list of ancestors of the given ATC code (including the code itself).
"""
ancestors = []
code_lengths = [1, 3, 4, 5, 7]
if not (1 <= len(atc_code) <= 7):
raise Exception(f"Invalid ATC code: {atc_code}")
for i, length in enumerate(code_lengths):
if len(atc_code) == length:
if atc_code in atc_code_list:
ancestors.append(atc_code)
if length > 1:
ancestors.extend(
find_atc_ancestors(
atc_code[: code_lengths[i - 1]], atc_code_list
)
)
return ancestors
else:
raise Exception(f"Invalid ATC code: {atc_code}")
raise Exception(f"Invalid ATC code: {atc_code}")
def process_and_list_all_atc_codes(data_path: str):
"""
This function reads the dataset and lists all the unique ATC codes in the dataset. It also finds the ancestors of
each code and saves them in a csv file.
:param data_path: path to the dataset (csv file containing the ATC codes in the columns for each sample)
:return: None (it saves the list of ATC codes in the processed_data folder)
"""
# create required folders
os.makedirs(cons.PROCESSED_DIR, exist_ok=True)
os.makedirs(cons.SELECTED_FEATURES_DIR, exist_ok=True)
# read the dataset
df = pd.read_csv(data_path, dtype=str)
# read list of ATC codes
atc_df = pd.read_csv(cons.ATC_LIST_PATH, dtype=str)
CODE_COLS = [
cons.CODE_COL_PREFIX + str(i) for i in range(1, cons.MAX_NUM_CODE_COLS + 1)
]
drugs_df = df[CODE_COLS]
unique_codes = np.unique(drugs_df.to_numpy().astype(str))
# find all unique ATC codes in the dataset with different levels
all_atc_codes = []
parent_dic = {}
for code in unique_codes:
try:
ancestors = find_atc_ancestors(code, atc_df["atc_code"].to_list())
all_atc_codes.extend(ancestors)
if len(ancestors) > 1:
for i in range(len(ancestors) - 1):
parent_dic[ancestors[i]] = ancestors[i + 1]
except: # invalid code
print(f"Invalid ATC code: {code}")
pass
all_atc_codes = np.unique(all_atc_codes)
all_atc_codes = np.sort(all_atc_codes)
all_atc_parents = [
parent_dic[code] if code in parent_dic else "" for code in all_atc_codes
]
all_atc_chapters = [code[0] for code in all_atc_codes]
all_atc_descriptions = [
atc_df[atc_df["atc_code"] == code]["atc_name"].values[0]
for code in all_atc_codes
]
all_atc_rank = [
float(atc_df[atc_df["atc_code"] == code]["rank"].values[0])
for code in all_atc_codes
]
all_codes_list = pd.DataFrame(
{
"code": all_atc_codes,
"parent": all_atc_parents,
"rank": all_atc_rank,
"chapter": all_atc_chapters,
"description": all_atc_descriptions,
}
)
all_codes_list.to_csv(
os.path.join(cons.PROCESSED_DIR, "all_codes_list.csv"), index=False
)
print("Listing the ATC Codes Done...")
def get_hierarchical_structure(code: str, all_codes_df: pd.DataFrame):
"""
find the ancestors of a given code by looking to its parents
:param all_codes_df:
:param code:
:return: a list containing the ancestors (until it reaches the chapter name)
"""
hierarchy_list = []
while code != "":
hierarchy_list.append(code)
try:
code = all_codes_df.loc[all_codes_df["code"] == code, "parent"].values[0]
except:
break
return hierarchy_list
def one_hot_encode_icd_codes(
codes: list[str], one_hot_col_list: list[str], ancestor_dic: dict
):
"""
This function encodes the ICD/ATC codes in a one-hot format.
:param codes: a list of ICD/ATC codes
:param one_hot_col_list: a list of all ICD/ATC codes
:param ancestor_dic: a dictionary containing the ancestors of each ICD/ATC code
:return: a one-hot encoded row
"""
one_hot_row = pd.Series(0, index=one_hot_col_list)
codes_to_encode = []
for code in codes:
try:
hierarchy_list = ancestor_dic[code]
codes_to_encode = codes_to_encode + hierarchy_list
except KeyError as e:
print("Wrong Code - KeyError:", e)
continue
one_hot_row.loc[codes_to_encode] = 1
return one_hot_row
def apply_one_hot_encoding_on_dataset(data_path: str):
all_codes_df = pd.read_csv(
os.path.join(cons.PROCESSED_DIR, "all_codes_list.csv"), na_filter=False
)
one_hot_col_list = all_codes_df["code"].values
# generate a lookup table of the ancestors of each code (for optimization)
ancestor_dic = {}
for code in all_codes_df["code"].values:
ancestor_dic[code] = get_hierarchical_structure(code, all_codes_df)
print("Ancestors Lookup Table Created...")
# read the length of the dataset
with open(data_path) as f:
num_of_samples = sum(1 for line in f) - 1
# Start the process - Read the sorted dataset with chunks (as the one-hot-encoded df may be too large to fit in memory)
one_hot_encoded_data = []
DISEASE_CODE_COLS = [
cons.CODE_COL_PREFIX + str(i) for i in range(1, cons.MAX_NUM_CODE_COLS + 1)
]
print("Start Time =", datetime.now().strftime("%H:%M:%S"))
chunksize = 5000
df_iterator = pd.read_csv(data_path, chunksize=chunksize, dtype=str)
for i, df in enumerate(df_iterator):
df_icd = df[DISEASE_CODE_COLS]
one_hot_df = df_icd.apply(
lambda row: one_hot_encode_icd_codes(
row.dropna().tolist(), one_hot_col_list, ancestor_dic
),
axis=1,
)
# Append the chunk of one-hot encoded data to the list
one_hot_encoded_data.append(one_hot_df.values)
print(
f"End Time for chuck {i} - {(i + 1) * chunksize}/{num_of_samples} ({round(100*(i + 1) * chunksize / num_of_samples, 2)}%) :",
datetime.now().strftime("%H:%M:%S"),
)
# Concatenate the list of one-hot encoded dataframes
one_hot_encoded_data = np.concatenate(one_hot_encoded_data, axis=0)
# Save the one-hot encoded data
np.save(
os.path.join(cons.PROCESSED_DIR, "one_hot_encoded_data.npy"),
one_hot_encoded_data,
)
print("One-Hot-Encoding Done...")
def main(data_path: str, process_atc: bool):
if process_atc:
process_and_list_all_atc_codes(data_path)
else:
process_and_list_all_icd_codes(data_path)
apply_one_hot_encoding_on_dataset(data_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process ICD codes in the dataset")
parser.add_argument(
"data_path",
type=str,
help="path to the dataset (csv file containing the ICD codes in the columns for each sample)",
)
parser.add_argument(
"--atc", action="store_true", help="process ATC codes instead of ICD codes"
)
args = parser.parse_args()
main(args.data_path, args.atc)