def test_brain_morphometry_view_gets_created(): vert_coords, faces, morphometry_data, meta_data = bl.subject( 'subject1', subjects_dir=TEST_DATA_DIR) fig = mlab.figure(bgcolor=(0, 0, 0), size=(800, 600)) surface = bv.brain_morphometry_view(fig, vert_coords, faces, morphometry_data) assert type(fig) == mayavi.core.scene.Scene
def test_brain_atlas_view_gets_created(): vert_coords, faces, morphometry_data, morphometry_meta_data = bl.subject( 'subject1', subjects_dir=TEST_DATA_DIR, load_morphometry_data=False) vertex_labels, label_colors, label_names, atlas_meta_data = bl.annot( 'subject1', TEST_DATA_DIR, 'aparc') fig = mlab.figure(bgcolor=(0, 0, 0), size=(800, 600)) surface = bv.brain_atlas_view(fig, vert_coords, faces, vertex_labels, label_colors, label_names) assert type(fig) == mayavi.core.scene.Scene
def test_brain_label_view_gets_created(): vert_coords, faces, morphometry_data, morphometry_meta_data = bl.subject( 'subject1', subjects_dir=TEST_DATA_DIR, load_morphometry_data=False) verts_in_label, label_meta_data = bl.label('subject1', TEST_DATA_DIR, 'cortex', meta_data=morphometry_meta_data) fig = mlab.figure(bgcolor=(0, 0, 0), size=(800, 600)) surface = bv.brain_label_view(fig, vert_coords, faces, verts_in_label) assert type(fig) == mayavi.core.scene.Scene
def test_brainload_works_and_testdata_exists(): vert_coords, faces, morphometry_data, meta_data = bl.subject( 'subject1', subjects_dir=TEST_DATA_DIR) assert len(meta_data) == 20
def atlasviewer(): """ Brain atlas data viewer. A viewer for brain atlas and label data. Typically used to inspect cortical parcellation results. """ # Parse command line arguments parser = argparse.ArgumentParser( description="View brain label data or brain annotation / atlas data.") parser.add_argument( "subject", help= "The subject you want to load. String, a directory under the subjects_dir." ) parser.add_argument("mode", help="The mode. One of ('atlas', 'label').") parser.add_argument( "data", help= "The data to load from the label dub dir of the subject, without the ?h part and the file extensions. If mode is 'atlas', something like 'aparc'. If mode is 'label', something like 'cortex'." ) parser.add_argument( "-s", "--surface", help="The surface to load. String, defaults to 'white'.", default="white") parser.add_argument( "-d", "--subjects_dir", help= "The subjects_dir containing the subject. Defaults to environment variable SUBJECTS_DIR.", default="") parser.add_argument( "-e", "--hemi", help= "The hemisphere to load. One of ('both', 'lh, 'rh'). Defaults to 'both'.", default="both", choices=['lh', 'rh', 'both']) parser.add_argument("-i", "--interactive", help="Display brain plot in an interactive window.", action="store_true") parser.add_argument( "-o", "--outputfile", help="Output image file name. String, defaults to 'brain_<mode>.png'.", default=None) parser.add_argument("-v", "--verbose", help="Increase output verbosity.", action="store_true") parser.add_argument( "-x", "--mesh-export", help= "Mesh export output filename. The file extension should be '.obj' or '.ply' to indicate the output format, otherwise obj is used. Optional, if not given at all, then no mesh will be exported.", default="") args = parser.parse_args() verbose = False if args.verbose: verbose = True print("Verbosity turned on.") if args.subjects_dir == "": subjects_dir = os.getenv('SUBJECTS_DIR') else: subjects_dir = args.subjects_dir subject_id = args.subject mode = args.mode outputfile = args.outputfile if outputfile is None: if mode == "label": outputfile = "brain_label.png" else: outputfile = "brain_atlas.png" surface = args.surface hemi = args.hemi data = args.data interactive = False if args.interactive: interactive = True mlab.options.offscreen = not interactive vert_coords, faces, morphometry_data, morphometry_meta_data = bl.subject( subject_id, subjects_dir=subjects_dir, surf=surface, hemi=hemi, load_morphometry_data=False) fig_title = 'Atlasviewer: %s: %s on surface %s' % (subject_id, data, surface) cfg, cfg_file = bv.get_config() fig = mlab.figure(fig_title, bgcolor=(1, 1, 1), size=(bv.cfg_getint('figure', 'width', 800), bv.cfg_getint('figure', 'height', 600))) if mode == 'atlas': if verbose: print( "Loading atlas %s for subject %s from subjects dir %s: displaying on surface %s for hemisphere %s." % (data, subject_id, subjects_dir, surface, hemi)) vertex_labels, label_colors, label_names, atlas_meta_data = bl.annot( subject_id, subjects_dir, data, hemi=hemi, orig_ids=False) brain_mesh = bv.brain_atlas_view(fig, vert_coords, faces, vertex_labels, label_colors, label_names) else: if verbose: print( "Loading label %s for subject %s from subjects dir %s: displaying on surface %s for hemisphere %s." % (data, subject_id, subjects_dir, surface, hemi)) verts_in_label, label_meta_data = bl.label( subject_id, subjects_dir, data, hemi=hemi, meta_data=morphometry_meta_data) brain_mesh = bv.brain_label_view(fig, vert_coords, faces, verts_in_label) if args.mesh_export != "": colormap_name = bv.cfg_get('meshexport', 'colormap', 'viridis') colormap_adjust_alpha_to = bv.cfg_getint('meshexport', 'colormap_adjust_alpha_to', -1) print("Exporting brain mesh to file '%s'..." % args.mesh_export) bv.export_mesh_to_file( args.mesh_export, vert_coords, faces, morphometry_data=morphometry_data, colormap_name=colormap_name, colormap_adjust_alpha_to=colormap_adjust_alpha_to) print("Saving brain view to file '%s'..." % (outputfile)) mlab.savefig(outputfile) if interactive: if verbose: print( "Interactive mode set, displaying brain plot in interactive window." ) bv.show() sys.exit(0)
def brainviewer(): """ Brain morphometry data viewer. A viewer for morphometry data. Supports data from the subject itself and subject data that has been mapped to a common subject like fsaverage. """ # Parse command line arguments parser = argparse.ArgumentParser( description="View brain morphometry data.") parser.add_argument( "subject", help= "The subject you want to load. String, a directory under the subjects_dir." ) parser.add_argument( "-d", "--subjects_dir", help= "The subjects_dir containing the subject. Defaults to environment variable SUBJECTS_DIR.", default="") parser.add_argument( "-m", "--measure", help= "The measure to load. String, defaults to None (no morphometry data). Examples: 'area' or 'thickness'.", default=None) parser.add_argument( "-s", "--surface", help="The surface to load. String, defaults to 'white'.", default="white") parser.add_argument( "-e", "--hemi", help= "The hemisphere to load. One of ('both', 'lh, 'rh'). Defaults to 'both'.", default="both", choices=['lh', 'rh', 'both']) parser.add_argument( "-c", "--common-subject-mode", help="Load data mapped to a common or average subject.", action="store_true") parser.add_argument( "-a", "--average-subject", help= "The common or average subject to use. String, defaults to 'fsaverage'. Ignored unless -c is active.", default="fsaverage") parser.add_argument( "-f", "--fwhm", help= "The smoothing or fwhm setting to use for the common subject measure. String, defaults to '10'. Ignored unless -c is active.", default="10") parser.add_argument("-i", "--interactive", help="Display brain plot in an interactive window.", action="store_true") parser.add_argument( "-o", "--outputfile", help= "Output image file name. String, defaults to 'brain_morphometry.png'.", default="brain_morphometry.png") parser.add_argument("-n", "--no-clip", help="Do not clip morphometry values.", action="store_true") parser.add_argument("-v", "--verbose", help="Increase output verbosity.", action="store_true") parser.add_argument( "-x", "--mesh-export", help= "Mesh export output filename. The file extension should be '.obj' or '.ply' to indicate the output format, otherwise obj is used. Optional, if not given at all, then no mesh will be exported.", default="") args = parser.parse_args() cfg, cfg_file = bv.get_config() verbose = False if args.verbose: verbose = True print("Verbosity turned on.") if cfg_file is None: print( "Using internal default configuration. No config file found at '%s'." % bv.get_default_config_filename()) else: print("Using configuration from file: '%s'" % cfg_file) if args.subjects_dir == "": subjects_dir = os.getenv('SUBJECTS_DIR') else: subjects_dir = args.subjects_dir subject_id = args.subject measure = args.measure surface = args.surface hemi = args.hemi interactive = False if args.interactive: interactive = True mlab.options.offscreen = not interactive load_morphometry_data = measure is not None if args.common_subject_mode: fwhm = args.fwhm average_subject = args.average_subject if verbose: print( "Loading data mapped to common subject %s for subject %s from subjects dir '%s': measure %s of surface %s for hemisphere %s at fwhm %s." % (average_subject, subject_id, subjects_dir, measure, surface, hemi, fwhm)) vert_coords, faces, morphometry_data, meta_data = bl.subject_avg( subject_id, subjects_dir=subjects_dir, measure=measure, surf=surface, hemi=hemi, fwhm=fwhm, average_subject=average_subject, load_morphometry_data=load_morphometry_data) else: if verbose: print( "Loading data for subject %s from subjects dir '%s': measure %s of surface %s for hemisphere %s." % (subject_id, subjects_dir, measure, surface, hemi)) vert_coords, faces, morphometry_data, meta_data = bl.subject( subject_id, subjects_dir=subjects_dir, measure=measure, surf=surface, hemi=hemi, load_morphometry_data=load_morphometry_data) if not load_morphometry_data: if verbose: print("No morphometry data loaded, setting all values to zero.") morphometry_data = np.zeros((vert_coords.shape[0], ), dtype=float) morphometry_data = morphometry_data.astype(float) if verbose: if hemi == "lh" or hemi == "both": print("Loaded lh surface mesh from file '%s'." % meta_data["lh.surf_file"]) if load_morphometry_data: print("Loaded lh morphometry data from file '%s'." % meta_data["lh.morphometry_file"]) if hemi == "rh" or hemi == "both": print("Loaded rh surface from file '%s'." % meta_data["rh.surf_file"]) if load_morphometry_data: print("Loaded rh morphometry data from file '%s'." % meta_data["rh.morphometry_file"]) print("Loaded mesh consisting of %d vertices and %d faces." % (vert_coords.shape[0], faces.shape[0])) if load_morphometry_data: print("Loaded morphometry data for %d vertices." % (morphometry_data.shape[0])) fig_title = 'Brainviewer: %s: %s of surface %s' % (subject_id, measure, surface) if args.mesh_export != "": colormap_name = bv.cfg_get('meshexport', 'colormap', 'viridis') colormap_adjust_alpha_to = bv.cfg_getint('meshexport', 'colormap_adjust_alpha_to', -1) clip_values_export = bv.cfg_getboolean('meshexport', 'clip_values', True) if clip_values_export and not args.no_clip: clip_values_lower = bv.cfg_getint('meshexport', 'clip_values_lower', 5) clip_values_upper = bv.cfg_getint('meshexport', 'clip_values_upper', 95) print( "Clipping exported values below percentile %d and above %d." % (clip_values_lower, clip_values_upper)) morphometry_data_for_export = bex.clip_data_at_percentiles( morphometry_data, lower=clip_values_lower, upper=clip_values_upper) else: morphometry_data_for_export = morphometry_data print("Exporting brain mesh to file '%s'..." % args.mesh_export) bv.export_mesh_to_file( args.mesh_export, vert_coords, faces, morphometry_data=morphometry_data_for_export, colormap_name=colormap_name, colormap_adjust_alpha_to=colormap_adjust_alpha_to) fig = mlab.figure(fig_title, bgcolor=(1, 1, 1), size=(bv.cfg_getint('figure', 'width', 800), bv.cfg_getint('figure', 'height', 600))) mesh_args = { 'representation': bv.cfg_get('mesh', 'representation', 'surface'), 'colormap': bv.cfg_get('mesh', 'colormap', 'cool') } clip_values_live = bv.cfg_getboolean('mesh', 'clip_values', True) if clip_values_live and not args.no_clip: clip_values_lower = bv.cfg_getint('mesh', 'clip_values_lower', 5) clip_values_upper = bv.cfg_getint('mesh', 'clip_values_upper', 95) print("Clipping visualized values below percentile %d and above %d." % (clip_values_lower, clip_values_upper)) morphometry_data_live = bex.clip_data_at_percentiles( morphometry_data, lower=clip_values_lower, upper=clip_values_upper) else: morphometry_data_live = morphometry_data brain_mesh = bv.brain_morphometry_view(fig, vert_coords, faces, morphometry_data_live, **mesh_args) print("Saving brain view to image file '%s'..." % (args.outputfile)) mlab.savefig(args.outputfile) if interactive: if verbose: print( "Interactive mode set, displaying brain plot in interactive window." ) bv.show() sys.exit(0)