Mass spectrum

CorrelationHistogram

class apav.analysis.massspectrum.CorrelationHistogram(roi, extents=((0, 200), (0, 200)), bin_width=0.1, multiplicity=2, symmetric=False, flip=False)[source]

Bases: apav.analysis.base.AnalysisBase

Statistical analysis of the correlation evaporation by ion pair histograms

Correlation histograms are computed by forming all ion pairs in given multiple-events. The correlation histogram may be computed using any multiplicity “order”, for example creating histograms from the 2nd and 5th order multiple events:

>>> roi = Roi.from_epos("path_to_epos_file.epos")
>>> corr_2 = CorrelationHistogram(roi, multiplicity=2)
>>> corr_5 = CorrelationHistogram(roi, multiplicity=5)

The ions from higher order multiplicities are separated into ion pair combinations. For example a 5th order multiple event composed of 5 ions, ABCDE is separated in the 10 ion pairs: AB AC AD AE BC BD BE CD CE DE. See the MultipleEventExtractor class for more detail.

The histogram may also be formed from the ion pairs for of all multiple events combined together. This is

achieved by passing the value “multiples” to the multiplicity keyword, indicating all ion pairs should be used:

>>> roi = Roi.from_epos("path_to_epos_file.epos")
>>> corr_2 = CorrelationHistogram(roi, multiplicity="multiples")
Parameters
  • roi (Roi) – region of interest

  • extents (Tuple[Tuple, Tuple]) – x and y extents for the mass_histogram to be calculated

  • bin_width (float) – bin width in daltons

  • multiplicity (Union[int, str]) – the multiplicity to compute the histogram with

  • symmetric (bool) – make the mass_histogram symmetric across the diagonal

  • flip (bool) – flip the histogram along its diagonal (i.e. swap the ion1/ion2 axes)

property bin_width: float

Get the bin width in Da

Return type

float

property event_extractor: apav.core.multipleevent.MultipleEventExtractor

Get the MultipleEventExtractor used to filter the ion pairs

Return type

MultipleEventExtractor

export(path)[source]

Export the histogram as a text image, which should processable in most other applications. :type path: str :param path: Filepath for the image

property extents: Tuple[Tuple[float, float], Tuple[float, float]]

Get the histogram boundaries as ((ion1_min, ion1_max), (ion2_min, ion2_max))

Return type

Tuple[Tuple[float, float], Tuple[float, float]]

property flip: bool

Whether or not the histogram was flipped (transposed)

Return type

bool

property histogram: numpy.ndarray

Get the raw histogram

Return type

ndarray

property multiplicity: Union[int, str]

Get the ion pair multiplicity

Return type

Union[int, str]

property pairs: numpy.ndarray

Get all pairs in correlation histogram

Return type

ndarray

plot()[source]

Interactively view the histogram. This function is for exploring the dataset interactively, while maintaining performance. Publication quality plots should be created in other software intended for plotting. See the export function for saving the histogram in a format suitable for conventional plotting.

property symmetric: bool

Whether or not the histogram is symmetrized Returns:

Return type

bool

RangedMassSpectrum

class apav.analysis.massspectrum.RangedMassSpectrum(roi, ranges, bin_width=0.01, decompose=False, percent=False, upper=200, multiplicity='all')[source]

Bases: apav.analysis.base.AnalysisBase

Uncorrected ranged mass spectrum analysis

This class is used for performing mass spectrum quantification on uncorrected mass histograms. No background subtractions are performed, see NoiseCorrectedMassSpectrum and LocalBkgCorrectedMassSpectrum for different levels of background correction.

This computation does not use any histogram to perform its computation (the composition), however, the histogram is computed for the sake of plotting the ranged mass spectrum in RangedMassSpectrum.plot() and can be accessed from RangedMassSpectrum.histogram

The mass ranges are provided as a RangeCollection instance to the ranges argument.

Parameters
  • roi (Roi) – Region of interest

  • ranges (RangeCollection) – RangeCollection defining mass ranges

  • decompose (bool) – Decompose polyatomic ions into their elemental components or not

  • percent (bool) – Return composition as percentage instead of fraction

  • upper (float) – The upper value for the mass spectrum

  • multiplicity (Union[int, str]) – Option to quantify specific multiple-hit events, either “all” for the all hits, int=1 for singles, int > 1 for specific multi-hits, or “multiples” for all multiples

property bin_width: float

Get the bin width in Da

Return type

float

counts_in_range(rng)[source]

Calculate the number of counts within a specified mass/charge range. This method should be overridden in subclasses since the calculated counts in a range is implementation-specific (i.e. background subtraction).

Return type

int

property decompose: bool

Whether or not molecular species are broken down into elemental forms

Return type

bool

property histogram: numpy.ndarray

Get the binned computed histogram

Return type

ndarray

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

Return type

Union[str, int]

property percent: bool

Whether the quantification is in percentage or not (fraction)

Return type

bool

plot()[source]

Get a plot to visualize the mass spectrum

print()[source]

Convenience to print the quantification results

property quant_dict: Dict[str, Tuple[float, float]]

Get a dictionary of the quantification results

The keys are a string of the range composition and the values are a tuple of composition % or fraction (depending on the percent kwarg) and the total counts in that range

Return type

Dict[str, Tuple[float, float]]

property ranges: apav.core.range.RangeCollection

Get the RangeCollection defining all mass ranges

Return type

RangeCollection

property upper: float

Get the upper limit of the calculated histogram

Return type

float

NoiseCorrectedMassSpectrum

class apav.analysis.massspectrum.NoiseCorrectedMassSpectrum(roi, ranges, noise_background=<apav.analysis.background.Background object>, **kwargs)[source]

Bases: apav.analysis.massspectrum.RangedMassSpectrum

Ranged mass spectrum analysis with correction for random noise background

This class performs mass spectrum quantification on mass histograms after removing background from random noise. I.e. this noise is corrected by fitting a background model to the initial portion of the mass spectrum, usual before 1 Dalton. The default model for the fit can be modified using the noise_background keyword argument.

Additional keyword arguments are passed to RangedMassSpectrum.

The quantification can be done on manually provided mass spectra instead of an Roi by using the alternate constructor NoiseCorrectedMassSpectrum.from_array().

Parameters
  • roi (Roi) – Region of interest

  • ranges (RangeCollection) – RangeCollection defining mass ranges

  • noise_background (Background) – Background defining the random noise background

  • **kwargs

    Keyword arguments passed to NoiseCorrectedMassSpectrum constructor

property bin_width: float

Get the bin width in Da

Return type

float

counts_in_range(rng)[source]

Calculate the corrected counts in a range on the mass spectrum. Overwritten from RangedMassSpectrum.counts_in_range().

Parameters

rng (Range) – Range instance for the computation

Return type

int

property decompose: bool

Whether or not molecular species are broken down into elemental forms

Return type

bool

classmethod from_array(x, y, ranges, decompose=False, percent=False)[source]

Create the NoiseCorrectedMassSpectrum from a numpy array (of a mass spectrum) instead of a Roi

property histogram: numpy.ndarray

Get the binned computed histogram

Return type

ndarray

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

Return type

Union[str, int]

property noise_background: apav.analysis.background.Background

Get the Background instance defining the noise background :rtype: Background :return:

property noise_corrected_histogram: numpy.ndarray

Get the noise corrected mass histogram as a numpy array

Return type

ndarray

property noise_counts: float

Get the area of the noise fit

Return type

float

property noise_fit_data: numpy.ndarray

The fit to the random noise background

Return type

ndarray

property percent: bool

Whether the quantification is in percentage or not (fraction)

Return type

bool

plot()[source]

Get a interactive plot of the noise corrected mass spectrum.

print()

Convenience to print the quantification results

property quant_dict: Dict[str, Tuple[float, float]]

Get a dictionary of the quantification results

The keys are a string of the range composition and the values are a tuple of composition % or fraction (depending on the percent kwarg) and the total counts in that range

Return type

Dict[str, Tuple[float, float]]

property ranges: apav.core.range.RangeCollection

Get the RangeCollection defining all mass ranges

Return type

RangeCollection

property upper: float

Get the upper limit of the calculated histogram

Return type

float

LocalBkgCorrectedMassSpectrum

class apav.analysis.massspectrum.LocalBkgCorrectedMassSpectrum(roi, ranges, local_background, show_warnings=False, **kwargs)[source]

Bases: apav.analysis.massspectrum.NoiseCorrectedMassSpectrum

Ranged mass spectrum analysis with correction for random noise and local background subtraction

This class performs mass spectrum quantification correcting for random noise background, as well as local background subtraction for isotopic peaks.

The random noise background subtraction is performed by NoiseCorrectedMassSpectrum and can be adjusted by the keyword arguments passed to it.

The local background subtractions are defined by the local_background parameter which is a BackgroundCollection instance. See the BackgroundCollection for detail. Generally, backgrounds are defined on a fit interval(s), and each background defines an interval(s) which determines the mass ranges it will apply to. For example:

>>> roi = Roi.from_pos("path_to_pos_ file.pos")
>>> ranges = RangeCollection.from_rng("path_to_range_file.rng")
>>> bkgs = BackgroundCollection()
>>> bkgs.add(Background((10, 12), [(14, 15), (20, 22)]))
>>> bkgs.add(Background([(32, 40), (42, 45)], (47, 50)))
>>> quant = LocalBkgCorrectedMassSpectrum(roi, ranges, bkgs)
>>> quant.print()

Does a local background subtracted quantification using 2 backgrounds fits and prints the result.

Parameters
  • roi (Roi) – Region of interest

  • ranges (RangeCollection) – RangeCollection defining mass ranges

  • local_background (BackgroundCollection) – BackgroundCollection defining background model

  • noise_background – Background defining the random noise background

  • disable_warnings – Disable warnings (such as when a range does has not been assigned a background)

  • **kwargs

    Keyword arguments passed to NoiseCorrectedMassSpectrum constructor

property background_collection: apav.analysis.background.BackgroundCollection

Get the BackgroundCollection applied to the computation

Return type

BackgroundCollection

property bin_width: float

Get the bin width in Da

Return type

float

counts_in_range(rng)[source]

Calculate the number of counts within a specified mass/charge range. Overwritten from NoiseCorrectedMassSpectrum.counts_in_range()

Return type

int

property decompose: bool

Whether or not molecular species are broken down into elemental forms

Return type

bool

classmethod from_array(x, y, ranges, background, decompose=False, percent=False)[source]

Mass spectrum analysis from an array

property histogram: numpy.ndarray

Get the binned computed histogram

Return type

ndarray

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

Return type

Union[str, int]

property noise_background: apav.analysis.background.Background

Get the Background instance defining the noise background :rtype: Background :return:

property noise_corrected_histogram: numpy.ndarray

Get the noise corrected mass histogram as a numpy array

Return type

ndarray

property noise_counts: float

Get the area of the noise fit

Return type

float

property noise_fit_data: numpy.ndarray

The fit to the random noise background

Return type

ndarray

property percent: bool

Whether the quantification is in percentage or not (fraction)

Return type

bool

plot()[source]

Get an interactive plot showing the background fits

print()

Convenience to print the quantification results

property quant_dict: Dict[str, Tuple[float, float]]

Get a dictionary of the quantification results

The keys are a string of the range composition and the values are a tuple of composition % or fraction (depending on the percent kwarg) and the total counts in that range

Return type

Dict[str, Tuple[float, float]]

property ranges: apav.core.range.RangeCollection

Get the RangeCollection defining all mass ranges

Return type

RangeCollection

property show_warnings

Whether or not to show warnings when there are ranges that are not background subtracted

property upper: float

Get the upper limit of the calculated histogram

Return type

float