Repository: matchms/matchms (python)

Issue:
Fix use of FingerprintSimilarity in Pipelines
**Describe the bug**
Computation using Pipelines fails with a quite elusive bug.

**To Reproduce**
```python
from matchms.similarity import MetadataMatch, FingerprintSimilarity
from matchms.importing import load_from_msp, scores_from_json
from matchms import Scores, set_matchms_logger_level
from matchms.filtering import add_fingerprint
from matchms import Pipeline
set_matchms_logger_level("WARNING")

pipeline = Pipeline()
pipeline.query_files = filename
pipeline.reference_files = filename

filter_steps = [
    ["default_filters"],
    ["add_fingerprint"],
    ["require_minimum_number_of_peaks", {"n_required": 5}]
]
pipeline.filter_steps_queries = filter_steps
pipeline.filter_steps_refs = filter_steps

pipeline.score_computations = [
    ["metadatamatch", {"field": "precursor_mz", "matching_type": "difference", "tolerance": 50}],
    ["fingerprintsimilarity", {"similarity_measure": "jaccard"}],
    ["metadatamatch", {"field": "retention_index", "matching_type": "difference", "tolerance": 20}],
    #["cosinegreedy",  {"tolerance": 0.5}]
]    

pipeline.run()

scores = pipeline.scores
```

**Screenshots**
```
Output exceeds the [size limit](command:workbench.action.openSettings?%5B%22notebook.output.textLineLimit%22%5D). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?94bd4245-8db1-4ee8-9171-cb7272009071)
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Cell In[3], line 20
     11 pipeline.filter_steps_refs = filter_steps
     13 pipeline.score_computations = [
     14     ["metadatamatch", {"field": "precursor_mz", "matching_type": "difference", "tolerance": 50}],
     15     ["fingerprintsimilarity", {"similarity_measure": "jaccard"}],
     16     ["metadatamatch", {"field": "retention_index", "matching_type": "difference", "tolerance": 20}],
     17     #["cosinegreedy",  {"tolerance": 0.5}]
     18 ]    
---> 20 pipeline.run()
     22 scores = pipeline.scores

File [c:\Users\473355\Miniconda3\envs\matchms-pipeline\lib\site-packages\matchms\Pipeline.py:167](file:///C:/Users/473355/Miniconda3/envs/matchms-pipeline/lib/site-packages/matchms/Pipeline.py:167), in Pipeline.run(self)
    165     else:
    166         self.write_to_logfile(f"-- Score computation: {computation} --")
--> 167         self._apply_similarity_measure(computation, i)
    168 self.write_to_logfile(f"--- Pipeline run finised ({str(datetime.now())}) ---")

File [c:\Users\473355\Miniconda3\envs\matchms-pipeline\lib\site-packages\matchms\Pipeline.py:204](file:///C:/Users/473355/Miniconda3/envs/matchms-pipeline/lib/site-packages/matchms/Pipeline.py:204), in Pipeline._apply_similarity_measure(self, computation, i)
    198     self.scores = calculate_scores(self.spectrums_references,
    199                                    self.spectrums_queries,
    200                                    similarity_measure,
    201                                    array_type="sparse",
    202                                    is_symmetric=self.is_symmetric)
...
    
    jaccard_score = 0
    if u_or_v.sum() != 0:
```


Pull request:
Fixed use of FingerprintSimilarity in Pipeline
This PR includes the following:

- Fixed pipeline filter, as previously changes by filters were not propagated to the "spectrums_queries" and "spectrums_references" #390
-  Added test for the use of FingerprintSimilarity in Pipeline
- updated changelog

closes #390 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c54725a0..92ebbfea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+
+### Changed
+
+- Fixed pipeline filter [#414](https://github.com/matchms/matchms/pull/414)
+
+### Fixed
+
 ## [0.19.0] - 2023-05-10
 
 ### Added
diff --git a/matchms/Pipeline.py b/matchms/Pipeline.py
index 0fb94961..092a9a21 100644
--- a/matchms/Pipeline.py
+++ b/matchms/Pipeline.py
@@ -139,18 +139,22 @@ class Pipeline:
         self.write_to_logfile(f"Time: {str(datetime.now())}")
         for step in self.filter_steps_queries:
             self.write_to_logfile(f"-- Processing step: {step} --")
-        for spectrum in tqdm(self.spectrums_queries,
+        for i in tqdm(range(len(self.spectrums_queries)),
                              disable=(not self.progress_bar),
                              desc="Processing query spectrums"):
+            spectrum = self.spectrums_queries[i]
             for step in self.filter_steps_queries:
                 spectrum = self.apply_filter(spectrum, step)
-            self.spectrums_queries = [s for s in self.spectrums_queries if s is not None]
+            self.spectrums_queries[i] = spectrum
+        self.spectrums_queries = [s for s in self.spectrums_queries if s is not None]
         if self.is_symmetric is False:
-            for spectrum in tqdm(self.spectrums_references,
+            for i in tqdm(range(len(self.spectrums_references)),
                                  disable=(not self.progress_bar),
                                  desc="Processing reference spectrums"):
+                spectrum = self.spectrums_references[i]
                 for step in self.filter_steps_refs:
                     spectrum = self.apply_filter(spectrum, step)
+                self.spectrums_references[i] = spectrum
             self.spectrums_references = [s for s in self.spectrums_references if s is not None]
 
         # Score computation and masking


Test patch:
diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py
index 4d37e970..3136c266 100644
--- a/tests/test_pipeline.py
+++ b/tests/test_pipeline.py
@@ -174,4 +174,24 @@ def test_pipeline_logging(tmp_path):
     with open(logfile, "r", encoding="utf-8") as f:
         firstline = f.readline().rstrip()
     assert "Start running matchms pipeline" in firstline
-    
\ No newline at end of file
+    
+
+def test_FingerprintSimilarity_pipeline():
+    pytest.importorskip("rdkit")
+    pipeline = Pipeline()
+    pipeline.query_files = spectrums_file_msp
+    pipeline.reference_files = spectrums_file_msp
+    filter_steps = [
+        ["default_filters"],
+        ["add_fingerprint"]
+    ]
+    pipeline.filter_steps_queries = filter_steps
+    pipeline.filter_steps_refs = filter_steps
+    pipeline.score_computations = [
+        ["metadatamatch", {"field": "precursor_mz", "matching_type": "difference", "tolerance": 50}],
+        ["fingerprintsimilarity", {"similarity_measure": "jaccard"}]
+    ]    
+    pipeline.run()
+
+    assert pipeline.scores.scores.shape == (5, 5, 2)
+    assert pipeline.scores.score_names == ('MetadataMatch', 'FingerprintSimilarity')