Exemplo n.º 1
0
def test_find_parcel_centroids(cammoun_surf, scale, parcels, n_right, method):
    lh, rh = cammoun_surf[scale]

    coords, hemi = freesurfer.find_parcel_centroids(lhannot=lh, rhannot=rh,
                                                    method=method,
                                                    version='fsaverage5')
    assert len(coords) == parcels
    assert len(hemi) == parcels
    assert np.sum(hemi) == n_right
def hungarian(y, parcellation, scale, fn=None):
    y = np.asarray(y)
    if USE_CACHED and fn is not None:
        spins = simnulls.load_spins(fn, n_perm=N_PERM)
    else:
        annot = _get_annot(parcellation, scale)
        coords, hemi = nnsurf.find_parcel_centroids(lhannot=annot.lh,
                                                    rhannot=annot.rh,
                                                    version='fsaverage5',
                                                    surf='sphere',
                                                    method='surface')
        spins = nnstats.gen_spinsamples(coords, hemi, method='hungarian',
                                        n_rotate=N_PERM, seed=SEED)
    return y[spins]
def vazquez_rodriguez(y, parcellation, scale, fn=None):
    y = np.asarray(y)
    if USE_CACHED and fn is not None:
        spins = simnulls.load_spins(fn, n_perm=N_PERM)
    else:
        if parcellation != 'vertex':
            annot = _get_annot(parcellation, scale)
            coords, hemi = nnsurf.find_parcel_centroids(lhannot=annot.lh,
                                                        rhannot=annot.rh,
                                                        version='fsaverage5',
                                                        surf='sphere',
                                                        method='surface')
        else:
            coords, hemi = nnsurf._get_fsaverage_coords(scale, 'sphere')
        spins = nnstats.gen_spinsamples(coords, hemi, method='original',
                                        n_rotate=N_PERM, seed=SEED)
    return y[spins]
Exemplo n.º 4
0
def generate_spins(parcel, lhannot, rhannot, order, info_path, hemi='',
                   k=10000):
    '''
    Function to generate spun permutation of a parcellation's parcels.
    '''

    # Generate the spins
    coords, hemi_centroids = find_parcel_centroids(lhannot=lhannot,
                                                   rhannot=rhannot)
    spins_LR = gen_spinsamples(coords, hemi_centroids, n_rotate=k)

    # Get some info about hemispheres and subcortex
    hemi_info = load_data._load_hemi_info(parcel, info_path)
    sub_info = load_data._load_subcortex_info(parcel, info_path)

    # Remove subcortex info from hemi_info (spins are only on the surface)
    hemi_info = np.delete(hemi_info, sub_info)

    R_id = np.where(hemi_info == 0)[0]
    L_id = np.where(hemi_info == 1)[0]
    n_R = len(R_id)
    n_L = len(L_id)

    # If order is RL, we must invert the order of the spun annotations
    if order == 'RL':
        spins = np.zeros((spins_LR.shape))
        spins[R_id, :] = (spins_LR[np.where(hemi_centroids == 1)[0], :] - n_L)
        spins[L_id, :] = (spins_LR[np.where(hemi_centroids == 0)[0], :] + n_R)
    elif order == 'LR':
        spins = spins_LR

    # Only keep the information about the hemisphere we want
    if hemi == "L":
        if order == 'RL':
            spins = spins[np.where(hemi_info == 1)[0], :] - n_R
        elif order == 'LR':
            spins = spins[np.where(hemi_info == 1)[0], :]
    elif hemi == 'R':
        if order == 'RL':
            spins = spins[np.where(hemi_info == 0)[0], :]
        elif order == 'LR':
            spins = spins[np.where(hemi_info == 1)[0], :] - n_L

    spins = spins.astype(int)

    return spins
Exemplo n.º 5
0
def generate_dist(annot):
    """
    Parameters
    ----------
    annot : (2,) namedtuple
        With entries ('lh', 'rh') corresponding to annotation files for
        specified hemisphere for `fsaverage5` resolution

    Returns
    -------
    dist : (90, 90) np.ndarray
        Distance matrix for resamplings from different null frameworks and
        parcel centroid definition methods
    """

    # generate spins for all the parcel-based rotation methods using
    # three different methods for generating parcel centroid estimates
    orig_spins, vasa_spins, hung_spins = [], [], []
    for sptype, spins in zip(['original', 'vasa', 'hungarian'],
                             [orig_spins, vasa_spins, hung_spins]):
        for method in ['average', 'surface', 'geodesic']:
            # get parcel centroids using specified centroid method
            coords, hemi = nnsurf.find_parcel_centroids(lhannot=annot.lh,
                                                        rhannot=annot.rh,
                                                        version='fsaverage5',
                                                        method=method)
            # generate 10 spin resamples for specified spin method
            out = nnstats.gen_spinsamples(coords,
                                          hemi,
                                          n_rotate=10,
                                          method=sptype,
                                          seed=1234)
            spins.append(out)

    # stack and transpose for easy distance calculation
    all_spins = np.row_stack(
        (np.column_stack(orig_spins).T, np.column_stack(vasa_spins).T,
         np.column_stack(hung_spins).T))
    all_dist = cdist(all_spins, all_spins, metric='hamming')

    return all_dist
Exemplo n.º 6
0
        print('Generating naive permutations for fsaverage5 surface')
        rs = np.random.default_rng(1234)
        spins = np.column_stack([
            rs.permutation(len(coords)) for f in range(10000)
        ])
        putils.save_dir(fname, spins)

    # now pre-generate the parcellation spins for five methods. we can't
    # pre-generate the project-reduce-average method because that relies on the
    # data itself, but we _can_ use the above vertex-level spins for that
    for name, annotations in parcellations.items():
        print(f'PARCELLATION: {name}')
        for scale, annot in annotations.items():
            coords, hemi = nnsurf.find_parcel_centroids(lhannot=annot.lh,
                                                        rhannot=annot.rh,
                                                        version='fsaverage5',
                                                        surf='sphere',
                                                        method='surface')
            spin_fn = f'{scale}_spins.csv'

            fname = SPINDIR / name / 'vazquez-rodriguez' / spin_fn
            if not fname.exists():
                print(f'Generating V-R spins for {scale}')
                spins = nnstats.gen_spinsamples(coords, hemi, exact=False,
                                                n_rotate=10000, verbose=True,
                                                check_duplicates=False,
                                                seed=1234)
                putils.save_dir(fname, spins)

            fname = SPINDIR / name / 'vasa' / spin_fn
            if not fname.exists():