コード例 #1
1
 def testvoronoi(self):
     X = nr.randn(10000,2)
     A = np.concatenate([np.ones((7000,2)),np.zeros((3000,2))])
     X = X+3*A
     C = np.array([[0,0],[3,3]])
     L = fc.voronoi(X,C)
     l = L[:7000].astype('d')
     self.assert_(np.mean(l,0)>0.5)
コード例 #2
0
def hparcel(Pa,ldata,anat_coord,nbperm=0,niter=5, mu=10.,dmax = 10., lamb = 100.0, chunksize = 1.e5,verbose=0):
	"""
	Function that performs the parcellation by optimizing the
	sinter-subject similarity while retaining the connectedness
	within subject and some consistency across subjects.
	
    Parameters
    ----------
	Pa: a Parcel structure that essentially contains 
        the grid position information and the individual masks
	anat_coord: array of shape(nbvox,3) which defines the position
                 of the grid points in some space
	nbperm=0: the number of times the parcellation and prfx
              computation is performed on sign-swaped data
	niter=10: number of iterations to obtain the convergence of the method
              information in the clustering algorithm
    mu=10., float, relative weight of anatomical information
	
    Results
    -------
	Pa: the resulting parcellation structure appended with the labelling
	"""
	
	# a various parameters
	nn = 18
	nbvox = Pa.nbvox
	xyz = Pa.ijk
	Sess = Pa.nb_subj
	
	Gs = []
	Feature = []
	RFeature = []
	Ranat = []
	# browse the data
	for s in range(Sess):
		lxyz = xyz[Pa.label[:,s]>-1].astype(np.int)
		lnvox = np.sum(Pa.label[:,s]>-1)
		lac = anat_coord[Pa.label[:,s]>-1]
		g = fg.WeightedGraph(lnvox)
		g.from_3d_grid(lxyz,nn)
		g.remove_trivial_edges()
		beta = np.reshape(ldata[s],(lnvox,ldata[s].shape[1]))
		feature = np.hstack((beta,mu*lac/(1.e-15+np.std(anat_coord,0))))
		Gs.append(g)
		Feature.append(feature)
		aux = np.argsort(rand(lnvox))[:np.minimum(chunksize/Sess,lnvox)]
		RFeature.append(feature[aux,:])
		Ranat.append(lac[aux,:])

	RFeature = np.concatenate(RFeature)
	Ranat = np.concatenate(Ranat)

	# main function
	U,proto_anat = optim_hparcel(Ranat, RFeature, Feature, Pa, Gs, anat_coord,
                                         lamb, dmax, niter=niter,  
                                         verbose=verbose)

	# write the individual labelling
	Labels = -1*np.ones((nbvox,Sess)).astype(np.int)
	for s in range(Sess):
		Labels[Pa.label[:,s]>-1,s] = U[s]
		
	palc = Pa.label.copy()
	Pa.set_labels(Labels)
	if Pa.isfield('functional'): Pa.remove_feature('functional')
	Pa.make_feature(ldata,'functional')
	prfx = Pa.PRFX('functional')
		
	# compute the group-level labels 
	u = fc.voronoi(anat_coord,proto_anat)
	Pa.group_labels  = u
	if nbperm>0:
		Pb = fp.Parcellation(Pa.k,xyz,palc)
		prfx0 = perm_prfx(Pb,Gs,Feature, ldata,anat_coord,nbperm,niter,dmax, lamb, chunksize)
		return Pa,prfx0
	else:
		return Pa