Skip to content

handle errors cleanly rather than crashing #1010

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

Merged
merged 2 commits into from
Sep 12, 2023
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
9 changes: 7 additions & 2 deletions pgml-extension/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Module providing various metrics used to rank the algorithms.
use pgrx::*;
use std::collections::{BTreeSet, HashMap};

use ndarray::{Array2, ArrayView1};
Expand Down Expand Up @@ -51,13 +52,17 @@ impl ConfusionMatrix {
y_hat: &ArrayView1<usize>,
num_classes: usize,
) -> ConfusionMatrix {
assert_eq!(ground_truth.len(), y_hat.len());
if ground_truth.len() != y_hat.len() {
error!("Can't compute metrics when the ground truth labels are a different size than the predicted labels. {} != {}", ground_truth.len(), y_hat.len())
};

// Distinct classes.
let mut classes = ground_truth.iter().collect::<BTreeSet<_>>();
classes.extend(&mut y_hat.iter().collect::<BTreeSet<_>>().into_iter());

assert_eq!(num_classes, classes.len());
if num_classes != classes.len() {
error!("Can't compute metrics when the number of classes in the test set is different than the number of classes in the training set. {} != {}", num_classes, classes.len())
};

// Class value = index in the confusion matrix
// e.g. class value 5 will be index 4 if there are classes 1, 2, 3 and 4 present.
Expand Down