示例#1
0
        public void encodeDecodeLinear()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] mzs = new double[n];
            mzs[0] = 300 + random.NextDouble();
            for (int i = 1; i < n; i++)
            {
                mzs[i] = mzs[i - 1] + random.NextDouble();
            }

            byte[] encoded      = new byte[n * 5];
            double fixedPoint   = MSNumpress.optimalLinearFixedPoint(mzs, n);
            int    encodedBytes = MSNumpress.encodeLinear(mzs, n, encoded, fixedPoint);

            double[] decoded        = new double[n];
            int      decodedDoubles = MSNumpress.decodeLinear(encoded, encodedBytes, decoded);

            Assert.AreEqual(n, decodedDoubles);

            var list = Enumerable.Range(0, 1000).Select(i => decoded[i] / mzs[i]).ToList();

            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(mzs[i], decoded[i], 0.000005);
            }
        }
示例#2
0
        public void encodeDecodeLinearEmpty()
        {
            byte[] encoded      = new byte[8];
            int    encodedBytes = MSNumpress.encodeLinear(new double[0], 0, encoded, 100000.0);

            double[] decoded      = new double[0];
            int      decodedBytes = MSNumpress.decodeLinear(encoded, 8, decoded);

            Assert.AreEqual(0, decodedBytes);
        }
示例#3
0
    /// <summary>
    /// Convenience function for decoding binary data encoded by MSNumpress.
    /// </summary>
    /// <param name="cvAccession">The PSI-MS obo CV accession of the encoded data.</param>
    /// <param name="data">array of double to be encoded.</param>
    /// <param name="dataSize">number of doubles from data to encode.</param>
    /// <returns>The decoded doubles</returns>
    /// <exception cref="ArgumentException">
    /// Cannot decode numLin data, need at least 8 initial bytes for fixed point.
    /// or
    /// Corrupt numLin data!
    /// or
    /// Cannot decode numPic data, need at least 8 initial bytes for fixed point.
    /// or
    /// Corrupt numPic data!
    /// or
    /// '" + cvAccession + "' is not a numpress compression term
    /// </exception>
    /// <remarks>
    /// If the passed cvAccession is one of
    ///
    ///    ACC_NUMPRESS_LINEAR = "MS:1002312"
    ///    ACC_NUMPRESS_PIC    = "MS:1002313"
    ///    ACC_NUMPRESS_SLOF   = "MS:1002314"
    ///
    /// the corresponding decode function will be called.
    /// </remarks>
    public static double[] decode(string cvAccession, byte[] data, int dataSize)
    {
        if (cvAccession == ACC_NUMPRESS_LINEAR)
        {
            if (dataSize < 8 || data.Length < 8)
            {
                throw new ArgumentException("Cannot decode numLin data, need at least 8 initial bytes for fixed point.");
            }

            double[] buffer       = new double[dataSize * 2];
            int      nbrOfDoubles = MSNumpress.decodeLinear(data, dataSize, buffer);
            if (nbrOfDoubles < 0)
            {
                throw new ArgumentException("Corrupt numLin data!");
            }

            double[] result = new double[nbrOfDoubles];
            Array.Copy(buffer, 0, result, 0, nbrOfDoubles);

            return(result);
        }

        if (cvAccession == ACC_NUMPRESS_SLOF)
        {
            double[] result = new double[(dataSize - 8) / 2];
            MSNumpress.decodeSlof(data, dataSize, result);

            return(result);
        }

        if (cvAccession == ACC_NUMPRESS_PIC)
        {
            if (dataSize < 8 || data.Length < 8)
            {
                throw new ArgumentException("Cannot decode numPic data, need at least 8 initial bytes for fixed point.");
            }

            double[] buffer       = new double[dataSize * 2];
            int      nbrOfDoubles = MSNumpress.decodePic(data, dataSize, buffer);
            if (nbrOfDoubles < 0)
            {
                throw new ArgumentException("Corrupt numPic data!");
            }

            double[] result = new double[nbrOfDoubles];
            Array.Copy(buffer, 0, result, 0, nbrOfDoubles);
            return(result);
        }

        throw new ArgumentException("'" + cvAccession + "' is not a numpress compression term");
    }
示例#4
0
        public void decodeLinearWierd()
        {
            double[] mzs          = { 100.0, 200.0, 4000.00005, 0.00010 };
            byte[]   encoded      = new byte[28];
            double   fixedPoint   = MSNumpress.optimalLinearFixedPoint(mzs, 4);
            int      encodedBytes = MSNumpress.encodeLinear(mzs, 4, encoded, fixedPoint);

            double[] decoded    = new double[4];
            int      numDecoded = MSNumpress.decodeLinear(encoded, encodedBytes, decoded);

            Assert.AreEqual(100.0, decoded[0], 0.000005);
            Assert.AreEqual(200.0, decoded[1], 0.000005);
            Assert.AreEqual(4000.00005, decoded[2], 0.000005);
            Assert.AreEqual(0.00010, decoded[3], 0.000005);
        }
示例#5
0
        public void decodeLinearNice()
        {
            double[] mzs          = { 100.0, 200.0, 300.00005, 400.00010 };
            byte[]   encoded      = new byte[28];
            int      encodedBytes = MSNumpress.encodeLinear(mzs, 4, encoded, 100000.0);

            double[] decoded    = new double[4];
            int      numDecoded = MSNumpress.decodeLinear(encoded, encodedBytes, decoded);

            Assert.AreEqual(4, numDecoded);
            Assert.AreEqual(100.0, decoded[0], 0.000005);
            Assert.AreEqual(200.0, decoded[1], 0.000005);
            Assert.AreEqual(300.00005, decoded[2], 0.000005);
            Assert.AreEqual(400.00010, decoded[3], 0.000005);
        }
示例#6
0
        public void encodeDecodeLinear5()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] mzs = new double[n];
            mzs[0] = 300 + random.NextDouble();
            for (int i = 1; i < n; i++)
            {
                mzs[i] = mzs[i - 1] + random.NextDouble();
            }

            byte[]   encoded      = new byte[n * 5];
            double[] decoded      = new double[n];
            double[] firstDecoded = new double[n];
            double   fixedPoint   = MSNumpress.optimalLinearFixedPoint(mzs, n);

            int encodedBytes   = MSNumpress.encodeLinear(mzs, n, encoded, fixedPoint);
            int decodedDoubles = MSNumpress.decodeLinear(encoded, encodedBytes, decoded);

            for (int i = 0; i < n; i++)
            {
                firstDecoded[i] = decoded[i];
            }

            for (int i = 0; i < 5; i++)
            {
                MSNumpress.encodeLinear(decoded, n, encoded, fixedPoint);
                MSNumpress.decodeLinear(encoded, encodedBytes, decoded);
            }

            Assert.AreEqual(n, decodedDoubles);

            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(firstDecoded[i], decoded[i], double.Epsilon);
            }
        }