Example #1
0
        private (int, int) MedianHistogram(int buckets)
        {
            //Converts fitsArray to vector for histogram purposes
            List <double> fsVector = new List <double>();

            for (int i = 0; i < Xaxis; i++)
            {
                for (int j = 0; j < Yaxis; j++)
                {
                    fsVector.Add(FITSArray[i, j]);
                }
            }
            MathNet.Numerics.Statistics.Histogram fsHistBuckets = new MathNet.Numerics.Statistics.Histogram(fsVector, buckets);
            //Create an list that can be statistically analyzed
            List <double> fsHistList = new List <double>();

            for (int i = 0; i < fsHistBuckets.BucketCount; i++)
            {
                fsHistList.Add(fsHistBuckets[i].Count);
            }
            //Determine the gaussian distribution of the histogram
            //double medianBucketValue = MathNet.Numerics.Statistics.Statistics.Mean(fsHistList);
            double maxBucketValue = fsHistList.Max();
            //int medianBucketIndex = FindNearestBucketSize(fsHistBuckets,medianBucketValue);
            int maxBucketIndex = FindNearestBucketSize(fsHistBuckets, 0, fsHistBuckets.BucketCount, maxBucketValue);
            //double stdDev = MathNet.Numerics.Statistics.Statistics.StandardDeviation(fsHistList);
            int lowerBucketIndex = FindNearestBucketSize(fsHistBuckets, 0, maxBucketIndex, maxBucketValue / 4);
            int upperBucketIndex = FindNearestBucketSize(fsHistBuckets, maxBucketIndex, fsHistBuckets.BucketCount, maxBucketValue / 4);

            int upperBucketValue = (int)fsHistBuckets[upperBucketIndex].UpperBound;
            int lowerBucketValue = (int)fsHistBuckets[lowerBucketIndex].LowerBound;

            return(lowerBucketValue, upperBucketValue);
        }
Example #2
0
        public static string GetDerivHistogram(CylData cylData)
        {
            try
            {
                var    derivs = new List <double>();
                double max    = double.MinValue;
                double min    = double.MaxValue;
                for (int i = 0; i < cylData.Count - 1; i++)
                {
                    var p1 = cylData[i + 1];
                    var p0 = cylData[i];
                    var d  = Math.Abs((p1.R - p0.R) / (p1.ThetaRad - p0.ThetaRad));
                    if (d < min)
                    {
                        min = d;
                    }
                    if (d > max)
                    {
                        max = d;
                    }
                    derivs.Add(d);
                }

                //double deltaD = (max - min) / 20;
                var h    = new MathNet.Numerics.Statistics.Histogram(derivs, 10, min, max);
                var bmax = 100 * h.GetBucketOf(max).Count / h.DataCount;
                var bmin = 100 * h.GetBucketOf(min).Count / h.DataCount;

                return(String.Concat("%min, %max Derivatives : ", bmin.ToString("f4"), " , ", bmax.ToString("f4")));
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #3
0
        private int FindNearestBucketSize(MathNet.Numerics.Statistics.Histogram hg, int startLookingIndex, int stopLookingIndex, double level)
        {
            //find the bucket whose contents is closest to the value level
            int min     = int.MaxValue;
            int thisone = 0;

            for (int i = startLookingIndex; i < stopLookingIndex; i++)
            {
                if (Math.Abs(hg[i].Count - level) < min)
                {
                    min     = (int)Math.Abs(hg[i].Count - level);
                    thisone = i;
                }
            }
            return(thisone);
        }
Example #4
0
        public static List <HistogramBucket> GetDerivHistogram(List <double> data, int bucketCount)
        {
            try
            {
                var    derivs = new List <double>();
                double max    = double.MinValue;
                double min    = double.MaxValue;
                for (int i = 0; i < data.Count - 1; i++)
                {
                    var p1 = data[i + 1];
                    var p0 = data[i];
                    var d  = Math.Abs(p1 - p0);
                    if (d < min)
                    {
                        min = d;
                    }
                    if (d > max)
                    {
                        max = d;
                    }
                    derivs.Add(d);
                }

                double deltaD    = (max - min) / bucketCount;
                var    h         = new MathNet.Numerics.Statistics.Histogram(derivs, bucketCount, min, max);
                var    bucketV   = min + (deltaD / 2.0);
                var    histogram = new List <HistogramBucket>();

                while (bucketV <= max)
                {
                    var b = h.GetBucketOf(bucketV);
                    histogram.Add(new HistogramBucket(bucketV, b.Count));
                    bucketV += deltaD;
                }
                return(histogram);
                //return String.Concat("%min, %max Derivatives : ", bmin.ToString("f4"), " , ", bmax.ToString("f4"));
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void PlotNormalDistribution(double[] data, Histogram histogram, string distributionName, Color color)
        {
            if (data == null || histogram == null)
                return;

            var points = 50;

            var normalDistribution = new NormalDistribution(data.Average(), Math.Variance(data), points, histogram.LowerBound, histogram.UpperBound);

            //var normalDistribution = new NormalDistribution(histogram.Mean(), histogram.Variance(), points, histogram.LowerBound, histogram.UpperBound);

            var densityCurve = normalDistribution.DensityCurve;

            var xValues = new double[points];
            var yValues = new double[points];

            for (var index = 0; index < points; index++)
            {
                xValues[index] = densityCurve[index, 0];
                yValues[index] = densityCurve[index, 1];
            }

            // Add data sources.
            var yDataSource = new EnumerableDataSource<double>(yValues);
            yDataSource.SetYMapping(Y => Y);
            yDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty, Y => string.Format("Normal Value \n\n{0}", Y));

            var xDataSource = new EnumerableDataSource<double>(xValues);
            xDataSource.SetXMapping(X => X);

            var compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

            // MatchValuePlotter.Viewport.Restrictions.Add(new PhysicalProportionsRestriction { ProportionRatio = 500000 });
            var graph = ChartPlotter.AddLineGraph(compositeDataSource, color, 0.5, distributionName);

            // Cache for later usage (e.g. change visibility).
            if (graph != null)
                _histogramGraphs.Add(graph);
        }
        private void PlotHistogram(Histogram histogram, string histogramName, Color color)
        {
            if (histogram == null)
                return;

            // Prepare data in arrays.
            var pointIndex = 0;
            var dataPointCount = 4 * histogram.BucketCount;
            var x = new double[dataPointCount];
            var y = new double[dataPointCount];

            foreach (var bucket in histogram.Buckets())
            {
                //var yValue = bucket.Count;
                var yValue = bucket.RelativeCount(histogram);

                // lower left point of the histogram bar
                x[pointIndex] = bucket.LowerBound;
                y[pointIndex] = 0;

                // upper left point of the histogram bar
                x[pointIndex + 1] = bucket.LowerBound;
                y[pointIndex + 1] = yValue;

                // upper right point of the histogram bar
                x[pointIndex + 2] = bucket.UpperBound;
                y[pointIndex + 2] = yValue;

                // lower right point of the histogram bar
                x[pointIndex + 3] = bucket.UpperBound;
                y[pointIndex + 3] = 0;

                pointIndex += 4;
            }

            // Add data sources.
            var yDataSource = new EnumerableDataSource<double>(y);
            yDataSource.SetYMapping(Y => Y);
            yDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty, Y => string.Format("Match Value \n\n{0}", Y));

            var xDataSource = new EnumerableDataSource<double>(x);
            xDataSource.SetXMapping(X => X);

            var compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

            // MatchValuePlotter.Viewport.Restrictions.Add(new PhysicalProportionsRestriction { ProportionRatio = 500000 });
            var graph = ChartPlotter.AddLineGraph(compositeDataSource, color, 1, histogramName);

            // Cache for later usage (e.g. change visibility).
            if (graph != null)
                _histogramGraphs.Add(graph);
        }
        private void GetSTAResponseHistogramPlotData(out Histogram rawStimuliSTAResponseHistogram, out double[] rawStimuliSTAMatchValues, out Histogram spikeTriggeredStimuliSTAResponseDiagram, out double[] spikeTriggeredStimuliSTAMatchValues, out Nonlinearity nonlinearity, bool recalcData = true)
        {
            rawStimuliSTAResponseHistogram = null;
            rawStimuliSTAMatchValues = null;
            spikeTriggeredStimuliSTAResponseDiagram = null;
            spikeTriggeredStimuliSTAMatchValues = null;
            nonlinearity = null;

            if (_spikes == null)
                return;

            // caching
            if (!recalcData)
            {
                rawStimuliSTAResponseHistogram = _rawStimuliSTAResponseHistogram;
                spikeTriggeredStimuliSTAResponseDiagram = _spikeTriggeredStimuliSTAResponseDiagram;
                nonlinearity = _nonlinearity;

                return;
            }

            var spikes = new List<double[][]>();

            if (Cell1CheckBox.IsChecked != null && Cell1CheckBox.IsChecked.Value)
                spikes.Add(_spikes[0]);

            if (Cell2CheckBox.IsChecked != null && Cell2CheckBox.IsChecked.Value)
                spikes.Add(_spikes[1]);

            if (Cell3CheckBox.IsChecked != null && Cell3CheckBox.IsChecked.Value)
                spikes.Add(_spikes[2]);

            if (Cell4CheckBox.IsChecked != null && Cell4CheckBox.IsChecked.Value)
                spikes.Add(_spikes[3]);

            if (spikes.Count == 0)
                return;

            int offset;
            if (!int.TryParse(OffsetTextbox.Text, out offset))
                offset = 13;

            int maxTime;
            if (!int.TryParse(TimeTextbox.Text, out maxTime))
                maxTime = 13;

            var rawStimuliHistogramBuckets = MatchValuesForRawStimuliBucketsUpDown.Value != null ? MatchValuesForRawStimuliBucketsUpDown.Value.Value : 200;
            var spikeTriggeredStimuliHistogramBuckets = MatchValuesForSpikeTriggeredStimuliBucketsUpDown.Value != null ? MatchValuesForSpikeTriggeredStimuliBucketsUpDown.Value.Value : 200;
            double[,] smoothKernel = null;
            bool useDynamicDivisorForEdges = false;

            GetSmoothKernel(out smoothKernel, out useDynamicDivisorForEdges);

            SpikeTriggeredAnalysis.CalculateSTAResponseHistogram(_stimuli, spikes.ToArray(), false, offset, maxTime, smoothKernel, useDynamicDivisorForEdges, rawStimuliHistogramBuckets, out rawStimuliSTAMatchValues, out rawStimuliSTAResponseHistogram);
            SpikeTriggeredAnalysis.CalculateSTAResponseHistogram(_stimuli, spikes.ToArray(), true, offset, maxTime, smoothKernel, useDynamicDivisorForEdges, spikeTriggeredStimuliHistogramBuckets, out spikeTriggeredStimuliSTAMatchValues, out spikeTriggeredStimuliSTAResponseDiagram);

            _rawStimuliSTAResponseHistogram = rawStimuliSTAResponseHistogram;
            _rawStimuliSTAMatchValues = rawStimuliSTAMatchValues;

            _spikeTriggeredStimuliSTAResponseDiagram = spikeTriggeredStimuliSTAResponseDiagram;
            _spikeTriggeredStimuliSTAMatchValues = spikeTriggeredStimuliSTAMatchValues;

            var frequencyRawStimuli = new NormalDistribution(rawStimuliSTAMatchValues.Average(), Math.Variance(rawStimuliSTAMatchValues), 10, rawStimuliSTAResponseHistogram.LowerBound, rawStimuliSTAResponseHistogram.UpperBound);
            var frequencySpikeTriggeredStimuli = new NormalDistribution(spikeTriggeredStimuliSTAMatchValues.Average(), Math.Variance(spikeTriggeredStimuliSTAMatchValues), 10, spikeTriggeredStimuliSTAResponseDiagram.LowerBound, spikeTriggeredStimuliSTAResponseDiagram.UpperBound);

            nonlinearity =
                // caching
                _nonlinearity = new Nonlinearity(frequencyRawStimuli, frequencySpikeTriggeredStimuli, 100);
        }