示例#1
0
        private static IEnumerable <IGrouping <int, T> > ToGroupBy <T>(IClustering <T> engine, IEnumerable <T> source)
        {
            if (engine is null)
            {
                throw new ArgumentNullException(nameof(engine));
            }
            else if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            Dictionary <int, List <T> > result = new();

            foreach (var(item, cluster) in engine.Clusterize(source))
            {
                if (result.TryGetValue(cluster, out var list))
                {
                    list.Add(item);
                }
                else
                {
                    result.Add(cluster, new List <T>()
                    {
                        item
                    });
                }
            }

            int index = 0;

            foreach (var pair in result.OrderBy(p => p.Key))
            {
                yield return(new Grouping <int, T>(index++, pair.Value));
            }
        }
示例#2
0
        public static bool SaveClusteringSolution(IClustering algorithm, string inputFile, string outputFile, List <Facility> facilities)
        {
            if (facilities == null)
            {
                return(false);
            }

            using (StreamWriter writer = new StreamWriter(outputFile))
            {
                ClusteringInfo info = new ClusteringInfo();
                info.date       = DateTime.Now;
                info.inputFile  = inputFile;
                info.outputFile = outputFile;

                // write some information header
                writer.WriteLine(JsonConvert.SerializeObject(info));
                writer.WriteLine(algorithm.GetInfo());

                writer.WriteLine(SEPARATOR_HEADER);

                // write structure it self
                foreach (Facility f in facilities)
                {
                    writer.WriteLine(JsonConvert.SerializeObject(f));
                }
            }

            return(true);
        }
示例#3
0
        public static bool SaveClusteringToMeshLab(IClustering algorithm, string outputFile, List <Facility> facilities, Vertex[] points)
        {
            if (facilities == null)
            {
                return(false);
            }

            using (StreamWriter writer = new StreamWriter(outputFile))
            {
                //ClusteringInfo info = new ClusteringInfo();
                //info.date = DateTime.Now;
                //info.inputFile = inputFile;
                //info.outputFile = outputFile;

                //// write some information header
                //writer.WriteLine(JsonConvert.SerializeObject(info));
                //writer.WriteLine(algorithm.GetInfo());

                //writer.WriteLine(SEPARATOR_HEADER);

                // writer.WriteLine("Cl, x, y, z, nx, ny, nz, k1, k2");

                for (int i = 0; i < facilities.Count; ++i)
                {
                    // writer.Write("{0}", i);

                    Color colorCluster = ToColor(((double)i) / facilities.Count);

                    for (int dim = 0; dim < 3; ++dim)
                    {
                        writer.Write(" {0}", facilities[i].Coords[dim]);
                    }

                    writer.Write(" {0} {1} {2}", colorCluster.R, colorCluster.G, colorCluster.B);

                    writer.Write("\n");

                    foreach (int indexClient in facilities[i].VertexIndices)
                    {
                        // writer.Write("{0}", i);

                        Vertex client = points[indexClient];

                        for (int dim = 0; dim < 3; ++dim)
                        {
                            writer.Write(" {0}", client.Coords[dim]);
                        }

                        writer.Write(" {0} {1} {2}", colorCluster.R, colorCluster.G, colorCluster.B);

                        writer.Write("\n");
                    }
                }
                writer.WriteLine();
            }

            return(true);
        }
示例#4
0
        /// <summary>
        /// Group By Clasters
        /// </summary>
        /// <param name="source">Items to clusterize</param>
        /// <param name="algorithm">Clustering Algorithm</param>
        /// <example>
        /// <code>
        /// var source = new double[][] {
        ///   new double[] {1, 2},
        ///   new double[] {3, 4},
        ///   ...
        /// };
        ///
        /// var result = source.GroupByClusters(new KMeansClustering(p => p, 3));
        /// </code>
        /// </example>
        public static IEnumerable <IGrouping <int, T> > GroupByClusters <T>(
            this IEnumerable <T> source, IClustering <T> algorithm)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            else if (algorithm is null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            return(ToGroupBy(algorithm, source));
        }
        private void Clustering(IClustering clusterAlgorithm)
        {
            DialogResult dialogResult = openClassMethodsFile.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string inputfile = openClassMethodsFile.FileName;

                Dictionary <string, List <string> > rawClasses = ClassParser.ReadClassEntities(inputfile);

                List <INode> clusters = initializeClusters(rawClasses);

                List <INode> clustered = clusterAlgorithm.Clusterize(clusters);

                ReloadTree(clustered[0]);
            }
        }
示例#6
0
        public int CreatePalette(int colors, int colorCountLimit, int maxDimension, string title, RawImage image, IClustering clustering, int thumbnailSize)
        {
            RawImage img = image.ResizeTo(maxDimension);

            int[] vector = img.Vector(colorCountLimit);
            clustering.SetPoints(RawImage.ToColor3Array(vector, this, 0));

            Color3[] p = RawImage.ToColor3Array(vector, this, colors);
            for (int j = 0; j < p.Length; j++)
            {
                clustering.AddCluster(p[j]);
            }

            clustering.Start();
            clustering.Run();

            Color[] palette = PaletteManager.LabSort(RawImage.FromColor3Array(clustering.Clusters(colors), this), Color.Black);

            int id = this.AddPalette(title);

            for (int i = 0; i < palette.Length; i++)
            {
                this.AddColor(palette[i]);
                this.AddPaletteColor(id, palette[i].ToArgb() & 0xffffff, i + 1, (i + 1) >= palette.Length);
            }

            if (thumbnailSize > 0)
            {
                img = img.ResizeTo(thumbnailSize);
                this.AddPaletteThumbnail(id, img.Jpeg(80), 2048);
                img.Clear();
                img = null;
            }

            return(id);
        }
示例#7
0
 private void InitClustering()
 {
     _clustering = clusteringMethodSelector.SelectedClustering;
 }
示例#8
0
        private void ProcessEvent(string ev, Parameters param)
        {
            if (ev == "addfile")
            {
                if (!Utils.CheckTimeKey(this.parameters["Key", String.Empty]))
                {
                    return;
                }

                /*
                 * Default user: 1234
                 * Default password: 1234
                 */
                string hash = "7f271e5f7da0199841293ff2d5b7e677";
                if (Utils.GetLogonHash(this.parameters["Login"], this.Context.Request.Params["password"]) != hash)
                {
                    return;
                }
                try
                {
                    DateTime start = DateTime.Now;

                    string fname;
                    byte[] file = this.GetFile(out fname);
                    if (file == null)
                    {
                        return;
                    }

                    string desc = this.parameters["Description", null];
                    if (String.IsNullOrEmpty(desc))
                    {
                        desc = String.Format("File {0}, uploaded {1}", fname, start.ToString());
                    }

                    RawImage img = RawImage.Load(file);
                    file = null;

                    PaletteManager mgr = new PaletteManager(Utils.ConnectionString);

                    IClustering clustering = this.parameters.Get("", 1L) == 1L ?
                                             Clustering.KMeansClustering(150) :
                                             Clustering.MedianCutClustering();

                    int id = mgr.CreatePalette(8, 2, 600, desc, img, clustering, 96);

                    img = null;

                    double sec = DateTime.Now.Subtract(start).TotalSeconds;

                    this.paletteId = id;
                    this.message   = String.Format("Palette #{0} created, {1} sec.", id, sec.ToString("###0.0#"));
                }
                catch (Exception e)
                {
                    this.errorMessage = "FATAL ERROR: " + e.GetBaseException().Message;
                }
                return;
            }
            if (ev == "clustering")
            {
                if (!Utils.CheckTimeKey(this.parameters["Key", String.Empty]))
                {
                    return;
                }

                /*
                 * Default user: 1234
                 * Default password: 1234
                 */
                string hash = "7f271e5f7da0199841293ff2d5b7e677";
                if (Utils.GetLogonHash(this.parameters["Login"], this.Context.Request.Params["password"]) != hash)
                {
                    return;
                }

                try
                {
                    DateTime start = DateTime.Now;

                    PaletteManager mgr = new PaletteManager(Utils.ConnectionString);
                    Clustering.ClusterPalettes(mgr, 20, 60);

                    double sec = DateTime.Now.Subtract(start).TotalSeconds;
                    this.message = String.Format("Clustering completed, {1} sec.", sec.ToString("###0.0#"));
                }
                catch (Exception e)
                {
                    this.errorMessage = "FATAL ERROR: " + e.GetBaseException().Message;
                }
                return;
            }
        }