Search code examples
pythonbaseline

Python: how to linearly baseline each peak in a set of peaks


I need help writing code that would allow me to baseline each peak in a set of peaks (enthalpy vs. time isothermal titration calorimetry data).

The data is created by the ITC instrument in this fashion (where '@#' signifies the start of a peak and the data are listed below as time [seconds], enthalpy [ucal/s], and temperature [deg. C but unnecessary as it is usually held constant]):

@0
2.00,13.585249,25.00761
4.00,13.585438,25.00699
6.00,13.585557,25.00688
8.00,13.585472,25.00804
@1,6.0000
302.00,13.607173,25.00958
304.00,13.607608,25.00931
306.00,13.607758,25.00965

There are well over 100 points per peak (I've shortened it above), and I'd like to incorporate a linear equation to zero each enthalpy value in each peak so I may integrate each peak to produce a binding plot. I'd welcome any help/advice; thank you!


Solution

  • I was able to do it. Thank you to those who replied! I will leave this here to anyone who may need to baseline peaks with a linear fit in the future (assuming the first point and last 40 points will suffice in a decent fit line like it does in ITC):

    #defining function to calculate baseline of peaks in x vs y graph
    def calc_baseline(x,y):   
        zeroed_y=[]   
        for n in range(len(y)): 
            line_y=array(y[n][0:1]+y[n][-41:-1])
            line_x=array(x[n][0:1]+x[n][-41:-1])
            p=scipy.polyfit(baseline_x,baseline_y,1)
            baseline_y=array(x[n])*p[0]+p[1]
            zeroed_y.append(baseline_y)
        return zeroed_y
    
    #defining function to zero baselines of peaks in x vs y graph, assuming number_injections is a known integer
    def zero_baseline(number_injections,y,zeroed_y):
        zeroed_y_lists=[]
        for i in range(0,number_injections+1):
            zeroed_y=y[i]-zeroed_y[i]
            zeroed_y_lists.append(zeroed_y)
        return zeroed_y_lists