Spectrum¶
mzmlpy.spectra.Spectrum
dataclass
¶
Bases: _ParamGroup, _BinaryDataArrayMixin, _ScanListMixin, _PrecursorListMixin, _ProductListMixin
An mzML spectrum element.
Exposes binary data arrays (mz, intensity, charge_array, ion mobility via has_im/im_types),
scan metadata (rt, ion_injection_time, mz_range, ook0, spectrum_type, polarity,
ms_level, total_ion_current), and structured precursor/product lists.
id_dict
property
¶
Parse the native id into its key=value components (e.g. {"scan": 19}).
Vendor native ids are space-separated key=value tokens, e.g. Thermo
"controllerType=0 controllerNumber=1 scan=19" or Bruker "frame=1016 scan=1".
Integer components are returned as ints, so spectrum.id_dict["scan"] gives the scan
number without manual parsing.
default_array_length
property
¶
Get spectrum default array length, or None if not present.
data_processing_ref
property
¶
Get spectrum data processing reference, or None if not present.
source_file_ref
property
¶
Get spectrum source file reference, or None if not present.
intensity
property
¶
Get intensity array as a numpy array, or None if not present.
charge_array
property
¶
Return the per-point charge array (MS:1000516), or None if no charge binary array is present.
Named charge_array because charge means a single precursor charge state elsewhere
(:attr:SelectedIon.charge, :attr:precursor_charge).
has_im
property
¶
Return True if this spectrum carries ion mobility data — either as a binary array (e.g. combined-IM frames) or as a scan-level cvParam (e.g. Bruker timsTOF PASEF MS2).
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
¶
Get spectrum type (centroid / profile / unknown).
polarity
cached
property
¶
Get polarity (positive / negative / or unknown scan).
total_ion_current
cached
property
¶
Total ion current of this spectrum (MS:1000285), or None if absent.
base_peak_intensity
property
¶
Base peak intensity for this spectrum (MS:1000505).
lowest_observed_mz
property
¶
Lowest observed m/z for this spectrum (MS:1000528).
highest_observed_mz
property
¶
Highest observed m/z for this spectrum (MS:1000527).
cv_params
cached
property
¶
cv_params: tuple[CvParam, ...]
The element's cvParams, in document order.
user_params
cached
property
¶
user_params: tuple[UserParam, ...]
The element's userParams, in document order.
ref_params
cached
property
¶
ref_params: tuple[ReferenceableParamGroupRef, ...]
The element's referenceableParamGroupRefs, in document order.
products
property
¶
products: tuple[Product, ...]
Get a list of Product objects for the product list of this spectrum, or None
precursors
cached
property
¶
precursors: tuple[Precursor, ...]
Get a list of Precursor objects for the precursor list of this spectrum, or None .
precursor_mz
property
¶
m/z of the first selected ion of the first precursor, or None.
Matches tdfpy's Precursor.precursor_mz. For DIA spectra, which usually report only an
isolation window, use :attr:isolation_mz_range.
precursor_charge
property
¶
Charge state of the first selected ion of the first precursor, or None.
Named precursor_charge (not charge) so 0.9 code that read Spectrum.charge as the
per-point array fails loudly; that array is now :attr:charge_array.
collision_energy
property
¶
Collision energy of the first precursor's activation, as recorded (see
:attr:Activation.collision_energy), or None.
isolation_mz_range
property
¶
Isolated m/z range of the first precursor's isolation window (see
:attr:IsolationWindow.isolation_mz_range), or None.
spectra_combination
property
¶
Get spectrum combination type (if any) for this spectrum.
scans
cached
property
¶
scans: tuple[Scan, ...]
The Scan objects of this spectrum's scan list, or an empty tuple if it has none.
mz_range
property
¶
m/z range acquired for this spectrum (see :attr:Scan.mz_range), from its first scan.
rt
property
¶
Retention time (scan start time) of this spectrum in seconds, from its first scan.
ion_injection_time
property
¶
Ion injection time in milliseconds (see :attr:Scan.ion_injection_time), from the first scan.
ook0
property
¶
Inverse reduced ion mobility 1/K0 (Vs/cm²) from this spectrum's first scan, or None.
This never falls back to drift time. Use spectrum.scans[0].drift_time for drift-tube data.
filter_string
property
¶
Scan filter string for this spectrum (from its single scan), if present.
binary_arrays
cached
property
¶
binary_arrays: tuple[BinaryDataArray, ...]
The binary data arrays, in document order.
get_cv_param
¶
get_cv_param(id: str) -> CvParam | None
Get a cvParam by accession or name, or None if absent.
cv_float
¶
Return a cvParam's value as a float, or None if the term is absent or has no value.
Raises MzmlError naming the term and its bad value if the value is present but not numeric — more actionable than a bare "could not convert string to float".
Source code in src/mzmlpy/elems/dtree_wrapper.py
cv_int
¶
Return a cvParam's value as an int, or None if absent or valueless (see :meth:cv_float).
Source code in src/mzmlpy/elems/dtree_wrapper.py
get_binary_array
¶
get_binary_array(id: str) -> BinaryDataArray | None
Get a BinaryDataConverter object for the binary data array with the specified id.
has_binary_array
¶
serialize
¶
Return the element as nested dicts: tag, attributes, text, children.
The result is a fresh copy; changing it does not change the wrapped element.
Source code in src/mzmlpy/elems/dtree_wrapper.py
get_attribute
¶
has_cv_param
¶
Check if a cvParam with the given accession or name exists.
has_user_param
¶
get_ref_param
¶
get_ref_param(
ref: str,
) -> ReferenceableParamGroupRef | None
Get a referenceable parameter by ref.
has_ref_param
¶
Scan¶
mzmlpy.spectra.Scan
dataclass
¶
Bases: _ParamGroup
A single scan event with timing, window, and CV parameter metadata.
scan_windows
property
¶
scan_windows: tuple[ScanWindow, ...]
Get a list of ScanWindow objects for the scan window list of this scan.
is_single_windowed_scan
property
¶
Check if this scan has a single scan window.
mz_range
property
¶
m/z range acquired by this scan: (lowest lower limit, highest upper limit) over its
scan windows, or None if no window has both limits.
A scan with several windows returns the envelope of all complete windows; use
:attr:scan_windows for the individual windows.
rt
property
¶
Retention time (scan start time, MS:1000016) of this scan in seconds, or None if absent.
Values recorded in milliseconds, minutes or hours are converted to seconds. A value with no
unit, or a unit that is not a time unit, is taken as seconds and warns once per unit. A
non-numeric value raises :class:MzmlError.
ion_injection_time
property
¶
Ion injection time (MS:1000927) of this scan in milliseconds, or None if absent.
Milliseconds is the unit instruments and search engines report. Values recorded in other
time units are converted. A value with no unit, or a unit that is not a time unit, is taken
as milliseconds and warns once per unit. A non-numeric value raises :class:MzmlError.
ook0
property
¶
Inverse reduced ion mobility 1/K0 (Vs/cm², MS:1002815) of this scan, e.g. Bruker timsTOF, or None.
drift_time
property
¶
Ion mobility drift time (MS:1002476) of this scan, or None.
filter_string
property
¶
Instrument filter string for this scan (MS:1000512), e.g. a Thermo scan filter.
faims_compensation_voltage
property
¶
FAIMS compensation voltage for this scan (MS:1001581).
Front-end high-field asymmetric waveform ion mobility (FAIMS) filtering: each scan may carry a single compensation voltage. See the PSI IM-MS/DIA recommendation v1.0, §3.6.
selexion_separation_voltage
property
¶
SCIEX SelexION differential-mobility separation voltage for this scan (MS:1003394).
selexion_compensation_voltage
property
¶
SCIEX SelexION differential-mobility compensation voltage for this scan (MS:1003371).
cv_params
cached
property
¶
cv_params: tuple[CvParam, ...]
The element's cvParams, in document order.
user_params
cached
property
¶
user_params: tuple[UserParam, ...]
The element's userParams, in document order.
ref_params
cached
property
¶
ref_params: tuple[ReferenceableParamGroupRef, ...]
The element's referenceableParamGroupRefs, in document order.
serialize
¶
Return the element as nested dicts: tag, attributes, text, children.
The result is a fresh copy; changing it does not change the wrapped element.
Source code in src/mzmlpy/elems/dtree_wrapper.py
get_attribute
¶
get_cv_param
¶
get_cv_param(id: str) -> CvParam | None
Get a cvParam by accession or name, or None if absent.
has_cv_param
¶
Check if a cvParam with the given accession or name exists.
cv_float
¶
Return a cvParam's value as a float, or None if the term is absent or has no value.
Raises MzmlError naming the term and its bad value if the value is present but not numeric — more actionable than a bare "could not convert string to float".
Source code in src/mzmlpy/elems/dtree_wrapper.py
cv_int
¶
Return a cvParam's value as an int, or None if absent or valueless (see :meth:cv_float).
Source code in src/mzmlpy/elems/dtree_wrapper.py
has_user_param
¶
get_ref_param
¶
get_ref_param(
ref: str,
) -> ReferenceableParamGroupRef | None
Get a referenceable parameter by ref.
has_ref_param
¶
ScanWindow¶
mzmlpy.spectra.ScanWindow
dataclass
¶
Bases: _ParamGroup
A scan window defining the m/z range acquired in a single scan.
mz_range
property
¶
(lower_mz, upper_mz) of this scan window, or None if either limit is missing.
cv_params
cached
property
¶
cv_params: tuple[CvParam, ...]
The element's cvParams, in document order.
user_params
cached
property
¶
user_params: tuple[UserParam, ...]
The element's userParams, in document order.
ref_params
cached
property
¶
ref_params: tuple[ReferenceableParamGroupRef, ...]
The element's referenceableParamGroupRefs, in document order.
serialize
¶
Return the element as nested dicts: tag, attributes, text, children.
The result is a fresh copy; changing it does not change the wrapped element.
Source code in src/mzmlpy/elems/dtree_wrapper.py
get_attribute
¶
get_cv_param
¶
get_cv_param(id: str) -> CvParam | None
Get a cvParam by accession or name, or None if absent.
has_cv_param
¶
Check if a cvParam with the given accession or name exists.
cv_float
¶
Return a cvParam's value as a float, or None if the term is absent or has no value.
Raises MzmlError naming the term and its bad value if the value is present but not numeric — more actionable than a bare "could not convert string to float".
Source code in src/mzmlpy/elems/dtree_wrapper.py
cv_int
¶
Return a cvParam's value as an int, or None if absent or valueless (see :meth:cv_float).
Source code in src/mzmlpy/elems/dtree_wrapper.py
has_user_param
¶
get_ref_param
¶
get_ref_param(
ref: str,
) -> ReferenceableParamGroupRef | None
Get a referenceable parameter by ref.
has_ref_param
¶
BinaryDataArray¶
mzmlpy.spectra.BinaryDataArray
dataclass
¶
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: CompressionTypeAccession | None
Return the compression accession for this array, or None if no compression CV term is present.
cvParam order has no meaning in mzML, so when several compression terms are present they are resolved independently of order: a combined term wins over the generic codec it already includes, a bare MS-Numpress term plus the generic zlib or zstd term (the form written before the combined "followed by zlib" terms existed) resolves to the combined term, and "no compression" never masks a real codec. Any other conflict warns and returns the first term.
encoding
cached
property
¶
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
¶
Decode and return the binary data as a NumPy array.
Raw numeric encodings retain their declared dtype, including empty arrays. Numpress returns reconstructed float64 values. Arrays are writable. Decoding runs on every access. Store the result locally when using it repeatedly.
cv_params
cached
property
¶
cv_params: tuple[CvParam, ...]
The element's cvParams, in document order.
user_params
cached
property
¶
user_params: tuple[UserParam, ...]
The element's userParams, in document order.
ref_params
cached
property
¶
ref_params: tuple[ReferenceableParamGroupRef, ...]
The element's referenceableParamGroupRefs, in document order.
serialize
¶
Return the element as nested dicts: tag, attributes, text, children.
The result is a fresh copy; changing it does not change the wrapped element.
Source code in src/mzmlpy/elems/dtree_wrapper.py
get_attribute
¶
get_cv_param
¶
get_cv_param(id: str) -> CvParam | None
Get a cvParam by accession or name, or None if absent.
has_cv_param
¶
Check if a cvParam with the given accession or name exists.
cv_float
¶
Return a cvParam's value as a float, or None if the term is absent or has no value.
Raises MzmlError naming the term and its bad value if the value is present but not numeric — more actionable than a bare "could not convert string to float".
Source code in src/mzmlpy/elems/dtree_wrapper.py
cv_int
¶
Return a cvParam's value as an int, or None if absent or valueless (see :meth:cv_float).
Source code in src/mzmlpy/elems/dtree_wrapper.py
has_user_param
¶
get_ref_param
¶
get_ref_param(
ref: str,
) -> ReferenceableParamGroupRef | None
Get a referenceable parameter by ref.