Ejemplo n.º 1
0
        public void Smooth()
        {
            const double oneNinth = 1d / 9d;

            var sourceArray = new float[10, 10];

            for (var i = 0; i < 10; i++)
            {
                for (var j = 0; j < 10; j++)
                {
                    sourceArray[i, j] = 10.0f;
                }
            }

            var tools  = new ConvolutionTools <float>();
            var accum  = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter = new double[3, 3] {
                {
                    oneNinth, oneNinth, oneNinth
                },
                {
                    oneNinth, oneNinth, oneNinth
                },
                {
                    oneNinth, oneNinth, oneNinth
                }
            };

            var smoother = new ArrayDataSmoother <float>(tools, ConvolutionMaskSize.Mask3X3, accum,
                                                         (accum, size) => new FilterConvolver <float>(accum, filter, NullInfillMode.NoInfill));

            var result = smoother.Smooth(sourceArray);

            result.Should().BeEquivalentTo(sourceArray);
        }
Ejemplo n.º 2
0
        public void Result()
        {
            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);

            accum.Accumulate(123.0f, 1.0);
            accum.Result().Should().Be(123.0f);
        }
Ejemplo n.º 3
0
        public void Creation_WeightedMean_FailWithContextSizeOutOfRange()
        {
            var    accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            Action act         = () => _ = new WeightedMeanFilter <float>(accumulator, (ConvolutionMaskSize)100, 2.0, NullInfillMode.NoInfill);

            act.Should().Throw <ArgumentException>().WithMessage("Context size of 100 is out of range: 3..11");
        }
Ejemplo n.º 4
0
        public void Creation()
        {
            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);

            accum.Should().NotBeNull();
            accum.NullValue.Should().Be(CellPassConsts.NullHeight);
        }
Ejemplo n.º 5
0
        public void Smooth()
        {
            const float  elevation = 10.0f;
            const double oneNinth  = 1d / 9d;

            var source        = DataSmoothingTestUtilities.ConstructSingleSubGridElevationSubGridTreeAtOrigin(elevation);
            var sourceSubGrid = source.LocateSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.SubGridTreeLevels) as GenericLeafSubGrid <float>;

            sourceSubGrid.Should().NotBeNull();

            var tools  = new ConvolutionTools <float>();
            var accum  = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter = new double[3, 3] {
                {
                    oneNinth, oneNinth, oneNinth
                },
                {
                    oneNinth, oneNinth, oneNinth
                },
                {
                    oneNinth, oneNinth, oneNinth
                }
            };

            var smoother = new TreeDataSmoother <float>(tools, ConvolutionMaskSize.Mask3X3, accum,
                                                        (accum, size) => new FilterConvolver <float>(accum, filter, NullInfillMode.NoInfill));

            var result = smoother.Smooth(source);

            var resultSubGrid = result.LocateSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.SubGridTreeLevels) as GenericLeafSubGrid <float>;

            resultSubGrid.Should().NotBeNull();
            resultSubGrid.Items.Should().BeEquivalentTo(sourceSubGrid.Items);
        }
Ejemplo n.º 6
0
        public void SingleSubGrid_AtOrigin(ConvolutionMaskSize contextSize)
        {
            const float ELEVATION = 10.0f;

            var tree    = DataSmoothingTestUtilities.ConstructSingleSubGridElevationSubGridTreeAtOrigin(ELEVATION);
            var subGrid = tree.LocateSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.SubGridTreeLevels) as GenericLeafSubGrid <float>;

            subGrid.Should().NotBeNull();

            var result = DataSmoothingTestUtilities.ConstructElevationSubGrid(CellPassConsts.NullHeight);

            result.Should().NotBeNull();

            var accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, contextSize);
            var filter      = new MeanFilter <float>(accumulator, contextSize, NullInfillMode.NoInfill);
            var smoother    = new ConvolutionTools <float>();

            smoother.Convolve(subGrid, result, filter);

            // All cell values should remain mostly unchanged due to non-null values around perimeter of subgrid in smoothing context
            // Check all acquired values in the single subgrid are the same elevation, except for the perimeter values which
            // will be 2/3 * Elevation due to null values. Some corner vales will have 0.44444 * ElEVATION for same reason
            SubGridUtilities.SubGridDimensionalIterator((x, y) =>
            {
                var ok = Math.Abs(result.Items[x, y] = ELEVATION) < 0.0001 ||
                         Math.Abs(result.Items[x, y] = (2 / 3) * ELEVATION) < 0.0001 ||
                         Math.Abs(result.Items[x, y] = 0.44444f * ELEVATION) < 0.0001;
                ok.Should().BeTrue();
            });
        }
Ejemplo n.º 7
0
        public void Creation_WeightedMean()
        {
            var accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter      = new WeightedMeanFilter <float>(accumulator, ConvolutionMaskSize.Mask3X3, 2.0, NullInfillMode.NoInfill);

            filter.Should().NotBeNull();
        }
Ejemplo n.º 8
0
        public void Creation_FailWithInvalidFilterDimension()
        {
            var    accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            Action act         = () => _ = new FilterConvolver <float>(accumulator, new double[4, 4], NullInfillMode.NoInfill);

            act.Should().Throw <ArgumentException>().WithMessage("Context size must be positive odd number greater than 1");
        }
Ejemplo n.º 9
0
        public void Creation_FailWithFilterDimensionMisMatch()
        {
            var    accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            Action act         = () => _ = new FilterConvolver <float>(accumulator, new double[3, 4], NullInfillMode.NoInfill);

            act.Should().Throw <ArgumentException>().WithMessage("Major dimension (3) and minor dimension (4) of filterMatrix must be the same");
        }
Ejemplo n.º 10
0
        public void Creation_WeightedMean_CenterWeightCorrect()
        {
            var accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter      = new WeightedMeanFilter <float>(accumulator, ConvolutionMaskSize.Mask3X3, 2.0, NullInfillMode.NoInfill);

            filter.Should().NotBeNull();

            filter.FilterMatrix[1, 1].Should().Be(2 / 10.0d);
        }
Ejemplo n.º 11
0
        public void FilterConvolverAssertsDimensionsMatch()
        {
            var accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter      = new FilterConvolver <float>(accumulator, new double[3, 3], NullInfillMode.NoInfill);
            var smoother    = new ConvolutionTools <float>();

            Action act = () => smoother.Convolve(new float[3, 3], new float[4, 4], filter);

            act.Should().Throw <ArgumentException>().WithMessage("Dimensions of source and destination data are not the same");
        }
Ejemplo n.º 12
0
        public void Accumulate_NullValue_WithCoefficient()
        {
            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3)
            {
                ConvolutionSourceValue = 123.0f
            };

            accum.Accumulate(CellPassConsts.NullHeight, 1.0);
            accum.Result().Should().Be(123.0f);
            accum.NumNonNullValues.Should().Be(0);
        }
Ejemplo n.º 13
0
        public void Creation()
        {
            var tools  = new ConvolutionTools <float>();
            var accum  = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter = new double[3, 3];

            var smoother = new TreeDataSmoother <float>(tools, ConvolutionMaskSize.Mask3X3, accum,
                                                        (acc, size) => new FilterConvolver <float>(accum, filter, NullInfillMode.NoInfill));

            smoother.Should().NotBeNull();
            smoother.AdditionalBorderSize.Should().Be(3 / 2);
        }
Ejemplo n.º 14
0
        public void Creation_Base()
        {
            var accumulator = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);
            var filter      = new FilterConvolver <float>(accumulator, new double[3, 3], NullInfillMode.NoInfill);

            filter.Should().NotBeNull();

            filter.InfillNullValuesOnly.Should().BeFalse();
            filter.UpdateNullValues.Should().BeFalse();

            filter = new FilterConvolver <float>(accumulator, new double[3, 3], NullInfillMode.InfillNullValuesOnly);
            filter.InfillNullValuesOnly.Should().BeTrue();
            filter.UpdateNullValues.Should().BeTrue();
        }
Ejemplo n.º 15
0
        public void Accumulate_NoCoefficient()
        {
            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, ConvolutionMaskSize.Mask3X3);

            accum.Accumulate(123.0f);
            accum.Result().Should().Be(123.0f);
            accum.NumNonNullValues.Should().Be(1);

            accum.Accumulate(177.0f);
            accum.Result().Should().Be(300.0f);
            accum.NumNonNullValues.Should().Be(2);

            accum.Accumulate(CellPassConsts.NullHeight);
            accum.Result().Should().Be(300.0f);
            accum.NumNonNullValues.Should().Be(2);
        }
Ejemplo n.º 16
0
        public void NullInfillResult_BelowConcensus(ConvolutionMaskSize contextSize)
        {
            const float accumValue       = 100.0f;
            var         contextSizeAsInt = (int)contextSize;

            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, contextSize)
            {
                ConvolutionSourceValue = CellPassConsts.NullHeight
            };

            var concensusFraction = (int)Math.Truncate(0.5 * (contextSizeAsInt * contextSizeAsInt));

            for (var i = 0; i < concensusFraction; i++)
            {
                accum.Accumulate(accumValue, 1.0);
            }

            accum.NullInfillResult().Should().Be(CellPassConsts.NullHeight);
        }
Ejemplo n.º 17
0
        public void NullInfillResult_AboveConcensus(ConvolutionMaskSize contextSize)
        {
            const float accumValue       = 100.0f;
            var         contextSizeAsInt = (int)contextSize;

            var accum = new ConvolutionAccumulator_Float(CellPassConsts.NullHeight, contextSize)
            {
                ConvolutionSourceValue = CellPassConsts.NullHeight
            };

            float contextSizeSquare = contextSizeAsInt * contextSizeAsInt;
            var   concensusFraction = (int)Math.Truncate(0.5f * contextSizeSquare);

            for (var i = 0; i < concensusFraction + 1; i++)
            {
                accum.Accumulate(accumValue, 1.0);
            }

            var expectedInfillResult = (contextSizeSquare / (concensusFraction + 1)) * ((concensusFraction + 1) * accumValue);

            accum.NullInfillResult().Should().Be(expectedInfillResult);
        }