Download tests/test_evaluation.py from thu-sail-lab/VETime: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/thu-sail-lab/VETime/resolve/main/tests/test_evaluation.py
- Command line
-
hf download hf://thu-sail-lab/VETime/tests/test_evaluation.py
-
curl -L -o test_evaluation.py https://huggingface.co/thu-sail-lab/VETime/resolve/main/tests/test_evaluation.py
2.52 kB
| import unittest | |
| from unittest.mock import patch | |
| import numpy as np | |
| from sklearn.metrics import f1_score | |
| from evaluation.metrics import get_metrics, get_metrics_pred | |
| class EvaluationTests(unittest.TestCase): | |
| def setUp(self): | |
| self.labels = np.zeros(64, dtype=int) | |
| self.labels[20:24] = 1 | |
| self.scores = np.linspace(0.05, 0.15, 64) | |
| self.scores[20:24] = [0.81, 0.86, 0.91, 0.96] | |
| def test_supplied_predictions_are_respected(self): | |
| pred = np.zeros(64, dtype=int) | |
| pred[19:22] = 1 | |
| original = pred.copy() | |
| with patch("numpy.quantile", side_effect=AssertionError("Threshold search is not allowed")): | |
| result = get_metrics(self.scores, self.labels, slidingWindow=3, pred=pred, thre=20) | |
| self.assertAlmostEqual(result["Standard-F1"], f1_score(self.labels, original)) | |
| np.testing.assert_array_equal(pred, original) | |
| self.assertEqual(len(result), 16) | |
| self.assertTrue(all(np.isfinite(v) for v in result.values())) | |
| def test_padding_does_not_change_scores_or_calibration(self): | |
| expected = get_metrics(self.scores, self.labels, slidingWindow=3, thre=20) | |
| padded = get_metrics(np.r_[self.scores, 1000.0], np.r_[self.labels, -1], slidingWindow=3, thre=20) | |
| for key in expected: | |
| np.testing.assert_allclose(padded[key], expected[key], equal_nan=True) | |
| def test_default_matches_fixed_three_sigma_predictions(self): | |
| pred = self.scores > self.scores.mean() + 3 * self.scores.std() | |
| result = get_metrics(self.scores, self.labels, slidingWindow=3, thre=20) | |
| self.assertAlmostEqual(result["Standard-F1"], f1_score(self.labels, pred)) | |
| def test_empty_predictions_do_not_trigger_oracle_search(self): | |
| result = get_metrics_pred(self.scores, self.labels, np.zeros(64), slidingWindow=3) | |
| self.assertEqual(result["Standard-F1"], 0) | |
| self.assertEqual(result["PA-F1"], 0) | |
| self.assertEqual(result["F1_T"], 0) | |
| def test_single_class_ranking_metrics_are_undefined(self): | |
| result = get_metrics(self.scores, np.zeros(64), pred=np.zeros(64), slidingWindow=3) | |
| self.assertTrue(np.isnan(result["AUC-ROC"])) | |
| self.assertTrue(np.isnan(result["VUS-PR"])) | |
| def test_invalid_inputs_are_rejected(self): | |
| for scores, labels in [([], []), ([np.nan], [0]), ([0.2], [2]), ([0.2], [-1])]: | |
| with self.assertRaises(ValueError): | |
| get_metrics(scores, labels) | |
| if __name__ == "__main__": | |
| unittest.main() | |