示例#1
0
def freesurfer_annot_to_vtks(surface_file, label_file, output_stem='',
                             json_file='files_to_load.json',
                             sample_rate=1,
                             force=False, verbose=True, output_dir=DATA_DIR):
    """ Splits a surface file into vtk files based on regions in the label file.
    """
    def print_verbose(*args):
        """Print only if verbose True"""
        if verbose:
            print(args)

    #
    vtk_dir = os.path.join(output_dir, os.path.dirname(output_stem))

    # Make the output directory
    if not os.path.exists(output_dir):
        os.makedirs(vtk_dir)

    # Convert the surface file to vtk
    if os.path.splitext(surface_file)[1] == '.vtk':
        surface_vtk = surface_file
    else:
        surface_vtk = os.path.join(vtk_dir,
                                   os.path.basename(surface_file) + '.vtk')
        if force or not os.path.exists(surface_vtk):
            print_verbose('Converting surface to vtk: %s' % surface_file)
            from mindboggle.mio.vtks import freesurfer_surface_to_vtk
            freesurfer_surface_to_vtk(surface_file, surface_vtk)

    # Convert the data file to vtk
    if os.path.splitext(label_file)[1] == '.vtk':
        label_vtk = label_file
        labels, names = None, None
    else:
        label_vtk = os.path.join(vtk_dir,
                                 os.path.basename(label_file) + '.vtk')
        if force or not os.path.exists(label_vtk):
            print_verbose('Converting data to vtk: %s' % label_file)
            from mindboggle.mio.vtks import freesurfer_annot_to_vtk
            freesurfer_annot_to_vtk(label_file, surface_vtk, label_vtk)
        labels, _, names = nib.freesurfer.read_annot(label_file)

        used_labels = np.unique(labels[labels >= 1])
        used_names = np.asarray(names)[used_labels]
        print_verbose("Unused areas: %s" % (set(names) - set(used_names)))
        names = used_names
        labels = used_labels

    # Expand the data file to multiple vtks
    print_verbose('Expanding vtk data to multiple files.')
    from mindboggle.mio.vtks import explode_scalars
    explode_output_stem = os.path.join(output_dir, output_stem)
    explode_scalars(label_vtk, output_stem=explode_output_stem)
    output_vtks = filter(lambda p: p not in [surface_vtk, label_vtk],
                         glob.glob(explode_output_stem + '*.vtk'))

    print_verbose('Downsampling vtk files.')
    for vtk_file in output_vtks:
        downsample_vtk(vtk_file, sample_rate=sample_rate)

    if json_file:
        print_verbose('Creating download manifest file.')
        if labels is None:
            names = labels = [os.path.splitext(vtk_file)[0] for vtk_file in output_vtks]

        vtk_dict = dict([(name, output_stem + '%s.vtk' % lbl)
                         for lbl, name in zip(labels, names)])

        json_file = os.path.join(output_dir, json_file)
        with open(json_file, 'wb') as fp:
            json.dump(dict(filename=vtk_dict), fp)

    return json_file
示例#2
0
from mindboggle.guts.mesh import decimate_file


subj_path = os.environ['SUBJECTS_DIR']
fsavg_path = os.path.join(subj_path, 'fsaverage')

surf_file = os.path.join(fsavg_path, 'surf', 'lh.pial')
label_file = os.path.join(fsavg_path, 'label', 'lh.aparc.annot')

surf_vtk = 'lh_surf.vtk'
label_vtk = 'lh_label.vtk'

# Convert surface and labels to vtk, break into ROIs
print('Generating vtks')
if not os.path.exists(surf_vtk):
    freesurfer_surface_to_vtk(surf_file, surf_vtk)
if not os.path.exists(label_vtk):
    freesurfer_annot_to_vtk(label_file, surf_vtk, label_vtk)

# Break into ROIs
if True:
    # downsample_vtk(label_vtk, sample_rate=sample_rate)
    explode_scalars(label_vtk, output_stem='lh_roi_')
roi_dict = dict([(i, roi_vtk) for i, roi_vtk in enumerate(glob.glob('lh_roi_*.vtk'))])

    # Downsample
if False:
    print('Downsampling vtks')
    for roi_vtk in roi_dict.values():
        decimate_file(roi_vtk, reduction=0.5, output_vtk=roi_vtk,
                      save_vtk=True, smooth_steps=100)