forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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]>
- Loading branch information
1 parent
734954a
commit d22f549
Showing
7 changed files
with
303 additions
and
101 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
sdk/anomalydetector/ai-anomaly-detector/samples/example-data/request-data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
2018-03-01T00:00:00Z,32858923 | ||
2018-03-02T00:00:00Z,29615278 | ||
2018-03-03T00:00:00Z,22839355 | ||
2018-03-04T00:00:00Z,25948736 | ||
2018-03-05T00:00:00Z,34139159 | ||
2018-03-06T00:00:00Z,33843985 | ||
2018-03-07T00:00:00Z,33637661 | ||
2018-03-08T00:00:00Z,32627350 | ||
2018-03-09T00:00:00Z,29881076 | ||
2018-03-10T00:00:00Z,22681575 | ||
2018-03-11T00:00:00Z,24629393 | ||
2018-03-12T00:00:00Z,34010679 | ||
2018-03-13T00:00:00Z,33893888 | ||
2018-03-14T00:00:00Z,33760076 | ||
2018-03-15T00:00:00Z,33093515 | ||
2018-03-16T00:00:00Z,29945555 | ||
2018-03-17T00:00:00Z,22676212 | ||
2018-03-18T00:00:00Z,25262514 | ||
2018-03-19T00:00:00Z,33631649 | ||
2018-03-20T00:00:00Z,34468310 | ||
2018-03-21T00:00:00Z,34212281 | ||
2018-03-22T00:00:00Z,38144434 | ||
2018-03-23T00:00:00Z,34662949 | ||
2018-03-24T00:00:00Z,24623684 | ||
2018-03-25T00:00:00Z,26530491 | ||
2018-03-26T00:00:00Z,35445003 | ||
2018-03-27T00:00:00Z,34250789 | ||
2018-03-28T00:00:00Z,33423012 | ||
2018-03-29T00:00:00Z,30744783 | ||
2018-03-30T00:00:00Z,25825128 | ||
2018-03-31T00:00:00Z,21244209 | ||
2018-04-01T00:00:00Z,22576956 | ||
2018-04-02T00:00:00Z,31957221 | ||
2018-04-03T00:00:00Z,33841228 | ||
2018-04-04T00:00:00Z,33554483 | ||
2018-04-05T00:00:00Z,32383350 | ||
2018-04-06T00:00:00Z,29494850 | ||
2018-04-07T00:00:00Z,22815534 | ||
2018-04-08T00:00:00Z,25557267 | ||
2018-04-09T00:00:00Z,34858252 | ||
2018-04-10T00:00:00Z,34750597 | ||
2018-04-11T00:00:00Z,34717956 | ||
2018-04-12T00:00:00Z,34132534 | ||
2018-04-13T00:00:00Z,30762236 | ||
2018-04-14T00:00:00Z,22504059 | ||
2018-04-15T00:00:00Z,26149060 | ||
2018-04-16T00:00:00Z,35250105 |
52 changes: 52 additions & 0 deletions
52
sdk/anomalydetector/ai-anomaly-detector/samples/javascript/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- javascript | ||
products: | ||
- azure | ||
- azure-cognitive-services | ||
urlFragment: ai-anomaly-detector-javascript | ||
--- | ||
|
||
# Azure Anomaly Detector client library samples for JavaScript | ||
|
||
These sample programs show how to use the JavaScript client libraries for Azure Cognitive Services Anomaly Detector in some common scenarios. | ||
|
||
| **File Name** | **Description** | | ||
| ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | | ||
| [sample_detect_entire_series_anomaly.js][detectentireseriesanomaly] | Detect anomaly for each point of the series | | ||
| [sample_detect_last_point_anomaly.js][detectlastpointanomaly] | Detect anomaly for the last point of the series | | ||
| [sample_detect_change_point.js][detectchangepoint] | Detect change point for each point of the series | | ||
|
||
## Prerequisites | ||
|
||
The samples are compatible with Node.js >= 8.0.0. | ||
|
||
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. | ||
|
||
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. | ||
|
||
## Setup | ||
|
||
To run the samples using the published version of the package: | ||
|
||
1. Install the dependencies using `npm`: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
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. | ||
|
||
3. Run whichever samples you like: | ||
|
||
```bash | ||
node sample_detect_entire_series_anomaly.js | ||
``` | ||
|
||
[detectentireseriesanomaly]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_entire_series_anomaly.js | ||
[detectlastpointanomaly]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_last_point_anomaly.js | ||
[detectchangepoint]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_change_point.js | ||
[azcogsvc]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account | ||
[freesub]: https://azure.microsoft.com/free/ | ||
[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/anomalydetector/ai-anomaly-detector/README.md |
99 changes: 0 additions & 99 deletions
99
sdk/anomalydetector/ai-anomaly-detector/samples/javascript/basic.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sdk/anomalydetector/ai-anomaly-detector/samples/javascript/sample_detect_change_point.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Demonstrates how to detect change points on entire series. | ||
*/ | ||
|
||
const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector"); | ||
const { AzureKeyCredential } = require("@azure/core-auth"); | ||
const fs = require("fs"); | ||
const parse = require("csv-parse/lib/sync"); | ||
|
||
// Load the .env file if it exists | ||
const dotenv = require("dotenv"); | ||
dotenv.config(); | ||
|
||
// You will need to set this environment variables in .env file or edit the following values | ||
const apiKey = process.env["API_KEY"] || ""; | ||
const endpoint = process.env["ENDPOINT"] || ""; | ||
const timeSeriesDataPath = "../example-data/request-data.csv"; | ||
|
||
function read_series_from_file(path) { | ||
let result = Array(); | ||
let input = fs.readFileSync(path).toString(); | ||
let parsed = parse(input, { skip_empty_lines: true }); | ||
parsed.forEach(function(e) { | ||
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) }); | ||
}); | ||
return result; | ||
} | ||
|
||
async function main() { | ||
// create client | ||
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey)); | ||
|
||
// construct request | ||
const request = { | ||
series: read_series_from_file(timeSeriesDataPath), | ||
granularity: "daily" | ||
}; | ||
|
||
// get change point detect results | ||
const result = await client.detectChangePoint(request); | ||
|
||
if ( | ||
result.isChangePoint.some(function(changePoint) { | ||
return changePoint === true; | ||
}) | ||
) { | ||
console.log("Change points were detected from the series at index:"); | ||
result.isChangePoint.forEach(function(changePoint, index) { | ||
if (changePoint === true) { | ||
console.log(index); | ||
} | ||
}); | ||
} else { | ||
console.log("There is no change point detected from the series."); | ||
} | ||
// output: | ||
// Change points were detected from the series at index: | ||
// 20 | ||
// 27 | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
79 changes: 79 additions & 0 deletions
79
...alydetector/ai-anomaly-detector/samples/javascript/sample_detect_entire_series_anomaly.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Demonstrates how to detect anomaly points on entire series. | ||
*/ | ||
|
||
const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector"); | ||
const { AzureKeyCredential } = require("@azure/core-auth"); | ||
const fs = require("fs"); | ||
const parse = require("csv-parse/lib/sync"); | ||
|
||
// Load the .env file if it exists | ||
const dotenv = require("dotenv"); | ||
dotenv.config(); | ||
|
||
// You will need to set this environment variables in .env file or edit the following values | ||
const apiKey = process.env["API_KEY"] || ""; | ||
const endpoint = process.env["ENDPOINT"] || ""; | ||
const timeSeriesDataPath = "../example-data/request-data.csv"; | ||
|
||
function read_series_from_file(path) { | ||
let result = Array(); | ||
let input = fs.readFileSync(path).toString(); | ||
let parsed = parse(input, { skip_empty_lines: true }); | ||
parsed.forEach(function(e) { | ||
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) }); | ||
}); | ||
return result; | ||
} | ||
|
||
async function main() { | ||
// create client | ||
const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey)); | ||
|
||
// construct request | ||
const request = { | ||
series: read_series_from_file(timeSeriesDataPath), | ||
granularity: "daily" | ||
}; | ||
|
||
// get entire detect result | ||
const result = await client.detectEntireSeries(request); | ||
|
||
if ( | ||
result.isAnomaly.some(function(anomaly) { | ||
return anomaly === true; | ||
}) | ||
) { | ||
console.log("Anomalies were detected from the series at index:"); | ||
result.isAnomaly.forEach(function(anomaly, index) { | ||
if (anomaly === true) { | ||
console.log(index); | ||
} | ||
}); | ||
} else { | ||
console.log("There is no anomaly detected from the series."); | ||
} | ||
// output: | ||
// Anomalies were detected from the series at index: | ||
// 3 | ||
// 18 | ||
// 21 | ||
// 22 | ||
// 23 | ||
// 24 | ||
// 25 | ||
// 28 | ||
// 29 | ||
// 30 | ||
// 31 | ||
// 32 | ||
// 35 | ||
// 44 | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
Oops, something went wrong.