public AnomalyDetectionResponse CheckSampleInCluster([FromBody] Sample sample) { int detectedCluster; double[] sampleResult; if (sample.YAxis.HasValue && !sample.ZAxis.HasValue) { sampleResult = new double[] { sample.XAxis, (double)sample.YAxis } } ; else if (sample.YAxis.HasValue && sample.ZAxis.HasValue) { sampleResult = new double[] { sample.XAxis, (double)sample.YAxis, (double)sample.ZAxis } } ; else { sampleResult = new double[] { sample.XAxis } }; try { AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(sample.FilePath, sampleResult, sample.Tolerance); var response = kApi.CheckSample(SampleSettings2, out detectedCluster); return(response); }catch (Exception ex) { return(new AnomalyDetectionResponse(200, ex.Message)); } }
// checks and returns result of pattern testing (1 for matching, 0 otherwise) private static double[] PatternTesting(double[][] rawData, int numClusters, AnomalyDetectionAPI kmeanApi, int tolerance) { CheckingSampleSettings SampleSettings; int clusterIndex; bool fitsPattern; double[] result = new double[rawData.Length / numClusters]; for (int i = 0; i < rawData.Length; i = i + numClusters) { fitsPattern = true; // check each centroid of each function for (int j = 0; j < numClusters; j++) { // check centroids SampleSettings = new CheckingSampleSettings(null, rawData[i + j], tolerance: tolerance); kmeanApi.CheckSample(SampleSettings, out clusterIndex); // if a centroid doesn't belong to any cluster if (clusterIndex == -1) { fitsPattern = false; break; } } if (fitsPattern) { result[i / numClusters] = 1; } else { result[i / numClusters] = 0; } } // contains results of pattern testing (1 for matching, 0 otherwise) return(result); }
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); }
public AnomalyDetectionResponse CheckSample(CheckingSampleSettings Settings, out int ClusterIndex) { Random rnd = new Random(); int n = rnd.Next(1); int c = rnd.Next(2); if (n == 0) { ClusterIndex = c; return(new AnomalyDetectionResponse(0, "This sample belongs to cluster: " + c)); } else { ClusterIndex = -1; return(new AnomalyDetectionResponse(1, "This sample doesn't belong to any cluster")); } }
// 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); }