Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
n-elie committed Aug 6, 2024
1 parent 7f8be06 commit 6f3c399
Show file tree
Hide file tree
Showing 34 changed files with 1,182 additions and 524 deletions.
11 changes: 7 additions & 4 deletions libmetgem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@

from .common import MZ, INTENSITY
from ._cython import IS_CYTHONIZED
from .filter import square_root_and_normalize_data
from .filter import square_root_data, normalize_data

import numpy as np

def human_readable_data(data: np.ndarray) -> np.ndarray:
def human_readable_data(data: np.ndarray, square_intensities=True) -> np.ndarray:
"""
Normalize an MS/MS spectrum to the spectrum's maximum intensity and
revert `filter_data` square-root process.
Args:
data: A 2D array representing an MS/MS spectrum.
square_intensities (bool): Whether to square intensities to revert
`filter_data` square-root process.
Returns:
A copy of the array with intensities squared and normalised to maximum.
A copy of the array with intensities squared (optional) and normalised to maximum.
See Also:
filter_data
"""

data = data.copy()
data[:, INTENSITY] = data[:, INTENSITY] ** 2
if square_intensities:
data[:, INTENSITY] = data[:, INTENSITY] ** 2
data[:, INTENSITY] = data[:, INTENSITY] / data[:, INTENSITY].max() * 100
return data

Expand Down
13 changes: 12 additions & 1 deletion libmetgem/_common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ ctypedef packed struct score_t:
double value
uint8_t type

ctypedef enum score_algorithm_t:
cosine
entropy
weighted_entropy

ctypedef enum norm_method_t:
dot
sum

cdef np.ndarray[np.float32_t, ndim=2] arr_from_peaks_vector(vector[peak_t] v)
cdef np.ndarray arr_from_score_vector(vector[score_t] v)
cdef np.ndarray arr_from_1d_vector(vector[numeric] v, np.dtype dtype)
cdef void *np_arr_pointer(np.ndarray[numeric, ndim=2] data)
cdef void *np_arr_pointer(np.ndarray[numeric, ndim=2] data)
cdef score_algorithm_t str_to_score_algorithm(str)
cdef norm_method_t str_to_norm_method(str)
11 changes: 11 additions & 0 deletions libmetgem/_common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ cdef void *np_arr_pointer(np.ndarray[numeric, ndim=2] data):
if not data.flags['C_CONTIGUOUS']:
data = np.ascontiguousarray(data, dtype=data.dtype)
return <void *>&data[0, 0]

cdef score_algorithm_t str_to_score_algorithm(str score):
if score == 'weighted_entropy':
return score_algorithm_t.weighted_entropy
elif score == 'entropy':
return score_algorithm_t.entropy
else:
return score_algorithm_t.cosine

cdef norm_method_t str_to_norm_method(str norm):
return norm_method_t.sum if norm == 'sum' else norm_method_t.dot
9 changes: 5 additions & 4 deletions libmetgem/_cosine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

cimport numpy as np

from ._common cimport peak_t
from ._common cimport peak_t, score_algorithm_t

cpdef enum SpectraMatchState:
fragment = 0
neutral_loss = 1

cdef double cosine_score_nogil(double, peak_t*, np.npy_intp,
double, peak_t*, np.npy_intp,
double, int) noexcept nogil
cdef double generic_score_nogil(
double, const peak_t*, np.npy_intp,
double, const peak_t*, np.npy_intp,
double, int, score_algorithm_t score_algorithm=*) noexcept nogil
Loading

0 comments on commit 6f3c399

Please sign in to comment.