Skip to content

Commit 49cc1fe

Browse files
conhuayongxingwumoreOver0
authored
add python sdk sample (#13338)
* add python sample * add change point * update anomaly detector sample code * update readme * update readme for samples * update MANIFEST.in * fix pr Co-authored-by: yongxingwu <[email protected]> Co-authored-by: Xia Zhu <[email protected]>
1 parent 537dd08 commit 49cc1fe

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed

sdk/anomalydetector/azure-ai-anomalydetector/MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
recursive-include tests *.py *.yaml
2+
recursive-include samples *.py *.md
23
include *.md
34
include azure/__init__.py
45
include azure/ai/__init__.py
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
- azure-cognitive-services
8+
- azure-anomaly-detector
9+
urlFragment: anomalydetector-samples
10+
---
11+
12+
# Samples for Azure Anomaly Detector client library for Python
13+
14+
These code samples show common scenario operations with the Anomaly Detector client library.
15+
16+
These sample programs show common scenarios for the Anomaly Detector client's offerings.
17+
18+
|**File Name**|**Description**|
19+
|----------------|-------------|
20+
|[sample_detect_entire_series_anomaly.py][sample_detect_entire_series_anomaly] |Detecting anomalies in the entire time series.|
21+
|[sample_detect_last_point_anomaly.py][sample_detect_last_point_anomaly] |Detecting the anomaly status of the latest data point.|
22+
|[sample_detect_change_point.py][sample_detect_change_point] |Detecting change points in the entire time series.|
23+
24+
## Prerequisites
25+
* Python 2.7 or 3.5 or higher is required to use this package.
26+
* The Pandas data analysis library.
27+
* You must have an [Azure subscription][azure_subscription] and an
28+
[Azure Anomaly Detector account][azure_anomaly_detector_account] to run these samples.
29+
30+
## Setup
31+
32+
1. Install the Azure Anomaly Detector client library for Python with [pip][pip]:
33+
34+
```bash
35+
pip install azure-ai-anomalydetector
36+
```
37+
38+
2. Clone or download this sample repository
39+
3. Open the sample folder in Visual Studio Code or your IDE of choice.
40+
41+
## Running the samples
42+
43+
1. Open a terminal window and `cd` to the directory that the samples are saved in.
44+
2. Set the environment variables specified in the sample file you wish to run.
45+
3. Follow the usage described in the file, e.g. `python sample_detect_entire_series_anomaly.py`
46+
47+
## Next steps
48+
49+
Check out the [API reference documentation][python-fr-ref-docs] to learn more about
50+
what you can do with the Azure Anomaly Detector client library.
51+
52+
[pip]: https://pypi.org/project/pip/
53+
[azure_subscription]: https://azure.microsoft.com/free/cognitive-services
54+
[azure_anomaly_detector_account]: https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesAnomalyDetector
55+
[python-fr-ref-docs]: https://azuresdkdocs.blob.core.windows.net/$web/python/azure-cognitiveservices-anomalydetector/0.3.0/index.html
56+
57+
[sample_detect_entire_series_anomaly]: ./sample_detect_entire_series_anomaly.py
58+
[sample_detect_last_point_anomaly]: ./sample_detect_last_point_anomaly.py
59+
[sample_detect_change_point]: ./sample_detect_change_point.py
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
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
FILE: sample_detect_change_point.py
6+
7+
DESCRIPTION:
8+
This sample demonstrates how to detect entire series change points.
9+
10+
Prerequisites:
11+
* The Anomaly Detector client library for Python
12+
* A .csv file containing a time-series data set with
13+
UTC-timestamp and numerical values pairings.
14+
Example data is included in this repo.
15+
16+
USAGE:
17+
python sample_detect_change_point.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) ANOMALY_DETECTOR_KEY - your source Form Anomaly Detector API key.
21+
2) ANOMALY_DETECTOR_ENDPOINT - the endpoint to your source Anomaly Detector resource.
22+
"""
23+
24+
import os
25+
from azure.ai.anomalydetector import AnomalyDetectorClient
26+
from azure.ai.anomalydetector.models import DetectRequest, TimeSeriesPoint, TimeGranularity, \
27+
AnomalyDetectorError
28+
from azure.core.credentials import AzureKeyCredential
29+
import pandas as pd
30+
31+
32+
class DetectChangePointsSample(object):
33+
34+
def detect_change_point(self):
35+
SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_KEY"]
36+
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
37+
TIME_SERIES_DATA_PATH = os.path.join("./sample_data", "request-data.csv")
38+
39+
# Create an Anomaly Detector client
40+
41+
# <client>
42+
client = AnomalyDetectorClient(AzureKeyCredential(SUBSCRIPTION_KEY), ANOMALY_DETECTOR_ENDPOINT)
43+
# </client>
44+
45+
# Load in the time series data file
46+
47+
# <loadDataFile>
48+
series = []
49+
data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', parse_dates=[0])
50+
for index, row in data_file.iterrows():
51+
series.append(TimeSeriesPoint(timestamp=row[0], value=row[1]))
52+
# </loadDataFile>
53+
54+
# Create a request from the data file
55+
56+
# <request>
57+
request = DetectRequest(series=series, granularity=TimeGranularity.daily)
58+
# </request>
59+
60+
# detect change points throughout the entire time series
61+
62+
# <detectChangePoint>
63+
print('Detecting change points in the entire time series.')
64+
65+
try:
66+
response = client.detect_change_point(request)
67+
except AnomalyDetectorError as e:
68+
print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
69+
except Exception as e:
70+
print(e)
71+
72+
if any(response.is_change_point):
73+
print('An change point was detected at index:')
74+
for i, value in enumerate(response.is_change_point):
75+
if value:
76+
print(i)
77+
else:
78+
print('No change point were detected in the time series.')
79+
# </detectChangePoint>
80+
81+
82+
if __name__ == '__main__':
83+
sample = DetectChangePointsSample()
84+
sample.detect_change_point()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
FILE: sample_detect_entire_series_anomaly.py
6+
7+
DESCRIPTION:
8+
This sample demonstrates how to detect entire series anomalies.
9+
10+
Prerequisites:
11+
* The Anomaly Detector client library for Python
12+
* A .csv file containing a time-series data set with
13+
UTC-timestamp and numerical values pairings.
14+
Example data is included in this repo.
15+
16+
USAGE:
17+
python sample_detect_entire_series_anomaly.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) ANOMALY_DETECTOR_KEY - your source Form Anomaly Detector API key.
21+
2) ANOMALY_DETECTOR_ENDPOINT - the endpoint to your source Anomaly Detector resource.
22+
"""
23+
24+
import os
25+
from azure.ai.anomalydetector import AnomalyDetectorClient
26+
from azure.ai.anomalydetector.models import DetectRequest, TimeSeriesPoint, TimeGranularity, \
27+
AnomalyDetectorError
28+
from azure.core.credentials import AzureKeyCredential
29+
import pandas as pd
30+
31+
32+
class DetectEntireAnomalySample(object):
33+
34+
def detect_entire_series(self):
35+
SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_KEY"]
36+
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
37+
TIME_SERIES_DATA_PATH = os.path.join("./sample_data", "request-data.csv")
38+
39+
# Create an Anomaly Detector client
40+
41+
# <client>
42+
client = AnomalyDetectorClient(AzureKeyCredential(SUBSCRIPTION_KEY), ANOMALY_DETECTOR_ENDPOINT)
43+
# </client>
44+
45+
# Load in the time series data file
46+
47+
# <loadDataFile>
48+
series = []
49+
data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', parse_dates=[0])
50+
for index, row in data_file.iterrows():
51+
series.append(TimeSeriesPoint(timestamp=row[0], value=row[1]))
52+
# </loadDataFile>
53+
54+
# Create a request from the data file
55+
56+
# <request>
57+
request = DetectRequest(series=series, granularity=TimeGranularity.daily)
58+
# </request>
59+
60+
# detect anomalies throughout the entire time series, as a batch
61+
62+
# <detectAnomaliesBatch>
63+
print('Detecting anomalies in the entire time series.')
64+
65+
try:
66+
response = client.detect_entire_series(request)
67+
except AnomalyDetectorError as e:
68+
print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
69+
except Exception as e:
70+
print(e)
71+
72+
if any(response.is_anomaly):
73+
print('An anomaly was detected at index:')
74+
for i, value in enumerate(response.is_anomaly):
75+
if value:
76+
print(i)
77+
else:
78+
print('No anomalies were detected in the time series.')
79+
# </detectAnomaliesBatch>
80+
81+
82+
if __name__ == '__main__':
83+
sample = DetectEntireAnomalySample()
84+
sample.detect_entire_series()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
FILE: sample_detect_last_point_anomaly.py
6+
7+
DESCRIPTION:
8+
This sample demonstrates how to detect last series point whether is anomaly.
9+
10+
Prerequisites:
11+
* The Anomaly Detector client library for Python
12+
* A .csv file containing a time-series data set with
13+
UTC-timestamp and numerical values pairings.
14+
Example data is included in this repo.
15+
16+
USAGE:
17+
python sample_detect_last_point_anomaly.py
18+
19+
Set the environment variables with your own values before running the sample:
20+
1) ANOMALY_DETECTOR_KEY - your source Form Anomaly Detector API key.
21+
2) ANOMALY_DETECTOR_ENDPOINT - the endpoint to your source Anomaly Detector resource.
22+
"""
23+
24+
import os
25+
from azure.ai.anomalydetector import AnomalyDetectorClient
26+
from azure.ai.anomalydetector.models import DetectRequest, TimeSeriesPoint, TimeGranularity, \
27+
AnomalyDetectorError
28+
from azure.core.credentials import AzureKeyCredential
29+
import pandas as pd
30+
31+
32+
class DetectLastAnomalySample(object):
33+
34+
def detect_last_point(self):
35+
SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_KEY"]
36+
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
37+
TIME_SERIES_DATA_PATH = os.path.join("./sample_data", "request-data.csv")
38+
39+
# Create an Anomaly Detector client
40+
41+
# <client>
42+
client = AnomalyDetectorClient(AzureKeyCredential(SUBSCRIPTION_KEY), ANOMALY_DETECTOR_ENDPOINT)
43+
# </client>
44+
45+
# Load in the time series data file
46+
47+
# <loadDataFile>
48+
series = []
49+
data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', parse_dates=[0])
50+
for index, row in data_file.iterrows():
51+
series.append(TimeSeriesPoint(timestamp=row[0], value=row[1]))
52+
# </loadDataFile>
53+
54+
# Create a request from the data file
55+
56+
# <request>
57+
request = DetectRequest(series=series, granularity=TimeGranularity.daily)
58+
# </request>
59+
60+
# Detect the anomaly status of the latest data point
61+
62+
# <latestPointDetection>
63+
print('Detecting the anomaly status of the latest data point.')
64+
65+
try:
66+
response = client.detect_last_point(request)
67+
except AnomalyDetectorError as e:
68+
print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
69+
except Exception as e:
70+
print(e)
71+
72+
if response.is_anomaly:
73+
print('The latest point is detected as anomaly.')
74+
else:
75+
print('The latest point is not detected as anomaly.')
76+
# </latestPointDetection>
77+
78+
79+
if __name__ == '__main__':
80+
sample = DetectLastAnomalySample()
81+
sample.detect_last_point()

0 commit comments

Comments
 (0)