示例#1
0
def plot_fits():
    params_dict = {'c0_to_c1_k': 2e-2,
                   'c1_scaling': 20.,
                   'c1_to_c2_k': 1e-4,
                   'c2_scaling': 6.,
                   'c1_to_c2_k': 1e-3,
                   'c3_scaling': 0.5}

    builder = Builder(params_dict=params_dict)
    builder.build_model_multiconf(3, data_dict['c126_nbd'][0],
                                  normalized_data=True,
                                  reversible=False)

    """
    k1 = builder.model.parameters['c0_to_c1_k']
    k2 = builder.model.parameters['c1_to_c2_k']
    k1_index = builder.model.parameters.index(k1)
    k2_index = builder.model.parameters.index(k2)
    k1_est_index = builder.estimate_params.index(k1)
    k2_est_index = builder.estimate_params.index(k2)
    """

    pysb_fit = fitting.fit_pysb_builder(builder, 'NBD', data_dict['c126_nbd_time'],
                                        data_dict['c126_nbd'])

    plt.figure()
    plt.plot(data_dict['c126_nbd_time'], data_dict['c126_nbd'], linestyle='', marker='.')
    plt.plot(data_dict['c126_nbd_time'], pysb_fit.ypred)
    plt.xlabel('Time (sec)')
def test_2conf_irrev_formula():
    params_dict = {'c1_scaling': 5}
    bd = Builder(params_dict=params_dict)
    bd.build_model_multiconf(2, 1., reversible=False, normalized_data=True)
    t = np.linspace(0, 4000, 100)
    sol = Solver(bd.model, t)
    sol.run()
    nbd_sol = sol.yexpr['NBD']
    nbd_func = bd.obs_func(t)
    ok_(np.allclose(nbd_sol, nbd_func),
        'Integrated NBD does not match closed form NBD for 2conf model')
def test_4conf_irrev_formula_k1_k2_k3_same():
    """Doesn't test all possibilities."""
    params_dict = {'c1_scaling': 8,
                   'c2_scaling': 2,
                   'c3_scaling': 4,
                   'c0_to_c1_k': 0.05,
                   'c1_to_c2_k': 0.05,
                   'c2_to_c3_k': 0.001
            }
    bd = Builder(params_dict=params_dict)
    bd.build_model_multiconf(4, 1., reversible=False, normalized_data=True)
    t = np.linspace(0, 4000, 100)
    bd.obs_func(t)
def test_3conf_irrev_formula_k1_k2_different():
    params_dict = {'c1_scaling': 8,
                   'c2_scaling': 2,
                   'c0_to_c1_k': 0.05,
                   'c1_to_c2_k': 0.005
            }
    bd = Builder(params_dict=params_dict)
    bd.build_model_multiconf(3, 1., reversible=False, normalized_data=True)
    t = np.linspace(0, 4000, 100)
    sol = Solver(bd.model, t)
    sol.run()
    nbd_sol = sol.yexpr['NBD']
    nbd_func = bd.obs_func(t)
    ok_(np.allclose(nbd_sol, nbd_func),
        'Integrated NBD does not match closed form NBD for 3conf model')
def test_5conf_irrev_formula_k1_k2_k3_k4_different():
    params_dict = {'c1_scaling': 8,
                   'c2_scaling': 2,
                   'c3_scaling': 6,
                   'c4_scaling': 1.5,
                   'c0_to_c1_k': 0.05,
                   'c1_to_c2_k': 0.005,
                   'c2_to_c3_k': 0.001,
                   'c3_to_c4_k': 0.002,
            }
    bd = Builder(params_dict=params_dict)
    bd.build_model_multiconf(5, 1., reversible=False, normalized_data=True)
    t = np.linspace(0, 4000, 100)
    sol = Solver(bd.model, t)
    sol.run()
    nbd_sol = sol.yexpr['NBD']
    nbd_func = bd.obs_func(t)
    plt.ion()
    plt.figure()
    plt.plot(t, nbd_sol)
    plt.plot(t, nbd_func)
    ok_(np.allclose(nbd_sol, nbd_func),
        'Integrated NBD does not match closed form NBD for 5conf model')
示例#6
0
import sys
import pickle
import numpy as np
from matplotlib import pyplot as plt

from preprocess_data import data_to_fit, bg_time, lipo_concs_to_fit
from tbidbaxlipo.util import set_fig_params_for_publication, \
                             emcee_fit, format_axis
from pysb import *
from tbidbaxlipo.models.nbd.multiconf import Builder

bd = Builder()
bd.build_model_multiconf(num_confs=2, c0_scaling=1, normalized_data=True,
                        reversible=False)

def run_pt_mcmc(time, data, lipo_concs, filename=None):
    y = [data[-2]]
    bd.global_params = [bd['c1_scaling'], bd['c0_to_c1_k']]
    bd.local_params = []
    params = {}
    gf = emcee_fit.GlobalFit(bd, time, y, params, 'NBD')
    num_temps = 10
    num_walkers = 100
    burn_steps = 20
    sample_steps = 50
    sampler = emcee_fit.pt_mpi_sample(gf, num_temps, num_walkers, burn_steps,
                                      sample_steps)
    # Get rid of the pool so that the sampler can be pickled
    sampler.pool = None
    if filename is not None:
        with open(filename, 'w') as f:
def plot_2conf_fits(activator):
    plt.ion()
    #nbd_sites = ['15']
    nbd_sites = ['3', '5', '15', '36', '47', '54', '62', '68', '79', '120',
                 '122', '126', '138', '151', '175', '179', '184', '188']
    replicates = range(1, 4)
    count = 0
    fit_results = []

    plt.figure("Fitted k1")
    for nbd_index, nbd_site in enumerate(nbd_sites):
        k1_means = []
        k1_sds = []

        for rep_index in replicates:
            nt = df[(activator, 'NBD', nbd_site, rep_index, 'TIME')].values
            ny = df[(activator, 'NBD', nbd_site, rep_index, 'VALUE')].values

            plt.figure('%s, NBD-%s-Bax Fits' % (activator, nbd_site),
                       figsize=(6, 5))

            builder = Builder(params_dict=params_dict)
            builder.build_model_multiconf(2, ny[0], normalized_data=True)

            pysb_fit = fitting.fit_pysb_builder(builder, 'NBD', nt, ny)
            plt.plot(nt, ny, linestyle='', marker='.',
                    color=rep_colors[rep_index])
            plt.plot(nt, pysb_fit.ypred,
                     label='%s Rep %d' % (activator, rep_index),
                     color=rep_colors[rep_index])
            plt.xlabel('Time (sec)')
            plt.ylabel('$F/F_0$')
            plt.title('NBD $F/F_0$ fits, NBD-%s-Bax' % nbd_site)
            plt.legend(loc='lower right')
            # Calculate stderr of parameters (doesn't account for covariance)
            (k1_mean, k1_sd) = _mean_sd('c0_to_c1_k', builder, pysb_fit)
            k1_means.append(k1_mean)
            k1_sds.append(k1_sd)
            (c1_mean, c1_sd) = _mean_sd('c1_scaling', builder, pysb_fit)

            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c0_to_c1_k',
                                         k1_mean, k1_sd))
            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c1_scaling',
                                         c1_mean, c1_sd))
            count += 1

        plt.figure('%s, NBD-%s-Bax Fits' % (activator, nbd_site))
        plt.tight_layout()

        plt.figure("Fitted k1")
        plt.bar(range(nbd_index*4, (nbd_index*4) + 3), k1_means,
                yerr=k1_sds, width=1, color='r', ecolor='k')

    num_sites = len(nbd_sites)
    plt.figure("Fitted k1")
    plt.ylabel('Fitted k1 ($sec^{-1}$)')
    ax = plt.gca()
    ax.set_xticks(np.arange(1.5, 1.5 + num_sites * 4, 4))
    ax.set_xticklabels(nbd_sites)

    return fit_results
def plot_3conf_fits(activator):
    plt.ion()
    #nbd_sites = ['15']
    nbd_sites = ['3', '5', '15', '36', '47', '54', '62', '68', '79', '120',
                 '122', '126', '138', '151', '175', '179', '184', '188']
    replicates = range(1, 4)
    count = 0
    fit_results = []
    plt.figure("Fitted k1/k2")
    for nbd_index, nbd_site in enumerate(nbd_sites):
        k1_means = []
        k2_means = []
        k1_sds = []
        k2_sds = []

        for rep_index in replicates:
            rt = df[(activator, 'Release', nbd_site, rep_index, 'TIME')].values
            ry = df[(activator, 'Release', nbd_site, rep_index, 'VALUE')].values
            nt = df[(activator, 'NBD', nbd_site, rep_index, 'TIME')].values
            ny = df[(activator, 'NBD', nbd_site, rep_index, 'VALUE')].values

            """
            plt.figure()
            plt.plot(rt, ry)
            plt.figure()
            plt.plot(nt, ny)
            plt.figure()
            plt.plot(nt, ry / ny)

            ry_norm = (ry - np.min(ry)) / (np.max(ry) - np.min(ry))
            ny_norm = (ny - np.min(ny)) / (np.max(ny) - np.min(ny))

            plt.figure()
            plt.plot(rt, ry_norm, color='g')
            plt.plot(rt, ny_norm, color='b')
            """
            plt.figure('%s, NBD-%s-Bax Fits' % (activator, nbd_site),
                       figsize=(12, 5))
            plt.subplot(1, 2, 1)
            plt.plot(rt, ry,
                     linestyle='', marker='.',
                     color=rep_colors[rep_index])
            twoexp = tf.TwoExpLinear()
            #twoexp = tf.TwoExp()
            params = twoexp.fit_timecourse(rt, ry)
            plt.plot(rt, twoexp.fit_func(rt, params),
                     label='%s Rep %d' % (activator, rep_index),
                     color=rep_colors[rep_index])
            plt.xlabel('Time (sec)')
            plt.ylabel('% Tb release')
            plt.title('%% Tb release fits, NBD-%s-Bax' % nbd_site)
            plt.legend(loc='lower right')

            builder = Builder(params_dict=params_dict)
            builder.build_model_multiconf(3, ny[0], normalized_data=True)

            pysb_fit = fitting.fit_pysb_builder(builder, 'NBD', nt, ny)
            plt.subplot(1, 2, 2)
            plt.plot(nt, ny, linestyle='', marker='.',
                    color=rep_colors[rep_index])
            plt.plot(nt, pysb_fit.ypred,
                     label='%s Rep %d' % (activator, rep_index),
                     color=rep_colors[rep_index])
            plt.xlabel('Time (sec)')
            plt.ylabel('$F/F_0$')
            plt.title('NBD $F/F_0$ fits, NBD-%s-Bax' % nbd_site)
            plt.legend(loc='lower right')
            # Calculate stderr of parameters (doesn't account for covariance)
            (k1_mean, k1_sd) = _mean_sd('c0_to_c1_k', builder, pysb_fit)
            (k2_mean, k2_sd) = _mean_sd('c1_to_c2_k', builder, pysb_fit)
            (c1_mean, c1_sd) = _mean_sd('c1_scaling', builder, pysb_fit)
            (c2_mean, c2_sd) = _mean_sd('c2_scaling', builder, pysb_fit)

            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c0_to_c1_k',
                                         k1_mean, k1_sd))
            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c1_scaling',
                                         c1_mean, c1_sd))
            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c1_to_c2_k',
                                         k2_mean, k2_sd))
            fit_results.append(FitResult(builder, activator, nbd_site,
                                         rep_index, 'NBD', 'c2_scaling',
                                         c2_mean, c2_sd))

            k1_means.append(k1_mean)
            k2_means.append(k2_mean)
            k1_sds.append(k1_sd)
            k2_sds.append(k2_sd)

            """
            plt.figure()
            s = Solver(builder.model, nt)
            s.run(param_values=pysb_fit.params)
            plt.plot(nt, s.yobs['Bax_c0'])
            plt.plot(nt, s.yobs['Bax_c1'])
            plt.plot(nt, s.yobs['Bax_c2'])
            """
            count += 1
        #plt.title(nbd_site)
        #plt.xlabel('Time (sec)')
        #plt.ylabel('$F/F_0$')
        plt.figure('%s, NBD-%s-Bax Fits' % (activator, nbd_site))
        plt.tight_layout()

        plt.figure("Fitted k1/k2")
        plt.bar(range(nbd_index*7, (nbd_index*7) + 3), k1_means,
                yerr=k1_sds, width=1, color='r', ecolor='k')
        plt.bar(range(nbd_index*7+3, (nbd_index*7) + 6), k2_means,
                yerr=k2_sds, width=1, color='g', ecolor='k')

    num_sites = len(nbd_sites)
    plt.figure("Fitted k1/k2")
    plt.ylabel('Fitted k1/k2 ($sec^{-1}$)')
    ax = plt.gca()
    ax.set_xticks(np.arange(3, 3 + num_sites * 7, 7))
    ax.set_xticklabels(nbd_sites)

    return fit_results