-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10_4_ald_compare_single_pg.py
479 lines (394 loc) · 15 KB
/
10_4_ald_compare_single_pg.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# ---
# 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]
# # Compare predictions between model and RSN
#
# - see differences in imputation for diverging cases
# - dumps top5
# %% tags=["hide-input"]
import logging
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import njab
import pandas as pd
import seaborn
import pimmslearn
import pimmslearn.analyzers
import pimmslearn.imputation
import pimmslearn.io.datasplits
logger = pimmslearn.logging.setup_nb_logger()
logging.getLogger('fontTools').setLevel(logging.WARNING)
plt.rcParams['figure.figsize'] = [4, 2.5] # [16.0, 7.0] , [4, 3]
pimmslearn.plotting.make_large_descriptors(7)
# catch passed parameters
args = None
args = dict(globals()).keys()
# %% [markdown]
# ## Parameters
# %% tags=["parameters"]
folder_experiment = 'runs/appl_ald_data/plasma/proteinGroups'
fn_clinical_data = "data/ALD_study/processed/ald_metadata_cli.csv"
make_plots = True # create histograms and swarmplots of diverging results
model_key = 'VAE'
sample_id_col = 'Sample ID'
target = 'kleiner'
cutoff_target: int = 2 # => for binarization target >= cutoff_target
out_folder = 'diff_analysis'
file_format = 'csv'
baseline = 'RSN' # default is RSN, but could be any other trained model
template_pred = 'pred_real_na_{}.csv' # fixed, do not change
ref_method_score = None # filepath to reference method score
# %% 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"]))
args.folder_scores = (args.folder_experiment
/ params["out_folder"]
/ params["target"]
/ 'scores'
)
args.update_from_dict(params)
args
# %% [markdown]
# Write outputs to excel
# %% tags=["hide-input"]
files_out = dict()
fname = args.out_folder / 'diff_analysis_compare_DA.xlsx'
writer = pd.ExcelWriter(fname)
files_out[fname.name] = fname.as_posix()
logger.info("Writing to excel file: %s", fname)
# %% [markdown]
# ## Load scores
# List dump of scores:
# %% tags=["hide-input"]
score_dumps = [fname for fname in Path(
args.folder_scores).iterdir() if fname.suffix == '.pkl']
score_dumps
# %% [markdown]
# Load scores from dumps:
# %% tags=["hide-input"]
scores = pd.concat([pd.read_pickle(fname) for fname in score_dumps], axis=1)
scores
# %% [markdown]
# If reference dump is provided, add it to the scores
# %% tags=["hide-input"]
if args.ref_method_score:
scores_reference = (pd
.read_pickle(args.ref_method_score)
.rename({'None': 'None (100%)'},
axis=1))
scores = scores.join(scores_reference)
logger.info(f'Added reference method scores from {args.ref_method_score}')
# %% [markdown]
# ### Load frequencies of observed features
# %% tags=["hide-input"]
fname = args.folder_experiment / 'freq_features_observed.csv'
freq_feat = pd.read_csv(fname, index_col=0)
freq_feat.columns = pd.MultiIndex.from_tuples([('data', 'frequency'),])
freq_feat
# %% [markdown]
# ### Assemble qvalues
# %% tags=["hide-input"]
qvalues = scores.loc[pd.IndexSlice[:, args.target],
pd.IndexSlice[:, 'qvalue']
].join(freq_feat
).set_index(
('data', 'frequency'), append=True)
qvalues.index.names = qvalues.index.names[:-1] + ['frequency']
fname = args.out_folder / 'qvalues_target.pkl'
files_out[fname.name] = fname.as_posix()
qvalues.to_pickle(fname)
qvalues.to_excel(writer, sheet_name='qvalues_all')
qvalues
# %% [markdown]
# ### Assemble pvalues
# %% tags=["hide-input"]
pvalues = scores.loc[pd.IndexSlice[:, args.target],
pd.IndexSlice[:, 'p-unc']
].join(freq_feat
).set_index(
('data', 'frequency'), append=True)
pvalues.index.names = pvalues.index.names[:-1] + ['frequency']
fname = args.out_folder / 'pvalues_target.pkl'
files_out[fname.name] = fname.as_posix()
pvalues.to_pickle(fname)
pvalues.to_excel(writer, sheet_name='pvalues_all')
pvalues
# %% [markdown]
# ### Assemble rejected features
# %% tags=["hide-input"]
da_target = scores.loc[pd.IndexSlice[:, args.target],
pd.IndexSlice[:, 'rejected']
].join(freq_feat
).set_index(
('data', 'frequency'), append=True)
da_target.index.names = da_target.index.names[:-1] + ['frequency']
fname = args.out_folder / 'equality_rejected_target.pkl'
files_out[fname.name] = fname.as_posix()
da_target.to_pickle(fname)
count_rejected = njab.pandas.combine_value_counts(da_target.droplevel(-1, axis=1))
count_rejected.to_excel(writer, sheet_name='count_rejected')
count_rejected
# %% [markdown]
# ### Tabulate rejected decisions by method:
# %% tags=["hide-input"]
# # ! This uses implicitly that RSN is not available for some protein groups
# # ! Make an explicit list of the 313 protein groups available in original data
mask_common = da_target.notna().all(axis=1)
count_rejected_common = njab.pandas.combine_value_counts(da_target.loc[mask_common].droplevel(-1, axis=1))
count_rejected_common.to_excel(writer, sheet_name='count_rejected_common')
count_rejected_common
# %% [markdown]
# ### Tabulate rejected decisions by method for newly included features (if available)
# %% tags=["hide-input"]
count_rejected_new = njab.pandas.combine_value_counts(da_target.loc[~mask_common].droplevel(-1, axis=1))
count_rejected_new.to_excel(writer, sheet_name='count_rejected_new')
count_rejected_new
# %% [markdown]
# ### Tabulate rejected decisions by method for all features
# %% tags=["hide-input"]
da_target.to_excel(writer, sheet_name='equality_rejected_all')
logger.info("Written to sheet 'equality_rejected_all' in excel file.")
da_target
# %% [markdown]
# Tabulate number of equal decison by method (`True`) to the ones with varying
# decision depending on the method (`False`)
# %% tags=["hide-input"]
da_target_same = (da_target.sum(axis=1) == 0) | da_target.all(axis=1)
da_target_same.value_counts()
# %% [markdown]
# List frequency of features with varying decisions
# %% tags=["hide-input"]
feat_idx_w_diff = da_target_same[~da_target_same].index
feat_idx_w_diff.to_frame()[['frequency']].reset_index(-1, drop=True)
# %% [markdown]
# take only those with different decisions
# %% tags=["hide-input"]
(qvalues
.loc[feat_idx_w_diff]
.sort_values(('None', 'qvalue'))
.to_excel(writer, sheet_name='qvalues_diff')
)
(qvalues
.loc[feat_idx_w_diff]
.loc[mask_common] # mask automatically aligned
.sort_values(('None', 'qvalue'))
.to_excel(writer, sheet_name='qvalues_diff_common')
)
try:
(qvalues
.loc[feat_idx_w_diff]
.loc[~mask_common]
.sort_values(('None', 'qvalue'))
.to_excel(writer, sheet_name='qvalues_diff_new')
)
except IndexError:
print("No new features or no new ones (with diverging decisions.)")
writer.close()
# %% [markdown]
# ## Plots for inspecting imputations (for diverging decisions)
# %% tags=["hide-input"]
if not args.make_plots:
logger.warning("Not plots requested.")
import sys
sys.exit(0)
# %% [markdown]
# ## Load target
# %% tags=["hide-input"]
target = pd.read_csv(args.fn_clinical_data,
index_col=0,
usecols=[args.sample_id_col, args.target])
target = target.dropna()
target
# %% tags=["hide-input"]
target_to_group = target.copy()
target = target >= args.cutoff_target
target = target.replace({False: f'{args.target} < {args.cutoff_target}',
True: f'{args.target} >= {args.cutoff_target}'}
).astype('category')
pd.crosstab(target.squeeze(), target_to_group.squeeze())
# %% [markdown]
# ## Measurments
# %% 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]).unstack()
data
# %% [markdown]
# plot all of the new pgs which are at least once significant which are not already dumped.
# %% tags=["hide-input"]
feat_new_abundant = da_target.loc[~mask_common].any(axis=1)
feat_new_abundant = feat_new_abundant.loc[feat_new_abundant].index.get_level_values(0)
feat_new_abundant
# %% tags=["hide-input"]
feat_sel = feat_idx_w_diff.get_level_values(0)
feat_sel = feat_sel.union(feat_new_abundant)
len(feat_sel)
# %% tags=["hide-input"]
data = data.loc[:, feat_sel]
data
# %% [markdown]
# - RSN prediction are based on all samples mean and std (N=455) as in original study
# - VAE also trained on all samples (self supervised)
# One could also reduce the selected data to only the samples with a valid target marker,
# but this was not done in the original study which considered several different target markers.
#
# RSN : shifted per sample, not per feature!
#
# Load all prediction files and reshape
# %% tags=["hide-input"]
# exclude 'None' as this is without imputation (-> data)
model_keys = [k for k in qvalues.columns.get_level_values(0) if k != 'None']
pred_paths = [
args.out_preds / args.template_pred.format(method)
for method in model_keys]
pred_paths
# %% tags=["hide-input"]
load_single_csv_pred_file = pimmslearn.analyzers.compare_predictions.load_single_csv_pred_file
pred_real_na = dict()
for method in model_keys:
fname = args.out_preds / args.template_pred.format(method)
print(f"missing values pred. by {method}: {fname}")
pred_real_na[method] = load_single_csv_pred_file(fname)
pred_real_na = pd.DataFrame(pred_real_na)
pred_real_na
# %% [markdown]
# Once imputation, reduce to target samples only (samples with target score)
# %% tags=["hide-input"]
# select samples with target information
data = data.loc[target.index]
pred_real_na = pred_real_na.loc[target.index]
# assert len(data) == len(pred_real_na)
# %% tags=["hide-input"]
idx = feat_sel[0]
# %% tags=["hide-input"]
feat_observed = data[idx].dropna()
feat_observed
# %% tags=["hide-input"]
# axes = axes.ravel()
# args.out_folder.parent / 'intensity_plots'
# each feature -> one plot?
# plot all which are at least for one method significant?
folder = args.out_folder / 'intensities_for_diff_in_DA_decision'
folder.mkdir(parents=True, exist_ok=True)
# %% tags=["hide-input"]
min_y_int, max_y_int = pimmslearn.plotting.data.get_min_max_iterable(
[data.stack(), pred_real_na.stack()])
min_max = min_y_int, max_y_int
target_name = target.columns[0]
min_max, target_name
# %% [markdown]
# ## Compare with target annotation
# %% tags=["hide-input"]
# labels somehow?
# target.replace({True: f' >={args.cutoff_target}', False: f'<{args.cutoff_target}'})
for i, idx in enumerate(feat_sel):
print(f"Swarmplot {i:3<}: {idx}:")
fig, ax = plt.subplots()
# dummy plots, just to get the Path objects
tmp_dot = ax.scatter([1, 2], [3, 4], marker='X')
new_mk, = tmp_dot.get_paths()
tmp_dot.remove()
feat_observed = data[idx].dropna()
def get_centered_label(method, n, q):
model_str = f'{method}'
stats_str = f'(N={n:,d}, q={q:.3f})'
if len(model_str) > len(stats_str):
stats_str = f"{stats_str:<{len(model_str)}}"
else:
model_str = f"{model_str:<{len(stats_str)}}"
return f'{model_str}\n{stats_str}'
key = get_centered_label(method='observed',
n=len(feat_observed),
q=float(qvalues.loc[idx, ('None', 'qvalue')])
)
to_plot = {key: feat_observed}
for method in model_keys:
try:
pred = pred_real_na.loc[pd.IndexSlice[:,
idx], method].dropna().droplevel(-1)
if len(pred) == 0:
# in case no values was imputed -> qvalue is as based on measured
key = get_centered_label(method=method,
n=len(pred),
q=float(qvalues.loc[idx, ('None', 'qvalue')]
))
elif qvalues.loc[idx, (method, 'qvalue')].notna().all():
key = get_centered_label(method=method,
n=len(pred),
q=float(qvalues.loc[idx, (method, 'qvalue')]
))
elif qvalues.loc[idx, (method, 'qvalue')].isna().all():
logger.info(f"NA qvalues for {idx}: {method}")
continue
else:
raise ValueError("Unknown case.")
to_plot[key] = pred
except KeyError:
print(f"No missing values for {idx}: {method}")
continue
to_plot = pd.DataFrame.from_dict(to_plot)
to_plot.columns.name = 'group'
groups_order = to_plot.columns.to_list()
to_plot = to_plot.stack().to_frame('intensity').reset_index(-1)
to_plot = to_plot.join(target.astype('category'), how='inner')
to_plot = to_plot.astype({'group': 'category'})
ax = seaborn.swarmplot(data=to_plot,
x='group',
y='intensity',
order=groups_order,
dodge=True,
hue=args.target,
size=2,
ax=ax)
first_pg = idx.split(";")[0]
ax.set_title(
f'Imputation for protein group {first_pg} with target {target_name} (N= {len(data):,d} samples)')
_ = ax.set_ylim(min_y_int, max_y_int)
_ = ax.locator_params(axis='y', integer=True)
_ = ax.set_xlabel('')
_xticks = ax.get_xticks()
ax.xaxis.set_major_locator(
matplotlib.ticker.FixedLocator(_xticks)
)
_ = ax.set_xticklabels(ax.get_xticklabels(), rotation=45,
horizontalalignment='right')
N_hues = len(pd.unique(to_plot[args.target]))
_ = ax.collections[0].set_paths([new_mk])
_ = ax.collections[1].set_paths([new_mk])
label_target_0, label_target_1 = ax.collections[-2].get_label(), ax.collections[-1].get_label()
_ = ax.collections[-2].set_label(f'imputed, {label_target_0}')
_ = ax.collections[-1].set_label(f'imputed, {label_target_1}')
_obs_label0 = ax.scatter([], [], color='C0', marker='X', label=f'observed, {label_target_0}')
_obs_label1 = ax.scatter([], [], color='C1', marker='X', label=f'observed, {label_target_1}')
_ = ax.legend(
handles=[_obs_label0, _obs_label1, *ax.collections[-4:-2]],
fontsize=5, title_fontsize=5, markerscale=0.4,)
fname = (folder /
f'{first_pg}_swarmplot.pdf')
files_out[fname.name] = fname.as_posix()
pimmslearn.savefig(
fig,
name=fname)
plt.close()
# %% [markdown]
# Saved files:
# %% tags=["hide-input"]
files_out