public void TestClusterCalcuation()
        {
            //
            // In test we know where are positions of centroids.
            // We will now create data around known centroids and let alorithm
            // find centroids.
            double[][] clusterCentars = new double[3][];
            clusterCentars[0] = new double[] { 5.0, 5.0 };
            clusterCentars[1] = new double[] { 15.0, 15.0 };
            clusterCentars[2] = new double[] { 30.0, 30.0 };

            string[] attributes = new string[] { "Height", "Weight" };

            var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5);

            int numAttributes = attributes.Length; // 2 in this demo (height,weight)
            int numClusters   = 3;                 // vary this to experiment (must be between 2 and number data tuples)
            int maxCount      = 300;               // trial and error

            SaveLoadSettings persistenceProviderSettings;

            var resp = SaveLoadSettings.JSON_Settings("model.json", out persistenceProviderSettings, false);

            AnomalyDetectionAPI      kmeanApi        = new AnomalyDetectionAPI(rawData, numClusters);
            ClusteringSettings       clusterSettings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, persistenceProviderSettings, KmeansAlgorithm: 1, Replace: true);
            AnomalyDetectionResponse response        = kmeanApi.ImportNewDataForClustering(clusterSettings);

            Assert.True(response.Code == 0);

            int detectedCluster;

            double[] Sample = new double[] { 26, 28 };
            CheckingSampleSettings SampleSettings = new CheckingSampleSettings(null, Sample, 3);

            response = kmeanApi.CheckSample(SampleSettings, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 0);

            double[] Sample2 = new double[] { 150, 16 };
            CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(null, Sample2, 3);

            response = kmeanApi.CheckSample(SampleSettings2, out detectedCluster);
            Assert.True(response.Code == 1);
            Assert.True(detectedCluster == -1);// Out of all clusters.

            double[] Sample3 = new double[] { 16, 14 };
            CheckingSampleSettings SampleSettings3 = new CheckingSampleSettings(null, Sample3, 3);

            response = kmeanApi.CheckSample(SampleSettings3, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 1);

            double[] Sample4 = new double[] { 6, 4 };
            CheckingSampleSettings SampleSettings4 = new CheckingSampleSettings(null, Sample4, 3);

            response = kmeanApi.CheckSample(SampleSettings4, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 2);
        }
Example #2
0
        public void ContinuousTrainData()
        {
            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 0.2, -4.0 };
            initialCentroids[1] = new double[] { 0.2, -6.0 };
            initialCentroids[2] = new double[] { 0.4, -4.0 };
            initialCentroids[3] = new double[] { 0.4, -6.0 };

            string[] attributes = new string[] { "x", "y" };

            int numAttributes = attributes.Length;  // 2 in this demo (x,y)
            int numClusters   = 4;
            int maxCount      = 300;

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var rawDatalist = getRealDataSample(@"C:\Data\Function1.csv").ToList();

                double[][] oldSamples;

                var nn = kmeanApi.GetPreviousSamples(sett, out oldSamples);

                if (oldSamples != null)
                {
                    foreach (var old in oldSamples)
                    {
                        var row = old.Cast <object>().ToArray();
                        rawDatalist.Add(row);
                    }
                }
                return(rawDatalist.ToArray());
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            api.UseGaussNormalizer();

            var rawData = api.Run() as double[][];

            Helpers.WriteToCSVFile(rawData);

            ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
        }
Example #3
0
        public void ContinuousTrainData2()
        {
            int cnt = 0;

            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 40.0, 10.0 };
            initialCentroids[1] = new double[] { 20.0, 10.0 };
            initialCentroids[2] = new double[] { 40.0, 20.0 };
            initialCentroids[3] = new double[] { 20.0, 20.0 };

            string[] attributes = new string[] { "x", "y" };

            int numAttributes = attributes.Length;  // 2 in this demo (x,y)
            int numClusters   = 4;
            int maxCount      = 300;

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters, initialCentroids);

            LearningApi api = new LearningApi(loadMetaData1());

            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                var rawDatalist = getData(cnt);

                return(rawDatalist);
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            api.UseGaussNormalizer();

            for (int i = 0; i < 15; i++)
            {
                cnt = i;

                var rawData = api.Run() as double[][];

                ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

                AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
            }
        }
        public void Test_GetClusters()
        {
            double[][] clusterCentars = new double[3][];
            clusterCentars[0] = new double[] { 5.0, 5.0 };
            clusterCentars[1] = new double[] { 15.0, 15.0 };
            clusterCentars[2] = new double[] { 30.0, 30.0 };

            double[][] initialCentroids = new double[3][];
            clusterCentars[0] = new double[] { 5.0, 5.0 };
            clusterCentars[1] = new double[] { 15.0, 15.0 };
            clusterCentars[2] = new double[] { 30.0, 30.0 };

            string[] attributes = new string[] { "Height", "Weight" };

            var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5);

            int numAttributes = attributes.Length; // 2 in this demo (height,weight)
            int numClusters   = 3;                 // vary this to experiment (must be between 2 and number data tuples)
            int maxCount      = 300;               // trial and error

            ClusteringSettings Settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(Settings);

            AnomalyDetectionResponse response = kmeanApi.Training(rawData, clusterCentars);

            Assert.True(response.Code == 0);


            Cluster[] clusters;
            response = kmeanApi.GetClusters(null, out clusters); // Path can be null if GetClusters() runs after Training()
            Assert.True(response.Code == 0);
            Assert.True(clusters.Length != 0);
            Assert.True(clusters != null);


            AnomalyDetectionAPI kApi = new AnomalyDetectionAPI();
            string filePath          = $"{Directory.GetCurrentDirectory()}\\Cluster Result\\CheckSample.json";

            Cluster[] clusters1;
            response = kmeanApi.GetClusters(null, out clusters1);
            Assert.True(response.Code == 0);
            Assert.True(clusters.Length != 0);
            Assert.True(clusters != null);
        }
Example #5
0
        public AnomalyDetectionResponse Training([FromBody] TrainingContent trainingContent)
        {
            ClusteringSettings clusterSettings = new ClusteringSettings(trainingContent.KmeansMaxIterations, trainingContent.NumOfClusters, trainingContent.NumOfAttributes, KmeansAlgorithm: 1, Replace: true);

            AnomalyDetectionAPI      kmeanApi = new AnomalyDetectionAPI(clusterSettings);
            AnomalyDetectionResponse response = null;

            try
            {
                double[][] rawData = null;

                foreach (var file in trainingContent.CsvFilePaths)
                {
                    rawData = dataProvider(file);

                    response = kmeanApi.Training(rawData);
                }

                if (response.Code == 0)
                {
                    response = kmeanApi.Save(trainingContent.SavePath);
                }

                return(response);
            }
            catch (Exception Ex)
            {
                if (Ex is System.IO.FileNotFoundException)
                {
                    response = new AnomalyDetectionResponse(200, "File not found");
                }
                else if (Ex is System.IO.DirectoryNotFoundException)
                {
                    response = new AnomalyDetectionResponse(204, "Directory not found");
                }
                else
                {
                    response = new AnomalyDetectionResponse(202, "File cannot be loaded");
                }
                return(response);
            }
        }
Example #6
0
        public void TestObjestWithCentroid()
        {
            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 0.2, -4.0 };
            initialCentroids[1] = new double[] { 0.2, -6.0 };
            initialCentroids[2] = new double[] { 0.4, -4.0 };
            initialCentroids[3] = new double[] { 0.4, -6.0 };

            string[] attributes = new string[] { "x", "y" };

            var data = getRealDataSample(@"C:\Data\Function1.csv");

            List <double[]> list = new List <double[]>();

            foreach (var n in data)
            {
                double[] d = new double[n.Length];

                for (int i = 0; i < n.Length; i++)
                {
                    d[i] = double.Parse(n[i].ToString());
                }


                list.Add(d);
            }

            var rawData       = list.ToArray();
            int numAttributes = attributes.Length; // 2 in this demo (height,weight)
            int numClusters   = 4;                 // vary this to experiment (must be between 2 and number data tuples)
            int maxCount      = 300;               // trial and error

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(rawData, numClusters);

            ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
        }
Example #7
0
        public AnomalyDetectionResponse Training(string[] csvFilePath, string savePath, string loadimpPath, int numClusters, int numOfAttributes, int kmeansMaxIterations)
        {
            ClusteringSettings clusterSettings = new ClusteringSettings(kmeansMaxIterations, numClusters, numOfAttributes, KmeansAlgorithm: 1, Replace: true);

            AnomalyDetectionAPI      kmeanApi = new AnomalyDetectionAPI(clusterSettings);
            AnomalyDetectionResponse response;

            try
            {
                double[][] rawData = null;

                foreach (var file in csvFilePath)
                {
                    rawData = dataProvider(file);
                }

                response = kmeanApi.Training(rawData);

                response = kmeanApi.Save(savePath);

                return(response);
            }
            catch (Exception Ex)
            {
                if (Ex is System.IO.FileNotFoundException)
                {
                    response = new AnomalyDetectionResponse(200, "File not found");
                }
                else if (Ex is System.IO.DirectoryNotFoundException)
                {
                    response = new AnomalyDetectionResponse(204, "Directory not found");
                }
                else
                {
                    response = new AnomalyDetectionResponse(202, "File cannot be loaded");
                }
                return(response);
            }
        }
Example #8
0
        public void TestWithNormalize_GaussAndCentroid()
        {
            double[][] initialCentroids = new double[4][];
            initialCentroids[0] = new double[] { 0.2, -4.0 };
            initialCentroids[1] = new double[] { 0.2, -6.0 };
            initialCentroids[2] = new double[] { 0.4, -4.0 };
            initialCentroids[3] = new double[] { 0.4, -6.0 };

            string[] attributes = new string[] { "x", "y" };
            // Creates learning api object
            LearningApi api = new LearningApi(loadMetaData1());

            //Real dataset must be defined as object type, because data can be numeric, binary and classification
            api.UseActionModule <object[][], object[][]>((input, ctx) =>
            {
                return(getRealDataSample(@"C:\Data\Function15.csv"));
            });

            //this call must be first in the pipeline
            api.UseDefaultDataMapper();

            api.UseGaussNormalizer();

            var rawData = api.Run() as double[][];

            int numAttributes = attributes.Length; // 2 in this demo (height,weight)
            int numClusters   = 4;                 // vary this to experiment (must be between 2 and number data tuples)
            int maxCount      = 300;               // trial and error

            SaveLoadSettings sett;

            var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function15.json", out sett, true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(rawData, numClusters);

            ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings);
        }
        public void Test_BalancedClusters()
        {
            // prepare the 2d functions for clustering
            double[][] mFun = Helpers.cSVtoDoubleJaggedArray(@"C:\Users\KywAnn\Desktop\Anomaly Detection Functions Normalized\Sine 3 HP\Sine 3 HP.csv");
            // normalize functions
            mFun = NormalizeData(mFun);
            // load the original function
            double[][] rawData = getSimilarFunctionsData(mFun, 1);

            // Settings for the K-Means Alg
            int maxCount            = 500;
            int maxNumberOfClusters = 5;
            int minNumberOfClusters = 2;
            int numAttributes       = 2;
            int KAlg = 2;
            // recommended number of clusters
            int recNumClusters;
            ClusteringSettings  clusterSettings = new ClusteringSettings(maxCount, 2, numAttributes, KmeansAlgorithm: KAlg, Replace: true);
            AnomalyDetectionAPI kmeanApi        = new AnomalyDetectionAPI(clusterSettings);
            // get the suggested number of clusters bas on the balanced clusters method
            AnomalyDetectionResponse ar = kmeanApi.RecommendedNumberOfClusters(rawData, maxCount, numAttributes, maxNumberOfClusters, minNumberOfClusters, 3, null, out recNumClusters, kmeansAlgorithm: KAlg);
        }
        public KmeansDataController()
        {
            if (numofclstrs == 0)
            {
                numofclstrs = 3;
            }

            int numberofclusters = 3;
            /// desired number of clusters
            int numberofattributes = 2;

            /// X,Y,Z components


            double[][] result = MockApi.MockApi.CSVtoDoubleJaggedArray(@"F:\ProData\data.csv");
            /// CSV to Array Conversion
            if (System.IO.File.Exists(@"F:\ProData\data.csv"))
            {
                numberofattributes = result[0].Length;
            }
            /// Number of attributes are 3 for 3D and 2 for 2D
            IAnomalyDetectionApi X = new AnomalyDetectionApi.AnomalyDetectionAPI(result, numofclstrs);
            ClusteringSettings   Y = new ClusteringSettings(result, 10, numofclstrs, numberofattributes, @"F:\ProData\cluster.json", Replace: true);

            /// Set number of clusters, max iterations and give data in result
            X.ImportNewDataForClustering(Y);
            /// CheckSample is a function that detect to which cluster the given sample belongs to.
            ClusteringResults[] r;
            /// All the Kmeans results needed for plotting
            AnomalyDetectionResponse Z = X.GetResults(@"F:\ProData\cluster.json", out r);

            /// Codes for Errors and Success
            //if (PgaeLoadFlag == 1) //use this only for first time page load
            //{
            if (r != null)                             // in case result contains some value we need to add results
            {
                if (kmeansdata.IsValueCreated == true) //if already points are created we need to reinitiallize
                {
                    checksample = kmeansdata.Value[0].CheckSampleResult;
                    kmeansdata  = new Lazy <List <KmeansData> >();
                }
                int i = 0;                                            //Counts number of Samples we have
                for (int j = 0; j < numofclstrs; j++)                 //Runs for numberofclusters defined earlier
                {
                    for (int k = 0; k < r[j].ClusterData.Length; k++) // add the data in web Api
                    {
                        if (numberofattributes == 3)                  // add data for 2D
                        {
                            kmeansdata.Value.Add(new KmeansData {
                                ID = i, ClustersDataLength = r[j].ClusterData.Length, TotalCentroid = r.Length, CentroidIdNumber = j, CentroidX = r[j].Centroid[0], CentroidY = r[j].Centroid[1], CentroidZ = r[j].Centroid[2], Xaxis = r[j].ClusterData[k][0], Yaxis = r[j].ClusterData[k][1], Zaxis = r[j].ClusterData[k][2], ClusterDataDistanceToCentroid = r[j].ClusterDataDistanceToCentroid[k], ClusterDataOriginalIndex = r[j].ClusterDataOriginalIndex[k], ClusterOfNearestForeignSample = r[j].ClusterOfNearestForeignSample, DistanceToNearestClusterCentroid = r[j].DistanceToNearestClusterCentroid, DistanceToNearestForeignSample = r[j].DistanceToNearestForeignSample, DistanceToNearestForeignSampleInNearestCluster = r[j].DistanceToNearestForeignSampleInNearestCluster, InClusterFarthestSampleX = r[j].InClusterFarthestSample[0], InClusterFarthestSampleY = r[j].InClusterFarthestSample[1], InClusterFarthestSampleZ = r[j].InClusterFarthestSample[2], InClusterFarthestSampleIndex = r[j].InClusterFarthestSampleIndex, InClusterMaxDistance = r[j].InClusterMaxDistance, MeanX = r[j].Mean[0], MeanY = r[j].Mean[1], MeanZ = r[j].Mean[2], NearestCluster = r[j].NearestCluster, NearestForeignSampleX = r[j].NearestForeignSample[0], NearestForeignSampleY = r[j].NearestForeignSample[1], NearestForeignSampleZ = r[j].NearestForeignSample[2], NearestForeignSampleInNearestClusterX = r[j].NearestForeignSampleInNearestCluster[0], NearestForeignSampleInNearestClusterY = r[j].NearestForeignSampleInNearestCluster[1], NearestForeignSampleInNearestClusterZ = r[j].NearestForeignSampleInNearestCluster[2], NumberOfAttribs = numberofattributes
                            });
                        }
                        if (numberofattributes == 2)    // add data for 3D
                        {
                            kmeansdata.Value.Add(new KmeansData {
                                ID = i, ClustersDataLength = r[j].ClusterData.Length, TotalCentroid = r.Length, CentroidIdNumber = j, CentroidX = r[j].Centroid[0], CentroidY = r[j].Centroid[1], Xaxis = r[j].ClusterData[k][0], Yaxis = r[j].ClusterData[k][1], ClusterDataDistanceToCentroid = r[j].ClusterDataDistanceToCentroid[k], ClusterDataOriginalIndex = r[j].ClusterDataOriginalIndex[k], ClusterOfNearestForeignSample = r[j].ClusterOfNearestForeignSample, DistanceToNearestClusterCentroid = r[j].DistanceToNearestClusterCentroid, DistanceToNearestForeignSample = r[j].DistanceToNearestForeignSample, DistanceToNearestForeignSampleInNearestCluster = r[j].DistanceToNearestForeignSampleInNearestCluster, InClusterFarthestSampleX = r[j].InClusterFarthestSample[0], InClusterFarthestSampleY = r[j].InClusterFarthestSample[1], InClusterFarthestSampleIndex = r[j].InClusterFarthestSampleIndex, InClusterMaxDistance = r[j].InClusterMaxDistance, MeanX = r[j].Mean[0], MeanY = r[j].Mean[1], NearestCluster = r[j].NearestCluster, NearestForeignSampleX = r[j].NearestForeignSample[0], NearestForeignSampleY = r[j].NearestForeignSample[1], NearestForeignSampleInNearestClusterX = r[j].NearestForeignSampleInNearestCluster[0], NearestForeignSampleInNearestClusterY = r[j].NearestForeignSampleInNearestCluster[1], NumberOfAttribs = numberofattributes
                            });
                        }
                        i++;
                    }
                }

                //   PgaeLoadFlag++;
                //}
                kmeansdata.Value[0].CheckSampleResult = checksample;
            }
        }
        // POST api/product

        /*
         * public void KmeansDataAdd(KmeansData data) //post method
         * {
         *
         *   data.ID = ProductID;
         *   kmeansdata.Value.Add(data); //add the post product data to the product list
         *   ProductID++;
         *   //instead of adding product data to the static product list you can save data to the database.
         * }
         * /*/
        public void KmeansDataCheck(KmeansData data) //post method
        {
            if (data.NumberOfClstrs != 0 && data.NumberOfClstrs != null)
            {
                numofclstrs = data.NumberOfClstrs;
                //  if (System.IO.File.Exists(@"F:\ProData\cluster.json"))// in case some files exist already delete them and upload the current one
                //  {
                //      System.IO.File.Delete(@"F:\ProData\cluster.json");
                //      System.IO.File.Delete(@"F:\ProData\Result\cluster.json");
                //  }
                int numberofattributes = 2;
                /// X,Y,Z components


                double[][] result = MockApi.MockApi.CSVtoDoubleJaggedArray(@"F:\ProData\data.csv");
                /// CSV to Array Conversion
                if (System.IO.File.Exists(@"F:\ProData\data.csv"))
                {
                    numberofattributes = result[0].Length;
                }
                /// Number of attributes are 3 for 3D and 2 for 2D
                IAnomalyDetectionApi X = new AnomalyDetectionApi.AnomalyDetectionAPI(result, numofclstrs);
                ClusteringSettings   Y = new ClusteringSettings(result, 10, numofclstrs, numberofattributes, @"F:\ProData\cluster.json", Replace: true);
                /// Set number of clusters, max iterations and give data in result
                AnomalyDetectionResponse AB = X.ImportNewDataForClustering(Y);
                /// CheckSample is a function that detect to which cluster the given sample belongs to.
                ClusteringResults[] r;
                /// All the Kmeans results needed for plotting
                AnomalyDetectionResponse Z = X.GetResults(@"F:\ProData\cluster.json", out r);
                /// Codes for Errors and Success
                //if (PgaeLoadFlag == 1) //use this only for first time page load
                //{
                //    if (r != null)// in case result contains some value we need to add results
                //   {
                if (kmeansdata.IsValueCreated == true)    //if already points are created we need to reinitiallize
                {
                    checksample = kmeansdata.Value[0].CheckSampleResult;
                    kmeansdata  = new Lazy <List <KmeansData> >();
                }
                int i = 0;                                            //Counts number of Samples we have
                for (int j = 0; j < numofclstrs; j++)                 //Runs for numberofclusters defined earlier
                {
                    for (int k = 0; k < r[j].ClusterData.Length; k++) // add the data in web Api
                    {
                        if (numberofattributes == 3)                  // add data for 2D
                        {
                            kmeansdata.Value.Add(new KmeansData {
                                ID = i, ClustersDataLength = r[j].ClusterData.Length, TotalCentroid = r.Length, CentroidIdNumber = j, CentroidX = r[j].Centroid[0], CentroidY = r[j].Centroid[1], CentroidZ = r[j].Centroid[2], Xaxis = r[j].ClusterData[k][0], Yaxis = r[j].ClusterData[k][1], Zaxis = r[j].ClusterData[k][2], ClusterDataDistanceToCentroid = r[j].ClusterDataDistanceToCentroid[k], ClusterDataOriginalIndex = r[j].ClusterDataOriginalIndex[k], ClusterOfNearestForeignSample = r[j].ClusterOfNearestForeignSample, DistanceToNearestClusterCentroid = r[j].DistanceToNearestClusterCentroid, DistanceToNearestForeignSample = r[j].DistanceToNearestForeignSample, DistanceToNearestForeignSampleInNearestCluster = r[j].DistanceToNearestForeignSampleInNearestCluster, InClusterFarthestSampleX = r[j].InClusterFarthestSample[0], InClusterFarthestSampleY = r[j].InClusterFarthestSample[1], InClusterFarthestSampleZ = r[j].InClusterFarthestSample[2], InClusterFarthestSampleIndex = r[j].InClusterFarthestSampleIndex, InClusterMaxDistance = r[j].InClusterMaxDistance, MeanX = r[j].Mean[0], MeanY = r[j].Mean[1], MeanZ = r[j].Mean[2], NearestCluster = r[j].NearestCluster, NearestForeignSampleX = r[j].NearestForeignSample[0], NearestForeignSampleY = r[j].NearestForeignSample[1], NearestForeignSampleZ = r[j].NearestForeignSample[2], NearestForeignSampleInNearestClusterX = r[j].NearestForeignSampleInNearestCluster[0], NearestForeignSampleInNearestClusterY = r[j].NearestForeignSampleInNearestCluster[1], NearestForeignSampleInNearestClusterZ = r[j].NearestForeignSampleInNearestCluster[2], NumberOfAttribs = numberofattributes
                            });
                        }
                        if (numberofattributes == 2)    // add data for 3D
                        {
                            kmeansdata.Value.Add(new KmeansData {
                                ID = i, ClustersDataLength = r[j].ClusterData.Length, TotalCentroid = r.Length, CentroidIdNumber = j, CentroidX = r[j].Centroid[0], CentroidY = r[j].Centroid[1], Xaxis = r[j].ClusterData[k][0], Yaxis = r[j].ClusterData[k][1], ClusterDataDistanceToCentroid = r[j].ClusterDataDistanceToCentroid[k], ClusterDataOriginalIndex = r[j].ClusterDataOriginalIndex[k], ClusterOfNearestForeignSample = r[j].ClusterOfNearestForeignSample, DistanceToNearestClusterCentroid = r[j].DistanceToNearestClusterCentroid, DistanceToNearestForeignSample = r[j].DistanceToNearestForeignSample, DistanceToNearestForeignSampleInNearestCluster = r[j].DistanceToNearestForeignSampleInNearestCluster, InClusterFarthestSampleX = r[j].InClusterFarthestSample[0], InClusterFarthestSampleY = r[j].InClusterFarthestSample[1], InClusterFarthestSampleIndex = r[j].InClusterFarthestSampleIndex, InClusterMaxDistance = r[j].InClusterMaxDistance, MeanX = r[j].Mean[0], MeanY = r[j].Mean[1], NearestCluster = r[j].NearestCluster, NearestForeignSampleX = r[j].NearestForeignSample[0], NearestForeignSampleY = r[j].NearestForeignSample[1], NearestForeignSampleInNearestClusterX = r[j].NearestForeignSampleInNearestCluster[0], NearestForeignSampleInNearestClusterY = r[j].NearestForeignSampleInNearestCluster[1], NumberOfAttribs = numberofattributes
                            });
                        }
                        i++;
                    }
                }

                //   PgaeLoadFlag++;
                //}
                kmeansdata.Value[0].CheckSampleResult = checksample;
            }

            //   }
            else
            {
                numofclstrs = 3;
                int numberofclusters = 3;
                /// desired number of clusters
                int numberofattributes = 3;
                /// X,Y,Z components
                double[][] result = MockApi.MockApi.CSVtoDoubleJaggedArray(@"F:\ProData\data.csv");
                /// CSV to Array Conversion
                if (System.IO.File.Exists(@"F:\ProData\data.csv"))
                {
                    numberofattributes = result[0].Length;
                }
                if (numberofattributes == data.ClustersDataLength)
                {
                    /// Number of attributes are 3 for 3D and 2 for 2D
                    IAnomalyDetectionApi X = new AnomalyDetectionApi.AnomalyDetectionAPI(result, numofclstrs);
                    ClusteringSettings   Y = new ClusteringSettings(result, 10, numofclstrs, numberofattributes, @"F:\ProData\cluster.json");
                    /// Set number of clusters, max iterations and give data in result
                    X.ImportNewDataForClustering(Y);
                    /// CheckSample is a function that detect to which cluster the given sample belongs to.
                    ClusteringResults[] r;
                    /// All the Kmeans results needed for plotting
                    AnomalyDetectionResponse Z = X.GetResults(@"F:\ProData\cluster.json", out r);

                    if (numberofattributes == 3)
                    {
                        double[] sample = new double[3] {
                            data.XaxisCheck, data.YaxisCheck, data.ZaxisCheck
                        };
                        CheckingSampleSettings A = new CheckingSampleSettings(@"F:\ProData\cluster.json", sample, 10);
                        int N;
                        Z = X.CheckSample(A, out N);
                        data.CheckSampleResult = Z.Message;
                        kmeansdata.Value[0].CheckSampleResult = Z.Message;
                    }
                    if (numberofattributes == 2)
                    {
                        double[] sample = new double[2] {
                            data.XaxisCheck, data.YaxisCheck
                        };
                        CheckingSampleSettings A = new CheckingSampleSettings(@"F:\ProData\cluster.json", sample, 10);
                        int N;
                        Z = X.CheckSample(A, out N);
                        data.CheckSampleResult = Z.Message;
                        kmeansdata.Value[0].CheckSampleResult = Z.Message;
                    }
                }
                else
                {
                    kmeansdata.Value[0].CheckSampleResult = "Your Data attributes are invalid ! Please Enter number of Cordinates according to the File Uploaded !";
                }
            }
            //kmeansdata.Value.Add.();
            //data.XaxisCheck = ProductID;
            //kmeansdata.Value.Add(data); //add the post product data to the product list
            //ProductID++;
            //instead of adding product data to the static product list you can save data to the database.
        }
        public void Test_CheckSample()
        {
            double[][] clusterCentars = new double[3][];
            clusterCentars[0] = new double[] { 5.0, 5.0 };
            clusterCentars[1] = new double[] { 15.0, 15.0 };
            clusterCentars[2] = new double[] { 30.0, 30.0 };

            double[][] initialCentroids = new double[3][];
            clusterCentars[0] = new double[] { 5.0, 5.0 };
            clusterCentars[1] = new double[] { 15.0, 15.0 };
            clusterCentars[2] = new double[] { 30.0, 30.0 };

            string[] attributes = new string[] { "Height", "Weight" };

            var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5);

            int numAttributes = attributes.Length; // 2 in this demo (height,weight)
            int numClusters   = 3;                 // vary this to experiment (must be between 2 and number data tuples)
            int maxCount      = 300;               // trial and error

            ClusteringSettings Settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, InitialGuess: true, Replace: true);

            AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(Settings);

            AnomalyDetectionResponse response = kmeanApi.Training(rawData, clusterCentars);

            Assert.True(response.Code == 0);

            response = kmeanApi.Save("CheckSample.json");
            Assert.True(response.Code == 0);

            int detectedCluster;

            double[] Sample = new double[] { 26, 28 };
            CheckingSampleSettings SampleSettings = new CheckingSampleSettings(null, Sample, 3); //If path is null you should run Training()

            response = kmeanApi.CheckSample(SampleSettings, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 2);

            AnomalyDetectionAPI kApi = new AnomalyDetectionAPI();
            string filePath          = $"{Directory.GetCurrentDirectory()}\\Instance Result\\CheckSample.json";

            double[] Sample2 = new double[] { 150, 16 };
            CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(filePath, Sample2, 3);

            response = kApi.CheckSample(SampleSettings2, out detectedCluster);
            Assert.True(response.Code == 1);
            Assert.True(detectedCluster == -1);// Out of all clusters.

            double[] Sample3 = new double[] { 16, 14 };
            CheckingSampleSettings SampleSettings3 = new CheckingSampleSettings(filePath, Sample3, 3);

            response = kApi.CheckSample(SampleSettings3, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 1);

            double[] Sample4 = new double[] { 6, 4 };
            CheckingSampleSettings SampleSettings4 = new CheckingSampleSettings(filePath, Sample4, 3);

            response = kApi.CheckSample(SampleSettings4, out detectedCluster);
            Assert.True(response.Code == 0);
            Assert.True(detectedCluster == 0);
        }
        public static IList <BrightnessDataPoint> AnomalyDetectionResponseToBrightnessData(AnomalyDetectionResponse response)
        {
            ArgumentCheck.IsNull(response, "response");

            var anomalousValues = response.Results.AnomalyDetectionResult.Value.Values;

            var result = new List <BrightnessDataPoint>();

            for (var i = 0; i < anomalousValues.GetLength(0); i++)
            {
                var brightnessDataPoint = new BrightnessDataPoint(
                    DateTime.Parse(anomalousValues[i, 0]),
                    Byte.Parse(anomalousValues[i, 1]));

                result.Add(brightnessDataPoint);
            }

            return(result);
        }