Skip to content

Commit ec2663f

Browse files
add self.stats to selector
1 parent 5d3bfee commit ec2663f

File tree

2 files changed

+77
-29
lines changed

2 files changed

+77
-29
lines changed

bvas/bvas_selector.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
from bvas.util import namespace_to_numpy
88

99

10+
def populate_alpha_beta_stats(container, stats):
11+
for s in ['h_alpha', 'h_beta', 'h']:
12+
if hasattr(container, s):
13+
stats['Mean ' + s] = getattr(container, s)
14+
15+
16+
def populate_weight_stats(selector, stats, weights, quantiles=[5.0, 10.0, 20.0, 50.0, 90.0, 95.0]):
17+
q5, q10, q20, q50, q90, q95 = np.percentile(weights, quantiles).tolist()
18+
s = "5/10/20/50/90/95: {:.2e} {:.2e} {:.2e} {:.2e} {:.2e} {:.2e}"
19+
stats['Weight quantiles'] = s.format(q5, q10, q20, q50, q90, q95)
20+
s = "mean/std/min/max: {:.2e} {:.2e} {:.2e} {:.2e}"
21+
stats['Weight moments'] = s.format(weights.mean().item(), weights.std().item(),
22+
weights.min().item(), weights.max().item())
23+
24+
1025
class BVASSelector(object):
1126
r"""
1227
Main analysis class for Bayesian Viral Allele Selection (BVAS).
@@ -89,6 +104,7 @@ def run(self, T, T_burnin, seed=None):
89104
pango = pd.Series(self.variant_names, name="Variant Name")
90105
self.growth_rates = pd.concat([growth_rate, growth_rate_std, pango], axis=1)
91106

92-
if hasattr(self.container, 'h'):
93-
if self.container.h.size == 1:
94-
print('[h_ratio] {:.4f}'.format(self.container.h.item()))
107+
self.stats = {}
108+
self.weights = np.array(self.container._weights)
109+
populate_alpha_beta_stats(self.container, self.stats)
110+
populate_weight_stats(self, self.stats, self.weights)

notebooks/basic_demo.ipynb

+58-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "6657ec71",
5+
"id": "e84d968e",
66
"metadata": {},
77
"source": [
88
"# Basic BVAS demo using simulated data"
@@ -11,7 +11,7 @@
1111
{
1212
"cell_type": "code",
1313
"execution_count": 1,
14-
"id": "a1456578",
14+
"id": "27b49fb2",
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "markdown",
27-
"id": "d9224796",
27+
"id": "fe5e4e14",
2828
"metadata": {},
2929
"source": [
3030
"### Simulate data"
@@ -33,7 +33,7 @@
3333
{
3434
"cell_type": "code",
3535
"execution_count": 2,
36-
"id": "0ec74519",
36+
"id": "750d0541",
3737
"metadata": {},
3838
"outputs": [],
3939
"source": [
@@ -51,7 +51,7 @@
5151
{
5252
"cell_type": "code",
5353
"execution_count": 3,
54-
"id": "935c020e",
54+
"id": "9f8274ff",
5555
"metadata": {},
5656
"outputs": [
5757
{
@@ -77,7 +77,7 @@
7777
},
7878
{
7979
"cell_type": "markdown",
80-
"id": "dfc65b90",
80+
"id": "d995ccbc",
8181
"metadata": {},
8282
"source": [
8383
"### Instantiate BVASSelector object"
@@ -86,7 +86,7 @@
8686
{
8787
"cell_type": "code",
8888
"execution_count": 4,
89-
"id": "79fd0337",
89+
"id": "ad539a7c",
9090
"metadata": {},
9191
"outputs": [],
9292
"source": [
@@ -103,7 +103,7 @@
103103
},
104104
{
105105
"cell_type": "markdown",
106-
"id": "3ad5765d",
106+
"id": "5da317dd",
107107
"metadata": {},
108108
"source": [
109109
"### Run BVAS MCMC-based inference"
@@ -112,13 +112,13 @@
112112
{
113113
"cell_type": "code",
114114
"execution_count": 5,
115-
"id": "51be4150",
115+
"id": "a11e7ef9",
116116
"metadata": {},
117117
"outputs": [
118118
{
119119
"data": {
120120
"application/vnd.jupyter.widget-view+json": {
121-
"model_id": "345f12e6501141989ec6d910ecb9c4c2",
121+
"model_id": "838905aacba843549b78082c99f55479",
122122
"version_major": 2,
123123
"version_minor": 0
124124
},
@@ -136,7 +136,7 @@
136136
},
137137
{
138138
"cell_type": "markdown",
139-
"id": "0c60cc7f",
139+
"id": "e5cbb4df",
140140
"metadata": {},
141141
"source": [
142142
"### Inspect results\n",
@@ -153,7 +153,7 @@
153153
{
154154
"cell_type": "code",
155155
"execution_count": 6,
156-
"id": "d1d58d65",
156+
"id": "5c26864a",
157157
"metadata": {},
158158
"outputs": [
159159
{
@@ -186,7 +186,7 @@
186186
{
187187
"cell_type": "code",
188188
"execution_count": 7,
189-
"id": "67d6406f",
189+
"id": "76c1bf93",
190190
"metadata": {},
191191
"outputs": [
192192
{
@@ -215,7 +215,7 @@
215215
{
216216
"cell_type": "code",
217217
"execution_count": 8,
218-
"id": "1814216c",
218+
"id": "d41b59d1",
219219
"metadata": {},
220220
"outputs": [],
221221
"source": [
@@ -225,7 +225,39 @@
225225
},
226226
{
227227
"cell_type": "markdown",
228-
"id": "d0939917",
228+
"id": "5a91ef89",
229+
"metadata": {},
230+
"source": [
231+
"### MCMC stats\n",
232+
"\n",
233+
"Additional MCMC stats are in the `selector.stats` dictionary."
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": 9,
239+
"id": "4902488d",
240+
"metadata": {},
241+
"outputs": [
242+
{
243+
"data": {
244+
"text/plain": [
245+
"{'Weight quantiles': '5/10/20/50/90/95: 1.84e-16 1.84e-16 2.71e+00 5.11e+00 1.17e+01 1.17e+01',\n",
246+
" 'Weight moments': 'mean/std/min/max: 6.19e+00 4.16e+00 1.84e-16 1.17e+01'}"
247+
]
248+
},
249+
"execution_count": 9,
250+
"metadata": {},
251+
"output_type": "execute_result"
252+
}
253+
],
254+
"source": [
255+
"selector.stats"
256+
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"id": "5ed607dd",
229261
"metadata": {},
230262
"source": [
231263
"# Compare to MAP inference\n",
@@ -235,8 +267,8 @@
235267
},
236268
{
237269
"cell_type": "code",
238-
"execution_count": 9,
239-
"id": "d3d22182",
270+
"execution_count": 10,
271+
"id": "e2c3a9c3",
240272
"metadata": {},
241273
"outputs": [],
242274
"source": [
@@ -245,8 +277,8 @@
245277
},
246278
{
247279
"cell_type": "code",
248-
"execution_count": 10,
249-
"id": "ff352512",
280+
"execution_count": 11,
281+
"id": "6759a828",
250282
"metadata": {},
251283
"outputs": [
252284
{
@@ -373,7 +405,7 @@
373405
"Spurious70 0.011479 15"
374406
]
375407
},
376-
"execution_count": 10,
408+
"execution_count": 11,
377409
"metadata": {},
378410
"output_type": "execute_result"
379411
}
@@ -385,16 +417,16 @@
385417
},
386418
{
387419
"cell_type": "markdown",
388-
"id": "40e3bd65",
420+
"id": "c1349f1a",
389421
"metadata": {},
390422
"source": [
391423
"# Compare to MAP inference with a Sparsity-inducing Laplace prior"
392424
]
393425
},
394426
{
395427
"cell_type": "code",
396-
"execution_count": 11,
397-
"id": "b6b6cecc",
428+
"execution_count": 12,
429+
"id": "06e2e0fb",
398430
"metadata": {},
399431
"outputs": [
400432
{
@@ -431,8 +463,8 @@
431463
},
432464
{
433465
"cell_type": "code",
434-
"execution_count": 12,
435-
"id": "63d92a7b",
466+
"execution_count": 13,
467+
"id": "178836fa",
436468
"metadata": {},
437469
"outputs": [
438470
{
@@ -559,7 +591,7 @@
559591
"Spurious88 0.002844 15"
560592
]
561593
},
562-
"execution_count": 12,
594+
"execution_count": 13,
563595
"metadata": {},
564596
"output_type": "execute_result"
565597
}

0 commit comments

Comments
 (0)