| 1 |
|
| 2 |
""" |
| 3 |
Classes to encode and decode :py:attr:`~pymzml.spec.Spectrum.mz` and |
| 4 |
:py:attr:`~pymzml.spec.Spectrum.i` values. |
| 5 |
|
| 6 |
@author M. Kösters, C. Fufezan |
| 7 |
""" |
| 8 |
import warnings |
| 9 |
import zlib |
| 10 |
from base64 import b64decode as b64dec |
| 11 |
from multiprocessing import Pool |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
|
| 15 |
|
| 16 |
try: |
| 17 |
|
| 18 |
import pynumpress |
| 19 |
|
| 20 |
MSDecoder = pynumpress |
| 21 |
except ImportError: |
| 22 |
|
| 23 |
import pymzml.ms_numpress |
| 24 |
|
| 25 |
warnings.warn( |
| 26 |
"Cython PyNumpress is not installed; falling back to slower, python-only version", |
| 27 |
ImportWarning, |
| 28 |
) |
| 29 |
MSDecoder = pymzml.ms_numpress.MSNumpress() |
| 30 |
|
| 31 |
|
| 32 |
def _decode(data, comp, d_array_length, f_type, d_type): |
| 33 |
""" |
| 34 |
Decode ms-numpress, b64 and/or zlib compressed data. |
| 35 |
|
| 36 |
Args: |
| 37 |
data (str): compressed data |
| 38 |
comp (str): compression method |
| 39 |
d_array_length (int): length of the uncompressed data array |
| 40 |
fType (str): float type (32 or 64 bit) |
| 41 |
d_type (str): type of data (mz, i, or time) |
| 42 |
|
| 43 |
Returns: |
| 44 |
result (tuple(str, list)): tuple containing the datatype and the |
| 45 |
decompressed data list. |
| 46 |
""" |
| 47 |
if f_type == "32-bit float": |
| 48 |
f_type = np.float32 |
| 49 |
elif f_type == "64-bit float": |
| 50 |
f_type = np.float64 |
| 51 |
else: |
| 52 |
f_type = None |
| 53 |
|
| 54 |
decoded_data = b64dec(data) |
| 55 |
if "zlib" in comp or "zlib compression" in comp: |
| 56 |
decoded_data = zlib.decompress(decoded_data) |
| 57 |
|
| 58 |
if ( |
| 59 |
"ms-np-linear" in comp |
| 60 |
or "ms-np-pic" in comp |
| 61 |
or "ms-np-slof" in comp |
| 62 |
or "MS-Numpress linear prediction compression" in comp |
| 63 |
or "MS-Numpress short logged float compression" in comp |
| 64 |
): |
| 65 |
result = [] |
| 66 |
|
| 67 |
if ( |
| 68 |
"ms-np-linear" in comp |
| 69 |
or "MS-Numpress linear prediction compression" in comp |
| 70 |
): |
| 71 |
result = MSDecoder.decodeLinear(decoded_data) |
| 72 |
elif "ms-np-pic" in comp: |
| 73 |
result = MSDecoder.decode_pic(decoded_data) |
| 74 |
elif ( |
| 75 |
"ms-np-slof" in comp or "MS-Numpress short logged float compression" in comp |
| 76 |
): |
| 77 |
result = MSDecoder.decode_slof(decoded_data) |
| 78 |
return (d_type, result) |
| 79 |
|
| 80 |
array = np.fromstring(decoded_data, f_type) |
| 81 |
return (d_type, array) |
| 82 |
|
| 83 |
|
| 84 |
class Decoder: |
| 85 |
""" |
| 86 |
Decoder class to enable parallel decoding of peaks. |
| 87 |
|
| 88 |
Keyword Args: |
| 89 |
nb_worker(int): number of pool workers to use. Defaults to 2. |
| 90 |
""" |
| 91 |
|
| 92 |
def __init__(self, nb_workers=2): |
| 93 |
""" """ |
| 94 |
self._mz = None |
| 95 |
self._i = None |
| 96 |
|
| 97 |
|
| 98 |
def pool_decode(self, data, callback): |
| 99 |
""" |
| 100 |
Decode mz and i values in parallel. |
| 101 |
|
| 102 |
Args: |
| 103 |
data (): ... |
| 104 |
|
| 105 |
Keyword Args: |
| 106 |
callback (:obj:`func`): Callback function to call if decoding is |
| 107 |
finished. Should be :py:meth:`~pymzml.spec.Spectrum._register`. |
| 108 |
""" |
| 109 |
ZE_POOL = Pool(processes=2) |
| 110 |
|
| 111 |
ZE_POOL.starmap(_decode, data) |
| 112 |
|
| 113 |
def _error_callback(self, result): |
| 114 |
""" """ |
| 115 |
raise Exception("Failed with error:\n{0}".format(result)) |
| 116 |
|
| 117 |
|
| 118 |
if __name__ == "__main__": |
| 119 |
print(__doc__) |