Repository: matchms/matchms

Issue:
Latest release did not end up on zenodo or RSD
The latest release found on zenodo is 0.4.0 (0.5.0 hence wasn't added).
In the RSD I also only found an older version (0.3.1). 

I guess that usually should be done automatically when a release is done, so not sure what went wrong here.

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 07a6a014..bd59a321 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Very minor bugs in `add_parent_mass` [#188](https://github.com/matchms/matchms/pull/188)
+
 ## [0.8.1] - 2021-02-19
 
 ### Fixed
diff --git a/matchms/filtering/add_parent_mass.py b/matchms/filtering/add_parent_mass.py
index d7b48dfa..e69a6d09 100644
--- a/matchms/filtering/add_parent_mass.py
+++ b/matchms/filtering/add_parent_mass.py
@@ -36,11 +36,11 @@ def add_parent_mass(spectrum_in: SpectrumType, estimate_from_adduct: bool = True
         try:
             precursor_mz = spectrum.get("precursor_mz", None)
             if precursor_mz is None:
-                precursor_mz = spectrum.get("pepmass")[0]
-        except KeyError:
+                precursor_mz = spectrum.get("pepmass", None)[0]
+        except TypeError:
             print("Not sufficient spectrum metadata to derive parent mass.")
+            return spectrum
 
-        spectrum = spectrum_in.clone()
         if estimate_from_adduct and adduct in adducts_dict:
             multiplier = adducts_dict[adduct]["mass_multiplier"]
             correction_mass = adducts_dict[adduct]["correction_mass"]
@@ -51,9 +51,9 @@ def add_parent_mass(spectrum_in: SpectrumType, estimate_from_adduct: bool = True
             protons_mass = PROTON_MASS * charge
             precursor_mass = precursor_mz * abs(charge)
             parent_mass = precursor_mass - protons_mass
-        else:
-            print("Not sufficient spectrum metadata to derive parent mass.")
 
-        if parent_mass is not None:
-            spectrum.set("parent_mass", parent_mass)
+        if parent_mass is None:
+            print("Not sufficient spectrum metadata to derive parent mass.")
+        else:
+            spectrum.set("parent_mass", float(parent_mass))
     return spectrum


Test patch:
diff --git a/tests/test_add_parent_mass.py b/tests/test_add_parent_mass.py
index 46d2b1cf..3ebccd5e 100644
--- a/tests/test_add_parent_mass.py
+++ b/tests/test_add_parent_mass.py
@@ -4,7 +4,7 @@ from matchms import Spectrum
 from matchms.filtering import add_parent_mass
 
 
-def test_add_parent_mass():
+def test_add_parent_mass(capsys):
     """Test if parent mass is correctly derived."""
     mz = numpy.array([], dtype='float')
     intensities = numpy.array([], dtype='float')
@@ -17,9 +17,26 @@ def test_add_parent_mass():
     spectrum = add_parent_mass(spectrum_in)
 
     assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0."
+    assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float."
+    assert "Not sufficient spectrum metadata to derive parent mass." not in capsys.readouterr().out
 
 
-def test_add_parent_mass_no_pepmass():
+def test_add_parent_mass_no_pepmass(capsys):
+    """Test if correct expection is returned."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {"charge": -1}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_parent_mass(spectrum_in)
+
+    assert spectrum.get("parent_mass") is None, "Expected no parent mass"
+    assert "Not sufficient spectrum metadata to derive parent mass." in capsys.readouterr().out
+
+
+def test_add_parent_mass_no_pepmass_but_precursormz(capsys):
     """Test if parent mass is correctly derived if "pepmass" is not present."""
     mz = numpy.array([], dtype='float')
     intensities = numpy.array([], dtype='float')
@@ -32,6 +49,8 @@ def test_add_parent_mass_no_pepmass():
     spectrum = add_parent_mass(spectrum_in)
 
     assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0."
+    assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float."
+    assert "Not sufficient spectrum metadata to derive parent mass." not in capsys.readouterr().out
 
 
 @pytest.mark.parametrize("adduct, expected", [("[M+2Na-H]+", 399.02884),
@@ -51,6 +70,22 @@ def test_add_parent_mass_using_adduct(adduct, expected):
     spectrum = add_parent_mass(spectrum_in)
 
     assert numpy.allclose(spectrum.get("parent_mass"), expected, atol=1e-4), f"Expected parent mass of about {expected}."
+    assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float."
+
+
+def test_add_parent_mass_not_sufficient_data(capsys):
+    """Test when there is not enough information to derive parent_mass."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {"precursor_mz": 444.0}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_parent_mass(spectrum_in)
+
+    assert spectrum.get("parent_mass") is None, "Expected no parent mass"
+    assert "Not sufficient spectrum metadata to derive parent mass." in capsys.readouterr().out
 
 
 def test_empty_spectrum():
diff --git a/tests/test_modified_cosine.py b/tests/test_modified_cosine.py
index 5656e600..e5b0adcd 100644
--- a/tests/test_modified_cosine.py
+++ b/tests/test_modified_cosine.py
@@ -62,6 +62,28 @@ def test_modified_cosine_with_mass_shift_5_tolerance_2():
     assert score["matches"] == 6, "Expected 6 matching peaks."
 
 
+def test_modified_cosine_with_mass_shifted_and_unshifted_matches():
+    """Test modified cosine on two spectra with mass set shift.
+    In this example 5 peak pairs are possible, but only 3 should be selected (every peak
+    can only be counted once!)"""
+    spectrum_1 = Spectrum(mz=numpy.array([100, 110, 200, 300, 400, 500, 600], dtype="float"),
+                          intensities=numpy.array([100, 50, 1, 80, 1, 1, 50], dtype="float"),
+                          metadata={"precursor_mz": 1000.0})
+
+    spectrum_2 = Spectrum(mz=numpy.array([110, 200, 300, 310, 700, 800], dtype="float"),
+                          intensities=numpy.array([100, 1, 90, 90, 1, 100], dtype="float"),
+                          metadata={"precursor_mz": 1010.0})
+
+    modified_cosine = ModifiedCosine()
+    score = modified_cosine.pair(spectrum_1, spectrum_2)
+    spec1 = spectrum_1.peaks.intensities
+    spec2 = spectrum_2.peaks.intensities
+    peak_pairs_multiplied = spec1[0] * spec2[0] + spec1[3] * spec2[3] + spec1[2] * spec2[1]
+    expected_score = peak_pairs_multiplied / numpy.sqrt(numpy.sum(spec1 ** 2) * numpy.sum(spec2 ** 2))
+    assert score["score"] == pytest.approx(expected_score, 0.00001), "Expected different cosine score."
+    assert score["matches"] == 3, "Expected 3 matching peaks."
+
+
 def test_modified_cosine_order_of_input_spectrums():
     """Test modified cosine on two spectra in changing order."""
     spectrum_1 = Spectrum(mz=numpy.array([100, 150, 200, 300, 500, 510, 1100], dtype="float"),