Skip to content

Commit

Permalink
fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanyong Duan committed Jan 8, 2025
1 parent ca5504d commit 6321c1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,5 @@ time_series_anomaly_detector.egg-info/
*.so
*.pyd
.script\Dockerfile
src/anomaly_detector/univariate/_anomaly_kernel_cython.c
src/anomaly_detector/univariate/_anomaly_kernel_cython.c
model/
34 changes: 18 additions & 16 deletions tests/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import numpy as np
from anomaly_detector.multivariate.model import MultivariateAnomalyDetector
from anomaly_detector.common.exception import DataFormatError, InvalidParameterError
import os

TEST_FILE_ROOT = "testCase/testCase_10000_20/"
WORKING_DIR = os.path.dirname(os.path.realpath(__file__))
TEST_FILE_ROOT = "testCase/testCase_10000_20"


def read_yaml_config(path: str) -> dict:
Expand All @@ -26,51 +28,51 @@ def setup_method(self):

def test_invalid_value(self):
with pytest.raises(DataFormatError, match="Cannot convert values to float"):
train_data = pd.read_csv(TEST_FILE_ROOT + "invalid_value.csv")
params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["normal_config"]
train_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "invalid_value.csv"))
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["normal_config"]
self.model.fit(train_data, params)

def test_timestamp_not_exist(self):
train_data = pd.read_csv(TEST_FILE_ROOT + "no_timestamp.csv")
params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["normal_config"]
train_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "no_timestamp.csv"))
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["normal_config"]
self.model.fit(train_data, params)

def test_normal_predict(self):
eval_data = np.random.randn(201, 20)
eval_data[-1, :] += 100
columns = [f"variable_{i}" for i in range(20)]
eval_data = pd.DataFrame(eval_data, columns=columns)
with open(TEST_FILE_ROOT + 'model.pkl', 'rb') as f:
with open(os.path.join(WORKING_DIR, TEST_FILE_ROOT, 'model.pkl'), 'rb') as f:
loaded_model = pickle.load(f)
loaded_model.predict(eval_data)

def test_inference_data_smaller_than_window(self):
with pytest.raises(ValueError):
eval_data = pd.read_csv(TEST_FILE_ROOT + "inference_data_smaller_than_window.csv")
eval_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "inference_data_smaller_than_window.csv"))
eval_data = eval_data.set_index("timestamp", drop=True)
with open(TEST_FILE_ROOT + 'model.pkl', 'rb') as f:
with open(os.path.join(WORKING_DIR, TEST_FILE_ROOT,'model.pkl'), 'rb') as f:
loaded_model = pickle.load(f)
loaded_model.predict(eval_data)

def test_invalid_fillna_config(self):
with pytest.raises(InvalidParameterError):
train_data = pd.read_csv(TEST_FILE_ROOT + "normal_data.csv")
train_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "normal_data.csv"))
train_data = train_data.set_index("timestamp", drop=True)
params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["invalid_fillna_config"]
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["invalid_fillna_config"]
self.model.fit(train_data, params)

def test_invalid_fillna_value(self):
with pytest.raises(InvalidParameterError):
train_data = pd.read_csv(TEST_FILE_ROOT + "normal_data.csv")
train_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "normal_data.csv"))
train_data = train_data.set_index("timestamp", drop=True)
params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["invalid_fillna_value"]
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["invalid_fillna_value"]
self.model.fit(train_data, params)

def test_invalid_window_value(self):
with pytest.raises(Exception):
train_data = pd.read_csv(TEST_FILE_ROOT + "normal_data.csv")
train_data = pd.read_csv(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "normal_data.csv"))
train_data = train_data.set_index("timestamp", drop=True)
params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["invalid_window_value"]
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["invalid_window_value"]
self.model.fit(train_data, params)

def test_response(self):
Expand All @@ -82,11 +84,11 @@ def test_response(self):
training_data = np.random.randn(data_len, var_len)
columns = [f"variable_{i}" for i in range(var_len)]
training_data = pd.DataFrame(training_data, columns=columns)
timestamps = pd.date_range(start="2023-01-03", periods=data_len, freq="H")
timestamps = pd.date_range(start="2023-01-03", periods=data_len, freq="h")
training_data["timestamp"] = timestamps.strftime("%Y-%m-%dT%H:%M:%SZ")
training_data = training_data.set_index("timestamp", drop=True)

params = read_yaml_config(TEST_FILE_ROOT + "config.yaml")["normal_config"]
params = read_yaml_config(os.path.join(WORKING_DIR, TEST_FILE_ROOT, "config.yaml"))["normal_config"]
model = MultivariateAnomalyDetector()
print(var_len, data_len)
model.fit(training_data, params=params)
Expand Down

0 comments on commit 6321c1d

Please sign in to comment.