Skip to content

Commit 2771a16

Browse files
moreOver0lmazuel
authored andcommitted
add AnomalyDetector sample (#40)
* init * add anomaly detector sample
1 parent 5e79ac6 commit 2771a16

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ azure-cognitiveservices-vision-computervision>=0.3.0 # sample won't work with p
1515
azure-cognitiveservices-vision-contentmoderator>=1.0.0 # sample won't work with previous versions
1616
azure-cognitiveservices-vision-customvision>=0.4.0 # sample won't work with previous versions
1717
azure-cognitiveservices-vision-face
18+
azure-cognitiveservices-anomalydetector>=0.2.0 # sample won't work with previous versions
19+
pandas
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from azure.cognitiveservices.anomalydetector import AnomalyDetectorClient
2+
from azure.cognitiveservices.anomalydetector.models import Request, Point, Granularity, \
3+
APIErrorException
4+
from datetime import datetime, timezone
5+
from msrest.authentication import CognitiveServicesCredentials
6+
import pandas as pd
7+
import os
8+
9+
SUBSCRIPTION_KEY_ENV_NAME = "ANOMALYDETECTOR_SUBSCRIPTION_KEY"
10+
ANOMALYDETECTOR_LOCATION = os.environ.get("ANOMALYDETECTOR_LOCATION", "westcentralus")
11+
12+
CSV_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csv_files")
13+
14+
def get_series_from_file(path):
15+
df = pd.read_csv(path, header=None, encoding="utf-8", parse_dates=[0])
16+
series = []
17+
for index, row in df.iterrows():
18+
series.append(Point(timestamp=row[0], value=row[1]))
19+
return series
20+
21+
def get_request():
22+
series = get_series_from_file(os.path.join(CSV_FOLDER, "anomaly_detector_daily_series.csv"))
23+
return Request(series=series, granularity=Granularity.daily)
24+
25+
def entire_detect(subscription_key):
26+
print("Sample of detecting anomalies in the entire series.")
27+
28+
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
29+
try:
30+
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
31+
request = get_request()
32+
response = client.entire_detect(request)
33+
if True in response.is_anomaly:
34+
print("Anomaly was detected from the series at index:")
35+
for i in range(len(request.series)):
36+
if response.is_anomaly[i]:
37+
print(i)
38+
else:
39+
print("There is no anomaly detected from the series.")
40+
except Exception as e:
41+
if isinstance(e, APIErrorException):
42+
print("Error code: {}".format(e.error.code))
43+
print("Error message: {}".format(e.error.message))
44+
else:
45+
print(e)
46+
47+
def last_detect(subscription_key):
48+
print("Sample of detecting whether the latest point in series is anomaly.")
49+
50+
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
51+
try:
52+
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
53+
request = get_request()
54+
response = client.last_detect(request)
55+
if response.is_anomaly:
56+
print("The latest point is detected as anomaly.")
57+
else:
58+
print("The latest point is not detected as anomaly.")
59+
except Exception as e:
60+
if isinstance(e, APIErrorException):
61+
print("Error code: {}".format(e.error.code))
62+
print("Error message: {}".format(e.error.message))
63+
else:
64+
print(e)
65+
66+
if __name__ == "__main__":
67+
import sys, os.path
68+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
69+
from tools import execute_samples
70+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2018-03-01T00:00:00Z,32858923
2+
2018-03-02T00:00:00Z,29615278
3+
2018-03-03T00:00:00Z,22839355
4+
2018-03-04T00:00:00Z,25948736
5+
2018-03-05T00:00:00Z,34139159
6+
2018-03-06T00:00:00Z,33843985
7+
2018-03-07T00:00:00Z,33637661
8+
2018-03-08T00:00:00Z,32627350
9+
2018-03-09T00:00:00Z,29881076
10+
2018-03-10T00:00:00Z,22681575
11+
2018-03-11T00:00:00Z,24629393
12+
2018-03-12T00:00:00Z,34010679
13+
2018-03-13T00:00:00Z,33893888
14+
2018-03-14T00:00:00Z,33760076
15+
2018-03-15T00:00:00Z,33093515
16+
2018-03-16T00:00:00Z,29945555
17+
2018-03-17T00:00:00Z,22676212
18+
2018-03-18T00:00:00Z,25262514
19+
2018-03-19T00:00:00Z,33631649
20+
2018-03-20T00:00:00Z,34468310
21+
2018-03-21T00:00:00Z,34212281
22+
2018-03-22T00:00:00Z,38144434
23+
2018-03-23T00:00:00Z,34662949
24+
2018-03-24T00:00:00Z,24623684
25+
2018-03-25T00:00:00Z,26530491
26+
2018-03-26T00:00:00Z,35445003
27+
2018-03-27T00:00:00Z,34250789
28+
2018-03-28T00:00:00Z,33423012
29+
2018-03-29T00:00:00Z,30744783
30+
2018-03-30T00:00:00Z,25825128
31+
2018-03-31T00:00:00Z,21244209
32+
2018-04-01T00:00:00Z,22576956
33+
2018-04-02T00:00:00Z,31957221
34+
2018-04-03T00:00:00Z,33841228
35+
2018-04-04T00:00:00Z,33554483
36+
2018-04-05T00:00:00Z,32383350
37+
2018-04-06T00:00:00Z,29494850
38+
2018-04-07T00:00:00Z,22815534
39+
2018-04-08T00:00:00Z,25557267
40+
2018-04-09T00:00:00Z,34858252
41+
2018-04-10T00:00:00Z,34750597
42+
2018-04-11T00:00:00Z,34717956
43+
2018-04-12T00:00:00Z,34132534
44+
2018-04-13T00:00:00Z,30762236
45+
2018-04-14T00:00:00Z,22504059
46+
2018-04-15T00:00:00Z,26149060
47+
2018-04-16T00:00:00Z,35250105

0 commit comments

Comments
 (0)