Skip to content

Commit

Permalink
handle case when no peaks are found
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 11, 2024
1 parent 329ae1f commit 839a05d
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING

import loguru
import numpy as np
import scipy
from loguru import logger

from depiction.spectrum.peak_picking.basic_peak_picker import BasicPeakPicker

Expand Down Expand Up @@ -36,6 +36,9 @@ def pick_peaks(self, mz_arr: NDArray[float], int_arr: NDArray[float]) -> tuple[N
for local_max_index in local_maxima_indices
]
peaks_interp = [peak for peak in peaks_interp if peak[0] is not None and peak[1] is not None]
if not peaks_interp:
logger.warning("No peaks were found.")
return np.array([]), np.array([])
peak_mz, peak_int = zip(*peaks_interp)
peak_mz = np.asarray(peak_mz)
peak_int = np.asarray(peak_int)
Expand Down Expand Up @@ -68,7 +71,7 @@ def _interpolate_max_mz_and_intensity(
spline = scipy.interpolate.CubicSpline(interp_mz, interp_int)
roots = spline.derivative().roots()
if np.isnan(roots).any():
loguru.logger.warning(
logger.warning(
f"Error: {len(roots)} roots found for local maximum at index {local_max_index}; "
f"{interp_mz=}, {interp_int=}, {roots=}"
)
Expand All @@ -90,6 +93,5 @@ def _find_local_maxima_indices(self, mz_arr: NDArray[float], int_arr: NDArray[fl
)
return local_maxima_indices


# TODO the interpolation could be much faster, if it were implemented in numba for our specific case of 3 points,
# since in general the scipy library will do everything much moe general than is actually required.

0 comments on commit 839a05d

Please sign in to comment.