matchms.similarity.NeutralLossesCosine module
- class matchms.similarity.NeutralLossesCosine.NeutralLossesCosine(tolerance: float = 0.1, mz_power: float = 0.0, intensity_power: float = 1.0, ignore_peaks_above_precursor: bool = True)[source]
Bases:
BaseSimilarityWithSparseCalculate ‘neutral losses cosine score’ between mass spectra.
The neutral losses cosine score aims at quantifying the similarity between two mass spectra. The score is calculated by finding best possible matches between peaks of two spectra. Two peaks are considered a potential match if their m/z ratios lie within the given ‘tolerance’ once a mass-shift is applied. The mass shift is the difference in precursor-m/z between the two spectra. In general, ModifiedCosineGreedy is recommended over NeutralLossesCosine because it will on average deliver more reliable results.
- __init__(tolerance: float = 0.1, mz_power: float = 0.0, intensity_power: float = 1.0, ignore_peaks_above_precursor: bool = True)[source]
- Parameters:
tolerance – Peaks will be considered a match when <= tolerance apart. Default is 0.1.
mz_power – The power to raise mz to in the cosine function. The default is 0, in which case the peak intensity products will not depend on the m/z ratios.
intensity_power – The power to raise intensity to in the cosine function. The default is 1.
ignore_peaks_above_precursor – By default this is set to True, meaning that peaks with m/z values larger than the precursor-m/z will be ignored (since those would correspond to negative “neutral losses”).
- keep_score(score) bool
Return whether a score should be retained in sparse outputs.
This defines the default sparse retention behavior. Users can override it per call via
score_filter=....Default behavior: - scalar score: keep if
score != 0- structured score: keep if all fields are non-zero
- matrix(spectra_1: Sequence[Spectrum], spectra_2: Sequence[Spectrum] | None = None, score_fields: Sequence[str] | None = None, progress_bar: bool = True)
Calculate a dense similarity matrix.
- Parameters:
spectra_1 – First collection of spectra.
spectra_2 – Second collection of spectra. If None, compare
spectra_1against itself. For commutative similarities this automatically uses a symmetric optimization.score_fields – Score fields to return. -
Nonemeans return all available fields. - For scalar scores, only("score",)is valid. - For structured scores, this can be a subset such as("score",).progress_bar – When True, show a progress bar. Default is True.
- Returns:
Dense score result wrapped in a
Scorescontainer.- Return type:
- pair(spectrum_1: Spectrum, spectrum_2: Spectrum) tuple[float, int][source]
Calculate neutral losses cosine score between two spectra.
- Parameters:
reference – Single reference spectrum.
query – Single query spectrum.
- Return type:
Tuple with cosine score and number of matched peaks.
- sparse_matrix(spectra_1: Sequence[Spectrum], spectra_2: Sequence[Spectrum] | None = None, idx_row: ArrayLike | None = None, idx_col: ArrayLike | None = None, score_fields: Sequence[str] | None = None, score_filter: Callable[[ndarray], bool] | None = None, progress_bar: bool = True)
Calculate sparse similarity results.
Filtering is applied to the full score before score field projection.
- Parameters:
spectra_1 – First collection of spectra.
spectra_2 – Second collection of spectra. If None, compare
spectra_1against itself.idx_row – Row indices of pairs to compute. If None and
idx_colis also None, all pairwise comparisons are considered and only retained scores are stored.idx_col – Column indices of pairs to compute. Must have the same shape as
idx_row.score_fields – Score fields to return. -
Nonemeans return all available fields. - For scalar scores, only("score",)is valid. - For structured scores, this can be a subset such as("score",).score_filter – Optional callable receiving the full score and returning whether it should be retained. If None,
keep_score()is used.progress_bar – When True, show a progress bar.
- Returns:
Sparse score result wrapped in a
Scorescontainer.- Return type: