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); }
/// <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"); }
public void encodeDecodePic5() { 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); } }
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); } }