Browse Source

fix test_skinterface for MatrixProfile

master
lhenry15 4 years ago
parent
commit
e60bd2aa00
2 changed files with 30 additions and 18 deletions
  1. +30
    -15
      tods/detection_algorithm/MatrixProfile.py
  2. +0
    -3
      tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py

+ 30
- 15
tods/detection_algorithm/MatrixProfile.py View File

@@ -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]):


+ 0
- 3
tods/tests/sk_interface/detection_algorithm/test_ski_MatrixProfile.py View File

@@ -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)



Loading…
Cancel
Save