Skip to content

Commit

Permalink
introduce configurable peak filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Oct 17, 2024
1 parent 5ead8e0 commit a959c4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/depiction/tools/calibrate/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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,
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/depiction/tools/calibrate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit a959c4b

Please sign in to comment.