Example #1
0
 def test_field_from_coo_matrix(self):
     import scipy.sparse as sps
     V = 10
     a = np.random.rand(V, V)>.9
     fi = ff.field_from_coo_matrix_and_data(sps.coo_matrix(a), a)
     print fi.E , a.sum()
     self.assert_(fi.E==a.sum())
Example #2
0
def HROI_as_discrete_domain_blobs(dom, data, threshold=-np.infty, smin=0,
                                  id=''):
    """
    """
    if threshold > data.max():
        label = -np.ones(data.shape)
        parents = np.array([])
        return HierarchicalROI(dom, label, parents, id=id)
    
    # check size
    df = field_from_coo_matrix_and_data(dom.topology, data)
    idx, height, parents, label = df.threshold_bifurcations(th=threshold)    
    k = np.size(idx)

    nroi = HierarchicalROI(dom, label, parents, id=id)

    # Create a signal feature
    nroi.make_feature('signal', np.reshape(data, (np.size(data), 1)))
    
    # perform smin reduction
    k = 2* nroi.get_k()
    while k>nroi.get_k():
        k = nroi.get_k()
        size = nroi.get_size()
        nroi.merge_ascending(size>smin)
        nroi.merge_descending()
        if nroi.k==0:
            break
        size = nroi.get_size()
        if size.max()<smin:
            break #return None
        
        nroi.select(size>smin)        
    return nroi
Example #3
0
def HROI_from_watershed(domain, data, threshold=-np.infty, id=''):
    """
    Instantiate an HierarchicalROI as the watershed of a certain dataset
    
    Parameters  
    ----------
    domain: discrete_domain.StructuredDomain instance,
            definition of the spatial context
    data: array of shape (domain.size),
          the corresponding data field
    threshold: float optional,
               thresholding level
    
    Returns
    -------
    the HierachicalROI instance
      
    Fixme
    -----
    should be a subdomain (?)
    Additionally a discrete_field is created, with the key 'index'.
                 It contains the index in the field from which 
                 each point of each ROI
    """
    if threshold > data.max():
        label = -np.ones(data.shape)
        parents = np.array([])
        return HierarchicalROI(domain, label, parents, id=id)

    df = field_from_coo_matrix_and_data(domain.topology, data)
    idx, height, parents, label = df.custom_watershed(0, threshold)
    nroi = HierarchicalROI(domain, label, parents, id=id)

    # this is  a custom thing, sorry
    nroi.set_roi_feature('seed', idx)
    return nroi
def bsa_dpmm(bf, gf0, sub, gfc, dmax, thq, ths, verbose=0):
    """
    Estimation of the population level model of activation density using 
    dpmm and inference
    
    Parameters
    ----------
    bf list of nipy.neurospin.spatial_models.hroi.HierarchicalROI instances
       representing individual ROIs
       let nr be the number of terminal regions across subjects
    gf0, array of shape (nr)
         the mixture-based prior probability 
         that the terminal regions are true positives
    sub, array of shape (nr)
         the subject index associated with the terminal regions
    gfc, array of shape (nr, coord.shape[1])
         the coordinates of the of the terminal regions
    dmax float>0:
         expected cluster std in the common space in units of coord
    thq = 0.5 (float in the [0,1] interval)
        p-value of the prevalence test
    ths=0, float in the rannge [0,nsubj]
        null hypothesis on region prevalence that is rejected during inference
    verbose=0, verbosity mode

    Returns
    -------
    crmap: array of shape (nnodes):
           the resulting group-level labelling of the space
    LR: a instance of sbf.LandmarkRegions that describes the ROIs found
        in inter-subject inference
        If no such thing can be defined LR is set to None
    bf: List of  nipy.neurospin.spatial_models.hroi.Nroi instances
        representing individual ROIs
    p: array of shape (nnodes):
       likelihood of the data under H1 over some sampling grid
    """
    dom = bf[0].domain
    n_subj = len(bf)
    
    crmap = -np.ones(dom.size, np.int)
    LR = None
    p = np.zeros(dom.size)
    if len(sub)<1:
        return crmap, LR, bf, p

    sub = np.concatenate(sub).astype(np.int) 
    gfc = np.concatenate(gfc)
    gf0 = np.concatenate(gf0)

    g0 = 1./dom.local_volume.sum()
    
    # prepare the DPMM
    dim = dom.em_dim
    g1 = g0
    prior_precision =  1./(dmax*dmax)*np.ones((1,dim))
    dof = 10
    burnin = 100
    nis = 1000
    # nis = number of iterations to estimate p
    
    #nii = 100
    ## nii = number of iterations to estimate q
    #p,q =  fc.fdp(gfc, 0.5, g0, g1, dof, prior_precision, 1-gf0,
    #              sub, burnin, coord, nis, nii)
    p, q =  dpmm(gfc, 0.5, g0, g1, dof, prior_precision, 1-gf0,
               sub, burnin, dom.coord, nis)
    
    if verbose:
        import matplotlib.pylab as mp
        mp.figure()
        mp.plot(1-gf0,q,'.')
        h1,c1 = mp.histogram((1-gf0),bins=100)
        h2,c2 = mp.histogram(q,bins=100)
        mp.figure()
        mp.bar(c1[:len(h1)],h1,width=0.005)
        mp.bar(c2[:len(h2)]+0.003,h2,width=0.005,color='r')
        print 'Number of candidate regions %i, regions found %i' % (
                    np.size(q), q.sum())

    from nipy.neurospin.graph.field import field_from_coo_matrix_and_data 
    Fbeta = field_from_coo_matrix_and_data(dom.topology, p)
    _, _, _, label = Fbeta.custom_watershed(0, g0)

    # append some information to the hroi in each subject
    for s in range(n_subj):
        bfs = bf[s]
        if bfs.k>0 :
            leaves = bfs.isleaf()
            us = -np.ones(bfs.k).astype(np.int)

            # set posterior proba
            lq = np.zeros(bfs.k)
            lq[leaves] = q[sub==s]
            bfs.set_roi_feature('posterior_proba', lq)

            # set prior proba
            lq = np.zeros(bfs.k)
            lq[leaves] = 1-gf0[sub==s]
            bfs.set_roi_feature('prior_proba', lq)

            pos = bfs.representative_feature('position', 'mean')
            midx = [np.argmin(np.sum((dom.coord-pos[k])**2,1))
                    for k in range(bfs.k)]
            j = label[np.array(midx)]
            us[leaves] = j[leaves]

            # when parent regions has similarly labelled children,
            # include it also
            us = bfs.make_forest().propagate_upward(us)
            bfs.set_roi_feature('label',us)
                        
    # derive the group-level landmarks
    # with a threshold on the number of subjects
    # that are represented in each one 
    LR, nl = sbf.build_LR(bf, thq, ths, dmax, verbose=verbose)

    # make a group-level map of the landmark position        
    crmap = _relabel_(label, nl)   
    
    return crmap, LR, bf, p
Example #5
0
def Compute_Amers(dom, lbeta, dmax=10., thr=3.0, ths=0, pval=0.2, verbose=0):
    """
    This is the main function for building the BFLs

    Parameters
    ----------
    dom : StructuredDomain instance,
          generic descriptor of the space domain
    lbeta: an array of shape (nbnodes, subjects):
           the multi-subject statistical maps
    dmax:float, optional, spatial relaxation allowed in the preocedure
    thr: float, optional, threshold at the first-level
    ths: float, optional,  number of subjects to validate a BFL
    pval: float, optional  : significance p-value for the spatial inference

    Returns
    -------
    crmap: array of shape (nnodes):
           the resulting group-level labelling of the space
    AF : a instance of LandmarkRegions that describes the ROIs found
        in inter-subject inference
        If no such thing can be defined LR is set to None
    BFLs:  List of  nipy.neurospin.spatial_models.hroi.Nroi instances
        representing individual ROIs
    Newlabel: labelling of the individual ROIs
    """
    BFLs = []
    nbsubj = lbeta.shape[1]
    nvox = lbeta.shape[0]

    from nipy.neurospin.graph.field import field_from_coo_matrix_and_data 
    Fbeta = field_from_coo_matrix_and_data(dom.topology, lbeta[:, 0])

    for s in range(nbsubj):
        beta = np.reshape(lbeta[:,s],(nvox,1))
        Fbeta.set_field(beta)
        bfls = hroi.HROI_from_watershed(dom, beta, threshold=thr)
 
        if bfls.k>0:
            bfls.make_feature('position', dom.coord)
            pos = bfls.representative_feature('position', 'mean')
            bfls.set_roi_feature('position', pos)
    
        BFLs.append(bfls)
    
    _clean_density_redraw(BFLs, dmax, dom.coord, pval, verbose=0,
                          nrec=1, nsamples=10)
    
    Gc = _hierarchical_asso(BFLs,dmax)
    Gc.weights = np.log(Gc.weights)-np.log(Gc.weights.min())
    if verbose:
        print Gc.V, Gc.E, Gc.weights.min(), Gc.weights.max()
    
    # building cliques
    u,cost = average_link_graph_segment(Gc, 0.1, Gc.V*1.0/nbsubj)

    # relabel the BFLs
    q = 0
    for s in range(nbsubj):
        BFLs[s].set_roi_feature('label',u[q:q+BFLs[s].k])
        q += BFLs[s].k
    
    LR, mlabel = build_LR(BFLs, ths=ths)
    if LR!=None:
        crmap = LR.map_label(dom.coord, pval=0.95, dmax=dmax)
        
    return crmap, LR, BFLs