-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Sample3_DetectChangePoint.cs
executable file
·74 lines (60 loc) · 2.47 KB
/
Sample3_DetectChangePoint.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
// 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 DetectChangePoint()
{
//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);
#region Snippet:ReadSeriesDataForChangePoint
//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
UnivariateChangePointDetectionOptions request = new UnivariateChangePointDetectionOptions(list, TimeGranularity.Daily);
#endregion
#region Snippet:DetectChangePoint
//detect
Console.WriteLine("Detecting the change point in the series.");
UnivariateChangePointDetectionResult result = client.DetectUnivariateChangePoint(request);
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.");
}
#endregion
}
}
}