Quick example to fit a baseline


In [1]:
%matplotlib inline
import numpy as np
import scipy
from matplotlib import pyplot as plt

import rampy
from sklearn import preprocessing

Creating a signal


In [2]:
nb_points  =500
x = np.linspace(50, 500, nb_points)

# gaussian peaks
p1 = 20.0 * np.exp(-np.log(2) * ((x-150.0)/15.0)**2)
p2 = 100.0 * np.exp(-np.log(2) * ((x-250.0)/5.0)**2)
p3 = 50.0 * np.exp(-np.log(2) * ((x-450.0)/1.0)**2)
p4 = 20.0 * np.exp(-np.log(2) * ((x-350.0)/30.0)**2)
p5 = 30.0 * np.exp(-np.log(2) * ((x-460.0)/5.0)**2)

# background: a large gaussian + linear 
bkg = 60.0 * np.exp(-np.log(2) * ((x-250.0)/200.0)**2) + 0.1*x

#noise
noise = 2.0 * np.random.normal(size=nb_points)

#observation
y = p1 + p2 + p3 + p4 + p5 + noise +bkg

plt.plot(x,y,"r-")


Out[2]:
[<matplotlib.lines.Line2D at 0x7f0520258250>]

Now using different baselines to retrieve the true background


In [3]:
# need to define some fitting regions for the spline
roi = np.array([[0,100],[200,220],[280, 290],[420,430],[480,500]])

# calculating the baselines
ycalc_poly, base_poly = rampy.baseline(x,y,roi,'poly',polynomial_order=3 )
#ycalc_gcvspl, base_gcvspl = rampy.baseline(x,y,roi,'gcvspline',s=0.1 ) # activate if you have installed gcvspline
ycalc_uni, base_uni = rampy.baseline(x,y,roi,'unispline',s=1e0)
ycalc_als, base_als = rampy.baseline(x,y,roi,'als',lam=10**7,p=0.05)
ycalc_arpls, base_arpsl = rampy.baseline(x,y,roi,'arPLS',lam=10**7,ratio=0.001)
ycalc_drpls, base_drpsl = rampy.baseline(x,y,roi,'drPLS')

# doing the figure
plt.figure(figsize=(10,10))
plt.plot(x,y,"k-",label="Raw")
plt.plot(x,bkg,"r-",label="Bkg")

plt.plot(x,base_poly,"-",color="grey",label="polynomial")
plt.plot(x,base_uni,"b-",label="unispline baseline")
#plot(x,base_gcvspl,"-",color="orange",label="gcvspline baseline") # activate if you have installed gcvspline
plt.plot(x,base_als,":",color="purple",label="als baseline")
plt.plot(x,base_arpsl,"-.",color="cyan",label="arPLS baseline")
plt.plot(x,base_drpsl,"--",color="cyan",label="drPLS baseline")

plt.xlabel("X")
plt.ylabel("Y")

plt.legend()


Out[3]:
<matplotlib.legend.Legend at 0x7f051c992110>

In [4]:
help(rampy.baseline)


Help on function baseline in module rampy.baseline:

baseline(x_input, y_input, bir, method, **kwargs)
    Allows subtracting a baseline under a x y spectrum.
    
    Parameters
    ----------
    x_input : ndarray
        x values.
    y_input : ndarray
        y values.
    bir : ndarray
        Contain the regions of interest, organised per line.
        For instance, roi = np.array([[100., 200.],[500.,600.]]) will
        define roi between 100 and 200 as well as between 500 and 600.
        Note: This is NOT used by the "als" and "arPLS" algorithms, but still is a requirement when calling the function.
        bir and method probably will become args in a futur iteration of rampy to solve this.
    method : str
        "poly": polynomial fitting, with splinesmooth the degree of the polynomial.
        "unispline": spline with the UnivariateSpline function of Scipy, splinesmooth is
                     the spline smoothing factor (assume equal weight in the present case);
        "gcvspline": spline with the gcvspl.f algorythm, really robust.
                     Spectra must have x, y, ese in it, and splinesmooth is the smoothing factor;
                     For gcvspline, if ese are not provided we assume ese = sqrt(y).
                     Requires the installation of gcvspline with a "pip install gcvspline" call prior to use;
        "exp": exponential background;
        "log": logarythmic background;
        "rubberband": rubberband baseline fitting;
        "als": (automatic) baseline least square fitting following Eilers and Boelens 2005;
        "arPLS": (automatic) Baseline correction using asymmetrically reweighted penalized least squares smoothing. Baek et al. 2015, Analyst 140: 250-257;
        'drPLS': (automatic) Baseline correction method based on doubly reweighted penalized least squares. Xu et al., Applied Optics 58(14):3913-3920.
    
    kwargs
    ------
    polynomial_order : Int
        The degree of the polynomial (0 for a constant), default = 1.
    s : Float
        spline smoothing coefficient for the unispline and gcvspline algorithms.
    lam : Float
        float, the lambda smoothness parameter for the ALS, ArPLS and drPLS algorithms. Typical values are between 10**2 to 10**9, default = 10**5 for ALS and ArPLS and default = 10**6 for drPLS.
    p : Float
        float, for the ALS algorithm, advised value between 0.001 to 0.1, default = 0.01.
    ratio : float
        ratio parameter of the arPLS and drPLS algorithm. default = 0.01 for arPLS and 0.001 for drPLS.
    niter : Int
        number of iteration of the ALS and drPLS algorithm, default = 10 for ALS and default = 100 for drPLS.
    eta : Float
        roughness parameter for the drPLS algorithm, is between 0 and 1, default = 0.5
    p0_exp : List
        containg the starting parameter for the exp baseline fit with curve_fit. Default = [1.,1.,1.].
    p0_log : List
        containg the starting parameter for the log baseline fit with curve_fit. Default = [1.,1.,1.,1.].
    
    Returns
    -------
    out1 : ndarray
        Contain the corrected signal.
    out2 : ndarray
        Contain the baseline.


In [ ]: