-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
227 lines (182 loc) · 7.41 KB
/
index.js
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
import "./styles.css";
// show a loading message while we load the wasm
const status = document.getElementById("status");
status.textContent = "Loading WASM...";
// load the wasm
import('./pkg')
.then(wasm => {
const main = document.getElementById("main");
const start_button = document.getElementById("start_button");
const seed_input = document.getElementById("seed");
const chain_count_input = document.getElementById("chain_count");
const chain_count_display = document.getElementById("chain_count_display");
const results = document.getElementById("results");
const tuning = document.getElementById("tuning");
const samples = document.getElementById("samples");
const raw = document.getElementById("raw_text");
const input = document.getElementById("input_text");
const input_url = document.getElementById("input_url");
const load_button = document.getElementById("load_button");
const posterior = document.getElementById("posterior");
const int = (str) => {
const i = parseInt(str);
if (isNaN(i)) {
return 0;
}
return i;
}
const clear_data = () => {
try{
// clear the canvas
const canvas = document.getElementById("trace_plot");
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
// clear the posterior
posterior.textContent = "";
} catch (e) {
console.error(e);
input.value = "Error: " + e
};
}
const plot = () => {
try {
console.log("plotting");
wasm.plot_tmax("plot", posterior.textContent, input.value);
} catch (e) {
console.error(e);
input.value = "Error: " + e
};
}
const sample = (onSuccess) => {
status.textContent = "Running...";
setTimeout(() => {
const start = Date.now();
const chain_count = BigInt(chain_count_display.value);
const seed = BigInt(seed_input.value);
const input_data = input.value;
const tuning_value = BigInt(tuning.value);
const samples_value = BigInt(samples.value);
console.log(`input:\n${input_data}`);
console.log(`seed: ${seed}`);
console.log(`chain_count: ${chain_count}`);
console.log(`tuning: ${tuning_value}`);
console.log(`samples: ${samples_value}`);
setTimeout(() => {
wasm.run_with("trace_plot", "posterior", seed, input_data, chain_count, tuning_value, samples_value);
const end = Date.now();
const elapsed = end - start;
results.textContent = `Elapsed: ${elapsed}ms`;
setTimeout(() => {
plot();
if (onSuccess) {
onSuccess();
}
status.textContent = "Ready";
}, 10);
}, 10);
}, 10);
}
const prepare = (onSuccess) => {
status.textContent = "Preparing...";
try {
// clear the canvas
const canvas = document.getElementById("trace_plot");
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
input.value = wasm.prepare(raw.value);
if (onSuccess) {
onSuccess();
}
} catch (e) {
console.error(e);
input.value = "Error: " + e
};
}
const prepare_and_sample = (onSuccess) => {
prepare(() => {
setTimeout(() => {
clear_data();
plot();
setTimeout(() => {
sample(onSuccess);
}, 10);
}, 10);
});
}
const load = (url, onSuccess) => {
// disable the buttons
load_button.disabled = true;
start_button.disabled = true;
// clear the results
results.textContent = "";
setTimeout(() => {
try {
status.textContent = `Fetching ${url}`;
setTimeout(async() => {
try {
const response = await fetch(url);
const text = await response.text();
raw.value = text;
if (onSuccess) {
onSuccess(() => {
status.textContent = 'Ready';
// enable the button
load_button.disabled = false;
start_button.disabled = false;
});
}
} catch (e) {
console.error(e);
input.value = "Error: " + e
}
}, 10);
} catch (e) {
console.error(e);
input.value = "Error: " + e
};
}, 10);
}
load_button.onclick = () => {
const url = input_url.value;
load(url, (onSuccess) => {
prepare_and_sample(onSuccess);
});
}
// on_change of raw, update input
// raw.onchange = () => {
// console.log("raw changed");
// prepare_and_run();
// }
prepare_and_sample(() => {
const new_seed = Math.floor(Math.random() * 10000);
seed_input.value = new_seed.toString();
});
// update chain_count_display on chain_count change
chain_count_input.onchange = () => {
const chain_count = int(chain_count_input.value);
chain_count_display.value = chain_count.toString();
}
status.textContent = "Ready";
const run = (onSuccess) => {
// disable the buttons
load_button.disabled = true;
start_button.disabled = true;
// clear the results
results.textContent = "";
prepare_and_sample(() => {
if (onSuccess) {
onSuccess();
// enable the button
load_button.disabled = false;
start_button.disabled = false;
}
});
}
start_button.onclick = () => {
run(() => {// update the seed
const new_seed = Math.floor(Math.random() * 10000);
seed_input.value = new_seed.toString();
});
}
})
.catch(console.error);