def get_cammoun_schaefer(vers='fsaverage5', data_dir=None, networks='7'): """ Returns Cammoun 2012 and Schaefer 2018 atlases as dictionary Parameters ---------- vers : str, optional Which version of the atlases to get. Default: 'fsaverage5' data_dir : str or os.PathLike, optional Data directory where downloaded atlases should be stored. If not specified will default to $NNT_DATA or ~/nnt-data networks : {'7', '17'}, optional Which networks to get for Schaefer 2018 atlas. Default: '7' Returns ------- atlases : dict Where keys are 'atl-cammoun2012' and 'atl-schaefer2018' """ cammoun = nndata.fetch_cammoun2012(vers, data_dir=data_dir) schaefer = nndata.fetch_schaefer2018('fsaverage5', data_dir=data_dir) schaefer = { k: schaefer.get(k) for k in schaefer.keys() if 'Parcels7Networks' in k } return {'atl-cammoun2012': cammoun, 'atl-schaefer2018': schaefer}
def test_fetch_schaefer2018(tmpdir, version): keys = [ '{}Parcels{}Networks'.format(p, n) for p in range(100, 1001, 100) for n in [7, 17] ] schaefer = datasets.fetch_schaefer2018(version, data_dir=tmpdir, verbose=0) assert all(k in schaefer and len(schaefer[k]) == 2 and all( os.path.isfile(hemi) for hemi in schaefer[k]) for k in keys)
def get_schaefer2018_yeo(scale, data_dir=None): """ Returns Yeo RSN affiliations for Schaefer parcellation Parameters ---------- scale : str Scale of Schaefer et al., 2018 to use data_dir : str or os.PathLike Directory where parcellation should be downloaded to (or exists, if already downloaded). Default: None 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 """ # substring in schaefer parcel names indiciating yeo network affiliation schaefer_yeo = dict(Vis='visual', SomMot='somatomotor', DorsAttn='dorsal attention', SalVentAttn='ventral attention', Limbic='limbic', Cont='frontoparietal', Default='default mode') # get requested annotation files schaefer = nndata.fetch_schaefer2018('fsaverage5', data_dir=data_dir)[scale] network_labels, parcel_labels = [], [] for hemi in ('lh', 'rh'): # read in annotation file for given hemisphere annot = getattr(schaefer, hemi) labels, ctab, names = nib.freesurfer.read_annot(annot) names = [m.decode() for m in names] # create empty arrays for vertex- and parcel-level affiliations networks = np.zeros_like(labels) parcels = np.zeros(len(names), dtype=int) for n, parcel in enumerate(names): for abbrev, net in schaefer_yeo.items(): # check which network this parcel belongs to by looking for the # network substring in the parcel name and assign accordingly if abbrev in parcel: parcels[n] = YEO_CODES[net] networks[labels == n] = YEO_CODES[net] # store network affiliations for this hemisphere network_labels.append(networks) parcel_labels.append(parcels) return NETWORKS(np.hstack(network_labels), np.hstack(parcel_labels))
def _fetch_schaefer_parcellation(template: str, n_regions: int, seven_networks: int, data_dir: Path) -> List[np.ndarray]: """Fetches Schaefer parcellations.""" n_networks = 7 if seven_networks else 17 key = f"{n_regions}Parcels{n_networks}Networks" bunch = nnt_datasets.fetch_schaefer2018(version=template, data_dir=str(data_dir)) if template == "fslr32k": cifti = nib_load(bunch[key]) parcellation_full = np.squeeze(cifti.get_fdata()) parcellations = [x for x in np.reshape(parcellation_full, (2, -1))] else: parcellations = [read_annot(file)[0] for file in bunch[key]] parcellations[1][parcellations[1] != 0] += n_regions // 2 return parcellations
def get_schaefer2018_vek(scale, data_dir=None): """ Returns von Economo cytoarchitectonic classes for Schaefer parcellation Parameters ---------- scale : str Scale of Schaefer et al., 2018 to use data_dir : str or os.PathLike Directory where parcellation should be downloaded to (or exists, if already downloaded). Default: None Returns ------- labels : (2,) namedtuple Where the first entry ('vertices') is the vertex-level classes and the second entry ('parcels') is the parcel-level classes """ schaefer = nndata.fetch_schaefer2018('fsaverage5', data_dir=data_dir)[scale] vek_annots = _apply_vek_prob(data_dir=data_dir) return _parcellate_vek_classes(schaefer, vek_annots)
def plot_brain_surface(values, network, hemi=None, cmap="viridis", alpha=0.8, colorbar=True, centered=False, vmin=None, vmax=None, representation='surface'): ''' Function to plot data on the brain, on a surface parcellation. PARAMETERS ---------- values : ndarray (n,) Values to be plotted on the brain, where n is the number of nodes in the parcellation. network : dictionary Dictionary storing the network on associated with the values (to be used to identify the adequate surface parcellation) ''' cortical_hemi_mask = network['hemi_mask'][network['subcortex_mask'] == 0] n = len(cortical_hemi_mask) if hemi is None: hemi = network['info']['hemi'] if hemi == "L": scores = np.zeros((n)) + np.mean(values) scores[cortical_hemi_mask == 1] = values values = scores elif hemi == "R": scores = np.zeros((n)) + np.mean(values) scores[cortical_hemi_mask == 0] = values values = scores order = network["order"] noplot = network["noplot"] lh = network["lhannot"] rh = network["rhannot"] if os.path.isfile(lh) or os.path.isfile(rh) is False: fetch_cammoun2012(version='fsaverage') fetch_schaefer2018() # Adjust colormap based on parameters if centered is True: m = max(abs(np.amin(values)), np.amax(values)) vmin = -m vmax = m else: if vmin is None: vmin = np.amin(values) if vmax is None: vmax = np.amax(values) # Plot the brain surface im = plot_fsaverage(values, lhannot=lh, rhannot=rh, noplot=noplot, order=order, views=['lateral', 'm'], vmin=vmin, vmax=vmax, colormap=cmap, alpha=alpha, colorbar=colorbar, data_kws={'representation': representation}, show_toolbar=True) return im