diff --git a/src/depiction/calibration/spectrum/calibration_method_regress_shift.py b/src/depiction/calibration/spectrum/calibration_method_regress_shift.py index a809370..2ec4af0 100644 --- a/src/depiction/calibration/spectrum/calibration_method_regress_shift.py +++ b/src/depiction/calibration/spectrum/calibration_method_regress_shift.py @@ -7,7 +7,9 @@ from depiction.calibration.models.fit_model import fit_model from depiction.calibration.spectrum.reference_peak_distances import ReferencePeakDistances from depiction.image import MultiChannelImage +from depiction.image.smoothing.min_filter import MinFilter from depiction.image.smoothing.spatial_smoothing_sparse_aware import SpatialSmoothingSparseAware +from depiction.spectrum.peak_filtering import FilterNHighestIntensityPartitioned, PeakFilteringType class CalibrationMethodRegressShift(CalibrationMethod): @@ -37,6 +39,7 @@ def __init__( input_smoothing_kernel_size: int = 27, input_smoothing_kernel_std: float = 10.0, min_points: int = 3, + peak_filtering: PeakFilteringType | None = None, ) -> None: self._ref_mz_arr = ref_mz_arr self._max_distance = max_distance @@ -47,8 +50,15 @@ def __init__( self._input_smoothing_activated = input_smoothing_activated self._input_smoothing_kernel_size = input_smoothing_kernel_size self._input_smoothing_kernel_std = input_smoothing_kernel_std + self._peak_filtering = peak_filtering def extract_spectrum_features(self, peak_mz_arr: NDArray[float], peak_int_arr: NDArray[float]) -> DataArray: + if self._peak_filtering: + # TODO this is sort of problematic, to be reconsidered how we can avoid passing peak arrays as spectrum + # arrays (it should be part of the API and implemented consistently) + peak_mz_arr, peak_int_arr = self._peak_filtering.filter_peaks( + peak_mz_arr, peak_int_arr, peak_mz_arr, peak_int_arr + ) distances_mz = ReferencePeakDistances.get_distances_max_peak_in_window( peak_mz_arr=peak_mz_arr, peak_int_arr=peak_int_arr, diff --git a/src/depiction/tools/calibrate/calibrate.py b/src/depiction/tools/calibrate/calibrate.py index 5c0c018..5bbabbc 100644 --- a/src/depiction/tools/calibrate/calibrate.py +++ b/src/depiction/tools/calibrate/calibrate.py @@ -24,6 +24,7 @@ CalibrationConstantGlobalShiftConfig, CalibrationConfig, ) +from depiction.tools.filter_peaks.filter_peaks import get_peak_filter def extract_reference_masses(mass_list: Path) -> NDArray[float]: @@ -41,6 +42,7 @@ def extract_reference_masses(mass_list: Path) -> NDArray[float]: def get_calibration_instance(config: CalibrationConfig, mass_list: Path | None): match config.method: case CalibrationRegressShiftConfig(): + peak_filtering = get_peak_filter(config.method.peak_filtering) if config.method.peak_filtering else None return CalibrationMethodRegressShift( ref_mz_arr=extract_reference_masses(mass_list), max_distance=config.method.max_distance, @@ -51,6 +53,7 @@ def get_calibration_instance(config: CalibrationConfig, mass_list: Path | None): input_smoothing_kernel_size=config.method.input_smoothing_kernel_size, input_smoothing_kernel_std=config.method.input_smoothing_kernel_std, min_points=config.method.min_points, + peak_filtering=peak_filtering, ) case CalibrationConstantGlobalShiftConfig(): return CalibrationMethodGlobalConstantShift( diff --git a/src/depiction/tools/calibrate/config.py b/src/depiction/tools/calibrate/config.py index 0d8225e..516a91f 100644 --- a/src/depiction/tools/calibrate/config.py +++ b/src/depiction/tools/calibrate/config.py @@ -4,6 +4,8 @@ from pydantic import BaseModel, Field +from depiction.tools.filter_peaks.config import FilterPeaksConfig + class CalibrationRegressShiftConfig(BaseModel): calibration_method: Literal["RegressShift"] = "RegressShift" @@ -20,6 +22,8 @@ class CalibrationRegressShiftConfig(BaseModel): input_smoothing_kernel_std: float = 10.0 min_points: int = 3 + peak_filtering: FilterPeaksConfig | None = None + class CalibrationChemicalPeptideNoiseConfig(BaseModel): calibration_method: Literal["ChemicalPeptideNoise"] = "ChemicalPeptideNoise"