Repository: matchms/matchms

Issue:
Fix generating empty spectrums
**Describe the bug**

The following code generate an empty spectrum

```
s = Spectrum(np.array([1.]), np.array([1.]))
processing = SpectrumProcessor(
    [
        lambda spec: select_by_mz(spec, 20, 30)
    ]
)
processing.process_spectrums([s])

```

 It raises an error when trying to print the spectra, but I assume this can casue other problems. (I found one while trying to compute scores using this spectra.

```
ValueError: min() arg is an empty sequence
```

**To Reproduce**
Code above.

**Expected behavior**
It should not break, I guess empty spectrum is find in some cases, when processing data.

**Desktop (please complete the following information):**
 - OS: Windows
 - Version python3.11.2 ,  matchms:0.24.0





Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 98091849..b8ac3693 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ## unreleased
 
 ### Fixed
+- Fix to handle spectra with empty peak arrays. [#598](https://github.com/matchms/matchms/issues/598)
 - Fix instability introduced in CosineGreedy by `np.argsort`. [#595](https://github.com/matchms/matchms/issues/595)
 
 ### Changed
diff --git a/matchms/Spectrum.py b/matchms/Spectrum.py
index 7d2bba6e..08855d9b 100644
--- a/matchms/Spectrum.py
+++ b/matchms/Spectrum.py
@@ -105,10 +105,12 @@ class Spectrum:
         return int.from_bytes(bytearray(combined_hash, 'utf-8'), 'big')
 
     def __repr__(self):
+        precursor_mz_str = f"{self.get('precursor_mz', 0.0):.2f}"
         num_peaks = len(self.peaks)
+        if num_peaks == 0:
+            return f"Spectrum(precursor m/z={precursor_mz_str}, no fragments)"
         min_mz = min(self.peaks.mz)
         max_mz = max(self.peaks.mz)
-        precursor_mz_str = f"{self.get('precursor_mz', 0.0):.2f}"
         return f"Spectrum(precursor m/z={precursor_mz_str}, {num_peaks} fragments between {min_mz:.1f} and {max_mz:.1f})"
 
     def __str__(self):


Test patch:
diff --git a/tests/test_spectrum.py b/tests/test_spectrum.py
index b6874122..d6428758 100644
--- a/tests/test_spectrum.py
+++ b/tests/test_spectrum.py
@@ -141,6 +141,16 @@ def test_spectrum_to_dict_matchms_style(spectrum: Spectrum):
     assert spectrum_dict == expected_dict
 
 
+def test_spectrum_repr_and_str_method():
+    # Test the __repr__ and __str__ methods.
+    spectrum = Spectrum(mz=np.array([1., 2., 3.]), intensities=np.array([0, 0.5, 1.]))
+    assert repr(spectrum) == str(spectrum) == "Spectrum(precursor m/z=0.00, 3 fragments between 1.0 and 3.0)"
+
+    # Test if spectra with empty peak arrays are handled correctly:
+    spectrum = Spectrum(mz=np.array([]), intensities=np.array([]))
+    assert repr(spectrum) == str(spectrum) == "Spectrum(precursor m/z=0.00, no fragments)"
+
+
 def test_spectrum_hash(spectrum: Spectrum):
     assert hash(spectrum) == 382278160858921722, "Expected different hash."
     assert spectrum.metadata_hash() == "78c223faa157cc130390", \