def nbd_fret_err(nbd_data, fret_data, plot=False, plot_title=None): """Utility function for calculating NBD and FRET data error.""" # Plot titles if plot_title: nbd_plot_title = plot_title + ' NBD' fret_plot_title = plot_title + ' Bid/Bax FRET' else: nbd_plot_title = None fret_plot_title = None # Calculate NBD err nbd_residuals, _ = calc_err_var(nbd_data, last_n_pts=80, plot=plot, plot_title=nbd_plot_title) nbd_err = np.std(nbd_residuals, ddof=1) # Calculate FRET err fret_residuals, _ = calc_err_var(fret_data, last_n_pts=80, plot=plot, plot_title=fret_plot_title) fret_err = np.std(fret_residuals, ddof=1) return (nbd_err, fret_err)
nbd_residue, rep_num, 'TIME')].values # The fitting procedure expects a three-dimensional array for # each entry; the first dimension is reserved for multiple # concentrations, as in a titration, which we don't have; the # second is reserved for multiple observables, and the third # contains the timepoints. data_var = np.zeros((1, len(observables), len(time_var))) # Iterate over the observables for obs_ix, obs in enumerate(observables): timecourse = \ df[(activator, obs, nbd_residue, rep_num, 'VALUE')].values data_var[0, obs_ix, :] = timecourse # Get the residuals for this observable/rep (residuals, fig) = \ calc_err_var(timecourse, last_n_pts=num_residuals, fit_type='cubic', plot=False) # Add the residuals for this rep to the list data_residuals[0, obs_ix, :] = residuals # NBD-specific variables if obs == 'NBD': # Initial fluorescence value f0 = timecourse[0] # Update maximum fluorescence value max_nbd_for_rep = np.max(timecourse) if max_nbd_for_rep > max_nbd_value: max_nbd_value = max_nbd_for_rep # Get the minimum fluorescence allowable for this rep: # 0.1 * the initial value lbound = f0 * 0.1 # Get the maximum allowable fluorescence for this rep: the
def find_outliers(values): (res, fig) = calc_err_var(values, last_n_pts=len(values), fit_type='two_exp_sum', plot=False) zscores = mod_zscore(res) return filtered(values, zscores, 3.5)
bgsub_wells = subtract_background_set(nbd_wells, bax_bg_wells) # Background subtracted, averaged #(bgsub_averages, bgsub_stds) = averages(bgsub_wells, layout) # First timepoint shifted to 0 (better for fitting) #reset_bgsub_means = reset_first_timepoint_to_zero(bgsub_norm_averages) #Timecourses normalized, BG-subtracted, averaged, then with first point #shifted to t = 0. #reset_bgsub_sds = reset_first_timepoint_to_zero(bgsub_norm_stds) # Get the time vector time = bgsub_wells['Bax 0 nM, NBD-Bax 96 nM'][TIME] # Initialize numpy data matrix data_to_fit = np.zeros((len(bgsub_wells.keys()), 1, len(time))) # Initialize matrix of experimental error values data_sigma = np.zeros((len(bgsub_wells.keys()), 1)) conc_index = 0 # Normalize data by the initial values bax_concs_to_fit = [] for conc_name in bgsub_wells.keys(): bax_concs_to_fit.append(float(conc_name.split(' ')[1])) y = bgsub_wells[conc_name][VALUE] y = y / np.mean(y[0:2]) data_to_fit[conc_index, 0, :] = y (residuals, fig) = calc_err_var(y, last_n_pts=200, fit_type='quadratic', plot=False) data_sigma[conc_index, 0] = np.std(residuals, ddof=1) conc_index += 1 nbd_bax_conc = 96.
if max_nbd_for_rep > max_nbd_value: max_nbd_value = max_nbd_for_rep # Get the minimum fluorescence allowable for this rep: 0.1 * the # initial value lbound = f0 * 0.1 # Get the maximum allowable fluorescence for this rep: the lesser # of 10 times the initial value or a corrected absolute NBD # fluorescence of 300k max_nbd_fluorescence = 300000 mutant_absolute_max = \ labeling_ratios[nbd_residue] * max_nbd_fluorescence mutant_relative_max = 10 * f0 ubound = min(mutant_absolute_max, mutant_relative_max) # Get the residuals for this rep (residuals, fig) = \ calc_err_var(timecourse, last_n_pts=50, fit_type='cubic', plot=False) # Add the residuals for this rep to the list residuals_reps.append(residuals) # Variable names time_var_name = 'time_%s_%s_%s_r%s' % \ (activator, obs, nbd_residue, rep_num) data_var_name = 'data_%s_%s_%s_r%s' % \ (activator, obs, nbd_residue, rep_num) # Upper and lower bounds for fluorescence parameters lbound_name = data_var_name + '_lbound' ubound_name = data_var_name + '_ubound' f0_name = data_var_name + '_f0' # Add these as module-level variables setattr(this_module, time_var_name, time_var) setattr(this_module, data_var_name, data_var) setattr(this_module, lbound_name, lbound)
# Matrices to use in fitting time_54 = df_pre[('Bid', 'FRET', '54', 1, 'TIME')].values data_54 = np.zeros((1, 2, len(time_54))) data_54[0, 0, :] = df_pre[('Bid', 'NBD', '54', 1, 'VALUE')].values data_54[0, 1, :] = df_pre[('Bid', 'FRET', '54', 1, 'VALUE')].values time_126 = df_pre[('Bid', 'FRET', '126', 1, 'TIME')].values data_126 = np.zeros((1, 2, len(time_126))) data_126[0, 0, :] = df_pre[('Bid', 'NBD', '126', 1, 'VALUE')].values data_126[0, 1, :] = df_pre[('Bid', 'FRET', '126', 1, 'VALUE')].values # Calculate the prior on standard error of the data by running polynomial # fits on the final points # 54 data_54_sigma = np.zeros((1, 2)) (residuals, _) = calc_err_var(data_54[0, 0, :], last_n_pts=80, fit_type='cubic', plot=False) data_54_sigma[0, 0] = np.std(residuals, ddof=1) (residuals, _) = calc_err_var(data_54[0, 1, :], last_n_pts=80, fit_type='cubic', plot=False) data_54_sigma[0, 1] = np.std(residuals, ddof=1) # 126 data_126_sigma = np.zeros((1, 2)) (residuals, _) = calc_err_var(data_126[0, 0, :], last_n_pts=80, fit_type='cubic', plot=False) data_126_sigma[0, 0] = np.std(residuals, ddof=1) (residuals, _) = calc_err_var(data_126[0, 1, :], last_n_pts=80, fit_type='cubic', plot=False) data_126_sigma[0, 1] = np.std(residuals, ddof=1)