Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw insufficient data error when expected #250

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/script/ml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function setupPredictionInterval(): void {
});
}

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

if (!currentState.isInputConnected) return;

// Get formatted version of previous data
const data = getPrevData();
if (data === undefined)
throw new Error('Unsufficient amount of data to make prediction');
const input: number[] = makeInputs(data);
const inputTensor = tf.tensor([input]);
const prediction: Tensor = get(model).predict(inputTensor) as Tensor;
prediction
.data()
.then(data => {
tfHandlePrediction(data as Float32Array);
})
.catch(err => console.error('Prediction error:', err));
if (currentState.isInputConnected) {
// Get formatted version of previous data
const data = getPrevData();
if (data === undefined)
if (previouslyConnected) {
throw new Error('Insufficient amount of data to make prediction');
} else {
// If we have connected a micro:bit while on the test model page
// insufficient data is expected.
return;
}
const input: number[] = makeInputs(data);
const inputTensor = tf.tensor([input]);
const prediction: Tensor = get(model).predict(inputTensor) as Tensor;
prediction
.data()
.then(data => {
tfHandlePrediction(data as Float32Array);
})
.catch(err => console.error('Prediction error:', err));
}
previouslyConnected = currentState.isInputConnected;
}

function tfHandlePrediction(result: Float32Array) {
Expand Down
Loading