Skip to content

Spectrum

mzmlpy.spectra.Spectrum dataclass

Spectrum(element: ElementTree.Element)

Bases: _ParamGroup, _BinaryDataArrayMixin, _ScanListMixin, _PrecursorListMixin, _ProductListMixin

An mzML spectrum element.

Exposes binary data arrays (mz, intensity, charge, ion mobility via has_im/im_types), scan metadata (scan_start_time, ion_injection_time, lower_mz, upper_mz, spectrum_type, polarity, ms_level, TIC), and structured precursor/product lists.

id property

id: str

Get spectrum id.

spot_id property

spot_id: str | None

Get spectrum spot id, or None if not present.

index property

index: int | None

Get spectrum index, or None if not present.

default_array_length property

default_array_length: int | None

Get spectrum default array length, or None if not present.

data_processing_ref property

data_processing_ref: str | None

Get spectrum data processing reference, or None if not present.

source_file_ref property

source_file_ref: str | None

Get spectrum source file reference, or None if not present.

mz property

mz: NDArray[np.float64] | None

Get m/z array as a numpy array, or None if not present.

intensity property

intensity: NDArray[np.float64] | None

Get intensity array as a numpy array, or None if not present.

charge property

charge: NDArray[np.float64] | None

Return the per-point charge array, or None if no charge binary array is present.

has_im property

has_im: bool

Return True if any ion mobility binary array is present in this spectrum.

im_types property

im_types: set[BinaryDataArrayAccession]

Return the set of ion mobility array accessions present in this spectrum; empty set if none.

spectrum_type cached property

spectrum_type: Literal['centroid', 'profile'] | None

Get spectrum type (centroid / profile / unknown).

polarity cached property

polarity: Literal['positive', 'negative'] | None

Get polarity (positive / negative / or unknown scan).

TIC cached property

TIC: float | None

Get total ion current (TIC) for this spectrum.

ms_level cached property

ms_level: int | None

Get MS level for this spectrum.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: list[CvParam]

Parse cvParams from the XML element.

accessions cached property

accessions: set[str]

Get a set of all accession numbers from the cvParams.

names cached property

names: set[str]

Get a set of all names from the cvParams.

user_params property

user_params: list[UserParam]

Parse userParams from the XML element.

ref_params property

ref_params: list[ReferenceableParamGroupRef]

Get a list of all referenceable parameters from the XML element.

has_products property

has_products: bool

Check if this spectrum has a product list.

products property

products: list[Product]

Get a list of Product objects for the product list of this spectrum, or None

has_precursors property

has_precursors: bool

Check if this spectrum has a precursor list.

precursors property

precursors: list[Precursor]

Get a list of Precursor objects for the precursor list of this spectrum, or None .

spectra_combination property

spectra_combination: (
    Literal["no_combination", "median", "sum", "mean"]
    | None
)

Get spectrum combination type (if any) for this spectrum.

scans property

scans: list[Scan]

Get a list of Scan objects for the scan list of this spectrum, or None if no scan list is present.

is_single_scan property

is_single_scan: bool

Check if this spectrum has a single scan.

lower_mz property

lower_mz: float | None

Get scan window lower limit for this spectrum, if it has a single scan with a single scan window.

upper_mz property

upper_mz: float | None

Get scan window upper limit for this spectrum, if it has a single scan with a single scan window.

scan_start_time property

scan_start_time: timedelta | None

Get scan start time for this spectrum, if it has a single scan.

ion_injection_time property

ion_injection_time: timedelta | None

Get ion injection time for this spectrum, if it has a single scan.

binary_arrays property

binary_arrays: list[BinaryDataArray]

Get a list of BinaryDataConverter objects for each binary data array.

get_cvparm

get_cvparm(id: str) -> CvParam | None

Get a cvParam by accession or name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
94
95
96
97
98
99
def get_cvparm(self, id: str) -> CvParam | None:
    """Get a cvParam by accession or name."""
    for cv_param in self.cv_params:
        if cv_param.accession == id or cv_param.name == id:
            return cv_param
    return None

get_binary_array

get_binary_array(id: str) -> BinaryDataArray | None

Get a BinaryDataConverter object for the binary data array with the specified id.

Source code in src/mzmlpy/spectra.py
210
211
212
213
214
def get_binary_array(self, id: str) -> BinaryDataArray | None:
    """Get a BinaryDataConverter object for the binary data array with the specified id."""
    if self._binary_array_list is not None:
        return self._binary_array_list.get_binary_array(id)
    return None

has_binary_array

has_binary_array(id: str) -> bool

Check if a binary data array with the specified id exists.

Source code in src/mzmlpy/spectra.py
216
217
218
219
220
def has_binary_array(self, id: str) -> bool:
    """Check if a binary data array with the specified id exists."""
    if self._binary_array_list is not None:
        return self._binary_array_list.has_binary_array(id)
    return False

serialize

serialize() -> dict

return the full element content

Source code in src/mzmlpy/elems/dtree_wrapper.py
59
60
61
62
63
64
65
66
def serialize(self) -> dict:
    """return the full element content"""
    return {
        "tag": self.element.tag,
        "attributes": self.element.attrib,
        "text": self.element.text,
        "children": [_DataTreeWrapper(child).serialize() for child in self.element],
    }

get_attribute

get_attribute(attr_name: str) -> str | None

Get an attribute value from the element.

Source code in src/mzmlpy/elems/dtree_wrapper.py
68
69
70
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

has_cvparm

has_cvparm(id: str) -> bool

Check if a cvParam with the given accession or name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
101
102
103
def has_cvparm(self, id: str) -> bool:
    """Check if a cvParam with the given accession or name exists."""
    return any(cv_param.accession == id or cv_param.name == id for cv_param in self.cv_params)

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
133
134
135
136
137
138
def get_user_param(self, name: str) -> UserParam | None:
    """Get a userParam by name."""
    for user_param in self.user_params:
        if user_param.name == name:
            return user_param
    return None

has_user_param

has_user_param(name: str) -> bool

Check if a userParam with the given name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
140
141
142
def has_user_param(self, name: str) -> bool:
    """Check if a userParam with the given name exists."""
    return any(user_param.name == name for user_param in self.user_params)

get_ref_param

get_ref_param(
    ref: str,
) -> ReferenceableParamGroupRef | None

Get a referenceable parameter by ref.

Source code in src/mzmlpy/elems/dtree_wrapper.py
153
154
155
156
157
158
def get_ref_param(self, ref: str) -> ReferenceableParamGroupRef | None:
    """Get a referenceable parameter by ref."""
    for ref_param in self.ref_params:
        if ref_param.ref == ref:
            return ref_param
    return None

has_ref_param

has_ref_param(ref: str) -> bool

Check if a referenceable parameter with the given ref exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
160
161
162
def has_ref_param(self, ref: str) -> bool:
    """Check if a referenceable parameter with the given ref exists."""
    return any(ref_param.ref == ref for ref_param in self.ref_params)

Scan

mzmlpy.spectra.Scan dataclass

Scan(element: ElementTree.Element)

Bases: _ParamGroup

A single scan event with timing, window, and CV parameter metadata.

scan_windows property

scan_windows: list[ScanWindow]

Get a list of ScanWindow objects for the scan window list of this scan.

is_single_windowed_scan property

is_single_windowed_scan: bool

Check if this scan has a single scan window.

lower_mz property

lower_mz: float | None

Get scan window lower limit for this scan, if it has a single scan window.

upper_mz property

upper_mz: float | None

Get scan window upper limit for this scan, if it has a single scan window.

scan_start_time property

scan_start_time: timedelta | None

Get scan start time for this scan.

ion_injection_time property

ion_injection_time: timedelta | None

Get ion injection time for this scan.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: list[CvParam]

Parse cvParams from the XML element.

accessions cached property

accessions: set[str]

Get a set of all accession numbers from the cvParams.

names cached property

names: set[str]

Get a set of all names from the cvParams.

user_params property

user_params: list[UserParam]

Parse userParams from the XML element.

ref_params property

ref_params: list[ReferenceableParamGroupRef]

Get a list of all referenceable parameters from the XML element.

serialize

serialize() -> dict

return the full element content

Source code in src/mzmlpy/elems/dtree_wrapper.py
59
60
61
62
63
64
65
66
def serialize(self) -> dict:
    """return the full element content"""
    return {
        "tag": self.element.tag,
        "attributes": self.element.attrib,
        "text": self.element.text,
        "children": [_DataTreeWrapper(child).serialize() for child in self.element],
    }

get_attribute

get_attribute(attr_name: str) -> str | None

Get an attribute value from the element.

Source code in src/mzmlpy/elems/dtree_wrapper.py
68
69
70
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cvparm

get_cvparm(id: str) -> CvParam | None

Get a cvParam by accession or name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
94
95
96
97
98
99
def get_cvparm(self, id: str) -> CvParam | None:
    """Get a cvParam by accession or name."""
    for cv_param in self.cv_params:
        if cv_param.accession == id or cv_param.name == id:
            return cv_param
    return None

has_cvparm

has_cvparm(id: str) -> bool

Check if a cvParam with the given accession or name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
101
102
103
def has_cvparm(self, id: str) -> bool:
    """Check if a cvParam with the given accession or name exists."""
    return any(cv_param.accession == id or cv_param.name == id for cv_param in self.cv_params)

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
133
134
135
136
137
138
def get_user_param(self, name: str) -> UserParam | None:
    """Get a userParam by name."""
    for user_param in self.user_params:
        if user_param.name == name:
            return user_param
    return None

has_user_param

has_user_param(name: str) -> bool

Check if a userParam with the given name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
140
141
142
def has_user_param(self, name: str) -> bool:
    """Check if a userParam with the given name exists."""
    return any(user_param.name == name for user_param in self.user_params)

get_ref_param

get_ref_param(
    ref: str,
) -> ReferenceableParamGroupRef | None

Get a referenceable parameter by ref.

Source code in src/mzmlpy/elems/dtree_wrapper.py
153
154
155
156
157
158
def get_ref_param(self, ref: str) -> ReferenceableParamGroupRef | None:
    """Get a referenceable parameter by ref."""
    for ref_param in self.ref_params:
        if ref_param.ref == ref:
            return ref_param
    return None

has_ref_param

has_ref_param(ref: str) -> bool

Check if a referenceable parameter with the given ref exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
160
161
162
def has_ref_param(self, ref: str) -> bool:
    """Check if a referenceable parameter with the given ref exists."""
    return any(ref_param.ref == ref for ref_param in self.ref_params)

ScanWindow

mzmlpy.spectra.ScanWindow dataclass

ScanWindow(element: ElementTree.Element)

Bases: _ParamGroup

A scan window defining the m/z range acquired in a single scan.

lower_mz property

lower_mz: float | None

Get scan window lower limit for this spectrum.

upper_mz property

upper_mz: float | None

Get scan window upper limit for this spectrum.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: list[CvParam]

Parse cvParams from the XML element.

accessions cached property

accessions: set[str]

Get a set of all accession numbers from the cvParams.

names cached property

names: set[str]

Get a set of all names from the cvParams.

user_params property

user_params: list[UserParam]

Parse userParams from the XML element.

ref_params property

ref_params: list[ReferenceableParamGroupRef]

Get a list of all referenceable parameters from the XML element.

serialize

serialize() -> dict

return the full element content

Source code in src/mzmlpy/elems/dtree_wrapper.py
59
60
61
62
63
64
65
66
def serialize(self) -> dict:
    """return the full element content"""
    return {
        "tag": self.element.tag,
        "attributes": self.element.attrib,
        "text": self.element.text,
        "children": [_DataTreeWrapper(child).serialize() for child in self.element],
    }

get_attribute

get_attribute(attr_name: str) -> str | None

Get an attribute value from the element.

Source code in src/mzmlpy/elems/dtree_wrapper.py
68
69
70
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cvparm

get_cvparm(id: str) -> CvParam | None

Get a cvParam by accession or name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
94
95
96
97
98
99
def get_cvparm(self, id: str) -> CvParam | None:
    """Get a cvParam by accession or name."""
    for cv_param in self.cv_params:
        if cv_param.accession == id or cv_param.name == id:
            return cv_param
    return None

has_cvparm

has_cvparm(id: str) -> bool

Check if a cvParam with the given accession or name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
101
102
103
def has_cvparm(self, id: str) -> bool:
    """Check if a cvParam with the given accession or name exists."""
    return any(cv_param.accession == id or cv_param.name == id for cv_param in self.cv_params)

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
133
134
135
136
137
138
def get_user_param(self, name: str) -> UserParam | None:
    """Get a userParam by name."""
    for user_param in self.user_params:
        if user_param.name == name:
            return user_param
    return None

has_user_param

has_user_param(name: str) -> bool

Check if a userParam with the given name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
140
141
142
def has_user_param(self, name: str) -> bool:
    """Check if a userParam with the given name exists."""
    return any(user_param.name == name for user_param in self.user_params)

get_ref_param

get_ref_param(
    ref: str,
) -> ReferenceableParamGroupRef | None

Get a referenceable parameter by ref.

Source code in src/mzmlpy/elems/dtree_wrapper.py
153
154
155
156
157
158
def get_ref_param(self, ref: str) -> ReferenceableParamGroupRef | None:
    """Get a referenceable parameter by ref."""
    for ref_param in self.ref_params:
        if ref_param.ref == ref:
            return ref_param
    return None

has_ref_param

has_ref_param(ref: str) -> bool

Check if a referenceable parameter with the given ref exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
160
161
162
def has_ref_param(self, ref: str) -> bool:
    """Check if a referenceable parameter with the given ref exists."""
    return any(ref_param.ref == ref for ref_param in self.ref_params)

BinaryDataArray

mzmlpy.spectra.BinaryDataArray dataclass

BinaryDataArray(element: ElementTree.Element)

Bases: _ParamGroup

Wraps a single binaryDataArray XML element, handling base64 decoding and decompression.

Exposes compression type, numeric encoding, semantic array type, and a data property that decodes the raw bytes into a NumPy array on each access.

compression cached property

compression: CompressionTypeAccessions | None

Return the compression accession for this array, or None if no compression CV term is present.

encoding cached property

encoding: BinaryDataTypeAccession | None

Return the binary data type accession (e.g. 32-bit or 64-bit float/int), or None if absent.

binary_array_type cached property

binary_array_type: BinaryDataArrayAccession | None

Return the semantic array type accession (e.g. m/z, intensity, ion mobility), or None if absent.

data property

data: np.ndarray

Decode and return the binary data as a NumPy array.

Decoding runs on every access — store the result in a local variable if you need it more than once.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: list[CvParam]

Parse cvParams from the XML element.

accessions cached property

accessions: set[str]

Get a set of all accession numbers from the cvParams.

names cached property

names: set[str]

Get a set of all names from the cvParams.

user_params property

user_params: list[UserParam]

Parse userParams from the XML element.

ref_params property

ref_params: list[ReferenceableParamGroupRef]

Get a list of all referenceable parameters from the XML element.

serialize

serialize() -> dict

return the full element content

Source code in src/mzmlpy/elems/dtree_wrapper.py
59
60
61
62
63
64
65
66
def serialize(self) -> dict:
    """return the full element content"""
    return {
        "tag": self.element.tag,
        "attributes": self.element.attrib,
        "text": self.element.text,
        "children": [_DataTreeWrapper(child).serialize() for child in self.element],
    }

get_attribute

get_attribute(attr_name: str) -> str | None

Get an attribute value from the element.

Source code in src/mzmlpy/elems/dtree_wrapper.py
68
69
70
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cvparm

get_cvparm(id: str) -> CvParam | None

Get a cvParam by accession or name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
94
95
96
97
98
99
def get_cvparm(self, id: str) -> CvParam | None:
    """Get a cvParam by accession or name."""
    for cv_param in self.cv_params:
        if cv_param.accession == id or cv_param.name == id:
            return cv_param
    return None

has_cvparm

has_cvparm(id: str) -> bool

Check if a cvParam with the given accession or name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
101
102
103
def has_cvparm(self, id: str) -> bool:
    """Check if a cvParam with the given accession or name exists."""
    return any(cv_param.accession == id or cv_param.name == id for cv_param in self.cv_params)

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
133
134
135
136
137
138
def get_user_param(self, name: str) -> UserParam | None:
    """Get a userParam by name."""
    for user_param in self.user_params:
        if user_param.name == name:
            return user_param
    return None

has_user_param

has_user_param(name: str) -> bool

Check if a userParam with the given name exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
140
141
142
def has_user_param(self, name: str) -> bool:
    """Check if a userParam with the given name exists."""
    return any(user_param.name == name for user_param in self.user_params)

get_ref_param

get_ref_param(
    ref: str,
) -> ReferenceableParamGroupRef | None

Get a referenceable parameter by ref.

Source code in src/mzmlpy/elems/dtree_wrapper.py
153
154
155
156
157
158
def get_ref_param(self, ref: str) -> ReferenceableParamGroupRef | None:
    """Get a referenceable parameter by ref."""
    for ref_param in self.ref_params:
        if ref_param.ref == ref:
            return ref_param
    return None

has_ref_param

has_ref_param(ref: str) -> bool

Check if a referenceable parameter with the given ref exists.

Source code in src/mzmlpy/elems/dtree_wrapper.py
160
161
162
def has_ref_param(self, ref: str) -> bool:
    """Check if a referenceable parameter with the given ref exists."""
    return any(ref_param.ref == ref for ref_param in self.ref_params)