Exemplo n.º 1
0
def mne_watershed_bem(sbj_dir, sbj_id):

    from mne.bem import make_watershed_bem

    print 'call make_watershed_bem'
    make_watershed_bem(sbj_id, sbj_dir, overwrite=True)

    return sbj_id
Exemplo n.º 2
0
def mne_watershed_bem(sbj_dir, sbj_id):

    from mne.bem import make_watershed_bem

    print "call make_watershed_bem"
    make_watershed_bem(sbj_id, sbj_dir, overwrite=True)

    return sbj_id
Exemplo n.º 3
0
def create_bem_sol(sbj_dir, sbj_id):
    """Create bem solution."""
    import os.path as op
    import mne

    from mne.bem import make_watershed_bem
    from mne.report import Report

    report = Report()

    bem_dir = op.join(sbj_dir, sbj_id, 'bem')

    surf_name = 'inner_skull.surf'
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + surf_name)
    inner_skull_fname = op.join(bem_dir, surf_name)

    # check if bem-sol was created, if not creates the bem sol using C MNE
    bem_fname = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
    model_fname = op.join(bem_dir, '%s-5120-bem.fif' % sbj_id)

    if not op.isfile(bem_fname):
        # chek if inner_skull surf exists, if not BEM computation is
        # performed by MNE python functions mne.bem.make_watershed_bem
        if not (op.isfile(sbj_inner_skull_fname)
                or op.isfile(inner_skull_fname)):
            print("%s ---> FILE NOT FOUND!!!---> BEM "
                  "computed" % inner_skull_fname)
            make_watershed_bem(sbj_id, sbj_dir, overwrite=True)
        else:
            print(("\n*** inner skull %s surface "
                   "exists!!!\n" % inner_skull_fname))

        # Create a BEM model for a subject
        surfaces = mne.make_bem_model(sbj_id,
                                      ico=4,
                                      conductivity=[0.3],
                                      subjects_dir=sbj_dir)

        # Write BEM surfaces to a fiff file
        mne.write_bem_surfaces(model_fname, surfaces)

        # Create a BEM solution using the linear collocation approach
        bem = mne.make_bem_solution(surfaces)
        mne.write_bem_solution(bem_fname, bem)

        print(('\n*** BEM solution file %s written ***\n' % bem_fname))

        # add BEM figures to a Report
        report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
        report_filename = op.join(bem_dir, "BEM_report.html")
        print(('\n*** REPORT file %s written ***\n' % report_filename))
        print(report_filename)
        report.save(report_filename, open_browser=False, overwrite=True)
    else:
        bem = bem_fname
        print(('\n*** BEM solution file %s exists!!! ***\n' % bem_fname))

    return bem
def create_bem_sol(sbj_dir, sbj_id):
    import os.path as op
    import mne

    from mne.bem import make_watershed_bem
    from mne.report import Report

    report = Report()

    bem_dir = op.join(sbj_dir, sbj_id, 'bem')

    surf_name = 'inner_skull.surf'
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + surf_name)
    inner_skull_fname = op.join(bem_dir, surf_name)

    # check if bem-sol was created, if not creates the bem sol using C MNE
    bem_fname = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
    model_fname = op.join(bem_dir, '%s-5120-bem.fif' % sbj_id)

    if not op.isfile(bem_fname):
        # chek if inner_skull surf exists, if not BEM computation is
        # performed by MNE python functions mne.bem.make_watershed_bem
        if not (op.isfile(sbj_inner_skull_fname) or
                op.isfile(inner_skull_fname)):
            print inner_skull_fname + '---> FILE NOT FOUND!!!---> BEM computed'
            make_watershed_bem(sbj_id, sbj_dir, overwrite=True)
        else:
            print '\n*** inner skull %s surface exists!!!\n' % inner_skull_fname

        # Create a BEM model for a subject
        surfaces = mne.make_bem_model(sbj_id, ico=4, conductivity=[0.3],
                                      subjects_dir=sbj_dir)

        # Write BEM surfaces to a fiff file
        mne.write_bem_surfaces(model_fname, surfaces)

        # Create a BEM solution using the linear collocation approach
        bem = mne.make_bem_solution(surfaces)
        mne.write_bem_solution(bem_fname, bem)

        print '\n*** BEM solution file %s written ***\n' % bem_fname

        # add BEM figures to a Report
        report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
        report_filename = op.join(bem_dir, "BEM_report.html")
        print '\n*** REPORT file %s written ***\n' % report_filename
        print report_filename
        report.save(report_filename, open_browser=False, overwrite=True)
    else:
        bem = bem_fname
        print '\n*** BEM solution file %s exists!!! ***\n' % bem_fname

    return bem
Exemplo n.º 5
0
def run():
    """Run command."""
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__)

    parser.add_option("-s", "--subject", dest="subject",
                      help="Subject name (required)", default=None)
    parser.add_option("-d", "--subjects-dir", dest="subjects_dir",
                      help="Subjects directory", default=None)
    parser.add_option("-o", "--overwrite", dest="overwrite",
                      help="Write over existing files", action="store_true")
    parser.add_option("-v", "--volume", dest="volume",
                      help="Defaults to T1", default='T1')
    parser.add_option("-a", "--atlas", dest="atlas",
                      help="Specify the --atlas option for mri_watershed",
                      default=False, action="store_true")
    parser.add_option("-g", "--gcaatlas", dest="gcaatlas",
                      help="Use the subcortical atlas", default=False,
                      action="store_true")
    parser.add_option("-p", "--preflood", dest="preflood",
                      help="Change the preflood height", default=None)
    parser.add_option("--copy", dest="copy",
                      help="Use copies instead of symlinks for surfaces",
                      action="store_true")
    parser.add_option("--verbose", dest="verbose",
                      help="If not None, override default verbose level",
                      default=None)

    options, args = parser.parse_args()

    if options.subject is None:
        parser.print_help()
        sys.exit(1)

    subject = options.subject
    subjects_dir = options.subjects_dir
    overwrite = options.overwrite
    volume = options.volume
    atlas = options.atlas
    gcaatlas = options.gcaatlas
    preflood = options.preflood
    copy = options.copy
    verbose = options.verbose

    make_watershed_bem(subject=subject, subjects_dir=subjects_dir,
                       overwrite=overwrite, volume=volume, atlas=atlas,
                       gcaatlas=gcaatlas, preflood=preflood, copy=copy,
                       verbose=verbose)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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 compute_LF_matrix(sbj_id, sbj_dir, raw_info, aseg, spacing, labels):
    import os.path as op
    import mne

    from mne.bem import make_watershed_bem
    from mne.report import Report

    from nipype.utils.filemanip import split_filename as split_f

    from neuropype_ephy.compute_fwd_problem import create_mixed_source_space

    report = Report()

    bem_dir = op.join(sbj_dir, sbj_id, 'bem')

    surf_name = 'inner_skull.surf'
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + surf_name)
    inner_skull_fname = op.join(bem_dir, surf_name)

    data_path, raw_fname, ext = split_f(raw_info['filename'])

    if aseg:
        fwd_filename = op.join(data_path, '%s-%s-aseg-fwd.fif'
                               % (raw_fname, spacing))
    else:
        fwd_filename = op.join(data_path, '%s-%s-fwd.fif'
                               % (raw_fname, spacing))

    # check if we have just created the fwd matrix
    if not op.isfile(fwd_filename):
        # check if bem-sol was created, if not creates the bem sol using C MNE
        bem_fname = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
        model_fname = op.join(bem_dir, '%s-5120-bem.fif' % sbj_id)
        if not op.isfile(bem_fname):
            # chek if inner_skull surf exists, if not BEM computation is
            # performed by MNE python functions mne.bem.make_watershed_bem
            if not (op.isfile(sbj_inner_skull_fname) or
                    op.isfile(inner_skull_fname)):
                print sbj_inner_skull_fname + '---> FILE NOT FOUND!!! ---> BEM is computed'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                make_watershed_bem(sbj_id, sbj_dir, overwrite=True)
            else:
                print '*** inner skull surface exists!!!'

            # Create a BEM model for a subject
            surfaces = mne.make_bem_model(sbj_id, ico=4, conductivity=[0.3],
                                          subjects_dir=sbj_dir)
            # Write BEM surfaces to a fiff file
            mne.write_bem_surfaces(model_fname, surfaces)

            # Create a BEM solution using the linear collocation approach
            bem = mne.make_bem_solution(surfaces)
            mne.write_bem_solution(bem_fname, bem)

            print '*** BEM solution file %s written ***' % bem_fname
            # add BEM figures to a Report
            report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
            report_filename = op.join(bem_dir, "BEM_report.html")
            print report_filename
            report.save(report_filename, open_browser=False, overwrite=True)
        else:
            bem = bem_fname
            print '*** BEM solution file %s exists!!!' % bem_fname

        # check if source space exists, if not it creates using mne-python fun
        # we have to create the cortical surface source space even when aseg is
        # True
        src_fname = op.join(bem_dir, '%s-%s-src.fif' % (sbj_id, spacing))
        if not op.isfile(src_fname):
            src = mne.setup_source_space(sbj_id, subjects_dir=sbj_dir,
                                         fname=True,
                                         spacing=spacing.replace('-', ''),
                                         add_dist=False, overwrite=True,
                                         n_jobs=2)
            print '*** source space file %s written ***' % src_fname         
        else:
            print '*** source space file %s exists!!!' % src_fname
            src = mne.read_source_spaces(src_fname)

        if aseg:
            src = create_mixed_source_space(sbj_dir, sbj_id, spacing,
                                            labels, src)

        n = sum(src[i]['nuse'] for i in range(len(src)))
        print('il src space contiene %d spaces e %d vertici' % (len(src), n))

        # check if the co-registration file was created
        # if not raise an runtime error
        trans_fname = op.join(data_path, '%s-trans.fif' % raw_fname)
        if not op.isfile(trans_fname):
            raise RuntimeError('coregistration file %s NOT found!!!'
                               % trans_fname)

        # if all is ok creates the fwd matrix
        mne.make_forward_solution(raw_info, trans_fname, src, bem,
                                  fwd_filename,
                                  mindist=5.0, # ignore sources <= 0mm from inner skull
                                  meg=True, eeg=False,
                                  n_jobs=2,
                                  overwrite=True)

    else:
        print '*** FWD file %s exists!!!' % fwd_filename

    return fwd_filename
def run():
    """Run command."""
    from mne.commands.utils import get_optparser, _add_verbose_flag

    parser = get_optparser(__file__)

    parser.add_option("-s",
                      "--subject",
                      dest="subject",
                      help="Subject name (required)",
                      default=None)
    parser.add_option("-d",
                      "--subjects-dir",
                      dest="subjects_dir",
                      help="Subjects directory",
                      default=None)
    parser.add_option("-o",
                      "--overwrite",
                      dest="overwrite",
                      help="Write over existing files",
                      action="store_true")
    parser.add_option("-v",
                      "--volume",
                      dest="volume",
                      help="Defaults to T1",
                      default='T1')
    parser.add_option("-a",
                      "--atlas",
                      dest="atlas",
                      help="Specify the --atlas option for mri_watershed",
                      default=False,
                      action="store_true")
    parser.add_option("-g",
                      "--gcaatlas",
                      dest="gcaatlas",
                      help="Specify the --brain_atlas option for "
                      "mri_watershed",
                      default=False,
                      action="store_true")
    parser.add_option("-p",
                      "--preflood",
                      dest="preflood",
                      help="Change the preflood height",
                      default=None)
    parser.add_option("--copy",
                      dest="copy",
                      help="Use copies instead of symlinks for surfaces",
                      action="store_true")
    parser.add_option("-t",
                      "--T1",
                      dest="T1",
                      help="Whether or not to pass the -T1 flag "
                      "(can be true, false, 0, or 1). "
                      "By default it takes the same value as gcaatlas.",
                      default=None)
    parser.add_option("-b",
                      "--brainmask",
                      dest="brainmask",
                      help="The filename for the brainmask output file "
                      "relative to the "
                      "$SUBJECTS_DIR/$SUBJECT/bem/watershed/ directory.",
                      default="ws")
    _add_verbose_flag(parser)

    options, args = parser.parse_args()

    if options.subject is None:
        parser.print_help()
        sys.exit(1)

    subject = options.subject
    subjects_dir = options.subjects_dir
    overwrite = options.overwrite
    volume = options.volume
    atlas = options.atlas
    gcaatlas = options.gcaatlas
    preflood = options.preflood
    copy = options.copy
    brainmask = options.brainmask
    T1 = options.T1
    if T1 is not None:
        T1 = T1.lower()
        _check_option("--T1", T1, ('true', 'false', '0', '1'))
        T1 = T1 in ('true', '1')
    verbose = options.verbose

    make_watershed_bem(subject=subject,
                       subjects_dir=subjects_dir,
                       overwrite=overwrite,
                       volume=volume,
                       atlas=atlas,
                       gcaatlas=gcaatlas,
                       preflood=preflood,
                       copy=copy,
                       T1=T1,
                       brainmask=brainmask,
                       verbose=verbose)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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 run():
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__)

    parser.add_option("-s",
                      "--subject",
                      dest="subject",
                      help="Subject name (required)",
                      default=None)
    parser.add_option("-d",
                      "--subjects-dir",
                      dest="subjects_dir",
                      help="Subjects directory",
                      default=None)
    parser.add_option("-o",
                      "--overwrite",
                      dest="overwrite",
                      help="Write over existing files",
                      action="store_true")
    parser.add_option("-v",
                      "--volume",
                      dest="volume",
                      help="Defaults to T1",
                      default='T1')
    parser.add_option("-a",
                      "--atlas",
                      dest="atlas",
                      help="Specify the --atlas option for mri_watershed",
                      default=False,
                      action="store_true")
    parser.add_option("-g",
                      "--gcaatlas",
                      dest="gcaatlas",
                      help="Use the subcortical atlas",
                      default=False,
                      action="store_true")
    parser.add_option("-p",
                      "--preflood",
                      dest="preflood",
                      help="Change the preflood height",
                      default=None)
    parser.add_option("--verbose",
                      dest="verbose",
                      help="If not None, override default verbose level",
                      default=None)

    options, args = parser.parse_args()

    if options.subject is None:
        parser.print_help()
        sys.exit(1)

    subject = options.subject
    subjects_dir = options.subjects_dir
    overwrite = options.overwrite
    volume = options.volume
    atlas = options.atlas
    gcaatlas = options.gcaatlas
    preflood = options.preflood
    verbose = options.verbose

    make_watershed_bem(subject=subject,
                       subjects_dir=subjects_dir,
                       overwrite=overwrite,
                       volume=volume,
                       atlas=atlas,
                       gcaatlas=gcaatlas,
                       preflood=preflood,
                       verbose=verbose)
else:
    from mne.io import Raw
    raw = Raw(raw_fname, preload=False)
    
    
save_dir = '/'.join(raw_fname.split('/')[:-1])
print('Save/read directory: %s' % save_dir)
    
    
trans_fname = op.join(save_dir,  '-trans.fif')
bem_sol_fname = op.join(freesurfer_dir, subject, 'bem',
                                subject + '-5120-bem-sol.fif')
oct_fname = op.join(freesurfer_dir, subject, 'bem',
                            subject + '-oct-6-src.fif')
fwd_fname = op.join(save_dir, subject + '-meg-fwd.fif')
    
    
    
overwrite=True
    
from mne.bem import make_watershed_bem
    
    
    
    
make_watershed_bem(subject=subject, subjects_dir=freesurfer_dir,overwrite=True, volume='T1', atlas=False,gcaatlas=False, preflood=None)
    
    
    
    
Exemplo n.º 14
0
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)