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_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 property

id: str

Get spectrum id.

id_dict property

id_dict: dict[str, int | str]

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.

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: np.ndarray | None

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

intensity property

intensity: np.ndarray | None

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

charge_array property

charge_array: np.ndarray | None

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

has_im: bool

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

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

Get spectrum type (centroid / profile / unknown).

polarity cached property

polarity: Polarity | None

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

total_ion_current cached property

total_ion_current: float | None

Total ion current of this spectrum (MS:1000285), or None if absent.

ms_level cached property

ms_level: int | None

Get MS level for this spectrum.

base_peak_mz property

base_peak_mz: float | None

Base peak m/z for this spectrum (MS:1000504).

base_peak_intensity property

base_peak_intensity: float | None

Base peak intensity for this spectrum (MS:1000505).

lowest_observed_mz property

lowest_observed_mz: float | None

Lowest observed m/z for this spectrum (MS:1000528).

highest_observed_mz property

highest_observed_mz: float | None

Highest observed m/z for this spectrum (MS:1000527).

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: tuple[CvParam, ...]

The element's cvParams, in document order.

accessions cached property

accessions: frozenset[str]

All cvParam accessions.

names cached property

names: frozenset[str]

All cvParam names.

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.

has_products property

has_products: bool

Check if this spectrum has a product list.

products property

products: tuple[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 cached property

precursors: tuple[Precursor, ...]

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

precursor_mz property

precursor_mz: float | None

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

precursor_charge: int | None

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: float | None

Collision energy of the first precursor's activation, as recorded (see :attr:Activation.collision_energy), or None.

isolation_mz_range property

isolation_mz_range: tuple[float, float] | None

Isolated m/z range of the first precursor's isolation window (see :attr:IsolationWindow.isolation_mz_range), or None.

spectra_combination property

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

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.

is_single_scan property

is_single_scan: bool

Check if this spectrum has a single scan.

mz_range property

mz_range: tuple[float, float] | None

m/z range acquired for this spectrum (see :attr:Scan.mz_range), from its first scan.

rt property

rt: float | None

Retention time (scan start time) of this spectrum in seconds, from its first scan.

ion_injection_time property

ion_injection_time: float | None

Ion injection time in milliseconds (see :attr:Scan.ion_injection_time), from the first scan.

ook0 property

ook0: float | None

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

filter_string: str | None

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.

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

cv_float

cv_float(id: str) -> float | None

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
def cv_float(self, id: str) -> float | None:
    """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".
    """
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return float(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-numeric value {cv_param.value!r}"
        ) from e

cv_int

cv_int(id: str) -> int | None

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
def cv_int(self, id: str) -> int | None:
    """Return a cvParam's value as an int, or None if absent or valueless (see :meth:`cv_float`)."""
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return int(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-integer value {cv_param.value!r}"
        ) from e

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
def get_binary_array(self, id: str) -> BinaryDataArray | None:
    """Get a BinaryDataConverter object for the binary data array with the specified id."""
    return self._binary_array_map.get(id)

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
def has_binary_array(self, id: str) -> bool:
    """Check if a binary data array with the specified id exists."""
    return id in self._binary_array_map

serialize

serialize() -> dict[str, object]

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
def serialize(self) -> dict[str, object]:
    """Return the element as nested dicts: ``tag``, ``attributes``, ``text``, ``children``.

    The result is a fresh copy; changing it does not change the wrapped element.
    """
    return {
        "tag": self.element.tag,
        "attributes": dict(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
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

has_cv_param

has_cv_param(id: str) -> bool

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

Source code in src/mzmlpy/elems/dtree_wrapper.py
def has_cv_param(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
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
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
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
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: tuple[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.

mz_range property

mz_range: tuple[float, float] | None

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

rt: float | None

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: float | None

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

ook0: float | None

Inverse reduced ion mobility 1/K0 (Vs/cm², MS:1002815) of this scan, e.g. Bruker timsTOF, or None.

drift_time property

drift_time: float | None

Ion mobility drift time (MS:1002476) of this scan, or None.

filter_string property

filter_string: str | None

Instrument filter string for this scan (MS:1000512), e.g. a Thermo scan filter.

faims_compensation_voltage property

faims_compensation_voltage: float | None

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

selexion_separation_voltage: float | None

SCIEX SelexION differential-mobility separation voltage for this scan (MS:1003394).

selexion_compensation_voltage property

selexion_compensation_voltage: float | None

SCIEX SelexION differential-mobility compensation voltage for this scan (MS:1003371).

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: tuple[CvParam, ...]

The element's cvParams, in document order.

accessions cached property

accessions: frozenset[str]

All cvParam accessions.

names cached property

names: frozenset[str]

All cvParam names.

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

serialize() -> dict[str, object]

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
def serialize(self) -> dict[str, object]:
    """Return the element as nested dicts: ``tag``, ``attributes``, ``text``, ``children``.

    The result is a fresh copy; changing it does not change the wrapped element.
    """
    return {
        "tag": self.element.tag,
        "attributes": dict(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
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cv_param

get_cv_param(id: str) -> CvParam | None

Get a cvParam by accession or name, or None if absent.

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

has_cv_param

has_cv_param(id: str) -> bool

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

Source code in src/mzmlpy/elems/dtree_wrapper.py
def has_cv_param(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)

cv_float

cv_float(id: str) -> float | None

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
def cv_float(self, id: str) -> float | None:
    """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".
    """
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return float(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-numeric value {cv_param.value!r}"
        ) from e

cv_int

cv_int(id: str) -> int | None

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
def cv_int(self, id: str) -> int | None:
    """Return a cvParam's value as an int, or None if absent or valueless (see :meth:`cv_float`)."""
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return int(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-integer value {cv_param.value!r}"
        ) from e

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
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
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
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
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.

mz_range property

mz_range: tuple[float, float] | None

(lower_mz, upper_mz) of this scan window, or None if either limit is missing.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: tuple[CvParam, ...]

The element's cvParams, in document order.

accessions cached property

accessions: frozenset[str]

All cvParam accessions.

names cached property

names: frozenset[str]

All cvParam names.

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

serialize() -> dict[str, object]

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
def serialize(self) -> dict[str, object]:
    """Return the element as nested dicts: ``tag``, ``attributes``, ``text``, ``children``.

    The result is a fresh copy; changing it does not change the wrapped element.
    """
    return {
        "tag": self.element.tag,
        "attributes": dict(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
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cv_param

get_cv_param(id: str) -> CvParam | None

Get a cvParam by accession or name, or None if absent.

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

has_cv_param

has_cv_param(id: str) -> bool

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

Source code in src/mzmlpy/elems/dtree_wrapper.py
def has_cv_param(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)

cv_float

cv_float(id: str) -> float | None

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
def cv_float(self, id: str) -> float | None:
    """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".
    """
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return float(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-numeric value {cv_param.value!r}"
        ) from e

cv_int

cv_int(id: str) -> int | None

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
def cv_int(self, id: str) -> int | None:
    """Return a cvParam's value as an int, or None if absent or valueless (see :meth:`cv_float`)."""
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return int(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-integer value {cv_param.value!r}"
        ) from e

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
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
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
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
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: 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

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.

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.

ns cached property

ns: str

Get XML namespace from the element tag.

cv_params cached property

cv_params: tuple[CvParam, ...]

The element's cvParams, in document order.

accessions cached property

accessions: frozenset[str]

All cvParam accessions.

names cached property

names: frozenset[str]

All cvParam names.

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

serialize() -> dict[str, object]

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
def serialize(self) -> dict[str, object]:
    """Return the element as nested dicts: ``tag``, ``attributes``, ``text``, ``children``.

    The result is a fresh copy; changing it does not change the wrapped element.
    """
    return {
        "tag": self.element.tag,
        "attributes": dict(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
def get_attribute(self, attr_name: str) -> str | None:
    """Get an attribute value from the element."""
    return self.element.attrib.get(attr_name)

get_cv_param

get_cv_param(id: str) -> CvParam | None

Get a cvParam by accession or name, or None if absent.

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

has_cv_param

has_cv_param(id: str) -> bool

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

Source code in src/mzmlpy/elems/dtree_wrapper.py
def has_cv_param(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)

cv_float

cv_float(id: str) -> float | None

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
def cv_float(self, id: str) -> float | None:
    """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".
    """
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return float(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-numeric value {cv_param.value!r}"
        ) from e

cv_int

cv_int(id: str) -> int | None

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
def cv_int(self, id: str) -> int | None:
    """Return a cvParam's value as an int, or None if absent or valueless (see :meth:`cv_float`)."""
    cv_param = self.get_cv_param(id)
    if cv_param is None or cv_param.value is None:
        return None
    try:
        return int(cv_param.value)
    except ValueError as e:
        raise MzmlError(
            f"CV param {cv_param.name or id!r} ({id}) has a non-integer value {cv_param.value!r}"
        ) from e

get_user_param

get_user_param(name: str) -> UserParam | None

Get a userParam by name.

Source code in src/mzmlpy/elems/dtree_wrapper.py
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
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
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
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)