Skip to content

Commit

Permalink
Move samples to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mengaims committed Sep 4, 2020
1 parent 40e8721 commit 4b60995
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@
<PackageReference Include="Azure.Identity" />
</ItemGroup>

<ItemGroup>
<None Include="samples\data\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(AzureCoreTestFramework)" />
<ProjectReference Include="..\src\Azure.AI.AnomalyDetector.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="samples\data\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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<AnomalyDetectorTestEnvironment>
{
[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<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();

//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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AnomalyDetectorTestEnvironment>
{
[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<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();

//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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AnomalyDetectorTestEnvironment>
{
[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<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();

//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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4b60995

Please sign in to comment.