def get_titration_fit(): model_y_vals = [] model_x_vals = np.linspace(0, 1.01*max(concs), 50) fitfunc = None if (not model == None): if (axis == 'Bax'): for conc in model_x_vals: model.parameters['tBid_0'].value = float(outer_conc_str) model.parameters['Bax_0'].value = conc model_y_vals.append(get_model_k0(model)) # Only do fitting if there is model object passed else: # Fitting if (fittype == 'linear'): # define fitting function m = Parameter(1) b = Parameter(0) def linear(x): return ((m()*x) + b()) fit(linear, [m, b], np.array(data_arr), np.array(concs)) #print(bid_conc_str + "nm cBid: k=" + str(k()) + ", n=" + str(n())) fitfunc = linear #(slope, intercept) = np.polyfit(log_concs, log_k0, 1) #k0_fit = np.polyval([slope, intercept], log_concs) #print "slope: ", slope, ", intercept: ", intercept #plot(log_concs, k0_fit, '-') elif (fittype == 'power'): # define fitting function k = Parameter(1) n = Parameter(0.4) def powerlaw(x): return (k()*(x**n())) fit(powerlaw, [k, n], np.array(data_arr), np.array(concs)) #print(bid_conc_str + "nm cBid: k=" + str(k()) + ", n=" + str(n())) fitfunc = powerlaw #(slope, intercept) = np.polyfit(log_concs, log_k0, 1) #k0_fit = np.polyval([slope, intercept], log_concs) print("exponent: %d" % n()) #plot(log_concs, k0_fit, '-') if (axis == 'Bax'): print(outer_conc_str + "nm cBid: exponent=" + str(n())) elif (fittype == 'hill' or fittype == 'hillexp'): # define fitting function km = Parameter(85) vmax = Parameter(0.0005) nh = Parameter(1) b = data_arr[0] def michaelis_menten(x): return \ ((vmax()*(x**nh()))/((km()**nh()) + (x**nh())) + b) #def line(x): return (m()*x)+b() # Perform the fit if (fittype == 'hillexp'): fit(michaelis_menten, [km, vmax, nh], np.array(data_arr), np.array(concs)) else: fit(michaelis_menten, [km, vmax], np.array(data_arr), np.array(concs)) if (axis == 'Bax'): kcat = vmax() / float(tbid_conc_str) print('%s nm cBid: Km=%f, Vmax=%f, kcat=%f, nh=%f' % (outer_conc_str, km(), vmax(), kcat(), nh())) fitfunc = michaelis_menten elif (fittype == None): pass else: raise Exception("Fitting function must be 'hill', " "'hillexp', 'power', or None") # Plot data data_marker = 's-' if fittype==None else 's' data_legend = outer_conc_str + " " + outer_axis if fittype==None \ else '_nolegend_' if (loglogplot): plt.loglog(concs, data_arr, data_marker + col, label=data_legend) rise1 = np.log(data_arr[2]) - np.log(data_arr[1]) run1 = np.log(concs[2]) - np.log(concs[1]) slope1 = rise1 / run1 #print "riserise #print "run = " + str(run) rise2 = np.log(data_arr[3]) - np.log(data_arr[2]) run2 = np.log(concs[3]) - np.log(concs[2]) slope2 = rise2 / run2 print(outer_conc_str + 'nm ' + outer_axis + ': Slope1,2=' + str(slope1) + ', ' + str(slope2)) # TODO else: plt.plot(concs, data_arr, data_marker + col, label=data_legend) # Plot fit if (not model == None): plt.plot(model_x_vals, model_y_vals, '-'+col, label=outer_conc_str + " " + outer_axis) if (fitfunc): # Show fit error mse_val = mse(fitfunc, np.array(data_arr), np.array(concs)) #print ("mse_val = " + str(mse_val)) total_mse += mse_val # TODO: Magic numbers 0 and 300 fit_x_vals = np.linspace(0, 1.01*max(concs), 50) fit_y_vals = map(fitfunc, fit_x_vals) if (loglogplot): plt.loglog(fit_x_vals, fit_y_vals, '-'+col, label=outer_conc_str + " " + outer_axis) else: plt.plot(fit_x_vals, fit_y_vals, '-'+col, label=outer_conc_str + " " + outer_axis)
def get_timecourse_fit(timecourse, fittype='biphasic'): """Get a fit curve for a timecourse. Parameters ---------- timecourse : pandas.Series The timecourse to be fit. fittype : string One of the following: * `singleexp`. Single exponential. * `biphasic`. Two-phase exponential (see Schwarz). * `explin`. Exponential plus linear term (default). * `doubleexp`. Sum of two exponentials * `expexp`. Exponential raised to an exponential. Returns ------- FitResult object Contains the (time, val) coordinates for the fitted curve as well as the mean squared error and parameter values. """ # Initial parameter guesses k = Parameter(0.0025) k2 = Parameter(0.00025) fmax = Parameter(4) fmax2 = Parameter(0.4) m = Parameter(0.01) #vi = Parameter( (timecourse.values[1]-timecourse.values[0])/900) vi = Parameter(0.0005) vf = Parameter(0.0005) # Based on a complete guess of 2500 sec for the half-life tau = Parameter(2.8e-4) # Define fitting function def biphasic(t): return (vf()*t) + ( (vi() - vf()) * ((1 - np.exp(-tau()*t))/tau()) ) def single_exp(t): return ((fmax()*(1 - np.exp(-k()*t)))) def exp_lin(t): return ((fmax()*(1 - np.exp(-k()*t))) + (m()*t)) def double_exp(t): return ((fmax()*(1 - np.exp(-k()*t))) + (fmax2()*(1 - np.exp(-k2()*t)))) def exp_exp(t): return ((fmax()*(1 - np.exp((1- np.exp(-k()*t)) )))) parameters = None # Run the fit if (fittype == 'biphasic'): fit(biphasic, [vi, vf, tau], timecourse.values, timecourse.keys().values) fitfunc = biphasic parameters = {'vi': vi(), 'vf': vf(), 'tau': tau()} elif (fittype == 'singleexp'): fit(single_exp, [k, fmax], timecourse.values, timecourse.keys().values) fitfunc = single_exp parameters = {'k': k(), 'fmax':fmax()} elif (fittype == 'explin'): fit(exp_lin, [k, fmax, m], timecourse.values, timecourse.keys().values) fitfunc = exp_lin parameters = {'k': k(), 'fmax':fmax(), 'm':m()} elif (fittype == 'doubleexp'): fit(double_exp, [k, fmax, k2, fmax2], timecourse.values, timecourse.keys().values) fitfunc = double_exp parameters = {'k': k(), 'fmax':fmax(), 'k2':k2(), 'fmax':fmax2()} elif (fittype == 'expexp'): fit(exp_exp, [k, fmax], timecourse.values, timecourse.keys().values) fitfunc = exp_exp parameters = {'k': k(), 'fmax':fmax()} else: raise Exception('unknown fit type') # Calculate the mean squared error of the fit mse_val = mse(fitfunc, timecourse.values, timecourse.keys().values) # Return time/value pairs for fit curve, along with the error fit_time = np.linspace(0, max(timecourse.keys().values), 200) fit_vals = map(fitfunc, fit_time) return FitResult(fit_time, fit_vals, mse_val, parameters)