def test_select_by_intensity(peaks, intensity_from, intensity_to, expected): spectrum_in = SpectrumBuilder().with_mz(peaks[0]).with_intensities( peaks[1]).build() spectrum = select_by_intensity(spectrum_in, intensity_from=intensity_from, intensity_to=intensity_to) assert spectrum.peaks.mz.size == len(expected[0]) assert spectrum.peaks.mz.size == spectrum.peaks.intensities.size assert numpy.array_equal(spectrum.peaks.mz, expected[0]) assert numpy.array_equal(spectrum.peaks.intensities, expected[1])
def test_select_by_intensity_with_to_parameter(): mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([1, 10, 100, 1000], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum = select_by_intensity(spectrum_in, intensity_to=35.0) assert spectrum.peaks.mz.size == 1 assert spectrum.peaks.mz.size == spectrum.peaks.intensities.size assert numpy.array_equal(spectrum.peaks.mz, numpy.array([20], dtype="float")) assert numpy.array_equal(spectrum.peaks.intensities, numpy.array([10], dtype="float"))
def test_select_by_intensity_no_parameters_2(): mz = numpy.array([998, 999, 1000, 1001, 1002], dtype="float") intensities = numpy.array([198, 199, 200, 201, 202], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum = select_by_intensity(spectrum_in) assert spectrum.peaks.mz.size == 3 assert spectrum.peaks.mz.size == spectrum.peaks.intensities.size assert numpy.array_equal(spectrum.peaks.mz, numpy.array([998, 999, 1000], dtype="float")) assert numpy.array_equal(spectrum.peaks.intensities, numpy.array([198, 199, 200], dtype="float"))
def spectrum_processing_minimal( spectrum: SpectrumType, **settings: Union[int, float]) -> Union[SpectrumType, None]: """Minimal necessary spectrum processing that is required by MS2Query. This mostly includes intensity normalization and setting spectra to None when they do not meet the minimum requirements. Args: ---------- spectrum: Spectrum to process mz_from Set lower threshold for m/z peak positions. Default is 10.0. n_required_below_mz Number of minimal required peaks with m/z below 1000.0Da for a spectrum to be considered. Spectra not meeting this criteria will be set to None. intensity_from Set lower threshold for peak intensity. Default is 0.001. max_mz_required Only peaks <= max_mz_required will be counted to check if spectrum contains sufficient peaks to be considered. """ settings = set_minimal_processing_defaults(**settings) spectrum = normalize_intensities(spectrum) spectrum = select_by_intensity(spectrum, intensity_from=settings["intensity_from"]) spectrum = select_by_mz(spectrum, mz_from=settings["mz_from"], mz_to=np.inf) spectrum = require_peaks_below_mz( spectrum, n_required=settings["n_required_below_mz"], max_mz=settings["max_mz_required"]) spectrum = require_precursor_mz(spectrum) return spectrum