Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
hechth committed Aug 14, 2024
1 parent 6a22ce2 commit 0194a81
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion spec2vec/Spec2Vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from gensim.models import Word2Vec
from matchms import Spectrum
from matchms.similarity.BaseSimilarity import BaseSimilarity
from sparsestack import StackedSparseArray
from sparsestack import StackedSparseArray
from tqdm import tqdm
from spec2vec.serialization import Word2VecLight
from spec2vec.SpectrumDocument import SpectrumDocument
Expand Down
3 changes: 1 addition & 2 deletions spec2vec/SpectrumDocument.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional
from matchms import Spectrum
from matchms.Spikes import Spikes
from .Document import Document
from matchms import Spectrum


class SpectrumDocument(Document):
Expand Down
1 change: 1 addition & 0 deletions spec2vec/SpectrumDocumentWithLosses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .SpectrumDocument import SpectrumDocument


class SpectrumDocumentWithLosses(SpectrumDocument):
def __init__(self, spectrum, n_decimals: int = 2, loss_mz_from: int = 10, loss_mz_to: int = 200):
self._loss_mz_from = loss_mz_from
Expand Down
2 changes: 1 addition & 1 deletion spec2vec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"Document",
"serialization",
"SpectrumDocument",
"SpectrumDocumentWithLosses,"
"SpectrumDocumentWithLosses",
"Spec2Vec",
]
39 changes: 15 additions & 24 deletions tests/test_spectrum_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
from spec2vec import SpectrumDocument


def test_spectrum_document_init_n_decimals_default_value_no_losses():

@pytest.fixture
def spectrum():
mz = np.array([10, 20, 30, 40], dtype="float")
intensities = np.array([0, 0.01, 0.1, 1], dtype="float")
metadata = dict(precursor_mz=100.0)
metadata = {"precursor_mz": 100.0, "smiles": "testsmiles"}
spectrum = Spectrum(mz=mz, intensities=intensities, metadata=metadata)
return spectrum


def test_spectrum_document_init_n_decimals_default_value_no_losses(spectrum):
spectrum_document = SpectrumDocument(spectrum)

assert spectrum_document.n_decimals == 2, "Expected different default for n_decimals"
Expand All @@ -20,11 +24,7 @@ def test_spectrum_document_init_n_decimals_default_value_no_losses():
assert next(spectrum_document) == "[email protected]"


def test_spectrum_document_init_n_decimals_1_no_losses():
mz = np.array([10, 20, 30, 40], dtype="float")
intensities = np.array([0, 0.01, 0.1, 1], dtype="float")
metadata = dict(precursor_mz=100.0)
spectrum = Spectrum(mz=mz, intensities=intensities, metadata=metadata)
def test_spectrum_document_init_n_decimals_1_no_losses(spectrum):
spectrum_document = SpectrumDocument(spectrum, n_decimals=1)

assert spectrum_document.n_decimals == 1
Expand All @@ -35,18 +35,13 @@ def test_spectrum_document_init_n_decimals_1_no_losses():
assert next(spectrum_document) == "[email protected]"


def test_spectrum_document_metadata_getter():
def test_spectrum_document_metadata_getter(spectrum):
"""Test metadata getter"""
mz = np.array([10, 20, 30, 40], dtype="float")
intensities = np.array([0, 0.01, 0.1, 1], dtype="float")
metadata = {"precursor_mz": 100.0,
"smiles": "testsmiles"}
spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata)
spectrum_document = SpectrumDocument(spectrum_in, n_decimals=2)
spectrum_document = SpectrumDocument(spectrum, n_decimals=2)

assert spectrum_document.n_decimals == 2
assert len(spectrum_document) == 4
assert spectrum_document.metadata == metadata, "Expected different metadata"
assert spectrum_document.metadata == spectrum.metadata, "Expected different metadata"
assert spectrum_document.get("smiles") == "testsmiles", "Expected different metadata"
assert spectrum_document.words == [
"[email protected]", "[email protected]", "[email protected]", "[email protected]"
Expand All @@ -69,16 +64,12 @@ def test_spectrum_document_metadata_getter_notallowed_key():
assert str(msg.value) == "Key cannot be attribute of SpectrumDocument class"


def test_spectrum_document_peak_getter():
def test_spectrum_document_peak_getter(spectrum):
"""Test peak getter"""
mz = np.array([10, 20, 30, 40], dtype="float")
intensities = np.array([0, 0.01, 0.1, 1], dtype="float")
metadata = {"precursor_mz": 100.0}
spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata)
spectrum_document = SpectrumDocument(spectrum_in, n_decimals=2)
spectrum_document = SpectrumDocument(spectrum, n_decimals=2)

assert spectrum_document.words == [
"[email protected]", "[email protected]", "[email protected]", "[email protected]"
]
assert np.all(spectrum_document.peaks.mz == mz), "Expected different peak m/z"
assert np.all(spectrum_document.peaks.intensities == intensities), "Expected different peaks"
assert np.all(spectrum_document.peaks.mz == spectrum.mz), "Expected different peak m/z"
assert np.all(spectrum_document.peaks.intensities == spectrum.intensities), "Expected different peaks"
6 changes: 3 additions & 3 deletions tests/test_spectrum_document_with_losses.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from matchms import Spectrum
import pytest
import numpy as np
import pytest
from matchms import Spectrum
from spec2vec import SpectrumDocumentWithLosses


@pytest.fixture
def spectrum() -> Spectrum:
mz = np.array([10, 20, 30, 40], dtype="float")
intensities = np.array([0, 0.01, 0.1, 1], dtype="float")
metadata = dict(precursor_mz=100.0)
metadata = {"precursor_mz": 100.0}
return Spectrum(mz=mz, intensities=intensities, metadata=metadata)

def test_spectrum_document_init_default_with_losses(spectrum: Spectrum):
Expand Down

0 comments on commit 0194a81

Please sign in to comment.