From 223b0b558a9f1ada80c41e30d6e3823c055db8ef Mon Sep 17 00:00:00 2001 From: conhua Date: Wed, 26 Aug 2020 18:10:18 +0800 Subject: [PATCH 01/10] add .net samples --- .../samples/csharp-sdk-sample.cs | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs new file mode 100644 index 000000000000..82e967a478fb --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs @@ -0,0 +1,150 @@ +/* +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the MIT License. + + +This sample demonstrates the Anomaly Detection service's two detection methods: + * Anomaly detection on an entire time-series dataset. + * Anomaly detection on the latest data point in a dataset. + + * Prerequisites: + * The Anomaly Detector client library for .NET + * A .csv file containing a time-series data set with + UTC-timestamp and numerical values pairings. + Example data is included in this repo. +*/ + +namespace AnomalyDetectorSample +{ + // + using System; + using System.IO; + using System.Text; + using System.Linq; + using System.Collections.Generic; + using System.Threading.Tasks; + using Microsoft.Azure.CognitiveServices.AnomalyDetector; + using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models; + // + + class Program{ + + // + static void Main(string[] args){ + //This sample assumes you have created an environment variable for your key and endpoint + string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); + string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY"); + string datapath = "request-data.csv"; + + IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client + + Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file + + EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection + LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set + + Console.WriteLine("\nPress ENTER to exit."); + Console.ReadLine(); + } + // + + // + static IAnomalyDetectorClient createClient(string endpoint, string key) + { + IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key)) + { + Endpoint = endpoint + }; + return client; + } + // + + // + //Run the anomaly detection examples with extra error handling + static void runSamples(IAnomalyDetectorClient client, string dataPath) + { + + try + { + List series = GetSeriesFromFile(dataPath); + Request request = new Request(series, Granularity.Daily); + + EntireDetectSampleAsync(client, request).Wait(); + LastDetectSampleAsync(client, request).Wait(); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + if (e.InnerException != null && e.InnerException is APIErrorException) + { + APIError error = ((APIErrorException)e.InnerException).Body; + Console.WriteLine("Error code: " + error.Code); + Console.WriteLine("Error message: " + error.Message); + } + else if (e.InnerException != null) + { + Console.WriteLine(e.InnerException.Message); + } + } + } + // + + // + static Request GetSeriesFromFile(string path) + { + List list = File.ReadAllLines(path, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList(); + + return new Request(list, Granularity.Daily); + } + // + + // + static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request) + { + Console.WriteLine("Detecting anomalies in the entire time series."); + + EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) + { + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsAnomaly[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine(" No anomalies detected in the series."); + } + } + // + + // + static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request) + { + + Console.WriteLine("Detecting the anomaly status of the latest point in the series."); + LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) + { + Console.WriteLine("The latest point was detected as an anomaly."); + } + else + { + Console.WriteLine("The latest point was not detected as an anomaly."); + } + } + // + } +} From ed3d94f67495f6a2034d97e09e3a88890333aa07 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Wed, 26 Aug 2020 19:07:54 +0800 Subject: [PATCH 02/10] Update .NET sdk sample --- .../samples/csharp-sdk-sample.cs | 127 +++++++++--------- .../samples/request-data.csv | 47 +++++++ 2 files changed, 109 insertions(+), 65 deletions(-) create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs index 82e967a478fb..edbb99fab089 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs @@ -17,26 +17,28 @@ Example data is included in this repo. namespace AnomalyDetectorSample { // + using Azure; + using Azure.AI.AnomalyDetector; + using Azure.AI.AnomalyDetector.Models; using System; + using System.Collections.Generic; using System.IO; - using System.Text; using System.Linq; - using System.Collections.Generic; + using System.Text; using System.Threading.Tasks; - using Microsoft.Azure.CognitiveServices.AnomalyDetector; - using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models; // - class Program{ - + class Program + { // - static void Main(string[] args){ + static void Main(string[] args) + { //This sample assumes you have created an environment variable for your key and endpoint string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY"); string datapath = "request-data.csv"; - IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client + AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file @@ -49,45 +51,16 @@ static void Main(string[] args){ // // - static IAnomalyDetectorClient createClient(string endpoint, string key) - { - IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key)) - { - Endpoint = endpoint - }; - return client; - } - // - - // - //Run the anomaly detection examples with extra error handling - static void runSamples(IAnomalyDetectorClient client, string dataPath) + static AnomalyDetectorClient createClient(string endpoint, string key) { + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(key); - try - { - List series = GetSeriesFromFile(dataPath); - Request request = new Request(series, Granularity.Daily); + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); - EntireDetectSampleAsync(client, request).Wait(); - LastDetectSampleAsync(client, request).Wait(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - if (e.InnerException != null && e.InnerException is APIErrorException) - { - APIError error = ((APIErrorException)e.InnerException).Body; - Console.WriteLine("Error code: " + error.Code); - Console.WriteLine("Error message: " + error.Message); - } - else if (e.InnerException != null) - { - Console.WriteLine(e.InnerException.Message); - } - } + return client; } - // + // // static Request GetSeriesFromFile(string path) @@ -96,53 +69,77 @@ static Request GetSeriesFromFile(string path) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList(); - - return new Request(list, Granularity.Daily); + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + return new Request(list, Granularity.Daily); } // // - static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request) + static async Task EntireDetectSampleAsync(AnomalyDetectorClient client, Request request) { Console.WriteLine("Detecting anomalies in the entire time series."); - EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly.Contains(true)) + try { - Console.WriteLine("An anomaly was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) + EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) { - if (result.IsAnomaly[i]) + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) { - Console.Write(i); - Console.Write(" "); + if (result.IsAnomaly[i]) + { + Console.Write(i); + Console.Write(" "); + } } + Console.WriteLine(); } - Console.WriteLine(); + else + { + Console.WriteLine(" No anomalies detected in the series."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); } - else + catch (Exception ex) { - Console.WriteLine(" No anomalies detected in the series."); + Console.WriteLine(ex.Message); } } // // - static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request) + static async Task LastDetectSampleAsync(AnomalyDetectorClient client, Request request) { - Console.WriteLine("Detecting the anomaly status of the latest point in the series."); - LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); - if (result.IsAnomaly) + try + { + LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) + { + Console.WriteLine("The latest point was detected as an anomaly."); + } + else + { + Console.WriteLine("The latest point was not detected as an anomaly."); + } + } + catch (RequestFailedException ex) { - Console.WriteLine("The latest point was detected as an anomaly."); + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); } - else + catch (Exception ex) { - Console.WriteLine("The latest point was not detected as an anomaly."); + Console.WriteLine(ex.Message); } } // diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv new file mode 100644 index 000000000000..7f242c3712c1 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv @@ -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 \ No newline at end of file From 40e8721099c91c133d370639bddbd2f234883164 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Fri, 28 Aug 2020 21:39:40 +0800 Subject: [PATCH 03/10] Upate sample files and readme --- .../samples/README.md | 49 ++++++ .../Sample1_DetectEntireSeriesAnomaly.cs | 96 ++++++++++++ .../samples/Sample2_DetectLastPointAnomaly.cs | 87 +++++++++++ .../samples/Sample3_DetectChangePoint.cs | 96 ++++++++++++ .../samples/csharp-sdk-sample.cs | 147 ------------------ .../samples/{ => data}/request-data.csv | 0 6 files changed, 328 insertions(+), 147 deletions(-) create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs rename sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/{ => data}/request-data.csv (100%) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md new file mode 100644 index 000000000000..9fe409451b72 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md @@ -0,0 +1,49 @@ +--- +page_type: sample +languages: +- csharp +products: +- azure +- azure-cognitive-services +- azure-anomaly-detector +name: Azure Anomaly Detector samples for .NET +description: Samples for the Azure.AI.AnomalyDetector client library +--- + +# Azure Anomaly Detector client SDK Samples +These code samples show common scenario operations with the Anomaly Detector client library. + +|**File Name**|**Description**| +|----------------|-------------| +|[Sample1_DetectEntireSeriesAnomaly.cs][sample_detect_entire_series_anomaly] |Detecting anomalies in the entire time series.| +|[Sample2_DetectLastPointAnomaly.cs][sample_detect_last_point_anomaly] |Detecting the anomaly status of the latest data point.| +|[Sample3_DetectChangePoint.cs][sample_detect_change_point] |Detecting change points in the entire time series.| + +## Prerequisites +* Azure subscription - [Create one for free][azure_subscription]. +* The current version of [.NET Core][dotnet_core]. +* Once you have your Azure subscription, [create an Anomaly Detector resource][create_anomaly_detector_resource] in the Azure portal to get your key and endpoint. Wait for it to deploy and click the **Go to resource** button. You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production. You will need the key and endpoint from the resource you create to connect your application to the Anomaly Detector API. + +## Setting up +### Create two environment variables for authentication: +* ANOMALY_DETECTOR_ENDPOINT - The resource endpoint for sending API requests. You will get it from your Anomaly Detector resource. +* ANOMALY_DETECTOR_API_KEY - The api key for authenticating your requests. You will get it from your Anomaly Detector resource. + +### Install the client library for your project: +dotnet add package Microsoft.Azure.CognitiveServices.AnomalyDetector --version 3.0.0-preview.1 + +### Sample data +Copy the [sample data][data] to proper location. + + + + + +[azure_subscription]: https://azure.microsoft.com/free/cognitive-services +[dotnet_core]: https://dotnet.microsoft.com/download/dotnet-core +[create_anomaly_detector_resource]: https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesAnomalyDetector + +[sample_detect_entire_series_anomaly]: ./Sample1_DetectEntireSeriesAnomaly.cs +[sample_detect_last_point_anomaly]: ./Sample2_DetectLastPointAnomaly.cs +[sample_detect_change_point]: ./Sample3_DetectChangePoint.cs +[data]: ./data \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs new file mode 100644 index 000000000000..ce900488b087 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs @@ -0,0 +1,96 @@ +using Azure; +using Azure.AI.AnomalyDetector; +using Azure.AI.AnomalyDetector.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +class Sample1_DetectEntireSeriesAnomaly +{ + // + static void Main(string[] args) + { + //This sample assumes you have created an environment variable for your key and endpoint + string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); + string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data","request-data.csv"); + + AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client + + Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file + + EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection + + Console.WriteLine("\nPress ENTER to exit."); + Console.ReadLine(); + } + // + + // + static AnomalyDetectorClient createClient(string endpoint, string key) + { + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(key); + + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + return client; + } + // + + // + static Request GetSeriesFromFile(string path) + { + List list = File.ReadAllLines(path, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + return new Request(list, Granularity.Daily); + } + // + + // + static async Task EntireDetectSampleAsync(AnomalyDetectorClient client, Request request) + { + Console.WriteLine("Detecting anomalies in the entire time series."); + + try + { + EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) + { + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsAnomaly[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine(" No anomalies detected in the series."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + // +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs new file mode 100644 index 000000000000..a3afc2a54842 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs @@ -0,0 +1,87 @@ +using Azure; +using Azure.AI.AnomalyDetector; +using Azure.AI.AnomalyDetector.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +class Sample2_DetectLastPointAnomaly +{ + // + static void Main(string[] args) + { + //This sample assumes you have created an environment variable for your key and endpoint + string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); + string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data", "request-data.csv"); + + AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client + + Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file + + LastDetectSampleAsync(client, request).Wait(); // Async method for last anomaly detection + + Console.WriteLine("\nPress ENTER to exit."); + Console.ReadLine(); + } + // + + // + static AnomalyDetectorClient createClient(string endpoint, string key) + { + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(key); + + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + return client; + } + // + + // + static Request GetSeriesFromFile(string path) + { + List list = File.ReadAllLines(path, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + return new Request(list, Granularity.Daily); + } + // + + // + static async Task LastDetectSampleAsync(AnomalyDetectorClient client, Request request) + { + Console.WriteLine("Detecting the anomaly status of the latest point in the series."); + + try + { + LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) + { + Console.WriteLine("The latest point was detected as an anomaly."); + } + else + { + Console.WriteLine("The latest point was not detected as an anomaly."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + // +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs new file mode 100644 index 000000000000..74d6fbe5320e --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs @@ -0,0 +1,96 @@ +using Azure; +using Azure.AI.AnomalyDetector; +using Azure.AI.AnomalyDetector.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +class Sample3_DetectChangePoint +{ + // + static void Main(string[] args) + { + //This sample assumes you have created an environment variable for your key and endpoint + string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); + string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data", "request-data.csv"); + + AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client + + ChangePointDetectRequest request = GetSeriesFromFile(datapath); // The request payload with points from the data file + + ChangePointDetectSampleAsync(client, request).Wait(); // Async method for change point detection + + Console.WriteLine("\nPress ENTER to exit."); + Console.ReadLine(); + } + // + + // + static AnomalyDetectorClient createClient(string endpoint, string key) + { + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(key); + + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + return client; + } + // + + // + static ChangePointDetectRequest GetSeriesFromFile(string path) + { + List list = File.ReadAllLines(path, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + return new ChangePointDetectRequest(list, Granularity.Daily); + } + // + + // + static async Task ChangePointDetectSampleAsync(AnomalyDetectorClient client, ChangePointDetectRequest request) + { + Console.WriteLine("Detecting the change point in the series."); + + try + { + ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); + + if (result.IsChangePoint.Contains(true)) + { + Console.WriteLine("A change point was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsChangePoint[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine("No change point detected in the series."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + // +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs deleted file mode 100644 index edbb99fab089..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/csharp-sdk-sample.cs +++ /dev/null @@ -1,147 +0,0 @@ -/* -Copyright (c) Microsoft Corporation. All rights reserved. -Licensed under the MIT License. - - -This sample demonstrates the Anomaly Detection service's two detection methods: - * Anomaly detection on an entire time-series dataset. - * Anomaly detection on the latest data point in a dataset. - - * Prerequisites: - * The Anomaly Detector client library for .NET - * A .csv file containing a time-series data set with - UTC-timestamp and numerical values pairings. - Example data is included in this repo. -*/ - -namespace AnomalyDetectorSample -{ - // - using Azure; - using Azure.AI.AnomalyDetector; - using Azure.AI.AnomalyDetector.Models; - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Text; - using System.Threading.Tasks; - // - - class Program - { - // - static void Main(string[] args) - { - //This sample assumes you have created an environment variable for your key and endpoint - string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); - string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY"); - string datapath = "request-data.csv"; - - AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client - - Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file - - EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection - LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set - - Console.WriteLine("\nPress ENTER to exit."); - Console.ReadLine(); - } - // - - // - static AnomalyDetectorClient createClient(string endpoint, string key) - { - var endpointUri = new Uri(endpoint); - var credential = new AzureKeyCredential(key); - - AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); - - return client; - } - // - - // - static Request GetSeriesFromFile(string path) - { - List list = File.ReadAllLines(path, Encoding.UTF8) - .Where(e => e.Trim().Length != 0) - .Select(e => e.Split(',')) - .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); - - return new Request(list, Granularity.Daily); - } - // - - // - static async Task EntireDetectSampleAsync(AnomalyDetectorClient client, Request request) - { - Console.WriteLine("Detecting anomalies in the entire time series."); - - try - { - EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly.Contains(true)) - { - Console.WriteLine("An anomaly was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) - { - if (result.IsAnomaly[i]) - { - Console.Write(i); - Console.Write(" "); - } - } - Console.WriteLine(); - } - else - { - Console.WriteLine(" No anomalies detected in the series."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - // - - // - static async Task LastDetectSampleAsync(AnomalyDetectorClient client, Request request) - { - Console.WriteLine("Detecting the anomaly status of the latest point in the series."); - - try - { - LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly) - { - Console.WriteLine("The latest point was detected as an anomaly."); - } - else - { - Console.WriteLine("The latest point was not detected as an anomaly."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - // - } -} diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv similarity index 100% rename from sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/request-data.csv rename to sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv From 4b60995fcb16fb5ba42e20cbf9475a73823caf8a Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Thu, 3 Sep 2020 00:19:40 +0800 Subject: [PATCH 04/10] Move samples to tests --- .../Azure.AI.AnomalyDetector.Tests.csproj | 8 ++ .../Sample1_DetectEntireSeriesAnomaly.cs | 79 +++++++++++++++++++ .../samples/Sample2_DetectLastPointAnomaly.cs | 70 ++++++++++++++++ .../samples/Sample3_DetectChangePoint.cs | 79 +++++++++++++++++++ .../tests/samples/data/request-data.csv | 47 +++++++++++ 5 files changed, 283 insertions(+) create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/Azure.AI.AnomalyDetector.Tests.csproj b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/Azure.AI.AnomalyDetector.Tests.csproj index e3b5520269f2..3132dd048d97 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/Azure.AI.AnomalyDetector.Tests.csproj +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/Azure.AI.AnomalyDetector.Tests.csproj @@ -11,8 +11,16 @@ + + + + + + + + diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs new file mode 100644 index 000000000000..2079f14656ba --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs @@ -0,0 +1,79 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Azure.AI.AnomalyDetector.Models; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.AnomalyDetector.Tests.Samples +{ + public partial class AnomalyDetectorSamples : SamplesBase + { + [Test] + public async Task DetectEntireSeriesAnomaly() + { + //read endpoint and apiKey + string endpoint = TestEnvironment.Endpoint; + string apiKey = TestEnvironment.ApiKey; + + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(apiKey); + + //create client + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + //read data + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); + + List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + //create request + Request request = new Request(list, Granularity.Daily); + + //detect + Console.WriteLine("Detecting anomalies in the entire time series."); + try + { + EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) + { + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsAnomaly[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine(" No anomalies detected in the series."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } +} diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs new file mode 100644 index 000000000000..f3544a410a5b --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Azure.AI.AnomalyDetector.Models; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.AnomalyDetector.Tests.Samples +{ + public partial class AnomalyDetectorSamples : SamplesBase + { + [Test] + public async Task DetectLastPointAnomaly() + { + //read endpoint and apiKey + string endpoint = TestEnvironment.Endpoint; + string apiKey = TestEnvironment.ApiKey; + + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(apiKey); + + //create client + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + //read data + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); + + List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + //create request + Request request = new Request(list, Granularity.Daily); + + //detect + Console.WriteLine("Detecting the anomaly status of the latest point in the series."); + try + { + LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) + { + Console.WriteLine("The latest point was detected as an anomaly."); + } + else + { + Console.WriteLine("The latest point was not detected as an anomaly."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } +} diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs new file mode 100644 index 000000000000..4b68923becc6 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs @@ -0,0 +1,79 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Azure.AI.AnomalyDetector.Models; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.AnomalyDetector.Tests.Samples +{ + public partial class AnomalyDetectorSamples : SamplesBase + { + [Test] + public async Task DetectChangePoint() + { + //read endpoint and apiKey + string endpoint = TestEnvironment.Endpoint; + string apiKey = TestEnvironment.ApiKey; + + var endpointUri = new Uri(endpoint); + var credential = new AzureKeyCredential(apiKey); + + //create client + AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + + //read data + string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); + + List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + + //create request + ChangePointDetectRequest request = new ChangePointDetectRequest(list, Granularity.Daily); + + //detect + Console.WriteLine("Detecting the change point in the series."); + try + { + ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); + + if (result.IsChangePoint.Contains(true)) + { + Console.WriteLine("A change point was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsChangePoint[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine("No change point detected in the series."); + } + } + catch (RequestFailedException ex) + { + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } +} diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv new file mode 100644 index 000000000000..7f242c3712c1 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv @@ -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 \ No newline at end of file From c053b2f01eb4e825efa519962670fb73aa45cf77 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Thu, 3 Sep 2020 22:44:39 +0800 Subject: [PATCH 05/10] Add md files --- .../samples/README.md | 39 ++------ .../Sample1_DetectEntireSeriesAnomaly.cs | 96 ------------------- .../Sample1_DetectEntireSeriesAnomaly.md | 84 ++++++++++++++++ .../samples/Sample2_DetectLastPointAnomaly.cs | 87 ----------------- .../samples/Sample2_DetectLastPointAnomaly.md | 75 +++++++++++++++ .../samples/Sample3_DetectChangePoint.cs | 96 ------------------- .../samples/Sample3_DetectChangePoint.md | 84 ++++++++++++++++ .../samples/data/request-data.csv | 47 --------- .../DetectChangePoint.json | 7 ++ .../DetectChangePointAsync.json | 7 ++ .../DetectEntireSeriesAnomaly.json | 7 ++ .../DetectEntireSeriesAnomalyAsync.json | 7 ++ .../DetectLastPointAnomaly.json | 7 ++ .../DetectLastPointAnomalyAsync.json | 7 ++ .../Sample1_DetectEntireSeriesAnomaly.cs | 7 +- .../samples/Sample2_DetectLastPointAnomaly.cs | 2 +- .../samples/Sample3_DetectChangePoint.cs | 2 +- 17 files changed, 300 insertions(+), 361 deletions(-) delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json create mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md index 9fe409451b72..d3a697fe1519 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md @@ -13,37 +13,12 @@ description: Samples for the Azure.AI.AnomalyDetector client library # Azure Anomaly Detector client SDK Samples These code samples show common scenario operations with the Anomaly Detector client library. -|**File Name**|**Description**| +|**Sample Name**|**Description**| |----------------|-------------| -|[Sample1_DetectEntireSeriesAnomaly.cs][sample_detect_entire_series_anomaly] |Detecting anomalies in the entire time series.| -|[Sample2_DetectLastPointAnomaly.cs][sample_detect_last_point_anomaly] |Detecting the anomaly status of the latest data point.| -|[Sample3_DetectChangePoint.cs][sample_detect_change_point] |Detecting change points in the entire time series.| +|[Sample1_DetectEntireSeriesAnomaly][sample_detect_entire_series_anomaly] |Detecting anomalies in the entire time series.| +|[Sample2_DetectLastPointAnomaly][sample_detect_last_point_anomaly] |Detecting the anomaly status of the latest data point.| +|[Sample3_DetectChangePoint][sample_detect_change_point] |Detecting change points in the entire time series.| -## Prerequisites -* Azure subscription - [Create one for free][azure_subscription]. -* The current version of [.NET Core][dotnet_core]. -* Once you have your Azure subscription, [create an Anomaly Detector resource][create_anomaly_detector_resource] in the Azure portal to get your key and endpoint. Wait for it to deploy and click the **Go to resource** button. You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production. You will need the key and endpoint from the resource you create to connect your application to the Anomaly Detector API. - -## Setting up -### Create two environment variables for authentication: -* ANOMALY_DETECTOR_ENDPOINT - The resource endpoint for sending API requests. You will get it from your Anomaly Detector resource. -* ANOMALY_DETECTOR_API_KEY - The api key for authenticating your requests. You will get it from your Anomaly Detector resource. - -### Install the client library for your project: -dotnet add package Microsoft.Azure.CognitiveServices.AnomalyDetector --version 3.0.0-preview.1 - -### Sample data -Copy the [sample data][data] to proper location. - - - - - -[azure_subscription]: https://azure.microsoft.com/free/cognitive-services -[dotnet_core]: https://dotnet.microsoft.com/download/dotnet-core -[create_anomaly_detector_resource]: https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesAnomalyDetector - -[sample_detect_entire_series_anomaly]: ./Sample1_DetectEntireSeriesAnomaly.cs -[sample_detect_last_point_anomaly]: ./Sample2_DetectLastPointAnomaly.cs -[sample_detect_change_point]: ./Sample3_DetectChangePoint.cs -[data]: ./data \ No newline at end of file +[sample_detect_entire_series_anomaly]: ./Sample1_DetectEntireSeriesAnomaly.md +[sample_detect_last_point_anomaly]: ./Sample2_DetectLastPointAnomaly.md +[sample_detect_change_point]: ./Sample3_DetectChangePoint.md \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs deleted file mode 100644 index ce900488b087..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Azure; -using Azure.AI.AnomalyDetector; -using Azure.AI.AnomalyDetector.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -class Sample1_DetectEntireSeriesAnomaly -{ - // - static void Main(string[] args) - { - //This sample assumes you have created an environment variable for your key and endpoint - string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); - string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); - string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data","request-data.csv"); - - AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client - - Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file - - EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection - - Console.WriteLine("\nPress ENTER to exit."); - Console.ReadLine(); - } - // - - // - static AnomalyDetectorClient createClient(string endpoint, string key) - { - var endpointUri = new Uri(endpoint); - var credential = new AzureKeyCredential(key); - - AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); - - return client; - } - // - - // - static Request GetSeriesFromFile(string path) - { - List list = File.ReadAllLines(path, Encoding.UTF8) - .Where(e => e.Trim().Length != 0) - .Select(e => e.Split(',')) - .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); - - return new Request(list, Granularity.Daily); - } - // - - // - static async Task EntireDetectSampleAsync(AnomalyDetectorClient client, Request request) - { - Console.WriteLine("Detecting anomalies in the entire time series."); - - try - { - EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly.Contains(true)) - { - Console.WriteLine("An anomaly was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) - { - if (result.IsAnomaly[i]) - { - Console.Write(i); - Console.Write(" "); - } - } - Console.WriteLine(); - } - else - { - Console.WriteLine(" No anomalies detected in the series."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - // -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md new file mode 100644 index 000000000000..6ee1015c5c9c --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md @@ -0,0 +1,84 @@ +# Detect Entire Series Anomaly +This sample shows how to detect all the anomalies in the entire time series. + +To get started, make sure you have satisfied all the prerequisites and got all the resources required by [README][README]. + +## Create an AnomalyDetectorClient + +To create a new `AnomalyDetectorClient` you need the endpoint and credentials from your resource. In the sample below you'll use an Anomaly Detector API key credential by creating an `AzureKeyCredential` object. + +You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. + +```C# Snippet:CreateAnomalyDetectorClient +string endpoint = ""; +string apiKey = ""; + +var endpointUri = new Uri(endpoint); +var credential = new AzureKeyCredential(apiKey); + +AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); +``` + +## Load time series and create Request + +You could download our [sample data][SampleData], read in the time series data and add it to a `Request` object. + +Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. + +Make a `Request` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. + +```C# Snippet:ReadSeriesData +string datapath = ""; + +List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + +Request request = new Request(list, Granularity.Daily); +``` + +## Detect anomalies +Call the client's `EntireDetectAsync` method with the `Request` object and await the response as an `EntireDetectResponse` object. Iterate through the response's `IsAnomaly` values and print any that are true. These values correspond to the index of anomalous data points, if any were found. + +```C# Snippet:DetectEntireSeriesAnomaly +Console.WriteLine("Detecting anomalies in the entire time series."); +try +{ + EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) + { + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsAnomaly[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine(" No anomalies detected in the series."); + } +} +catch (RequestFailedException ex) +{ + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); +} +catch (Exception ex) +{ + Console.WriteLine(ex.Message); +} +``` +To see the full example source files, see: + +* [Detect Entire Series Anomaly](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs) + +[README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md +[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs deleted file mode 100644 index a3afc2a54842..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.cs +++ /dev/null @@ -1,87 +0,0 @@ -using Azure; -using Azure.AI.AnomalyDetector; -using Azure.AI.AnomalyDetector.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -class Sample2_DetectLastPointAnomaly -{ - // - static void Main(string[] args) - { - //This sample assumes you have created an environment variable for your key and endpoint - string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); - string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); - string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data", "request-data.csv"); - - AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client - - Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file - - LastDetectSampleAsync(client, request).Wait(); // Async method for last anomaly detection - - Console.WriteLine("\nPress ENTER to exit."); - Console.ReadLine(); - } - // - - // - static AnomalyDetectorClient createClient(string endpoint, string key) - { - var endpointUri = new Uri(endpoint); - var credential = new AzureKeyCredential(key); - - AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); - - return client; - } - // - - // - static Request GetSeriesFromFile(string path) - { - List list = File.ReadAllLines(path, Encoding.UTF8) - .Where(e => e.Trim().Length != 0) - .Select(e => e.Split(',')) - .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); - - return new Request(list, Granularity.Daily); - } - // - - // - static async Task LastDetectSampleAsync(AnomalyDetectorClient client, Request request) - { - Console.WriteLine("Detecting the anomaly status of the latest point in the series."); - - try - { - LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly) - { - Console.WriteLine("The latest point was detected as an anomaly."); - } - else - { - Console.WriteLine("The latest point was not detected as an anomaly."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - // -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md new file mode 100644 index 000000000000..4468c7d42cf2 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md @@ -0,0 +1,75 @@ +# Detect Last Point Anomaly +This sample shows how to detect the anomaly status of the latest data point. + +To get started, make sure you have satisfied all the prerequisites and got all the resources required by [README][README]. + +## Create an AnomalyDetectorClient + +To create a new `AnomalyDetectorClient` you need the endpoint and credentials from your resource. In the sample below you'll use an Anomaly Detector API key credential by creating an `AzureKeyCredential` object. + +You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. + +```C# Snippet:CreateAnomalyDetectorClient +string endpoint = ""; +string apiKey = ""; + +var endpointUri = new Uri(endpoint); +var credential = new AzureKeyCredential(apiKey); + +AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); +``` + +## Load time series and create Request + +You could download our [sample data][SampleData], read in the time series data and add it to a `Request` object. + +Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. + +Make a `Request` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. + +```C# Snippet:ReadSeriesData +string datapath = ""; + +List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + +Request request = new Request(list, Granularity.Daily); +``` + +## Detect anomaly status of the latest data point +Call the client's `LastDetectAsync` method with the `Request` object and await the response as a `LastDetectResponse` object. Check the response's `IsAnomaly` attribute to determine if the latest data point sent was an anomaly or not. + +```C# Snippet:DetectLastPointAnomaly +Console.WriteLine("Detecting the anomaly status of the latest point in the series."); +try +{ + LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) + { + Console.WriteLine("The latest point was detected as an anomaly."); + } + else + { + Console.WriteLine("The latest point was not detected as an anomaly."); + } +} +catch (RequestFailedException ex) +{ + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); +} +catch (Exception ex) +{ + Console.WriteLine(ex.Message); +} +``` +To see the full example source files, see: + +* [Detect Last Point Anomaly](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs) + +[README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md +[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs deleted file mode 100644 index 74d6fbe5320e..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Azure; -using Azure.AI.AnomalyDetector; -using Azure.AI.AnomalyDetector.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -class Sample3_DetectChangePoint -{ - // - static void Main(string[] args) - { - //This sample assumes you have created an environment variable for your key and endpoint - string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); - string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY"); - string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data", "request-data.csv"); - - AnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client - - ChangePointDetectRequest request = GetSeriesFromFile(datapath); // The request payload with points from the data file - - ChangePointDetectSampleAsync(client, request).Wait(); // Async method for change point detection - - Console.WriteLine("\nPress ENTER to exit."); - Console.ReadLine(); - } - // - - // - static AnomalyDetectorClient createClient(string endpoint, string key) - { - var endpointUri = new Uri(endpoint); - var credential = new AzureKeyCredential(key); - - AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); - - return client; - } - // - - // - static ChangePointDetectRequest GetSeriesFromFile(string path) - { - List list = File.ReadAllLines(path, Encoding.UTF8) - .Where(e => e.Trim().Length != 0) - .Select(e => e.Split(',')) - .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); - - return new ChangePointDetectRequest(list, Granularity.Daily); - } - // - - // - static async Task ChangePointDetectSampleAsync(AnomalyDetectorClient client, ChangePointDetectRequest request) - { - Console.WriteLine("Detecting the change point in the series."); - - try - { - ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); - - if (result.IsChangePoint.Contains(true)) - { - Console.WriteLine("A change point was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) - { - if (result.IsChangePoint[i]) - { - Console.Write(i); - Console.Write(" "); - } - } - Console.WriteLine(); - } - else - { - Console.WriteLine("No change point detected in the series."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - // -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md new file mode 100644 index 000000000000..46534b9ac22d --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md @@ -0,0 +1,84 @@ +# Detect Change Point +This sample shows how to detect the change point in time series. + +To get started, make sure you have satisfied all the prerequisites and got all the resources required by [README][README]. + +## Create an AnomalyDetectorClient + +To create a new `AnomalyDetectorClient` you need the endpoint and credentials from your resource. In the sample below you'll use an Anomaly Detector API key credential by creating an `AzureKeyCredential` object. + +You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. + +```C# Snippet:CreateAnomalyDetectorClient +string endpoint = ""; +string apiKey = ""; + +var endpointUri = new Uri(endpoint); +var credential = new AzureKeyCredential(apiKey); + +AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); +``` + +## Load time series and create ChangePointDetectRequest + +You could download our [sample data][SampleData], read in the time series data and add it to a `ChangePointDetectRequest` object. + +Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. + +Make a `ChangePointDetectRequest` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. + +```C# Snippet:ReadSeriesData +string datapath = ""; + +List list = File.ReadAllLines(datapath, Encoding.UTF8) + .Where(e => e.Trim().Length != 0) + .Select(e => e.Split(',')) + .Where(e => e.Length == 2) + .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + +ChangePointDetectRequest request = new ChangePointDetectRequest(list, Granularity.Daily); +``` + +## Detect Change Point +Call the client's `ChangePointDetectAsync` method with the `ChangePointDetectRequest` object and await the response as a `ChangePointDetectResponse` object. Iterate through the response's `IsChangePoint` values and print any that are true. These values correspond to the index of change points, if any were found. + +```C# Snippet:DetectChangePoint +Console.WriteLine("Detecting the change point in the series."); +try +{ + ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); + + if (result.IsChangePoint.Contains(true)) + { + Console.WriteLine("A change point was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) + { + if (result.IsChangePoint[i]) + { + Console.Write(i); + Console.Write(" "); + } + } + Console.WriteLine(); + } + else + { + Console.WriteLine("No change point detected in the series."); + } +} +catch (RequestFailedException ex) +{ + Console.WriteLine("Error code: " + ex.ErrorCode); + Console.WriteLine("Error message: " + ex.Message); +} +catch (Exception ex) +{ + Console.WriteLine(ex.Message); +} +``` +To see the full example source files, see: + +* [Detect Change Point](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs) + +[README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md +[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv deleted file mode 100644 index 7f242c3712c1..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv +++ /dev/null @@ -1,47 +0,0 @@ -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 \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json new file mode 100644 index 000000000000..24476917b6f4 --- /dev/null +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json @@ -0,0 +1,7 @@ +{ + "Entries": [], + "Variables": { + "ANOMALY_DETECTOR_API_KEY": "Sanitized", + "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" + } +} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs index 2079f14656ba..e11f13743c0d 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs @@ -14,8 +14,13 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : SamplesBase + public partial class AnomalyDetectorSamples : RecordedTestBase { + public AnomalyDetectorSamples(bool isAsync) : base(isAsync) + { + Sanitizer = new AnomalyDetectorRecordedTestSanitizer(); + } + [Test] public async Task DetectEntireSeriesAnomaly() { diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs index f3544a410a5b..cce02be6068d 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -14,7 +14,7 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : SamplesBase + public partial class AnomalyDetectorSamples : RecordedTestBase { [Test] public async Task DetectLastPointAnomaly() diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs index 4b68923becc6..65d6c956453b 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs @@ -14,7 +14,7 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : SamplesBase + public partial class AnomalyDetectorSamples : RecordedTestBase { [Test] public async Task DetectChangePoint() From 96c4d8ff97032e04b9da2b617a86beb863bace0f Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Fri, 4 Sep 2020 23:56:52 +0800 Subject: [PATCH 06/10] Change the sample base class and change to new class definition --- .../Sample1_DetectEntireSeriesAnomaly.md | 53 ++++++++----------- .../samples/Sample2_DetectLastPointAnomaly.md | 41 ++++++-------- .../samples/Sample3_DetectChangePoint.md | 50 +++++++---------- .../DetectChangePoint.json | 7 --- .../DetectChangePointAsync.json | 7 --- .../DetectEntireSeriesAnomaly.json | 7 --- .../DetectEntireSeriesAnomalyAsync.json | 7 --- .../DetectLastPointAnomaly.json | 7 --- .../DetectLastPointAnomalyAsync.json | 7 --- .../Sample1_DetectEntireSeriesAnomaly.cs | 48 ++++++----------- .../samples/Sample2_DetectLastPointAnomaly.cs | 31 ++++------- .../samples/Sample3_DetectChangePoint.cs | 43 ++++++--------- 12 files changed, 98 insertions(+), 210 deletions(-) delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json delete mode 100644 sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md index 6ee1015c5c9c..20269cbfad42 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md @@ -19,61 +19,50 @@ var credential = new AzureKeyCredential(apiKey); AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); ``` -## Load time series and create Request +## Load time series and create DetectRequest -You could download our [sample data][SampleData], read in the time series data and add it to a `Request` object. +You could download our [sample data][SampleData], read in the time series data and add it to a `DetectRequest` object. -Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. +Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoint` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `TimeSeriesPoint` object. -Make a `Request` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. +Make a `DetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesData string datapath = ""; -List list = File.ReadAllLines(datapath, Encoding.UTF8) +List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); -Request request = new Request(list, Granularity.Daily); +DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); ``` -## Detect anomalies -Call the client's `EntireDetectAsync` method with the `Request` object and await the response as an `EntireDetectResponse` object. Iterate through the response's `IsAnomaly` values and print any that are true. These values correspond to the index of anomalous data points, if any were found. +## Detect anomalies of the entire series +Call the client's `DetectEntireSeriesAsync` method with the `DetectRequest` object and await the response as an `EntireDetectResponse` object. Iterate through the response's `IsAnomaly` values and print any that are true. These values correspond to the index of anomalous data points, if any were found. ```C# Snippet:DetectEntireSeriesAnomaly Console.WriteLine("Detecting anomalies in the entire time series."); -try -{ - EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); - if (result.IsAnomaly.Contains(true)) +EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false); + +if (result.IsAnomaly.Contains(true)) +{ + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) { - Console.WriteLine("An anomaly was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) + if (result.IsAnomaly[i]) { - if (result.IsAnomaly[i]) - { - Console.Write(i); - Console.Write(" "); - } + Console.Write(i); + Console.Write(" "); } - Console.WriteLine(); } - else - { - Console.WriteLine(" No anomalies detected in the series."); - } -} -catch (RequestFailedException ex) -{ - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); + Console.WriteLine(); } -catch (Exception ex) +else { - Console.WriteLine(ex.Message); + Console.WriteLine(" No anomalies detected in the series."); } ``` To see the full example source files, see: diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md index 4468c7d42cf2..c57c4b60063d 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md @@ -19,52 +19,41 @@ var credential = new AzureKeyCredential(apiKey); AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); ``` -## Load time series and create Request +## Load time series and create DetectRequest -You could download our [sample data][SampleData], read in the time series data and add it to a `Request` object. +You could download our [sample data][SampleData], read in the time series data and add it to a `DetectRequest` object. -Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. +Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoint` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `TimeSeriesPoint` object. -Make a `Request` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. +Make a `DetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesData string datapath = ""; -List list = File.ReadAllLines(datapath, Encoding.UTF8) +List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); -Request request = new Request(list, Granularity.Daily); +DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); ``` ## Detect anomaly status of the latest data point -Call the client's `LastDetectAsync` method with the `Request` object and await the response as a `LastDetectResponse` object. Check the response's `IsAnomaly` attribute to determine if the latest data point sent was an anomaly or not. +Call the client's `DetectLastPointAsync` method with the `DetectRequest` object and await the response as a `LastDetectResponse` object. Check the response's `IsAnomaly` attribute to determine if the latest data point sent was an anomaly or not. ```C# Snippet:DetectLastPointAnomaly Console.WriteLine("Detecting the anomaly status of the latest point in the series."); -try -{ - LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); - - if (result.IsAnomaly) - { - Console.WriteLine("The latest point was detected as an anomaly."); - } - else - { - Console.WriteLine("The latest point was not detected as an anomaly."); - } -} -catch (RequestFailedException ex) + +LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false); + +if (result.IsAnomaly) { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); + Console.WriteLine("The latest point was detected as an anomaly."); } -catch (Exception ex) +else { - Console.WriteLine(ex.Message); + Console.WriteLine("The latest point was not detected as an anomaly."); } ``` To see the full example source files, see: diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md index 46534b9ac22d..6409ec805a2d 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md @@ -23,57 +23,47 @@ AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential You could download our [sample data][SampleData], read in the time series data and add it to a `ChangePointDetectRequest` object. -Call `File.ReadAllLines` with the file path and create a list of `Point` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `Point` object. +Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoint` objects, and strip any new line characters. Extract the values and separate the timestamp from its numerical value, and add them to a new `TimeSeriesPoint` object. -Make a `ChangePointDetectRequest` object with the series of points, and `Granularity.Daily` for the Granularity (or periodicity) of the data points. +Make a `ChangePointDetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesData string datapath = ""; -List list = File.ReadAllLines(datapath, Encoding.UTF8) +List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); -ChangePointDetectRequest request = new ChangePointDetectRequest(list, Granularity.Daily); +//create request +ChangePointDetectRequest request = new ChangePointDetectRequest(list, TimeGranularity.Daily); ``` -## Detect Change Point -Call the client's `ChangePointDetectAsync` method with the `ChangePointDetectRequest` object and await the response as a `ChangePointDetectResponse` object. Iterate through the response's `IsChangePoint` values and print any that are true. These values correspond to the index of change points, if any were found. +## Detect change point +Call the client's `DetectChangePointAsync` method with the `ChangePointDetectRequest` object and await the response as a `ChangePointDetectResponse` object. Iterate through the response's `IsChangePoint` values and print any that are true. These values correspond to the index of change points, if any were found. ```C# Snippet:DetectChangePoint Console.WriteLine("Detecting the change point in the series."); -try -{ - ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); - if (result.IsChangePoint.Contains(true)) +ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false); + +if (result.IsChangePoint.Contains(true)) +{ + Console.WriteLine("A change point was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) { - Console.WriteLine("A change point was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) + if (result.IsChangePoint[i]) { - if (result.IsChangePoint[i]) - { - Console.Write(i); - Console.Write(" "); - } + Console.Write(i); + Console.Write(" "); } - Console.WriteLine(); } - else - { - Console.WriteLine("No change point detected in the series."); - } -} -catch (RequestFailedException ex) -{ - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); + Console.WriteLine(); } -catch (Exception ex) +else { - Console.WriteLine(ex.Message); + Console.WriteLine("No change point detected in the series."); } ``` To see the full example source files, see: diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePoint.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectChangePointAsync.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomaly.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectEntireSeriesAnomalyAsync.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomaly.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json deleted file mode 100644 index 24476917b6f4..000000000000 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/SessionRecords/AnomalyDetectorSamples/DetectLastPointAnomalyAsync.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Entries": [], - "Variables": { - "ANOMALY_DETECTOR_API_KEY": "Sanitized", - "ANOMALY_DETECTOR_ENDPOINT": "https://anomalydetector-use2.cognitiveservices.azure.com/" - } -} \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs index e11f13743c0d..fcd122b11b1e 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs @@ -14,13 +14,8 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : RecordedTestBase + public partial class AnomalyDetectorSamples : SamplesBase { - public AnomalyDetectorSamples(bool isAsync) : base(isAsync) - { - Sanitizer = new AnomalyDetectorRecordedTestSanitizer(); - } - [Test] public async Task DetectEntireSeriesAnomaly() { @@ -37,47 +32,36 @@ public async Task DetectEntireSeriesAnomaly() //read data string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); - List list = File.ReadAllLines(datapath, Encoding.UTF8) + List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); //create request - Request request = new Request(list, Granularity.Daily); + DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); //detect Console.WriteLine("Detecting anomalies in the entire time series."); - try - { - EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); - if (result.IsAnomaly.Contains(true)) + EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly.Contains(true)) + { + Console.WriteLine("An anomaly was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) { - Console.WriteLine("An anomaly was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) + if (result.IsAnomaly[i]) { - if (result.IsAnomaly[i]) - { - Console.Write(i); - Console.Write(" "); - } + Console.Write(i); + Console.Write(" "); } - Console.WriteLine(); - } - else - { - Console.WriteLine(" No anomalies detected in the series."); } + Console.WriteLine(); } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); - } - catch (Exception ex) + else { - Console.WriteLine(ex.Message); + Console.WriteLine(" No anomalies detected in the series."); } } } diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs index cce02be6068d..42a266f0a36b 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -14,7 +14,7 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : RecordedTestBase + public partial class AnomalyDetectorSamples : SamplesBase { [Test] public async Task DetectLastPointAnomaly() @@ -32,38 +32,27 @@ public async Task DetectLastPointAnomaly() //read data string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); - List list = File.ReadAllLines(datapath, Encoding.UTF8) + List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); //create request - Request request = new Request(list, Granularity.Daily); + DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); //detect Console.WriteLine("Detecting the anomaly status of the latest point in the series."); - try - { - LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); - if (result.IsAnomaly) - { - Console.WriteLine("The latest point was detected as an anomaly."); - } - else - { - Console.WriteLine("The latest point was not detected as an anomaly."); - } - } - catch (RequestFailedException ex) + LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false); + + if (result.IsAnomaly) { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); + Console.WriteLine("The latest point was detected as an anomaly."); } - catch (Exception ex) + else { - Console.WriteLine(ex.Message); + Console.WriteLine("The latest point was not detected as an anomaly."); } } } diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs index 65d6c956453b..3bf90adf70aa 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs @@ -14,7 +14,7 @@ namespace Azure.AI.AnomalyDetector.Tests.Samples { - public partial class AnomalyDetectorSamples : RecordedTestBase + public partial class AnomalyDetectorSamples : SamplesBase { [Test] public async Task DetectChangePoint() @@ -32,47 +32,36 @@ public async Task DetectChangePoint() //read data string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); - List list = File.ReadAllLines(datapath, Encoding.UTF8) + List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) - .Select(e => new Point(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); + .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); //create request - ChangePointDetectRequest request = new ChangePointDetectRequest(list, Granularity.Daily); + ChangePointDetectRequest request = new ChangePointDetectRequest(list, TimeGranularity.Daily); //detect Console.WriteLine("Detecting the change point in the series."); - try - { - ChangePointDetectResponse result = await client.ChangePointDetectAsync(request).ConfigureAwait(false); - if (result.IsChangePoint.Contains(true)) + ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false); + + if (result.IsChangePoint.Contains(true)) + { + Console.WriteLine("A change point was detected at index:"); + for (int i = 0; i < request.Series.Count; ++i) { - Console.WriteLine("A change point was detected at index:"); - for (int i = 0; i < request.Series.Count; ++i) + if (result.IsChangePoint[i]) { - if (result.IsChangePoint[i]) - { - Console.Write(i); - Console.Write(" "); - } + Console.Write(i); + Console.Write(" "); } - Console.WriteLine(); } - else - { - Console.WriteLine("No change point detected in the series."); - } - } - catch (RequestFailedException ex) - { - Console.WriteLine("Error code: " + ex.ErrorCode); - Console.WriteLine("Error message: " + ex.Message); + Console.WriteLine(); } - catch (Exception ex) + else { - Console.WriteLine(ex.Message); + Console.WriteLine("No change point detected in the series."); } } } From bdc1bcc46b314712a8de5235145ea57340fc9425 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Sun, 6 Sep 2020 21:24:39 +0800 Subject: [PATCH 07/10] Fix sample data path --- .../samples/Sample1_DetectEntireSeriesAnomaly.md | 2 +- .../samples/Sample2_DetectLastPointAnomaly.md | 2 +- .../samples/Sample3_DetectChangePoint.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md index 20269cbfad42..873a889f615a 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md @@ -70,4 +70,4 @@ To see the full example source files, see: * [Detect Entire Series Anomaly](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs) [README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md -[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file +[SampleData]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md index c57c4b60063d..9da1af424d69 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md @@ -61,4 +61,4 @@ To see the full example source files, see: * [Detect Last Point Anomaly](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs) [README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md -[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file +[SampleData]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv \ No newline at end of file diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md index 6409ec805a2d..5f023b657860 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md @@ -71,4 +71,4 @@ To see the full example source files, see: * [Detect Change Point](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs) [README]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/README.md -[SampleData]: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/data/request-data.csv \ No newline at end of file +[SampleData]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/data/request-data.csv \ No newline at end of file From d9c95e24fa614d85d867c3590b679c873c0a0e81 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Wed, 9 Sep 2020 08:44:59 +0800 Subject: [PATCH 08/10] Fix code snippets --- .../samples/Sample3_DetectChangePoint.md | 2 +- .../samples/Sample1_DetectEntireSeriesAnomaly.cs | 12 ++++++++++++ .../tests/samples/Sample2_DetectLastPointAnomaly.cs | 4 ++++ .../tests/samples/Sample3_DetectChangePoint.cs | 8 ++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md index 5f023b657860..93fb9e8d62f4 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md @@ -27,7 +27,7 @@ Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoin Make a `ChangePointDetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. -```C# Snippet:ReadSeriesData +```C# Snippet:ReadSeriesDataForChangePoint string datapath = ""; List list = File.ReadAllLines(datapath, Encoding.UTF8) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs index fcd122b11b1e..8c158cd6828f 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample1_DetectEntireSeriesAnomaly.cs @@ -19,6 +19,8 @@ public partial class AnomalyDetectorSamples : SamplesBase list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) @@ -41,6 +47,10 @@ public async Task DetectEntireSeriesAnomaly() //create request DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); + #endregion + + #region Snippet:DetectEntireSeriesAnomaly + //detect Console.WriteLine("Detecting anomalies in the entire time series."); @@ -63,6 +73,8 @@ public async Task DetectEntireSeriesAnomaly() { Console.WriteLine(" No anomalies detected in the series."); } + + #endregion } } } diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs index 42a266f0a36b..0fb43fb6c614 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -41,6 +41,8 @@ public async Task DetectLastPointAnomaly() //create request DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); + #region Snippet:DetectLastPointAnomaly + //detect Console.WriteLine("Detecting the anomaly status of the latest point in the series."); @@ -54,6 +56,8 @@ public async Task DetectLastPointAnomaly() { Console.WriteLine("The latest point was not detected as an anomaly."); } + + #endregion } } } diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs index 3bf90adf70aa..7ba3f427f092 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample3_DetectChangePoint.cs @@ -29,6 +29,8 @@ public async Task DetectChangePoint() //create client AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + #region Snippet:ReadSeriesDataForChangePoint + //read data string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); @@ -41,6 +43,10 @@ public async Task DetectChangePoint() //create request ChangePointDetectRequest request = new ChangePointDetectRequest(list, TimeGranularity.Daily); + #endregion + + #region Snippet:DetectChangePoint + //detect Console.WriteLine("Detecting the change point in the series."); @@ -63,6 +69,8 @@ public async Task DetectChangePoint() { Console.WriteLine("No change point detected in the series."); } + + #endregion } } } From f3b1dd09cd4f61cc00683f2d7ff5b814738736c8 Mon Sep 17 00:00:00 2001 From: Meng Ai Date: Wed, 9 Sep 2020 09:03:37 +0800 Subject: [PATCH 09/10] Add missing snippet region --- .../tests/samples/Sample2_DetectLastPointAnomaly.cs | 8 ++++++++ .../tests/samples/Sample3_DetectChangePoint.cs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs index 0fb43fb6c614..6d65eb5436cf 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -19,6 +19,8 @@ public partial class AnomalyDetectorSamples : SamplesBase Date: Wed, 9 Sep 2020 09:19:51 +0800 Subject: [PATCH 10/10] Fix snippets reference --- .../samples/Sample1_DetectEntireSeriesAnomaly.md | 15 ++++++++++----- .../samples/Sample2_DetectLastPointAnomaly.md | 13 +++++++++---- .../samples/Sample3_DetectChangePoint.md | 15 +++++++++++---- .../samples/Sample2_DetectLastPointAnomaly.cs | 8 -------- .../tests/samples/Sample3_DetectChangePoint.cs | 4 ---- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md index 873a889f615a..e410413ff36a 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample1_DetectEntireSeriesAnomaly.md @@ -1,4 +1,4 @@ -# Detect Entire Series Anomaly +# Detect Entire Series Anomaly This sample shows how to detect all the anomalies in the entire time series. To get started, make sure you have satisfied all the prerequisites and got all the resources required by [README][README]. @@ -10,13 +10,18 @@ To create a new `AnomalyDetectorClient` you need the endpoint and credentials fr You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. ```C# Snippet:CreateAnomalyDetectorClient -string endpoint = ""; -string apiKey = ""; +//read endpoint and apiKey +string endpoint = TestEnvironment.Endpoint; +string apiKey = TestEnvironment.ApiKey; var endpointUri = new Uri(endpoint); var credential = new AzureKeyCredential(apiKey); +//create client AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + +//read data +string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); ``` ## Load time series and create DetectRequest @@ -28,14 +33,13 @@ Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoin Make a `DetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesData -string datapath = ""; - List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); +//create request DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); ``` @@ -43,6 +47,7 @@ DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); Call the client's `DetectEntireSeriesAsync` method with the `DetectRequest` object and await the response as an `EntireDetectResponse` object. Iterate through the response's `IsAnomaly` values and print any that are true. These values correspond to the index of anomalous data points, if any were found. ```C# Snippet:DetectEntireSeriesAnomaly +//detect Console.WriteLine("Detecting anomalies in the entire time series."); EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false); diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md index 9da1af424d69..3d60ce7115ff 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample2_DetectLastPointAnomaly.md @@ -10,13 +10,18 @@ To create a new `AnomalyDetectorClient` you need the endpoint and credentials fr You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. ```C# Snippet:CreateAnomalyDetectorClient -string endpoint = ""; -string apiKey = ""; +//read endpoint and apiKey +string endpoint = TestEnvironment.Endpoint; +string apiKey = TestEnvironment.ApiKey; var endpointUri = new Uri(endpoint); var credential = new AzureKeyCredential(apiKey); +//create client AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + +//read data +string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); ``` ## Load time series and create DetectRequest @@ -28,14 +33,13 @@ Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoin Make a `DetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesData -string datapath = ""; - List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) .Select(e => e.Split(',')) .Where(e => e.Length == 2) .Select(e => new TimeSeriesPoint(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList(); +//create request DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); ``` @@ -43,6 +47,7 @@ DetectRequest request = new DetectRequest(list, TimeGranularity.Daily); Call the client's `DetectLastPointAsync` method with the `DetectRequest` object and await the response as a `LastDetectResponse` object. Check the response's `IsAnomaly` attribute to determine if the latest data point sent was an anomaly or not. ```C# Snippet:DetectLastPointAnomaly +//detect Console.WriteLine("Detecting the anomaly status of the latest point in the series."); LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false); diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md index 93fb9e8d62f4..57648bbb6016 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/Sample3_DetectChangePoint.md @@ -1,4 +1,4 @@ -# Detect Change Point +# Detect Change Point This sample shows how to detect the change point in time series. To get started, make sure you have satisfied all the prerequisites and got all the resources required by [README][README]. @@ -10,13 +10,18 @@ To create a new `AnomalyDetectorClient` you need the endpoint and credentials fr You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. ```C# Snippet:CreateAnomalyDetectorClient -string endpoint = ""; -string apiKey = ""; +//read endpoint and apiKey +string endpoint = TestEnvironment.Endpoint; +string apiKey = TestEnvironment.ApiKey; var endpointUri = new Uri(endpoint); var credential = new AzureKeyCredential(apiKey); +//create client AnomalyDetectorClient client = new AnomalyDetectorClient(endpointUri, credential); + +//read data +string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); ``` ## Load time series and create ChangePointDetectRequest @@ -28,7 +33,8 @@ Call `File.ReadAllLines` with the file path and create a list of `TimeSeriesPoin Make a `ChangePointDetectRequest` object with the series of points, and `TimeGranularity.Daily` for the granularity (or periodicity) of the data points. ```C# Snippet:ReadSeriesDataForChangePoint -string datapath = ""; +//read data +string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "samples", "data", "request-data.csv"); List list = File.ReadAllLines(datapath, Encoding.UTF8) .Where(e => e.Trim().Length != 0) @@ -44,6 +50,7 @@ ChangePointDetectRequest request = new ChangePointDetectRequest(list, TimeGranul Call the client's `DetectChangePointAsync` method with the `ChangePointDetectRequest` object and await the response as a `ChangePointDetectResponse` object. Iterate through the response's `IsChangePoint` values and print any that are true. These values correspond to the index of change points, if any were found. ```C# Snippet:DetectChangePoint +//detect Console.WriteLine("Detecting the change point in the series."); ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false); diff --git a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs index 6d65eb5436cf..0fb43fb6c614 100644 --- a/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs +++ b/sdk/anomalydetector/Azure.AI.AnomalyDetector/tests/samples/Sample2_DetectLastPointAnomaly.cs @@ -19,8 +19,6 @@ public partial class AnomalyDetectorSamples : SamplesBase