matchms.filtering.SpectrumProcessor module

class matchms.filtering.SpectrumProcessor.ProcessingReport(filter_functions: List[Callable] | None = None)[source]

Bases: object

Class to keep track of spectrum changes during filtering.

__init__(filter_functions: List[Callable] | None = None)[source]
add_to_report(spectrum_old, spectrum_new: <module 'matchms.Spectrum' from '/home/docs/checkouts/readthedocs.org/user_builds/matchms/checkouts/latest/readthedocs/../matchms/Spectrum.py'>, filter_function_name: str)[source]

Add changes between spectrum_old and spectrum_new to the report.

to_dataframe()[source]

Create Pandas DataFrame Report of counted spectrum changes.

class matchms.filtering.SpectrumProcessor.SpectrumProcessor(filters: Iterable[str | Callable | Tuple[Callable | str, Dict[str, any]]])[source]

Bases: object

A class to process spectra using a series of filters.

The class enables a user to define a custom spectrum processing workflow by setting multiple flags and parameters.

Parameters:

filters – A list of filter functions, see add_filter for all the allowed formats.

__init__(filters: Iterable[str | Callable | Tuple[Callable | str, Dict[str, any]]])[source]
parse_and_add_filter(filter_description: str | Callable | Tuple[Callable | str, Dict[str, any]], filter_position: int | None = None)[source]

Adds a filter, by parsing the different allowed inputs.

filter:

Allowed formats: str (has to be a matchms function name) (str, {str, any} (has to be a matchms function name, followed by parameters) Callable (can be matchms filter or custom made filter) Callable, {str, any} (the dict should be parameters.

filter_position:

If None: Matchms filters are automatically ordered. Custom filters will be added at the end of the filter list. If not None, the filter will be added to the given position in the filter order list.

process_spectrum(spectrum, processing_report: ProcessingReport | None = None)[source]

Process the given spectrum with all filters in the processing pipeline.

Parameters:
  • spectrum (Spectrum) – The spectrum to process.

  • processing_report – A ProcessingReport object When passed the progress will be added to the object.

Returns:

The processed spectrum.

Return type:

Spectrum

process_spectrums(spectrums: list, progress_bar: bool = True, cleaned_spectra_file=None)[source]

Process a list of spectrums with all filters in the processing pipeline.

Parameters:
  • spectrums (list[Spectrum]) – The spectrums to process.

  • create_report (bool, optional) – Creates and outputs a report of the main changes during processing. The report will be returned as pandas DataFrame. Default is set to False.

  • progress_bar (bool, optional) – Displays progress bar if set to True. Default is True.

  • cleaned_spectra_file – Path to where the cleaned spectra should be saved.

Returns:

  • Spectrums – List containing the processed spectrums.

  • processing_report – A ProcessingReport containing the effect of the filters.

matchms.filtering.SpectrumProcessor.check_all_parameters_given(func: Callable)[source]

Asserts that all added parameters for a function are given (except spectrum_in)

matchms.filtering.SpectrumProcessor.create_partial_function(filter_function: Callable, filter_params: Dict[str, any] | None)[source]

Adds the filter params to the filter function

matchms.filtering.SpectrumProcessor.get_parameter_settings(func)[source]

Returns all parameters and parameter values for a function

This includes default parameter settings and, but also the settings stored in partial

matchms.filtering.SpectrumProcessor.objects_differ(obj1, obj2)[source]

Test if two objects are different. Supposed to work for standard Python data types as well as numpy arrays.