matchms.similarity.IntersectMz module

class matchms.similarity.IntersectMz.IntersectMz(scaling: float = 1.0)[source]

Bases: matchms.similarity.BaseSimilarity.BaseSimilarity

Example score for illustrating how to build custom spectra similarity score.

IntersectMz will count all exact matches of peaks and divide it by all unique peaks found in both spectrums.

Example of how matchms similarity functions can be used:

import numpy as np
from matchms import Spectrum
from matchms.similarity import IntersectMz

spectrum_1 = Spectrum(mz=np.array([100, 150, 200.]),
                      intensities=np.array([0.7, 0.2, 0.1]))
spectrum_2 = Spectrum(mz=np.array([100, 140, 190.]),
                      intensities=np.array([0.4, 0.2, 0.1]))

# Construct a similarity function
similarity_measure = IntersectMz(scaling=1.0)

score = similarity_measure.pair(spectrum_1, spectrum_2)

print(f"IntersectMz score is {score:.2f}")

Should output

IntersectMz score is 0.20
__init__(scaling: float = 1.0)[source]

Constructor. Here, function parameters are defined.


scaling – Scale scores to maximum possible score being ‘scaling’.

matrix(references: List[Spectrum], queries: List[Spectrum], is_symmetric: bool = False) numpy.ndarray

Optional: Provide optimized method to calculate an numpy.array of similarity scores for given reference and query spectrums. If no method is added here, the following naive implementation (i.e. a double for-loop) is used.

  • references – List of reference objects

  • queries – List of query objects

  • is_symmetric – Set to True when references and queries are identical (as for instance for an all-vs-all comparison). By using the fact that score[i,j] = score[j,i] the calculation will be about 2x faster.

pair(reference: Spectrum, query: Spectrum) float[source]

This will calculate the similarity score between two spectra.


alias of numpy.float64

sort(scores: numpy.ndarray)

Return array of indexes for sorted list of scores. This method can be adapted for different styles of scores.


scores – 1D Array of scores.


Indexes of sorted scores.

Return type