Exemple #1
0
        public static async Task RunAsync(string endpoint, string key)
        {
            Console.WriteLine("Sample of detecting anomalies in the entire series.");

            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            // Detection
            Request request = Program.GetRequest();

            request.MaxAnomalyRatio = 0.25;
            request.Sensitivity     = 95;
            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("Anomaly was detected from the series 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("There is no anomaly detected from the series.");
            }
        }
        //--------------------------------
        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.");
            }
        }
Exemple #3
0
        public async Task DetectEntireSeriesAnomaly()
        {
            #region Snippet:CreateAnomalyDetectorClient

            //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");

            #endregion

            #region Snippet:ReadSeriesData

            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(DateTime.Parse(e[0]), float.Parse(e[1]))).ToList();

            //create request
            DetectRequest request = new DetectRequest(list, TimeGranularity.Daily);

            #endregion

            #region Snippet:DetectEntireSeriesAnomaly

            //detect
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.DetectEntireSeriesAsync(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.");
            }

            #endregion
        }
        public async Task <Response <EntireDetectResponse> > DetectEntireSeriesAsync(DetectRequest body, CancellationToken cancellationToken = default)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            using var message = CreateDetectEntireSeriesRequest(body);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                EntireDetectResponse value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = EntireDetectResponse.DeserializeEntireDetectResponse(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
Exemple #5
0
        /// <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.");
            }
        }
Exemple #6
0
        public static async Task RunAsync(string endpoint, string key)
        {
            Console.WriteLine("Sample of detecting anomalies in the entire series.");

            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            // Create time series
            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"), 0),
                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"), 0),
                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)
            };

            // Detection
            Request request = new Request(series, Granularity.Monthly);

            request.MaxAnomalyRatio = 0.25;
            request.Sensitivity     = 95;
            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("Anomaly was detected from the series at index:");
                for (int i = 0; i < series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("There is no anomaly detected from the series.");
            }
        }
        public async Task DetectEntireSeriesAnomaly()
        {
            #region Snippet:CreateAnomalyDetectorClient

            //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);

            #endregion

            #region Snippet:ReadSeriesData

            //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
            DetectRequest request = new DetectRequest(list)
            {
                Granularity = TimeGranularity.Daily
            };

            #endregion

            #region Snippet:DetectEntireSeriesAnomaly

            //detect
            Console.WriteLine("Detecting anomalies in the entire time series.");

            try
            {
                EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false);

                bool hasAnomaly = false;
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.WriteLine("An anomaly was detected at index: {0}.", i);
                        hasAnomaly = true;
                    }
                }
                if (!hasAnomaly)
                {
                    Console.WriteLine("No anomalies detected in the series.");
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(String.Format("Entire detection failed: {0}", ex.Message));
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
                throw;
            }
            #endregion
        }