matchms.utils module

matchms.utils.filter_empty_spectra(spectra: list[Spectrum]) list[Spectrum][source]

Filter None values in spectra list.

Parameters:

spectra – List of spectra to filter.

matchms.utils.filter_none(iterable: Iterable) Iterable[source]

Filter iterable to remove ‘None’ elements.

Args:

iterable (Iterable): Iterable to filter.

Returns:

Iterable: Filtered iterable.

matchms.utils.fingerprint_export_warning(spectra: list[Spectrum])[source]

Check if any spectrum in the provided list contains a “fingerprint” and log a warning if so.

Parameters:

spectra (List[SpectrumType]) – A list of spectrum objects to be checked for the presence of a “fingerprint”.

Notes

This function will log a warning message if any spectrum in the list has a “fingerprint” attribute that is not None.

matchms.utils.get_common_keys(first: list[str], second: list[str]) list[str][source]

Get common elements of two sets of strings in a case insensitive way.

Args:

first (List[str]): First list of strings. second (List[str]): List of strings to search for matches.

Returns:

List[str]: List of common elements without regarding case of first list.

matchms.utils.get_first_common_element(first: Iterable[str], second: Iterable[str]) str[source]

Find the first common element between two iterables.

Args:

first (Iterable[str]): The first iterable of strings. second (Iterable[str]): The second iterable of strings.

Returns:

str: The first common element found in both iterables, or None if no common element is found.

matchms.utils.load_export_key_conversions(export_key_conversions_file: str = None, export_style: str = None) dict[source]

Load dictionary of export key conversions. Makes sure that file loading is cached. :param export_key_conversions_file: Path to the file containing export key conversions. If None, a default path is used. :type export_key_conversions_file: str, optional :param export_style: Style of the export keys to be used. :type export_style: str, optional

Returns:

Dictionary containing the export key conversions.

Return type:

dict

Raises:

AssertionError – If the specified export_key_conversions_file does not exist.

matchms.utils.load_known_key_conversions(key_conversions_file: str = None) dict[source]

Load dictionary of known key conversions. Makes sure that file loading is cached.

Parameters:

key_conversions_file (str, optional) – Path to the CSV file containing key conversions. If not provided, the default file “known_key_conversions.csv” located in the “data” directory relative to the current script will be used.

Returns:

A dictionary containing the key conversions loaded from the specified file.

Return type:

dict

Raises:

AssertionError – If the specified key conversions file does not exist.

matchms.utils.rename_deprecated_params(param_mapping: dict, version: str = None) Callable[source]

Decorator for renaming old, deprecated parameters.

Usage example: .. testcode:: @rename_deprecated_params({“spectrums”: “spectra”}, version=”0.1.0”) def example_func(spectra: List[Spectrum], another_param: str):

some function logic using spectra

example_func(spectrums: […], another_param: “some string”)

Parameters:
  • param_mapping – Dict of mapping from old to new parameter names e.g., {“spectrums”: “spectra”}

  • version – Version in which the parameters are marked as deprecated.

  • Returns – Callable function.

matchms.utils.to_camel_case(snake_str: str) str[source]

Converts snake_case to camelCase.

Used for conversion between snake_case and camelCase to match third party libraries (e.g., RDKit).

Parameters:

snake_str – The input string in snake_case

Return type:

camelCased str