# 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: `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 (`Number`) – 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

property event_extractor: MultipleEventExtractor

Get the MultipleEventExtractor used to filter the ion pairs

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))

property flip: bool

Whether the histogram was flipped (transposed)

property histogram: ndarray

Get the raw histogram

property multiplicity: Union[int, str]

Get the ion pair multiplicity

property pairs: ndarray

Get all pairs in correlation histogram

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:

## `RangedMassSpectrum`

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

Bases: `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 (`Number`) – 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: Number

Get the bin width in Da

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

property histogram: ndarray

Get the binned computed histogram

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

property percent: bool

Whether the quantification is in percentage or not (fraction)

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

property ranges: RangeCollection

Get the RangeCollection defining all mass ranges

property upper: Number

Get the upper limit of the calculated histogram

## `NoiseCorrectedMassSpectrum`

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

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:
property bin_width: Number

Get the bin width in Da

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

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: ndarray

Get the binned computed histogram

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

property noise_background: Background

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

property noise_corrected_histogram: ndarray

Get the noise corrected mass histogram as a numpy array

property noise_counts: float

Get the area of the noise fit

property noise_fit_data: ndarray

The fit to the random noise background

property percent: bool

Whether the quantification is in percentage or not (fraction)

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

property ranges: RangeCollection

Get the RangeCollection defining all mass ranges

property upper: Number

Get the upper limit of the calculated histogram

## `LocalBkgCorrectedMassSpectrum`

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

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:
property background_collection: BackgroundCollection

Get the `BackgroundCollection` applied to the computation

property bin_width: Number

Get the bin width in Da

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

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

Mass spectrum analysis from an array

property histogram: ndarray

Get the binned computed histogram

property multiplicity: Union[str, int]

Get the ion multiplicity user for quantification Returns:

property noise_background: Background

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

property noise_corrected_histogram: ndarray

Get the noise corrected mass histogram as a numpy array

property noise_counts: float

Get the area of the noise fit

property noise_fit_data: ndarray

The fit to the random noise background

property percent: bool

Whether the quantification is in percentage or not (fraction)

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

property ranges: RangeCollection

Get the RangeCollection defining all mass ranges

property show_warnings

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

property upper: Number

Get the upper limit of the calculated histogram