Skip to content
Extraits de code Groupes Projets
data_processing.py 9,1 ko
Newer Older
  • Learn to ignore specific revisions
  • import numpy as np
    from scipy.interpolate import interp1d
    import scipy.signal as signal
    from scipy.optimize import curve_fit
    
    def moving_median(x,window_length=3):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to perform a median filter on a array
        
            args:
                - x (array_like) : the input array.
                - window_length (scalar) : size of the median filter window.    
            return:
                - an array the same size as input containing the median filtered result.
        """
    
        return signal.medfilt(x,window_length)
    
    def smooth(x, window_length=10, polyorder=2):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to smooth an array by applying a Savitzky-Golay filter
        
            args:
                - x (array_like) : the data to be filtered.
                - window_length (scalar) : the length of the filter window .    
                - polyorder (scalar) : the order of the polynomial used to fit the samples. polyorder must be less than window_length.   
            return:
                - an array the same size as input containing the filtered result.
        """    
    
        return signal.savgol_filter(x,window_length,polyorder)
    
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
    
    
    def interpolate(x,y,x_interp,kind="cubic"):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to interpolate an 1-D array
        
            args:
                - x (array_like) : a 1-D array of real values.
                - y (array_like) : a 1-D array of real values of the same dimension of x.
                - x_interp (array_like) : a 1-D array of real values of the any dimension but with all values include between the max and min value of x.
                - kind (string or integer) : Specifies the kind of interpolation as a string specifying the order of the spline interpolator to use. The string has to be one of ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. 
            return:
                - an array the same size as x_interp containing the interpolated result.
        """    
    
        f=interp1d(x, y,kind=kind)    
        return f(x_interp)
    
    def remove_baseline(x,y,xmin_baseline,xmax_baseline,polyorder=2):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to remove the baseline a 1-D array
        
            args:
                - x (array_like) : a 1D input array.
                - y (array_like) : a 1D  input array with the same dimension of x.
                - xmin_baseline (scalar or array) : a scalar or array of values to set the minimum value of the data ranges on which the baseline is calculated. Several windows can be specified by specifying an array of values
                - xmax_baseline (scalar or array) : a scalar or array of values to set the maximum value of the data ranges on which the baseline is calculated. Several windows can be specified by specifying an array of values.
                - polyorder (scalar) : the order of the polynome to calculate the baseline    
            return:
                - a tuple (corrected_data, baseline) with the corrected data and the baseline calculated.
        """       
    
        index=[False]*len(x)
        for i in range(len(xmin_baseline)):
            index = index or ((x>=xmin_baseline[i]) & (x<=xmax_baseline[i]) )
        p=np.polyfit(x[index],y[index],deg=polyorder)
        baseline=np.polyval(p,x)
        return y-baseline, baseline
    
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
    def lorentzian(x,x0,A,W):
        """ Lorentzian function 
        
            args:
                - x (array_like) : a 1D input array.
                - x0 (scalar) : the position of the Lorentzian function
                - A (scalar) : the amplitude of the Lorentzian function
                - W (scalar) : the full width at half maximum (FWHM) of the Lorentzian function
            return:
                - an array of the same dimension as x
        """ 
        return A/(1+((x-x0)/(W/2))**2)
    
    def lorentzian2(x,x0,x01,A,A1,W,W1):
        """ Function with the sum of two Lorentzian 
        
            args:
                - x (array_like) : a 1D input array.
                - x01 (scalar) : the position of the first Lorentzian function
                - A1 (scalar) : the amplitude of the first Lorentzian function
                - W1 (scalar) : the full width at half maximum (FWHM) of the first Lorentzian function
                - x02 (scalar) : the position of the second Lorentzian function
                - A2 (scalar) : the amplitude of the second Lorentzian function
                - W2 (scalar) : the full width at half maximum (FWHM) of the second Lorentzian function
            return:
                - an array of the same dimension as x
        """ 
        return A/(1+((x-x0)/(W/2))**2)+A1/(1+((x-x01)/(W1/2))**2)
    
    
    def fit_lorentzian(x,y,xmin=None,xmax=None,x0=520.7,A0=1,W0=3):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to fit the data with a Lorentzian function 
        
            args:
                - x (array_like) : a 1D input array.
                - y (array_like) : a 1D  input array with the same dimension of x.
                - xmin (scalar) : a scalar or array of values to set the minimum value of the data range on which the Lorentzian is calculated. 
                - xmax (scalar) : a scalar or array of values to set the maximum value of the data range on which the Lorentzian is calculated. .
                - x0 (scalar) : the starting position of the Lorentzian function for the fit
                - A (scalar) : the starting amplitude of the Lorentzian function for the fit
                - W (scalar) : the starting full width at half maximum (FWHM) of the Lorentzian function for the fit
            return:
                - a tuple (parameter_dictionary, data_lorentzian) with the a dictionary containing the fitting parameters ("position", "amplitude" and "width") and the corresponding Lorentzian function evaluated in x.
        """
    
        if (xmin is None) and (xmax is None):
            x_fit=x
            y_fit=y
        else:
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
            index=(x>=xmin) & (x<=xmax) 
    
            x_fit=x[index]
            y_fit=y[index]
        p_lorentzian=curve_fit(lorentzian, x_fit, y_fit, p0=[x0,A0,W0])[0]
        return {"position":p_lorentzian[0],"amplitude":p_lorentzian[1],"width":p_lorentzian[2]}, lorentzian(x,*p_lorentzian)
    
    def fit_lorentzian_2peaks(x,y,xmin=None,xmax=None,x0=(520,520.7),A0=(1,1),W0=(3,3)):
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        """ Function to fit the data with a Lorentzian function 
        
            args:
                - x (array_like) : a 1D input array.
                - y (array_like) : a 1D  input array with the same dimension of x.
                - xmin (scalar) : a scalar or array of values to set the minimum value of the data range on which the Lorentzian is calculated. 
                - xmax (scalar) : a scalar or array of values to set the maximum value of the data range on which the Lorentzian is calculated. .
                - x0 (tuple) : the two starting position of the double Lorentzian function for the fit
                - A (tuple) : the two starting amplitude of the double Lorentzian function for the fit
                - W (tuple) : the two starting full width at half maximum (FWHM) of the double Lorentzian function for the fit
            return:
                - a tuple (parameter_dictionary, data_lorentzian) with the a dictionary containing the fitting parameters ("position", "amplitude" and "width") and the corresponding double Lorentzian function evaluated in x.
            """
    
        if (xmin is None) and (xmax is None):
            x_fit=x
            y_fit=y
        else:
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
            index=(x>=xmin) & (x<=xmax) 
    
            x_fit=x[index]
            y_fit=y[index]
        p_lorentzian=curve_fit(lorentzian2, x_fit, y_fit, p0=np.reshape([x0,A0,W0],newshape=6))[0]
        return {"position":p_lorentzian[:2],"amplitude":p_lorentzian[2:4],"width":p_lorentzian[4:]}, lorentzian(x,*p_lorentzian)
    
    
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
    def gaussian(x, x0, A, W):
        """ Faussian function 
        
            args:
                - x (array_like) : a 1D input array.
                - x0 (scalar) : the position of the Gaussian function
                - A (scalar) : the amplitude of the Lorentzian function
                - W (scalar) : the full width at half maximum (FWHM) of the Lorentzian function
            return:
                - an array of the same dimension od x
        """ 
        return A*np.exp(-np.power(x - x0, 2.) / (2 * np.power(W, 2.))) / (W * np.sqrt(2*np.pi))
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
    def fit_gaussian(x,y,xmin=None,xmax=None,x0=0,A=1,W=1):
        """ Function to fit the data with a Lorentzian function 
        
            args:
                - x (array_like) : a 1D input array.
                - y (array_like) : a 1D  input array with the same dimension of x.
                - xmin (scalar) : a scalar or array of values to set the minimum value of the data range on which the Gaussian is calculated. 
                - xmax (scalar) : a scalar or array of values to set the maximum value of the data range on which the Gaussian is calculated. .
                - x0 (scalar) : the starting position of the Gaussian function for the fit
                - A (scalar) : the starting amplitude of the Gaussian function for the fit
                - W (scalar) : the starting full width at half maximum (FWHM) of the Gaussian function for the fit
            return:
                - a tuple (parameter_dictionary, data_gaussian) with the a dictionary containing the fitting parameters ("position", "amplitude" and "width") and the corresponding Gaussian function evaluated in x.
        """
    
        if (xmin is None) and (xmax is None):
            x_fit=x
            y_fit=y
        else:
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
            index=(x>=xmin) & (x<=xmax) 
    
            x_fit=x[index]
            y_fit=y[index]
    
    
    Nicolas Roisin's avatar
    Nicolas Roisin a validé
        p_gaussian=curve_fit(gaussian, x_fit, y_fit, p0=[x0,A,W])[0]
        return {"position":p_gaussian[0],"width":p_gaussian[1],"amplitude":p_gaussian[2]}, gaussian(x,*p_gaussian)