rainbow-api 1.0.12__tar.gz → 1.0.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rainbow_api-1.0.13/MANIFEST.in +5 -0
- {rainbow_api-1.0.12/rainbow_api.egg-info → rainbow_api-1.0.13}/PKG-INFO +32 -1
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/README.md +31 -0
- rainbow_api-1.0.13/pyproject.toml +41 -0
- rainbow_api-1.0.13/rainbow/agilent/_uvdelta.pyx +166 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/agilent/chemstation.py +84 -25
- {rainbow_api-1.0.12 → rainbow_api-1.0.13/rainbow_api.egg-info}/PKG-INFO +32 -1
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow_api.egg-info/SOURCES.txt +4 -0
- rainbow_api-1.0.13/setup.py +37 -0
- rainbow_api-1.0.13/tests/test_accelerator.py +62 -0
- rainbow_api-1.0.12/pyproject.toml +0 -27
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/COPYING +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/COPYING.LESSER +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/__init__.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/agilent/__init__.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/agilent/masshunter.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/datadirectory.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/datafile.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/waters/__init__.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow/waters/masslynx.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow_api.egg-info/dependency_links.txt +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow_api.egg-info/requires.txt +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/rainbow_api.egg-info/top_level.txt +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/setup.cfg +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/tests/test_agilent.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/tests/test_datadirectory.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/tests/test_datafile.py +0 -0
- {rainbow_api-1.0.12 → rainbow_api-1.0.13}/tests/test_waters.py +0 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Ship the Cython source for the optional accelerator so source builds (sdist)
|
|
2
|
+
# can compile it. Exclude the generated C file: it is a build artifact and is
|
|
3
|
+
# regenerated from the .pyx at build time (Cython is in build-system.requires).
|
|
4
|
+
include rainbow/agilent/_uvdelta.pyx
|
|
5
|
+
exclude rainbow/agilent/_uvdelta.c
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: rainbow-api
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.13
|
|
4
4
|
Summary: Read chromatography and mass spectrometry binary files
|
|
5
5
|
Author: Evan Shi and Eugene Kwan
|
|
6
6
|
Author-email: evanyshi@cmu.edu
|
|
@@ -42,6 +42,10 @@ There is [documentation](http://rainbow-api.readthedocs.io/) for *rainbow* that
|
|
|
42
42
|
pip install rainbow-api
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
Prebuilt wheels include an optional compiled accelerator (see
|
|
46
|
+
[Performance](#performance)); installation never requires a compiler, and
|
|
47
|
+
*rainbow* works the same with or without it.
|
|
48
|
+
|
|
45
49
|
## Usage
|
|
46
50
|
|
|
47
51
|
The easiest way to get started is to give *rainbow* a directory path. Assume that we have a directory `mydata.D` that contains a binary file `DAD1.uv` with UV data.
|
|
@@ -60,7 +64,34 @@ The raw UV data is contained in numpy arrays that are attributes of `datafile`.
|
|
|
60
64
|
|
|
61
65
|
There is a [tutorial](https://rainbow-api.readthedocs.io/en/latest/tutorial.html) available. There are also example [snippets](https://rainbow-api.readthedocs.io/en/latest/examples.html) for basic tasks. Or just check out the full [API](https://rainbow-api.readthedocs.io/en/latest/api.html).
|
|
62
66
|
|
|
67
|
+
## Performance
|
|
68
|
+
|
|
69
|
+
Decoding Agilent diode-array (DAD) `.uv` files is the one hot spot in the
|
|
70
|
+
library: each file holds a large retention-time × wavelength grid of
|
|
71
|
+
delta-encoded absorbances, and the decoding is inherently sequential, so it
|
|
72
|
+
cannot be vectorized with NumPy. *rainbow* ships an optional compiled extension
|
|
73
|
+
(`rainbow/agilent/_uvdelta.pyx`, built with Cython) that runs this inner loop in
|
|
74
|
+
C, decoding large DAD files roughly **100× faster** while producing
|
|
75
|
+
bit-identical results.
|
|
76
|
+
|
|
77
|
+
The accelerator is entirely optional:
|
|
78
|
+
|
|
79
|
+
* Prebuilt wheels on PyPI already include it, so `pip install rainbow-api`
|
|
80
|
+
gives you the fast path with no compiler needed.
|
|
81
|
+
* If you install from source without a compiler (or without Cython), the build
|
|
82
|
+
simply skips the extension and *rainbow* falls back to a pure-Python decoder
|
|
83
|
+
with identical output. You can check which path is active with
|
|
84
|
+
`rainbow.agilent.chemstation._uvdelta_fast is not None`.
|
|
85
|
+
|
|
86
|
+
Other formats (`.ch` channels, `.ms`, MassHunter) are already fast and do not
|
|
87
|
+
use the accelerator.
|
|
88
|
+
|
|
63
89
|
## Contents
|
|
64
90
|
* `rainbow/` contains the code of the Python library.
|
|
65
91
|
* `docs/` contains code for generating documentation. To build documentation locally, you will need to install the `sphinx` and `sphinx-rtd-theme` packages. Then, move to the `docs/` directory and run `make html`. The docpages will be generated under `docs/_build`.
|
|
66
92
|
* `tests/` contains unit tests for the library. These can be run with `python -m unittest`.
|
|
93
|
+
|
|
94
|
+
For development, an editable install (`pip install -e .`) compiles the optional
|
|
95
|
+
accelerator in place if a C compiler and Cython are available; otherwise the
|
|
96
|
+
pure-Python fallback is used. The parity between the two paths is checked by
|
|
97
|
+
`tests/test_accelerator.py`.
|
|
@@ -23,6 +23,10 @@ There is [documentation](http://rainbow-api.readthedocs.io/) for *rainbow* that
|
|
|
23
23
|
pip install rainbow-api
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
Prebuilt wheels include an optional compiled accelerator (see
|
|
27
|
+
[Performance](#performance)); installation never requires a compiler, and
|
|
28
|
+
*rainbow* works the same with or without it.
|
|
29
|
+
|
|
26
30
|
## Usage
|
|
27
31
|
|
|
28
32
|
The easiest way to get started is to give *rainbow* a directory path. Assume that we have a directory `mydata.D` that contains a binary file `DAD1.uv` with UV data.
|
|
@@ -41,7 +45,34 @@ The raw UV data is contained in numpy arrays that are attributes of `datafile`.
|
|
|
41
45
|
|
|
42
46
|
There is a [tutorial](https://rainbow-api.readthedocs.io/en/latest/tutorial.html) available. There are also example [snippets](https://rainbow-api.readthedocs.io/en/latest/examples.html) for basic tasks. Or just check out the full [API](https://rainbow-api.readthedocs.io/en/latest/api.html).
|
|
43
47
|
|
|
48
|
+
## Performance
|
|
49
|
+
|
|
50
|
+
Decoding Agilent diode-array (DAD) `.uv` files is the one hot spot in the
|
|
51
|
+
library: each file holds a large retention-time × wavelength grid of
|
|
52
|
+
delta-encoded absorbances, and the decoding is inherently sequential, so it
|
|
53
|
+
cannot be vectorized with NumPy. *rainbow* ships an optional compiled extension
|
|
54
|
+
(`rainbow/agilent/_uvdelta.pyx`, built with Cython) that runs this inner loop in
|
|
55
|
+
C, decoding large DAD files roughly **100× faster** while producing
|
|
56
|
+
bit-identical results.
|
|
57
|
+
|
|
58
|
+
The accelerator is entirely optional:
|
|
59
|
+
|
|
60
|
+
* Prebuilt wheels on PyPI already include it, so `pip install rainbow-api`
|
|
61
|
+
gives you the fast path with no compiler needed.
|
|
62
|
+
* If you install from source without a compiler (or without Cython), the build
|
|
63
|
+
simply skips the extension and *rainbow* falls back to a pure-Python decoder
|
|
64
|
+
with identical output. You can check which path is active with
|
|
65
|
+
`rainbow.agilent.chemstation._uvdelta_fast is not None`.
|
|
66
|
+
|
|
67
|
+
Other formats (`.ch` channels, `.ms`, MassHunter) are already fast and do not
|
|
68
|
+
use the accelerator.
|
|
69
|
+
|
|
44
70
|
## Contents
|
|
45
71
|
* `rainbow/` contains the code of the Python library.
|
|
46
72
|
* `docs/` contains code for generating documentation. To build documentation locally, you will need to install the `sphinx` and `sphinx-rtd-theme` packages. Then, move to the `docs/` directory and run `make html`. The docpages will be generated under `docs/_build`.
|
|
47
73
|
* `tests/` contains unit tests for the library. These can be run with `python -m unittest`.
|
|
74
|
+
|
|
75
|
+
For development, an editable install (`pip install -e .`) compiles the optional
|
|
76
|
+
accelerator in place if a C compiler and Cython are available; otherwise the
|
|
77
|
+
pure-Python fallback is used. The parity between the two paths is checked by
|
|
78
|
+
`tests/test_accelerator.py`.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=63.0.0", "wheel", "Cython>=0.29.30"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rainbow-api"
|
|
7
|
+
version = "1.0.13"
|
|
8
|
+
description = "Read chromatography and mass spectrometry binary files"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "COPYING.LESSER"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Evan Shi and Eugene Kwan"},
|
|
13
|
+
{email = "evanyshi@cmu.edu"}
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)"
|
|
17
|
+
]
|
|
18
|
+
dependencies = [
|
|
19
|
+
"numpy",
|
|
20
|
+
"matplotlib",
|
|
21
|
+
"lxml",
|
|
22
|
+
"pandas"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Repository = "https://github.com/evanyeyeye/rainbow"
|
|
27
|
+
Documentation = "https://rainbow-api.readthedocs.io"
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.packages.find]
|
|
30
|
+
# Declare the package explicitly. Without this, setuptools' flat-layout
|
|
31
|
+
# auto-discovery treats sibling directories (e.g. cibuildwheel's wheelhouse/,
|
|
32
|
+
# or tests/, docs/) as top-level packages and fails with "Multiple top-level
|
|
33
|
+
# packages discovered in a flat-layout".
|
|
34
|
+
include = ["rainbow*"]
|
|
35
|
+
|
|
36
|
+
[tool.cibuildwheel]
|
|
37
|
+
# Build CPython wheels for the optional compiled accelerator. The extension
|
|
38
|
+
# is optional, so any platform that can't compile still yields a working
|
|
39
|
+
# (pure-Python-fallback) wheel, and the sdist covers everything else.
|
|
40
|
+
build = "cp38-* cp39-* cp310-* cp311-* cp312-* cp313-*"
|
|
41
|
+
skip = "*-musllinux* *_i686 *-win32"
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# cython: boundscheck=False, wraparound=False, cdivision=True, language_level=3
|
|
2
|
+
"""
|
|
3
|
+
Compiled accelerator for the Agilent ``.uv`` LC-delta decode loop.
|
|
4
|
+
|
|
5
|
+
The pure-Python decoders in :mod:`rainbow.agilent.chemstation` spend almost all
|
|
6
|
+
of their time in a per-sample loop over the absorbance "cube" (retention times
|
|
7
|
+
x wavelengths). For large diode-array (DAD) files this dominates parsing -- on
|
|
8
|
+
the order of 100ms per file. The encoding is inherently sequential (a running
|
|
9
|
+
accumulator with a sentinel that resets it) and its record stride is
|
|
10
|
+
data-dependent, so it cannot be vectorized with NumPy; only a compiled inner
|
|
11
|
+
loop removes the interpreter overhead. This module is that loop, written to
|
|
12
|
+
produce output bit-identical to the pure-Python reference.
|
|
13
|
+
|
|
14
|
+
It is imported opportunistically by ``chemstation.py``. If the extension was
|
|
15
|
+
not built (no compiler or no Cython at install time), the package falls back to
|
|
16
|
+
the pure-Python implementation transparently.
|
|
17
|
+
|
|
18
|
+
Format
|
|
19
|
+
------
|
|
20
|
+
Each retention time is stored as a 22-byte header (the 32-bit time sits 4 bytes
|
|
21
|
+
in) followed by ``nwl`` little-endian samples. Each sample is a 16-bit delta
|
|
22
|
+
added to a running accumulator, *unless* it equals ``-0x8000`` (the sentinel),
|
|
23
|
+
in which case the following 32-bit little-endian integer is the new absolute
|
|
24
|
+
accumulator value. The ``.uv`` fields are little-endian, which matches every
|
|
25
|
+
platform NumPy ships wheels for, so values are read with a native ``memcpy``.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import numpy as np
|
|
29
|
+
from libc.string cimport memcpy
|
|
30
|
+
|
|
31
|
+
# 16-bit sentinel: the next sample is an absolute 32-bit value, not a delta.
|
|
32
|
+
cdef short _SENTINEL = -0x8000
|
|
33
|
+
# Per-record header: 4 pad bytes, 4-byte time, 14 pad bytes.
|
|
34
|
+
cdef Py_ssize_t _HEADER = 22
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
cdef inline Py_ssize_t _decode_row(const unsigned char[::1] buf, Py_ssize_t off,
|
|
38
|
+
Py_ssize_t n, int nwl,
|
|
39
|
+
long long[:, ::1] data,
|
|
40
|
+
Py_ssize_t row) except -1:
|
|
41
|
+
"""Decode one row of ``nwl`` samples into ``data[row]``; return new offset.
|
|
42
|
+
|
|
43
|
+
Raises ``ValueError`` if the buffer ends mid-row -- mirroring the exception
|
|
44
|
+
the pure-Python path would raise on a short read, rather than reading out of
|
|
45
|
+
bounds (this module disables Cython's automatic bounds checks for speed).
|
|
46
|
+
"""
|
|
47
|
+
cdef int j
|
|
48
|
+
cdef long long acc = 0
|
|
49
|
+
cdef short ci
|
|
50
|
+
cdef int v
|
|
51
|
+
for j in range(nwl):
|
|
52
|
+
if off + 2 > n:
|
|
53
|
+
raise ValueError("truncated Agilent .uv delta stream")
|
|
54
|
+
memcpy(&ci, &buf[off], 2)
|
|
55
|
+
off += 2
|
|
56
|
+
if ci == _SENTINEL:
|
|
57
|
+
if off + 4 > n:
|
|
58
|
+
raise ValueError("truncated Agilent .uv delta stream")
|
|
59
|
+
memcpy(&v, &buf[off], 4)
|
|
60
|
+
off += 4
|
|
61
|
+
acc = v
|
|
62
|
+
else:
|
|
63
|
+
acc += ci
|
|
64
|
+
data[row, j] = acc
|
|
65
|
+
return off
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
cdef inline Py_ssize_t _scan_row(const unsigned char[::1] buf, Py_ssize_t off,
|
|
69
|
+
Py_ssize_t n, int nwl):
|
|
70
|
+
"""Return the offset just past a complete row, or -1 if it would overrun.
|
|
71
|
+
|
|
72
|
+
Used to count rows in a variable-length (partial) stream without decoding.
|
|
73
|
+
"""
|
|
74
|
+
cdef int j
|
|
75
|
+
cdef short ci
|
|
76
|
+
for j in range(nwl):
|
|
77
|
+
if off + 2 > n:
|
|
78
|
+
return -1
|
|
79
|
+
memcpy(&ci, &buf[off], 2)
|
|
80
|
+
off += 2
|
|
81
|
+
if ci == _SENTINEL:
|
|
82
|
+
if off + 4 > n:
|
|
83
|
+
return -1
|
|
84
|
+
off += 4
|
|
85
|
+
return off
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def decode_uv_delta(const unsigned char[::1] buf, Py_ssize_t data_start,
|
|
89
|
+
int num_times, int nwl):
|
|
90
|
+
"""Decode a fixed-length ``.uv`` delta stream with a known row count.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
buf: The whole file as a bytes-like buffer.
|
|
94
|
+
data_start: Offset of the first record.
|
|
95
|
+
num_times: Number of retention times (rows) to decode.
|
|
96
|
+
nwl: Number of wavelengths (samples) per row.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
``(times, data)`` where ``times`` is a ``uint32`` array of raw times and
|
|
100
|
+
``data`` is an ``(num_times, nwl)`` ``int64`` array of accumulated values.
|
|
101
|
+
"""
|
|
102
|
+
times_arr = np.empty(num_times, dtype=np.uint32)
|
|
103
|
+
data_arr = np.empty((num_times, nwl), dtype=np.int64)
|
|
104
|
+
cdef unsigned int[::1] times = times_arr
|
|
105
|
+
cdef long long[:, ::1] data = data_arr
|
|
106
|
+
|
|
107
|
+
cdef Py_ssize_t n = buf.shape[0]
|
|
108
|
+
cdef Py_ssize_t off = data_start
|
|
109
|
+
cdef Py_ssize_t i
|
|
110
|
+
cdef unsigned int t
|
|
111
|
+
|
|
112
|
+
for i in range(num_times):
|
|
113
|
+
if off + _HEADER > n:
|
|
114
|
+
raise ValueError("truncated Agilent .uv delta stream")
|
|
115
|
+
memcpy(&t, &buf[off + 4], 4)
|
|
116
|
+
times[i] = t
|
|
117
|
+
off = _decode_row(buf, off + _HEADER, n, nwl, data, i)
|
|
118
|
+
return times_arr, data_arr
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def decode_uv_delta_stream(const unsigned char[::1] buf, Py_ssize_t data_start,
|
|
122
|
+
int nwl):
|
|
123
|
+
"""Decode a partial ``.uv`` delta stream of unknown length (read to EOF).
|
|
124
|
+
|
|
125
|
+
Partial files do not record the number of retention times, so this makes two
|
|
126
|
+
passes: first count the complete rows that fit before the end of the buffer,
|
|
127
|
+
then decode exactly that many. A row is complete only if its header and all
|
|
128
|
+
``nwl`` samples fit -- matching the pure-Python try/except loop, which stops
|
|
129
|
+
at the first short read.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
buf: The whole file as a bytes-like buffer.
|
|
133
|
+
data_start: Offset of the first record.
|
|
134
|
+
nwl: Number of wavelengths (samples) per row.
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
``(times, data)`` as in :func:`decode_uv_delta`.
|
|
138
|
+
"""
|
|
139
|
+
cdef Py_ssize_t n = buf.shape[0]
|
|
140
|
+
cdef Py_ssize_t off = data_start
|
|
141
|
+
cdef Py_ssize_t probe
|
|
142
|
+
cdef int rows = 0
|
|
143
|
+
|
|
144
|
+
# Pass 1: count complete rows.
|
|
145
|
+
while off + _HEADER <= n:
|
|
146
|
+
probe = _scan_row(buf, off + _HEADER, n, nwl)
|
|
147
|
+
if probe < 0:
|
|
148
|
+
break
|
|
149
|
+
rows += 1
|
|
150
|
+
off = probe
|
|
151
|
+
|
|
152
|
+
times_arr = np.empty(rows, dtype=np.uint32)
|
|
153
|
+
data_arr = np.empty((rows, nwl), dtype=np.int64)
|
|
154
|
+
cdef unsigned int[::1] times = times_arr
|
|
155
|
+
cdef long long[:, ::1] data = data_arr
|
|
156
|
+
|
|
157
|
+
cdef Py_ssize_t i
|
|
158
|
+
cdef unsigned int t
|
|
159
|
+
|
|
160
|
+
# Pass 2: decode the rows counted above (none will overrun).
|
|
161
|
+
off = data_start
|
|
162
|
+
for i in range(rows):
|
|
163
|
+
memcpy(&t, &buf[off + 4], 4)
|
|
164
|
+
times[i] = t
|
|
165
|
+
off = _decode_row(buf, off + _HEADER, n, nwl, data, i)
|
|
166
|
+
return times_arr, data_arr
|
|
@@ -10,6 +10,13 @@ import numpy as np
|
|
|
10
10
|
from lxml import etree
|
|
11
11
|
from rainbow.datafile import DataFile
|
|
12
12
|
|
|
13
|
+
# Optional compiled accelerator for the .uv LC-delta decode loop.
|
|
14
|
+
# Falls back to the pure-Python implementations below if not built.
|
|
15
|
+
try:
|
|
16
|
+
from rainbow.agilent import _uvdelta as _uvdelta_fast
|
|
17
|
+
except ImportError:
|
|
18
|
+
_uvdelta_fast = None
|
|
19
|
+
|
|
13
20
|
"""
|
|
14
21
|
MAIN PARSING METHODS
|
|
15
22
|
|
|
@@ -334,6 +341,34 @@ def decode_double_delta(f, offset):
|
|
|
334
341
|
|
|
335
342
|
|
|
336
343
|
def decode_uv_delta(f, data_offsets, num_times, num_wavelengths):
|
|
344
|
+
"""Decode the delta-encoded absorbances of an Agilent .uv file.
|
|
345
|
+
|
|
346
|
+
Each retention time holds ``num_wavelengths`` absorbances stored as 16-bit
|
|
347
|
+
deltas against a running accumulator. The sentinel value ``-0x8000`` instead
|
|
348
|
+
signals that the next 32-bit integer is a new absolute value.
|
|
349
|
+
|
|
350
|
+
If the compiled accelerator (:mod:`rainbow.agilent._uvdelta`) was built it is
|
|
351
|
+
used for the inner loop; otherwise this falls back to the pure-Python loop
|
|
352
|
+
below, which produces identical output. See :obj:`parse_uv`.
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
f (_io.BufferedReader): File opened in 'rb' mode.
|
|
356
|
+
data_offsets (dict): Offsets for this file format.
|
|
357
|
+
num_times (int): Number of retention times.
|
|
358
|
+
num_wavelengths (int): Number of wavelengths per time.
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
Tuple of ``(times, data)``: a uint32 array of raw times and an
|
|
362
|
+
``(num_times, num_wavelengths)`` int64 array of absorbances.
|
|
363
|
+
|
|
364
|
+
"""
|
|
365
|
+
# Use the compiled accelerator if it was built (mirrors the loop below).
|
|
366
|
+
if _uvdelta_fast is not None:
|
|
367
|
+
f.seek(0)
|
|
368
|
+
buf = f.read()
|
|
369
|
+
return _uvdelta_fast.decode_uv_delta(
|
|
370
|
+
buf, data_offsets["data_start"], num_times, num_wavelengths)
|
|
371
|
+
|
|
337
372
|
uint_unpack = struct.Struct('<I').unpack
|
|
338
373
|
int_unpack = struct.Struct('<i').unpack
|
|
339
374
|
short_unpack = struct.Struct('<h').unpack
|
|
@@ -361,6 +396,22 @@ def decode_uv_delta(f, data_offsets, num_times, num_wavelengths):
|
|
|
361
396
|
|
|
362
397
|
|
|
363
398
|
def decode_uv_array(f, data_offsets, num_times, num_wavelengths):
|
|
399
|
+
"""Decode the absorbances of an Agilent .uv file stored as raw doubles.
|
|
400
|
+
|
|
401
|
+
Used by the ``OL`` format variant, where each absorbance is a little-endian
|
|
402
|
+
float64 rather than a delta. See :obj:`parse_uv`.
|
|
403
|
+
|
|
404
|
+
Args:
|
|
405
|
+
f (_io.BufferedReader): File opened in 'rb' mode.
|
|
406
|
+
data_offsets (dict): Offsets for this file format.
|
|
407
|
+
num_times (int): Number of retention times.
|
|
408
|
+
num_wavelengths (int): Number of wavelengths per time.
|
|
409
|
+
|
|
410
|
+
Returns:
|
|
411
|
+
Tuple of ``(times, data)``: a uint32 array of raw times and an
|
|
412
|
+
``(num_times, num_wavelengths)`` float64 array of absorbances.
|
|
413
|
+
|
|
414
|
+
"""
|
|
364
415
|
uint_unpack = struct.Struct('<I').unpack
|
|
365
416
|
|
|
366
417
|
f.seek(data_offsets["data_start"])
|
|
@@ -511,32 +562,40 @@ def parse_uv_partial(path):
|
|
|
511
562
|
return None
|
|
512
563
|
|
|
513
564
|
# Extract the retention times and absorbances from each data segment.
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
565
|
+
if _uvdelta_fast is not None:
|
|
566
|
+
# Compiled path: scan the variable-length stream to EOF.
|
|
567
|
+
f.seek(0)
|
|
568
|
+
buf = f.read()
|
|
569
|
+
times, data = _uvdelta_fast.decode_uv_delta_stream(
|
|
570
|
+
buf, data_offsets['data_start'], wavelengths.size)
|
|
571
|
+
times = times / 60000
|
|
572
|
+
else:
|
|
573
|
+
f.seek(data_offsets['data_start'])
|
|
574
|
+
times = []
|
|
575
|
+
absorbances = []
|
|
576
|
+
while True:
|
|
577
|
+
try:
|
|
578
|
+
f.read(4)
|
|
579
|
+
time = uint_unpack(f.read(4))[0]
|
|
580
|
+
times.append(time)
|
|
581
|
+
f.read(14)
|
|
582
|
+
# If the next short is equal to -0x8000
|
|
583
|
+
# then the next absorbance value is the next integer.
|
|
584
|
+
# Otherwise, the short is a delta from the last absorbance value.
|
|
585
|
+
absorb_accum = 0
|
|
586
|
+
for _ in range(wavelengths.size):
|
|
587
|
+
check_int = short_unpack(f.read(2))[0]
|
|
588
|
+
if check_int == -0x8000:
|
|
589
|
+
absorb_accum = int_unpack(f.read(4))[0]
|
|
590
|
+
else:
|
|
591
|
+
absorb_accum += check_int
|
|
592
|
+
absorbances.append(absorb_accum)
|
|
593
|
+
except Exception:
|
|
594
|
+
break
|
|
536
595
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
596
|
+
# Process the extracted values.
|
|
597
|
+
times = np.array(times) / 60000
|
|
598
|
+
data = np.array(absorbances).reshape((times.size, wavelengths.size))
|
|
540
599
|
|
|
541
600
|
# Scale the absorbances.
|
|
542
601
|
f.seek(data_offsets['scaling_factor'])
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: rainbow-api
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.13
|
|
4
4
|
Summary: Read chromatography and mass spectrometry binary files
|
|
5
5
|
Author: Evan Shi and Eugene Kwan
|
|
6
6
|
Author-email: evanyshi@cmu.edu
|
|
@@ -42,6 +42,10 @@ There is [documentation](http://rainbow-api.readthedocs.io/) for *rainbow* that
|
|
|
42
42
|
pip install rainbow-api
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
Prebuilt wheels include an optional compiled accelerator (see
|
|
46
|
+
[Performance](#performance)); installation never requires a compiler, and
|
|
47
|
+
*rainbow* works the same with or without it.
|
|
48
|
+
|
|
45
49
|
## Usage
|
|
46
50
|
|
|
47
51
|
The easiest way to get started is to give *rainbow* a directory path. Assume that we have a directory `mydata.D` that contains a binary file `DAD1.uv` with UV data.
|
|
@@ -60,7 +64,34 @@ The raw UV data is contained in numpy arrays that are attributes of `datafile`.
|
|
|
60
64
|
|
|
61
65
|
There is a [tutorial](https://rainbow-api.readthedocs.io/en/latest/tutorial.html) available. There are also example [snippets](https://rainbow-api.readthedocs.io/en/latest/examples.html) for basic tasks. Or just check out the full [API](https://rainbow-api.readthedocs.io/en/latest/api.html).
|
|
62
66
|
|
|
67
|
+
## Performance
|
|
68
|
+
|
|
69
|
+
Decoding Agilent diode-array (DAD) `.uv` files is the one hot spot in the
|
|
70
|
+
library: each file holds a large retention-time × wavelength grid of
|
|
71
|
+
delta-encoded absorbances, and the decoding is inherently sequential, so it
|
|
72
|
+
cannot be vectorized with NumPy. *rainbow* ships an optional compiled extension
|
|
73
|
+
(`rainbow/agilent/_uvdelta.pyx`, built with Cython) that runs this inner loop in
|
|
74
|
+
C, decoding large DAD files roughly **100× faster** while producing
|
|
75
|
+
bit-identical results.
|
|
76
|
+
|
|
77
|
+
The accelerator is entirely optional:
|
|
78
|
+
|
|
79
|
+
* Prebuilt wheels on PyPI already include it, so `pip install rainbow-api`
|
|
80
|
+
gives you the fast path with no compiler needed.
|
|
81
|
+
* If you install from source without a compiler (or without Cython), the build
|
|
82
|
+
simply skips the extension and *rainbow* falls back to a pure-Python decoder
|
|
83
|
+
with identical output. You can check which path is active with
|
|
84
|
+
`rainbow.agilent.chemstation._uvdelta_fast is not None`.
|
|
85
|
+
|
|
86
|
+
Other formats (`.ch` channels, `.ms`, MassHunter) are already fast and do not
|
|
87
|
+
use the accelerator.
|
|
88
|
+
|
|
63
89
|
## Contents
|
|
64
90
|
* `rainbow/` contains the code of the Python library.
|
|
65
91
|
* `docs/` contains code for generating documentation. To build documentation locally, you will need to install the `sphinx` and `sphinx-rtd-theme` packages. Then, move to the `docs/` directory and run `make html`. The docpages will be generated under `docs/_build`.
|
|
66
92
|
* `tests/` contains unit tests for the library. These can be run with `python -m unittest`.
|
|
93
|
+
|
|
94
|
+
For development, an editable install (`pip install -e .`) compiles the optional
|
|
95
|
+
accelerator in place if a C compiler and Cython are available; otherwise the
|
|
96
|
+
pure-Python fallback is used. The parity between the two paths is checked by
|
|
97
|
+
`tests/test_accelerator.py`.
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
COPYING
|
|
2
2
|
COPYING.LESSER
|
|
3
|
+
MANIFEST.in
|
|
3
4
|
README.md
|
|
4
5
|
pyproject.toml
|
|
6
|
+
setup.py
|
|
5
7
|
rainbow/__init__.py
|
|
6
8
|
rainbow/datadirectory.py
|
|
7
9
|
rainbow/datafile.py
|
|
8
10
|
rainbow/agilent/__init__.py
|
|
11
|
+
rainbow/agilent/_uvdelta.pyx
|
|
9
12
|
rainbow/agilent/chemstation.py
|
|
10
13
|
rainbow/agilent/masshunter.py
|
|
11
14
|
rainbow/waters/__init__.py
|
|
@@ -15,6 +18,7 @@ rainbow_api.egg-info/SOURCES.txt
|
|
|
15
18
|
rainbow_api.egg-info/dependency_links.txt
|
|
16
19
|
rainbow_api.egg-info/requires.txt
|
|
17
20
|
rainbow_api.egg-info/top_level.txt
|
|
21
|
+
tests/test_accelerator.py
|
|
18
22
|
tests/test_agilent.py
|
|
19
23
|
tests/test_datadirectory.py
|
|
20
24
|
tests/test_datafile.py
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Build hook for the optional compiled accelerator.
|
|
3
|
+
|
|
4
|
+
Project metadata lives in ``pyproject.toml``; this file exists only to compile
|
|
5
|
+
the optional Cython extension that speeds up Agilent .uv decoding. The
|
|
6
|
+
extension is marked ``optional`` so that a missing compiler (or missing Cython)
|
|
7
|
+
never breaks installation -- the package falls back to pure Python.
|
|
8
|
+
"""
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from setuptools import setup, Extension
|
|
12
|
+
|
|
13
|
+
_PYX = "rainbow/agilent/_uvdelta.pyx"
|
|
14
|
+
|
|
15
|
+
ext_modules = []
|
|
16
|
+
# Only attempt to compile the accelerator if both Cython and the source are
|
|
17
|
+
# present. The source is missing only from a malformed sdist; guarding here
|
|
18
|
+
# means such a build degrades to pure Python instead of failing outright.
|
|
19
|
+
if os.path.exists(_PYX):
|
|
20
|
+
try:
|
|
21
|
+
from Cython.Build import cythonize
|
|
22
|
+
|
|
23
|
+
ext_modules = cythonize(
|
|
24
|
+
[
|
|
25
|
+
Extension(
|
|
26
|
+
"rainbow.agilent._uvdelta",
|
|
27
|
+
[_PYX],
|
|
28
|
+
optional=True,
|
|
29
|
+
)
|
|
30
|
+
],
|
|
31
|
+
compiler_directives={"language_level": "3"},
|
|
32
|
+
)
|
|
33
|
+
except ImportError:
|
|
34
|
+
# Cython unavailable at build time: install as pure Python.
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
setup(ext_modules=ext_modules)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tests for the optional compiled .uv accelerator.
|
|
3
|
+
|
|
4
|
+
These verify the contract that matters for the optional extension: when the
|
|
5
|
+
compiled accelerator (:mod:`rainbow.agilent._uvdelta`) is present, it produces
|
|
6
|
+
output bit-identical to the pure-Python fallback, and it fails safely on
|
|
7
|
+
truncated input rather than reading out of bounds. The tests skip themselves
|
|
8
|
+
when the extension was not built.
|
|
9
|
+
"""
|
|
10
|
+
import unittest
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
from rainbow.agilent import chemstation as cs
|
|
16
|
+
|
|
17
|
+
# Every fixture .D directory that contains a .uv file, covering all decode
|
|
18
|
+
# paths: delta (brown=v31, red=v131), partial (green), and array/OL (pink).
|
|
19
|
+
UV_FIXTURES = ["brown", "red", "green", "pink"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _uv_path(color):
|
|
23
|
+
directory = Path("tests") / "inputs" / (color + ".D")
|
|
24
|
+
for entry in directory.iterdir():
|
|
25
|
+
if entry.suffix.lower() == ".uv":
|
|
26
|
+
return str(entry)
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@unittest.skipIf(cs._uvdelta_fast is None, "compiled accelerator not built")
|
|
31
|
+
class TestUVAccelerator(unittest.TestCase):
|
|
32
|
+
|
|
33
|
+
def _parse_pure_python(self, path):
|
|
34
|
+
"""Parse with the accelerator temporarily disabled."""
|
|
35
|
+
saved = cs._uvdelta_fast
|
|
36
|
+
cs._uvdelta_fast = None
|
|
37
|
+
try:
|
|
38
|
+
return cs.parse_uv(path)
|
|
39
|
+
finally:
|
|
40
|
+
cs._uvdelta_fast = saved
|
|
41
|
+
|
|
42
|
+
def test_fast_matches_pure_python(self):
|
|
43
|
+
"""Compiled output is bit-identical to the pure-Python reference."""
|
|
44
|
+
for color in UV_FIXTURES:
|
|
45
|
+
path = _uv_path(color)
|
|
46
|
+
self.assertIsNotNone(path, f"no .uv fixture for {color}")
|
|
47
|
+
with self.subTest(fixture=color):
|
|
48
|
+
fast = cs.parse_uv(path)
|
|
49
|
+
slow = self._parse_pure_python(path)
|
|
50
|
+
np.testing.assert_array_equal(fast.data, slow.data)
|
|
51
|
+
np.testing.assert_array_equal(fast.xlabels, slow.xlabels)
|
|
52
|
+
np.testing.assert_array_equal(fast.ylabels, slow.ylabels)
|
|
53
|
+
|
|
54
|
+
def test_truncated_input_raises(self):
|
|
55
|
+
"""A stream that ends mid-record raises instead of crashing."""
|
|
56
|
+
truncated = b"\x00" * 64
|
|
57
|
+
with self.assertRaises(ValueError):
|
|
58
|
+
cs._uvdelta_fast.decode_uv_delta(truncated, 0, 100, 106)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
unittest.main()
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["setuptools>=63.0.0", "wheel"]
|
|
3
|
-
build-backend = "setuptools.build_meta"
|
|
4
|
-
|
|
5
|
-
[project]
|
|
6
|
-
name = "rainbow-api"
|
|
7
|
-
version = "1.0.12"
|
|
8
|
-
description = "Read chromatography and mass spectrometry binary files"
|
|
9
|
-
readme = "README.md"
|
|
10
|
-
license = {text = "COPYING.LESSER"}
|
|
11
|
-
authors = [
|
|
12
|
-
{name = "Evan Shi and Eugene Kwan"},
|
|
13
|
-
{email = "evanyshi@cmu.edu"}
|
|
14
|
-
]
|
|
15
|
-
classifiers = [
|
|
16
|
-
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)"
|
|
17
|
-
]
|
|
18
|
-
dependencies = [
|
|
19
|
-
"numpy",
|
|
20
|
-
"matplotlib",
|
|
21
|
-
"lxml",
|
|
22
|
-
"pandas"
|
|
23
|
-
]
|
|
24
|
-
|
|
25
|
-
[project.urls]
|
|
26
|
-
Repository = "https://github.com/evanyeyeye/rainbow"
|
|
27
|
-
Documentation = "https://rainbow-api.readthedocs.io"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|