matchms.Metadata module

class matchms.Metadata.Metadata(metadata: dict | None = None, matchms_key_style: bool = True)[source]

Bases: object

Class to handle spectrum metadata in matchms.

Metadata entries will be stored as PickyDict dictionary in Unlike normal Python dictionaries, not all key names will be accepted. Key names will be forced to be lower-case to avoid confusions between key such as “Precursor_MZ” and “precursor_mz”.

To avoid the default harmonization of the metadata dictionary use the option matchms_key_style=False.

Code example:

metadata = Metadata({"Precursor_MZ": 201.5, "Compound Name": "SuperStuff"})
print(metadata["precursor_mz"])  # => 201.5
print(metadata["compound_name"])  # => SuperStuff

Or if the matchms default metadata harmonization should not take place:

metadata = Metadata({"Precursor_MZ": 201.5, "Compound Name": "SuperStuff"},
print(metadata["precursor_mz"])  # => 201.5
print(metadata["compound_name"])  # => None (now you need to use "compound name")
__init__(metadata: dict | None = None, matchms_key_style: bool = True)[source]
  • metadata – Spectrum metadata as a dictionary.

  • matchms_key_style – Set to False if metadata harmonization to default keys is not desired. The default is True.

get(key: str, default=None)[source]

Retrieve value from metadata dict.


Runs default harmonization of metadata.

Method harmonized metadata field names which includes setting them to lower-case and runing a series of regex replacements followed by default field name replacements (such as precursor_mass –> precursor_mz).


Runs default harmonization of metadata.

This includes harmonizing entries for ionmode, retention time and index, charge, as well as the removal of invalid entried (“”, “NA”, “N/A”, “NaN”).


Retrieve all items (key, value pairs) of metadata dict.


Retrieve all keys of metadata dict.

set(key: str, value)[source]

Set value in metadata dict.

static set_key_replacements(keys: dict)[source]

Set key replacements for metadata harmonization.


keys – Dictionary with key replacements.

to_dict(export_style: str = 'matchms')[source]

Returns a regular Python dictionary containing the metadata entries.


export_style – Specifies the naming style of the metadata fields. Default is “matchms”.


Retrieve all values of metadata dict.