Repository: matchms/matchms
Pull request #530 (2023-09-08 10:07:57)

Add uneffective filters to report
Previously the ProcessingReport did not show any filter steps that did not alter any spectra, but this can be useful to quickly spot mistakes in the pipeline or to remove unnecessary filters, so this PR adds them. 


Patch:
diff --git a/matchms/filtering/SpectrumProcessor.py b/matchms/filtering/SpectrumProcessor.py
index a353e617..af2e3847 100644
--- a/matchms/filtering/SpectrumProcessor.py
+++ b/matchms/filtering/SpectrumProcessor.py
@@ -2,7 +2,7 @@ import inspect
 import logging
 from collections import defaultdict
 from functools import partial
-from typing import Callable, Dict, Optional, Tuple, Union
+from typing import Callable, Dict, List, Optional, Tuple, Union
 import numpy as np
 import pandas as pd
 from tqdm import tqdm
@@ -182,7 +182,7 @@ class SpectrumProcessor:
             List containing the processed spectrums.
         """
         if create_report:
-            processing_report = ProcessingReport()
+            processing_report = ProcessingReport(self.filters)
         else:
             processing_report = None
 
@@ -253,7 +253,11 @@ def get_parameter_settings(func):
 class ProcessingReport:
     """Class to keep track of spectrum changes during filtering.
     """
-    def __init__(self):
+    def __init__(self, filter_functions: Optional[List[Callable]] = None):
+        if filter_functions:
+            self.filter_names = [filter_function.__name__ for filter_function in filter_functions]
+        else:
+            self.filter_names = []
         self.counter_changed_metadata = defaultdict(int)
         self.counter_removed_spectrums = defaultdict(int)
         self.counter_changed_peaks = defaultdict(int)
@@ -284,6 +288,11 @@ class ProcessingReport:
         processing_report = pd.merge(removed, metadata_changed, how="outer", on="filter")
         processing_report = pd.merge(processing_report, peaks_changed, how="outer", on="filter")
 
+        # Add filters that did not do any changes:
+        for filter_name in self.filter_names:
+            if filter_name not in processing_report["filter"].values:
+                processing_report.loc[len(processing_report)] = {"filter": filter_name}
+
         processing_report = processing_report.set_index("filter").fillna(0)
         return processing_report.astype(int)
 


Test patch:
diff --git a/tests/filtering/test_spectrum_processor.py b/tests/filtering/test_spectrum_processor.py
index 354538a3..f716f3a6 100644
--- a/tests/filtering/test_spectrum_processor.py
+++ b/tests/filtering/test_spectrum_processor.py
@@ -147,10 +147,12 @@ def test_filter_spectrums_report(spectrums):
     assert report.counter_number_processed == 3
     assert report.counter_changed_metadata == {'make_charge_int': 2, 'interpret_pepmass': 3, 'derive_ionmode': 3}
     report_df = report.to_dataframe()
-    assert np.all(report_df.loc[["require_minimum_number_of_peaks", "interpret_pepmass", "add_losses"]].values == np.array(
+    assert np.all(report_df.loc[["require_minimum_number_of_peaks", "interpret_pepmass",
+                                 "add_losses", "correct_charge"]].values == np.array(
         [[1, 0, 0],
          [0, 3, 0],
-         [0, 0, 2]]))
+         [0, 0, 2],
+         [0, 0, 0]]))
 
 
 def test_processing_report_class(spectrums):