-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Sample2_DetectLastPointAnomaly.cs
executable file
·76 lines (65 loc) · 2.56 KB
/
Sample2_DetectLastPointAnomaly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 Azure.Core.TestFramework;
using NUnit.Framework;
namespace Azure.AI.AnomalyDetector.Tests.Samples
{
public partial class AnomalyDetectorSamples : SamplesBase<AnomalyDetectorTestEnvironment>
{
[Test]
public void DetectLastPointAnomaly()
{
//read endpoint and apiKey
string endpoint = TestEnvironment.Endpoint;
string apiKey = TestEnvironment.ApiKey;
Uri endpointUri = new Uri(endpoint);
AzureKeyCredential 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<TimeSeriesPoint> 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(float.Parse(e[1])){ Timestamp = DateTime.Parse(e[0])}).ToList();
//create request
UnivariateDetectionOptions request = new UnivariateDetectionOptions(list)
{
Granularity = TimeGranularity.Daily
};
#region Snippet:DetectLastPointAnomaly
//detect
Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
try
{
UnivariateLastDetectionResult result = client.GetUnivariateClient().DetectUnivariateLastPoint(request);
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($"Last detection failed: {ex.Message}");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"Detection error. {ex.Message}");
throw;
}
#endregion
}
}
}