def test_imshow_heatmap():
    from scipy.interpolate import griddata
    from matplotlib import pyplot as plt

    mesh3D = mesh(200)
    mesh2D = proj_to_2D(mesh3D)

    data = np.zeros((3,3))
    data[0,1] += 2

    vals = np.exp(log_dirichlet_density(mesh3D,2.,data=data.sum(0)))
    temp = log_censored_dirichlet_density(mesh3D,2.,data=data)
    censored_vals = np.exp(temp - temp.max())

    xi = np.linspace(-1,1,1000)
    yi = np.linspace(-0.5,1,1000)

    plt.figure()
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),vals,(xi[None,:],yi[:,None]),method='cubic'))
    plt.axis('off')
    plt.title('uncensored likelihood')

    plt.figure()
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),censored_vals,(xi[None,:],yi[:,None]),method='cubic'))
    plt.axis('off')
    plt.title('censored likelihood')
def prior_posterior_2D(meshsize=250,alpha=2.,data=np.array([[0,2,0],[0,0,0],[0,0,0]])):
    assert data.shape == (3,3)

    mesh3D = simplex.mesh(meshsize)
    mesh2D = simplex.proj_to_2D(mesh3D) # use specialized b/c it plays nicer with triangulation algorithm

    priorvals = np.exp(dirichlet.log_dirichlet_density(mesh3D,alpha))

    posteriorvals_uncensored = np.exp(dirichlet.log_dirichlet_density(mesh3D,alpha,data=data.sum(0)))

    temp = dirichlet.log_censored_dirichlet_density(mesh3D,alpha,data=data)
    temp = np.exp(temp - temp.max())
    posteriorvals_censored = temp/temp.sum() # direct discretized integration!

    # used for grid interpolation
    xi = np.linspace(mesh2D[:,0].min(), mesh2D[:,0].max(), 2000, endpoint=True)
    yi = np.linspace(mesh2D[:,1].min(), mesh2D[:,1].max(), 2000, endpoint=True)

    plt.figure(figsize=(8,8))
    # use exactly one of the next two code lines!
    # this one performs interpolation to get a rectangular-pixel grid, but
    # produces a blurred image
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),priorvals,(xi[na,:],yi[:,na]),method='cubic'))
    # this one exactly represents the data by performing a DeLaunay
    # triangulation, but it must draw each triangular pixel individually,
    # resulting in large files and slow draw times
    # plt.tripcolor(mesh2D[:,0],mesh2D[:,1],priorvals) # exact triangles, no blurring
    plt.axis('off')
    save('../writeup/figures/dirichlet_prior_2D.pdf')

    plt.figure(figsize=(8,8))
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),posteriorvals_uncensored,(xi[na,:],yi[:,na]),method='cubic'))
    # plt.tripcolor(mesh2D[:,0],mesh2D[:,1],posteriorvals_uncensored)
    plt.axis('off')
    save('../writeup/figures/dirichlet_uncensored_posterior_2D.pdf')

    plt.figure(figsize=(8,8))
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),posteriorvals_censored,(xi[na,:],yi[:,na]),method='cubic'))
    # plt.tripcolor(mesh2D[:,0],mesh2D[:,1],posteriorvals_censored)
    plt.axis('off')
    save('../writeup/figures/dirichlet_censored_posterior_2D.pdf')
def test_pcolor_heatmap():
    # import matplotlib.tri as tri
    from matplotlib import pyplot as plt

    mesh3D = mesh(100,edges=True)
    mesh2D = proj_to_2D(mesh3D)
    # triangulation = tri.Triangulation(mesh2D) # this is called in tripcolor

    data = np.zeros((3,3))
    data[0,1] += 1

    vals = np.exp(log_dirichlet_density(mesh3D,2.,data=data.sum(0)))
    temp = log_censored_dirichlet_density(mesh3D,2.,data=data)
    censored_vals = np.exp(temp - temp.max())

    plt.figure()
    plt.tripcolor(mesh2D[:,0],mesh2D[:,1],vals)
    plt.title('uncensored')

    plt.figure()
    plt.tripcolor(mesh2D[:,0],mesh2D[:,1],censored_vals)
    plt.title('censored')
def aux_posterior_2D(meshsize=250,alpha=2.,data=np.array([[0,2,0],[0,0,0],[0,0,0]])):
    assert data.shape == (3,3)

    mesh3D = simplex.mesh(meshsize)
    mesh2D = simplex.proj_to_2D(mesh3D) # use specialized b/c it plays nicer with triangulation algorithm

    # get samples
    auxsamples = sampling.generate_pi_samples_withauxvars(alpha,10000,data)

    # evaluate a kde based on the samples
    aux_kde = density.kde(0.005,auxsamples[len(auxsamples)//20:])
    aux_kde_vals = aux_kde(mesh3D)

    ### plot

    # used for grid interpolation
    xi = np.linspace(mesh2D[:,0].min(), mesh2D[:,0].max(), 2000, endpoint=True)
    yi = np.linspace(mesh2D[:,1].min(), mesh2D[:,1].max(), 2000, endpoint=True)

    plt.figure(figsize=(8,8))
    plt.imshow(griddata((mesh2D[:,0],mesh2D[:,1]),aux_kde_vals,(xi[na,:],yi[:,na]),method='cubic'))
    plt.axis('off')

    save('../writeup/figures/dirichlet_censored_auxvar_posterior_2D.pdf')