Skip to content

Commit f7695eb

Browse files
Don't throw insufficient data error when expected (#250)
If you connect a micro:bit while viewing the test model page with a trained model, we expect that there won't be enough data initially to make a prediction.
1 parent d9dc13e commit f7695eb

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/script/ml.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ function setupPredictionInterval(): void {
268268
});
269269
}
270270

271+
let previouslyConnected = false;
271272
// Classify data
272273
export function classify() {
273274
// Get currentState to check whether the prediction has been interrupted by other processes
@@ -290,21 +291,28 @@ export function classify() {
290291
return;
291292
}
292293

293-
if (!currentState.isInputConnected) return;
294-
295-
// Get formatted version of previous data
296-
const data = getPrevData();
297-
if (data === undefined)
298-
throw new Error('Unsufficient amount of data to make prediction');
299-
const input: number[] = makeInputs(data);
300-
const inputTensor = tf.tensor([input]);
301-
const prediction: Tensor = get(model).predict(inputTensor) as Tensor;
302-
prediction
303-
.data()
304-
.then(data => {
305-
tfHandlePrediction(data as Float32Array);
306-
})
307-
.catch(err => console.error('Prediction error:', err));
294+
if (currentState.isInputConnected) {
295+
// Get formatted version of previous data
296+
const data = getPrevData();
297+
if (data === undefined)
298+
if (previouslyConnected) {
299+
throw new Error('Insufficient amount of data to make prediction');
300+
} else {
301+
// If we have connected a micro:bit while on the test model page
302+
// insufficient data is expected.
303+
return;
304+
}
305+
const input: number[] = makeInputs(data);
306+
const inputTensor = tf.tensor([input]);
307+
const prediction: Tensor = get(model).predict(inputTensor) as Tensor;
308+
prediction
309+
.data()
310+
.then(data => {
311+
tfHandlePrediction(data as Float32Array);
312+
})
313+
.catch(err => console.error('Prediction error:', err));
314+
}
315+
previouslyConnected = currentState.isInputConnected;
308316
}
309317

310318
function tfHandlePrediction(result: Float32Array) {

0 commit comments

Comments
 (0)