matchms.similarity.BaseSimilarity module

class matchms.similarity.BaseSimilarity.BaseSimilarity[source]

Bases: object

Similarity function base class. When building a custom similarity measure, inherit from this class and implement the desired methods.


Whether similarity function is commutative, which means that the order of spectrums does not matter (similarity(A, B) == similarity(B, A)). Default is True.

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

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.

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

Method to calculate the similarity for one input pair.

  • reference – Single reference spectrum.

  • query – Single query spectrum.

  • Returns – score as numpy array (using self.score_datatype). For instance returning numpy.asarray(score, dtype=self.score_datatype)


alias of numpy.float64

sort(scores: numpy.ndarray)[source]

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