From 8217b5662b9a16f7d00babc3848cfab6a6b769aa Mon Sep 17 00:00:00 2001 From: lhenry15 Date: Wed, 16 Jun 2021 14:49:09 -0500 Subject: [PATCH] fix LSTM unit_test --- .../core/CollectiveCommonTest.py | 1 - tods/detection_algorithm/core/LSTMOD.py | 32 ++++------------------ .../detection_algorithm/test_LSTMODetector.py | 2 +- 3 files changed, 6 insertions(+), 29 deletions(-) mode change 100755 => 100644 tods/detection_algorithm/core/LSTMOD.py diff --git a/tods/detection_algorithm/core/CollectiveCommonTest.py b/tods/detection_algorithm/core/CollectiveCommonTest.py index 684c5ed..7ab7083 100755 --- a/tods/detection_algorithm/core/CollectiveCommonTest.py +++ b/tods/detection_algorithm/core/CollectiveCommonTest.py @@ -94,7 +94,6 @@ class CollectiveCommonTest: def test_prediction_scores(self): pred_scores, _, _ = self.clf.decision_function(self.X_test) - # check score shapes assert_equal(pred_scores.shape[0], self.y_test.shape[0]) diff --git a/tods/detection_algorithm/core/LSTMOD.py b/tods/detection_algorithm/core/LSTMOD.py old mode 100755 new mode 100644 index 8edb792..d15907a --- a/tods/detection_algorithm/core/LSTMOD.py +++ b/tods/detection_algorithm/core/LSTMOD.py @@ -141,33 +141,7 @@ class LSTMOutlierDetector(CollectiveBaseDetector): relative_error = (np.linalg.norm(y_predict - y_buf, axis=1) / np.linalg.norm(y_buf + 1e-6, axis=1)).ravel() return relative_error - - def predict(self, X): # pragma: no cover - """Predict if a particular sample is an outlier or not. - - Parameters - ---------- - X : numpy array of shape (n_samples, n_features) - The input samples. - - Returns - ------- - outlier_labels : numpy array of shape (n_samples,) - For each observation, tells whether or not - it should be considered as an outlier according to the - fitted model. 0 stands for inliers and 1 for outliers. - """ - - check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_']) - - pred_score, X_left_inds, X_right_inds = self.decision_function(X) - - pred_score = np.concatenate((np.zeros((self.window_size,)), pred_score)) - X_left_inds = np.concatenate((np.zeros((self.window_size,)), X_left_inds)) - X_right_inds = np.concatenate((np.zeros((self.window_size,)), X_right_inds)) - - return (pred_score > self.threshold_).astype( - 'int').ravel(), X_left_inds.ravel(), X_right_inds.ravel() + def decision_function(self, X: np.array): """Predict raw anomaly scores of X using the fitted detector. @@ -259,6 +233,10 @@ class LSTMOutlierDetector(CollectiveBaseDetector): # print(relative_error_right_inds) pred_score = danger_coefficient * self.danger_coefficient_weight + averaged_relative_error * (1 - self.danger_coefficient_weight) + pred_score = np.concatenate((np.zeros((self.window_size,)), pred_score)) + relative_error_left_inds = np.concatenate((np.arange(self.window_size), relative_error_left_inds+self.window_size)) + relative_error_right_inds = np.concatenate((np.arange(self.window_size)+self.window_size, relative_error_right_inds+self.window_size)) + return pred_score, relative_error_left_inds, relative_error_right_inds diff --git a/tods/tests/detection_algorithm/test_LSTMODetector.py b/tods/tests/detection_algorithm/test_LSTMODetector.py index 812f4f1..673f7ee 100644 --- a/tods/tests/detection_algorithm/test_LSTMODetector.py +++ b/tods/tests/detection_algorithm/test_LSTMODetector.py @@ -27,7 +27,7 @@ class LSTMODTestCase(unittest.TestCase): self.X_train = d3m_dataframe({'data': [3., 4., 8., 16, 18, 13., 22., 36., 59., 128, 62, 67, 78, 100]}, columns=['data'], generate_metadata=True) - self.y_train = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + self.y_train = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.X_test = d3m_dataframe({'data': [3., 4., 8.6, 13.4, 22.5, 17, 19.2, 36.1, 127, -23, 59.2]}, columns=['data'], generate_metadata=True) self.y_test = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0])