-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_anomaly.py
92 lines (70 loc) · 2.56 KB
/
test_anomaly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import sys
sys.path.insert(0, os.path.abspath(".."))
import uuid
import pandas as pd
import pytest
from mlflow.tracking.client import MlflowClient
import pycaret.anomaly
import pycaret.datasets
@pytest.fixture(scope="module")
def data():
return pycaret.datasets.get_data("anomaly")
def test_anomaly(data):
experiment_name = uuid.uuid4().hex
pycaret.anomaly.setup(
data,
normalize=True,
log_experiment=True,
experiment_name=experiment_name,
experiment_custom_tags={"tag": 1},
log_plots=True,
html=False,
session_id=123,
n_jobs=1,
)
# create model
iforest = pycaret.anomaly.create_model("iforest", experiment_custom_tags={"tag": 1})
knn = pycaret.anomaly.create_model("knn", experiment_custom_tags={"tag": 1})
# Plot model
pycaret.anomaly.plot_model(iforest)
pycaret.anomaly.plot_model(knn)
# assign model
iforest_results = pycaret.anomaly.assign_model(iforest)
knn_results = pycaret.anomaly.assign_model(knn)
assert isinstance(iforest_results, pd.DataFrame)
assert isinstance(knn_results, pd.DataFrame)
# predict model
iforest_predictions = pycaret.anomaly.predict_model(model=iforest, data=data)
knn_predictions = pycaret.anomaly.predict_model(model=knn, data=data)
assert isinstance(iforest_predictions, pd.DataFrame)
assert isinstance(knn_predictions, pd.DataFrame)
# get config
X = pycaret.anomaly.get_config("X")
seed = pycaret.anomaly.get_config("seed")
assert isinstance(X, pd.DataFrame)
assert isinstance(seed, int)
# set config
pycaret.anomaly.set_config("seed", 124)
seed = pycaret.anomaly.get_config("seed")
assert seed == 124
# returns table of models
all_models = pycaret.anomaly.models()
assert isinstance(all_models, pd.DataFrame)
# Assert the custom tags are created
client = MlflowClient()
experiment = client.get_experiment_by_name(experiment_name)
for experiment_run in client.list_run_infos(experiment.experiment_id):
run = client.get_run(experiment_run.run_id)
assert run.data.tags.get("tag") == "1"
# save model
pycaret.anomaly.save_model(knn, "knn_model_23122019")
# reset
pycaret.anomaly.set_current_experiment(pycaret.anomaly.AnomalyExperiment())
# load model
knn = pycaret.anomaly.load_model("knn_model_23122019")
# predict model
knn_predictions = pycaret.anomaly.predict_model(model=knn, data=data)
assert isinstance(knn_predictions, pd.DataFrame)
if __name__ == "__main__":
test_anomaly()