Exemplo n.º 1
0
def test_imm_wnc2():
    """
    Test the basic imm_wnc when null class is shrunk to 0
    """
    n = 50
    dim = 1
    alpha = .5
    g0 = 1.
    x = np.random.rand(n, dim)
    x[:.3*n] *= .2
    x[:.1*n] *= .3

    # instantiate
    migmm = MixedIMM(alpha, dim)
    migmm.set_priors(x)
    migmm.set_constant_densities(null_dens=g0)
    ncp = np.zeros(n)
    
    # warming
    migmm.sample(x, null_class_proba=ncp, niter=100, init=True)
    g = np.linspace(0, 1, 101)

    #sampling
    like, pproba =  migmm.sample(x, null_class_proba=ncp, niter=300)
    assert like.min()>.1
    assert like.max()<5.
    assert (pproba==ncp).all()
Exemplo n.º 2
0
def test_imm_wnc3():
    """
    Test the basic imm_wnc when null class is of proba 1 (nothing is estimated)
    """
    n = 50
    dim = 1
    alpha = .5
    g0 = 1.
    x = np.random.rand(n, dim)
    x[:.3*n] *= .2
    x[:.1*n] *= .3

    # instantiate
    migmm = MixedIMM(alpha, dim)
    migmm.set_priors(x)
    migmm.set_constant_densities(null_dens=g0)
    ncp = np.ones(n)
    
    # warming
    migmm.sample(x, null_class_proba=ncp, niter=100, init=True)
    g = np.linspace(0, 1, 101)

    #sampling
    like, pproba =  migmm.sample(x, null_class_proba=ncp, niter=300)
    assert (pproba==ncp).all()
Exemplo n.º 3
0
def test_imm_wnc1():
    """
    Test the basic imm_wnc, where the probaility under the null are random  
    """
    n = 50
    dim = 1
    alpha = .5
    g0 = 1.
    x = np.random.rand(n, dim)
    x[:.3*n] *= .2
    x[:.1*n] *= .3

    # instantiate
    migmm = MixedIMM(alpha, dim)
    migmm.set_priors(x)
    migmm.set_constant_densities(null_dens=g0)
    ncp = np.random.rand(n)
    
    # warming
    migmm.sample(x, null_class_proba=ncp, niter=100, init=True)
    g = np.reshape(np.linspace(0, 1, 101), (101, dim))

    #sampling
    like, pproba =  migmm.sample(x, null_class_proba=ncp, niter=300,
                         sampling_points=g)

    # the density should sum to 1
    ds = 0.01*like.sum()
    assert ds<1
    assert ds>.8
    assert np.sum(pproba>.5)>1
    assert np.sum(pproba<.5)>1
def dpmm(gfc, alpha, g0, g1, dof, prior_precision, gf1, sub, burnin,
         spatial_coords=None, nis=1000, co_clust=False, verbose=False):
    """
    Apply the dpmm analysis to the data: python version
    """
    from nipy.neurospin.clustering.imm import MixedIMM
    dim = gfc.shape[1]
    migmm = MixedIMM(alpha, dim)
    migmm.set_priors(gfc)
    migmm.set_constant_densities(null_dens=g0, prior_dens=g1)
    migmm._prior_dof = dof
    migmm._prior_scale = np.diag(prior_precision[0]/dof)
    migmm._inv_prior_scale_ = [np.diag(dof*1./(prior_precision[0]))]
    migmm.sample(gfc, null_class_proba=1-gf1, niter=burnin, init=False,
                 kfold=sub)
    if verbose:
        print 'number of components: ', migmm.k

    #sampling
    if co_clust:
        like, pproba, co_clust =  migmm.sample(
            gfc, null_class_proba=1-gf1, niter=nis,
            sampling_points=spatial_coords, kfold=sub, co_clustering=co_clust)
        if verbose:
            print 'number of components: ', migmm.k
        
        return like, 1-pproba, co_clust
    else:
        like, pproba =  migmm.sample(
            gfc, null_class_proba=1-gf1, niter=nis,
            sampling_points=spatial_coords, kfold=sub, co_clustering=co_clust)
    if verbose:
        print 'number of components: ', migmm.k
    
    return like, 1-pproba