Skip to content

Commit ee46d71

Browse files
committed
Add doctests
1 parent 4d6e8d0 commit ee46d71

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ addopts = "--profile=black"
7777
pythonpath = [
7878
"."
7979
]
80+
addopts = "--doctest-modules"
8081
testpaths = [
8182
"tests",
8283
"integration-tests",

simms/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import warnings
32
from contextlib import contextmanager
4-
from matchms.filtering.SpectrumProcessor import SpectrumProcessor
53
from .__version__ import __version__
64

75

simms/similarity/CudaCosineGreedy.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class CudaCosineGreedy(BaseSimilarity):
2121
2222
This implementation is meant to replicate outputs of `matchms.similarity.CosineGreedy`.
2323
24+
For example:
25+
2426
>>> import numpy as np
2527
>>> from matchms import Spectrum
2628
>>> from simms.similarity import CudaCosineGreedy
2729
>>> reference = Spectrum(mz=np.array([100, 150, 200.]), intensities=np.array([0.7, 0.2, 0.1]))
2830
>>> query = Spectrum(mz=np.array([100, 140, 190.]), intensities=np.array([0.4, 0.2, 0.1]))
29-
>>> cosine_greedy = CudaCosineGreedy(tolerance=0.2) # Use factory to construct a similarity function
31+
>>> cosine_greedy = CudaCosineGreedy(tolerance=0.2)
3032
>>> score = cosine_greedy.pair(reference, query)
3133
>>> print(f"Cosine score is {score['score']:.2f} with {score['matches']} matched peaks")
3234
Cosine score is 0.83 with 1 matched peaks
@@ -179,7 +181,7 @@ def _get_batches(self, references, queries):
179181

180182
def pair(self, reference: Spectrum, query: Spectrum) -> float:
181183
"""
182-
Do not use - it is very inefficient, and used for testing purposes only.
184+
Do not use, unless testing. GPUs work best with a lot of data at the same time.
183185
Calculates the cosine similarity score between a reference and a query spectrum.
184186
185187
Parameters:

simms/similarity/CudaModifiedCosine.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ class CudaModifiedCosine(BaseSimilarity):
5050
for further details.
5151
5252
This implementation is meant to replicate outputs of `matchms.similarity.ModifiedCosine`.
53+
54+
For example:
55+
>>> import numpy as np
56+
>>> from matchms import Spectrum
57+
>>> from simms.similarity import CudaModifiedCosine
58+
>>> spectrum_1 = Spectrum(mz=np.array([100, 150, 200.]),
59+
... intensities=np.array([0.7, 0.2, 0.1]),
60+
... metadata={"precursor_mz": 100.0})
61+
>>> spectrum_2 = Spectrum(mz=np.array([104.9, 140, 190.]),
62+
... intensities=np.array([0.4, 0.2, 0.1]),
63+
... metadata={"precursor_mz": 105.0})
64+
>>> modified_cosine = CudaModifiedCosine(tolerance=0.2)
65+
>>> score = modified_cosine.pair(spectrum_1, spectrum_2)
66+
>>> print(f"Modified cosine score is {score['score']:.2f} with {score['matches']} matched peaks")
67+
Modified cosine score is 0.83 with 1 matched peaks
5368
"""
5469

5570
score_datatype = [

simms/similarity/spectrum_similarity_functions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ def cosine_kernel(
8989
--------
9090
callable
9191
CUDA kernel function for calculating cosine similarity scores.
92+
93+
For example:
94+
95+
>>> from simms.similarity.spectrum_similarity_functions import cosine_kernel
96+
>>> from numba import cuda
97+
>>> import numpy as np
98+
>>> batch_size, n_max_peaks = 2, 4
99+
>>> kernel = cosine_kernel(tolerance=0.1, n_max_peaks=n_max_peaks, batch_size=batch_size)
100+
>>> rspec = cuda.to_device(np.array([
101+
... [ [1., 10., 100., 200.,], # mz values
102+
... [2., 20., 200., 400.,], ],
103+
... [ [1., 1., 1., 1.,], # Intensities
104+
... [1., 1., 1., 1.,], ]
105+
... ], dtype=np.float32))
106+
>>> qspec = rspec # symmetric
107+
>>> metadata = cuda.to_device(np.array([
108+
... [ 4., 4., ], # lengths of reference spectra
109+
... [ 4., 4., ], # lengths of query spectra
110+
... [ 2., 2., ], # norms of reference spectra
111+
... [ 2., 2., ], # norms of query spectra
112+
... ], dtype=np.float32))
113+
>>> out = cuda.to_device(np.zeros((3, batch_size, batch_size), dtype=np.float32))
114+
>>> kernel(rspec, qspec, metadata, out)
115+
>>> scores, matches, overflows = out.copy_to_host()
116+
>>> print(scores)
117+
[[1. 0.25]
118+
[0.25 1. ]]
92119
"""
93120

94121
if is_symmetric:

simms/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def download(
122122
Downloads a set of sample spectra files from https://github.com/PangeAI/simms/releases/tag/samples-0.1
123123
Downloaded files are cached, and not re-downloaded after the initial call.
124124
"""
125-
import pooch
125+
import pooch # Import here, since we want fewer runtime dependencies - this is for development only
126126

127127
potential_local_file = Path(f"data/{name}")
128128
if potential_local_file.exists():

0 commit comments

Comments
 (0)