Skip to content

Commit

Permalink
Add md files
Browse files Browse the repository at this point in the history
  • Loading branch information
mengaims committed Sep 4, 2020
1 parent 4b60995 commit c053b2f
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 361 deletions.
39 changes: 7 additions & 32 deletions sdk/anomalydetector/Azure.AI.AnomalyDetector/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<code>dotnet add package Microsoft.Azure.CognitiveServices.AnomalyDetector --version 3.0.0-preview.1</code>

### 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
[sample_detect_entire_series_anomaly]: ./Sample1_DetectEntireSeriesAnomaly.md
[sample_detect_last_point_anomaly]: ./Sample2_DetectLastPointAnomaly.md
[sample_detect_change_point]: ./Sample3_DetectChangePoint.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 = "<endpoint>";
string apiKey = "<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 = "<dataPath>";

List<Point> 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 = "<endpoint>";
string apiKey = "<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 = "<dataPath>";

List<Point> 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
Loading

0 comments on commit c053b2f

Please sign in to comment.