示例#1
0
        public void encodeInt()
        {
            byte[] res = new byte[10];
            int    l;

            l = MSNumpress.encodeInt(0L, res, 0);
            Assert.AreEqual(1, l);
            Assert.AreEqual(8, res[0] & 0xf);

            l = MSNumpress.encodeInt(-1L, res, 0);
            Assert.AreEqual(2, l);
            Assert.AreEqual(0xf, res[0] & 0xf);
            Assert.AreEqual(0xf, res[1] & 0xf);

            l = MSNumpress.encodeInt(35L, res, 0);
            Assert.AreEqual(3, l);
            Assert.AreEqual(6, res[0] & 0xf);
            Assert.AreEqual(0x3, res[1] & 0xf);
            Assert.AreEqual(0x2, res[2] & 0xf);

            l = MSNumpress.encodeInt(370000000L, res, 0);
            Assert.AreEqual(9, l);
            Assert.AreEqual(0, res[0] & 0xf);
            Assert.AreEqual(0x0, res[1] & 0xf);
            Assert.AreEqual(0x8, res[2] & 0xf);
            Assert.AreEqual(0x0, res[3] & 0xf);
            Assert.AreEqual(0xc, res[4] & 0xf);
            Assert.AreEqual(0xd, res[5] & 0xf);
            Assert.AreEqual(0x0, res[6] & 0xf);
            Assert.AreEqual(0x6, res[7] & 0xf);
            Assert.AreEqual(0x1, res[8] & 0xf);
        }
示例#2
0
        public void encodeDecodeSlof()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] ics = new double[n];
            for (int i = 0; i < n; i++)
            {
                ics[i] = Math.Pow(10, 6 * random.NextDouble());
            }

            byte[] encoded      = new byte[n * 2 + 8];
            double fixedPoint   = MSNumpress.optimalSlofFixedPoint(ics, n);
            int    encodedBytes = MSNumpress.encodeSlof(ics, n, encoded, fixedPoint);

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

            Assert.AreEqual(n, decodedDoubles);

            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(0.0, (ics[i] - decoded[i]) / ((ics[i] + decoded[i]) / 2), 0.0005);
            }
        }
示例#3
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);
            }
        }
示例#4
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);
        }
示例#5
0
        public void encodeDecodeFixedPoint()
        {
            double fp = 300.21941382293625;

            byte[] encoded = new byte[8];
            MSNumpress.encodeFixedPoint(fp, encoded);
            double decoded = MSNumpress.decodeFixedPoint(encoded);

            Assert.AreEqual(fp, decoded, 0);
        }
示例#6
0
        public void LargeNegativesTest()
        {
            Random random = new Random();
            int    n      = 5;

            double[] ics        = new double[] { -1000, 1000, -2000, 2000, -3000 };
            double   fixedPoint = MSNumpress.optimalSlofFixedPoint(ics, n);

            Assert.AreNotEqual(fixedPoint, Double.NaN);
        }
示例#7
0
        public void DecodeShotPicManuallyAndAutomatically()
        {
            var bytes           = new byte[] { 106, 40, 134, 162 };
            var manual          = new double[4];
            var numberOfDoubles = MSNumpress.decodePic(bytes, bytes.Length, manual);

            Assert.AreEqual(4, numberOfDoubles);
            var automatic = MSNumpress.decode(MSNumpress.ACC_NUMPRESS_PIC, bytes, bytes.Length);

            CollectionAssert.AreEqual(manual, automatic);
        }
示例#8
0
 public void encodeFixedPoint()
 {
     byte[] encoded = new byte[8];
     MSNumpress.encodeFixedPoint(1.00, encoded);
     Assert.AreEqual(0x3f, 0xff & encoded[0]);
     Assert.AreEqual(0xf0, 0xff & encoded[1]);
     Assert.AreEqual(0x0, 0xff & encoded[2]);
     Assert.AreEqual(0x0, 0xff & encoded[3]);
     Assert.AreEqual(0x0, 0xff & encoded[4]);
     Assert.AreEqual(0x0, 0xff & encoded[5]);
     Assert.AreEqual(0x0, 0xff & encoded[6]);
     Assert.AreEqual(0x0, 0xff & encoded[7]);
 }
示例#9
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");
    }
示例#10
0
        public void encodeLinear()
        {
            double[] mzs          = { 100.0, 200.0, 300.00005, 400.00010 };
            byte[]   encoded      = new byte[40];
            int      encodedBytes = MSNumpress.encodeLinear(mzs, 4, encoded, 100000.0);

            Assert.AreEqual(18, encodedBytes);
            Assert.AreEqual(0x80, 0xff & encoded[8]);
            Assert.AreEqual(0x96, 0xff & encoded[9]);
            Assert.AreEqual(0x98, 0xff & encoded[10]);
            Assert.AreEqual(0x00, 0xff & encoded[11]);
            Assert.AreEqual(0x75, 0xff & encoded[16]);
            Assert.AreEqual(0x80, 0xf0 & encoded[17]);
        }
示例#11
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);
        }
示例#12
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);
        }
示例#13
0
        public void NegativesOneTest()
        {
            Random random = new Random();
            int    n      = 5;

            double[] ics        = new double[] { -1, 1, -2, 2, -3 };
            double   fixedPoint = MSNumpress.optimalSlofFixedPoint(ics, n);

            Assert.AreNotEqual(fixedPoint, Double.NaN);

            ics        = new double[] { -1.001, 1.001, -2.001, 2.001, -3.001 };
            fixedPoint = MSNumpress.optimalSlofFixedPoint(ics, n);
            Assert.AreNotEqual(fixedPoint, Double.NaN);

            ics        = new double[] { -.99, .99, -1.99, 1.99, -2.99 };
            fixedPoint = MSNumpress.optimalSlofFixedPoint(ics, n);
            Assert.AreNotEqual(fixedPoint, Double.NaN);
        }
示例#14
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);
            }
        }
示例#15
0
        public void encodeDecodeSlof5()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] ics = new double[n];
            for (int i = 0; i < n; i++)
            {
                ics[i] = Math.Pow(10, 6 * random.NextDouble());
            }

            byte[]   encoded      = new byte[n * 2 + 8];
            double[] decoded      = new double[n];
            double[] firstDecoded = new double[n];
            double   fixedPoint   = MSNumpress.optimalSlofFixedPoint(ics, n);

            int encodedBytes   = MSNumpress.encodeSlof(ics, n, encoded, fixedPoint);
            int decodedDoubles = MSNumpress.decodeSlof(encoded, encodedBytes, decoded);

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

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

            Assert.AreEqual(n, decodedDoubles);

            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(firstDecoded[i], decoded[i], double.Epsilon);
            }
        }
示例#16
0
        public void TestEncodeDecodePic5()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] ics = new double[n];
            for (int i = 0; i < n; i++)
            {
                ics[i] = Math.Pow(10, 6 * random.NextDouble());
            }

            byte[]   encoded      = new byte[n * 5];
            double[] decoded      = new double[n];
            double[] firstDecoded = new double[n];

            int encodedBytes   = MSNumpress.encodePic(ics, n, encoded);
            int decodedDoubles = MSNumpress.decodePic(encoded, encodedBytes, decoded);

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

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

            Assert.AreEqual(n, decodedDoubles);

            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(firstDecoded[i], decoded[i], double.Epsilon);
            }
        }
示例#17
0
        public void encodeDecodePic()
        {
            Random random = new Random();
            int    n      = 1000;

            double[] ics = new double[n];
            for (int i = 0; i < n; i++)
            {
                ics[i] = Math.Pow(10, 6 * random.NextDouble());
            }

            byte[] encoded      = new byte[n * 5];
            int    encodedBytes = MSNumpress.encodePic(ics, n, encoded);

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

            Assert.AreEqual(n, decodedDoubles);

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