-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10_5_comp_diff_analysis_repetitions.py
195 lines (160 loc) · 5.17 KB
/
10_5_comp_diff_analysis_repetitions.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
# ---
# 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
# ---
# %%
from pathlib import Path
import njab
import pandas as pd
import pimmslearn
# %%
pickled_qvalues = snakemake.input.qvalues
pickled_qvalues
# %%
files_out = dict()
folder_out = Path(snakemake.params.folder_experiment)
fname = folder_out / 'agg_differences_compared.xlsx'
writer = pd.ExcelWriter(fname)
fname
# %%
def _load_pickle(pfath, run: int):
df = pd.read_pickle(pfath)
df['run'] = f'run{run:02d}'
df = df.set_index('run', append=True)
return df
df_long_qvalues = pd.concat(
[_load_pickle(f, i) for i, f in enumerate(pickled_qvalues)]
)
df_long_qvalues
# %% [markdown]
# Q-values for features across runs
# %%
qvalue_stats = df_long_qvalues.groupby(level=0).agg(['mean', 'std'])
qvalue_stats.to_excel(writer,
sheet_name='all_qvalue_stats',
float_format='%3.5f')
qvalue_stats
# %%
decisions_da_target = snakemake.input.equality_rejected_target
decisions_da_target
# %%
da_counts = sum(pd.read_pickle(f) for f in decisions_da_target)
da_counts.to_excel(writer,
sheet_name='all_rejected_counts')
da_counts
# %% [markdown]
# Option: set custom qvalue threshold
# %%
qvalue_treshold = 0.05
da_counts = sum(pd.read_pickle(f) < qvalue_treshold for f in pickled_qvalues)
da_counts
# %%
da_target_same = (da_counts.sum(axis=1) == 0) | da_counts.all(axis=1)
da_target_same.value_counts()
# %%
idx_different = (da_target_same
[~da_target_same]
.index
.get_level_values(0)
)
# %%
da_counts = da_counts.loc[idx_different]
da_counts
# %% [markdown]
# Order by mean qvalue of non-imputed comparison
# %%
qvalue_stats = (qvalue_stats
.loc[idx_different]
.sort_values(('None', 'qvalue', 'mean'))
)
qvalue_stats
# %% [markdown]
# save more verbose index on scores, transfer to counts
# %%
da_counts = da_counts.loc[qvalue_stats.index]
# da_counts.to_excel(writer,
# sheet_name='different_rejected_counts')
qvalue_stats.index = da_counts.index
# qvalue_stats.to_excel(writer,
# sheet_name='different_qvalue_stats',
# float_format='%3.5f'
# )
# %%
da_counts = da_counts.droplevel(-1, axis=1)
da_counts
# %% [markdown]
# - case: feature omitted in original study
# - case: feature added: drop RSN as it does not make sense.
# (or assing None value -> that's what counts)
# %%
mask_pgs_included_in_ald_study = qvalue_stats[('RSN', 'qvalue', 'mean')].notna()
mask_pgs_included_in_ald_study
# %%
# pgs included in original ald study
tab_diff_rejec_counts_old = (da_counts
.loc[mask_pgs_included_in_ald_study]
.reset_index()
.groupby(
by=da_counts.columns.to_list())
.size()
.to_frame('N')
)
tab_diff_rejec_counts_old.to_excel(writer,
sheet_name='tab_diff_rejec_counts_old')
tab_diff_rejec_counts_old
# %%
da_counts.loc[mask_pgs_included_in_ald_study
].to_excel(writer,
sheet_name='diff_rejec_counts_old')
qvalue_stats.loc[mask_pgs_included_in_ald_study
].to_excel(writer,
sheet_name='diff_qvalue_stats_old',
float_format='%3.5f'
)
# %%
# new pgs
tab_diff_rejec_counts_new = (da_counts
.loc[~mask_pgs_included_in_ald_study]
.reset_index()
.drop('RSN', axis=1)
.groupby(
by=[m for m in da_counts.columns if m != 'RSN'])
.size()
.to_frame('N')
)
tab_diff_rejec_counts_new.to_excel(writer,
sheet_name='tab_diff_rejec_counts_new')
tab_diff_rejec_counts_new
# %%
da_counts.loc[~mask_pgs_included_in_ald_study
].to_excel(writer,
sheet_name='diff_rejec_counts_new')
qvalue_stats.loc[~mask_pgs_included_in_ald_study
].to_excel(writer,
sheet_name='diff_qvalue_stats_new',
float_format='%3.5f'
)
# %%
mask_new_da_with_imp = mask_new_da_with_imputation = ((~mask_pgs_included_in_ald_study)
& (da_counts['None'] != 10))
tab_new_da_with_imp = njab.pandas.combine_value_counts(
da_counts
.loc[mask_new_da_with_imputation]
).fillna(0).astype(int)
tab_new_da_with_imp.index.name = 'number of reps'
tab_new_da_with_imp.columns.name = 'DA decisions by method'
tab_new_da_with_imp.to_excel(writer, sheet_name='tab_new_da_with_imp')
tab_new_da_with_imp
# %%
writer.close()
fname
# %%