def create_bem_surf(subject, subjects_dir=None, overwrite=False): from mne.bem import make_watershed_bem # Set file name ---------------------------------------------------------- bem_dir = op.join(subjects_dir, subject, 'bem') src_fname = op.join(bem_dir, subject + '-oct-6-src.fif') bem_fname = op.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = op.join(bem_dir, subject + '-5120-bem-sol.fif') # Create watershed BEM surfaces if overwrite or not op.isfile(op.join(bem_dir, subject + '-head.fif')): make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None, show=False) # Setup source space if overwrite or not op.isfile(src_fname): from mne import setup_source_space files = ['lh.white', 'rh.white', 'lh.sphere', 'rh.sphere'] for fname in files: if not op.exists(op.join(subjects_dir, subject, 'surf', fname)): raise RuntimeError('missing: %s' % fname) src = setup_source_space(subject=subject, subjects_dir=subjects_dir, spacing='oct6', surface='white', add_dist=True, n_jobs=-1, verbose=None) src.save(src_fname, overwrite=True) # Prepare BEM model if overwrite or not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) # run with a single layer model (enough for MEG data) surfs = make_bem_model(subject, conductivity=[0.3], subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem)
def anatomy_pipeline(subject, subjects_dir=None, overwrite=False): from mne.bem import make_watershed_bem from mne.commands.mne_make_scalp_surfaces import _run as make_scalp_surface from mne.utils import get_config if subjects_dir is None: subjects_dir = get_config('SUBJECTS_DIR') # Set file name ---------------------------------------------------------- bem_dir = op.join(subjects_dir, subject, 'bem') src_fname = op.join(bem_dir, subject + '-oct-6-src.fif') bem_fname = op.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = op.join(bem_dir, subject + '-5120-bem-sol.fif') # 0. Create watershed BEM surfaces if overwrite or not op.isfile(op.join(bem_dir, subject + '-head.fif')): check_libraries() if not check_freesurfer(subjects_dir=subjects_dir, subject=subject): warnings.warn('%s is probably not segmented correctly, check ' 'log.' % subject) make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # 1. Make scalp surfaces miss_surface = False # make_scalp is only for outer_skin for part in ['brain', 'inner_skull', 'outer_skin', 'outer_skull']: fname = op.join( bem_dir, 'watershed', '%s_%s_surface' % (subject, part)) if not op.isfile(fname): miss_surface = True if overwrite or miss_surface: make_scalp_surface(subjects_dir=subjects_dir, subject=subject, force=True, overwrite=True, verbose=None) # 2. Copy files outside watershed folder in case of bad manipulation miss_surface_copy = False for surface in ['inner_skull', 'outer_skull', 'outer_skin']: fname = op.join(bem_dir, '%s.surf' % surface) if not op.isfile(fname): miss_surface_copy = True if overwrite or miss_surface_copy: for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from shutil import copyfile from_file = op.join(bem_dir, 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(bem_dir, '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) copyfile(from_file, to_file) # 3. Setup source space if overwrite or not op.isfile(src_fname): from mne import setup_source_space, write_source_spaces check_libraries() files = ['lh.white', 'rh.white', 'lh.sphere', 'rh.sphere'] for fname in files: if not op.exists(op.join(subjects_dir, subject, 'surf', fname)): raise RuntimeError('missing: %s' % fname) src = setup_source_space(subject=subject, subjects_dir=subjects_dir, spacing='oct6', surface='white', add_dist=True, n_jobs=-1, verbose=None) write_source_spaces(src_fname, src) # 4. Prepare BEM model if overwrite or not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) check_libraries() surfs = make_bem_model(subject=subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem)
def mne_anatomy(subject, subjects_dir, overwrite=False): import warnings # Checks that watershed hasn't already been run for fname in [ 'fiducials.fif', 'head.fif', 'head-dense.fif', 'head-medium.fif', 'head-sparse.fif', 'inner_skull.surf', 'oct-6-src.fif' ]: fname = op.join(subjects_dir, subject, 'bem', subject + '-' + fname) if (not overwrite) and op.exists(fname): raise IOError('%s already exists. Set overwrite=True.' % fname) return # Create BEM surfaces make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # Copy files outside watershed folder in case of bad manipulation for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from_file = op.join(subjects_dir, subject, 'bem', 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(subjects_dir, subject, 'bem', '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) # Update file try: os.symlink(from_file, to_file) except OSError as e: # if disk is not NTFS, symoblic link isn't possible if e.strerror == 'Operation not permitted': from shutil import copyfile copyfile(from_file, to_file) # Make scalp surfaces make_scalp_surfaces(subjects_dir, subject, force='store_true', overwrite='store_true', verbose=None) # Setup source space src_fname = op.join(subjects_dir, subject, 'bem', subject + 'oct-6-src.fif') if not op.isfile(src_fname): from mne import setup_source_space setup_source_space(subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # Prepare BEM model bem_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem.fif') bem_sol_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem-sol.fif') if not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) surfs = make_bem_model(subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) # Make morphs to fsaverage if has it try: read_morph_map(subject, 'fsaverage', subjects_dir=subjects_dir) except IOError as e: if 'No such file or directory' in e.strerror: warnings.warn(e.strerror)
def mne_anatomy(subject, subjects_dir, overwrite=False): import warnings # Checks that watershed hasn't already been run for fname in ['fiducials.fif', 'head.fif', 'head-dense.fif', 'head-medium.fif', 'head-sparse.fif', 'inner_skull.surf', 'oct-6-src.fif']: fname = op.join(subjects_dir, subject, 'bem', subject + '-' + fname) if (not overwrite) and op.exists(fname): raise IOError('%s already exists. Set overwrite=True.' % fname) return # Create BEM surfaces make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # Copy files outside watershed folder in case of bad manipulation for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from_file = op.join(subjects_dir, subject, 'bem', 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(subjects_dir, subject, 'bem', '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) # Update file try: os.symlink(from_file, to_file) except OSError as e: # if disk is not NTFS, symoblic link isn't possible if e.strerror == 'Operation not permitted': from shutil import copyfile copyfile(from_file, to_file) # Make scalp surfaces make_scalp_surfaces(subjects_dir, subject, force='store_true', overwrite='store_true', verbose=None) # Setup source space src_fname = op.join(subjects_dir, subject, 'bem', subject + 'oct-6-src.fif') if not op.isfile(src_fname): from mne import setup_source_space setup_source_space(subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # Prepare BEM model bem_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem.fif') bem_sol_fname = op.join(subjects_dir, subject, 'bem', subject + '-5120-bem-sol.fif') if not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) surfs = make_bem_model(subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) # Make morphs to fsaverage if has it try: read_morph_map(subject, 'fsaverage', subjects_dir=subjects_dir) except IOError as e: if 'No such file or directory' in e.strerror: warnings.warn(e.strerror)
# Check or compute bem segmentation anatomy_pipeline(subject=subject, subjects_dir=subjects_dir, overwrite=False) if True: # Runs MNE/Freesurfer BEM models from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) for subject in bad_watershed: bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_fname = os.path.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = os.path.join(bem_dir, subject + '-5120-bem-sol.fif') # single layer surfs = make_bem_model(subject=subject, subjects_dir=subjects_dir, conductivity=(.3, )) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) from mne.viz import plot_bem if True: # Plot subjects' BEMs and source spaces figs = list() for meg_subject, subject in zip(range(1, 21), subjects_id): if subject in missing_mri + bad_mri: continue # Plot BEM fig = plot_bem(subject=subject, subjects_dir=subjects_dir, show=True)
if subject not in missing_mri + bad_watershed + bad_mri: continue # Check or compute bem segmentation anatomy_pipeline(subject=subject, subjects_dir=subjects_dir, overwrite=False) if True: # Runs MNE/Freesurfer BEM models from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) for subject in bad_watershed: bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_fname = os.path.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = os.path.join(bem_dir, subject + '-5120-bem-sol.fif') # single layer surfs = make_bem_model(subject=subject, subjects_dir=subjects_dir, conductivity=(.3,)) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem) from mne.viz import plot_bem if True: # Plot subjects' BEMs and source spaces figs = list() for meg_subject, subject in zip(range(1, 21), subjects_id): if subject in missing_mri + bad_mri: continue # Plot BEM fig = plot_bem(subject=subject, subjects_dir=subjects_dir, show=True) # plot source space
def create_bem_surf(subject, subjects_dir=None, overwrite=False): # from jr.meg # noqa # from mne.bem import make_watershed_bem # from mne.commands.mne_make_scalp_surfaces import _run as make_scalp_surface from mne.utils import get_config if subjects_dir is None: subjects_dir = get_config('SUBJECTS_DIR') # Set file name ---------------------------------------------------------- bem_dir = op.join(subjects_dir, subject, 'bem') src_fname = op.join(bem_dir, subject + '-oct-6-src.fif') bem_fname = op.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = op.join(bem_dir, subject + '-5120-bem-sol.fif') # Skip make_watershed_bem and make_scalp_surface because it is # already done from bash shell (freesurfer command) miss_surface_copy = False for surface in ['inner_skull', 'outer_skull', 'outer_skin']: fname = op.join(bem_dir, '%s.surf' % surface) if not op.isfile(fname): miss_surface_copy = True if overwrite or miss_surface_copy: for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from shutil import copyfile from_file = op.join(bem_dir, 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(bem_dir, '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) copyfile(from_file, to_file) # 3. Setup source space if overwrite or not op.isfile(src_fname): from mne import setup_source_space check_libraries() files = ['lh.white', 'rh.white', 'lh.sphere', 'rh.sphere'] for fname in files: if not op.exists(op.join(subjects_dir, subject, 'surf', fname)): raise RuntimeError('missing: %s' % fname) setup_source_space(subject=subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # 4. Prepare BEM model if overwrite or not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) check_libraries() # run with a single layer model (enough for MEG data) surfs = make_bem_model(subject, conductivity=[0.3], subjects_dir=subjects_dir) # surfs = make_bem_model(subject=subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem)
def anatomy_pipeline(subject, subjects_dir=None, overwrite=False): from mne.bem import make_watershed_bem from mne.commands.mne_make_scalp_surfaces import _run as make_scalp_surface from mne.utils import get_config if subjects_dir is None: subjects_dir = get_config('SUBJECTS_DIR') # Set file name ---------------------------------------------------------- bem_dir = op.join(subjects_dir, subject, 'bem') src_fname = op.join(bem_dir, subject + '-oct-6-src.fif') bem_fname = op.join(bem_dir, subject + '-5120-bem.fif') bem_sol_fname = op.join(bem_dir, subject + '-5120-bem-sol.fif') # 0. Create watershed BEM surfaces if overwrite or not op.isfile(op.join(bem_dir, subject + '-head.fif')): check_libraries() if not check_freesurfer(subjects_dir=subjects_dir, subject=subject): warnings.warn('%s is probably not segmented correctly, check ' 'log.' % subject) make_watershed_bem(subject=subject, subjects_dir=subjects_dir, overwrite=True, volume='T1', atlas=False, gcaatlas=False, preflood=None) # 1. Make scalp surfaces miss_surface = False # make_scalp is only for outer_skin for part in ['brain', 'inner_skull', 'outer_skin', 'outer_skull']: fname = op.join( bem_dir, 'watershed', '%s_%s_surface' % (subject, part)) if not op.isfile(fname): miss_surface = True if overwrite or miss_surface: make_scalp_surface(subjects_dir=subjects_dir, subject=subject, force=True, overwrite=True, verbose=None) # 2. Copy files outside watershed folder in case of bad manipulation miss_surface_copy = False for surface in ['inner_skull', 'outer_skull', 'outer_skin']: fname = op.join(bem_dir, '%s.surf' % surface) if not op.isfile(fname): miss_surface_copy = True if overwrite or miss_surface_copy: for surface in ['inner_skull', 'outer_skull', 'outer_skin']: from shutil import copyfile from_file = op.join(bem_dir, 'watershed/%s_%s_surface' % (subject, surface)) to_file = op.join(bem_dir, '%s.surf' % surface) if op.exists(to_file): os.remove(to_file) copyfile(from_file, to_file) # 3. Setup source space if overwrite or not op.isfile(src_fname): from mne import setup_source_space check_libraries() files = ['lh.white', 'rh.white', 'lh.sphere', 'rh.sphere'] for fname in files: if not op.exists(op.join(subjects_dir, subject, 'surf', fname)): raise RuntimeError('missing: %s' % fname) setup_source_space(subject=subject, subjects_dir=subjects_dir, fname=src_fname, spacing='oct6', surface='white', overwrite=True, add_dist=True, n_jobs=-1, verbose=None) # 4. Prepare BEM model if overwrite or not op.exists(bem_sol_fname): from mne.bem import (make_bem_model, write_bem_surfaces, make_bem_solution, write_bem_solution) check_libraries() surfs = make_bem_model(subject=subject, subjects_dir=subjects_dir) write_bem_surfaces(fname=bem_fname, surfs=surfs) bem = make_bem_solution(surfs) write_bem_solution(fname=bem_sol_fname, bem=bem)