-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10_3_ald_ml_new_feat.py
447 lines (371 loc) · 14 KB
/
10_3_ald_ml_new_feat.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.2
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# # Fit logistic regression model
#
# - based on different imputation methods
# - baseline: reference
# - model: any other selected imputation method
# %% tags=["hide-input"]
import logging
from pathlib import Path
from typing import List
import matplotlib.pyplot as plt
import njab.sklearn
import pandas as pd
import sklearn
from njab.plotting.metrics import plot_split_auc, plot_split_prc
from njab.sklearn.types import Splits
import pimmslearn
import pimmslearn.analyzers
import pimmslearn.io.datasplits
plt.rcParams['figure.figsize'] = (2.5, 2.5)
plt.rcParams['lines.linewidth'] = 1
plt.rcParams['lines.markersize'] = 2
fontsize = 5
figsize = (2.5, 2.5)
pimmslearn.plotting.make_large_descriptors(fontsize)
logger = pimmslearn.logging.setup_nb_logger()
logging.getLogger('fontTools').setLevel(logging.ERROR)
def parse_roc(*res: List[njab.sklearn.types.Results]) -> pd.DataFrame:
ret = list()
for _r in res:
_roc = (pd.DataFrame(_r.test.roc,
index='fpr tpr cutoffs'.split()
)).loc[['fpr', 'tpr']]
_roc = _roc.T
_roc.columns = pd.MultiIndex.from_product([[_r.name], _roc.columns])
ret.append(_roc)
ret = pd.concat(ret, axis=1)
return ret
def parse_prc(*res: List[njab.sklearn.types.Results]) -> pd.DataFrame:
ret = list()
for _r in res:
_prc = pd.DataFrame(_r.test.prc,
index='precision recall cutoffs'.split()
).loc[['precision', 'recall']]
_prc = _prc.T.rename(columns={'recall': 'tpr'})
_prc.columns = pd.MultiIndex.from_product([[_r.name], _prc.columns])
ret.append(_prc)
ret = pd.concat(ret, axis=1)
return ret
# catch passed parameters
args = None
args = dict(globals()).keys()
# %% [markdown]
# ## Parameters
# Default and set parameters for the notebook.
# %% tags=["parameters"]
folder_data: str = '' # specify data directory if needed
fn_clinical_data = "data/ALD_study/processed/ald_metadata_cli.csv"
folder_experiment = "runs/appl_ald_data/plasma/proteinGroups"
model_key = 'VAE'
target = 'kleiner'
sample_id_col = 'Sample ID'
cutoff_target: int = 2 # => for binarization target >= cutoff_target
file_format = "csv"
out_folder = 'diff_analysis'
fn_qc_samples = '' # 'data/ALD_study/processed/qc_plasma_proteinGroups.pkl'
baseline = 'RSN' # default is RSN, as this was used in the original ALD Niu. et. al 2022
template_pred = 'pred_real_na_{}.csv' # fixed, do not change
# %% tags=["hide-input"]
params = pimmslearn.nb.get_params(args, globals=globals())
args = pimmslearn.nb.Config()
args.folder_experiment = Path(params["folder_experiment"])
args = pimmslearn.nb.add_default_paths(args,
out_root=(args.folder_experiment
/ params["out_folder"]
/ params["target"]
/ f"{params['baseline']}_vs_{params['model_key']}"))
args.update_from_dict(params)
files_out = dict()
args
# %% [markdown]
# ## Load data
#
# ### Load target
# %%
target = pd.read_csv(args.fn_clinical_data,
index_col=0,
usecols=[args.sample_id_col, args.target])
target = target.dropna()
target
# %% [markdown]
# ### MS proteomics or specified omics data
# Aggregated from data splits of the imputation workflow run before.
# %% tags=["hide-input"]
data = pimmslearn.io.datasplits.DataSplits.from_folder(
args.data, file_format=args.file_format)
data = pd.concat([data.train_X, data.val_y, data.test_y])
data.sample(5)
# %% [markdown]
# Get overlap between independent features and target
# %% [markdown]
# ### Select by ALD criteria
# Use parameters as specified in [ALD study](https://github.com/RasmussenLab/pimms/tree/main/project/data/ALD_study).
# %% tags=["hide-input"]
DATA_COMPLETENESS = 0.6
MIN_N_PROTEIN_GROUPS: int = 200
FRAC_PROTEIN_GROUPS: int = 0.622
CV_QC_SAMPLE: float = 0.4
ald_study, cutoffs = pimmslearn.analyzers.diff_analysis.select_raw_data(data.unstack(
), data_completeness=DATA_COMPLETENESS, frac_protein_groups=FRAC_PROTEIN_GROUPS)
if args.fn_qc_samples:
qc_samples = pd.read_pickle(args.fn_qc_samples)
qc_samples = qc_samples[ald_study.columns]
qc_cv_feat = qc_samples.std() / qc_samples.mean()
qc_cv_feat = qc_cv_feat.rename(qc_samples.columns.name)
fig, ax = plt.subplots(figsize=(4, 7))
ax = qc_cv_feat.plot.box(ax=ax)
ax.set_ylabel('Coefficient of Variation')
print((qc_cv_feat < CV_QC_SAMPLE).value_counts())
ald_study = ald_study[pimmslearn.analyzers.diff_analysis.select_feat(qc_samples)]
column_name_first_prot_to_pg = {
pg.split(';')[0]: pg for pg in data.unstack().columns}
ald_study = ald_study.rename(columns=column_name_first_prot_to_pg)
ald_study
# %% [markdown]
# Number of complete cases which can be used:
# %% tags=["hide-input"]
mask_has_target = data.index.levels[0].intersection(target.index)
assert not mask_has_target.empty, f"No data for target: {data.index.levels[0]} and {target.index}"
print(
f"Samples available both in proteomics data and for target: {len(mask_has_target)}")
target, data, ald_study = target.loc[mask_has_target], data.loc[mask_has_target], ald_study.loc[mask_has_target]
# %% [markdown]
# ### Load imputations from specified model
# %% tags=["hide-input"]
fname = args.out_preds / args.template_pred.format(args.model_key)
print(f"missing values pred. by {args.model_key}: {fname}")
load_single_csv_pred_file = pimmslearn.analyzers.compare_predictions.load_single_csv_pred_file
pred_real_na = load_single_csv_pred_file(fname).loc[mask_has_target]
pred_real_na.sample(3)
# %% [markdown]
# ### Load imputations from baseline model
# %% tags=["hide-input"]
fname = args.out_preds / args.template_pred.format(args.baseline)
pred_real_na_baseline = load_single_csv_pred_file(fname) # .loc[mask_has_target]
pred_real_na_baseline
# %% [markdown]
# ## Modeling setup
# General approach:
# - use one train, test split of the data
# - select best 10 features from training data `X_train`, `y_train` before binarization of target
# - dichotomize (binarize) data into to groups (zero and 1)
# - evaluate model on the test data `X_test`, `y_test`
#
# Repeat general approach for
# 1. all original ald data: all features justed in original ALD study
# 2. all model data: all features available my using the self supervised deep learning model
# 3. newly available feat only: the subset of features available from the
# self supervised deep learning model which were newly retained using the
# new approach
#
# All data:
# %% tags=["hide-input"]
X = pd.concat([data, pred_real_na]).unstack()
X
# %% [markdown]
# ### Subset of data by ALD criteria
# %% tags=["hide-input"]
# could be just observed, drop columns with missing values
ald_study = pd.concat(
[ald_study.stack(),
pred_real_na_baseline.loc[
# only select columns in selected in ald_study
pd.IndexSlice[:, pred_real_na.index.levels[-1].intersection(ald_study.columns)]
]
]
).unstack()
ald_study
# %% [markdown]
# Features which would not have been included using ALD criteria:
# %% tags=["hide-input"]
new_features = X.columns.difference(ald_study.columns)
new_features
# %% [markdown]
# Binarize targets, but also keep groups for stratification
# %% tags=["hide-input"]
target_to_group = target.copy()
target = target >= args.cutoff_target
pd.crosstab(target.squeeze(), target_to_group.squeeze())
# %% [markdown]
# ## Determine best number of parameters by cross validation procedure
#
# using subset of data by ALD criteria:
# %% tags=["hide-input"]
cv_feat_ald = njab.sklearn.find_n_best_features(X=ald_study, y=target, name=args.target,
groups=target_to_group)
cv_feat_ald = (cv_feat_ald
.drop('test_case', axis=1)
.groupby('n_features')
.agg(['mean', 'std']))
cv_feat_ald
# %% [markdown]
# Using all data:
# %% tags=["hide-input"]
cv_feat_all = njab.sklearn.find_n_best_features(X=X, y=target, name=args.target,
groups=target_to_group)
cv_feat_all = cv_feat_all.drop('test_case', axis=1).groupby('n_features').agg(['mean', 'std'])
cv_feat_all
# %% [markdown]
# Using only new features:
# %% tags=["hide-input"]
cv_feat_new = njab.sklearn.find_n_best_features(X=X.loc[:, new_features],
y=target, name=args.target,
groups=target_to_group)
cv_feat_new = cv_feat_new.drop('test_case', axis=1).groupby('n_features').agg(['mean', 'std'])
cv_feat_new
# %% [markdown]
# ### Best number of features by subset of the data:
# %% tags=["hide-input"]
n_feat_best = pd.DataFrame(
{'ald': cv_feat_ald.loc[:, pd.IndexSlice[:, 'mean']].idxmax(),
'all': cv_feat_all.loc[:, pd.IndexSlice[:, 'mean']].idxmax(),
'new': cv_feat_new.loc[:, pd.IndexSlice[:, 'mean']].idxmax()
}
).droplevel(-1)
n_feat_best
# %% [markdown]
# ## Train, test split
# Show number of cases in train and test data
# %% tags=["hide-input"]
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
X,
target,
test_size=.2,
stratify=target_to_group,
random_state=42)
idx_train = X_train.index
idx_test = X_test.index
njab.pandas.combine_value_counts(
pd.concat([y_train, y_test],
axis=1,
ignore_index=True,
).rename(columns={0: 'train', 1: 'test'})
)
# %% [markdown]
# ## Results
#
# - `run_model` returns dataclasses with the further needed results
# - add mrmr selection of data (select best number of features to use instead of fixing it)
#
# Save results for final model on entire data, new features and ALD study criteria selected data.
# %% tags=["hide-input"]
splits = Splits(X_train=X.loc[idx_train],
X_test=X.loc[idx_test],
y_train=y_train,
y_test=y_test)
results_model_full = njab.sklearn.run_model(
splits,
n_feat_to_select=n_feat_best.loc['test_roc_auc', 'all'])
results_model_full.name = f'{args.model_key} all'
fname = args.out_folder / f'results_{results_model_full.name}.pkl'
files_out[fname.name] = fname
pimmslearn.io.to_pickle(results_model_full, fname)
splits = Splits(X_train=X.loc[idx_train, new_features],
X_test=X.loc[idx_test, new_features],
y_train=y_train,
y_test=y_test)
results_model_new = njab.sklearn.run_model(
splits,
n_feat_to_select=n_feat_best.loc['test_roc_auc', 'new'])
results_model_new.name = f'{args.model_key} new'
fname = args.out_folder / f'results_{results_model_new.name}.pkl'
files_out[fname.name] = fname
pimmslearn.io.to_pickle(results_model_new, fname)
splits_ald = Splits(
X_train=ald_study.loc[idx_train],
X_test=ald_study.loc[idx_test],
y_train=y_train,
y_test=y_test)
results_ald_full = njab.sklearn.run_model(
splits_ald,
n_feat_to_select=n_feat_best.loc['test_roc_auc', 'ald'])
results_ald_full.name = 'ALD study all'
fname = args.out_folder / f'results_{results_ald_full.name}.pkl'
files_out[fname.name] = fname
pimmslearn.io.to_pickle(results_ald_full, fname)
# %% [markdown]
# ### ROC-AUC on test split
# %% tags=["hide-input"]
fig, ax = plt.subplots(1, 1, figsize=figsize)
plot_split_auc(results_ald_full.test, results_ald_full.name, ax)
plot_split_auc(results_model_full.test, results_model_full.name, ax)
plot_split_auc(results_model_new.test, results_model_new.name, ax)
fname = args.out_folder / 'auc_roc_curve.pdf'
files_out[fname.name] = fname
pimmslearn.savefig(fig, name=fname)
# %% [markdown]
# Data used to plot ROC:
# %% tags=["hide-input"]
res = [results_ald_full, results_model_full, results_model_new]
auc_roc_curve = parse_roc(*res)
auc_roc_curve.to_excel(fname.with_suffix('.xlsx'))
auc_roc_curve
# %% [markdown]
# ### Features selected for final models
# %% tags=["hide-input"]
selected_features = pd.DataFrame(
[results_ald_full.selected_features,
results_model_full.selected_features,
results_model_new.selected_features],
index=[
results_ald_full.name,
results_model_full.name,
results_model_new.name]
).T
selected_features.index.name = 'rank'
fname = args.out_folder / 'mrmr_feat_by_model.xlsx'
files_out[fname.name] = fname
selected_features.to_excel(fname)
selected_features
# %% [markdown]
# ### Precision-Recall plot on test data
# %% tags=["hide-input"]
fig, ax = plt.subplots(1, 1, figsize=figsize)
ax = plot_split_prc(results_ald_full.test, results_ald_full.name, ax)
ax = plot_split_prc(results_model_full.test, results_model_full.name, ax)
ax = plot_split_prc(results_model_new.test, results_model_new.name, ax)
fname = folder = args.out_folder / 'prec_recall_curve.pdf'
files_out[fname.name] = fname
pimmslearn.savefig(fig, name=fname)
# %% [markdown]
# Data used to plot PRC:
# %% tags=["hide-input"]
prec_recall_curve = parse_prc(*res)
prec_recall_curve.to_excel(fname.with_suffix('.xlsx'))
prec_recall_curve
# %% [markdown]
# ## Train data plots
# %% tags=["hide-input"]
fig, ax = plt.subplots(1, 1, figsize=figsize)
ax = plot_split_prc(results_ald_full.train, results_ald_full.name, ax)
ax = plot_split_prc(results_model_full.train, results_model_full.name, ax)
ax = plot_split_prc(results_model_new.train, results_model_new.name, ax)
fname = folder = args.out_folder / 'prec_recall_curve_train.pdf'
files_out[fname.name] = fname
pimmslearn.savefig(fig, name=fname)
# %% tags=["hide-input"]
fig, ax = plt.subplots(1, 1, figsize=figsize)
plot_split_auc(results_ald_full.train, results_ald_full.name, ax)
plot_split_auc(results_model_full.train, results_model_full.name, ax)
plot_split_auc(results_model_new.train, results_model_new.name, ax)
fname = folder = args.out_folder / 'auc_roc_curve_train.pdf'
files_out[fname.name] = fname
pimmslearn.savefig(fig, name=fname)
# %% [markdown]
# Output files:
# %% tags=["hide-input"]
files_out