Beispiel #1
0
def create_eeg_mesh(subject, excludes=[], overwrite_faces_verts=True):
    try:
        from scipy.spatial import Delaunay
        from src.utils import trig_utils
        input_file = op.join(MMVT_DIR, subject, 'eeg', 'eeg_positions.npz')
        mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
        faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
        f = np.load(input_file)
        verts = f['pos']
        excluded_inds = [np.where(f['names'] == e)[0] for e in excludes]
        # verts = np.delete(verts, excluded_inds, 0)
        verts_tup = [(x, y, z) for x, y, z in verts]
        tris = Delaunay(verts_tup)
        faces = tris.convex_hull
        areas = [trig_utils.poly_area(verts[poly]) for poly in tris.convex_hull]
        inds = [k for k, s in enumerate(areas) if s > np.percentile(areas, 97)]
        faces = np.delete(faces, inds, 0)
        utils.write_ply_file(verts, faces, mesh_ply_fname, True)
        utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, overwrite_faces_verts,
                                   utils.namebase(faces_verts_out_fname))
        np.savez(input_file, pos=f['pos'], names=f['names'], tri=faces, excludes=excludes)
    except:
        print('Error in create_eeg_mesh!')
        print(traceback.format_exc())
        return False
    return True
Beispiel #2
0
def create_eeg_mesh(subject, excludes=[], overwrite_faces_verts=True):
    try:
        from scipy.spatial import Delaunay
        from src.utils import trig_utils
        input_file = op.join(MMVT_DIR, subject, 'eeg', 'eeg_positions.npz')
        mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
        faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg',
                                        'eeg_faces_verts.npy')
        f = np.load(input_file)
        verts = f['pos']
        excluded_inds = [np.where(f['names'] == e)[0] for e in excludes]
        # verts = np.delete(verts, excluded_inds, 0)
        verts_tup = [(x, y, z) for x, y, z in verts]
        tris = Delaunay(verts_tup)
        faces = tris.convex_hull
        areas = [
            trig_utils.poly_area(verts[poly]) for poly in tris.convex_hull
        ]
        inds = [k for k, s in enumerate(areas) if s > np.percentile(areas, 97)]
        faces = np.delete(faces, inds, 0)
        utils.write_ply_file(verts, faces, mesh_ply_fname, True)
        utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname,
                                   overwrite_faces_verts,
                                   utils.namebase(faces_verts_out_fname))
        np.savez(input_file,
                 pos=f['pos'],
                 names=f['names'],
                 tri=faces,
                 excludes=excludes)
    except:
        print('Error in create_eeg_mesh!')
        print(traceback.format_exc())
        return False
    return True
Beispiel #3
0
def create_eeg_mesh_from_obj(subject, obj_fname):
    mesh_ply_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_helmet.ply')
    # mesh = obj.Obj(obj_fname)
    # verts, faces = mesh.vertices, mesh.faces
    verts, faces = utils.read_obj_file(obj_fname)
    utils.write_ply_file(verts, faces, mesh_ply_fname)
    faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
    utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, True,
                               utils.namebase(faces_verts_out_fname))
Beispiel #4
0
def calc_faces_verts_dic(subject, atlas, overwrite=False):
    # hemis_plus = HEMIS + ['cortex']
    ply_files = [op.join(MMVT_DIR, subject, 'surf', '{}.pial.npz'.format(hemi)) for hemi in utils.HEMIS]
    out_files = [op.join(MMVT_DIR, subject, 'faces_verts_{}.npy'.format(hemi)) for hemi in utils.HEMIS]
    subcortical_plys = glob.glob(op.join(MMVT_DIR, subject, 'subcortical', '*.ply'))
    errors = {}
    if len(subcortical_plys) > 0:
        faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, 'subcortical', '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in subcortical_plys]
        ply_files.extend(subcortical_plys)
        out_files.extend(faces_verts_dic_fnames)
    for hemi in utils.HEMIS:
        labels_plys = glob.glob(op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '*.ply'))
        if len(labels_plys) > 0:
            faces_verts_dic_fnames = [op.join(MMVT_DIR, subject, '{}.pial.{}'.format(atlas, hemi), '{}_faces_verts.npy'.format(
                utils.namebase(ply))) for ply in labels_plys]
            ply_files.extend(labels_plys)
            out_files.extend(faces_verts_dic_fnames)

    for ply_file, out_file in zip(ply_files, out_files):
        if not overwrite and op.isfile(out_file):
            # print('{} already exist.'.format(out_file))
            continue
        # ply_file = op.join(SUBJECTS_DIR, subject,'surf', '{}.pial.ply'.format(hemi))
        # print('preparing a lookup table for {}'.format(ply_file))
        verts, faces = utils.read_ply_file(ply_file)
        errors = utils.calc_ply_faces_verts(verts, faces, out_file, overwrite, utils.namebase(ply_file), errors)
    if len(errors) > 0:
        for k, message in errors.items():
            print('{}: {}'.format(k, message))
    return len(errors) == 0
def create_faces_verts(subject):
    skull_fol = op.join(MMVT_DIR, subject, 'skull')
    errors = {}
    for skull_surf in ['inner_skull', 'outer_skull']:
        ply_fname = op.join(skull_fol, '{}.ply'.format(skull_surf))
        verts, faces = utils.read_ply_file(ply_fname)
        faces_verts_fname = op.join(skull_fol,
                                    'faces_verts_{}.npy'.format(skull_surf))
        errors = utils.calc_ply_faces_verts(verts, faces,
                                            faces_verts_fname, False,
                                            utils.namebase(ply_fname), errors)
    if len(errors) > 0:
        for k, message in errors.items():
            print('{}: {}'.format(k, message))
Beispiel #6
0
def recreate_mesh_faces_verts(subject, ply_fname):
    verts, faces = utils.read_ply_file(ply_fname)
    faces_verts_out_fname = op.join(MMVT_DIR, subject, 'eeg', 'eeg_faces_verts.npy')
    utils.calc_ply_faces_verts(verts, faces, faces_verts_out_fname, True,
                               utils.namebase(faces_verts_out_fname))