def full_brain_info(stat_map, mesh=None, threshold=None, cmap=plotting.cm.cold_hot): info = {} if mesh is None: mesh = datasets.fetch_surf_fsaverage5() # mesh = load_fsaverage() surf_maps = [ surface.vol_to_surf(stat_map, mesh['pial_{}'.format(h)]) for h in ['left', 'right'] ] colors, cmax, cmap, norm, at = colorscale(cmap, np.asarray(surf_maps).ravel(), threshold) for hemi, surf_map in zip(['left', 'right'], surf_maps): pial = mesh['pial_{}'.format(hemi)] sulc_depth_map = surface.load_surf_data(mesh['sulc_{}'.format(hemi)]) sulc_depth_map -= sulc_depth_map.min() sulc_depth_map /= sulc_depth_map.max() info['pial_{}'.format(hemi)] = to_plotly(pial) info['inflated_{}'.format(hemi)] = to_plotly( mesh['infl_{}'.format(hemi)]) vertexcolor = cmap(norm(surf_map).data) if threshold is not None: anat_color = cm.get_cmap('Greys')(sulc_depth_map) vertexcolor[np.abs(surf_map) < at] = anat_color[ np.abs(surf_map) < at] vertexcolor = np.asarray(vertexcolor * 255, dtype='uint8') info['vertexcolor_{}'.format(hemi)] = [ '#{:02x}{:02x}{:02x}'.format(*row) for row in vertexcolor ] info["cmin"], info["cmax"] = -cmax, cmax return info, colors
def test_vol_to_surf(): # test 3d niimg to cortical surface projection and invariance to a change # of affine mni = datasets.load_mni152_template() mesh = _generate_surf() _check_vol_to_surf_results(mni, mesh) fsaverage = datasets.fetch_surf_fsaverage5().pial_left _check_vol_to_surf_results(mni, fsaverage)
def draw_community_fsaverage(): fsaverage = datasets.fetch_surf_fsaverage5() template = load_img( '/home/carlo/workspace/communityalg/data/template_638.nii') memb = np.loadtxt('Memb1.txt') plot_parcellation(template, fsaverage['infl_left'], memb, bg_data=None, output_file='surf_inflated.png')
def plot_4d_image(img, names=None, output_dir=None, colors=None, view_types=['stat_map'], threshold=True, n_jobs=1, verbose=10): if not os.path.exists(output_dir): os.makedirs(output_dir) if colors is None: colors = repeat(None) if 'surf_stat_map_right' in view_types or 'surf_stat_map_left' in view_types: fetch_surf_fsaverage5() filename = img img = check_niimg(img, ensure_ndim=4) img.get_data() if names is None or isinstance(names, str): if names is None: dirname, filename = os.path.split(filename) names = filename.replace('.nii.gz', '') names = numbered_names(names) else: assert len(names) == img.get_shape()[3] mask = fetch_mask() masker = NiftiMasker(mask_img=mask).fit() components = masker.transform(img) n_components = len(components) threshold = np.percentile(np.abs(components), 100. * (1 - 1. / n_components)) if threshold else 0 imgs = Parallel(n_jobs=n_jobs, verbose=verbose)( delayed(plot_single)(img, name, output_dir, view_types, color, threshold=threshold) for name, img, color in zip(names, iter_img(img), colors)) return imgs
avg /= n_images return avg def get_collection_counts(img_fnames): counts = dict() for ii, image in enumerate(img_fnames): collection, name = image.split('/')[-2:] counts[collection] = counts.get(collection, 0) + 1 return counts # counts = get_collection_counts(images) target_img = nib.load(images[0]) desc = 'Average maps for %s' % search_term avg_effect = average_maps(images_effect, target_img, desc) desc = 'Average other maps' avg_other = average_maps(images_other, target_img, desc) contrast_img = nib.Nifti1Image(avg_effect - avg_other, target_img.affine) avg_img = nib.Nifti1Image(avg_effect, target_img.affine) fig = plot_glass_brain(contrast_img, plot_abs=False) fig.savefig('figures/encoder_%s.png' % search_term) # surface fsaverage = datasets.fetch_surf_fsaverage5() texture = surface.vol_to_surf(contrast_img, fsaverage.pial_right) plot_surf_stat_map(fsaverage.infl_right, texture, symmetric_cbar=True) plt.show()