示例#1
0
 public void DoIndexNormalization(ILogBuilder log)
 {
     log.log("Global factor normalization [" + DoFactorNormalization + "]");
     if (DoFactorNormalization)
     {
         rangeFinder ranger = new rangeFinder();
         foreach (var pair in index)
         {
             ranger.Learn(pair.Value);
         }
         foreach (var pair in index.ToList())
         {
             index[pair.Key] = ranger.GetPositionInRange(pair.Value);
         }
         log.log("Global factor [" + shortName + "] range [" + ranger.Range.ToString("F5") + "] before normalization.");
     }
     else
     {
     }
 }
        /// <summary>
        /// Normalizes score within domain
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="log">The log.</param>
        /// <returns></returns>
        public static Dictionary <String, rangeFinder> NormalizeWithinDomain(this IEnumerable <DocumentSelectResultEntry> context, ILogBuilder log)
        {
            Dictionary <String, List <DocumentSelectResultEntry> > byDomain = context.GetByDomain(log);
            Dictionary <String, rangeFinder> output = new Dictionary <string, rangeFinder>();

            foreach (var pair in byDomain)
            {
                rangeFinder ranger = new rangeFinder(pair.Key);

                foreach (DocumentSelectResultEntry entry in pair.Value)
                {
                    ranger.Learn(entry.score);
                }

                foreach (DocumentSelectResultEntry entry in pair.Value)
                {
                    entry.score = ranger.GetPositionInRange(entry.score);
                }

                output.Add(ranger.id, ranger);
            }
            return(output);
        }
示例#3
0
        /// <summary>
        /// Renders the specified <see cref="HeatMapModel"/>, optionally saves the output SVG
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public Svg.SvgDocument Render(HeatMapModel model, String filePath = "")
        {
            rangeFinder valueRange = model.DetectMinMax();

            var lColor = style.LowColor.GetColorVersionWithAlpha(style.MinOpacity);  //.ColorToHex();
            var hColor = style.HighColor.GetColorVersionWithAlpha(style.MaxOpacity); //.ColorToHex();



            ColorGradient colorGradient = new ColorGradient(lColor, hColor, ColorGradientFunction.AllAToB);


            cursorZoneSpatialSettings format = style.fieldContainer.GetFormatSetup();

            format.spatialUnit       = 8;
            format.spatialUnitHeight = 10;

            Int32 width  = (model.weight * format.width) + format.margin.right;
            Int32 height = (model.height * format.height) + format.margin.bottom;

            Svg.SvgDocument output = new Svg.SvgDocument
            {
                Width  = width,
                Height = height,
                Ppi    = 100
            };

            var mainContainer = new SvgGroup();

            output.Children.Add(mainContainer);

            //(new SvgLength(width), new SvgLength(height));

            // output.ViewBox = new SvgViewBox(-100, -100, width+100, height+100);

            var group = new SvgGroup();

            mainContainer.Children.Add(group);

            var layerTwo = new SvgGroup();

            mainContainer.Children.Add(layerTwo);

            prepareLabels(model);

            var hor = new SvgGroup();

            if (style.options.HasFlag(HeatMapRenderOptions.addHorizontalLabels))
            {
                layerTwo.Children.Add(hor);
            }

            for (int x = 0; x < model.weight; x++)
            {
                Int32 xStart = x * format.width;

                Svg.SvgText label = xLabels[x].GetSvgText(format, x, -1);

                //Svg.SvgText label = new SvgText(xLabels[x])
                //{
                //    X = (xStart + (format.width / 2) - format.margin.right).Get_px(),
                //    Y = (-format.height / 2).Get_px(),
                //    Color = new SvgColourServer(Color.Black),
                //    Font = "Gulliver"

                //};

                hor.Children.Add(label);

                var vert = new SvgGroup();
                layerTwo.Children.Add(vert);

                var vertLabels = new SvgGroup();
                var vertValues = new SvgGroup();
                var vertScale  = new SvgGroup();

                if (style.options.HasFlag(HeatMapRenderOptions.addVerticalLabels))
                {
                    vert.Children.Add(vertLabels);
                }
                if (style.options.HasFlag(HeatMapRenderOptions.addVerticalValueScale))
                {
                    vert.Children.Add(vertScale);
                }
                if (style.options.HasFlag(HeatMapRenderOptions.addVerticalValueScale))
                {
                    vert.Children.Add(vertValues);
                }

                for (int y = 0; y < model.height; y++)
                {
                    Int32 yStart = y * format.height;

                    if (x == 0)
                    {
                        Double ratio        = valueRange.GetPositionInRange(y); //model.GetRatioForScale(y, style.minimalOpacity, model.height); //(1+ style.minimalOpacity).GetRatio(y+1);
                        Double scaleFactor2 = ratio;
                        if (!style.options.HasFlag(HeatMapRenderOptions.resizeFields))
                        {
                            scaleFactor2 = 1;
                        }

                        if (ratio > 1)
                        {
                            ratio = 1;
                        }
                        var lbl2 = format.GetRectangle((-format.width * 2), yStart, colorGradient.GetColor(ratio), Convert.ToSingle(ratio), scaleFactor2);
                        vertScale.Children.Add(lbl2);

                        Svg.SvgText label2 = yLabels[y].GetSvgText(format, -1, y);
                        vertLabels.Children.Add(label2);

                        //Svg.SvgText label = xLabels[x].GetSvgText(format, x, -1);

                        //Svg.SvgText label2 = new SvgText(yLabels[y])
                        //{
                        //    X = (format.margin.left - format.width).Get_px(),
                        //    Y = (yStart+(format.height / 2)).Get_px(),
                        //    Color = new SvgColourServer(Color.Black),

                        //    Font = "Gulliver"
                        //};

                        Int32 xp = Convert.ToInt32((-Convert.ToDouble(format.width) * 2.5) + format.margin.left);

                        //Double vl = (1.GetRatio(y + 1)) * model.ranger.Maximum;

                        Double vl = model.GetValueForScaleY(y);

                        Svg.SvgText value = vl.ToString(style.valueFormat).GetSvgText(format, -3, y);

                        //Svg.SvgText value = new SvgText()
                        //{
                        //    X = (xp- format.margin.right).Get_px(),
                        //    Y = (yStart + (format.height / 2) ).Get_px(),
                        //    Fill = new SvgColourServer(Color.Black),
                        //   // Color = new SvgColourServer(Color.White),
                        //    Font = "Gulliver"
                        //};

                        vertValues.Children.Add(value);
                    }

                    Double val  = valueRange.GetPositionInRange(model[x, y]); // model.GetRatioValue(x, y, style.minimalOpacity);
                    Color  valC = colorGradient.GetColor(val);


                    Double scaleFactor = val;

                    if (!style.options.HasFlag(HeatMapRenderOptions.resizeFields))
                    {
                        scaleFactor = 1;
                    }
                    var rct = format.GetRectangle(xStart, yStart, valC, Convert.ToSingle(val), scaleFactor);

                    group.Children.Add(rct);
                }
            }

            if (!filePath.isNullOrEmpty())
            {
                if (!filePath.EndsWith(".svg", true, CultureInfo.CurrentCulture))
                {
                    filePath += ".svg";
                }


                output.Save(filePath);

                //  throw new NotImplementedException();

                /* var code = output.GetXML();  //Encoding.UTF8.GetString(stream.GetBuffer());
                 *
                 */
            }

            return(output);
        }
示例#4
0
        /// <summary>
        /// Gets cluster collection
        /// </summary>
        /// <param name="collectionName">Name for the collection.</param>
        /// <param name="result">The result.</param>
        /// <param name="scoreSelector">The score selector.</param>
        /// <returns></returns>
        public DocumentClusterCollection GetClusters(DocumentSimilarityResult result, String collectionName = "Clusters", Func <DocumentSimilarityResultPair, Double> scoreSelector = null, Double minSimScore = Double.MinValue)
        {
            if (minSimScore == Double.MinValue)
            {
                minSimScore = settings.MinScoreInRangeCriterion;
            }

            if (scoreSelector == null)
            {
                scoreSelector = settings.SimilarityScoreSource.GetSelector();
            }

            DocumentClusterCollection output = new DocumentClusterCollection()
            {
                name = collectionName
            };

            var documents     = result.GetDocuments();
            var sortedResults = result.GetAllResults().OrderByDescending(x => scoreSelector).ToList();

            rangeFinder similarityRange = new rangeFinder();

            foreach (var pair in sortedResults)
            {
                similarityRange.Learn(scoreSelector(pair));
            }


            Int32 limit = documents.Count;
            Int32 i     = 0;

            while (documents.Any())
            {
                i++;
                var doc = documents.FirstOrDefault();
                if (doc == null)
                {
                    break;
                }

                var results = result.GetResultsFor(doc);

                DocumentCluster currentCluster = output.NewCluster <DocumentCluster>(); //new DocumentCluster();
                currentCluster.ClusterSeed = doc;

                foreach (KeyValuePair <HtmlNode, DocumentSimilarityResultPair> pair in results)
                {
                    Double scoreAtRange = similarityRange.GetPositionInRange(scoreSelector(pair.Value));
                    if (scoreAtRange > minSimScore)
                    {
                        currentCluster.Add(pair.Key, scoreAtRange);
                        documents.Remove(pair.Key);
                    }
                }

                if (currentCluster.items.Count == 0)
                {
                    output.NullCluster.Add(doc, 0);
                    documents.Remove(doc);
                }
                else
                {
                    documents.Remove(doc);
                    currentCluster.items.Add(doc);
                    output.AddCluster(currentCluster);
                }

                if (i > limit)
                {
                    break;
                }
            }

            foreach (var item in output.NullCluster.items)
            {
                var             results         = result.GetResultsFor(item);
                Double          maxScore        = Double.MinValue;
                DocumentCluster selectedCluster = null;

                foreach (var cluster in output.GetClusters <DocumentCluster>(false))
                {
                    Double score = scoreSelector(results[cluster.ClusterSeed]);
                    if (score > maxScore)
                    {
                        maxScore        = score;
                        selectedCluster = cluster;
                    }
                }

                if (similarityRange.GetPositionInRange(maxScore) > minSimScore)
                {
                    selectedCluster.Add(item, maxScore);
                    output.NullCluster.Remove(item);
                }
                else
                {
                }
            }



            if (settings.ExclusiveClusterMembership)
            {
                var itemToCluster = output.GetItemToClusterAssociations <DocumentCluster>();

                foreach (var pair in itemToCluster)
                {
                    if (pair.Value.Count > 1)
                    {
                        Dictionary <HtmlNode, DocumentSimilarityResultPair> results = result.GetResultsFor(pair.Key);
                        Double          maxScore        = Double.MinValue;
                        DocumentCluster selectedCluster = null;

                        foreach (var cluster in pair.Value)
                        {
                            Double score = scoreSelector(results[cluster.ClusterSeed]);
                            if (score > maxScore)
                            {
                                maxScore        = score;
                                selectedCluster = cluster;
                            }
                        }

                        foreach (var cluster in pair.Value)
                        {
                            if (cluster != selectedCluster)
                            {
                                cluster.Remove(pair.Key);
                            }
                        }
                    }
                }
            }

            output.RemoveEmptyClusters();


            return(output);
        }