Repository: matchms/matchms
Pull request #130 (2020-08-18 15:55:42)

Additional filters
Added tests and updated filters.

fdiblen: To close this issue:
- [ ] We should remove if statements (line 76) in all the tests and update setup.cfg file with `--no-cov` options.
- [ ] Make sure that sonarcloud handles the coverage 

Patch:
diff --git a/matchms/filtering/require_minimum_of_high_peaks.py b/matchms/filtering/require_minimum_of_high_peaks.py
index 06a0bd5d..d8c93dad 100644
--- a/matchms/filtering/require_minimum_of_high_peaks.py
+++ b/matchms/filtering/require_minimum_of_high_peaks.py
@@ -1,12 +1,12 @@
 from ..typing import SpectrumType
-from . import select_by_relative_intensity
+from .select_by_relative_intensity import select_by_relative_intensity
 
 
 def require_minimum_of_high_peaks(spectrum: SpectrumType, no_peaks: int = 5,
-                                  intensity_percent: float = 2) -> SpectrumType:
+                                  intensity_percent: float = 2.0) -> SpectrumType:
 
     """Returns None if the number of peaks with relative intensity
-       above intensity_percent is less than no_peaks.
+       above or equal to intensity_percent is less than no_peaks.
 
     Args:
     -----
@@ -22,7 +22,7 @@ def require_minimum_of_high_peaks(spectrum: SpectrumType, no_peaks: int = 5,
     """
 
     assert no_peaks >= 1, "no_peaks must be a positive nonzero integer."
-    assert intensity_percent >= 100, "intensity_percent must be a floating point between 0-100."
+    assert intensity_percent <= 100 and intensity_percent >= 0, "intensity_percent must be a floating point between 0-100."
     intensities_above_p = select_by_relative_intensity(spectrum, intensity_from=intensity_percent/100, intensity_to=1.0)
     if len(intensities_above_p.peaks) < no_peaks:
         return None
diff --git a/matchms/filtering/require_precursor_below_mz.py b/matchms/filtering/require_precursor_below_mz.py
index 3cb0e4d8..cdc58594 100644
--- a/matchms/filtering/require_precursor_below_mz.py
+++ b/matchms/filtering/require_precursor_below_mz.py
@@ -18,7 +18,9 @@ def require_precursor_below_mz(spectrum: SpectrumType, max_mz: float = 1000) ->
 
     assert max_mz >= 0, "max_mz must be a positive floating point."
     precursor_mz = spectrum.get("precursor_mz")
-    if precursor_mz >= max_mz:
-        return None
+    if precursor_mz:
+
+        if precursor_mz >= max_mz:
+            return None
 
     return spectrum


Test patch:
diff --git a/tests/test_remove_peaks_around_precursor_mz.py b/tests/test_remove_peaks_around_precursor_mz.py
new file mode 100644
index 00000000..57c21847
--- /dev/null
+++ b/tests/test_remove_peaks_around_precursor_mz.py
@@ -0,0 +1,39 @@
+import numpy
+from matchms import Spectrum
+from matchms.filtering import remove_peaks_around_precursor_mz
+
+
+def test_remove_peaks_around_precursor_mz_no_params_or_precursor():
+    """No parameters provided and no precursor mz present."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = remove_peaks_around_precursor_mz(spectrum_in)
+
+    assert spectrum == spectrum_in, "Expected no changes."
+
+
+def test_remove_peaks_around_precursor_mz_no_params():
+    """No parameters provided but precursor mz present."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+    spectrum_in.set("precursor_mz", 60.)
+
+    spectrum = remove_peaks_around_precursor_mz(spectrum_in)
+
+    assert spectrum == spectrum_in, "Expected no changes."
+
+
+def test_remove_peaks_around_precursor_mz_tolerance_20():
+    """Set mz_tolerance to 20."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+    spectrum_in.set("precursor_mz", 60.)
+
+    spectrum = remove_peaks_around_precursor_mz(spectrum_in, mz_tolerance = 20)
+
+    assert len(spectrum.peaks) == 3, "Expected 3 peaks to remain."
+    assert spectrum.peaks.mz.tolist() == [10., 20., 30.], "Expected different peaks to remain."
diff --git a/tests/test_require_minimum_of_high_peaks.py b/tests/test_require_minimum_of_high_peaks.py
new file mode 100644
index 00000000..c470ad43
--- /dev/null
+++ b/tests/test_require_minimum_of_high_peaks.py
@@ -0,0 +1,36 @@
+import numpy
+from matchms import Spectrum
+from matchms.filtering import require_minimum_of_high_peaks
+
+
+def test_require_minimum_of_high_peaks_no_params():
+    """No parameters provided."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = require_minimum_of_high_peaks(spectrum_in)
+
+    assert spectrum is None, "Expected None as the number of peaks (4) is less than the default of 5 for no_peaks."
+
+
+def test_require_minimum_of_high_peaks_no_peaks_2():
+    """Set no_peaks to 2."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = require_minimum_of_high_peaks(spectrum_in, no_peaks = 2)
+
+    assert spectrum == spectrum_in, "Expected no changes."
+
+
+def test_require_minimum_of_high_peaks_intensity_percent_10():
+    """Set intensity_percent to 10."""
+    mz = numpy.array([10, 20, 30, 40, 50, 60, 70], dtype="float")
+    intensities = numpy.array([0, 1, 10, 25, 50, 75, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = require_minimum_of_high_peaks(spectrum_in, intensity_percent = 10)
+
+    assert spectrum == spectrum_in, "Expected no changes."
diff --git a/tests/test_require_precursor_below_mz.py b/tests/test_require_precursor_below_mz.py
new file mode 100644
index 00000000..0c7123e6
--- /dev/null
+++ b/tests/test_require_precursor_below_mz.py
@@ -0,0 +1,38 @@
+import numpy
+from matchms import Spectrum
+from matchms.filtering import require_precursor_below_mz
+
+
+def test_require_precursor_below_mz_no_params_or_precursor():
+    """No parameters provided and no precursor mz present."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = require_precursor_below_mz(spectrum_in)
+
+    assert spectrum == spectrum_in, "Expected no changes."
+
+
+def test_require_precursor_below_mz_no_params():
+    """No parameters provided but precursor mz present."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+    spectrum_in.set("precursor_mz", 60.)
+
+    spectrum = require_precursor_below_mz(spectrum_in)
+
+    assert spectrum == spectrum_in, "Expected no changes."
+
+
+def test_require_precursor_below_mz_max_50():
+    """Set max_mz to 50."""
+    mz = numpy.array([10, 20, 30, 40], dtype="float")
+    intensities = numpy.array([0, 1, 10, 100], dtype="float")
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+    spectrum_in.set("precursor_mz", 60.)
+
+    spectrum = require_precursor_below_mz(spectrum_in, max_mz = 50)
+
+    assert spectrum is None, "Expected spectrum to be None."