def plot_impulse_responses(models, results):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((6,6))
    col = harvard_colors()
    plt.grid()

    y_max = 0

    for i, (model, result) in enumerate(zip(models, results)):
        smpl = result.samples[-1]
        W = smpl.W_effective
        if "continuous" in str(smpl.__class__).lower():
            t, irs = smpl.impulses

            for k1 in xrange(K):
                for k2 in xrange(K):
                    plt.subplot(K,K,k1*K + k2 + 1)
                    plt.plot(t, W[k1,k2] * irs[:,k1,k2], color=col[i], lw=2)
        else:
            irs = smpl.impulses
            for k1 in xrange(K):
                for k2 in xrange(K):
                    plt.subplot(K,K,k1*K + k2 + 1)
                    plt.plot(W[k1,k2] * irs[:,k1,k2], color=col[i], lw=2)

        y_max = max(y_max, (W*irs).max())

    for k1 in xrange(K):
        for k2 in xrange(K):
            plt.subplot(K,K,k1*K+k2+1)
            plt.ylim(0,y_max*1.05)
    plt.show()
def plot_prc_curves(precs, recalls, fig_path="./"):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors
    col = harvard_colors()

    fig = create_figure((3, 3))
    ax = fig.add_subplot(111)
    if "xcorr" in recalls:
        ax.plot(recalls['xcorr'],
                precs['xcorr'],
                color=col[7],
                lw=1.5,
                label="xcorr")
    if "bfgs" in recalls:
        ax.plot(recalls['bfgs'],
                precs['bfgs'],
                color=col[3],
                lw=1.5,
                label="MAP")
    if "svi" in recalls:
        ax.plot(recalls['svi'],
                precs['svi'],
                color=col[0],
                lw=1.5,
                label="SVI")
    ax.set_xlabel("Recall")
    ax.set_ylabel("Precision")

    plt.legend(loc=1)
    ax.set_title("Network %d" % net)
    plt.subplots_adjust(bottom=0.25, left=0.25)

    plt.savefig(os.path.join(os.path.dirname(fig_path), "figure3d.pdf"))
    plt.show()
def plot_roc_curves(fprs, tprs):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors
    col = harvard_colors()

    fig = create_figure((3,3))
    ax = fig.add_subplot(111)
    ax.plot(fprs['xcorr'], tprs['xcorr'], color=col[7], lw=1.5, label="xcorr")
    ax.plot(fprs['bfgs'], tprs['bfgs'], color=col[3], lw=1.5, label="Std.")
    ax.plot(fprs['svi'], tprs['svi'], color=col[0], lw=1.5, label="MAP")
    ax.plot([0,1], [0,1], '-k', lw=0.5)
    ax.set_xlabel("FPR")
    ax.set_ylabel("TPR")

    # this is another inset axes over the main axes
    parchment = np.array([243,243,241])/255.
    inset = plt.axes([0.55, 0.275, .265, .265], axisbg=parchment)
    inset.plot(fprs['xcorr'], tprs['xcorr'], color=col[7], lw=1.5,)
    inset.plot(fprs['bfgs'], tprs['bfgs'], color=col[3], lw=1.5,)
    inset.plot(fprs['svi'], tprs['svi'], color=col[0], lw=1.5, )
    inset.plot([0,1], [0,1], '-k', lw=0.5)
    plt.setp(inset, xlim=(0,.2), ylim=(0,.2), xticks=[0, 0.2], yticks=[0,0.2], aspect=1.0)
    inset.yaxis.tick_right()

    plt.legend(loc=4)
    ax.set_title("ROC Curve")

    plt.subplots_adjust(bottom=0.2, left=0.2)

    plt.savefig("figure3c.pdf")
    plt.show()
def plot_pred_ll_vs_time(models, results, burnin=0,
                         std_ll=np.nan,
                         true_ll=np.nan):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((4,3))
    ax = fig.add_subplot(111)
    col = harvard_colors()
    plt.grid()

    t_start = 0
    t_stop = 0

    for i, (model, result) in enumerate(zip(models, results)):
        plt.plot(result.timestamps[burnin:], result.test_lls[burnin:], lw=2, color=col[i], label=model)

        # Update time limits
        t_start = min(t_start, result.timestamps[burnin:].min())
        t_stop = max(t_stop, result.timestamps[burnin:].max())

    # plt.legend(loc="outside right")

    # Plot the standard Hawkes test ll
    plt.plot([t_start, t_stop], std_ll*np.ones(2), lw=2, color=col[len(models)], label="Std.")

    # Plot the true ll
    plt.plot([t_start, t_stop], true_ll*np.ones(2), '--k', lw=2, label="True")

    ax.set_xlim(t_start, t_stop)
    ax.set_xlabel("time [sec]")
    ax.set_ylabel("Pred. Log Lkhd.")
    plt.show()
def plot_impulse_responses(models, results):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((6, 6))
    col = harvard_colors()
    plt.grid()

    y_max = 0

    for i, (model, result) in enumerate(zip(models, results)):
        smpl = result.samples[-1]
        W = smpl.W_effective
        if "continuous" in str(smpl.__class__).lower():
            t, irs = smpl.impulses

            for k1 in range(K):
                for k2 in range(K):
                    plt.subplot(K, K, k1 * K + k2 + 1)
                    plt.plot(t, W[k1, k2] * irs[:, k1, k2], color=col[i], lw=2)
        else:
            irs = smpl.impulses
            for k1 in range(K):
                for k2 in range(K):
                    plt.subplot(K, K, k1 * K + k2 + 1)
                    plt.plot(W[k1, k2] * irs[:, k1, k2], color=col[i], lw=2)

        y_max = max(y_max, (W * irs).max())

    for k1 in range(K):
        for k2 in range(K):
            plt.subplot(K, K, k1 * K + k2 + 1)
            plt.ylim(0, y_max * 1.05)
    plt.show()
Exemple #6
0
def make_figure_a(S, F, C):
    """
    Plot fluorescence traces, filtered fluorescence, and spike times
    for three neurons
    """
    col = harvard_colors()
    dt = 0.02
    T_start = 0
    T_stop = 1 * 50 * 60
    t = dt * np.arange(T_start, T_stop)

    ks = [0, 1]
    nk = len(ks)
    fig = create_figure((3, 3))
    for ind, k in enumerate(ks):
        ax = fig.add_subplot(nk, 1, ind + 1)
        ax.plot(t, F[T_start:T_stop, k], color=col[1],
                label="$F$")  # Plot the raw flourescence in blue
        ax.plot(t,
                C[T_start:T_stop, k],
                color=col[0],
                lw=1.5,
                label="$\widehat{F}$")  # Plot the filtered flourescence in red
        spks = np.where(S[T_start:T_stop, k])[0]
        ax.plot(t[spks], C[spks, k], 'ko',
                label="S")  # Plot the spike times in black

        # Make a legend
        if ind == 0:
            # Put a legend above
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                       loc=3,
                       ncol=3,
                       mode="expand",
                       borderaxespad=0.,
                       prop={'size': 9})

        # Add labels
        ax.set_ylabel("$F_%d(t)$" % (k + 1))
        if ind == nk - 1:
            ax.set_xlabel("Time $t$ [sec]")

        # Format the ticks
        ax.set_ylim([-0.1, 1.0])
        plt.locator_params(nbins=5, axis="y")

    plt.subplots_adjust(left=0.2, bottom=0.2)
    fig.savefig("figure3a.pdf")
    plt.show()
def plot_pred_ll_vs_time(models,
                         results,
                         burnin=0,
                         std_ll=np.nan,
                         true_ll=np.nan):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((4, 3))
    ax = fig.add_subplot(111)
    col = harvard_colors()
    plt.grid()

    t_start = 0
    t_stop = 0

    for i, (model, result) in enumerate(zip(models, results)):
        plt.plot(result.timestamps[burnin:],
                 result.test_lls[burnin:],
                 lw=2,
                 color=col[i],
                 label=model)

        # Update time limits
        t_start = min(t_start, result.timestamps[burnin:].min())
        t_stop = max(t_stop, result.timestamps[burnin:].max())

    # plt.legend(loc="outside right")

    # Plot the standard Hawkes test ll
    plt.plot([t_start, t_stop],
             std_ll * np.ones(2),
             lw=2,
             color=col[len(models)],
             label="Std.")

    # Plot the true ll
    plt.plot([t_start, t_stop],
             true_ll * np.ones(2),
             '--k',
             lw=2,
             label="True")

    ax.set_xlim(t_start, t_stop)
    ax.set_xlabel("time [sec]")
    ax.set_ylabel("Pred. Log Lkhd.")
    plt.show()
def plot_prc_curves(precs, recalls):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors
    col = harvard_colors()

    fig = create_figure((3,3))
    ax = fig.add_subplot(111)
    ax.plot(recalls['xcorr'], precs['xcorr'], color=col[7], lw=1.5, label="xcorr")
    ax.plot(recalls['bfgs'], precs['bfgs'], color=col[3], lw=1.5, label="MAP")
    ax.plot(recalls['svi'], precs['svi'], color=col[0], lw=1.5, label="SVI")
    ax.set_xlabel("Recall")
    ax.set_ylabel("Precision")

    plt.legend(loc=1)
    ax.set_title("Precision-Recall Curve")
    plt.subplots_adjust(bottom=0.25, left=0.25)

    plt.savefig("figure3d.pdf")
    plt.show()
Exemple #9
0
def make_figure_a(S, F, C):
    """
    Plot fluorescence traces, filtered fluorescence, and spike times
    for three neurons
    """
    col = harvard_colors()
    dt = 0.02
    T_start = 0
    T_stop = 1 * 50 * 60
    t = dt * np.arange(T_start, T_stop)

    ks = [0,1]
    nk = len(ks)
    fig = create_figure((3,3))
    for ind,k in enumerate(ks):
        ax = fig.add_subplot(nk,1,ind+1)
        ax.plot(t, F[T_start:T_stop, k], color=col[1], label="$F$")    # Plot the raw flourescence in blue
        ax.plot(t, C[T_start:T_stop, k], color=col[0], lw=1.5, label="$\widehat{F}$")    # Plot the filtered flourescence in red
        spks  = np.where(S[T_start:T_stop, k])[0]
        ax.plot(t[spks], C[spks,k], 'ko', label="S")            # Plot the spike times in black

        # Make a legend
        if ind == 0:
            # Put a legend above
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                       ncol=3, mode="expand", borderaxespad=0.,
                       prop={'size':9})

        # Add labels
        ax.set_ylabel("$F_%d(t)$" % (k+1))
        if ind == nk-1:
            ax.set_xlabel("Time $t$ [sec]")

        # Format the ticks
        ax.set_ylim([-0.1,1.0])
        plt.locator_params(nbins=5, axis="y")


    plt.subplots_adjust(left=0.2, bottom=0.2)
    fig.savefig("figure3a.pdf")
    plt.show()
def plot_roc_curves(fprs, tprs, fig_path="./"):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    col = harvard_colors()

    fig = create_figure((3, 3))
    ax = fig.add_subplot(111)

    # Plot the ROC curves
    if "xcorr" in fprs:
        ax.plot(fprs["xcorr"], tprs["xcorr"], color=col[7], lw=1.5, label="xcorr")
    if "bfgs" in fprs:
        ax.plot(fprs["bfgs"], tprs["bfgs"], color=col[3], lw=1.5, label="MAP")
    if "svi" in fprs:
        ax.plot(fprs["svi"], tprs["svi"], color=col[0], lw=1.5, label="SVI")

    # Plot the diagonal
    ax.plot([0, 1], [0, 1], "-k", lw=0.5)
    ax.set_xlabel("FPR")
    ax.set_ylabel("TPR")

    # this is another inset axes over the main axes
    # parchment = np.array([243,243,241])/255.
    # inset = plt.axes([0.55, 0.275, .265, .265], axisbg=parchment)
    # inset.plot(fprs['xcorr'], tprs['xcorr'], color=col[7], lw=1.5,)
    # inset.plot(fprs['bfgs'], tprs['bfgs'], color=col[3], lw=1.5,)
    # inset.plot(fprs['svi'], tprs['svi'], color=col[0], lw=1.5, )
    # inset.plot([0,1], [0,1], '-k', lw=0.5)
    # plt.setp(inset, xlim=(0,.2), ylim=(0,.2), xticks=[0, 0.2], yticks=[0,0.2], aspect=1.0)
    # inset.yaxis.tick_right()

    plt.legend(loc=4)
    ax.set_title("ROC Curve")

    plt.subplots_adjust(bottom=0.2, left=0.2)

    plt.savefig(os.path.join(os.path.dirname(fig_path), "figure3c.pdf"))
    plt.show()
def plot_prc_curves(precs, recalls, fig_path="./"):
    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    col = harvard_colors()

    fig = create_figure((3, 3))
    ax = fig.add_subplot(111)
    if "xcorr" in recalls:
        ax.plot(recalls["xcorr"], precs["xcorr"], color=col[7], lw=1.5, label="xcorr")
    if "bfgs" in recalls:
        ax.plot(recalls["bfgs"], precs["bfgs"], color=col[3], lw=1.5, label="MAP")
    if "svi" in recalls:
        ax.plot(recalls["svi"], precs["svi"], color=col[0], lw=1.5, label="SVI")
    ax.set_xlabel("Recall")
    ax.set_ylabel("Precision")

    plt.legend(loc=1)
    ax.set_title("Network %d" % net)
    plt.subplots_adjust(bottom=0.25, left=0.25)

    plt.savefig(os.path.join(os.path.dirname(fig_path), "figure3d.pdf"))
    plt.show()
"""
Demo of an eigenmodel.
"""
import numpy as np
import matplotlib.pyplot as plt

from graphistician import GaussianErdosRenyiFixedSparsity


try:
    from hips.plotting.colormaps import harvard_colors
    color = harvard_colors()[0]
except:
    color = 'b'
# color = 'b'

def demo(seed=None):
    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    # Create an eigenmodel with N nodes and D-dimensional feature vectors
    N = 10      # Number of nodes
    B = 2       # Dimensionality of the weights
    true_model = GaussianErdosRenyiFixedSparsity(N, B)

    # Sample a graph from the eigenmodel
    network = true_model.rvs()
def plot_pred_ll_vs_time(plls, timestamps, Z=1.0, T_train=None, nbins=4):

    # import seaborn as sns
    # sns.set(style="whitegrid")

    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((4,3))
    ax = fig.add_subplot(111)
    col = harvard_colors()
    plt.grid()

    # Compute the max and min time in seconds
    print "Homog PLL: ", plls['homog']
    # DEBUG
    plls['homog'] = 0.0
    Z = 1.0

    assert "bfgs" in plls and "bfgs" in timestamps
    # t_bfgs = timestamps["bfgs"]
    t_bfgs = 1.0
    t_start = 1.0
    t_stop = 0.0

    if 'svi' in plls and 'svi' in timestamps:
        isreal = ~np.isnan(plls['svi'])
        svis = plls['svi'][isreal]
        t_svi = timestamps['svi'][isreal]
        t_svi = t_bfgs + t_svi - t_svi[0]
        t_stop = max(t_stop, t_svi[-1])
        ax.semilogx(t_svi, (svis - plls['homog'])/Z, color=col[0], label="SVI", lw=1.5)

    if 'vb' in plls and 'vb' in timestamps:
        t_vb = timestamps['vb']
        t_vb = t_bfgs + t_vb
        t_stop = max(t_stop, t_vb[-1])
        ax.semilogx(t_vb, (plls['vb'] - plls['homog'])/Z, color=col[1], label="VB", lw=1.5)

    if 'gibbs' in plls and 'gibbs' in timestamps:
        t_gibbs = timestamps['gibbs']
        t_gibbs = t_bfgs + t_gibbs
        t_stop = max(t_stop, t_gibbs[-1])
        ax.semilogx(t_gibbs, (plls['gibbs'] - plls['homog'])/Z, color=col[2], label="Gibbs", lw=1.5)

    # if 'gibbs_ss' in plls and 'gibbs_ss' in timestamps:
    #     t_gibbs = timestamps['gibbs_ss']
    #     t_gibbs = t_bfgs + t_gibbs
    #     t_stop = max(t_stop, t_gibbs[-1])
    #     ax.semilogx(t_gibbs, (plls['gibbs_ss'] - plls['homog'])/Z, color=col[8], label="Gibbs-SS", lw=1.5)

    # Extend lines to t_st
    if 'svi' in plls and 'svi' in timestamps:
        final_svi_pll = -np.log(4) + logsumexp(svis[-4:])
        ax.semilogx([t_svi[-1], t_stop],
                    [(final_svi_pll - plls['homog'])/Z,
                     (final_svi_pll - plls['homog'])/Z],
                    '--',
                    color=col[0], lw=1.5)

    if 'vb' in plls and 'vb' in timestamps:
        ax.semilogx([t_vb[-1], t_stop],
                    [(plls['vb'][-1] - plls['homog'])/Z,
                     (plls['vb'][-1] - plls['homog'])/Z],
                    '--',
                    color=col[1], lw=1.5)

    ax.semilogx([t_start, t_stop],
                [(plls['bfgs'] - plls['homog'])/Z, (plls['bfgs'] - plls['homog'])/Z],
                color=col[3], lw=1.5, label="MAP" )

    # Put a legend above
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
               ncol=5, mode="expand", borderaxespad=0.,
               prop={'size':9})

    ax.set_xlim(t_start, t_stop)

    # Format the ticks
    # plt.locator_params(nbins=nbins)

    import matplotlib.ticker as ticker
    logxscale = 3
    xticks = ticker.FuncFormatter(lambda x, pos: '{0:.2f}'.format(x/10.**logxscale))
    ax.xaxis.set_major_formatter(xticks)
    ax.set_xlabel('Time ($10^{%d}$ s)' % logxscale)

    logyscale = 4
    yticks = ticker.FuncFormatter(lambda y, pos: '{0:.3f}'.format(y/10.**logyscale))
    ax.yaxis.set_major_formatter(yticks)
    ax.set_ylabel('Pred. LL ($ \\times 10^{%d}$)' % logyscale)

    # ylim = ax.get_ylim()
    # ax.plot([t_bfgs, t_bfgs], ylim, '--k')
    # ax.set_ylim(ylim)
    ylim = (-129980, -129840)
    ax.set_ylim(ylim)


    # plt.tight_layout()
    plt.subplots_adjust(bottom=0.2, left=0.2)
    # plt.title("Predictive Log Likelihood ($T=%d$)" % T_train)
    plt.show()
    fig.savefig('figure2b.pdf')
def plot_pred_ll_vs_time(models, results, burnin=0,
                         homog_ll=np.nan,
                         std_ll=np.nan,
                         nlin_ll=np.nan,
                         true_ll=np.nan,
                         baseline=0,
                         normalizer=1,
                         output_dir=".",
                         xlim=None, ylim=None,
                         title=None):
    from hips.plotting.layout import create_figure, create_axis_at_location
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((3,2))
    ax = create_axis_at_location(fig, 0.7, 0.4, 2.25, 1.35)
    col = harvard_colors()
    ax.grid()

    t_start = 0
    t_stop = 0

    def standardize(x):
        return (x-baseline)/normalizer

    for i, (model, result) in enumerate(zip(models, results)):
        ax.plot(result.timestamps[burnin:],
                 standardize(result.test_lls[burnin:]),
                 lw=2, color=col[i], label=model)

        # Update time limits
        t_start = min(t_start, result.timestamps[burnin:].min())
        t_stop = max(t_stop, result.timestamps[burnin:].max())

    if xlim is not None:
        t_start = xlim[0]
        t_stop = xlim[1]

    # plt.legend(loc="outside right")

    # Plot baselines
    ax.plot([t_start, t_stop], standardize(homog_ll)*np.ones(2),
             lw=2, color='k', label="Homog")

    # Plot the standard Hawkes test ll
    ax.plot([t_start, t_stop], standardize(std_ll)*np.ones(2),
             lw=2, color=col[len(models)], label="Std.")

    # Plot the Nonlinear Hawkes test ll
    ax.plot([t_start, t_stop], standardize(nlin_ll)*np.ones(2),
             lw=2, ls='--', color=col[len(models)+1], label="Nonlinear")

    # Plot the true ll
    ax.plot([t_start, t_stop], standardize(true_ll)*np.ones(2),
             '--k',  lw=2,label="True")


    ax.set_xlabel("time [sec]")
    ax.set_ylabel("Pred. LL. [nats/event]")

    ax.set_xscale("log")
    if xlim is not None:
        ax.set_xlim(xlim)
    else:
        ax.set_xlim(t_start, t_stop)
    if ylim is not None:
        ax.set_ylim(ylim)

    if title is not None:
        ax.set_title(title)

    output_file = os.path.join(output_dir, "pred_ll_vs_time.pdf")
    fig.savefig(output_file)
    plt.show()
Exemple #15
0
A_mean = A_smpls[N_samples // 2:].mean(0)
fr_mean = fr_smpls[N_samples // 2:].mean(0)
fr_std = fr_smpls[N_samples // 2:].std(0)

plot_glm(Y,
         W_mean,
         A_mean,
         fr_mean,
         std_firingrates=3 * fr_std,
         title="Posterior Mean")

# plot true location and inferred location
from hips.plotting.colormaps import harvard_colors
from utils.utils import compute_optimal_rotation

color = harvard_colors()[0:10]
fig = plt.figure()
ax = fig.add_subplot(111, aspect="equal")

N_select = 10
N_id_select = np.random.permutation(N)[0:N_select]
k = 0

for i in N_id_select:
    for j in range(N_samples):
        R = compute_optimal_rotation(L_smples[j], true_model.network.L)
        L_smples[j] = L_smples[j].dot(R)
        # affine translation
        D_t = np.mean(L_smples[j], 0) - np.mean(true_model.network.L, 0)
        L_smples[j] = L_smples[j] - D_t
    ax.scatter(L_smples[N_samples // 2:, i, 0],
def plot_pred_ll_vs_time(plls, timestamps, Z=1.0, T_train=None, nbins=4):

    # import seaborn as sns
    # sns.set(style="whitegrid")

    from hips.plotting.layout import create_figure
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((4, 3))
    ax = fig.add_subplot(111)
    col = harvard_colors()
    plt.grid()

    # Compute the max and min time in seconds
    print("Homog PLL: ", plls['homog'])
    # DEBUG
    plls['homog'] = 0.0
    Z = 1.0

    assert "bfgs" in plls and "bfgs" in timestamps
    # t_bfgs = timestamps["bfgs"]
    t_bfgs = 1.0
    t_start = 1.0
    t_stop = 0.0

    if 'svi' in plls and 'svi' in timestamps:
        isreal = ~np.isnan(plls['svi'])
        svis = plls['svi'][isreal]
        t_svi = timestamps['svi'][isreal]
        t_svi = t_bfgs + t_svi - t_svi[0]
        t_stop = max(t_stop, t_svi[-1])
        ax.semilogx(t_svi, (svis - plls['homog']) / Z,
                    color=col[0],
                    label="SVI",
                    lw=1.5)

    if 'vb' in plls and 'vb' in timestamps:
        t_vb = timestamps['vb']
        t_vb = t_bfgs + t_vb
        t_stop = max(t_stop, t_vb[-1])
        ax.semilogx(t_vb, (plls['vb'] - plls['homog']) / Z,
                    color=col[1],
                    label="VB",
                    lw=1.5)

    if 'gibbs' in plls and 'gibbs' in timestamps:
        t_gibbs = timestamps['gibbs']
        t_gibbs = t_bfgs + t_gibbs
        t_stop = max(t_stop, t_gibbs[-1])
        ax.semilogx(t_gibbs, (plls['gibbs'] - plls['homog']) / Z,
                    color=col[2],
                    label="Gibbs",
                    lw=1.5)

    # if 'gibbs_ss' in plls and 'gibbs_ss' in timestamps:
    #     t_gibbs = timestamps['gibbs_ss']
    #     t_gibbs = t_bfgs + t_gibbs
    #     t_stop = max(t_stop, t_gibbs[-1])
    #     ax.semilogx(t_gibbs, (plls['gibbs_ss'] - plls['homog'])/Z, color=col[8], label="Gibbs-SS", lw=1.5)

    # Extend lines to t_st
    if 'svi' in plls and 'svi' in timestamps:
        final_svi_pll = -np.log(4) + logsumexp(svis[-4:])
        ax.semilogx([t_svi[-1], t_stop], [(final_svi_pll - plls['homog']) / Z,
                                          (final_svi_pll - plls['homog']) / Z],
                    '--',
                    color=col[0],
                    lw=1.5)

    if 'vb' in plls and 'vb' in timestamps:
        ax.semilogx([t_vb[-1], t_stop], [(plls['vb'][-1] - plls['homog']) / Z,
                                         (plls['vb'][-1] - plls['homog']) / Z],
                    '--',
                    color=col[1],
                    lw=1.5)

    ax.semilogx([t_start, t_stop], [(plls['bfgs'] - plls['homog']) / Z,
                                    (plls['bfgs'] - plls['homog']) / Z],
                color=col[3],
                lw=1.5,
                label="MAP")

    # Put a legend above
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
               loc=3,
               ncol=5,
               mode="expand",
               borderaxespad=0.,
               prop={'size': 9})

    ax.set_xlim(t_start, t_stop)

    # Format the ticks
    # plt.locator_params(nbins=nbins)

    import matplotlib.ticker as ticker
    logxscale = 3
    xticks = ticker.FuncFormatter(
        lambda x, pos: '{0:.2f}'.format(x / 10.**logxscale))
    ax.xaxis.set_major_formatter(xticks)
    ax.set_xlabel('Time ($10^{%d}$ s)' % logxscale)

    logyscale = 4
    yticks = ticker.FuncFormatter(
        lambda y, pos: '{0:.3f}'.format(y / 10.**logyscale))
    ax.yaxis.set_major_formatter(yticks)
    ax.set_ylabel('Pred. LL ($ \\times 10^{%d}$)' % logyscale)

    # ylim = ax.get_ylim()
    # ax.plot([t_bfgs, t_bfgs], ylim, '--k')
    # ax.set_ylim(ylim)
    ylim = (-129980, -129840)
    ax.set_ylim(ylim)

    # plt.tight_layout()
    plt.subplots_adjust(bottom=0.2, left=0.2)
    # plt.title("Predictive Log Likelihood ($T=%d$)" % T_train)
    plt.show()
    fig.savefig('figure2b.pdf')
Exemple #17
0
"""
Demo of an eigenmodel.
"""
import numpy as np
import matplotlib.pyplot as plt

from graphistician import GaussianErdosRenyiFixedSparsity

try:
    from hips.plotting.colormaps import harvard_colors
    color = harvard_colors()[0]
except:
    color = 'b'
# color = 'b'


def demo(seed=None):
    if seed is None:
        seed = np.random.randint(2**32)

    print "Setting seed to ", seed
    np.random.seed(seed)

    # Create an eigenmodel with N nodes and D-dimensional feature vectors
    N = 10  # Number of nodes
    B = 2  # Dimensionality of the weights
    true_model = GaussianErdosRenyiFixedSparsity(N, B)

    # Sample a graph from the eigenmodel
    network = true_model.rvs()
Exemple #18
0
def plot_pred_ll_vs_time(models,
                         results,
                         burnin=0,
                         homog_ll=np.nan,
                         std_ll=np.nan,
                         nlin_ll=np.nan,
                         true_ll=np.nan,
                         baseline=0,
                         normalizer=1,
                         output_dir=".",
                         xlim=None,
                         ylim=None,
                         title=None):
    from hips.plotting.layout import create_figure, create_axis_at_location
    from hips.plotting.colormaps import harvard_colors

    # Make the ICML figure
    fig = create_figure((3, 2))
    ax = create_axis_at_location(fig, 0.7, 0.4, 2.25, 1.35)
    col = harvard_colors()
    ax.grid()

    t_start = 0
    t_stop = 0

    def standardize(x):
        return (x - baseline) / normalizer

    for i, (model, result) in enumerate(zip(models, results)):
        ax.plot(result.timestamps[burnin:],
                standardize(result.test_lls[burnin:]),
                lw=2,
                color=col[i],
                label=model)

        # Update time limits
        t_start = min(t_start, result.timestamps[burnin:].min())
        t_stop = max(t_stop, result.timestamps[burnin:].max())

    if xlim is not None:
        t_start = xlim[0]
        t_stop = xlim[1]

    # plt.legend(loc="outside right")

    # Plot baselines
    ax.plot([t_start, t_stop],
            standardize(homog_ll) * np.ones(2),
            lw=2,
            color='k',
            label="Homog")

    # Plot the standard Hawkes test ll
    ax.plot([t_start, t_stop],
            standardize(std_ll) * np.ones(2),
            lw=2,
            color=col[len(models)],
            label="Std.")

    # Plot the Nonlinear Hawkes test ll
    ax.plot([t_start, t_stop],
            standardize(nlin_ll) * np.ones(2),
            lw=2,
            ls='--',
            color=col[len(models) + 1],
            label="Nonlinear")

    # Plot the true ll
    ax.plot([t_start, t_stop],
            standardize(true_ll) * np.ones(2),
            '--k',
            lw=2,
            label="True")

    ax.set_xlabel("time [sec]")
    ax.set_ylabel("Pred. LL. [nats/event]")

    ax.set_xscale("log")
    if xlim is not None:
        ax.set_xlim(xlim)
    else:
        ax.set_xlim(t_start, t_stop)
    if ylim is not None:
        ax.set_ylim(ylim)

    if title is not None:
        ax.set_title(title)

    output_file = os.path.join(output_dir, "pred_ll_vs_time.pdf")
    fig.savefig(output_file)
    plt.show()
Exemple #19
0
import seaborn as sns
sns.set(style="white")

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import matplotlib
matplotlib.rcParams.update({'font.sans-serif' : 'Helvetica',
                            'axes.labelsize': 9,
                            'xtick.labelsize' : 7,
                            'ytick.labelsize' : 7,
                            'axes.titlesize' : 9})


from hips.plotting.layout import create_axis_at_location, create_figure, remove_plot_labels
from hips.plotting.colormaps import harvard_colors, gradient_cmap
colors = harvard_colors()

from graphistician.internals.utils import compute_optimal_rotation

from pybasicbayes.util.text import progprint_xrange

import pyhawkes.models
reload(pyhawkes.models)
from pyhawkes.models import ContinuousTimeNetworkHawkesModel

from pyhawkes.internals.network import LatentDistanceAdjacencyModel, ErdosRenyiFixedSparsity, ErdosRenyiModel

# Load the hippocampal data
with open("data/hippocampus.mat", "r") as f:
    matdata = loadmat(f)
    S = matdata['data']['S'][0,0].ravel().astype(np.float)
Exemple #20
0
import seaborn as sns
sns.set(style="white")

import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import matplotlib
matplotlib.rcParams.update({'font.sans-serif' : 'Helvetica',
                            'axes.labelsize': 9,
                            'xtick.labelsize' : 7,
                            'ytick.labelsize' : 7,
                            'axes.titlesize' : 9})


from hips.plotting.layout import create_axis_at_location, create_figure, remove_plot_labels
from hips.plotting.colormaps import harvard_colors, gradient_cmap
colors = harvard_colors()

from graphistician.internals.utils import compute_optimal_rotation

from pybasicbayes.util.text import progprint_xrange

import pyhawkes.models
imp.reload(pyhawkes.models)
from pyhawkes.models import ContinuousTimeNetworkHawkesModel

from pyhawkes.internals.network import LatentDistanceAdjacencyModel, ErdosRenyiFixedSparsity, ErdosRenyiModel

# Load the hippocampal data
with open("data/hippocampus.mat", "r") as f:
    matdata = loadmat(f)
    S = matdata['data']['S'][0,0].ravel().astype(np.float)