示例#1
0
        public void SolveLorentzianFactoryA()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualLortentzianA(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Lorentzian;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);
            var coeffs           = functionSelector.Coefficients;

            coeffs[0] = 6;  //width
            coeffs[1] = 50; //height
            coeffs[2] = -1; //xoffset

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(0.50000000000535016d, coeffs[0]); //real is 0.5.
            Assert.AreEqual(150.00000000174555d, coeffs[1]);  //real is 75
            Assert.AreEqual(0.99999999999999312d, coeffs[2]); //real is 1

            //using 1 instead of 0.5
            //Assert.AreEqual(0.49999999817701907d, coeffs[0]);//real is 0.5.
            //Assert.AreEqual(74.99999972887592d, coeffs[1]);//real is 75
            //Assert.AreEqual(0.9999999999999587d, coeffs[2]);//real is 1
        }
示例#2
0
        public void SolveLorentzianFactoryB()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualLortentzianB(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Lorentzian;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);
            var coeffs           = functionSelector.Coefficients;

            coeffs[0] = 1;   //width
            coeffs[1] = 100; //height
            coeffs[2] = -1;  //xoffset

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(-0.0018033993446626476d, coeffs[0], 0.0000001); //real is 0.5.
            Assert.AreEqual(-5.1154290702777061d, coeffs[1], 0.001);        //real is 75
            Assert.AreEqual(-0.0094999999962089646d, coeffs[2], 0.0000001); //real is 1

            //using 1 instead of 0.5
            //Assert.AreEqual(0.49999999817701907d, coeffs[0]);//real is 0.5.
            //Assert.AreEqual(74.99999972887592d, coeffs[1]);//real is 75
            //Assert.AreEqual(0.9999999999999587d, coeffs[2]);//real is 1
        }
示例#3
0
        public void SolveAsymmetricGaussian()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualGaussian(), out x, out y);

            var start = x.Min();
            var stop  = x.Max();
            var width = Math.Abs(stop - start);
            var A     = y.Max();


            if (mShowDetails)
            {
                for (var i = 0; i < x.Count; i++)
                {
                    Console.WriteLine("{0}\t{1}", x[i], y[i]);
                }

                Console.WriteLine();
            }


            var basisFunction = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.AsymmetricGaussian);

            var coeffs = basisFunction.Coefficients;

            coeffs[0] = start + width / 2;
            coeffs[1] = A;
            coeffs[2] = width * .9;
            coeffs[3] = coeffs[2];

            var report = EvaluateFunction(x, y, basisFunction, out coeffs);

            var numberOfSamples = x.Count * 2;

            // Calculate the width an spacing of each of the trapezoids.
            var delta = width / Convert.ToDouble(numberOfSamples);
            var xx    = start;

            // We already evaluated the first point, now for each element within
            for (var i = 0; i < numberOfSamples + 1; i++)
            {
                xx += delta;
                var yy = basisFunction.Evaluate(coeffs, xx);

                if (mShowDetails)
                {
                    Console.WriteLine("{0}\t{1}", xx, yy);
                }
            }
        }
示例#4
0
        public void SolveLinearFunction()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculateLine(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Linear);
            var coeffs           = functionSelector.Coefficients;

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(5, coeffs[0], .0001);
            Assert.AreEqual(0, coeffs[1], .0001);
        }
示例#5
0
        public void SolveLinearFunction()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculateLine(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Linear);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(5, coeffs[0], .0001);
            Assert.AreEqual(0, coeffs[1], .0001);
        }
示例#6
0
        public void SolveQuadraticFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedParabola(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.PolynomialQuadratic);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(-0.99999999960388553d, coeffs[0], .000001);
            Assert.AreEqual(2.410211171560969E-10d, coeffs[1], .000001);
            Assert.AreEqual(99.999999976322613d, coeffs[2], .000001);
        }
示例#7
0
        public void SolveHanningFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualHanning(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Hanning);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(Math.Round(30.521054724721569d, 7), Math.Round(coeffs[0], 7), .00001);
            Assert.AreEqual(Math.Round(37.723968728457208d, 6), Math.Round(coeffs[1], 6), .00001);
            Assert.AreEqual(Math.Round(1234.4579999999935d, 7), Math.Round(coeffs[2], 7), .00001);
        }
示例#8
0
        public void SolveQuadratic()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedParabola(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.PolynomialQuadratic);
            var coeffs           = functionSelector.Coefficients;

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(-0.99999999959999375d, coeffs[0], .00001);
            Assert.AreEqual(2.4338897338076459E-10d, coeffs[1], .00001);
            Assert.AreEqual(99.999999976089995d, coeffs[2], .00001);
        }
示例#9
0
        public void SolveQuadratic()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedParabola(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.PolynomialQuadratic);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(-0.99999999959999375d, coeffs[0], .00001);
            Assert.AreEqual(2.4338897338076459E-10d, coeffs[1], .00001);
            Assert.AreEqual(99.999999976089995d, coeffs[2], .00001);
        }
示例#10
0
        public void SolveGaussian()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualGaussian(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Gaussian);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            //sigma must be positive
            Assert.AreEqual(0.50000000014842283d, Math.Abs(coeffs[0]), .00001); //real is 0.5.  may return a negative value
            Assert.AreEqual(99.999999955476071d, coeffs[1], .00001);            //real is 100
            Assert.AreEqual(0.99999999999999967d, coeffs[2], .00001);           //real is 1
        }
示例#11
0
        public FitReport Fit(IEnumerable <double> x, IEnumerable <double> y, BasisFunctionsEnum basisFunction, ref double[] coeffs)
        {
            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(basisFunction);

            //incase the coefficients input are the wrong dimension
            var coeffCount = functionSelector.Coefficients.Count();

            if (coeffs.Length != coeffCount)
            {
                coeffs = functionSelector.Coefficients;
            }

            var worked = EvaluateFunction(x.ToList(), y.ToList(), functionSelector, ref coeffs);

            var results = new FitReportALGLIB(worked, worked.DidConverge);

            return(results);
        }
示例#12
0
        public void SolveGaussianFactoryRealProblemData()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualGaussianProblem(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Gaussian;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);
            var coeffs           = functionSelector.Coefficients;

            coeffs[0] = 2;                  //sigma
            coeffs[1] = 7375230.5281385286; //height
            coeffs[2] = 1080;               //xoffset

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(15.307150768556957d, Math.Abs(coeffs[0]), .01); //real is
            Assert.AreEqual(5839780.76391418d, coeffs[1], 100);             //real is
            Assert.AreEqual(1081.0890598765791d, coeffs[2], .001);          //real is


            Console.WriteLine("Try New Guess Coeff");

            var thisSectionFails = false;

            if (thisSectionFails)
            {
                var functionSelectorPoorChoice = BasisFunctionFactory.BasisFunctionSelector(functionChoise);

                var coeffs2 = functionSelectorPoorChoice.Coefficients;
                coeffs2[0] = 2;                  //sigma
                coeffs2[1] = 7375230.5281385286; //height
                coeffs2[2] = 1102;               //xoffset

                var report2 = EvaluateFunction(x, y, functionSelectorPoorChoice, out coeffs2);

                Assert.AreEqual(15.307150768556957d, Math.Abs(coeffs[0]), .01); //real is
                Assert.AreEqual(5839780.76391418d, coeffs[1], 100);             //real is
                Assert.AreEqual(1081.0890598765791d, coeffs[2], .001);          //real is
            }
        }
示例#13
0
        public void SolveCubicFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedCubic(), out x, out y);

            var functionChoise   = BasisFunctionsEnum.PolynomialCubic;
            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);
            var coeffs           = functionSelector.Coefficients;

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(-0.9999999999984106d, coeffs[0], .00001);
            Assert.AreEqual(5.0000000000444658d, coeffs[1], .00001);
            Assert.AreEqual(99.999999999930722d, coeffs[2], .00001);
            Assert.AreEqual(24.999999997435527d, coeffs[3], .00001);
        }
示例#14
0
        public void SolveCubicFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedCubic(), out x, out y);

            var functionChoise   = BasisFunctionsEnum.PolynomialCubic;
            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(-0.9999999999984106d, coeffs[0], .00001);
            Assert.AreEqual(5.0000000000444658d, coeffs[1], .00001);
            Assert.AreEqual(99.999999999930722d, coeffs[2], .00001);
            Assert.AreEqual(24.999999997435527d, coeffs[3], .00001);
        }
示例#15
0
        public void IntegrateLineFunction()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculateLine(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Linear);
            var coeffs           = functionSelector.Coefficients;

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            NumericalIntegrationBase integrator = new TrapezoidIntegration();
            var area = integrator.Integrate(functionSelector, coeffs, 0, 3, 500);

            Console.WriteLine("Area: {0}", area);
            Assert.AreEqual(22.5, area, .001);
        }
示例#16
0
        public void SolveCubicWithChebyshevFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedParabola(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Chebyshev;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            //Assert.AreEqual(-0.9999999999984106d, coeffs[0]);
            //Assert.AreEqual(5.0000000000444658d, coeffs[1]);
            //Assert.AreEqual(99.999999999930722d, coeffs[2]);
            //Assert.AreEqual(24.999999997435527d, coeffs[3]);
        }
示例#17
0
        /// <summary>
        /// Scores two chromatograms using their basis functions.
        /// </summary>
        /// <param name="profileA">chromatogram A</param>
        /// <param name="profileB">chromatogram B</param>
        /// <param name="basisFunction">Function used to interpolate</param>
        /// <param name="intensityProfile"></param>
        /// <returns>R-squared value for a given linear regression between intensity matrix</returns>
        public double ScoreChromatogramIntensity(Chromatogram profileA,
                                                 Chromatogram profileB,
                                                 BasisFunctionBase basisFunction,
                                                 ref List <XYData> intensityProfile)
        {
            if (intensityProfile == null)
            {
                throw new ArgumentNullException("intensityProfile");
            }

            var minScan = profileA.FitPoints.Min(x => x.X);
            var maxScan = profileA.FitPoints.Max(x => x.X);

            minScan = Math.Min(minScan, profileB.FitPoints.Min(x => x.X));
            maxScan = Math.Max(maxScan, profileB.FitPoints.Max(x => x.X));

            var deltaScan = Math.Abs(maxScan - minScan) / 100;
            var scan      = minScan;

            var pairs = new List <XYData>();

            while (scan <= maxScan)
            {
                var x = basisFunction.Evaluate(profileA.FitCoefficients, scan);
                var y = basisFunction.Evaluate(profileB.FitCoefficients, scan);

                pairs.Add(new XYData(x, y));
                scan += deltaScan;
            }

            var linearRegression = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Linear);
            var solver           = new LevenburgMarquadtSolver {
                BasisFunction = linearRegression.FunctionDelegate
            };

            var coeffs = linearRegression.Coefficients;
            var report = solver.Solve(pairs, ref coeffs);

            return(report.RSquared);
        }
示例#18
0
        public void SolveOrbitrapLorentzianFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualOrbitrap2(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Lorentzian);

            var coeffs = functionSelector.Coefficients;

            //important guesses
            coeffs[0] = 5;           //hanningI
            coeffs[1] = 80000;       //hanningK
            coeffs[2] = 1234.388251; //xoffset

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(0.014591732782157337d, coeffs[0], .001);
            Assert.AreEqual(41816.913857810927d, coeffs[1], .001);
            Assert.AreEqual(1234.4577771195013d, coeffs[2], .001);
        }
示例#19
0
        public void SolveHanningFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualHanning(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Hanning);
            var coeffs           = functionSelector.Coefficients;

            //important guesses
            coeffs[0] = 30;          //hanningI
            coeffs[1] = 5;           //hanningK
            coeffs[2] = 1234.388251; //xoffset

            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(Math.Round(30.521054724721569d, 7), Math.Round(coeffs[0], 7), .00001);
            Assert.AreEqual(Math.Round(37.723968728457208d, 6), Math.Round(coeffs[1], 6), .00001);
            Assert.AreEqual(Math.Round(1234.4579999999935d, 7), Math.Round(coeffs[2], 7), .00001);
        }
示例#20
0
        public void SolveGaussianFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualGaussian(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Gaussian;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);
            var coeffs           = functionSelector.Coefficients;

            coeffs[0] = 6;  //sigma
            coeffs[1] = 50; //height
            coeffs[2] = -1; //xoffset
            var showDetails = false;
            var worked      = EvaluateFunction(x, y, functionSelector, ref coeffs, showDetails);

            Assert.AreEqual(0.50000000014842283d, Math.Abs(coeffs[0]), .00001); //real is 0.5.  may return a negative value
            Assert.AreEqual(99.999999955476071d, coeffs[1], .00001);            //real is 100
            Assert.AreEqual(0.99999999999999967d, coeffs[2], .00001);           //real is 1
        }
示例#21
0
        public void IntegrateLineFunctionTimeTrial()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculateLine(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Linear);

            double[] coeffs;

            var showDetails = false;
            var report      = EvaluateFunction(x, y, functionSelector, out coeffs, showDetails);

            NumericalIntegrationBase integrator = new TrapezoidIntegration();

            Console.WriteLine("");
            Console.WriteLine("Samples\tTime(ms)\tArea\tPercentError");

            for (var i = 1; i < 10; i++)
            {
                var    averageTimes = new List <double>();
                double sum          = 0;
                var    iterations   = 1000;
                var    totalSamples = i * 100;
                double area         = 0;
                for (var j = 0; j < iterations; j++)
                {
                    var start = DateTime.Now;
                    area = integrator.Integrate(functionSelector, coeffs, 0, 3, totalSamples);
                    var end = DateTime.Now;
                    sum += end.Subtract(start).TotalMilliseconds;
                }
                var percentError = (area - 22.5) / 22.5 * 100;
                var averageTime  = sum / Convert.ToDouble(iterations);
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", totalSamples, averageTime, area, percentError);
            }
        }
示例#22
0
        public void SolveChebyshev()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(CalculatedParabola(), out x, out y);

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(BasisFunctionsEnum.Chebyshev);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            // Make sure it converged.
            Assert.IsTrue(report.DidConverge);

            // Here are the coefficients where this should pass.
            Assert.LessOrEqual(Math.Abs(coeffs[0] - -161.49999998947484), .0000001);
            Assert.LessOrEqual(Math.Abs(coeffs[1] - -340.99999998132216), .0000001);
            Assert.LessOrEqual(Math.Abs(coeffs[2] - -89.749999993541593), .0000001);
            Assert.LessOrEqual(Math.Abs(coeffs[3] - 0.50000000335890493), .0000001);
            Assert.LessOrEqual(Math.Abs(coeffs[4] - 0.50000000120664689), .0000001);
            Assert.LessOrEqual(Math.Abs(coeffs[5] - 0.00000000023672415945361692), .0000001);
        }
示例#23
0
        public void SolveLorentzianFactory()
        {
            List <double> x;
            List <double> y;

            ConvertXYDataToArrays(ManualLortentzianA(), out x, out y);

            var functionChoise = BasisFunctionsEnum.Lorentzian;

            var functionSelector = BasisFunctionFactory.BasisFunctionSelector(functionChoise);

            double[] coeffs;

            var report = EvaluateFunction(x, y, functionSelector, out coeffs);

            Assert.AreEqual(0.50000000000535016d, coeffs[0], .00001); //real is 0.5.
            Assert.AreEqual(150.00000000174555d, coeffs[1], .00001);  //real is 75
            Assert.AreEqual(0.99999999999999312d, coeffs[2], .00001); //real is 1

            //using 1 instead of 0.5
            //Assert.AreEqual(0.49999999817701907d, coeffs[0]);//real is 0.5.
            //Assert.AreEqual(74.99999972887592d, coeffs[1]);//real is 75
            //Assert.AreEqual(0.9999999999999587d, coeffs[2]);//real is 1
        }
示例#24
0
        /// <summary>
        ///     Scores a feature and adjusts its abundance accordingly.
        /// </summary>
        /// <param name="feature"></param>
        /// <returns></returns>
        public void ScoreFeature(UMCLight feature)
        {
            // Get the basis function of interest
            var basisFunction = BasisFunctionFactory.BasisFunctionSelector(BasisFunction);
            var integrator    = NumericalIntegrationFactory.CreateIntegrator(IntegrationType);


            // Evaluate every charge state XIC.
            foreach (var charge in feature.ChargeStateChromatograms.Keys)
            {
                var gram        = feature.ChargeStateChromatograms[charge];
                var totalPoints = gram.Points.Count;
                if (totalPoints > 1)
                {
                    // Convert the data types, not sure why this has to be done...
                    // Should just using the XYData points
                    var x = new List <double>();
                    var y = new List <double>();
                    foreach (var xyData in gram.Points)
                    {
                        x.Add(xyData.X);
                        y.Add(xyData.Y);
                    }

                    // First solve for the function
                    var coefficients = basisFunction.Coefficients;
                    var solver       = new LevenburgMarquadtSolver();
                    var report       = solver.Solve(x, y, ref coefficients);
                    gram.FitPoints = new List <XYData>();

                    foreach (var datum in gram.Points)
                    {
                        var yValue = basisFunction.Evaluate(coefficients, datum.X);
                        gram.FitPoints.Add(new XYData(datum.X, yValue));
                    }

                    var totalSamples = 4 * Math.Abs(gram.EndScan - gram.StartScan);

                    // Then integrate the function
                    // Let's integrate with 4x the number of scans
                    gram.Area = integrator.Integrate(basisFunction,
                                                     coefficients,
                                                     Convert.ToDouble(gram.StartScan),
                                                     Convert.ToDouble(gram.EndScan),
                                                     totalSamples);
                }
            }
            // Then calculate all of the fits for each
            foreach (var charge in feature.IsotopeChromatograms.Keys)
            {
                foreach (var gram in feature.IsotopeChromatograms[charge])
                {
                    var totalPoints = gram.Points.Count;
                    if (totalPoints > 1)
                    {
                        // Convert the data types, not sure why this has to be done...
                        // Should just using the XYData points
                        var x = new List <double>();
                        var y = new List <double>();
                        foreach (var xyData in gram.Points)
                        {
                            x.Add(xyData.X);
                            y.Add(xyData.Y);
                        }

                        // First solve for the function
                        var coefficients = basisFunction.Coefficients;
                        var solver       = new LevenburgMarquadtSolver();
                        solver.Solve(x, y, ref coefficients);
                        gram.FitPoints = new List <XYData>();

                        foreach (var datum in gram.Points)
                        {
                            var yValue = basisFunction.Evaluate(coefficients, datum.X);
                            gram.FitPoints.Add(new XYData(datum.X, yValue));
                        }

                        var totalSamples = 4 * Math.Abs(gram.EndScan - gram.StartScan);

                        // Then integrate the function
                        // Let's integrate with 4x the number of scans
                        gram.Area = integrator.Integrate(basisFunction,
                                                         coefficients,
                                                         Convert.ToDouble(gram.StartScan),
                                                         Convert.ToDouble(gram.EndScan),
                                                         totalSamples);
                    }
                }
            }
        }
示例#25
0
        public void SolveAsymmetricGaussianFactory(string path, BasisFunctionsEnum functionChoise)
        {
            var data    = ReadXicDatabase(Path.Combine(TestPathSingleton.TestDirectory, path));
            var newData = new List <Xic>();

            foreach (var xic in data)
            {
                var basisFunction = BasisFunctionFactory.BasisFunctionSelector(functionChoise);

                var coefficients = basisFunction.Coefficients;
                var start        = xic.x.Min() - .5;
                var stop         = xic.x.Max() + .5;
                var A            = xic.y.Max();
                A += (A * .05);
                var width = Math.Abs(stop - start);

                coefficients[0] = start + ((stop - start) / 2);
                coefficients[1] = A;
                coefficients[2] = width * .5;
                coefficients[3] = coefficients[2];

                //Console.WriteLine("Xic {0}", xic.id);
                //Console.WriteLine();

                for (var i = 0; i < xic.x.Count; i++)
                {
                    // Console.WriteLine("{0}\t{1}", xic.x[i], xic.y[i]);
                }
                SolverReport worked = null;
                Console.WriteLine();
                var showDetails = false;

                try
                {
                    worked = EvaluateFunction(xic.x, xic.y, basisFunction, ref coefficients, showDetails);
                }
                catch (Exception)
                {
                    continue;
                }

                // First solve for the function
                var solver = new LevenburgMarquadtSolver();

                var numberOfSamples = 100;

                // Calculate the width an spacing of each of the trapezoids.
                var delta = width / Convert.ToDouble(numberOfSamples);
                var x     = start;

                var newXic = new Xic();
                newXic.x         = new List <double>();
                newXic.y         = new List <double>();
                newXic.charge    = xic.charge;
                newXic.scoreName = xic.scoreName;
                newXic.score     = xic.score;
                newXic.id        = xic.id;
                newXic.prevXic   = xic;
                newXic.report    = worked;
                newXic.reviewer  = xic.reviewer;

                // We already evaluated the first point, now for each element within
                for (var i = 0; i < numberOfSamples + 1; i++)
                {
                    x += delta;
                    var y = basisFunction.Evaluate(coefficients, x);

                    newXic.x.Add(x);
                    newXic.y.Add(y);
                    // Console.WriteLine("{0}\t{1}", x, y);
                }
                //Console.WriteLine();

                newData.Add(newXic);
            }

            var newPath = path.Replace(".xic", functionChoise + "_fit.xic");

            using (TextWriter writer = File.CreateText(newPath))
            {
                foreach (var feature in newData)
                {
                    writer.WriteLine("id\t{0}\tcharge\t{1}", feature.id, feature.charge);
                    writer.WriteLine("score\t{0}\tscoreName\t{1}", feature.score, feature.scoreName);
                    writer.WriteLine("reviewer\t{0}", feature.reviewer);
                    writer.WriteLine("R2\t{0}", feature.report.RSquared);
                    writer.WriteLine("Rms\t{0}", feature.report.RmsError);
                    writer.WriteLine("Converged\t{0}", feature.report.DidConverge);
                    writer.WriteLine("mz\ttime\tintensity");

                    var prev = feature.prevXic;
                    for (var i = 0; i < feature.x.Count; i++)
                    {
                        var prevTime = "";
                        var prevInt  = "";
                        if (prev != null && i < prev.x.Count)
                        {
                            prevTime = prev.x[i].ToString();
                            prevInt  = prev.y[i].ToString();
                        }

                        writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", feature.mz, feature.x[i], feature.y[i], prevTime, prevInt);
                    }
                    writer.WriteLine();
                }
            }
        }