def get_distmat(hemi, parcellation, scale, fn=None):
    if hemi not in ('lh', 'rh'):
        raise ValueError(f'Invalid hemishere designation {hemi}')

    if USE_CACHED and fn is not None:
        fn = DISTDIR / parcellation / 'nomedial' / f'{scale}_{hemi}_dist.npy'
        dist = np.load(fn, allow_pickle=False, mmap_mode='c').astype('float32')
    else:
        surf = nndata.fetch_fsaverage('fsaverage5', data_dir=ROIDIR)['pial']
        subj, spath = nnsurf.check_fs_subjid('fsaverage5')
        medial = Path(spath) / subj / 'label'
        medial_labels = [
            'unknown', 'corpuscallosum', '???',
            'Background+FreeSurfer_Defined_Medial_Wall'
        ]
        if parcellation == 'vertex':
            medial_path = medial / f'{hemi}.Medial_wall.label'
            dist = surface.get_surface_distance(getattr(surf, hemi),
                                                medial=medial_path,
                                                use_wb=False,
                                                verbose=True)
        else:
            annot = _get_annot(parcellation, scale)
            dist = surface.get_surface_distance(getattr(surf, hemi),
                                                getattr(annot, hemi),
                                                medial_labels=medial_labels,
                                                use_wb=False,
                                                verbose=True)
    return dist
Esempio n. 2
0
def _mod_medial(data, remove=True, val=0):
    """
    Removes (inserts) medial wall from (into) `data` from fsaverage5 surface

    Parameters
    ----------
    data : (20484,) array_like
        Surface data
    remove : bool, optional
        Whether to remove medial wall instead of inserting it. Assumes input
        has (does not have) medial wall. Default: True
    val : float, optional
        What value to insert if `remove=False`. Default: 0

    Returns
    -------
    out : np.ndarray
        Provided surface `data` with medial wall removed/inserted
    """

    subj, path = check_fs_subjid('fsaverage5')
    lh, rh = [
        nib.freesurfer.read_label(
            os.path.join(path, subj, 'label', f'{h}.Medial_wall.label'))
        for h in ('lh', 'rh')
    ]
    lhm, rhm = np.ones(10242, dtype=bool), np.ones(10242, dtype=bool)
    lhm[lh], rhm[rh] = False, False

    if remove:
        x, y = np.split(data, 2)
        return np.hstack((x[lhm], y[rhm]))
    else:
        x, y = np.split(data, [np.sum(lhm)])
        xd, yd = np.ones(10242) * val, np.ones(10242) * val
        xd[lhm], yd[rhm] = x, y
        return np.hstack((xd, yd))
Esempio n. 3
0
def get_vertex_yeo(*args, **kwargs):
    """
    Returns Yeo RSN affiliations for fsaverage5 surface

    Returns
    -------
    labels : (2,) namedtuple
        Where the first entry ('vertices') is the vertex-level RSN affiliations
        and the second entry ('parcels') is the parcel-level RSN affiliations
    """

    # check for FreeSurfer installation and get path to fsaverage5 label dir
    subjdir = Path(nnsurf.check_fs_subjid('fsaverage5')[-1]).resolve()
    labeldir = subjdir / 'fsaverage5' / 'label'

    # load Yeo2011 network affiliations and store in expected output
    network_labels, parcel_labels = [], []
    for hemi in ('lh', 'rh'):
        annot = labeldir / f'{hemi}.Yeo2011_7Networks_N1000.annot'
        labels, ctab, names = nib.freesurfer.read_annot(annot)
        network_labels.append(labels)
        parcel_labels.append(range(len(names)))

    return NETWORKS(np.hstack(network_labels), np.hstack(parcel_labels))
"""

from pathlib import Path

from netneurotools import datasets as nndata, freesurfer as nnsurf
from parspin import surface, utils as putils

ROIDIR = Path('./data/raw/rois').resolve()
DISTDIR = Path('./data/derivatives/geodesic').resolve()
N_PROC = 24  # parallelization of distance calculation
SURFACE = 'pial'  # surface on which to calculate distance

if __name__ == '__main__':
    parcellations = putils.get_cammoun_schaefer(data_dir=ROIDIR)
    surf = nndata.fetch_fsaverage('fsaverage5', data_dir=ROIDIR)[SURFACE]
    subj, spath = nnsurf.check_fs_subjid('fsaverage5')
    medial = Path(spath) / subj / 'label'
    medial_labels = [
        'unknown', 'corpuscallosum', '???',
        'Background+FreeSurfer_Defined_Medial_Wall'
    ]

    # get parcel distance matrices with this horrible nested for-loop :scream:
    for name, annotations in parcellations.items():
        for scale, annot in annotations.items():
            for hemi in ['lh', 'rh']:
                for allow_med in [True, False]:
                    med = 'medial' if allow_med else 'nomedial'
                    out = DISTDIR / name / med / f'{scale}_{hemi}_dist.csv'
                    if out.exists():
                        continue