private static void FillInputParameters(AnomalyDetectionInput inputParameters)
        {
            if(inputParameters != null)
            {
                InputParameters["Max anomalies to return"] = inputParameters.MaxAnomaliesToReturn.ToString();

                InputParameters["Direction"] = inputParameters.Direction;

            
                InputParameters["Alpha"] = inputParameters.Alpha.ToString();

                InputParameters["Only Last"] = inputParameters.OnlyLast;
                InputParameters["Threshold"] = inputParameters.Threshold;

             

                InputParameters["Add Expected Value Column"] = inputParameters.AddExpectedValueColumn.ToString().ToUpper();

                if(inputParameters.IsLongtermTimeSeries != null)
                {
                    InputParameters["Longterm Time Series"] = inputParameters.IsLongtermTimeSeries.ToString().ToUpper();
                }

                InputParameters["Piecewise median time window"] = inputParameters.PiecewiseMedianTimeWindowInWeeks.ToString();

                if(inputParameters.ApplyLogScaling != null)
                {
                    InputParameters["Log Scaling"] = inputParameters.ApplyLogScaling.ToString().ToUpper();

                }

                InputParameters["Remove NAs"] = inputParameters.RemoveNAs.ToString().ToUpper();

                InputParameters["Create Plot"] = inputParameters.CreatePlot.ToString().ToUpper();

                InputParameters["X-axis label"] = inputParameters.XAxisLabel;

                InputParameters["Y-axis label"] = inputParameters.YAxisLabel;

                InputParameters["Title for plot"] = inputParameters.TitleForPlot;
            }

        }
Ejemplo n.º 2
0
        private static void FillInputParameters(AnomalyDetectionInput inputParameters)
        {
            if (inputParameters != null)
            {
                InputParameters["Max anomalies to return"] = inputParameters.MaxAnomaliesToReturn.ToString();

                InputParameters["Direction"] = inputParameters.Direction;


                InputParameters["Alpha"] = inputParameters.Alpha.ToString();

                InputParameters["Only Last"] = inputParameters.OnlyLast;
                InputParameters["Threshold"] = inputParameters.Threshold;



                InputParameters["Add Expected Value Column"] = inputParameters.AddExpectedValueColumn.ToString().ToUpper();

                if (inputParameters.IsLongtermTimeSeries != null)
                {
                    InputParameters["Longterm Time Series"] = inputParameters.IsLongtermTimeSeries.ToString().ToUpper();
                }

                InputParameters["Piecewise median time window"] = inputParameters.PiecewiseMedianTimeWindowInWeeks.ToString();

                if (inputParameters.ApplyLogScaling != null)
                {
                    InputParameters["Log Scaling"] = inputParameters.ApplyLogScaling.ToString().ToUpper();
                }

                InputParameters["Remove NAs"] = inputParameters.RemoveNAs.ToString().ToUpper();

                InputParameters["Create Plot"] = inputParameters.CreatePlot.ToString().ToUpper();

                InputParameters["X-axis label"] = inputParameters.XAxisLabel;

                InputParameters["Y-axis label"] = inputParameters.YAxisLabel;

                InputParameters["Title for plot"] = inputParameters.TitleForPlot;
            }
        }
        static void Main(string[] args)
        {
            AnomalyDetectionInput inputParams = new AnomalyDetectionInput();
            //Process command line arguments
            try
            {
                var result = CommandLine.Parser.Default.ParseArguments<Options>(args);
                if (!result.Errors.Any())
                {
                    string[,] inputData = null;
                    // Values are available here
                    if (!string.IsNullOrEmpty(result.Value.InputFile))
                    {
                        inputParams.InputDataFileName = result.Value.InputFile;
                        inputData = ReadCSVFile(inputParams.InputDataFileName);

                    }else
                    {
                        inputData = GenerateValues();
                    }

                    if (!string.IsNullOrEmpty(result.Value.OutputFile))
                        inputParams.OutputFileName = result.Value.OutputFile;

                    verbose = result.Value.Verbose;
                    if (verbose)
                    {
                        SetVerbose();
                    }

                    //Web Service parameters
                    inputParams.AddExpectedValueColumn = result.Value.AddExpectedValueColumn;
                    inputParams.Alpha = result.Value.Alpha;
                    inputParams.ApplyLogScaling = result.Value.ApplyLogScaling;
                    inputParams.CreatePlot = result.Value.CreatePlot;
                    inputParams.Direction = result.Value.Direction;
                    inputParams.IsLongtermTimeSeries = result.Value.IsLongtermTimeSeries;
                    inputParams.MaxAnomaliesToReturn = result.Value.MaxAnomaliesToReturn;
                    inputParams.OnlyLast = result.Value.OnlyLast;
                    inputParams.PiecewiseMedianTimeWindowInWeeks = result.Value.PiecewiseMedianTimeWindowInWeeks;
                    inputParams.RemoveNAs = result.Value.RemoveNA;
                    inputParams.Threshold = result.Value.Threshold;
                    if(inputParams.CreatePlot)
                    {
                        inputParams.TitleForPlot = result.Value.TitleForPlot;
                        inputParams.XAxisLabel = result.Value.XAxisLabel;
                        inputParams.YAxisLabel = result.Value.YAxisLabel;
                    }

                    generateRandomData = result.Value.GenerateRandomData;
                    if(generateRandomData && inputData == null)
                    {
                        inputData = GenerateValues();
                    }

                    Log.Info("Calling AnomalyWSClient.InvokeRequestResponseService");

                    try
                    {

                        AnomalyWSClient.InvokeRequestResponseService(inputData, inputParams).Wait();

                        Log.Info("End AnomalyWSClient.InvokeRequestResponseService");

                    }
                    catch (Exception ex)
                    {
                        Log.ErrorFormat("Error occurred while processing {0}.", ex.Message);
                        Log.Debug(ex);
                    }

                    Log.Info("Processing complete.");

                }//if
                else
                {
                    if (!(result.Errors.Count() == 1 && result.Errors.Take(1).FirstOrDefault().GetType() == typeof(HelpRequestedError)))
                    {

                        Log.Fatal("Error in input arguments");
                    }
                }

            }
            catch (Exception ex)
            {

                Log.ErrorFormat("Error occurred while processing {0}.",ex.Message);
                Log.Debug(ex);
            }

              //  Console.ReadLine();
        }
Ejemplo n.º 4
0
        public static async Task <Rootobject> InvokeRequestResponseService(string[,] timeSeriesValues, AnomalyDetectionInput inputParameters = null)
        {
            Log.Info("InvokeRequestResponseService");
            if (inputParameters != null)
            {
                Log.Info("Detected input parameters.");
                FillInputParameters(inputParameters);
                if (Log.IsInfoEnabled)
                {
                    PrintInputParameters();
                }
            }


            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary <string, StringTable>()
                    {
                        {
                            "input1",
                            new StringTable()
                            {
                                ColumnNames = new string[] { "timestamp", "count" },
                                // Values = new string[,] {  { "", "0" },  { "", "0" },  }
                                Values = timeSeriesValues
                            }
                        },
                    },
                    GlobalParameters = InputParameters
                };
                string apiKey = ConfigurationManager.AppSettings["AnomalyDetectionApiKey"]; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
                string url = ConfigurationManager.AppSettings["AnomalyDetectionWebServiceUrl"];
                Log.InfoFormat("Url {0}", url);
                client.BaseAddress = new Uri(url);

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)


                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();

                    Log.InfoFormat("Result: {0}", result);


                    if (!string.IsNullOrEmpty(result))
                    {
                        Rootobject ro = JsonConvert.DeserializeObject <Rootobject>(result);

                        Results r = ro.Results;

                        Value v = r.output1.value;

                        string[][] values = v.Values;

                        StringBuilder finalMatrixStrb   = new StringBuilder();
                        StringBuilder columnNamesBuffer = new StringBuilder();
                        foreach (string cn in v.ColumnNames)
                        {
                            columnNamesBuffer.Append(string.Format("{0},", cn));
                        }
                        string columnNames = columnNamesBuffer.ToString();
                        if (columnNames.EndsWith(","))
                        {
                            columnNames = columnNames.Remove(columnNames.LastIndexOf(','), 1);
                        }
                        finalMatrixStrb.AppendLine(columnNames);


                        for (int i = 0; i < values.Length; i++)
                        {
                            StringBuilder eachRow = new StringBuilder();
                            for (int j = 0; j < values[i].Length; j++)
                            {
                                string s  = values[i][j];
                                string si = string.Format("{0},", s);
                                eachRow.Append(si);
                            }
                            string finalRowTemp = eachRow.ToString();
                            if (finalRowTemp.EndsWith(","))
                            {
                                finalRowTemp = finalRowTemp.Remove(finalRowTemp.LastIndexOf(','), 1);
                            }
                            finalMatrixStrb.AppendLine(finalRowTemp);
                        }



                        string resultGuid = System.Guid.NewGuid().ToString("N");
                        string fileName   = string.Format("{0}.csv", resultGuid);

                        if (!string.IsNullOrEmpty(inputParameters.OutputFileName))
                        {
                            fileName = inputParameters.OutputFileName;
                        }
                        File.WriteAllText(fileName, finalMatrixStrb.ToString());

                        string opr = r.output2.value.Values[0][0];


                        JObject jo = JObject.Parse(opr);

                        if (jo != null)
                        {
                            JToken jt       = jo.Last;
                            var    children = jo.Children();
                            string imgb64   = null;
                            int    i        = 0;
                            foreach (JProperty j in children)
                            {
                                if (j.Name == "Graphics Device")
                                {
                                    imgb64 = j.Value.First.Value <string>();

                                    if (!string.IsNullOrEmpty(imgb64))
                                    {
                                        Image ig = Base64ToImage(imgb64);
                                        //string guid = System.Guid.NewGuid().ToString("N");
                                        string fn = Path.GetFileNameWithoutExtension(fileName);
                                        fileName = string.Format("{0}-{1}.png", fn, i++);
                                        ig.Save(fileName);
                                    }
                                }
                            }

                            //var v = imstr.FirstOrDefault();
                        }
                        return(ro);
                    }
                }
                else
                {
                    Log.FatalFormat("The request failed with status code: {0}", response.StatusCode);

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Log.Fatal(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();

                    Log.Fatal(responseContent);
                }
            }

            Log.Info("End InvokeRequestResponseService");
            return(null);
        }
        public static async Task<Rootobject> InvokeRequestResponseService(string[,] timeSeriesValues, AnomalyDetectionInput inputParameters=null)
        {
            Log.Info("InvokeRequestResponseService");
            if(inputParameters != null)
            {
                Log.Info("Detected input parameters.");
                FillInputParameters(inputParameters);
                if(Log.IsInfoEnabled)
                {
                    PrintInputParameters();
                }
                
            }
          

            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {

                    Inputs = new Dictionary<string, StringTable>() {
                        {
                            "input1",
                            new StringTable()
                            {
                               ColumnNames = new string[] {"timestamp", "count"},
                               // Values = new string[,] {  { "", "0" },  { "", "0" },  }
                                Values = timeSeriesValues
                            }
                        },
                                        },
                    GlobalParameters = InputParameters
                };
                string apiKey = ConfigurationManager.AppSettings["AnomalyDetectionApiKey"]; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
                string url = ConfigurationManager.AppSettings["AnomalyDetectionWebServiceUrl"];
                Log.InfoFormat("Url {0}", url);
                client.BaseAddress = new Uri(url);

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)


                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    Log.InfoFormat("Result: {0}", result);
                                     
                
                    if (!string.IsNullOrEmpty(result))
                    {
                        

                        Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(result);

                        Results r = ro.Results;

                        Value v = r.output1.value;

                        string[][] values = v.Values;

                        StringBuilder finalMatrixStrb = new StringBuilder();
                        StringBuilder columnNamesBuffer = new StringBuilder();
                        foreach (string cn in v.ColumnNames)
                        {

                            columnNamesBuffer.Append(string.Format("{0},", cn));
                           
                        }
                        string columnNames = columnNamesBuffer.ToString();
                        if (columnNames.EndsWith(","))
                        {
                            columnNames = columnNames.Remove(columnNames.LastIndexOf(','), 1);
                        }
                        finalMatrixStrb.AppendLine(columnNames);
                       
                                              
                        for(int i = 0; i< values.Length; i++)
                        {
                            StringBuilder eachRow = new StringBuilder();
                            for(int j = 0; j < values[i].Length; j++)
                            {
                                string s = values[i][j];
                                string si = string.Format("{0},", s);
                                eachRow.Append(si);
                                
                            }
                            string finalRowTemp = eachRow.ToString();
                            if (finalRowTemp.EndsWith(","))
                            {
                                finalRowTemp = finalRowTemp.Remove(finalRowTemp.LastIndexOf(','), 1);
                            }
                            finalMatrixStrb.AppendLine(finalRowTemp);
                           
                           
                      
                        }



                        string resultGuid = System.Guid.NewGuid().ToString("N");
                        string fileName = string.Format("{0}.csv", resultGuid);

                        if (!string.IsNullOrEmpty(inputParameters.OutputFileName))
                        {
                            fileName = inputParameters.OutputFileName;
                        }
                        File.WriteAllText(fileName, finalMatrixStrb.ToString());

                        string opr = r.output2.value.Values[0][0];

                     
                        JObject jo = JObject.Parse(opr);

                        if (jo != null)
                        {
                            JToken jt = jo.Last;
                            var children = jo.Children();
                            string imgb64 = null;
                            int i = 0;
                            foreach (JProperty j in children)
                            {
                                if (j.Name == "Graphics Device")
                                {
                                    imgb64 = j.Value.First.Value<string>();

                                    if (!string.IsNullOrEmpty(imgb64))
                                    {
                                        Image ig = Base64ToImage(imgb64);
                                        //string guid = System.Guid.NewGuid().ToString("N");
                                        string fn = Path.GetFileNameWithoutExtension(fileName);
                                        fileName = string.Format("{0}-{1}.png", fn, i++);
                                        ig.Save(fileName);
                                    }
                                }

                            }

                            //var v = imstr.FirstOrDefault();

                        }
                        return ro;
                    }
                }
                else
                {
                    Log.FatalFormat("The request failed with status code: {0}", response.StatusCode);

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Log.Fatal(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();
                    Log.Fatal(responseContent);
                }
            }

            Log.Info("End InvokeRequestResponseService");
            return null;
        }