forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cognitive service AnomalyDetector SDK with unittests (Azure#5276)
* add anomaly detector sdk with unit tests fullfill .net solution structure add unit tests update PackageReleaseNotes copy resource "PreserveNewest" change service name fix property name in response change sdk version to 1.0.0.0 update change from swagger, make non nullable update PackageReleaseNotes update swagger change re-generate code from zhuxia/ad from repo moreOver0/azure-rest-api-specs updates * update generate.ps1 and session records
- Loading branch information
Showing
30 changed files
with
2,610 additions
and
56 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/SDKs/CognitiveServices/dataPlane/AnomalyDetector/AnomalyDetector.Tests/BaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/SDKs/CognitiveServices/dataPlane/AnomalyDetector/AnomalyDetector.Tests/CRUDTests.cs
This file was deleted.
Oops, something went wrong.
168 changes: 168 additions & 0 deletions
168
...nitiveServices/dataPlane/AnomalyDetector/AnomalyDetector.Tests/EntireSeriesDetectTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
...CognitiveServices/dataPlane/AnomalyDetector/AnomalyDetector.Tests/LastPointDetectTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ctor/AnomalyDetector.Tests/Microsoft.Azure.CognitiveServices.AnomalyDetector.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.