Source code for MSQuant: PILstatistics2Tests.cs, MSQlib1/src/utility/PILstatistics2Tests.cs.

Table of contents page.

Home page for MSQuant.

/****************************************************************************
 * Copyright (C) 2008 Peter Mortensen and Matthias Mann                     *
 * This file is part of MSQuant.                                            *
 *                                                                          *
 * MSQuant is distributed under the terms of                                *
 * the GNU General Public License. See src/COPYING.TXT or                   *
 * <http://www.gnu.org/licenses/gpl.txt> for details.                       *
 *                                                                          *
 * MSQuant is free software; you can redistribute it                        *
 * and/or modify it under the terms of the GNU                              *
 * General Public License as published by the Free                          *
 * Software Foundation; either version 2 of the                             *
 * License, or (at your option) any later version.                          *
 *                                                                          *
 * MSQuant is distributed in the hope that it will be                       *
 * useful, but WITHOUT ANY WARRANTY; without even the                       *
 * implied warranty of MERCHANTABILITY or FITNESS FOR                       *
 * A PARTICULAR PURPOSE.  See the GNU General Public                        *
 * License for more details.                                                *
 *                                                                          *
 * You should have received a copy of the GNU General                       *
 * Public License along with MSQuant; if not, write to                      *
 * the Free Software Foundation, Inc., 59 Temple                            *
 * Place, Suite 330, Boston, MA  02111-1307  USA                            *
 *                                                                          *
 * Purpose: for unit testing purposes only. This file is (effectively) not  *
 *          a part of the MSQuant application.                              *
 *                                                                          *
 ****************************************************************************/

/****************************************************************************
 *                               CEBI                                       *
 *                    Software Development Group                            *
 *                         Peter Mortensen                                  *
 *                E-mail: NUKESPAMMERSdrmortensen@get2netZZZZZZ.dk          *
 *                 WWW: http://www.cebi.sdu.dk/                             *
 *                                                                          *
 *  Program for post-processing of result from search in mass               *
 *    spectrometric data.                                                   *
 *                                                                          *
 *    FILENAME:   PILstatistics2Tests.cs                                    *
 *    TYPE:       CSHARP                                                    *
 *                                                                          *
 * CREATED: PM 2007-11-30   Vrs 1.0. First real useful unit test (for       *
 *                          regression testing of linearRegression()        *
 *                          in PILstatistics2.vb)                           *
 * UPDATED: PM 2008-03-18   Converted from VB.NET to C#.                    *
 *                                                                          *
 *                                                                          *
 *                                                                          *
 ****************************************************************************/

using NUnit.Framework;


//****************************************************************************
//* <placeholder for header>                                                 *
//****************************************************************************
[TestFixture()]
public class PILstatistics2Tests
{


    //****************************************************************************
    //* <placeholder for header>                                                 *
    //****************************************************************************
    [Test()]
    public void CreateClass1()
    {

        // Dim someObj As SDUPutility.statsStructure = Nothing 'Note: someObj
        // will ***not*** be Nothing as it is a Structure. Why doesn't the
        // compiler warn about this????

        SDUPutility.statsStructure someObj =
            new SDUPutility.statsStructure(134, true, true);

        Assert.IsTrue(someObj.tag == 134);
    } //CreateClass1()


    //****************************************************************************
    //* <placeholder for header>                                                 *
    //****************************************************************************
    [Test()]
    public void CreateClass2()
    {

        string someObj = null;
        Assert.IsNull(someObj);
    } //CreateClass2()


    //****************************************************************************
    //* <placeholder for header>                                                 *
    //****************************************************************************
    [Test()]
    public void CreateClass3()
    {

        //Assert.IsFalse(true);
        Assert.IsFalse(false);
    } //CreateClass2()


    //****************************************************************************
    //* <placeholder for header>                                                 *
    //****************************************************************************
    [Test()]
    public void systemAssumptions()
    {
        {
            double num1 = 2.3;
            int int1 = (int)num1; //Note: not the same as VB.NET's CInt() !!!.
            Assert.AreEqual(2, int1, "Assumption about (int) cast not valid...");
        }

        {
            double num2 = 2.8;
            int int2 = (int)num2; //Note: not the same as VB.NET's CInt() !!!.
            Assert.AreEqual(2, int2, "Assumption about (int) cast not valid...");
        }
    } //systemAssumptions()


    //****************************************************************************
    //* <placeholder for header>                                                 *
    //****************************************************************************
    [Test()]
    public void linearRegression_twoPoint()
    {

        int len = 2;
        int lastIndex = len - 1;

        double[] xValues = new double[lastIndex + 1];
        double[] yValues = new double[lastIndex + 1];

        xValues[0] = 0.0;
        yValues[0] = 0.0;

        xValues[1] = 3.0;
        yValues[1] = 3.0;

        double slope;
        double offset;

        double[] yErrorsDomain = new double[lastIndex + 1];
        double minErr;
        double maxErr;
        double medianAbsErr;
        double minXUsed;
        double maxXUsed;
        bool[] useErrors = null;
        SDUPutility.SDUPstatistics.linearRegression(
            ref xValues, ref yValues, out slope, out offset, out yErrorsDomain,
            out minErr, out maxErr, out medianAbsErr, ref useErrors,
            out minXUsed, out maxXUsed);

        Assert.AreEqual(1.0, slope, 1E-20, "Slope is not 1.0");
        Assert.AreEqual(0.0, offset, 1E-20, "offset is not 0.0");

        Assert.AreEqual(0.0, minErr, 1E-20, "minErr is not 0.0");
        Assert.AreEqual(0.0, maxErr, 1E-20, "maxErr is not 0.0");
        Assert.AreEqual(0.0, medianAbsErr, 1E-20, "medianAbsErr is not 0.0");
    } //linearRegression_twoPoint()


    //****************************************************************************
    //* <placeholder for header> *
    //****************************************************************************
    [Test()]
    public void linearRegression_Physics7Point()
    {

        //Data from:
        // <http://phoenix.phys.clemson.edu/labs/torque/torque.html>.

        int len = 7;
        int lastIndex = len - 1;

        double[] xValues = new double[lastIndex + 1];
        double[] yValues = new double[lastIndex + 1];

        xValues[0] = 25.1;
        yValues[0] = 41.5;

        xValues[1] = 27.0;
        yValues[1] = 61.5;

        xValues[2] = 29.0;
        yValues[2] = 81.5;

        xValues[3] = 31.2;
        yValues[3] = 101.5;

        xValues[4] = 33.4;
        yValues[4] = 121.5;

        xValues[5] = 35.0;
        yValues[5] = 141.5;

        xValues[6] = 37.7;
        yValues[6] = 161.5;

        double slope;
        double offset;

        double[] yErrorsDomain = new double[lastIndex + 1];
        double minErr;
        double maxErr;
        double medianAbsErr;
        double minXUsed;
        double maxXUsed;
        bool[] useErrors = null;
        SDUPutility.SDUPstatistics.linearRegression(
            ref xValues, ref yValues, out slope, out offset, out yErrorsDomain,
            out minErr, out maxErr, out medianAbsErr, ref useErrors,
            out minXUsed, out maxXUsed);

        //asdasd() 'minErr and maxErr appears to be ***absolute*** (no
        //sign) values. Is that what we want? We loose
        //this information. Perhaps clients should know the sign?

        Assert.AreEqual(9.6024, slope, 0.0001, "Slope is not 9.6024");
        Assert.AreEqual(-198.0941, offset, 0.0001, "offset is not -198.0941");

        //Note: minErr (below) just happens to be very close to
        // zero. The errors are (rounded):
        //
        // -1.43
        // 0.33
        // 1.13
        // 0.00
        // -1.13
        // 3.51
        // -2.42
        //
        // Median error also happen to be nearly identical zero.
        //
        //
        //These 3 values were found by regression test (using
        //linearRegression() as of 2007-10-30).
        Assert.AreEqual(0.0, minErr, 2E-13, "minErr is not 0.0");
        Assert.AreEqual(3.51097, maxErr, 1E-05, "maxErr is not 3.51097");
        Assert.AreEqual(
            1.12522686, medianAbsErr, 1E-08, "medianAbsErr is not 1.12522686.");
    } //linearRegression_Physics7Point()


    //****************************************************************************
    //* <placeholder for header> *
    //****************************************************************************
    [Test()]
    public void linearRegression_Physics6Point()
    {
        //Data from:
        // <http://phoenix.phys.clemson.edu/labs/torque/torque.html>.

        int len = 6;
        int lastIndex = len - 1;

        double[] xValues = new double[lastIndex + 1];
        double[] yValues = new double[lastIndex + 1];

        xValues[0] = 1.0;
        yValues[0] = 2.6;

        xValues[1] = 2.3;
        yValues[1] = 2.8;

        xValues[2] = 3.1;
        yValues[2] = 3.1;

        xValues[3] = 4.8;
        yValues[3] = 4.7;

        xValues[4] = 5.6;
        yValues[4] = 5.1;

        xValues[5] = 6.3;
        yValues[5] = 5.3;

        double slope;
        double offset;

        double[] yErrorsDomain = new double[lastIndex + 1];
        double minErr;
        double maxErr;
        double medianAbsErr;
        double minXUsed;
        double maxXUsed;
        bool[] useErrors = null;
        SDUPutility.SDUPstatistics.linearRegression(
            ref xValues, ref yValues, out slope, out offset, out yErrorsDomain,
            out minErr, out maxErr, out medianAbsErr, ref useErrors,
            out minXUsed, out maxXUsed);

        //asdasd() 'minErr and maxErr appears to be ***absolute*** (no
        //sign) values. Is that what we want? We loose
        //this information. Perhaps clients should know the sign?

        Assert.AreEqual(0.5842, slope, 0.0001, "Slope is not 0.5842");
        Assert.AreEqual(1.6842, offset, 0.0001, "offset is not 1.6842");

        //Note: minErr just happens to be very close to zero. The errors
        // are (rounded):
        //
        // 0.33
        // -0.23
        // -0.40
        // 0.21
        // 0.14
        // -0.06

        //These 3 values were found by regression test (using
        //linearRegression() as of 2007-10-30).
        Assert.AreEqual(0.06458, minErr, 1E-05, "minErr is not 0.06458");
        Assert.AreEqual(0.395195, maxErr, 1E-06, "maxErr is not 0.395195");
        Assert.AreEqual(0.2278, medianAbsErr, 0.0001, "medianAbsErr is not 0.0");
    } //linearRegression_Physics6Point()


    //****************************************************************************
    //* <placeholder for header> *
    //****************************************************************************
    [Test()]
    public void NoverK()
    {

        double res1 = SDUPutility.SDUPstatistics.NoverK(17, 2);
        Assert.AreEqual(136, res1, 1E-06, "Permutations is not 136.");

        double res2 = SDUPutility.SDUPstatistics.NoverK(19, 3);
        Assert.AreEqual(969, res2, 1E-06, "Permutations is not 969.");

        double res3 = SDUPutility.SDUPstatistics.NoverK(14, 1);
        Assert.AreEqual(14, res3, 1E-06, "Permutations is not 14.");

        double res4 = SDUPutility.SDUPstatistics.NoverK(21, 6);
        Assert.AreEqual(54264, res4, 1E-06, "Permutations is not 54264.");

        double res5 = SDUPutility.SDUPstatistics.NoverK(14, 14);
        Assert.AreEqual(1, res5, 1E-06, "Permutations is not 1.");

        //Test we have a reasonable implementation....
        double res6 = SDUPutility.SDUPstatistics.NoverK(196, 6);
        Assert.AreEqual(72887293024, res6, 1E-06, "Permutations.");
        //  Value obtained by regression.

    } //NoverK()


    //****************************************************************************
    //* <placeholder for header> *
    //****************************************************************************
    [Test()]
    public void binning()
    {
        double tol = 1.0E-6;

        SDUPutility.statsStructure calibValues =
            new SDUPutility.statsStructure(27, false, false);

        calibValues.update(13.0, 0.1);
        calibValues.update(17.0, 0.1);
        calibValues.update(19.01, 0.1);

        Assert.AreEqual( 19.01, calibValues.maxX, tol, "");
        Assert.AreEqual(  0.1, calibValues.maxY, tol, "");
        Assert.AreEqual( 13.0, calibValues.minX, tol, "");
        Assert.AreEqual(  0.1, calibValues.minY, tol, "");
        Assert.AreEqual(    3, calibValues.N, "");

        Assert.AreEqual( 0.3, calibValues.sumOfabsY, tol, "");
        Assert.AreEqual(819.3801, calibValues.sumOfSqX7, tol, "");
        Assert.AreEqual(49.01, calibValues.sumOfX, tol, "");
        Assert.AreEqual(4.901, calibValues.sumOfXYproduct, tol, "");
        Assert.AreEqual( 0.3, calibValues.sumOfY, tol, "");

        //2 3 5 7 11 13 17 19 23 29
        //31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
        //101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191
        //193 197 199

        //Detected after the fact VB.NET's CInt() vs. C# (int) cast:
        {
            double binSize = 2.0;
            int bins;
            int[] binningArray;
            //x values in this case as we want to use those for this test.
            SDUPutility.SDUPstatistics.binningSetup_inputBinSize(
                calibValues.minX, calibValues.maxX,
                binSize,
                out binningArray, out bins);

            Assert.AreEqual(4, bins, "Number of bins not as expected.");

            Assert.AreEqual(2.0, binSize, tol, 
                            "binSize was un unexpectedly change by binningSetup().");

            int binLen = binningArray.Length;
            Assert.AreEqual(4, binLen, "Number of bins not as expected.");
        }

        //What is this???
        {
            double binSize = -1.0; //Flag that it should be computed and returned.

        }
    
    } //binning()


} //PILstatistics2Tests


    

    

Generated by script codePublish.pl at 2009-01-05T15:20:59.