Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: patches read_centroid_spectrum function #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/imzy/_readers/bruker/_tsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,35 @@ def read_centroid_spectrum(self, index: int) -> ty.Tuple[np.ndarray, np.ndarray]
"""Read centroid spectrum."""
# buffer-growing loop
while True:
cnt = int(self.profile_buffer_size) # necessary cast to run with python 3.5
cnt = int(self.line_buffer_size) # necessary cast to run with python 3.5
index_buf = np.empty(shape=cnt, dtype=np.float64)
intensity_buf = np.empty(shape=cnt, dtype=np.float32)

index = index + 1 # We need to add 1 to the index to match timsTOF 1-index with numpy self.pixels
required_len = self.dll.tsf_read_line_spectrum_v2(
self.handle,
index,
index_buf.ctypes.data_as(POINTER(c_double)),
intensity_buf.ctypes.data_as(POINTER(c_float)),
self.profile_buffer_size,
self.line_buffer_size,
)

if required_len < 0:
_throw_last_error(self.dll)

if required_len > self.profile_buffer_size:
if required_len > self.line_buffer_size:
if required_len > 16777216:
# arbitrary limit for now...
raise RuntimeError("Maximum expected frame size exceeded.")
self.line_buffer_size = required_len # grow buffer
else:
break
return index_buf[0:required_len], intensity_buf[0:required_len]

mzs = self._call_conversion_func(
index, index_buf[0:required_len], self._dll_index_to_mz_func
)

return mzs[0:required_len], intensity_buf[0:required_len]

def _read_spectrum(self, index: int) -> ty.Tuple[np.ndarray, np.ndarray]:
return self.mz_x, self.read_profile_spectrum(index)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_tsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
from imzy import TSFReader, get_reader
from koyo.system import IS_MAC
import numpy as np

from .utilities import get_tsf_data

Expand Down Expand Up @@ -81,3 +82,15 @@ def test_to_h5(path, tmp_path):
h5_path = reader.to_hdf5(h5_temp, mzs, tol=0.5)
assert h5_path.exists()
assert h5_temp != h5_path


@pytest.mark.skipif(IS_MAC, reason="Bruker reader is not supported on macOS.")
@pytest.mark.parametrize("path", get_tsf_data())
def test_read_centroid_spectrum(path):
reader = TSFReader(path)
for pixel in reader.pixels:
mzs, intensities = reader.read_centroid_spectrum(pixel)
assert mzs.dtype == np.float64
assert intensities.dtype == np.float32
assert pixel == reader.n_pixels - 1