Skip to content

Commit

Permalink
Remove PyInt references, work around Py3.11 Cython default argument p…
Browse files Browse the repository at this point in the history
…roblem
  • Loading branch information
mobiusklein committed Dec 5, 2023
1 parent e7c14a9 commit b8be01e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 29 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def Extension(*args, **kwargs):
else:
ext.extra_compile_args.append("-g3")
ext.extra_compile_args.append("-O0")
# ext.extra_compile_args.append("/fsanitize=address")
return ext

def OpenMPExtension(*args, **kwargs):
Expand Down
33 changes: 30 additions & 3 deletions src/ms_peak_picker/_c/peak_picker.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ cpdef enum PeakFit:
apex = 3


cdef enum range_tag:
mz_range = 1
index_range = 2
full_range = 3


cdef struct mz_range_t:
range_tag tag
double start_mz
double stop_mz


cdef struct index_range_t:
range_tag tag
Py_ssize_t start_index
Py_ssize_t stop_index


cdef struct full_range_t:
range_tag tag


cdef union discovery_range_t:
mz_range_t mz
index_range_t index
full_range_t full


cdef class PartialPeakFitState(object):
cdef:
public bint set
Expand Down Expand Up @@ -48,10 +76,9 @@ cdef class PeakProcessor(object):
cpdef object set_intensity_threshold(self, double intensity_threshold)


cpdef size_t _discover_peaks(self, np.ndarray[cython.floating, ndim=1, mode='c'] mz_array,
cdef size_t _discover_peaks(self, np.ndarray[cython.floating, ndim=1, mode='c'] mz_array,
np.ndarray[cython.floating, ndim=1, mode='c'] intensity_array,
double start_mz, double stop_mz,
Py_ssize_t start_index=*, Py_ssize_t stop_index=*) noexcept
discovery_range_t discovery_range) noexcept

cpdef double find_full_width_at_half_max(self, Py_ssize_t index,
np.ndarray[cython.floating, ndim=1, mode='c'] mz_array,
Expand Down
71 changes: 53 additions & 18 deletions src/ms_peak_picker/_c/peak_picker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -223,30 +223,41 @@ cdef class PeakProcessor(object):
cdef:
double _start_mz, _stop_mz
Py_ssize_t _start_index, _stop_index
discovery_range_t discovery_range

if start_mz is None:
_start_mz = -1
else:
_start_mz = start_mz
if stop_mz is None:
_stop_mz = -1
else:
_stop_mz = stop_mz
if start_index is None:
_start_index = 0
else:
_start_index = start_index
if stop_index is None:
_stop_index = 0
if start_mz is not None or stop_mz is not None:
if start_mz is None:
_start_mz = -1
else:
_start_mz = start_mz
if stop_mz is None:
_stop_mz = -1
else:
_stop_mz = stop_mz
discovery_range.mz.tag = range_tag.mz_range
discovery_range.mz.start_mz = _start_mz
discovery_range.mz.stop_mz = _stop_mz
elif start_index is not None or stop_index is not None:
if start_index is None:
_start_index = 0
else:
_start_index = start_index
if stop_index is None:
_stop_index = 0
else:
_stop_index = stop_index
discovery_range.index.tag = range_tag.index_range
discovery_range.index.start_index = _start_index
discovery_range.index.stop_index = _stop_index
else:
_stop_index = stop_index
return self._discover_peaks(mz_array, intensity_array, _start_mz, _stop_mz, _start_index, _stop_index)
discovery_range.full.tag = range_tag.full_range
return self._discover_peaks(mz_array, intensity_array, discovery_range)

@cython.nonecheck(False)
@cython.cdivision(True)
@cython.boundscheck(False)
cpdef size_t _discover_peaks(self, np.ndarray[cython.floating, ndim=1, mode='c'] mz_array, np.ndarray[cython.floating, ndim=1, mode='c'] intensity_array,
double start_mz, double stop_mz, Py_ssize_t start_index=0, Py_ssize_t stop_index=0) noexcept:
cdef size_t _discover_peaks(self, np.ndarray[cython.floating, ndim=1, mode='c'] mz_array, np.ndarray[cython.floating, ndim=1, mode='c'] intensity_array,
discovery_range_t discovery_range) noexcept:
"""Carries out the peak picking process on `mz_array` and `intensity_array`. All
peaks picked are appended to :attr:`peak_data`.
Expand Down Expand Up @@ -277,8 +288,32 @@ cdef class PeakProcessor(object):
size_t offset
bint infer_range

double start_mz, stop_mz
ssize_t start_index, stop_index

FittedPeak peak

if discovery_range.mz.tag == range_tag.mz_range:
start_mz = discovery_range.mz.start_mz
stop_mz = discovery_range.mz.stop_mz
else:
start_mz = -1
stop_mz = -1

if discovery_range.index.tag == range_tag.index_range:
start_index = discovery_range.index.start_index
stop_index = discovery_range.index.stop_index
else:
start_index = 0
stop_index = 0

if discovery_range.full.tag == range_tag.full_range:
start_mz = -1
stop_mz = -1
start_index = 0
stop_index = 0


verbose = self.verbose
is_centroid = self.peak_mode == PeakMode.centroid

Expand Down
8 changes: 4 additions & 4 deletions src/ms_peak_picker/_c/scan_averaging.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import numpy as np
cdef extern from * nogil:
int printf (const char *template, ...)

from cpython cimport PyFloat_AsDouble, PyInt_AsLong, PyErr_SetString, PyErr_Format
from cpython cimport PyFloat_AsDouble, PyLong_AsLong, PyErr_SetString, PyErr_Format
from cpython.list cimport PyList_GET_SIZE, PyList_GET_ITEM
from cpython.tuple cimport PyTuple_GET_SIZE, PyTuple_GET_ITEM
from cpython.slice cimport PySlice_GetIndicesEx
Expand Down Expand Up @@ -40,7 +40,7 @@ from ms_peak_picker._c.interval_t_vector cimport (

cnp.import_array()

cdef long num_processors = PyInt_AsLong(cpu_count())
cdef long num_processors = PyLong_AsLong(cpu_count())


@cython.cdivision(True)
Expand Down Expand Up @@ -266,7 +266,7 @@ cdef class GridAverager(object):

if n_workers is None:
n_workers = min(num_processors, len(spectra))
n_threads = PyInt_AsLong(n_workers)
n_threads = PyLong_AsLong(n_workers)


prepare_arrays(spectra, &spectrum_pairs)
Expand Down Expand Up @@ -314,7 +314,7 @@ cdef class GridAverager(object):
if n_workers is None:
n_threads = 1
else:
n_threads = PyInt_AsLong(n_workers)
n_threads = PyLong_AsLong(n_workers)

if start > stop:
stop, start = start, stop
Expand Down
6 changes: 3 additions & 3 deletions src/ms_peak_picker/_c/size_t_vector.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from cpython.sequence cimport (
PySequence_Fast_GET_ITEM, PySequence_Fast_GET_SIZE)
from cpython.slice cimport PySlice_GetIndicesEx

from cpython.int cimport PyInt_FromSize_t, PyInt_AsLong
from cpython.long cimport PyLong_FromSize_t, PyLong_AsLong

cdef enum VectorStateEnum:
should_free = 1
Expand Down Expand Up @@ -333,7 +333,7 @@ cdef class SizeTVector(object):
qsort(self.get_data(), self.size(), sizeof(size_t), compare_value_size_t)

cpdef object _to_python(self, size_t value):
return PyInt_FromSize_t(value)
return PyLong_FromSize_t(value)

cpdef size_t _to_c(self, object value) except *:
return PyInt_AsLong(value)
return PyLong_AsLong(value)

0 comments on commit b8be01e

Please sign in to comment.