Skip to content

Commit 353e968

Browse files
authored
Merge pull request #91859 from aahill/note-test-2
[CogSvcs][AnomalyDetector] Updating setup instructions
2 parents 9f81cea + 96a0c07 commit 353e968

8 files changed

+129
-294
lines changed

articles/cognitive-services/Anomaly-Detector/quickstarts/detect-data-anomalies-csharp-sdk.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: nitinme
88
ms.service: cognitive-services
99
ms.subservice: anomaly-detector
1010
ms.topic: quickstart
11-
ms.date: 08/01/2019
11+
ms.date: 10/15/2019
1212
ms.author: aahi
1313
---
1414

@@ -21,7 +21,7 @@ Use the Anomaly Detector client library for .NET to:
2121
* Detect anomalies throughout your time series dataset, as a batch request
2222
* Detect the anomaly status of the latest data point in your time series
2323

24-
[Library reference documentation](https://docs.microsoft.com/dotnet/api/Microsoft.Azure.CognitiveServices.AnomalyDetector?view=azure-dotnet-preview) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/cognitiveservices/AnomalyDetector) | [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.AnomalyDetector/) | [Samples](https://github.com/Azure-Samples/anomalydetector)
24+
[Library reference documentation](https://docs.microsoft.com/dotnet/api/Microsoft.Azure.CognitiveServices.AnomalyDetector?view=azure-dotnet-preview) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/cognitiveservices/AnomalyDetector) | [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.AnomalyDetector/) | [Code samples](https://github.com/Azure-Samples/anomalydetector)
2525

2626
## Prerequisites
2727

@@ -34,13 +34,9 @@ Use the Anomaly Detector client library for .NET to:
3434

3535
[!INCLUDE [anomaly-detector-resource-creation](../../../../includes/cognitive-services-anomaly-detector-resource-cli.md)]
3636

37-
After you get a key from your trial subscription or resource, [create an environment variable](../../cognitive-services-apis-create-account.md#configure-an-environment-variable-for-authentication) for the key, named `ANOMALY_DETECTOR_KEY`.
37+
### Create a new .NET Core application
3838

39-
### Create a new C# application
40-
41-
Create a new .NET Core application in your preferred editor or IDE.
42-
43-
In a console window (such as cmd, PowerShell, or Bash), use the dotnet `new` command to create a new console app with the name `anomaly-detector-quickstart`. This command creates a simple "Hello World" C# project with a single source file: *Program.cs*.
39+
In a console window (such as cmd, PowerShell, or Bash), use the dotnet `new` command to create a new console app with the name `anomaly-detector-quickstart`. This command creates a simple "Hello World" project with a single C# source file: *Program.cs*.
4440

4541
```console
4642
dotnet new console -n anomaly-detector-quickstart
@@ -62,14 +58,6 @@ Build succeeded.
6258
...
6359
```
6460

65-
From the project directory, open the *program.cs* file in your preferred editor or IDE. Add the following using `directives`:
66-
67-
[!code-csharp[using statements](~/samples-anomaly-detector/quickstarts/sdk/csharp-sdk-sample.cs?name=usingStatements)]
68-
69-
In the application's `main()` method, create variables for your resource's Azure location, and your key as an environment variable. If you created the environment variable after application is launched, the editor, IDE, or shell running it will need to be closed and reloaded to access the variable.
70-
71-
[!code-csharp[Main method](~/samples-anomaly-detector/quickstarts/sdk/csharp-sdk-sample.cs?name=mainMethod)]
72-
7361
### Install the client library
7462

7563
Within the application directory, install the Anomaly Detector client library for .NET with the following command:
@@ -78,7 +66,13 @@ Within the application directory, install the Anomaly Detector client library fo
7866
dotnet add package Microsoft.Azure.CognitiveServices.AnomalyDetector --version 0.8.0-preview
7967
```
8068

81-
If you're using the Visual Studio IDE, the client library is available as a NuGet package.
69+
From the project directory, open the *program.cs* file and add the following using `directives`:
70+
71+
[!code-csharp[using statements](~/samples-anomaly-detector/quickstarts/sdk/csharp-sdk-sample.cs?name=usingStatements)]
72+
73+
In the application's `main()` method, create variables for your resource's Azure location, and your key as an environment variable. If you created the environment variable after application is launched, the editor, IDE, or shell running it will need to be closed and reloaded to access the variable.
74+
75+
[!code-csharp[Main method](~/samples-anomaly-detector/quickstarts/sdk/csharp-sdk-sample.cs?name=mainMethod)]
8276

8377
## Object model
8478

articles/cognitive-services/Anomaly-Detector/quickstarts/detect-data-anomalies-csharp.md

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: nitinme
88
ms.service: cognitive-services
99
ms.subservice: anomaly-detector
1010
ms.topic: quickstart
11-
ms.date: 07/26/2019
11+
ms.date: 10/14/2019
1212
ms.author: aahi
1313
---
1414

@@ -37,23 +37,16 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
3737

3838
- A JSON file containing time series data points. The example data for this quickstart can be found on [GitHub](https://github.com/Azure-Samples/anomalydetector/blob/master/example-data/request-data.json).
3939

40-
[!INCLUDE [cognitive-services-anomaly-detector-data-requirements](../../../../includes/cognitive-services-anomaly-detector-data-requirements.md)]
40+
### Create an Anomaly Detector resource
4141

42-
[!INCLUDE [cognitive-services-anomaly-detector-signup-requirements](../../../../includes/cognitive-services-anomaly-detector-signup-requirements.md)]
42+
[!INCLUDE [anomaly-detector-resource-creation](../../../../includes/cognitive-services-anomaly-detector-resource-cli.md)]
4343

4444
## Create a new application
4545

4646
1. In Visual Studio, create a new console solution and add the following packages.
4747

48-
```csharp
49-
using System;
50-
using System.IO;
51-
using System.Net;
52-
using System.Net.Http;
53-
using System.Net.Http.Headers;
54-
using System.Text;
55-
using System.Threading.Tasks;
56-
```
48+
[!code-csharp[using statements](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=usingStatements)]
49+
5750

5851
2. Create variables for your subscription key and your endpoint. Below are the URIs you can use for anomaly detection. These will be appended to your service endpoint later to create the API request URLs.
5952

@@ -62,17 +55,7 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
6255
|Batch detection | `/anomalydetector/v1.0/timeseries/entire/detect` |
6356
|Detection on the latest data point | `/anomalydetector/v1.0/timeseries/last/detect` |
6457

65-
```csharp
66-
// Replace the subscriptionKey string value with your valid subscription key.
67-
const string subscriptionKey = "[YOUR_SUBSCRIPTION_KEY]";
68-
// Replace the endpoint URL with the correct one for your subscription.
69-
// Your endpoint can be found in the Azure portal. For example: https://westus2.api.cognitive.microsoft.com
70-
const string endpoint = "[YOUR_ENDPOINT_URL]";
71-
// Replace the dataPath string with a path to the JSON formatted time series data.
72-
const string dataPath = "[PATH_TO_TIME_SERIES_DATA]";
73-
const string latestPointDetectionUrl = "/anomalydetector/v1.0/timeseries/last/detect";
74-
const string batchDetectionUrl = "/anomalydetector/v1.0/timeseries/entire/detect";
75-
```
58+
[!code-csharp[initial variables for endpoint, key and data file](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=vars)]
7659

7760
## Create a function to send requests
7861

@@ -82,19 +65,7 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
8265

8366
3. Send the request with `PostAsync()`, and then return the response.
8467

85-
```csharp
86-
static async Task<string> Request(string apiAddress, string endpoint, string subscriptionKey, string requestData){
87-
using (HttpClient client = new HttpClient { BaseAddress = new Uri(apiAddress) }){
88-
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
89-
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
90-
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
91-
92-
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
93-
var res = await client.PostAsync(endpoint, content);
94-
return await res.Content.ReadAsStringAsync();
95-
}
96-
}
97-
```
68+
[!code-csharp[Request method](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=requestMethod)]
9869

9970
## Detect anomalies as a batch
10071

@@ -106,72 +77,24 @@ static async Task<string> Request(string apiAddress, string endpoint, string sub
10677

10778
4. Otherwise, find the positions of anomalies in the data set. The response's `isAnomaly` field contains an array of boolean values, each of which indicates whether a data point is an anomaly. Convert this to a string array with the response object's `ToObject<bool[]>()` function. Iterate through the array, and print the index of any `true` values. These values correspond to the index of anomalous data points, if any were found.
10879

109-
```csharp
110-
static void detectAnomaliesBatch(string requestData){
111-
System.Console.WriteLine("Detecting anomalies as a batch");
112-
113-
var result = Request(
114-
endpoint,
115-
batchDetectionUrl,
116-
subscriptionKey,
117-
requestData).Result;
118-
119-
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
120-
System.Console.WriteLine(jsonObj);
121-
122-
if (jsonObj["code"] != null){
123-
System.Console.WriteLine($"Detection failed. ErrorCode:{jsonObj["code"]}, ErrorMessage:{jsonObj["message"]}");
124-
}
125-
else{
126-
bool[] anomalies = jsonObj["isAnomaly"].ToObject<bool[]>();
127-
System.Console.WriteLine("\nAnomalies detected in the following data positions:");
128-
for (var i = 0; i < anomalies.Length; i++){
129-
if (anomalies[i])
130-
{
131-
System.Console.Write(i + ", ");
132-
}
133-
}
134-
}
135-
}
136-
```
80+
[!code-csharp[Detect anomalies batch](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=detectAnomaliesBatch)]
81+
13782

13883
## Detect the anomaly status of the latest data point
13984

14085
1. Create a new function called `detectAnomaliesLatest()`. Construct the request and send it by calling the `Request()` function with your endpoint, subscription key, the URL for latest point anomaly detection, and the time series data.
14186

14287
2. Deserialize the JSON object, and write it to the console.
14388

144-
```csharp
145-
static void detectAnomaliesLatest(string requestData){
146-
System.Console.WriteLine("\n\nDetermining if latest data point is an anomaly");
147-
var result = Request(
148-
endpoint,
149-
latestPointDetectionUrl,
150-
subscriptionKey,
151-
requestData).Result;
152-
153-
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
154-
System.Console.WriteLine(jsonObj);
155-
}
156-
```
89+
[!code-csharp[Detect anomalies latest](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=detectAnomaliesLatest)]
15790

15891
## Load your time series data and send the request
15992

16093
1. In the main method of your application, load your JSON time series data with `File.ReadAllText()`.
16194

16295
2. Call the anomaly detection functions created above. Use `System.Console.ReadKey()` to keep the console window open after running the application.
16396

164-
```csharp
165-
static void Main(string[] args){
166-
167-
var requestData = File.ReadAllText(dataPath);
168-
169-
detectAnomaliesBatch(requestData);
170-
detectAnomaliesLatest(requestData);
171-
172-
System.Console.ReadKey();
173-
}
174-
```
97+
[!code-csharp[Main method](~/samples-anomaly-detector/quickstarts/csharp-detect-anomalies.cs?name=main)]
17598

17699
### Example response
177100

@@ -182,4 +105,8 @@ A successful response is returned in JSON format. Click the links below to view
182105
## Next steps
183106

184107
> [!div class="nextstepaction"]
185-
> [REST API reference](https://westus2.dev.cognitive.microsoft.com/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect)
108+
>[Streaming anomaly detection with Azure Databricks](../tutorials/anomaly-detection-streaming-databricks.md)
109+
110+
* What is the [Anomaly Detector API?](../overview.md)
111+
* [Best practices](../concepts/anomaly-detection-best-practices.md) when using the Anomaly Detector API.
112+
* The source code for this sample can be found on [GitHub](https://github.com/Azure-Samples/AnomalyDetector/blob/master/quickstarts/sdk/csharp-sdk-sample.cs).

articles/cognitive-services/Anomaly-Detector/quickstarts/detect-data-anomalies-java.md

Lines changed: 15 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,15 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
3333

3434
- A JSON file containing time series data points. The example data for this quickstart can be found on [GitHub](https://github.com/Azure-Samples/anomalydetector/blob/master/example-data/request-data.json).
3535

36-
[!INCLUDE [cognitive-services-anomaly-detector-data-requirements](../../../../includes/cognitive-services-anomaly-detector-data-requirements.md)]
36+
### Create an Anomaly Detector resource
3737

38-
[!INCLUDE [cognitive-services-anomaly-detector-signup-requirements](../../../../includes/cognitive-services-anomaly-detector-signup-requirements.md)]
38+
[!INCLUDE [anomaly-detector-resource-creation](../../../../includes/cognitive-services-anomaly-detector-resource-cli.md)]
3939

4040
## Create a new application
4141

42-
1. Create a new Java project in your favorite IDE or editor, and import the following libraries.
43-
44-
```java
45-
import org.apache.http.HttpEntity;
46-
import org.apache.http.client.methods.CloseableHttpResponse;
47-
import org.apache.http.client.methods.HttpPost;
48-
import org.apache.http.entity.StringEntity;
49-
import org.apache.http.impl.client.CloseableHttpClient;
50-
import org.apache.http.impl.client.HttpClients;
51-
import org.apache.http.util.EntityUtils;
52-
import org.json.JSONArray;
53-
import org.json.JSONObject;
54-
import java.io.IOException;
55-
import java.nio.file.Files;
56-
import java.nio.file.Paths;
57-
```
42+
1. Create a new Java project and import the following libraries.
43+
44+
[!code-java[Import statements](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=imports)]
5845

5946
2. Create variables for your subscription key and your endpoint. Below are the URIs you can use for anomaly detection. These will be appended to your service endpoint later to create the API request URLs.
6047

@@ -63,23 +50,7 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
6350
|Batch detection | `/anomalydetector/v1.0/timeseries/entire/detect` |
6451
|Detection on the latest data point | `/anomalydetector/v1.0/timeseries/last/detect` |
6552

66-
```java
67-
// Replace the subscriptionKey string value with your valid subscription key.
68-
static final String subscriptionKey = "[YOUR_SUBSCRIPTION_KEY]";
69-
//replace the endpoint URL with the correct one for your subscription. Your endpoint can be found in the Azure portal.
70-
//For example: https://westus2.api.cognitive.microsoft.com
71-
static final String endpoint = "[YOUR_ENDPOINT_URL]";
72-
// Replace the dataPath string with a path to the JSON formatted time series data.
73-
static final String dataPath = "[PATH_TO_TIME_SERIES_DATA]";
74-
static final String latestPointDetectionUrl = "/anomalydetector/v1.0/timeseries/last/detect";
75-
static final String batchDetectionUrl = "/anomalydetector/v1.0/timeseries/entire/detect";
76-
```
77-
78-
3. Read in the JSON data file
79-
80-
```java
81-
String requestData = new String(Files.readAllBytes(Paths.get(dataPath)), "utf-8");
82-
```
53+
[!code-java[Initial key and endpoint variables](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=vars)]
8354

8455
## Create a function to send requests
8556

@@ -95,29 +66,7 @@ Use this quickstart to start using the Anomaly Detector API's two detection mode
9566

9667
6. Create an `HttpEntity` object to store the response content. Get the content with `getEntity()`. If the response isn't empty, return it.
9768

98-
```java
99-
static String sendRequest(String apiAddress, String endpoint, String subscriptionKey, String requestData) {
100-
try (CloseableHttpClient client = HttpClients.createDefault()) {
101-
HttpPost request = new HttpPost(endpoint + apiAddress);
102-
// Request headers.
103-
request.setHeader("Content-Type", "application/json");
104-
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
105-
request.setEntity(new StringEntity(requestData));
106-
try (CloseableHttpResponse response = client.execute(request)) {
107-
HttpEntity respEntity = response.getEntity();
108-
if (respEntity != null) {
109-
return EntityUtils.toString(respEntity, "utf-8");
110-
}
111-
} catch (Exception respEx) {
112-
respEx.printStackTrace();
113-
}
114-
} catch (IOException ex) {
115-
System.err.println("Exception on Anomaly Detector: " + ex.getMessage());
116-
ex.printStackTrace();
117-
}
118-
return null;
119-
}
120-
```
69+
[!code-java[API request method](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=request)]
12170

12271
## Detect anomalies as a batch
12372

@@ -127,53 +76,21 @@ static String sendRequest(String apiAddress, String endpoint, String subscriptio
12776

12877
3. Otherwise, find the positions of anomalies in the data set. The response's `isAnomaly` field contains a boolean value relating to whether a given data point is an anomaly. Get the JSON array, and iterate through it, printing the index of any `true` values. These values correspond to the index of anomalous data points, if any were found.
12978

130-
```java
131-
static void detectAnomaliesBatch(String requestData) {
132-
System.out.println("Detecting anomalies as a batch");
133-
String result = sendRequest(batchDetectionUrl, endpoint, subscriptionKey, requestData);
134-
if (result != null) {
135-
System.out.println(result);
136-
JSONObject jsonObj = new JSONObject(result);
137-
if (jsonObj.has("code")) {
138-
System.out.println(String.format("Detection failed. ErrorCode:%s, ErrorMessage:%s", jsonObj.getString("code"), jsonObj.getString("message")));
139-
} else {
140-
JSONArray jsonArray = jsonObj.getJSONArray("isAnomaly");
141-
System.out.println("Anomalies found in the following data positions:");
142-
for (int i = 0; i < jsonArray.length(); ++i) {
143-
if (jsonArray.getBoolean(i))
144-
System.out.print(i + ", ");
145-
}
146-
System.out.println();
147-
}
148-
}
149-
}
150-
```
79+
[!code-java[Method for batch detection](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=detectBatch)]
15180

15281
## Detect the anomaly status of the latest data point
15382

15483
* Create a method called `detectAnomaliesLatest()` to detect the anomaly status of the last data point in the data set. Call the `sendRequest()` method created above with your endpoint, url, subscription key, and json data. Get the result, and print it to the console.
15584

156-
```java
157-
static void detectAnomaliesLatest(String requestData) {
158-
System.out.println("Determining if latest data point is an anomaly");
159-
String result = sendRequest(latestPointDetectionUrl, endpoint, subscriptionKey, requestData);
160-
System.out.println(result);
161-
}
162-
```
85+
[!code-java[Latest point detection method](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=detectLatest)]
16386

16487
## Load your time series data and send the request
16588

16689
1. In the main method of your application, read in the JSON file containing the data that will be added to the requests.
16790

16891
2. Call the two anomaly detection functions created above.
16992

170-
```java
171-
public static void main(String[] args) throws Exception {
172-
String requestData = new String(Files.readAllBytes(Paths.get(dataPath)), "utf-8");
173-
detectAnomaliesBatch(requestData);
174-
detectAnomaliesLatest(requestData);
175-
}
176-
```
93+
[!code-java[Main method](~/samples-anomaly-detector/quickstarts/java-detect-anomalies.java?name=main)]
17794

17895
### Example response
17996

@@ -184,4 +101,8 @@ A successful response is returned in JSON format. Click the links below to view
184101
## Next steps
185102

186103
> [!div class="nextstepaction"]
187-
> [REST API reference](https://westus2.dev.cognitive.microsoft.com/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect)
104+
>[Streaming anomaly detection with Azure Databricks](../tutorials/anomaly-detection-streaming-databricks.md)
105+
106+
* What is the [Anomaly Detector API?](../overview.md)
107+
* [Best practices](../concepts/anomaly-detection-best-practices.md) when using the Anomaly Detector API.
108+
* The source code for this sample can be found on [GitHub](https://github.com/Azure-Samples/AnomalyDetector/blob/master/quickstarts/sdk/csharp-sdk-sample.cs).

0 commit comments

Comments
 (0)