Esempio n. 1
0
            public static size_t BrotliHistogramCombine(HistogramDistance *out_,
                                                        uint *cluster_size,
                                                        uint *symbols,
                                                        uint *clusters,
                                                        HistogramPair *pairs,
                                                        size_t num_clusters,
                                                        size_t symbols_size,
                                                        size_t max_clusters,
                                                        size_t max_num_pairs)
            {
                double cost_diff_threshold = 0.0;
                size_t min_cluster_size    = 1;
                size_t num_pairs           = 0;

                {
                    /* We maintain a vector of histogram pairs, with the property that the pair
                     * with the maximum bit cost reduction is the first. */
                    size_t idx1;
                    for (idx1 = 0; idx1 < num_clusters; ++idx1)
                    {
                        size_t idx2;
                        for (idx2 = idx1 + 1; idx2 < num_clusters; ++idx2)
                        {
                            BrotliCompareAndPushToQueue(out_, cluster_size, clusters[idx1],
                                                        clusters[idx2], max_num_pairs, &pairs[0], &num_pairs);
                        }
                    }
                }

                while (num_clusters > min_cluster_size)
                {
                    uint   best_idx1;
                    uint   best_idx2;
                    size_t i;
                    if (pairs[0].cost_diff >= cost_diff_threshold)
                    {
                        cost_diff_threshold = 1e99;
                        min_cluster_size    = max_clusters;
                        continue;
                    }
                    /* Take the best pair from the top of heap. */
                    best_idx1 = pairs[0].idx1;
                    best_idx2 = pairs[0].idx2;
                    HistogramDistance.HistogramAddHistogram(&out_[best_idx1], &out_[best_idx2]);
                    out_[best_idx1].bit_cost_ = pairs[0].cost_combo;
                    cluster_size[best_idx1]  += cluster_size[best_idx2];
                    for (i = 0; i < symbols_size; ++i)
                    {
                        if (symbols[i] == best_idx2)
                        {
                            symbols[i] = best_idx1;
                        }
                    }
                    for (i = 0; i < num_clusters; ++i)
                    {
                        if (clusters[i] == best_idx2)
                        {
                            memmove(&clusters[i], &clusters[i + 1],
                                    (num_clusters - i - 1) * sizeof(uint));
                            break;
                        }
                    }
                    --num_clusters;
                    {
                        /* Remove pairs intersecting the just combined best pair. */
                        size_t copy_to_idx = 0;
                        for (i = 0; i < num_pairs; ++i)
                        {
                            HistogramPair *p = &pairs[i];
                            if (p->idx1 == best_idx1 || p->idx2 == best_idx1 ||
                                p->idx1 == best_idx2 || p->idx2 == best_idx2)
                            {
                                /* Remove invalid pair from the queue. */
                                continue;
                            }
                            if (HistogramPairIsLess(&pairs[0], p))
                            {
                                /* Replace the top of the queue if needed. */
                                HistogramPair front = pairs[0];
                                pairs[0]           = *p;
                                pairs[copy_to_idx] = front;
                            }
                            else
                            {
                                pairs[copy_to_idx] = *p;
                            }
                            ++copy_to_idx;
                        }
                        num_pairs = copy_to_idx;
                    }

                    /* Push new pairs formed with the combined histogram to the heap. */
                    for (i = 0; i < num_clusters; ++i)
                    {
                        BrotliCompareAndPushToQueue(out_, cluster_size, best_idx1, clusters[i],
                                                    max_num_pairs, &pairs[0], &num_pairs);
                    }
                }
                return(num_clusters);
            }
Esempio n. 2
0
        public BudgetSegregation Calculate(string dodName, DirectoryInfo analysisFolder, DoDBase dod, Project.Masks.AttributeFieldMask mask)
        {
            // Build the budget segregation result set object that will be returned. This determines paths
            BudgetSegregation bsResult = new BudgetSegregation(dodName, analysisFolder, mask, dod);

            // Retrieve the segregated statistics from the DoD rasters depending on the thresholding type used.
            Dictionary <string, GCDConsoleLib.GCD.DoDStats> results = null;

            if (dod is DoDMinLoD)
            {
                results = RasterOperators.GetStatsMinLoD(dod.RawDoD.Raster, ((DoDMinLoD)dod).Threshold, mask.Vector, mask._Field, ProjectManager.Project.Units, ProjectManager.OnProgressChange);
            }
            else
            {
                Raster propErr = ((DoDPropagated)dod).PropagatedError;

                if (dod is DoDProbabilistic)
                {
                    results = RasterOperators.GetStatsProbalistic(dod.RawDoD.Raster, dod.ThrDoD.Raster, propErr, mask.Vector, mask._Field, ProjectManager.Project.Units, ProjectManager.OnProgressChange);
                }
                else
                {
                    results = RasterOperators.GetStatsPropagated(dod.RawDoD.Raster, propErr, mask.Vector, mask._Field, ProjectManager.Project.Units, ProjectManager.OnProgressChange);
                }
            }

            // Retrieve the histograms for all budget segregation classes
            Dictionary <string, Histogram> rawHistos = RasterOperators.BinRaster(dod.RawDoD.Raster, DEFAULTHISTOGRAMNUMBER, mask.Vector, mask._Field, ProjectManager.OnProgressChange);
            Dictionary <string, Histogram> thrHistos = RasterOperators.BinRaster(dod.ThrDoD.Raster, DEFAULTHISTOGRAMNUMBER, mask.Vector, mask._Field, ProjectManager.OnProgressChange);

            decimal defaultBinWidth = 1;

            if (rawHistos.Count > 0)
            {
                defaultBinWidth = (decimal)rawHistos.Values.First().BinWidth(ProjectManager.Project.Units).As(ProjectManager.Project.Units.VertUnit);
            }

            // Make sure that the output folder and the folder for the figures exist
            analysisFolder.Create();
            DoDBase.FiguresFolderPath(analysisFolder).Create();

            // Build the output necessary output files
            int           classIndex = 1;
            StringBuilder legendText = new StringBuilder("Class Index, Class Name");

            foreach (KeyValuePair <string, GCDConsoleLib.GCD.DoDStats> segClass in results)
            {
                if (!rawHistos.ContainsKey(segClass.Key))
                {
                    rawHistos.Add(segClass.Key, new Histogram(DEFAULTHISTOGRAMNUMBER, defaultBinWidth));
                }

                if (!thrHistos.ContainsKey(segClass.Key))
                {
                    thrHistos.Add(segClass.Key, new Histogram(DEFAULTHISTOGRAMNUMBER, defaultBinWidth));
                }

                legendText.AppendLine(string.Format("{0},{1}", classIndex, segClass.Key));

                string   filePrefix = string.Format("c{0:000}", classIndex);
                FileInfo sumaryXML  = new FileInfo(Path.Combine(analysisFolder.FullName, string.Format("{0}_summary.xml", filePrefix)));
                FileInfo rawHstPth  = new FileInfo(Path.Combine(analysisFolder.FullName, string.Format("{0}_raw.csv", filePrefix)));
                FileInfo thrHstPth  = new FileInfo(Path.Combine(analysisFolder.FullName, string.Format("{0}_thr.csv", filePrefix)));

                GenerateSummaryXML(segClass.Value, sumaryXML);
                GenerateChangeBarGraphicFiles(analysisFolder, segClass.Value, 600, 600, filePrefix);

                WriteHistogram(rawHistos[segClass.Key], rawHstPth);
                WriteHistogram(thrHistos[segClass.Key], thrHstPth);

                HistogramPair histograms = new HistogramPair(rawHistos[segClass.Key], rawHstPth, thrHistos[segClass.Key], thrHstPth);

                BudgetSegregationClass bsClass = new BudgetSegregationClass(segClass.Key, segClass.Value, histograms, sumaryXML);
                bsResult.Classes[segClass.Key] = bsClass;

                classIndex++;
            }

            // Generate the new inter-comparison spreadsheet
            FileInfo interCompare = new FileInfo(Path.Combine(analysisFolder.FullName, "InterCompare.xml"));

            try
            {
                InterComparison.Generate(results, interCompare);
            }
            catch (Exception ex)
            {
                // Do nothing. Essentially optional
                Console.WriteLine("Error generating inter-comparison as part of a budget segregation " + ex.Message);
            }

            // Write the class legend to file
            File.WriteAllText(bsResult.ClassLegend.FullName, legendText.ToString());

            return(bsResult);
        }
Esempio n. 3
0
            static void BrotliCompareAndPushToQueue(
                HistogramDistance *out_, uint *cluster_size, uint idx1,
                uint idx2, size_t max_num_pairs, HistogramPair *pairs,
                size_t *num_pairs)
            {
                bool          is_good_pair = false;
                HistogramPair p            = new HistogramPair();

                if (idx1 == idx2)
                {
                    return;
                }
                if (idx2 < idx1)
                {
                    uint t = idx2;
                    idx2 = idx1;
                    idx1 = t;
                }
                p.idx1       = idx1;
                p.idx2       = idx2;
                p.cost_diff  = 0.5 * ClusterCostDiff(cluster_size[idx1], cluster_size[idx2]);
                p.cost_diff -= out_[idx1].bit_cost_;
                p.cost_diff -= out_[idx2].bit_cost_;

                if (out_[idx1].total_count_ == 0)
                {
                    p.cost_combo = out_[idx2].bit_cost_;
                    is_good_pair = true;
                }
                else if (out_[idx2].total_count_ == 0)
                {
                    p.cost_combo = out_[idx1].bit_cost_;
                    is_good_pair = true;
                }
                else
                {
                    double            threshold = *num_pairs == 0 ? 1e99 : Math.Max(0.0, pairs[0].cost_diff);
                    HistogramDistance combo     = out_[idx1];
                    double            cost_combo;
                    HistogramDistance.HistogramAddHistogram(&combo, &out_[idx2]);
                    cost_combo = BitCostDistance.BrotliPopulationCost(&combo);
                    if (cost_combo < threshold - p.cost_diff)
                    {
                        p.cost_combo = cost_combo;
                        is_good_pair = true;
                    }
                }
                if (is_good_pair)
                {
                    p.cost_diff += p.cost_combo;
                    if (*num_pairs > 0 && HistogramPairIsLess(&pairs[0], &p))
                    {
                        /* Replace the top of the queue if needed. */
                        if (*num_pairs < max_num_pairs)
                        {
                            pairs[*num_pairs] = pairs[0];
                            ++(*num_pairs);
                        }
                        pairs[0] = p;
                    }
                    else if (*num_pairs < max_num_pairs)
                    {
                        pairs[*num_pairs] = p;
                        ++(*num_pairs);
                    }
                }
            }
Esempio n. 4
0
 protected abstract DoDBase GetDoDResult(string dodName, DoDStats changeStats, Raster rawDoD, Raster thrDoD, Raster errDoDPath, HistogramPair histograms, FileInfo summaryXML);
Esempio n. 5
0
 protected override DoDBase GetDoDResult(string dodName, DoDStats changeStats, Raster rawDoD, Raster thrDoD, Raster thrErr, HistogramPair histograms, FileInfo summaryXML)
 {
     return(new DoDPropagated(dodName, rawDoD.GISFileInfo.Directory, NewSurface, OldSurface, AOIMask, rawDoD, thrDoD, thrErr, histograms, summaryXML, NewError, OldError, PropagatedErrRaster, changeStats));
 }
Esempio n. 6
0
 protected override DoDBase GetDoDResult(string dodName, DoDStats changeStats, Raster rawDoD, Raster thrDoD, Raster errDoD, HistogramPair histograms, FileInfo summaryXML)
 {
     return(new DoDMinLoD(dodName, rawDoD.GISFileInfo.Directory, NewSurface, OldSurface, AOIMask, rawDoD, thrDoD, errDoD, histograms, summaryXML, Threshold, changeStats));
 }
        protected override DoDBase GetDoDResult(string dodName, DoDStats changeStats, Raster rawDoD, Raster thrDoD, Raster thrErr, HistogramPair histograms, FileInfo summaryXML)
        {
            bool bBayesian = SpatialCoherence is CoherenceProperties;
            int  nFilter   = 0;

            if (SpatialCoherence is CoherenceProperties)
            {
                nFilter = SpatialCoherence.BufferSize;
            }

            return(new DoDProbabilistic(dodName, rawDoD.GISFileInfo.Directory, NewSurface, OldSurface, AOIMask, histograms, summaryXML, rawDoD, thrDoD, thrErr, NewError, OldError,
                                        PropagatedErrRaster, m_PriorProbRaster, m_PosteriorRaster, m_ConditionalRaster, m_SpatialCoErosionRaster, m_SpatialCoDepositionRaster,
                                        SpatialCoherence, Threshold, changeStats));
        }