Mass Tolerances

Helpers for ppm errors, Da <-> ppm conversion and tolerance windows. Every function is also importable from tacular. Units are the lowercase strings "da" and "ppm"; anything else raises TacularError. A ppm tolerance is relative to the absolute value of the reference mass, so windows around negative masses are symmetric.

Mass tolerance helpers: ppm errors, Da <-> ppm conversion, and tolerance windows.

Units are the lowercase strings "da" and "ppm". Anything else raises TacularError. A ppm tolerance is relative to abs(mass), so windows around negative masses (for example, loss deltas) are well formed and symmetric.

>>> from tacular import ppm_error, tolerance_window, within_tolerance
>>> ppm_error(1000.01, 1000.0)
9.999999999990905
>>> tolerance_window(1000.0, 10, tolerance_unit="ppm")
(999.99, 1000.01)
>>> within_tolerance(1000.005, 1000.0, 10, tolerance_unit="ppm")
True
tacular.tolerance.ToleranceUnit

"da" (daltons, absolute) or "ppm" (parts per million, relative).

Type:

A tolerance unit

alias of Literal[‘da’, ‘ppm’]

tacular.tolerance.da_to_ppm(delta, mz)[source]

Convert a mass difference delta in Da to ppm of mz: delta / abs(mz) * 1e6.

tacular divides by abs(mz), so the sign of the result is the sign of delta even for a negative mz (spxtacular’s da_to_ppm divides by the signed mz).

Raises:

TacularError – if mz is 0.

Return type:

float

Parameters:
  • delta (float)

  • mz (float)

tacular.tolerance.ppm_error(observed, theoretical)[source]

The signed error of observed in ppm of theoretical: (observed - theoretical) / abs(theoretical) * 1e6.

Raises:

TacularError – if theoretical is 0.

Return type:

float

Parameters:
  • observed (float)

  • theoretical (float)

tacular.tolerance.ppm_to_da(delta_ppm, mz)[source]

Convert a mass difference delta_ppm in ppm of mz to Da: delta_ppm * abs(mz) / 1e6.

As in da_to_ppm(), a negative mz does not flip the sign.

Return type:

float

Parameters:
  • delta_ppm (float)

  • mz (float)

tacular.tolerance.tolerance_window(mass, tolerance, *, tolerance_unit='da')[source]

The (lo, hi) bounds of a tolerance window centred on mass.

tolerance_unit="da" reads tolerance in Da; tolerance_unit="ppm" in ppm of abs(mass). A negative tolerance gives an empty window (lo > hi).

Raises:

TacularError – if tolerance_unit is not "da" or "ppm", or mass or tolerance is NaN or infinite.

Return type:

tuple[float, float]

Parameters:
  • mass (float)

  • tolerance (float)

  • tolerance_unit (Literal['da', 'ppm'])

tacular.tolerance.within_tolerance(observed, theoretical, tolerance, *, tolerance_unit='da')[source]

Whether observed lies in tolerance_window(theoretical, tolerance, tolerance_unit=tolerance_unit), bounds included: lo <= observed <= hi.

tolerance_unit="ppm" reads tolerance in ppm of abs(theoretical). Testing against the window’s bounds (not abs(observed - theoretical) <= width) keeps the two functions consistent under float rounding: a value exactly at lo or hi is within.

Raises:

TacularError – if tolerance_unit is not "da" or "ppm", or any number is NaN or infinite.

Return type:

bool

Parameters:
  • observed (float)

  • theoretical (float)

  • tolerance (float)

  • tolerance_unit (Literal['da', 'ppm'])