Example #1
0
def test_parcel_multi_subj():
    """
    """
    # step 1:  generate some synthetic data
    nsubj = 10
    dimx = 60
    dimy = 60
    pos = 3*np.array([[ 6,  7],
                      [10, 10],
                      [15, 10]])
    ampli = np.array([5, 7, 6])
    sjitter = 6.0
    dataset = simul.make_surrogate_array(nbsubj=nsubj, dimx=dimx,
                                         dimy=dimy, 
                                         pos=pos, ampli=ampli, width=10.0)

    # step 2 : prepare all the information for the parcellation
    nbparcel = 10
    ref_dim = (dimx,dimy)
    xy = np.array(np.where(dataset[0])).T
    nvox = np.size(xy,0)
    xyz = np.hstack((xy,np.zeros((nvox,1))))
	
    ldata = np.reshape(dataset,(nsubj,dimx*dimy,1))
    anat_coord = xy
    mask = np.ones((nvox,nsubj)).astype('bool')
    Pa = fp.Parcellation(nbparcel,xyz,mask-1)

    # step 3 : run the algorithm
    Pa =  hp.hparcel(Pa, ldata, anat_coord, mu = 10.0)
    	
    # step 4:  look at the results
    Label =  np.array([np.reshape(Pa.label[:,s],(dimx,dimy))
                       for s in range(nsubj)])
    control = True
    for s in range(nsubj):
        control *= (np.unique(Label)==np.arange(nbparcel)).all()
    assert(control)
# with shape (dimx,dimy)

# step 2 : prepare all the information for the parcellation
nbparcel = 10
ref_dim = (dimx, dimy)
xy = np.array(np.where(dataset[0])).T
nvox = np.size(xy, 0)
xyz = np.hstack((xy, np.zeros((nvox, 1))))

ldata = np.reshape(dataset, (nsubj, dimx * dimy, 1))
anat_coord = xy
mask = np.ones((nvox, nsubj)).astype("bool")
Pa = fp.Parcellation(nbparcel, xyz, mask - 1)

# step 3 : run the algorithm
Pa = hp.hparcel(Pa, ldata, anat_coord, mu=3.0)
# note: play with mu to change the 'stiffness of the parcellation'

# step 4:  look at the results
Label = np.array([np.reshape(Pa.label[:, s], (dimx, dimy)) for s in range(nsubj)])

import matplotlib.pylab as mp

mp.figure()

for s in range(nsubj):
    mp.subplot(2, 5, s + 1)
    mp.imshow(dataset[s], interpolation="nearest")
    mp.axis("off")
mp.figure()
# verbosity mode
verbose = 1

# number of parcels
nbparcel = 500

# write dir
swd = tempfile.mkdtemp()


# prepare the parcel structure
fpa,ldata,coord = parcel_input(mask_images,nbeta,learn_images,ths,fdim)
fpa.k = nbparcel

# run the algorithm
fpa = hparcel(fpa,ldata,coord)
#fpa,prfx0 = hparcel(fpa,ldata,coord,nbperm=200,niter=5,verbose)

#produce some output images
Parcellation_output(fpa,mask_images,learn_images,coord,subj_id,
                    verbose=1,swd=swd)

# do some parcellation-based analysis:
# take some test images whose parcel-based signal needs to be assessed 
test_images=learn_images

# a design matrix for possibly subject-specific effects
DMtx = None

# compute and write the parcel-based statistics
Parcellation_based_analysis(fpa,test_images,numbeta,swd,DMtx,verbose)