def lamom2_s_fig(q0,
                 q1,
                 filenum,
                 eps=default_eps_lamom,
                 f=default_f_lamom,
                 a=default_a,
                 alpha=default_alpha,
                 beta=default_beta,
                 partype='s'):
    """
    two weakly coupled lambda-omega models, stochastic "slowly" varying parameter figure

    the model is simulated in this function. calls functions from lambda_omega.py

    filenum: seed
    """

    # initialize
    #filename = "trb2_psi_maxn_s"+str(filenum)+".dat"
    #filename = "trb2_psi_maxn_s1.dat"
    dt = .05

    noisefile = np.loadtxt("ounormed" + str(filenum) + "_mu1k.tab")
    total = noisefile[2]
    t = np.linspace(0, total, total / dt)
    initc = [2 / np.sqrt(2), 2 / np.sqrt(2), -2 / np.sqrt(2), 2 / np.sqrt(2)]

    # generate data for plots
    lcsolcoupled = euler.ESolve(lambda_omega.lamom_coupled,
                                initc,
                                t,
                                args=(a, alpha, beta, eps, q0, q1, f, dt,
                                      partype, noisefile))

    phi1init = np.arctan2(initc[1], initc[0])
    phi2init = np.arctan2(initc[3], initc[2])
    # compute Hodd
    # get theory phase
    phi_theory = euler.ESolve(lambda_omega.Hodd,
                              phi2init - phi1init,
                              t,
                              args=(a, alpha, beta, eps, q0, q1, f, dt,
                                    partype, noisefile))

    theta1 = np.arctan2(lcsolcoupled[:, 1], lcsolcoupled[:, 0])
    theta2 = np.arctan2(lcsolcoupled[:, 3], lcsolcoupled[:, 2])
    phi_exp = np.mod(theta2 - theta1 + np.pi, 2 * np.pi) - np.pi
    phi_theory = np.mod(phi_theory + np.pi, 2 * np.pi) - np.pi

    # create plot object
    fig = plt.figure()
    gs = gridspec.GridSpec(2, 3)
    #ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=2)
    #ax2 = plt.subplot2grid((3,3),(2,0),colspan=3)

    ax1 = plt.subplot(gs[:1, :])
    # bold tick labels

    ax1.set_yticks(np.arange(0, 0.5 + .125, .125) * 2 * np.pi)
    x_label = [r"$0$", r"$\pi/4$", r"$\pi/2$", r"$3\pi/4$", r"$\pi$"]
    ax1.set_yticklabels(x_label, fontsize=lamomfsize)

    #ytick_locs = np.arange(np.amin(phi_theory),np.amax(phi_theory),
    #                       (np.amax(phi_theory)-np.amin(phi_theory))/8.)
    #plt.yticks(ytick_locs, [r"$\mathbf{%1.1f}$" % x for x in ytick_locs])

    ax2 = plt.subplot(gs[1, :])
    #fig, axarr = plt.subplots(2, sharex=True)
    #axarr[0] = plt.subplot2grid(
    fig.set_size_inches(10, 7.5)
    #axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])

    # plot data+theory
    ax1.plot(t, phi_exp, lw=5, color="black")
    ax1.plot(t,
             phi_theory,
             lw=5,
             color="#3399ff",
             ls='dashdot',
             dashes=(10, 5))
    if q0 == .9:
        ax1.set_ylabel(r'$\bm{\phi(t)}$', fontsize=lamomfsize)

    #ax1.yaxis.set_major_locator(MultipleLocator(0.4))
    # make plot fit window
    #ax1.set_ylim(np.amin(full_model),0.3)#np.amax(full_model))
    #ax1.set_xlim(dat[:,0][0],dat[:,0][-1])
    ax1.set_xlim(0, total)
    ax1.set_ylim(-0.1, np.pi + 0.1)
    # plot s param
    q = q0 + (q1) * noisefile[3:]
    print 'mean =', np.mean(q), 'for seed=' + str(filenum)
    #ax2 = plt.subplots(2,1,1)
    #ax2 = ax1.twinx()
    s_N = len(noisefile[3:])
    s_N_half = s_N  #int(s_N/2.)

    ax2.plot(np.linspace(0, t[-1], s_N), q, lw=1, color="red")
    ax2.plot([t[0], t[-1]], [1, 1],
             lw=3,
             color='red',
             linestyle='--',
             dashes=(10, 2))
    #ax2.set_xlim(dat[:,0][0],dat[:,0][-1])
    if q0 == .9:
        ax2.set_ylabel(r'$\bm{q(t)}$', fontsize=lamomfsize, color='red')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')

    ax2.set_xlabel(r'$\bm{t}$', fontsize=lamomfsize)
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    ax2.yaxis.set_major_locator(MultipleLocator(0.4))
    ax2.xaxis.set_major_locator(MultipleLocator(4000))
    ax2.set_xlim(0, total)
    #xtick_locs = np.arange(t[0], t[-1], 2000,dtype='int')
    #minval=np.amin(q);maxval=np.amax(q)
    #ytick_locs = np.arange(minval,maxval,(maxval-minval)/8.)
    #plt.xticks(xtick_locs, [r"$\mathbf{%s}$" % x for x in xtick_locs])
    #plt.yticks(ytick_locs, [r"$\mathbf{%1.1f}$" % x for x in ytick_locs])

    #axes.set_xticks([])
    #axes.set_yticks([])
    #axes.set_frame_on(False)
    ax1.set_xticks([])
    #ax1.set_yticks([])
    #ax1.set_frame_on(False)
    ax1.tick_params(labelsize=lamomfsize, top='off', right='off')

    #ax2.set_xticks([])
    #ax2.set_yticks([])
    ax2.tick_params(labelsize=lamomfsize, top='off', right='off')
    ax2.set_frame_on(False)

    return fig
def trb2_p_fig(gm0=default_gm0,
               gm1=default_gm1,
               eps=default_eps,
               f=default_f,
               partype='p'):
    """
    two weakly coupled trab models, periodic slowly varying parameter figure
    data files created using trb2simple.ode and trb2simple_just1.ode
    """
    # initialize
    #filename = "trb2_psi_maxn_qp"+str(filenum)
    #filename = "trb2_psi_maxn_p1_ref.dat"

    filename = "trb2_psi_maxn_p1_ref2.dat"  # with reviewer's fix
    #filename = "trb2_psi_maxn_p1_refined_2tables.dat"

    dat = np.loadtxt(filename)
    psi0 = np.mean(dat[:, 1][:int(5 / .05)])
    T = dat[:, 0][-1]
    N = len(dat[:, 0])
    t = np.linspace(0, T, N)
    noisefile = None

    # generate data for plots
    sol = euler.ESolve(phase_model.happrox,
                       psi0,
                       t,
                       args=(gm0, gm1, f, eps, partype, noisefile))
    full_model = np.abs(np.mod(dat[:, 1] + .5, 1) -
                        .5)  # [0] to make regular row array
    slow_phs_model = np.abs(np.mod(sol + .5, 1) - .5)[:, 0]

    # create plot object
    fig, ax1 = plt.subplots()
    fig.set_size_inches(10, 5)

    ## plot data+theory
    ax1.scatter(dat[:, 0] / 1000.,
                full_model * 2 * np.pi,
                s=.5,
                facecolor="gray")
    ax1.plot(np.linspace(0, dat[:, 0][-1] / 1000., N),
             slow_phs_model * 2 * np.pi,
             lw=5,
             color="#3399ff")
    ax1.set_ylabel(r'$\bm{|\phi(t)|}$', fontsize=20)
    ax1.set_xlabel(r'$\bm{t (s)}$', fontsize=20)

    # set tick intervals
    myLocatorx = mticker.MultipleLocator(2000 / 1000.)
    #myLocatory = mticker.MultipleLocator(.5)
    ax1.xaxis.set_major_locator(myLocatorx)
    #ax1.yaxis.set_major_locator(myLocatory)

    # make plot fit window

    ax1.set_yticks(np.arange(0, 0.5, .125) * 2 * np.pi)
    x_label = [r"$0$", r"$\pi/4$", r"$\pi/2$", r"$3\pi/4$"]
    #x_label = [r"$0$", r"$\frac{\pi}{4}$", r"$\frac{\pi}{2}$", r"$\frac{3\pi}{4}$",   r"$\pi$"]
    ax1.set_yticklabels(x_label, fontsize=lamomfsize)

    ax1.set_ylim(
        np.amin([full_model]) * 2 * np.pi,
        np.amax([full_model]) * 2 * np.pi)
    ax1.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)

    ## plot P param
    ax2 = ax1.twinx()
    ax2.set_ylabel(r'$\bm{q(t)}$', fontsize=20, color='red')

    # slowly varying parameter
    gm = gm0 + (gm1 - gm0) * np.cos(eps * f * t)

    # set tick intervals
    myLocatory2 = mticker.MultipleLocator(.1)
    ax2.yaxis.set_major_locator(myLocatory2)

    # make param plot fit window
    ax2.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)
    ax2.set_ylim(np.amin(gm), np.amax(gm))

    # plot param + stability line
    ax2.plot(t / 1000., gm, lw=4, color="red", linestyle='--', dashes=(10, 2))
    ax2.plot([dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.], [0.3, 0.3],
             lw=2,
             color='red')

    # set ticks to red
    for tl in ax2.get_yticklabels():
        tl.set_color('r')

    # beautify
    ax1.tick_params(labelsize=20, top='off')
    ax1.tick_params(axis='x', pad=8)
    ax2.tick_params(labelsize=20, top='off')
    plt.gcf().subplots_adjust(bottom=0.15)

    return fig
def trb2newpar_p_fig(gm0=default_gm0,
                     gm1=default_gm1,
                     eps=default_eps,
                     f=default_f,
                     partype='p'):
    """
    two weakly coupled trab models, periodic slowly varying parameter figure, with parameters in interval [0.05,0.3]
    data files created using trb2_new_params/trb2simple_newpar.ode
    """

    # initialize
    # no more switch from stable/unstable. There always exists a stable point
    #filename = "trb2_new_params/trb2newpar_psi_p.dat" # no normalization by variance
    filename = "trb2_new_params/trb2newpar_psi_p2.dat"  # includes normalization by variance
    dat = np.loadtxt(filename)
    psi0 = np.mean(dat[:, 1][:int(5 / .05)])
    T = dat[:, 0][-1]
    N = len(dat[:, 0])
    dt = T / (1. * N)
    t = np.linspace(0, T, N)
    noisefile = None

    # generate data for plots
    sol = euler.ESolve(phase_model.happrox_newpar,
                       psi0,
                       t,
                       args=(gm0, gm1, f, eps, partype, noisefile))
    full_model = np.abs(np.mod(dat[:, 1] + .5, 1) -
                        .5)  # [0] to make regular row array
    slow_phs_model = np.abs(np.mod(sol + .5, 1) - .5)[:, 0]

    # create plot object
    fig, ax1 = plt.subplots()
    fig.set_size_inches(10, 5)
    #axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])

    ## plot data+theory
    ax1.scatter(dat[:, 0] / 1000.,
                full_model * 2 * np.pi,
                s=.5,
                facecolor="gray")
    ax1.plot(np.linspace(0, dat[:, 0][-1] / 1000., N),
             slow_phs_model * 2 * np.pi,
             lw=5,
             color="#3399ff")
    myLocatorx = mticker.MultipleLocator(2000 / 1000.)
    #myLocatory = mticker.MultipleLocator(.5)
    ax1.xaxis.set_major_locator(myLocatorx)
    #ax1.yaxis.set_major_locator(myLocatory)

    ax1.set_yticks(np.arange(0, 0.5 + .125, .125) * 2 * np.pi)
    x_label = [r"$0$", r"$\pi/4$", r"$\pi/2$", r"$3\pi/4$", r"$\pi"]
    #x_label = [r"$0$", r"$\frac{\pi}{4}$", r"$\frac{\pi}{2}$", r"$\frac{3\pi}{4}$",   r"$\pi$"]
    ax1.set_yticklabels(x_label, fontsize=lamomfsize)

    ax1.set_ylabel(r'$\bm{|\phi(t)|}$', fontsize=20)
    ax1.set_xlabel(r'$\bm{t (s)}$', fontsize=20)

    # make plot fit window
    ax1.set_ylim(
        np.amin([full_model]) * 2 * np.pi,
        np.amax(full_model) * 2 * np.pi)
    ax1.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)

    ## plot P param
    ax2 = ax1.twinx()
    gm = gm0 + (gm1 - gm0) * np.cos(eps * f * t)

    ax2.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)
    ax2.set_ylabel(r'$\bm{q(t)}$', fontsize=20, color='red')
    ax2.plot(t / 1000., gm, lw=4, color="red", linestyle='--', dashes=(10, 2))

    myLocatory2 = mticker.MultipleLocator(.05)
    ax2.yaxis.set_major_locator(myLocatory2)

    #ax2.plot([dat[:,0][0],dat[:,0][-1]],[0.3,0.3],lw=2,color='red')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')

    # beautify
    ax1.tick_params(labelsize=20, top='off')
    ax1.tick_params(axis='x', pad=8)
    ax2.tick_params(labelsize=20, top='off')
    plt.gcf().subplots_adjust(bottom=0.15)

    return fig
def trb2_s_fig(filenum=4,
               gm0=default_gm0,
               gm1=default_gm1,
               eps=default_eps,
               f=default_f,
               partype='s'):
    """
    two weakly coupled trab models, stochastic "slowly" varying parameter figure
    data files created using

    trb2simple.ode
    trb2simple_just1.ode
    generateou.ode
    """

    # initialize
    #filename = "trb2_psi_maxn_s"+str(filenum)+".dat"
    #filename = "trb2_psi_maxn_s1.dat"
    #filename = "trb2_psi_maxn_s"+str(filenum)+"_mu1k.dat"
    filename = "trb2_psi_maxn_s" + str(
        filenum) + "_mu1k2.dat"  # with reviewer edit
    dat = np.loadtxt(filename)
    psi0 = np.mean(dat[:, 1][:int(5 / .05)])
    T = dat[:, 0][-1]
    N = len(dat[:, 0])
    dt = T / (1. * N)
    t = np.linspace(0, T, N)
    #noisefile = np.loadtxt("ounormed"+str(filenum)+".tab")
    noisefile = np.loadtxt("ounormed" + str(filenum) + "_mu1k.tab")

    # generate data for plots
    sol = euler.ESolve(phase_model.happrox,
                       psi0,
                       t,
                       args=(gm0, gm1, f, eps, partype, noisefile))
    full_model = np.abs(np.mod(dat[:, 1] + .5, 1) -
                        .5)  # [0] to make regular row array
    slow_phs_model = np.abs(np.mod(sol + .5, 1) - .5)[:, 0]

    # create plot object
    fig = plt.figure()
    fig.set_size_inches(10, 7.5)

    gs = gridspec.GridSpec(2, 3)

    ax1 = plt.subplot(gs[:1, :])

    # plot data+theory
    ax1.scatter(dat[:, 0] / 1000.,
                full_model * 2 * np.pi,
                s=.5,
                facecolor="gray")
    ax1.plot(np.linspace(0, dat[:, 0][-1] / 1000., N),
             slow_phs_model * 2 * np.pi,
             lw=4,
             color="#3399ff")
    ax1.set_ylabel(r'$\bm{|\phi(t)|}$', fontsize=20)

    ax1.set_yticks(np.arange(0, 0.5 + .125, .125) * 2 * np.pi)
    x_label = [r"$0$", r"$\pi/4$", r"$\pi/2$", r"$3\pi/4$", r"$\pi$"]
    #x_label = [r"$0$", r"$\frac{\pi}{4}$", r"$\frac{\pi}{2}$", r"$\frac{3\pi}{4}$",   r"$\pi$"]
    ax1.set_yticklabels(x_label, fontsize=lamomfsize)

    # make plot fit window
    ax1.set_ylim(
        np.amin(full_model) * 2 * np.pi,
        np.amax(full_model) * 2 * np.pi)  #np.amax(full_model))
    ax1.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)

    #myLocatory = mticker.MultipleLocator(.5)
    #ax1.yaxis.set_major_locator(myLocatory)

    ## plot s param
    ax2 = plt.subplot(gs[1, :])

    s_N = len(noisefile[3:])

    ax2.plot(np.linspace(0, dat[:, 0][-1] / 1000., s_N),
             (gm0 + (gm1 - gm0) * noisefile[3:]),
             lw=1,
             color="red")
    ax2.plot([dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.], [0.3, 0.3],
             lw=3,
             color='red',
             linestyle='--',
             dashes=(10, 2))

    myLocatorx = mticker.MultipleLocator(2000 / 1000.)
    ax2.xaxis.set_major_locator(myLocatorx)

    ax2.set_xlim(dat[:, 0][0] / 1000., dat[:, 0][-1] / 1000.)
    ax2.set_ylabel(r'$\bm{q(t)}$', fontsize=20, color='red')

    myLocatory2 = mticker.MultipleLocator(.1)
    ax2.yaxis.set_major_locator(myLocatory2)

    ax2.set_xlabel(r'$\bm{t (s)}$', fontsize=20)
    for tl in ax2.get_yticklabels():
        tl.set_color('r')

    ax1.tick_params(labelsize=20, top='off', right='off')
    ax1.xaxis.set_ticklabels([])

    #ax2.set_xticks([])
    #ax2.set_yticks([])
    ax2.tick_params(labelsize=20, top='off', right='off')
    ax2.tick_params(axis='x', pad=8)
    ax2.set_frame_on(False)

    return fig
theta1 = np.loadtxt("trb2_new_params/trb2newpar_p_theta1.dat")
theta2 = np.loadtxt("trb2_new_params/trb2newpar_p_theta2.dat")

# slow param
eps = .0025
gm0 = .175
gm1 = .3
f = .5
gm = gm0 + (gm1 - gm0) * np.cos(eps * f * t)

# theoretical phase
# generate data for plots
partype = 'p'
noisefile = None
sol = euler.ESolve(phase_model.happrox_newpar,
                   psi0,
                   t,
                   args=(gm0, gm1, f, eps, partype, noisefile))
slow_phs_model = np.abs(np.mod(sol + .5, 1) - .5)[:, 0]

fig = plt.figure(figsize=(7, 7))

plt.ion()
#plt.show()

ax11 = plt.subplot2grid((2, 2), (0, 0))
ax11.set_title(r"\textbf{Oscillator 1}")
ax11.set_xlabel(r"\textbf{Voltage (mV)}", fontsize=15)
ax11.set_ylabel(r"$\mathbf{n}$", fontsize=15)
#ax11.plot(vlo,nlo)

ax12 = plt.subplot2grid((2, 2), (0, 1))