From e60bd2aa00f947e8478de7305ea8e2ebaf0b779b Mon Sep 17 00:00:00 2001 From: lhenry15 Date: Sun, 30 May 2021 13:42:09 -0500 Subject: [PATCH] fix test_skinterface for MatrixProfile --- tods/detection_algorithm/MatrixProfile.py | 45 ++++++++++++++-------- .../detection_algorithm/test_ski_MatrixProfile.py | 3 -- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/tods/detection_algorithm/MatrixProfile.py b/tods/detection_algorithm/MatrixProfile.py index 3952860..b0bdc74 100644 --- a/tods/detection_algorithm/MatrixProfile.py +++ b/tods/detection_algorithm/MatrixProfile.py @@ -101,9 +101,12 @@ class MP(CollectiveBaseDetector): return_numpy=True, flatten=True) sub_matrices = sub_matrices[:-1, :] - self.left_inds_ = self.left_inds_[:-1] - self.right_inds_ = self.right_inds_[:-1] + #self.left_inds_ = self.left_inds_[:-1] + #self.right_inds_ = self.right_inds_[:-1] matrix_profile, matrix_profile_indices = stumpy.mstump(X.transpose(), m = self._window_size) + blank = X.shape[0] - matrix_profile.shape[0] + for i in range(blank): + matrix_profile = np.append(matrix_profile, [matrix_profile[-1]], axis=0) #matrix_profile, matrix_profile_indices = stumpy.mstump(data, m = self._window_size) #left_inds_ = numpy.arange(0, len(matrix_profile), self._step_size) @@ -116,11 +119,15 @@ class MP(CollectiveBaseDetector): scaler = MinMaxScaler() scaler = scaler.fit(matrix_profile) matrix_profile = scaler.transform(matrix_profile) + + # sum over the dimension with normalized MP value + if len(matrix_profile.shape) > 1 or matrix_profile.shape[1] > 1: + matrix_profile = np.sum(matrix_profile, axis=1) self.decision_scores_ = matrix_profile self._process_decision_scores() return self - def decision_function(self, data): + def decision_function(self, X): """ @@ -141,26 +148,34 @@ class MP(CollectiveBaseDetector): transformed_columns=pd.concat([transformed_columns,output], axis=1) return transformed_columns """ - matrix_profile, matrix_profile_indices = stumpy.mstump(data.transpose(), m = self._window_size) + sub_matrices, left_inds_, right_inds_ = get_sub_matrices( + X, + window_size=self._window_size, + step=self._step_size, + return_numpy=True, + flatten=True) + sub_matrices = sub_matrices[:-1, :] + matrix_profile, matrix_profile_indices = stumpy.mstump(X.transpose(), m = self._window_size) + blank = X.shape[0] - matrix_profile.shape[0] + for i in range(blank): + matrix_profile = np.append(matrix_profile, [matrix_profile[-1]], axis=0) #matrix_profile, matrix_profile_indices = stumpy.mstump(data, m = self._window_size) - left_inds_ = numpy.arange(0, len(matrix_profile), self._step_size) - right_inds_ = left_inds_ + self._window_size - right_inds_[right_inds_ > len(matrix_profile)] = len(matrix_profile) - left_inds_ = np.array([left_inds_]).transpose() - right_inds_ = np.array([right_inds_]).transpose() + #left_inds_ = numpy.arange(0, len(matrix_profile), self._step_size) + #right_inds_ = left_inds_ + self._window_size + #right_inds_[right_inds_ > len(matrix_profile)] = len(matrix_profile) + #left_inds_ = np.array([left_inds_]).transpose() + #right_inds_ = np.array([right_inds_]).transpose() # apply min-max scaling scaler = MinMaxScaler() scaler = scaler.fit(matrix_profile) matrix_profile = scaler.transform(matrix_profile) - #output = [] - #for timestamp in matrix_profile: - # timestamp = sum(timestamp) - # output.append([timestamp]) - #output = np.concatenate((output, left_inds_, right_inds_),axis=1) - + # sum over the dimension with normalized MP value + if len(matrix_profile.shape) > 1 or matrix_profile.shape[1] > 1: + matrix_profile = np.sum(matrix_profile, axis=1) + return matrix_profile, left_inds_, right_inds_ class MatrixProfilePrimitive(UnsupervisedOutlierDetectorBase[Inputs, Outputs, Params, Hyperparams]): diff --git a/tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py b/tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py index 13997a7..32f9adf 100644 --- a/tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py +++ b/tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py @@ -27,9 +27,6 @@ class MatrixProfileSKI_TestCase(unittest.TestCase): n_train=self.n_train, n_test=self.n_test, contamination=self.contamination, random_state=42) - self.y_test = self.y_test[self.window_size-1:] - self.y_train = self.y_train[self.window_size-1:] - self.transformer = MatrixProfileSKI(contamination=self.contamination, window_size=self.window_size) self.transformer.fit(self.X_train)