Skip to content

Commit abacd17

Browse files
authored
configurable random seed & better validation (#2)
1 parent e27a07a commit abacd17

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

App.vue

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ canvas {
4848
<v-text-field label="Number of samples" type="Number" v-model.number="nSamples"
4949
:error-messages="nSamplesError"></v-text-field>
5050
</v-col>
51+
<v-col cols="6">
52+
<v-text-field label="Random seed" type="Number" v-model.number="randomSeed"
53+
:error-messages="randomSeedError"></v-text-field>
54+
</v-col>
55+
56+
5157
</v-row>
5258
</v-card-text>
5359
</v-card>
@@ -86,6 +92,8 @@ canvas {
8692
import { compute } from "./bayesian-wasm/pkg";
8793
import Group from "./components/Group.vue";
8894
95+
96+
8997
export default {
9098
data() {
9199
return {
@@ -105,6 +113,8 @@ export default {
105113
betaError: [],
106114
nSamples: 100000,
107115
nSamplesError: [],
116+
randomSeed: 0,
117+
randomSeedError: [],
108118
n_bins: 100,
109119
result: null,
110120
dpr: 3,
@@ -117,25 +127,36 @@ export default {
117127
if (this.b === null) invalid = true;
118128
let alpha = parseFloat(this.alpha);
119129
let beta = parseFloat(this.beta);
120-
let nSamples = parseInt(this.nSamples);
121-
if (isNaN(alpha) || alpha < 0) {
122-
this.alphaError = ["valid, positive float required."];
130+
let nSamples = parseFloat(this.nSamples, 10);
131+
let randomSeed = parseFloat(this.randomSeed, 10);
132+
if (isNaN(alpha) || alpha <= 0) {
133+
this.alphaError = ["valid, positive floating number required."];
123134
invalid = true;
124135
} else {
125136
this.alphaError = null;
126137
}
127-
if (isNaN(beta) || beta < 0) {
128-
this.betaError = ["valid, positive float required."];
138+
if (isNaN(beta) || beta <= 0) {
139+
this.betaError = ["valid, positive floating number required."];
129140
invalid = true;
130141
} else {
131142
this.betaError = [];
132143
}
133-
if (isNaN(nSamples) || nSamples <= 0) {
134-
this.nSamplesError = ["this field must be strictly positive."];
144+
if (isNaN(nSamples) || nSamples <= 0 || !Number.isInteger(nSamples)) {
145+
this.nSamplesError = ["This field must be a strictly positive integer."];
135146
invalid = true;
136147
} else {
137148
this.nSamplesError = [];
138149
}
150+
151+
152+
if (isNaN(randomSeed) || randomSeed < 0 || !Number.isInteger(randomSeed)) {
153+
this.randomSeedError = ["This field must be a non-negative integer."];
154+
invalid = true;
155+
} else {
156+
this.randomSeedError = [];
157+
}
158+
159+
139160
if (invalid) {
140161
return null;
141162
}
@@ -149,6 +170,7 @@ export default {
149170
b_pos: this.b.pos,
150171
n_samples: nSamples,
151172
n_bins: this.n_bins,
173+
random_seed: randomSeed,
152174
};
153175
return JSON.stringify(data);
154176
},

bayesian-wasm/src/bbinomial.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ fn _take_f64(v: &Value, key: &str) -> Result<f64, JsValue> {
1616
}
1717
}
1818

19+
fn _take_u64(v: &Value, key: &str) -> Result<u64, JsValue> {
20+
let target = &v[key];
21+
match target.as_u64() {
22+
Some(value) => Ok(value),
23+
None => Err(JsValue::from_str(key)),
24+
}
25+
}
26+
1927
fn _take_f64_arr(v: &Value, key: &str) -> Result<Vec<f64>, JsValue> {
2028
let target = &v[key];
2129
let mut result = Vec::new();
@@ -68,7 +76,6 @@ pub fn compute(
6876
let dpr_i = dpr as i32;
6977

7078
use serde_json;
71-
let mut rng = Pcg64::seed_from_u64(0 as u64);
7279
let parsed: Value = match serde_json::from_str(target) {
7380
Ok(v) => Ok(v),
7481
Err(_) => Err(JsValue::from_str("Invalid JSON!")),
@@ -92,13 +99,16 @@ pub fn compute(
9299
Err(_) => 1.0,
93100
};
94101

95-
let n_samples = match _take_f64(&parsed, "n_samples") {
96-
Ok(f) => f as i64,
97-
Err(_) => 1000,
102+
let n_samples = match _take_u64(&parsed, "n_samples") {
103+
Ok(f) => f,
104+
Err(_) => 1000 as u64,
98105
};
99-
if n_samples <= 0 {
100-
return Err(JsValue::from_str("n_samples must be strictly positive!"));
101-
}
106+
107+
let random_seed = match _take_u64(&parsed, "random_seed") {
108+
Ok(f) => f,
109+
Err(_) => 0 as u64,
110+
};
111+
102112
let n_bins = match _take_f64(&parsed, "n_bins") {
103113
Ok(f) => f as i64,
104114
Err(_) => 100,
@@ -114,6 +124,8 @@ pub fn compute(
114124
],
115125
};
116126

127+
let mut rng = Pcg64::seed_from_u64(random_seed);
128+
117129
let dist_a = Beta::new(prior_pos + a_pos, prior_neg + a_tot - a_pos).unwrap();
118130
let dist_b = Beta::new(prior_pos + b_pos, prior_neg + b_tot - b_pos).unwrap();
119131
let mut a_samples = Vec::new();

0 commit comments

Comments
 (0)