nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: matchms/matchms
Pull request #280 (2021-12-16 12:58:38)

minor fixes for name handling


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0fccfc94..3acafab6 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]
 
+### Changed
+
+- minor improvement of compound name cleaning in `derive_adduct_from_name()` filter [#280](https://github.com/matchms/matchms/pull/280)
+
 ## [0.11.0] - 2021-12-16
 
 ## Added
diff --git a/matchms/filtering/derive_adduct_from_name.py b/matchms/filtering/derive_adduct_from_name.py
index 0dd05855..2c7f3e67 100644
--- a/matchms/filtering/derive_adduct_from_name.py
+++ b/matchms/filtering/derive_adduct_from_name.py
@@ -42,6 +42,7 @@ def derive_adduct_from_name(spectrum_in: SpectrumType,
 
     if adduct_from_name and remove_adduct_from_name:
         name_adduct_removed = " ".join([x for x in name_split if x != adduct_from_name])
+        name_adduct_removed = name_adduct_removed.strip("; ")
         spectrum.set("compound_name", name_adduct_removed)
         logger.info("Removed adduct %s from compound name.", adduct_from_name)
 


Test patch:
diff --git a/tests/test_derive_adduct_from_name.py b/tests/test_derive_adduct_from_name.py
index 311c31ff..cd76cfff 100644
--- a/tests/test_derive_adduct_from_name.py
+++ b/tests/test_derive_adduct_from_name.py
@@ -1,4 +1,5 @@
 import numpy
+import pytest
 from testfixtures import LogCapture
 from matchms import Spectrum
 from matchms.filtering import derive_adduct_from_name
@@ -6,7 +7,20 @@ from matchms.logging_functions import reset_matchms_logger
 from matchms.logging_functions import set_matchms_logger_level
 
 
-def test_derive_adduct_from_name():
+@pytest.mark.parametrize("input_name, expected_adduct, expected_name", [
+    ("peptideXYZ [M+H+K]", "[M+H+K]", "peptideXYZ"),
+    ("GalCer(d18:2/16:1); [M+H]+", "[M+H]+", "GalCer(d18:2/16:1)")])
+def test_derive_adduct_from_name(input_name, expected_adduct, expected_name):
+    spectrum_in = Spectrum(mz=numpy.array([], dtype="float"),
+                           intensities=numpy.array([], dtype="float"),
+                           metadata={"compound_name": input_name})
+    spectrum = derive_adduct_from_name(spectrum_in)
+
+    assert spectrum.get("adduct") == expected_adduct, "Expected different adduct."
+    assert spectrum.get("compound_name") == expected_name, "Expected different cleaned name."
+
+
+def test_derive_adduct_from_name_logging():
     set_matchms_logger_level("INFO")
     spectrum_in = Spectrum(mz=numpy.array([], dtype="float"),
                            intensities=numpy.array([], dtype="float"),
Repository: matchms/matchms
Pull request #289 (2022-01-04 08:45:12)

Check if the max_intensitity is larger than 0
Solves issue #288 


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7bf88df6..babcc2ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 - minor improvement of compound name cleaning in `derive_adduct_from_name()` filter [#280](https://github.com/matchms/matchms/pull/280)
 
+### Fixed
+
+- able to handle spectra containg empty/zero intensities [#289](https://github.com/matchms/matchms/pull/289)
+
 ## [0.11.0] - 2021-12-16
 
 ## Added
diff --git a/matchms/filtering/normalize_intensities.py b/matchms/filtering/normalize_intensities.py
index 8a38fcda..3aa86988 100644
--- a/matchms/filtering/normalize_intensities.py
+++ b/matchms/filtering/normalize_intensities.py
@@ -1,8 +1,12 @@
+import logging
 import numpy
 from matchms.typing import SpectrumType
 from ..Spikes import Spikes
 
 
+logger = logging.getLogger("matchms")
+
+
 def normalize_intensities(spectrum_in: SpectrumType) -> SpectrumType:
     """Normalize intensities of peaks (and losses) to unit height."""
 
@@ -16,6 +20,10 @@ def normalize_intensities(spectrum_in: SpectrumType) -> SpectrumType:
 
     max_intensity = numpy.max(spectrum.peaks.intensities)
 
+    if max_intensity <= 0:
+        logger.warning("Spectrum with all peak intensities <= 0 was set to None.")
+        return None
+
     # Normalize peak intensities
     mz, intensities = spectrum.peaks
     normalized_intensities = intensities / max_intensity


Test patch:
diff --git a/tests/test_normalize_intensities.py b/tests/test_normalize_intensities.py
index a2d828a0..e3ae65dc 100644
--- a/tests/test_normalize_intensities.py
+++ b/tests/test_normalize_intensities.py
@@ -45,6 +45,19 @@ def test_normalize_intensities_empty_peaks():
     assert spectrum == spectrum_in, "Spectrum should remain unchanged."
 
 
+def test_normalize_intensities_all_zeros(caplog):
+    """Test if non-sense intensities are handled correctly."""
+    mz = numpy.array([10, 20, 30], dtype='float')
+    intensities = numpy.array([0, 0, 0], dtype='float')
+    spectrum_in = Spectrum(mz=mz, intensities=intensities)
+
+    spectrum = normalize_intensities(spectrum_in)
+
+    assert spectrum is None, "Expected spectrum to be set to None."
+    msg = "Spectrum with all peak intensities <= 0 was set to None."
+    assert msg in caplog.text, "Expected log message."
+
+
 def test_normalize_intensities_empty_spectrum():
     """Test running filter with spectrum == None."""
     spectrum = normalize_intensities(None)