Skip to content

Commit 1cf5632

Browse files
DavidZeLiang1228David Liang
andauthored
Refine MVAD sample (#19737)
* Add multivariate sample * MVAD sample refine * resolve comments * resolve comments * resolve comments * MVAD sample refine * remove credential info * bug fix * remove timeout * remove timeout Co-authored-by: David Liang <liangze@microsoft.com>
1 parent 6f688cc commit 1cf5632

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

sdk/anomalydetector/azure-ai-anomalydetector/samples/sample_multivariate_detect.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

2222
import os
2323
import time
24-
from datetime import datetime
24+
from datetime import datetime, timezone
2525

2626
from azure.ai.anomalydetector import AnomalyDetectorClient
2727
from azure.ai.anomalydetector.models import DetectionRequest, ModelInfo
28+
from azure.ai.anomalydetector.models import ModelStatus, DetectionStatus
2829
from azure.core.credentials import AzureKeyCredential
2930
from azure.core.exceptions import HttpResponseError
3031

3132

32-
class MultivariateSample():
33+
class MultivariateSample:
3334

3435
def __init__(self, subscription_key, anomaly_detector_endpoint, data_source=None):
3536
self.sub_key = subscription_key
@@ -43,40 +44,50 @@ def __init__(self, subscription_key, anomaly_detector_endpoint, data_source=None
4344

4445
self.data_source = data_source
4546

46-
def train(self, start_time, end_time, max_tryout=500):
47+
def train(self, start_time, end_time):
4748

4849
# Number of models available now
4950
model_list = list(self.ad_client.list_multivariate_model(skip=0, top=10000))
5051
print("{:d} available models before training.".format(len(model_list)))
5152

5253
# Use sample data to train the model
53-
print("Training new model...")
54+
print("Training new model...(it may take a few minutes)")
5455
data_feed = ModelInfo(start_time=start_time, end_time=end_time, source=self.data_source)
5556
response_header = \
5657
self.ad_client.train_multivariate_model(data_feed, cls=lambda *args: [args[i] for i in range(len(args))])[
5758
-1]
5859
trained_model_id = response_header['Location'].split("/")[-1]
5960

60-
# Model list after training
61-
new_model_list = list(self.ad_client.list_multivariate_model(skip=0, top=10000))
62-
6361
# Wait until the model is ready. It usually takes several minutes
6462
model_status = None
65-
tryout_count = 0
66-
while (tryout_count < max_tryout and model_status != "READY"):
67-
model_status = self.ad_client.get_multivariate_model(trained_model_id).model_info.status
68-
tryout_count += 1
69-
time.sleep(2)
7063

71-
assert model_status == "READY"
64+
while model_status != ModelStatus.READY and model_status != ModelStatus.FAILED:
65+
model_info = self.ad_client.get_multivariate_model(trained_model_id).model_info
66+
model_status = model_info.status
67+
time.sleep(1)
68+
69+
if model_status == ModelStatus.FAILED:
70+
print("Creating model failed.")
71+
print("Errors:")
72+
if model_info.errors:
73+
for error in model_info.errors:
74+
print("Error code: {}. Message: {}".format(error.code, error.message))
75+
else:
76+
print("None")
77+
return None
78+
79+
if model_status == ModelStatus.READY:
80+
# Model list after training
81+
new_model_list = list(self.ad_client.list_multivariate_model(skip=0, top=10000))
82+
83+
print("Done.\n--------------------")
84+
print("{:d} available models after training.".format(len(new_model_list)))
7285

73-
print("Done.", "\n--------------------")
74-
print("{:d} available models after training.".format(len(new_model_list)))
86+
# Return the latest model id
87+
return trained_model_id
7588

76-
# Return the latest model id
77-
return trained_model_id
7889

79-
def detect(self, model_id, start_time, end_time, max_tryout=500):
90+
def detect(self, model_id, start_time, end_time):
8091

8192
# Detect anomaly in the same data source (but a different interval)
8293
try:
@@ -87,14 +98,20 @@ def detect(self, model_id, start_time, end_time, max_tryout=500):
8798

8899
# Get results (may need a few seconds)
89100
r = self.ad_client.get_detection_result(result_id)
90-
tryout_count = 0
91-
while r.summary.status != "READY" and tryout_count < max_tryout:
92-
time.sleep(1)
101+
print("Get detection result...(it may take a few seconds)")
102+
103+
while r.summary.status != DetectionStatus.READY and r.summary.status != DetectionStatus.FAILED:
93104
r = self.ad_client.get_detection_result(result_id)
94-
tryout_count += 1
105+
time.sleep(1)
95106

96-
if r.summary.status != "READY":
97-
print("Request timeout after %d tryouts.".format(max_tryout))
107+
if r.summary.status == DetectionStatus.FAILED:
108+
print("Detection failed.")
109+
print("Errors:")
110+
if r.summary.errors:
111+
for error in r.summary.errors:
112+
print("Error code: {}. Message: {}".format(error.code, error.message))
113+
else:
114+
print("None")
98115
return None
99116

100117
except HttpResponseError as e:
@@ -138,10 +155,15 @@ def delete_model(self, model_id):
138155
sample = MultivariateSample(SUBSCRIPTION_KEY, ANOMALY_DETECTOR_ENDPOINT, data_source)
139156

140157
# Train a new model
141-
model_id = sample.train(datetime(2021, 1, 1, 0, 0, 0), datetime(2021, 1, 2, 12, 0, 0))
158+
model_id = sample.train(datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
159+
datetime(2021, 1, 2, 12, 0, 0, tzinfo=timezone.utc))
160+
assert model_id is not None
142161

143162
# Reference
144-
result = sample.detect(model_id, datetime(2021, 1, 2, 12, 0, 0), datetime(2021, 1, 3, 0, 0, 0))
163+
result = sample.detect(model_id, datetime(2021, 1, 2, 12, 0, 0, tzinfo=timezone.utc),
164+
datetime(2021, 1, 3, 0, 0, 0, tzinfo=timezone.utc))
165+
assert result is not None
166+
145167
print("Result ID:\t", result.result_id)
146168
print("Result summary:\t", result.summary)
147169
print("Result length:\t", len(result.results))

0 commit comments

Comments
 (0)