Skip to content

Commit d22f549

Browse files
[Sample] Javascript sample for ai-anomaly-detector (Azure#10934)
* javascript example for anomaly detector * javascript samples * update * update * update package * refactor * prettier format Co-authored-by: [email protected] <[email protected]>
1 parent 734954a commit d22f549

File tree

7 files changed

+303
-101
lines changed

7 files changed

+303
-101
lines changed
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- javascript
5+
products:
6+
- azure
7+
- azure-cognitive-services
8+
urlFragment: ai-anomaly-detector-javascript
9+
---
10+
11+
# Azure Anomaly Detector client library samples for JavaScript
12+
13+
These sample programs show how to use the JavaScript client libraries for Azure Cognitive Services Anomaly Detector in some common scenarios.
14+
15+
| **File Name** | **Description** |
16+
| ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
17+
| [sample_detect_entire_series_anomaly.js][detectentireseriesanomaly] | Detect anomaly for each point of the series |
18+
| [sample_detect_last_point_anomaly.js][detectlastpointanomaly] | Detect anomaly for the last point of the series |
19+
| [sample_detect_change_point.js][detectchangepoint] | Detect change point for each point of the series |
20+
21+
## Prerequisites
22+
23+
The samples are compatible with Node.js >= 8.0.0.
24+
25+
You need [an Azure subscription][freesub] and [an Azure Cognitive Services Instance][azcogsvc] to run these sample programs. Samples retrieve credentials to access the Cognitive Services endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.
26+
27+
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].
28+
29+
## Setup
30+
31+
To run the samples using the published version of the package:
32+
33+
1. Install the dependencies using `npm`:
34+
35+
```bash
36+
npm install
37+
```
38+
39+
2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Not all environment variables are required. Read the relevant sample sources and the `sample.env` file to determine which ones are required. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.
40+
41+
3. Run whichever samples you like:
42+
43+
```bash
44+
node sample_detect_entire_series_anomaly.js
45+
```
46+
47+
[detectentireseriesanomaly]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_entire_series_anomaly.js
48+
[detectlastpointanomaly]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_last_point_anomaly.js
49+
[detectchangepoint]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_change_point.js
50+
[azcogsvc]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account
51+
[freesub]: https://azure.microsoft.com/free/
52+
[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/README.md

sdk/anomalydetector/ai-anomaly-detector/samples/javascript/basic.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

sdk/anomalydetector/ai-anomaly-detector/samples/javascript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector",
2525
"sideEffects": false,
2626
"dependencies": {
27-
"@azure/ai-anomaly-detector": "../..",
28-
"@azure/identity": "latest",
27+
"@azure/ai-anomaly-detector": "latest",
28+
"csv-parse": "^4.4.0",
2929
"dotenv": "^8.2.0"
3030
}
3131
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* Demonstrates how to detect change points on entire series.
6+
*/
7+
8+
const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector");
9+
const { AzureKeyCredential } = require("@azure/core-auth");
10+
const fs = require("fs");
11+
const parse = require("csv-parse/lib/sync");
12+
13+
// Load the .env file if it exists
14+
const dotenv = require("dotenv");
15+
dotenv.config();
16+
17+
// You will need to set this environment variables in .env file or edit the following values
18+
const apiKey = process.env["API_KEY"] || "";
19+
const endpoint = process.env["ENDPOINT"] || "";
20+
const timeSeriesDataPath = "../example-data/request-data.csv";
21+
22+
function read_series_from_file(path) {
23+
let result = Array();
24+
let input = fs.readFileSync(path).toString();
25+
let parsed = parse(input, { skip_empty_lines: true });
26+
parsed.forEach(function(e) {
27+
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
28+
});
29+
return result;
30+
}
31+
32+
async function main() {
33+
// create client
34+
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));
35+
36+
// construct request
37+
const request = {
38+
series: read_series_from_file(timeSeriesDataPath),
39+
granularity: "daily"
40+
};
41+
42+
// get change point detect results
43+
const result = await client.detectChangePoint(request);
44+
45+
if (
46+
result.isChangePoint.some(function(changePoint) {
47+
return changePoint === true;
48+
})
49+
) {
50+
console.log("Change points were detected from the series at index:");
51+
result.isChangePoint.forEach(function(changePoint, index) {
52+
if (changePoint === true) {
53+
console.log(index);
54+
}
55+
});
56+
} else {
57+
console.log("There is no change point detected from the series.");
58+
}
59+
// output:
60+
// Change points were detected from the series at index:
61+
// 20
62+
// 27
63+
}
64+
65+
main().catch((err) => {
66+
console.error("The sample encountered an error:", err);
67+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* Demonstrates how to detect anomaly points on entire series.
6+
*/
7+
8+
const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector");
9+
const { AzureKeyCredential } = require("@azure/core-auth");
10+
const fs = require("fs");
11+
const parse = require("csv-parse/lib/sync");
12+
13+
// Load the .env file if it exists
14+
const dotenv = require("dotenv");
15+
dotenv.config();
16+
17+
// You will need to set this environment variables in .env file or edit the following values
18+
const apiKey = process.env["API_KEY"] || "";
19+
const endpoint = process.env["ENDPOINT"] || "";
20+
const timeSeriesDataPath = "../example-data/request-data.csv";
21+
22+
function read_series_from_file(path) {
23+
let result = Array();
24+
let input = fs.readFileSync(path).toString();
25+
let parsed = parse(input, { skip_empty_lines: true });
26+
parsed.forEach(function(e) {
27+
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
28+
});
29+
return result;
30+
}
31+
32+
async function main() {
33+
// create client
34+
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));
35+
36+
// construct request
37+
const request = {
38+
series: read_series_from_file(timeSeriesDataPath),
39+
granularity: "daily"
40+
};
41+
42+
// get entire detect result
43+
const result = await client.detectEntireSeries(request);
44+
45+
if (
46+
result.isAnomaly.some(function(anomaly) {
47+
return anomaly === true;
48+
})
49+
) {
50+
console.log("Anomalies were detected from the series at index:");
51+
result.isAnomaly.forEach(function(anomaly, index) {
52+
if (anomaly === true) {
53+
console.log(index);
54+
}
55+
});
56+
} else {
57+
console.log("There is no anomaly detected from the series.");
58+
}
59+
// output:
60+
// Anomalies were detected from the series at index:
61+
// 3
62+
// 18
63+
// 21
64+
// 22
65+
// 23
66+
// 24
67+
// 25
68+
// 28
69+
// 29
70+
// 30
71+
// 31
72+
// 32
73+
// 35
74+
// 44
75+
}
76+
77+
main().catch((err) => {
78+
console.error("The sample encountered an error:", err);
79+
});

0 commit comments

Comments
 (0)