|
a |
|
b/full pipeline for classification grade with MSI data.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 1, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import numpy as np\n", |
|
|
10 |
"#from pyimzml.ImzMLParser import ImzMLParser\n", |
|
|
11 |
"from tqdm import tqdm\n", |
|
|
12 |
"import os\n", |
|
|
13 |
"import pandas as pd\n", |
|
|
14 |
"import matplotlib.pyplot as plt\n", |
|
|
15 |
"from sklearn.neural_network import MLPClassifier\n", |
|
|
16 |
"from sklearn.model_selection import train_test_split \n", |
|
|
17 |
"from sklearn.ensemble import RandomForestClassifier, VotingClassifier\n", |
|
|
18 |
"from sklearn import preprocessing\n", |
|
|
19 |
"from sklearn.metrics import roc_curve, auc,classification_report\n", |
|
|
20 |
"from utils import print_confusion_matrix, assemble_dataset_supervised_learning\n", |
|
|
21 |
"from sklearn.utils import shuffle\n", |
|
|
22 |
"from sklearn.svm import SVC\n", |
|
|
23 |
"from itertools import product\n", |
|
|
24 |
"import xgboost as xgb\n", |
|
|
25 |
"from sklearn.model_selection import GridSearchCV\n", |
|
|
26 |
"from itertools import cycle\n", |
|
|
27 |
"from scipy import interp\n", |
|
|
28 |
"from sklearn.calibration import calibration_curve" |
|
|
29 |
] |
|
|
30 |
}, |
|
|
31 |
{ |
|
|
32 |
"cell_type": "markdown", |
|
|
33 |
"metadata": {}, |
|
|
34 |
"source": [ |
|
|
35 |
"## load data" |
|
|
36 |
] |
|
|
37 |
}, |
|
|
38 |
{ |
|
|
39 |
"cell_type": "code", |
|
|
40 |
"execution_count": null, |
|
|
41 |
"metadata": {}, |
|
|
42 |
"outputs": [], |
|
|
43 |
"source": [ |
|
|
44 |
"peaklist = np.array(pd.read_csv(r'.\\\\regions_peaklist_from_marta.txt', sep = \" \"))\n", |
|
|
45 |
"\n", |
|
|
46 |
"\n", |
|
|
47 |
"\n", |
|
|
48 |
"path_data = r'.\\msi_tables_filtered'\n", |
|
|
49 |
"list_dataset = os.listdir(path_data)\n", |
|
|
50 |
"\n", |
|
|
51 |
"##classification per tiles _ supervised \n", |
|
|
52 |
"\n", |
|
|
53 |
"labels = pd.read_csv('.\\labels_frozen.txt',sep = ';' ) #table with slide;label;unified_label;image_name\n", |
|
|
54 |
"\n", |
|
|
55 |
"full_dataset, y_labels = assemble_dataset_supervised_learning(labels,list_dataset,path_data, \"grade\")" |
|
|
56 |
] |
|
|
57 |
}, |
|
|
58 |
{ |
|
|
59 |
"cell_type": "markdown", |
|
|
60 |
"metadata": {}, |
|
|
61 |
"source": [ |
|
|
62 |
"## pre-process data per patient with box cox and 10**5 factor" |
|
|
63 |
] |
|
|
64 |
}, |
|
|
65 |
{ |
|
|
66 |
"cell_type": "code", |
|
|
67 |
"execution_count": null, |
|
|
68 |
"metadata": {}, |
|
|
69 |
"outputs": [], |
|
|
70 |
"source": [ |
|
|
71 |
"dict_X_gauss = {}\n", |
|
|
72 |
"\n", |
|
|
73 |
"pt = preprocessing.PowerTransformer(method='box-cox', standardize=False)\n", |
|
|
74 |
"name_images = full_dataset[full_dataset[\"dataset_name\"]==\"SlideA1\"][\"image_name\"]\n", |
|
|
75 |
"temp_patient_data = full_dataset[full_dataset[\"dataset_name\"]==\"SlideA1\"].drop(columns = ['dataset_name','image_name'])*10**5\n", |
|
|
76 |
"X_gaus = pt.fit_transform(temp_patient_data)\n", |
|
|
77 |
"\n", |
|
|
78 |
"columns = np.unique(full_dataset[\"dataset_name\"])\n", |
|
|
79 |
"\n", |
|
|
80 |
"for col in tqdm( columns[1:]):\n", |
|
|
81 |
" name_images = full_dataset[full_dataset[\"dataset_name\"]==col][\"image_name\"]\n", |
|
|
82 |
" temp_patient_data = full_dataset[full_dataset[\"dataset_name\"]==col].drop(columns = ['dataset_name','image_name'])*10**5\n", |
|
|
83 |
" array_trans = pt.fit_transform(temp_patient_data)\n", |
|
|
84 |
" X_gaus=np.concatenate((X_gaus,array_trans),axis =0)" |
|
|
85 |
] |
|
|
86 |
}, |
|
|
87 |
{ |
|
|
88 |
"cell_type": "code", |
|
|
89 |
"execution_count": null, |
|
|
90 |
"metadata": {}, |
|
|
91 |
"outputs": [], |
|
|
92 |
"source": [ |
|
|
93 |
"X_train, X_test_and_valid, y_train, y_test_and_valid, data_train, data_test_and_valid = train_test_split(X_gaus,y_labels , full_dataset[[\"dataset_name\",'image_name']],test_size = 0.30, random_state=10) " |
|
|
94 |
] |
|
|
95 |
}, |
|
|
96 |
{ |
|
|
97 |
"cell_type": "code", |
|
|
98 |
"execution_count": null, |
|
|
99 |
"metadata": {}, |
|
|
100 |
"outputs": [], |
|
|
101 |
"source": [ |
|
|
102 |
"#create test dataset\n", |
|
|
103 |
"len_half = len(y_test_and_valid)//2\n", |
|
|
104 |
"X_test = X_test_and_valid[:len_half]\n", |
|
|
105 |
"data_test = data_test_and_valid[:len_half]\n", |
|
|
106 |
"y_test = y_test_and_valid[:len_half]" |
|
|
107 |
] |
|
|
108 |
}, |
|
|
109 |
{ |
|
|
110 |
"cell_type": "code", |
|
|
111 |
"execution_count": null, |
|
|
112 |
"metadata": {}, |
|
|
113 |
"outputs": [], |
|
|
114 |
"source": [ |
|
|
115 |
"#create validation dataset\n", |
|
|
116 |
"X_valid = X_test_and_valid[len_half:]\n", |
|
|
117 |
"data_valid = data_test_and_valid[len_half:]\n", |
|
|
118 |
"y_valid = y_test_and_valid[len_half:]" |
|
|
119 |
] |
|
|
120 |
}, |
|
|
121 |
{ |
|
|
122 |
"cell_type": "markdown", |
|
|
123 |
"metadata": {}, |
|
|
124 |
"source": [ |
|
|
125 |
"## balancing training data" |
|
|
126 |
] |
|
|
127 |
}, |
|
|
128 |
{ |
|
|
129 |
"cell_type": "code", |
|
|
130 |
"execution_count": null, |
|
|
131 |
"metadata": {}, |
|
|
132 |
"outputs": [], |
|
|
133 |
"source": [ |
|
|
134 |
"max_len = X_train[y_train == 'high grade'].shape[0]\n", |
|
|
135 |
"len_h = X_train[y_train == 'non-dysplasia'].shape[0]\n", |
|
|
136 |
"len_lg = X_train[y_train == 'low grade'].shape[0]\n", |
|
|
137 |
"\n", |
|
|
138 |
"balanced_X_train = np.concatenate((X_train[y_train == 'non-dysplasia'][np.random.randint(0,len_h,max_len)], X_train[y_train == 'low grade'][np.random.randint(0,len_lg,max_len)],X_train[y_train == 'high grade']))\n", |
|
|
139 |
"balanced_y_train = np.array(['non-dysplasia']*max_len + ['low grade']*max_len + ['high grade']*X_train[y_train == 'highgrade'].shape[0])\n", |
|
|
140 |
"balanced_X_train,balanced_y_train = shuffle(balanced_X_train,balanced_y_train)" |
|
|
141 |
] |
|
|
142 |
}, |
|
|
143 |
{ |
|
|
144 |
"cell_type": "markdown", |
|
|
145 |
"metadata": {}, |
|
|
146 |
"source": [ |
|
|
147 |
"## grid search for MLP" |
|
|
148 |
] |
|
|
149 |
}, |
|
|
150 |
{ |
|
|
151 |
"cell_type": "code", |
|
|
152 |
"execution_count": null, |
|
|
153 |
"metadata": {}, |
|
|
154 |
"outputs": [], |
|
|
155 |
"source": [ |
|
|
156 |
"parameters = { 'batch_size':[32,64,128,356], 'alpha': 10.0 ** -np.arange(1, 10), 'hidden_layer_sizes':list(product(np.arange(10,21,10),np.arange(10,21,10)))}\n", |
|
|
157 |
"mlp_model = GridSearchCV(MLPClassifier(solver='adam',max_iter = 1100), parameters, n_jobs=20, , cv= 5, verbose = 2)\n", |
|
|
158 |
"\n", |
|
|
159 |
"mlp_model.fit(balanced_X_train,balanced_y_train)" |
|
|
160 |
] |
|
|
161 |
}, |
|
|
162 |
{ |
|
|
163 |
"cell_type": "markdown", |
|
|
164 |
"metadata": {}, |
|
|
165 |
"source": [ |
|
|
166 |
"## gridsearchCV for random forest" |
|
|
167 |
] |
|
|
168 |
}, |
|
|
169 |
{ |
|
|
170 |
"cell_type": "code", |
|
|
171 |
"execution_count": null, |
|
|
172 |
"metadata": {}, |
|
|
173 |
"outputs": [], |
|
|
174 |
"source": [ |
|
|
175 |
"param_grid = { \n", |
|
|
176 |
" 'n_estimators': [100,200,500],\n", |
|
|
177 |
" 'max_depth' : [4,8,16],\n", |
|
|
178 |
" 'criterion' :['gini', 'entropy']\n", |
|
|
179 |
"}\n", |
|
|
180 |
"\n", |
|
|
181 |
"rf_model = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5, verbose=1,n_jobs=20)\n", |
|
|
182 |
"rf_model.fit(balanced_X_train,balanced_y_train)\n" |
|
|
183 |
] |
|
|
184 |
}, |
|
|
185 |
{ |
|
|
186 |
"cell_type": "markdown", |
|
|
187 |
"metadata": {}, |
|
|
188 |
"source": [ |
|
|
189 |
"## gridsearchCV for XGBoost" |
|
|
190 |
] |
|
|
191 |
}, |
|
|
192 |
{ |
|
|
193 |
"cell_type": "code", |
|
|
194 |
"execution_count": null, |
|
|
195 |
"metadata": {}, |
|
|
196 |
"outputs": [], |
|
|
197 |
"source": [ |
|
|
198 |
"params = {\n", |
|
|
199 |
" \"min_child_weight\":range(1,6,2),\n", |
|
|
200 |
" \"gamma\": uniform(0, 0.5),\n", |
|
|
201 |
" \"learning_rate\": uniform(0.03, 0.3),\n", |
|
|
202 |
" \"max_depth\": range(3,10,2), \n", |
|
|
203 |
" \"n_estimators\": randint(100, 150),\n", |
|
|
204 |
" \"subsample\": uniform(0.6, 0.4)\n", |
|
|
205 |
"}\n", |
|
|
206 |
"\n", |
|
|
207 |
"xgb_model = GridSearchCV(estimator = xgb.XGBClassifier(colsample_bytree=0.8,\n", |
|
|
208 |
" objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27), \n", |
|
|
209 |
" param_grid = params, scoring='roc_auc',n_jobs=12,iid=False, cv=5,verbose=1)\n", |
|
|
210 |
"\n", |
|
|
211 |
"xgb_model.fit(balanced_X_train,balanced_y_train)" |
|
|
212 |
] |
|
|
213 |
}, |
|
|
214 |
{ |
|
|
215 |
"cell_type": "markdown", |
|
|
216 |
"metadata": {}, |
|
|
217 |
"source": [ |
|
|
218 |
"## save feature importance" |
|
|
219 |
] |
|
|
220 |
}, |
|
|
221 |
{ |
|
|
222 |
"cell_type": "code", |
|
|
223 |
"execution_count": null, |
|
|
224 |
"metadata": {}, |
|
|
225 |
"outputs": [], |
|
|
226 |
"source": [ |
|
|
227 |
"results=pd.DataFrame()\n", |
|
|
228 |
"results['columns']=list(full_dataset.columns)[2:]\n", |
|
|
229 |
"results['importances_rf'] = CV_rfc.feature_importances_\n", |
|
|
230 |
"results['importances_xgboost'] = xgb1.feature_importances_\n", |
|
|
231 |
"results['importances_mean'] = np.mean([xgb1.feature_importances_,CV_rfc.feature_importances_],axis=0)\n", |
|
|
232 |
"results.sort_values(by='importances_mean',ascending=False,inplace=True)\n", |
|
|
233 |
"results.to_excel(r\".\\features_rf_xgboost_msi_grade.xlsx\",index=None)\n", |
|
|
234 |
"other_results= pd.read_excel(r\".\\features_rf_xgboost_msi_gland_vs_tissue.xlsx\")\n", |
|
|
235 |
"other_results.sort_values(by='importances_mean',ascending=False,inplace=True)" |
|
|
236 |
] |
|
|
237 |
}, |
|
|
238 |
{ |
|
|
239 |
"cell_type": "markdown", |
|
|
240 |
"metadata": {}, |
|
|
241 |
"source": [ |
|
|
242 |
"## ensemble all best model" |
|
|
243 |
] |
|
|
244 |
}, |
|
|
245 |
{ |
|
|
246 |
"cell_type": "code", |
|
|
247 |
"execution_count": null, |
|
|
248 |
"metadata": {}, |
|
|
249 |
"outputs": [], |
|
|
250 |
"source": [ |
|
|
251 |
"#ensemble all best model\n", |
|
|
252 |
"\n", |
|
|
253 |
"vc = VotingClassifier(estimators=[\n", |
|
|
254 |
" ('mlp', mlp_model.best_estimator_), ('rf', rf_model.best_estimator_), ('xgb', xgb_model.best_estimator_)],\n", |
|
|
255 |
" voting='soft',n_jobs=12)\n", |
|
|
256 |
"vc = vc.fit(balanced_X_train,balanced_y_train)" |
|
|
257 |
] |
|
|
258 |
} |
|
|
259 |
], |
|
|
260 |
"metadata": { |
|
|
261 |
"kernelspec": { |
|
|
262 |
"display_name": "Python 3", |
|
|
263 |
"language": "python", |
|
|
264 |
"name": "python3" |
|
|
265 |
}, |
|
|
266 |
"language_info": { |
|
|
267 |
"codemirror_mode": { |
|
|
268 |
"name": "ipython", |
|
|
269 |
"version": 3 |
|
|
270 |
}, |
|
|
271 |
"file_extension": ".py", |
|
|
272 |
"mimetype": "text/x-python", |
|
|
273 |
"name": "python", |
|
|
274 |
"nbconvert_exporter": "python", |
|
|
275 |
"pygments_lexer": "ipython3", |
|
|
276 |
"version": "3.7.3" |
|
|
277 |
} |
|
|
278 |
}, |
|
|
279 |
"nbformat": 4, |
|
|
280 |
"nbformat_minor": 2 |
|
|
281 |
} |