class apav.analysis.background.Background(fit_interval, include_interval=(), model=<lmfit.Model: Model(power_law)>)[source]

Defines a background model through a specified range of a mass spectrum.

A background is fit on the provided fit_interval(s). Intervals provided to include_interval define ranges where mass spectrum ranges will use the background model for background subtraction. For example:

>>> bkg = Background((10, 12), (13, 20), models.PowerLawShiftModel())

Fits a shifted power law on the interval from 10 -> 12 Da. The background will be subtracted and applied to any mass ranges that begin in the interval from 13 -> 20 Da.

>>> bkg = Background([(10, 12), (15, 16)], [(13, 20), (22, 24)], models.PowerLawShiftModel())

Will fit the same power law model on 2 intervals from 10 -> 12 and 15 -> 16 Da. This background will be subtracted and applied to any mass ranges that begin in either of the intervals 13 ->20 Da or 22 -> 24 Da. This allows simple control over which background applies to which mass ranges, without being too explicit.

  • fit_interval (Union[Tuple[Number, Number], Sequence[Tuple[Number, Number]]]) – single or multiple intervals on the x to fit the background model

  • include_interval (Union[Tuple[Number, Number], Sequence[Tuple[Number, Number]]]) – mass ranges to apply the background subtraction to

  • model (Type[Model]) – the background model to be used


Add fit interval to the background :type minmax: Tuple[Number, Number] :param minmax: the new fit interval


Add a specified interval through which any containing mass ranges will be matched. This will occur if the lower bound of a given mass range is contained within rng. Overlapping ranges are allowed.

property area: Number

Get the area of the fit


Determine if this background contains the mass/charge ratio in any include range interval [min-max)

Return type:



Determine if a Range falls within this Background’s include range :type rng: Range :param rng: Range instance

Return type:



Compute the background curve on the given interval x :rtype: ndarray :return: the background estimate on the given range

fit(spec_x, spec_y)[source]

Fit the background to the spectrum (spec_x, spec_y) :type spec_x: ndarray :param spec_x: array of x axis :type spec_y: ndarray :param spec_y: array of y axis

property fit_intervals: List[Tuple[Number, Number]]

Get the fit intervals assigned to this background

property fit_results: MinimizerResult

Get the fit results of the background

property include_intervals: List[Tuple[Number, Number]]

Get the list of intervals that assign ranges (Range) to the background

property lower: Number

Get the lower value of the fit interval


Clear the current background fit and data

property upper: Number

Get the upper value of the fit interval


class apav.analysis.background.BackgroundCollection(backgrounds=())[source]

Handles operate on a collection of Backgrounds

This class is used to contain all backgrounds that operate on a spectrum/ROI.

>>> bkgs = BackgroundCollection()
>>> bkg1 = Background((10, 12), (13, 14))
>>> bkgs.add(bkg1)


>>> bkgs = BackgroundCollection()
>>> bkgs.add(Background((10, 12), (13, 14)), models.ExponentialModel)
>>> bkgs.add(Background((48, 52), (60, 65)), models.PowerLawShiftModel)

This will fit the first background model on the interval 10 -> 12 Da and apply to mass ranges begining from 13 -> 14 Da using an exponential decay model. The second background is fit from 48 -> 52 Da and is applied to mass ranges beginning between 60 -> 65 Da, using a Power Law model.


Add a new Background to the collection :type newbkg: Background :param newbkg: the new background

Return type:


property backgrounds: List[Background]

Get a list of backgrounds (Background) in the collection :return:


Save background information to file for reuse


Find a background whos included mass ranges intersects a Range’s mass range. If no background is found return None

fit(spec_x, spec_y)[source]

Do the calculation of each background with respect the provided mass spectrum :type spec_x: ndarray :param spec_x: array of x axis :type spec_y: ndarray :param spec_y: array of y axis

classmethod from_bkg(fpath)[source]

Load background information from file (see BackgroundCollection.export)


Clear the calculated data for each background


Sort the BackgroundCollection in-place


Get a sorted copy of the BackgroundCollection

Return type: