Skip to content

Commit b931ac1

Browse files
authored
Merge branch 'dev' into dev
2 parents aa36efe + 6f1f3d4 commit b931ac1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+423
-184
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ mlruns/
401401
mlartifacts/
402402
model/
403403
venv/
404+
dist/
405+
temp/
404406

405407
.idea/
406408

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
from .multivariate.model import MultivariateAnomalyDetector
26
from .univariate.univariate_anomaly_detection import UnivariateAnomalyDetector, EntireAnomalyDetector, LatestAnomalyDetector

anomaly-detector/anomaly_detector/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
from abc import abstractmethod
26

37
import mlflow
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+

anomaly-detector/anomaly_detector/common/constants.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
import enum
26

37
TIMESTAMP = "timestamp"
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
from typing import List, Union
26

37
import pandas as pd
@@ -12,9 +16,6 @@ def __init__(
1216
*,
1317
fill_na_method: str = FillNAMethod.Linear.name,
1418
fill_na_value: float = 0.0,
15-
window: Union[int, float, str],
16-
start_time: str = None,
17-
end_time: str = None,
1819
):
1920
if not hasattr(FillNAMethod, fill_na_method):
2021
raise InvalidParameterError(
@@ -25,32 +26,14 @@ def __init__(
2526

2627
self.fill_na_method = FillNAMethod[fill_na_method]
2728
self.fill_na_value = fill_na_value
28-
try:
29-
self.window = int(window)
30-
except TypeError:
31-
raise InvalidParameterError(f"Cannot convert window to int.")
32-
try:
33-
self.start_time = pd.to_datetime(start_time).tz_localize(None)
34-
self.end_time = pd.to_datetime(end_time).tz_localize(None)
35-
except Exception as e:
36-
raise InvalidParameterError(
37-
f"Cannot convert start_time or end_time. {str(e)}."
38-
)
39-
if self.start_time > self.end_time:
40-
raise InvalidParameterError(f"start_time cannot be later than end_time.")
4129

4230
def process(self, data: pd.DataFrame):
4331
if not isinstance(data, pd.DataFrame):
4432
raise DataFormatError(f"data must be a pandas.DataFrame")
45-
if TIMESTAMP not in data.columns:
46-
raise DataFormatError(f"data must has a {TIMESTAMP} column.")
47-
data = data.set_index(TIMESTAMP, drop=True).sort_index() # sort indices
48-
data.index = pd.to_datetime(data.index).tz_localize(None)
33+
data = data.sort_index() # sort indices
4934
data = data[sorted(data.columns)] # sort columns
5035
data = self.fill_na(data)
51-
data, effective_timestamps = self.truncate_data(data)
52-
effective_timestamps = [dt_to_str(x) for x in effective_timestamps]
53-
return data, effective_timestamps
36+
return data
5437

5538
def fill_na(self, data: pd.DataFrame):
5639
if not isinstance(data, pd.DataFrame):
@@ -72,20 +55,7 @@ def fill_na(self, data: pd.DataFrame):
7255
method="linear", limit_direction="both", axis=0, limit=len(data)
7356
)
7457
elif self.fill_na_method == FillNAMethod.Fixed:
75-
output_series = data.fillna(self.fill_merge_na_value)
58+
output_series = data.fillna(self.fill_na_value)
7659
else:
7760
output_series = data
7861
return output_series.fillna(0)
79-
80-
def truncate_data(self, data: pd.DataFrame):
81-
if not isinstance(data, pd.DataFrame):
82-
raise DataFormatError(f"data must be a pandas.DataFrame")
83-
effective_df = data.loc[self.start_time : self.end_time]
84-
if len(effective_df) == 0:
85-
raise DataFormatError(f"no effective data.")
86-
first_index = effective_df.index[0]
87-
start_loc = max(0, data.index.get_loc(first_index) - self.window + 1)
88-
start_index = data.index[start_loc]
89-
end_index = self.end_time
90-
data = data.loc[start_index:end_index]
91-
return data, effective_df.index.to_list()

anomaly-detector/anomaly_detector/common/error_code.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
class ErrorCodes:
26
# invalid data format
37
InvalidDataFormat = "Invalid data format. {0}"

anomaly-detector/anomaly_detector/common/exception.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
class AnomalyDetectorException(Exception):
26
def __init__(self, code="", message=""):
37
if code == "":

anomaly-detector/anomaly_detector/common/time_util.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
import math
26

37
from dateutil import parser, tz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+

anomaly-detector/anomaly_detector/multivariate/contract.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
from dataclasses import dataclass
26
from typing import List
37

@@ -9,11 +13,9 @@ class MultiADConstants:
913
INFERENCE_CLIP_MAX = 1000.0
1014
ANOMALY_UPPER_THRESHOLD = 0.5
1115
ANOMALY_LOWER_THRESHOLD = 0.3
12-
TOP_CONTRIBUTORS_COUNT = 30
1316
TOP_ATTENTION_COUNT = 10
1417
DEFAULT_INPUT_SIZE = 200
1518
DEFAULT_THRESHOLD_WINDOW = 200
16-
TOP_ATTENTION_COUNT = 10
1719

1820

1921
@dataclass

anomaly-detector/anomaly_detector/multivariate/dataset.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
15
from typing import List, Union
26

37
import numpy as np

0 commit comments

Comments
 (0)