//-------------------------------- static void Main(string[] args) { string location = "westus2"; //string location = "synnexanomalydetector"; string endpoint = $"https://{location}.api.cognitive.microsoft.com"; //string endpoint = $"https://{location}.cognitiveservices.azure.com"; Console.WriteLine("\n endpoint = " + endpoint); //This sample assumes you have created an environment variable for your key, named ANOMALY_DETECTOR_KEY // To set this value in your Azure tenant get your Cognitive Services key and add it to you environment using // command line call: // SETX ANOMALY_DETECTOR_KEY [your-key] // Example: SETX ANOMALY_DETECTOR_KEY 12930u82103981290830192839018 string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY"); Console.WriteLine("\n key = " + key); string datapath = "C:\\temp\\request-data.csv"; IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine(); }
//-------------------------------- static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request) { Console.WriteLine("Detecting anomalies in the entire time series."); EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); //debug line next Console.WriteLine("EntireDetectResponse result = " + result); // 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."); } }
// </createClient> // <runSamples> static void runSamples(IAnomalyDetectorClient client, string dataPath) { try { List <Point> series = GetSeriesFromFile(dataPath); Request request = new Request(series, Granularity.Daily); EntireDetectSampleAsync(client, request).Wait(); LastDetectSampleAsync(client, request).Wait(); } catch (Exception e) { Console.WriteLine(e.Message); if (e.InnerException != null && e.InnerException is APIErrorException) { APIError error = ((APIErrorException)e.InnerException).Body; Console.WriteLine("Error code: " + error.Code); Console.WriteLine("Error message: " + error.Message); } else if (e.InnerException != null) { Console.WriteLine(e.InnerException.Message); } } }
static async Task DetectChangePoint(IAnomalyDetectorClient client, Request request) { Console.WriteLine("Detecting the change points in the series."); var cpdr = new ChangePointDetectRequest(request.Series, request.Granularity); ChangePointDetectResponse result = await client.ChangePointDetectAsync(cpdr).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."); } }
/// <summary> /// Detect change point for the entire series /// </summary> /// <remarks> /// Evaluate change point score of every series point /// </remarks> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='body'> /// Time series points and granularity is needed. Advanced model parameters can /// also be set in the request if needed. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <ChangePointDetectResponse> ChangePointDetectAsync(this IAnomalyDetectorClient operations, ChangePointDetectRequest body, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.ChangePointDetectWithHttpMessagesAsync(body, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
private void CreateAnomalyDetectorClient() { if (_anomalyDetectorClient != null) { return; } _anomalyDetectorClient = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(_key)) { Endpoint = _endpoint }; }
// <mainMethod> static void Main(string[] args) { string endpoint = "[YOUR_ENDPOINT_URL]"; string key = "[YOUR_SUBSCRIPTION_KEY]"; string path = "[PATH_TO_TIME_SERIES_DATA]"; IAnomalyDetectorClient client = createClient(endpoint, key); runSamples(client, path); Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine(); }
/// <summary> /// Detectar o status de anomalias do último ponto de dados /// </summary> /// <param name="client">Cliente autenticado</param> /// <param name="request">Request com os dados</param> /// <returns></returns> static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request) { Console.WriteLine("\nDetectando o status de anomalias do último ponto de dados"); LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false); if (result.IsAnomaly) { Console.WriteLine("No último registro EXISTE uma anomalia."); } else { Console.WriteLine("No último registro NÃO existe uma anomalia."); } }
static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request) { Console.WriteLine("Detecting the anomaly status of the latest point in the series."); 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."); } }
static void Main(string[] args) { // Preparando nosso ambiente string endpoint = "MEUENDPOINT"; string key = "MINHACHAVEDEAPI"; string datapath = "request-data.csv"; IAnomalyDetectorClient client = createClient(endpoint, key); // autenticando usuario da API Request request = GetSeriesFromFile(datapath); // Gerando uma request com os dados do CSV EntireDetectSampleAsync(client, request).Wait(); // Método assincrono LastDetectSampleAsync(client, request).Wait(); // Método assincrono Console.WriteLine("\nPressione ENTER para sair."); Console.ReadLine(); }
// <mainMethod> static void Main(string[] args) { string location = "westus2"; string endpoint = $"https://{location}.api.cognitive.microsoft.com"; string key = "[YOUR_SUBSCRIPTION_KEY]"; string path = "[PATH_TO_TIME_SERIES_DATA]"; IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client request request = GetSeriesFromFile(datapath); // The request payload with points from the data file EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine(); }
// Necessário criar duas variáveis de ambiente para funcione corretamente. //setx ANOMALY_DETECTOR_KEY 2263789d652847e2a590b20e4f5459b4 //setx ANOMALY_DETECTOR_ENDPOINT https://15netanomalia.cognitiveservices.azure.com/ static void Main(string[] args) { //This sample assumes you have created an environment variable for your key and endpoint string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT"); string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY"); string datapath = "request-data.csv"; IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine(); }
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]); } }
public void TestSineDistributionSeries() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "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); } }
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]); } }
/// <summary> /// Detectar anomalias no conjunto de dados inteiro /// </summary> /// <param name="client">Cliente autenticado</param> /// <param name="request">Request com os dados</param> /// <returns></returns> static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request) { Console.WriteLine("\nDetectando anomalias no conjunto de dados inteiro"); EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false); if (result.IsAnomaly.Contains(true)) { Console.WriteLine("Uma anomalia foi detectada no índice:"); for (int i = 0; i < request.Series.Count; ++i) { if (result.IsAnomaly[i]) { Console.Write(i); Console.Write(" "); } } Console.WriteLine(); } else { Console.WriteLine("Nenhuma anomalia foi detectada neste conjunto de dados."); } }
public void TestLastAnomalySeries() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "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); } }