Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Azure.CognitiveServices.AnomalyDetector;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace AnomalyDetectorSDK.Tests
{
public abstract class BaseTests
{
public static bool IsTestTenant = false;
private static readonly string AnomalyDetectorSubscriptionKey;

static BaseTests()
{
// Retrieve the configuration information.
AnomalyDetectorSubscriptionKey = "";
}

protected IAnomalyDetectorClient GetAnomalyDetectorClient(DelegatingHandler handler)
{
IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(AnomalyDetectorSubscriptionKey), handlers: handler)
{
Endpoint = "https://westus2.api.cognitive.microsoft.com"
};

return client;
}


}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using Microsoft.Azure.CognitiveServices.AnomalyDetector;
using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Xunit;

namespace AnomalyDetectorSDK.Tests
{
public class EntireSeriesDetectTests : BaseTests
{
[Fact]
public void TestLastAnomalySeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestLastAnomalySeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());
var series = new List<Point>{
new Point(DateTime.Parse("1962-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-12-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-12-01T00:00:00Z"), 0)
};
Request request = new Request(series, Granularity.Monthly);
request.MaxAnomalyRatio = 0.25;
request.Sensitivity = 95;
var result = client.EntireDetectAsync(request).Result;
Assert.True(result.IsAnomaly[series.Count - 1]);
}
}

[Fact]
public void TestRandomAnomalySeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestRandomAnomalySeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());
var series = new List<Point>{
new Point(DateTime.Parse("1962-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-12-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-12-01T00:00:00Z"), 1)
};
Request request = new Request(series, Granularity.Monthly);
request.MaxAnomalyRatio = 0.25;
request.Sensitivity = 95;
int anomalyIndex = FakeRandom(0, series.Count - 1);
request.Series[anomalyIndex].Value = 0;
var result = client.EntireDetectAsync(request).Result;
Assert.True(result.IsAnomaly[anomalyIndex]);
}
}

[Fact]
public void TestSineDistributionSeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestSineDistributionSeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());
int len = 49;
int frequency = 4;
var startTime = DateTime.Parse("2018-05-01T00:00:00Z");

var series = Enumerable.Range(0, len)
.Select(e => new Point(startTime.AddDays(e), Math.Sin(2 * Math.PI * frequency * e / (len - 1)))).ToList();
Request request = new Request(series, Granularity.Daily);
var result = client.EntireDetectAsync(request).Result;
Assert.Equal(12, result.Period);

int anomalyIndex = FakeRandom(12, 48);
request.Series[anomalyIndex].Value = 2;
result = client.EntireDetectAsync(request).Result;
Assert.True(result.IsAnomaly[anomalyIndex]);
}
}

[Fact]
public void TestNormalDistributionSeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestNormalDistributionSeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());

var startTime = DateTime.Parse("2018-05-01T00:00:00Z");

double sigma = 0.01;
double step = 0.0008;
int len = 99;
double start = -(len / 2) * step;
var singleTile = new List<double>();
for (int i = 0; i < len; ++i)
{
double x = start + step * i;
singleTile.Add(1 / (sigma * Math.Sqrt(2 * Math.PI)) * Math.Exp(-x * x / (2 * sigma * sigma)));
}

var series = new List<Point>();
for (int i = 0; i < len * 4; ++i)
{
series.Add(new Point(startTime.AddDays(i), singleTile[i % len]));
}

Request request = new Request(series, Granularity.Daily);
var result = client.EntireDetectAsync(request).Result;
Assert.Equal(len, result.Period);
Assert.DoesNotContain(true, result.IsAnomaly);

int anomalyIndex = FakeRandom(len, len*4-1);
request.Series[anomalyIndex].Value = 100;
result = client.EntireDetectAsync(request).Result;
Assert.True(result.IsAnomaly[anomalyIndex]);
}
}

private int FakeRandom(int minValue, int maxValue)
{
return (minValue + maxValue) / 2;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Microsoft.Azure.CognitiveServices.AnomalyDetector;
using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Xunit;

namespace AnomalyDetectorSDK.Tests
{
public class LastPointDetectTests : BaseTests
{
[Fact]
public void TestLastAnomalySeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestLastAnomalySeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());
var series = new List<Point>{
new Point(DateTime.Parse("1962-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1962-12-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-01-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-02-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-03-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-04-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-05-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-06-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-07-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-08-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-09-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-10-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-11-01T00:00:00Z"), 1),
new Point(DateTime.Parse("1963-12-01T00:00:00Z"), 0)
};
Request request = new Request(series, Granularity.Monthly);
request.MaxAnomalyRatio = 0.25;
request.Sensitivity = 95;
var result = client.LastDetectAsync(request).Result;
Assert.True(result.IsAnomaly);

request.Series = new List<Point>{
new Point(DateTime.Parse("1962-01-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-02-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-03-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-04-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-05-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-06-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-07-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-08-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-09-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-10-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-11-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1962-12-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-01-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-02-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-03-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-04-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-05-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-06-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-07-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-08-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-09-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-10-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-11-01T00:00:00Z"), 0),
new Point(DateTime.Parse("1963-12-01T00:00:00Z"), 1)
};
result = client.LastDetectAsync(request).Result;
Assert.True(result.IsAnomaly);
}
}

[Fact]
public void TestSineDistributionSeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestSineDistributionSeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());
int len = 49;
int frequency = 4;
var startTime = DateTime.Parse("2018-05-01T00:00:00Z");

var series = Enumerable.Range(0, len)
.Select(e => new Point(startTime.AddDays(e), Math.Sin(2 * Math.PI * frequency * e / (len-1)))).ToList();
Request request = new Request(series, Granularity.Daily);
var result = client.LastDetectAsync(request).Result;
Assert.Equal(12, result.Period);
Assert.False(result.IsAnomaly);

request.Series[len-1].Value = 2;
result = client.LastDetectAsync(request).Result;
Assert.True(result.IsAnomaly);
}
}

[Fact]
public void TestNormalDistributionSeries()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "TestNormalDistributionSeries");
IAnomalyDetectorClient client = GetAnomalyDetectorClient(HttpMockServer.CreateInstance());

var startTime = DateTime.Parse("2018-05-01T00:00:00Z");

double sigma = 0.01;
double step = 0.0008;
int len = 99;
double start = -(len / 2) * step;
var singleTile = new List<double>();
for (int i = 0; i < len; ++i)
{
double x = start + step * i;
singleTile.Add(1 / (sigma * Math.Sqrt(2 * Math.PI)) * Math.Exp(-x * x / (2 * sigma * sigma)));
}

var series = new List<Point>();
for (int i = 0; i < len * 4; ++i)
{
series.Add(new Point(startTime.AddDays(i), singleTile[i % len]));
}

Request request = new Request(series, Granularity.Daily);
var result = client.LastDetectAsync(request).Result;
Assert.Equal(len, result.Period);
Assert.False(result.IsAnomaly);

request.Series[request.Series.Count - 1].Value = 100;
result = client.LastDetectAsync(request).Result;
Assert.True(result.IsAnomaly);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" />
<PropertyGroup>
<Description>Microsoft.Azure.CognitiveServices.AnomalyDetector.Tests Class Library</Description>
<AssemblyName>Microsoft.Azure.CognitiveServices.AnomalyDetector.Tests</AssemblyName>
<Version>1.0.0</Version>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
<ProjectReference Include="..\AnomalyDetector\Microsoft.Azure.CognitiveServices.AnomalyDetector.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
Loading