-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Avik Basu <[email protected]>
- Loading branch information
Showing
7 changed files
with
136 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
import pandas as pd | ||
import pytest | ||
|
||
from numalogic._constants import TESTS_DIR | ||
from numalogic.models.threshold import MaxPercentileThreshold | ||
|
||
|
||
@pytest.fixture | ||
def data() -> tuple[npt.NDArray[float], npt.NDArray[float]]: | ||
x = pd.read_csv( | ||
os.path.join(TESTS_DIR, "resources", "data", "prom_mv.csv"), index_col="timestamp" | ||
).to_numpy(dtype=np.float32) | ||
return x[:-50], x[-50:] | ||
|
||
|
||
@pytest.fixture() | ||
def fitted(data): | ||
clf = MaxPercentileThreshold(max_inlier_percentile=75, min_threshold=1e-3) | ||
x_train, _ = data | ||
clf.fit(x_train) | ||
return clf | ||
|
||
|
||
def test_score_samples(data, fitted): | ||
_, x_test = data | ||
y_scores = fitted.score_samples(x_test) | ||
assert len(fitted.threshold) == 3 | ||
assert fitted.threshold[1] == 1e-3 | ||
assert y_scores.shape == (50, 3) | ||
|
||
|
||
def test_predict(data, fitted): | ||
_, x_test = data | ||
y_pred = fitted.predict(x_test) | ||
assert y_pred.shape == (50, 3) | ||
|
||
|
||
def test_agg_score_samples(data): | ||
x_train, x_test = data | ||
clf_1 = MaxPercentileThreshold(max_inlier_percentile=75, min_threshold=1e-3, aggregate=True) | ||
clf_2 = MaxPercentileThreshold( | ||
max_inlier_percentile=75, | ||
min_threshold=1e-3, | ||
aggregate=True, | ||
feature_weights=[0.1, 0.7, 0.2], | ||
) | ||
clf_1.fit(x_train) | ||
clf_2.fit(x_train) | ||
|
||
y_scores_1 = clf_1.score_samples(x_test) | ||
y_scores_2 = clf_2.score_samples(x_test) | ||
|
||
assert y_scores_1.shape == y_scores_2.shape == (50, 1) | ||
assert np.sum(y_scores_1) > np.sum(y_scores_2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from numalogic.models.threshold import StaticThreshold, SigmoidThreshold | ||
|
||
|
||
class TestStaticThreshold(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.x = np.arange(20).reshape(10, 2).astype(float) | ||
|
||
def test_predict(self): | ||
clf = StaticThreshold(upper_limit=5) | ||
clf.fit(self.x) | ||
y = clf.predict(self.x) | ||
self.assertTupleEqual(self.x.shape, y.shape) | ||
self.assertEqual(np.max(y), 1) | ||
self.assertEqual(np.min(y), 0) | ||
|
||
def test_score(self): | ||
clf = StaticThreshold(upper_limit=5.0) | ||
y = clf.score_samples(self.x) | ||
self.assertTupleEqual(self.x.shape, y.shape) | ||
self.assertEqual(np.max(y), clf.outlier_score) | ||
self.assertEqual(np.min(y), clf.inlier_score) | ||
|
||
|
||
class TestSigmoidThreshold(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.x = np.arange(20).reshape(10, 2).astype(float) | ||
|
||
def test_predict(self): | ||
clf = SigmoidThreshold(upper_limit=5) | ||
clf.fit(self.x) | ||
y = clf.predict(self.x) | ||
self.assertTupleEqual(self.x.shape, y.shape) | ||
self.assertEqual(np.max(y), 1) | ||
self.assertEqual(np.min(y), 0) | ||
|
||
def test_score(self): | ||
clf = SigmoidThreshold(upper_limit=5.0) | ||
y = clf.score_samples(self.x) | ||
self.assertTupleEqual(self.x.shape, y.shape) | ||
self.assertEqual(np.max(y), clf.score_limit) | ||
self.assertGreater(np.min(y), 0.0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from numalogic.models.threshold import StdDevThreshold | ||
|
||
|
||
class TestStdDevThreshold(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.x_train = np.arange(100).reshape(50, 2) | ||
self.x_test = np.arange(100, 160, 6).reshape(5, 2) | ||
|
||
def test_estimator_predict(self): | ||
clf = StdDevThreshold() | ||
clf.fit(self.x_train) | ||
y = clf.predict(self.x_test) | ||
self.assertAlmostEqual(0.4, np.mean(y), places=1) | ||
|
||
def test_estimator_score(self): | ||
clf = StdDevThreshold() | ||
clf.fit(self.x_train) | ||
score = clf.score_samples(self.x_test) | ||
self.assertAlmostEqual(0.93317, np.mean(score), places=2) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |