Repository navigation
Expand file tree
/
Copy pathIntegratedClassification_v2.py
More file actions
328 lines (268 loc) · 15.7 KB
/
Copy pathIntegratedClassification_v2.py
File metadata and controls
328 lines (268 loc) · 15.7 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
import numpy as np
import pandas as pd
import nltk
import os
import re
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from sklearn.preprocessing import MinMaxScaler
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import GridSearchCV, train_test_split, StratifiedKFold
from sklearn.metrics import accuracy_score, classification_report, roc_auc_score, recall_score, roc_curve, precision_recall_curve, auc
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, VotingClassifier
from xgboost import XGBClassifier
from transformers import BertModel, BertTokenizer
import matplotlib.pyplot as plt
from tqdm import tqdm
import warnings
from sklearn.decomposition import PCA
warnings.filterwarnings('ignore')
import joblib
joblib.parallel_backend('loky', inner_max_num_threads=1)
import matplotlib
import torch
matplotlib.use('Agg')
# NLTK setup
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
# Global parameters
patient_subset = 1 # 1 - All patients, 2 - Patients with Notes, 3 - Patients without Notes
feature_subset = 1 # 1 - Demo, 2 - Demo + Tabular, 3 - Demo + Tabular + Notes
feat_sel = 0 # 1 - Feature selection on (RandomForest), 0 = off
test_split_ratio = 0.2
summarized = 1 # 0 - No summarization, 1 - DeepSeek LLM based summarization
seed = 0
vectorize_text = 1 # 1 - TF-IDF, 2 - BERT
n_component = 50 # Number of components to retain from PCA
file_path = '../Data/Fewshot_new_final_summary.xlsx'
output_csv = 'training_log.csv'
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
min_max_scaler = MinMaxScaler()
###############################################################################
# Utility function: Append a single row (dict) to CSV, with an auto-incremented Run ID
###############################################################################
def append_results_to_csv(results_dict, csv_file):
# Check if the file exists
if os.path.exists(csv_file):
df_existing = pd.read_csv(csv_file)
if 'Run ID' in df_existing.columns:
max_run_id = df_existing['Run ID'].max()
else:
max_run_id = 0
new_run_id = max_run_id + 1
results_dict['Run ID'] = new_run_id
# Append the new row
df_new = pd.DataFrame([results_dict])
df_combined = pd.concat([df_existing, df_new], ignore_index=True)
df_combined.to_csv(csv_file, index=False)
else:
# If no file, create a new DataFrame
results_dict['Run ID'] = 1
df_new = pd.DataFrame([results_dict])
df_new.to_csv(csv_file, index=False)
###############################################################################
# Preprocessing function
###############################################################################
def preprocess(suhi_df):
# Drop columns
# suhi_df.drop(columns=['record_id', 'new_patient'], inplace=True, errors='ignore')
# Remove rows with NaN in day_readmit
suhi_df.dropna(subset=['day_readmit'], inplace=True)
# Convert day_readmit == 2 to 0
suhi_df.loc[suhi_df['day_readmit'] == 2, 'day_readmit'] = 0
# Convert day_readmit to int
suhi_df['day_readmit'] = suhi_df['day_readmit'].astype(int)
return suhi_df
# # include words that satisfy token_pattern=r'[a-zA-Z]{2,}'
def filter_tokens_in_notes(notes):
pattern = re.compile(r'[a-zA-Z]{2,}')
filtered_notes = []
for note in notes:
# Find all tokens that match the pattern
filtered_tokens = pattern.findall(note)
# Join tokens back to form the filtered note
filtered_notes.append(' '.join(filtered_tokens))
return filtered_notes
for patient_subset in tqdm(range(1,3)):
for feature_subset in tqdm(range(2, 4)):
for engaged in tqdm(range(0,2)):
for seed in tqdm(range(0,30,1)):
for summarized in tqdm(range(0,2)):
for vectorize_text in tqdm(range(1,3)):
try:
# Load data
suhi_df = pd.read_excel(file_path)
suhi_df = suhi_df[suhi_df['engaged']==engaged]
# If feature_subset == 1 -> only Demographic (first 13 columns)
if (feature_subset == 1 or feature_subset == 2) and summarized==1 and vectorize_text==2:
continue
# # If we only want patients with notes
if patient_subset == 2:
# suhi_df['COMBINED_NOTES'] = suhi_df['COMBINED_NOTES'].replace('', np.nan)
suhi_df.dropna(subset=['COMBINED_NOTES'], inplace=True)
if summarized == 1:
if feature_subset == 2:
continue
else:
suhi_df['FEW_SHORT_LLM_SUMMARY'] = suhi_df['FEW_SHORT_LLM_SUMMARY'].replace('nan', '')
suhi_df['COMBINED_NOTES'] = suhi_df['FEW_SHORT_LLM_SUMMARY']
if patient_subset == 3 and feature_subset == 3:
continue
print('Process Begins')
###############################################################################
# Main Script Execution
###############################################################################
# Preprocess
suhi_df = preprocess(suhi_df)
# If we include text features and vectorize them using TF-IDF
if summarized==0:
min_df=20
else:
min_df=10
if feature_subset == 3 and vectorize_text == 1:
tfidf_vectorizer = TfidfVectorizer(
min_df= min_df,
)
suhi_df['COMBINED_NOTES'].fillna('', inplace=True)
suhi_df['COMBINED_NOTES'] = filter_tokens_in_notes(suhi_df['COMBINED_NOTES'])
text_embeddings = tfidf_vectorizer.fit_transform(suhi_df['COMBINED_NOTES'])
# If we include text features and vectorize them using BERT embeddings
if patient_subset == 2 and feature_subset == 3 and vectorize_text == 2:
# Load pre-trained BERT model
model = BertModel.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
suhi_df['COMBINED_NOTES'] = filter_tokens_in_notes(suhi_df['COMBINED_NOTES'])
text_embeddings = []
# Tokenize and encode the text
for text in tqdm(suhi_df['COMBINED_NOTES'].tolist()):
tokens = tokenizer(text, padding='max_length', truncation=True, max_length=512, return_tensors='pt')
tokens = {key: value.to(device) for key, value in tokens.items()}
with torch.no_grad():
text_embedding = model(**tokens)
text_embeddings.append(text_embedding.pooler_output.cpu().squeeze().numpy())
text_embeddings = np.array(text_embeddings)
pca = PCA().fit(text_embeddings)
pca = PCA(n_components=n_component)
text_embeddings = pca.fit_transform(text_embeddings)
elif patient_subset == 1 and feature_subset == 3 and vectorize_text == 2:
continue
print('Dropping Text and Columns')
# Drop textual/object columns (except for combining them if we do text vectorizing)
text_columns = [col for col in suhi_df.columns if suhi_df[col].dtype == 'object']
suhi_df.drop(columns=text_columns, inplace=True, errors='ignore')
# Drop date columns
date_columns = [col for col in suhi_df.columns if suhi_df[col].dtype == 'datetime64[ns]']
suhi_df.drop(columns=date_columns, inplace=True, errors='ignore')
# Drop columns that contain 'nores'
nores_columns = [col for col in suhi_df.columns if 'nores' in col]
suhi_df.drop(columns=nores_columns, inplace=True, errors='ignore')
# If we have vectorized text, merge them in
if feature_subset == 3 and vectorize_text == 1:
COMBINED_NOTES_vectorized_df = pd.DataFrame(text_embeddings.toarray())
COMBINED_NOTES_vectorized_df.columns = tfidf_vectorizer.get_feature_names_out()
suhi_df.reset_index(drop=True, inplace=True)
suhi_w_vectors_df = pd.concat([suhi_df, COMBINED_NOTES_vectorized_df], axis=1)
elif feature_subset == 3 and vectorize_text == 2:
COMBINED_NOTES_vectorized_df = pd.DataFrame(text_embeddings)
suhi_df.reset_index(drop=True, inplace=True)
suhi_w_vectors_df = pd.concat([suhi_df, COMBINED_NOTES_vectorized_df], axis=1)
else:
suhi_w_vectors_df = suhi_df
suhi_w_vectors_df.columns = suhi_w_vectors_df.columns.astype(str)
# Fill NaN with 0
suhi_w_vectors_df.fillna(0, inplace=True)
print(100*'-')
print(patient_subset, feature_subset)
print(suhi_w_vectors_df.shape)
print(suhi_w_vectors_df.columns)
# Split data
X = suhi_w_vectors_df.drop('day_readmit', axis=1)
y = suhi_w_vectors_df['day_readmit']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_split_ratio, random_state=seed
)
###############################################################################
# We'll collect all our results in one dictionary, final_results,
# so we only write one row per script run.
###############################################################################
final_results = {}
final_results["Patient Subset"] = patient_subset
final_results["Feature Subset"] = feature_subset
final_results["Feature Selection"] = bool(feat_sel)
final_results["Engaged"] = engaged
final_results["Random Seed"] = seed
final_results["Summarized"] = summarized
final_results["Vectorization"] = vectorize_text
final_results["File Name"] = file_path
final_results["Shape"] = suhi_w_vectors_df.shape
final_results["Columns"] = suhi_w_vectors_df.columns
###############################################################################
# Best hyper-parameters from previous analysis
###############################################################################
BEST_PARAMS = {
"RandomForestClassifier": {
"n_estimators": 200,
"max_depth": 5,
"min_samples_split": 20,
"random_state": seed,
},
"AdaBoostClassifier": {
"n_estimators": 100,
"algorithm": "SAMME",
"learning_rate": 0.10,
"random_state": seed,
},
"XGBClassifier": {
"n_estimators": 10,
"max_depth": 5,
"learning_rate": 0.10,
"random_state": seed,
},
}
###############################################################################
# Instantiate, train, and evaluate each model
###############################################################################
models = {
"RandomForest": RandomForestClassifier(**BEST_PARAMS["RandomForestClassifier"]),
"AdaBoost": AdaBoostClassifier(**BEST_PARAMS["AdaBoostClassifier"]),
"XGBoost": XGBClassifier(**BEST_PARAMS["XGBClassifier"]),
}
results = {}
for clf_name, model in models.items():
# ---- train ----
model.fit(X_train, y_train)
# ---- predictions ----
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1]
best_params = model.get_params()
feature_importances = model.feature_importances_
final_results["Feature Importances"] = feature_importances
# Predict on test set
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
test_accuracy = round(accuracy_score(y_test, y_pred), 4)
test_roc_auc = round(roc_auc_score(y_test, y_pred_proba), 4)
y_pred_thresholded = (y_pred_proba >= 0.35).astype(int)
test_sensitivity = round(recall_score(y_test, y_pred_thresholded), 4)
test_specificity = round(recall_score(y_test, y_pred_thresholded, pos_label=0), 4)
clf_report = classification_report(y_test, y_pred_thresholded)
# Store results for this classifier
final_results[f"{clf_name}_Test_Accuracy"] = test_accuracy
final_results[f"{clf_name}_Test"] = test_roc_auc
final_results[f"{clf_name}_Sensitivity"] = test_sensitivity
final_results[f"{clf_name}_Specificity"] = test_specificity
# final_results[f"{clf_name}_Classification_Report"] = clf_report
###############################################################################
# Append exactly ONE row for this entire run
###############################################################################
append_results_to_csv(final_results, output_csv)
print(f"\nDone! Logged results to {output_csv} as a single row.\n")
except Exception as e:
print(f"Error occurred: {e}")
print(patient_subset, feature_subset, engaged, seed, feat_sel, summarized, vectorize_text)
pass