Example #1
0
dimy = 60
pos = np.array([[12, 14], [20, 20], [30, 20]])
ampli = np.array([3, 4, 4])

nbvox = dimx * dimy
dataset = simul.surrogate_2d_dataset(nbsubj=1, dimx=dimx, dimy=dimy, pos=pos,
                                     ampli=ampli, width=10.0).squeeze()
values = dataset.ravel()


#-------------------------------------------------------
# Computations
#------------------------------------------------------- 

# create a domain descriptor associated with this
domain = domain_from_array(dataset ** 2 > 0)

nroi = hroi.HROI_as_discrete_domain_blobs(domain, dataset.ravel(),
                                          threshold=2.0, smin=3)
label = np.reshape(nroi.label, ((dimx, dimy)))

# create an average activaion image
nroi.make_feature('activation', dataset.ravel())
mean_activation = nroi.representative_feature('activation')
bmap = - np.ones((dimx, dimy))
for k in range(nroi.k):
    bmap[label == k] = mean_activation[k]

#--------------------------------------------------------
# Result display
#--------------------------------------------------------
def make_bsa_2d(betas, theta=3., dmax=5., ths=0, thq=0.5, smin=0, 
                       method='simple', verbose=0):
    """
    Function for performing bayesian structural analysis
    on a set of images.

    Parameters
    ----------
    betas, array of shape (nsubj, dimx, dimy) the data used
           Note that it is assumed to be a t- or z-variate
    theta=3., float,
              first level threshold of betas
    dmax=5., float, expected between subject variability
    ths=0, float,
           null hypothesis for the prevalence statistic
    thq=0.5, float,
             p-value of the null rejection
    smin=0, int,
            threshold on the nu_mber of contiguous voxels 
            to make regions meaningful structures
    method= 'simple', string,
            estimation method used ; to be chosen among 
            'simple', 'quick', 'loo', 'ipmi'
    verbose=0, verbosity mode     

    Returns
    -------
    AF the landmark_regions instance describing the result
    BF: list of hroi instances describing the individual data
    """
    ref_dim = np.shape(betas[0])
    nsubj = betas.shape[0]
    xyz = np.array(np.where(betas[:1])).T.astype(np.int)
    
    # Get  coordinates in mm
    xyz = xyz[:, 1:] # switch to dimension 2
    coord = xyz.astype(np.float)

    # get the functional information
    lbeta = np.array([np.ravel(betas[k]) for k in range(nsubj)]).T
    
    lmax = 0
    bdensity = 1
    dom = domain_from_array(np.ones(ref_dim))
    
    if method == 'simple':    
        group_map, AF, BF, likelihood = \
                   bsa.compute_BSA_simple(dom, lbeta, dmax, thq, smin, ths,
                                          theta)
    if method == 'quick':
        likelihood = np.zeros(ref_dim)
        group_map, AF, BF, coclustering = \
                   bsa.compute_BSA_quick(dom, lbeta, dmax, thq, smin, ths,
                                         theta)
    if method == 'loo':
        mll, ll0 = bsa.compute_BSA_loo(dom, lbeta, dmax, thq, smin, ths,
                                          theta, bdensity)
        return mll, ll0
        
    if method not in['loo', 'simple', 'quick']:
        raise ValueError('method is not correctly defined')
    
    if verbose == 0:
        return AF, BF
    
    if AF != None:
        lmax = AF.k + 2
        AF.show()

    group_map.shape = ref_dim
    mp.figure(figsize=(8, 3))
    ax = mp.subplot(1, 3, 1)
    mp.imshow(group_map, interpolation='nearest', vmin=-1, vmax=lmax)
    mp.title('Blob separation map', fontsize=10)
    mp.axis('off')
    plop = mp.colorbar(shrink=.8)

    if AF != None:
        group_map = AF.map_label(coord, 0.95, dmax)
        group_map.shape = ref_dim
    
    mp.subplot(1, 3, 2)
    mp.imshow(group_map, interpolation='nearest', vmin=-1, vmax=lmax)
    mp.title('group-level position 95% \n confidence regions', fontsize=10)
    mp.axis('off')
    mp.colorbar(shrink=.8)

    mp.subplot(1, 3, 3)
    likelihood.shape = ref_dim
    mp.imshow(likelihood, interpolation='nearest')
    mp.title('Spatial density under h1', fontsize=10)
    mp.axis('off')
    mp.colorbar(shrink=.8)

    
    fig_output = mp.figure(figsize=(8, 3.5))
    fig_output.text(.5, .9, "Individual landmark regions", ha="center")
    for s in range(nsubj):
        ax = mp.subplot(nsubj / 5, 5, s + 1)
        #ax.set_position([.02, .02, .96, .96])
        lw = - np.ones(ref_dim)
        if BF[s] is not None:
            nls = BF[s].get_roi_feature('label')
            nls[nls == - 1] = np.size(AF) + 2
            for k in range(BF[s].k):
                np.ravel(lw)[BF[s].label == k] = nls[k]

        mp.imshow(lw, interpolation='nearest', vmin=-1, vmax=lmax)
        mp.axis('off')

    fig_input = mp.figure(figsize=(8, 3.5))
    fig_input.text(.5,.9, "Input activation maps", ha='center')
    for s in range(nsubj):
        ax = mp.subplot(nsubj / 5, 5, s + 1)
        #ax.set_position([.02, .02, .96, .96])
        mp.imshow(betas[s], interpolation='nearest', vmin=betas.min(),
                  vmax=betas.max())
        mp.axis('off')
    return AF, BF