From 839a05d71b07528e80f63724ae2f6b8afa7a0fd0 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Tue, 11 Jun 2024 15:00:25 +0200 Subject: [PATCH] handle case when no peaks are found --- .../peak_picking/basic_interpolated_peak_picker.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/depiction/spectrum/peak_picking/basic_interpolated_peak_picker.py b/src/depiction/spectrum/peak_picking/basic_interpolated_peak_picker.py index f904325..8d51c87 100644 --- a/src/depiction/spectrum/peak_picking/basic_interpolated_peak_picker.py +++ b/src/depiction/spectrum/peak_picking/basic_interpolated_peak_picker.py @@ -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 @@ -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) @@ -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=}" ) @@ -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.