Esempio n. 1
0
 def _load_one_run(self, subject, run, preload=True):
     if get_config("MNE_DATASETS_EEGBCI_PATH") is None:
         set_config("MNE_DATASETS_EEGBCI_PATH",
                    osp.join(osp.expanduser("~"), "mne_data"))
     raw_fname = eegbci.load_data(subject,
                                  runs=[run],
                                  verbose="ERROR",
                                  base_url=BASE_URL)[0]
     raw = read_raw_edf(raw_fname, preload=preload, verbose="ERROR")
     raw.rename_channels(lambda x: x.strip("."))
     raw.rename_channels(lambda x: x.upper())
     # fmt: off
     renames = {
         "AFZ": "AFz",
         "PZ": "Pz",
         "FPZ": "Fpz",
         "FCZ": "FCz",
         "FP1": "Fp1",
         "CZ": "Cz",
         "OZ": "Oz",
         "POZ": "POz",
         "IZ": "Iz",
         "CPZ": "CPz",
         "FP2": "Fp2",
         "FZ": "Fz",
     }
     # fmt: on
     raw.rename_channels(renames)
     raw.set_montage(mne.channels.make_standard_montage("standard_1005"))
     return raw
Esempio n. 2
0
def set_download_dir(path):
    """Set the download directory if required to change from default mne path

    Parameters
    ----------
    path : None | str
    The new storage location, if it does not exist, a warning is raised and the
    path is created
    If None, and MNE_DATA config does not exist, a warning is raised and the
    storage location is set to the MNE default directory

    """
    if path is None:
        if get_config("MNE_DATA") is None:
            print(
                "MNE_DATA is not already configured. It will be set to "
                "default location in the home directory - " +
                osp.join(osp.expanduser("~"), "mne_data") +
                "All datasets will be downloaded to this location, if anything is "
                "already downloaded, please move manually to this location")

            set_config("MNE_DATA", osp.join(osp.expanduser("~"), "mne_data"))
    else:
        # Check if the path exists, if not, create it
        if not osp.isdir(path):
            print("The path given does not exist, creating it..")
            os.makedirs(path)
        set_config("MNE_DATA", path)
Esempio n. 3
0
    def data_path(self,
                  subject,
                  path=None,
                  force_update=False,
                  update_path=None,
                  verbose=None):
        if subject not in self.subject_list:
            raise (ValueError("Invalid subject number"))

        sub = "{:02d}".format(subject)
        sign = self.code.split()[1]
        key = "MNE_DATASETS_{:s}_PATH".format(sign)
        key_dest = "MNE-{:s}-data".format(sign.lower())
        if get_config(key) is None:
            set_config(key, osp.join(osp.expanduser("~"), "mne_data"))
        path = osp.join(_get_path(None, key, sign), key_dest)

        filelist = fs_get_file_list(self.figshare_id)
        reg = fs_get_file_hash(filelist)
        fsn = fs_get_file_id(filelist)
        gb = pooch.create(path=path, base_url=MAMEM_URL, registry=reg)

        spath = []
        for f in fsn.keys():
            if f[2:4] == sub:
                spath.append(gb.fetch(fsn[f]))

        # _do_path_update(path, update_path, key, sign)
        return spath
def main(dataset):
    """Download the testing data."""
    # Save everything 'MNE_DATA' dir ... defaults to ~/mne_data
    data_dir = mne.get_config(key='MNE_DATA', default=False)
    if not data_dir:
        mne.set_config('MNE_DATA', DEFAULT_DATA_DIR)
        os.makedirs(DEFAULT_DATA_DIR, exist_ok=True)
        data_dir = DEFAULT_DATA_DIR

    urls_dict = _provide_testing_data(dataset)
    get_dict = _provide_get_dict(dataset)

    for dsname, url in urls_dict.items():
        print('\n----------------------')
        dspath = op.join(data_dir, dsname)
        # install the dataset
        print('datalad installing "{}"'.format(dsname))
        dataset = dl.install(path=dspath, source=url)

        # XXX: git-annex bug: https://github.com/datalad/datalad/issues/3583
        # if datalad fails, use "get" twice, or set `n_jobs=1`
        if dsname == 'ds003104':
            n_jobs = 16
        else:
            n_jobs = 1

        # get the first subject
        for to_get in get_dict[dsname]:
            print('datalad get data "{}" for "{}"'.format(to_get, dsname))
            dataset.get(to_get, jobs=n_jobs)
Esempio n. 5
0
def run_freesurfer_command(command, subjects_dir):
    "Run a FreeSurfer command"
    env = os.environ.copy()
    env['SUBJECTS_DIR'] = subjects_dir

    # find FREESURFER_HOME
    fs_home = mne.get_config('FREESURFER_HOME')
    save_fs_home = False
    while True:
        problem = fs_home_problem(fs_home)
        if problem:
            save_fs_home = True
            message = "Please select the directory where FreeSurfer is installed"
            print problem
            print message
            fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
            if fs_home is False:
                raise RuntimeError("Could not find FreeSurfer")
        else:
            break
    if save_fs_home:
        mne.set_config('FREESURFER_HOME', fs_home)

    # adjust environment
    env['FREESURFER_HOME'] = fs_home
    bin_path = os.path.join(fs_home, 'bin')
    if bin_path not in env['PATH']:
        env['PATH'] = ':'.join((bin_path, env['PATH']))

    # run command
    run_subprocess(command, env=env)
Esempio n. 6
0
 def _load_one_run(self, subject, run, preload=True):
     if get_config('MNE_DATASETS_EEGBCI_PATH') is None:
         set_config('MNE_DATASETS_EEGBCI_PATH',
                    osp.join(osp.expanduser("~"), "mne_data"))
     raw_fname = eegbci.load_data(subject,
                                  runs=[run],
                                  verbose='ERROR',
                                  base_url=BASE_URL)[0]
     raw = read_raw_edf(raw_fname, preload=preload, verbose='ERROR')
     raw.rename_channels(lambda x: x.strip('.'))
     raw.rename_channels(lambda x: x.upper())
     raw.rename_channels({
         'AFZ': 'AFz',
         'PZ': 'Pz',
         'FPZ': 'Fpz',
         'FCZ': 'FCz',
         'FP1': 'Fp1',
         'CZ': 'Cz',
         'OZ': 'Oz',
         'POZ': 'POz',
         'IZ': 'Iz',
         'CPZ': 'CPz',
         'FP2': 'Fp2',
         'FZ': 'Fz'
     })
     raw.set_montage(mne.channels.make_standard_montage('standard_1005'))
     return raw
Esempio n. 7
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""
        data = {}
        if get_config('MNE_DATASETS_EEGBCI_PATH') is None:
            set_config('MNE_DATASETS_EEGBCI_PATH',
                       osp.join(osp.expanduser("~"), "mne_data"))

        # baseline runs
        data['baseline_eye_open'] = self._load_one_run(subject, 1)
        data['baseline_eye_closed'] = self._load_one_run(subject, 2)

        # hand runs
        for run in self.hand_runs:
            data['run_%d' % run] = self._load_one_run(subject, run)

        # feet runs
        for run in self.feet_runs:
            raw = self._load_one_run(subject, run)

            # modify stim channels to match new event ids. for feets runs,
            # hand = 2 modified to 4, and feet = 3, modified to 5
            stim = raw._data[-1]
            raw._data[-1, stim == 2] = 4
            raw._data[-1, stim == 3] = 5
            data['run_%d' % run] = raw

        return {"session_0": data}
Esempio n. 8
0
    def _get_single_subject_data(self, subject):
        """return data for a single subject"""
        data = {}
        if get_config("MNE_DATASETS_EEGBCI_PATH") is None:
            set_config("MNE_DATASETS_EEGBCI_PATH",
                       osp.join(osp.expanduser("~"), "mne_data"))

        # hand runs
        for run in self.hand_runs:
            raw = self._load_one_run(subject, run)
            stim = raw.annotations.description.astype(np.dtype("<U10"))
            stim[stim == "T0"] = "rest"
            stim[stim == "T1"] = "left_hand"
            stim[stim == "T2"] = "right_hand"
            raw.annotations.description = stim
            data["run_%d" % run] = raw

        # feet runs
        for run in self.feet_runs:
            raw = self._load_one_run(subject, run)
            # modify stim channels to match new event ids. for feets runs,
            # hand = 2 modified to 4, and feet = 3, modified to 5
            stim = raw.annotations.description.astype(np.dtype("<U10"))
            stim[stim == "T0"] = "rest"
            stim[stim == "T1"] = "hands"
            stim[stim == "T2"] = "feet"
            raw.annotations.description = stim
            data["run_%d" % run] = raw

        return {"session_0": data}
Esempio n. 9
0
def data_dl(url, sign, path=None, force_update=False, update_path=True, verbose=None):
    """Download file from url to specified path

    This function should replace data_path as the MNE will not support the download
    of dataset anymore. This version is using Pooch.

    Parameters
    ----------
    url : str
        Path to remote location of data
    sign : str
        Signifier of dataset
    path : None | str
        Location of where to look for the BNCI data storing location.
        If None, the environment variable or config parameter
        ``MNE_DATASETS_(signifier)_PATH`` is used. If it doesn't exist, the
        "~/mne_data" directory is used. If the dataset
        is not found under the given path, the data
        will be automatically downloaded to the specified folder.
    force_update : bool
        Force update of the dataset even if a local copy exists.
    update_path : bool | None
        If True, set the MNE_DATASETS_(signifier)_PATH in mne-python
        config to the given path. If None, the user is prompted.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see :func:`mne.verbose`).

    Returns
    -------
    path : list of str
        Local path to the given data file. This path is contained inside a list
        of length one, for compatibility.
    """
    sign = sign.upper()
    key = "MNE_DATASETS_{:s}_PATH".format(sign)
    key_dest = "MNE-{:s}-data".format(sign.lower())
    if get_config(key) is None:
        set_config(key, osp.join(osp.expanduser("~"), "mne_data"))
    path = _get_path(path, key, sign)
    destination = _url_to_local_path(url, osp.join(path, key_dest))

    # Fetch the file
    if not osp.isfile(destination) or force_update:
        if osp.isfile(destination):
            os.remove(destination)
        if not osp.isdir(osp.dirname(destination)):
            os.makedirs(osp.dirname(destination))
        known_hash = None
    else:
        known_hash = file_hash(destination)
    dlpath = retrieve(
        url, known_hash, fname=osp.basename(url), path=osp.dirname(destination)
    )

    # Offer to update the path
    if update_path:
        _do_path_update(path, update_path, key, sign)

    return dlpath
Esempio n. 10
0
def data_path(url,
              sign,
              path=None,
              force_update=False,
              update_path=True,
              verbose=None):
    """Get path to local copy of given dataset URL.

    This is a low-level function useful for getting a local copy of a
    remote dataset

    Parameters
    ----------
    url : str
        Path to remote location of data
    sign : str
        Signifier of dataset
    path : None | str
        Location of where to look for the BNCI data storing location.
        If None, the environment variable or config parameter
        ``MNE_DATASETS_(signifier)_PATH`` is used. If it doesn't exist, the
        "~/mne_data" directory is used. If the dataset
        is not found under the given path, the data
        will be automatically downloaded to the specified folder.
    force_update : bool
        Force update of the dataset even if a local copy exists.
    update_path : bool | None
        If True, set the MNE_DATASETS_(signifier)_PATH in mne-python
        config to the given path. If None, the user is prompted.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see :func:`mne.verbose`).

    Returns
    -------
    path : list of str
        Local path to the given data file. This path is contained inside a list
        of length one, for compatibility.

    """  # noqa: E501
    sign = sign.upper()
    key = 'MNE_DATASETS_{:s}_PATH'.format(sign)
    key_dest = 'MNE-{:s}-data'.format(sign.lower())
    if get_config(key) is None:
        set_config(key, osp.join(osp.expanduser("~"), "mne_data"))
    path = _get_path(path, key, sign)
    destination = _url_to_local_path(url, op.join(path, key_dest))
    # Fetch the file
    if not op.isfile(destination) or force_update:
        if op.isfile(destination):
            os.remove(destination)
        if not op.isdir(op.dirname(destination)):
            os.makedirs(op.dirname(destination))
        _fetch_file(url, destination, print_destination=False)

    # Offer to update the path
    _do_path_update(path, update_path, key, sign)
    return destination
Esempio n. 11
0
def run_anat(subject, subjects_dir='/cluster/transcend/MRI/WMA/recons'):
    mne.set_config('SUBJECTS_DIR',subjects_dir)
    findsession_log, dicom_dir = run_findsession(subject, subjects_dir=subjects_dir)
    dicom_path = read_findsession_log_file(findsession_log)
    copy_dicom(dicom_path, dicom_dir)
    dicom_log = run_dcmunpack(subject, subjects_dir=subjects_dir)
    t1 = read_dicom_log_file(dicom_log)
    process_subject_anatomy(subject, t1, subjects_dir='/cluster/transcend/MRI/WMA/recons')
    process_subject_bem(subject, subjects_dir=subjects_dir)
Esempio n. 12
0
def make_forward_solution(experiment,
                          subject,
                          spacing,
                          process_slug=DEFAULT_PROC):
    struct = sub_to_struct[experiment][subject]
    if struct == 'NA':
        raise IOError(
            'Freesurfer Reconstruction has not yet been done for this subject. See the Freesurfer Recommended Reconstruction page.')

    trans_fname = TRANS_FNAME.format(experiment=experiment, subject=subject, struct=struct)

    if not os.path.isfile(trans_fname):
        raise IOError(
            'Coregistration has not yet been done for this subject. Use mne_analyze on big-brain and follow MNE handbook chapter 7.')

    trans = mne.read_trans(trans_fname)

    fwd_path = FWD_PATH.format(experiment=experiment, subject=subject)
    if not os.path.exists(fwd_path):
        try:
            os.mkdir(fwd_path)
        except:
            os.mkdir(FWD_PATH.format(experiment=experiment, subject=''))
            os.mkdir(fwd_path)

    fwd_fname = FWD_FNAME.format(fwd_path=fwd_path, subject=subject, experiment=experiment, process_slug=process_slug,
                                 struct=struct, spacing=spacing)

    raw = mne.io.Raw(PROC_FNAME.format(experiment=experiment, subject=subject, process_slug=process_slug))

    bem_path = [fn for fn in os.listdir(BEM_PATH.format(struct=struct)) if fnmatch.fnmatch(fn, '*-bem-sol.fif')]

    if len(bem_path) == 0:
        raise IOError('BEM has not yet been done for this subject. See MNE_pipeline_2018.sh')

    bem_fname = pjoin(BEM_PATH.format(struct=struct), bem_path[0])

    src_file = SRC_FNAME.format(struct=struct, spacing=spacing)

    mne.set_config('SUBJECTS_DIR', SUBJ_DIR)

    # Not sure how to make sure this runs effectively
    if os.path.isfile(src_file):
        src = mne.read_source_spaces(src_file)
    else:
        src = mne.setup_source_space(struct, spacing='oct6')
        mne.write_source_spaces(src_file, src)

    fwd = mne.make_forward_solution(raw.info, trans,
                                    src=src,
                                    bem=bem_fname)

    mne.write_forward_solution(fwd_fname, fwd)

    return fwd
Esempio n. 13
0
    def __init__(
        self,
        evaluation_class,
        paradigm_class,
        suffix="",
        overwrite=False,
        hdf5_path=None,
        additional_columns=None,
    ):
        """
        class that will abstract result storage
        """
        from moabb.evaluations.base import BaseEvaluation
        from moabb.paradigms.base import BaseParadigm

        assert issubclass(evaluation_class, BaseEvaluation)
        assert issubclass(paradigm_class, BaseParadigm)

        if additional_columns is None:
            self.additional_columns = []
        else:
            assert all([isinstance(ac, str) for ac in additional_columns])
            self.additional_columns = additional_columns

        if hdf5_path is None:
            if get_config("MOABB_RESULTS") is None:
                set_config("MOABB_RESULTS",
                           osp.join(osp.expanduser("~"), "mne_data"))
            self.mod_dir = _get_path(None, "MOABB_RESULTS", "results")
            # was previously stored in the moabb source file folder:
            # self.mod_dir = osp.dirname(osp.abspath(inspect.getsourcefile(moabb)))
        else:
            self.mod_dir = osp.abspath(hdf5_path)
        self.filepath = osp.join(
            self.mod_dir,
            "results",
            paradigm_class.__name__,
            evaluation_class.__name__,
            "results{}.hdf5".format("_" + suffix),
        )

        os.makedirs(osp.dirname(self.filepath), exist_ok=True)
        self.filepath = self.filepath

        if overwrite and osp.isfile(self.filepath):
            os.remove(self.filepath)

        if not osp.isfile(self.filepath):
            with h5py.File(self.filepath, "w") as f:
                f.attrs["create_time"] = np.string_(
                    "{:%Y-%m-%d, %H:%M}".format(datetime.now()))
Esempio n. 14
0
    def data_path(self,
                  subject,
                  path=None,
                  force_update=False,
                  update_path=None,
                  verbose=None):
        if subject not in self.subject_list:
            raise (ValueError("Invalid subject number"))

        if get_config('MNE_DATASETS_EEGBCI_PATH') is None:
            set_config('MNE_DATASETS_EEGBCI_PATH',
                       osp.join(osp.expanduser("~"), "mne_data"))
        paths = eegbci.load_data(subject,
                                 runs=[1, 2] + self.hand_runs + self.feet_runs,
                                 verbose=verbose)
        return paths
Esempio n. 15
0
def main(dataset):
    """Download the testing data."""
    # Save everything 'MNE_DATA' dir ... defaults to ~/mne_data
    mne_data_dir = mne.get_config(key='MNE_DATA', default=False)
    if not mne_data_dir:
        mne.set_config('MNE_DATA', str(DEFAULT_DATA_DIR))
        DEFAULT_DATA_DIR.mkdir(exist_ok=True)
        mne_data_dir = DEFAULT_DATA_DIR
    else:
        mne_data_dir = Path(mne_data_dir)

    ds_names = DATASET_OPTIONS.keys() if not dataset else (dataset, )

    for ds_name in ds_names:
        print('\n----------------------')
        ds_path = mne_data_dir / ds_name
        _download(ds_name=ds_name, ds_path=ds_path)
Esempio n. 16
0
 def __init__(self, subject, experiment, paths_dic):
     self.subject = subject
     self.experiment = experiment
     self.pth_root = op.expanduser(paths_dic["root"])
     self.pth_out = op.expanduser(paths_dic["out"])
     self.pth_FS = op.expanduser(paths_dic["FS"])
     mne.set_config('SUBJECTS_DIR', self.pth_FS)
     self.check_outdir()
     self.pth_subject = op.join(self.pth_root, subject)
     if self.check_MXfilter_sbjs() is True:
         print('Using MaxFilter preprocessed data')
         self.raw = mne.io.read_raw_fif(self.pth_raw, preload=False)
     else:
         self.pth_raw = glob.glob(op.join(self.pth_subject, subject) + '_'
                                  + experiment + '*')[-1]
         self.raw = mne.io.read_raw_ctf(self.pth_raw, preload=False)
         if self.raw.compensation_grade != 3:
             self.raw.apply_gradient_compensation(3)
Esempio n. 17
0
    def _load_one_run(self, subject, run, preload=True):
        if get_config('MNE_DATASETS_EEGBCI_PATH') is None:
            set_config('MNE_DATASETS_EEGBCI_PATH',
                       osp.join(osp.expanduser("~"), "mne_data"))
        raw_fname = eegbci.load_data(subject,
                                     runs=[run],
                                     verbose='ERROR',
                                     base_url=BASE_URL)[0]
        raw = read_raw_edf(raw_fname, preload=preload, verbose='ERROR')
        raw.rename_channels(lambda x: x.strip('.'))
        raw.rename_channels(lambda x: x.upper())
        raw.rename_channels({
            'AFZ': 'AFz',
            'PZ': 'Pz',
            'FPZ': 'Fpz',
            'FCZ': 'FCz',
            'FP1': 'Fp1',
            'CZ': 'Cz',
            'OZ': 'Oz',
            'POZ': 'POz',
            'IZ': 'Iz',
            'CPZ': 'CPz',
            'FP2': 'Fp2',
            'FZ': 'Fz'
        })
        raw.set_montage(mne.channels.make_standard_montage('standard_1005'))

        # creat simulate stim channel
        # mne >= 0.18
        events, _ = mne.events_from_annotations(raw)
        stim_channel = np.zeros((1, raw.n_times))
        for event in events:
            stim_channel[0, event[0]] = event[2]

        info = mne.create_info(['STI 014'],
                               raw.info['sfreq'],
                               ch_types=['stim'])
        raw = raw.add_channels([mne.io.RawArray(stim_channel, info)],
                               force_update_info=True)
        return raw
Esempio n. 18
0
def _ask_user_for_bin_dir(package):
    """
    Change the location from which binaries are used.

    Parameters
    ----------
    package : str
        Binary package for which to set the directory. One from:
        ``['mne', 'freesurfer', 'edfapi']``
    """
    title = "Select %s Directory" % package
    message = "Please select the directory of the %s package." % package
    while True:
        answer = ui.ask_dir(title, message, must_exist=True)
        if answer:
            if test_root(package, answer):
                mne.set_config(_env_vars[package], answer)
                return answer
            else:
                ui.message("Wrong Directory", "This is not the right directory.")
        else:
            raise IOError("%s directory not set" % package)
Esempio n. 19
0
def _ask_user_for_bin_dir(package):
    """
    Change the location from which binaries are used.

    Parameters
    ----------
    package : str
        Binary package for which to set the directory. One from:
        ``['mne', 'freesurfer', 'edfapi']``
    """
    title = "Select %s Directory" % package
    message = "Please select the directory of the %s package." % package
    while True:
        answer = ui.ask_dir(title, message, must_exist=True)
        if answer:
            if test_root(package, answer):
                mne.set_config(_env_vars[package], answer)
                return answer
            else:
                ui.message("Wrong Directory", "This is not the right directory.")
        else:
            raise IOError("%s directory not set" % package)
Esempio n. 20
0
def download_sample_data(dataset="ssvep", subject=1, session=1):
    """Download BCI data for example purpose

    Parameters
    ----------
    dataset : str
        type of the dataset, could be "ssvep", "p300" or "imagery"
        Default is "ssvep", as other are not implemented
    subject : int
        Subject id, dataset specific (default: 1)
    session : int, default 1
        Session number%load , dataset specific (default: 1)

    Returns
    -------
    destination : str
        Path to downloaded data
    """
    if dataset == "ssvep":
        DATASET_URL = 'https://zenodo.org/record/2392979/files/'
        url = '{:s}subject{:02d}_run{:d}_raw.fif'.format(DATASET_URL,
                                                         subject, session + 1)
        sign = 'SSVEPEXO'
        key, key_dest = 'MNE_DATASETS_SSVEPEXO_PATH', 'MNE-ssvepexo-data'
    elif dataset == "p300" or dataset == "imagery":
        raise NotImplementedError("Not yet implemented")

    # Use MNE _fetch_file to download EEG file
    if get_config(key) is None:
        set_config(key, os.path.join(os.path.expanduser("~"), "mne_data"))
    # Adquire local de dados do MNE
    path = _get_path(None, key, sign)
    # Baixa o que está no URL para pasta local, que é criada no segundo parametro
    destination = _url_to_local_path(url, os.path.join(path, key_dest))
    # Cria pasta com todo caminho pro arquivo
    os.makedirs(os.path.dirname(destination), exist_ok=True)
    if not os.path.exists(destination):
        _fetch_file(url, destination, print_destination=False)
    return destination
Esempio n. 21
0
def run_freesurfer_command(command, subjects_dir):
    "Run a FreeSurfer command"
    env = os.environ.copy()
    env['SUBJECTS_DIR'] = subjects_dir

    # find FREESURFER_HOME
    fs_home = mne.get_config('FREESURFER_HOME')
    save_fs_home = False
    while True:
        problem = fs_home_problem(fs_home)
        if problem:
            save_fs_home = True
            print problem
            if fs_home == os.environ.get('FREESURFER_HOME', 0):
                print(
                    "WARNING: This directory is set as FREESURFER_HOME "
                    "environment variable. As long as you don't remove this "
                    "environment variable, you will be asked for the proper "
                    "FreeSurfer location every time a FreeSurfer command is "
                    "run.")
            message = "Please select the directory where FreeSurfer is installed"
            print message
            fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
            if fs_home is False:
                raise RuntimeError("Could not find FreeSurfer")
        else:
            break
    if save_fs_home:
        mne.set_config('FREESURFER_HOME', fs_home)

    # adjust environment
    env['FREESURFER_HOME'] = fs_home
    bin_path = os.path.join(fs_home, 'bin')
    if bin_path not in env['PATH']:
        env['PATH'] = ':'.join((bin_path, env['PATH']))

    # run command
    run_subprocess(command, env=env)
Esempio n. 22
0
def get_dataset_path(sign, path):
    """Returns the dataset path allowing for changes in MNE_DATA
     config

    Parameters
    ----------
    sign : str
        Signifier of dataset
    path : None | str
        Location of where to look for the data storing location.
        If None, the environment variable or config parameter
        ``MNE_DATASETS_(signifier)_PATH`` is used. If it doesn't exist, the
        "~/mne_data" directory is used. If the dataset
        is not found under the given path, the data
        will be automatically downloaded to the specified folder.

    Returns
    -------
        path : None | str
        Location of where to look for the data storing location
    """
    sign = sign.upper()
    key = "MNE_DATASETS_{:s}_PATH".format(sign)
    if get_config(key) is None:
        if get_config("MNE_DATA") is None:
            path_def = osp.join(osp.expanduser("~"), "mne_data")
            print(
                "MNE_DATA is not already configured. It will be set to "
                "default location in the home directory - "
                + path_def
                + "\nAll datasets will be downloaded to this location, if anything is "
                "already downloaded, please move manually to this location"
            )
            if not osp.isdir(path_def):
                os.makedirs(path_def)
            set_config("MNE_DATA", osp.join(osp.expanduser("~"), "mne_data"))
        set_config(key, get_config("MNE_DATA"))
    return _get_path(path, key, sign)
Esempio n. 23
0
def run_freesurfer_command(command, subjects_dir):
    "Run a FreeSurfer command"
    env = os.environ.copy()
    env['SUBJECTS_DIR'] = subjects_dir

    # find FREESURFER_HOME
    fs_home = mne.get_config('FREESURFER_HOME')
    save_fs_home = False
    while True:
        problem = fs_home_problem(fs_home)
        if problem:
            save_fs_home = True
            print problem
            if fs_home == os.environ.get('FREESURFER_HOME', 0):
                print("WARNING: This directory is set as FREESURFER_HOME "
                      "environment variable. As long as you don't remove this "
                      "environment variable, you will be asked for the proper "
                      "FreeSurfer location every time a FreeSurfer command is "
                      "run.")
            message = "Please select the directory where FreeSurfer is installed"
            print message
            fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
            if fs_home is False:
                raise RuntimeError("Could not find FreeSurfer")
        else:
            break
    if save_fs_home:
        mne.set_config('FREESURFER_HOME', fs_home)

    # adjust environment
    env['FREESURFER_HOME'] = fs_home
    bin_path = os.path.join(fs_home, 'bin')
    if bin_path not in env['PATH']:
        env['PATH'] = ':'.join((bin_path, env['PATH']))

    # run command
    run_subprocess(command, env=env)
Esempio n. 24
0
    def src_modelling(self, spacing=['oct5'], overwrite=False):
        from mne import (read_forward_solution, make_forward_solution,
                         write_forward_solution, setup_source_space)
        subject = self.subject
        task = self.experiment
        mne.set_config('SUBJECTS_DIR', self.pth_FS)
        FS_subj = op.join(self.pth_FS, subject)
        fname_trans = op.join(FS_subj, subject + '-trans.fif')
        fname_bem = op.join(FS_subj, '%s-bem_sol.fif' % subject)

        if not op.exists(fname_bem) or overwrite:
            # make_watershed_bem already run in the sh script
#            mne.bem.make_watershed_bem(subject, overwrite=True,
#                                       volume='T1', atlas=True, gcaatlas=False,
#                                       preflood=None)
            model = mne.make_bem_model(subject, ico=4, conductivity=[0.3])
            bem = mne.make_bem_solution(model)
            mne.write_bem_solution(fname_bem, bem)
        else:
            bem = mne.read_bem_solution(fname_bem)

        for space in spacing:
            fname_src = op.join(FS_subj, 'bem', '%s-src.fif' % space)
            bname_fwd = '%s_%s_%s-fwd.fif' % (subject, task, space)
            fname_fwd = op.join(self.out_srcData, bname_fwd)
            if not op.exists(fname_src) or overwrite:
                src = setup_source_space(subject, space,
                                         subjects_dir=self.pth_FS)
                src.save(fname_src, overwrite=overwrite)

            if op.exists(fname_fwd) and not overwrite:
                self.fwd = read_forward_solution(fname_fwd)
            else:
                self.fwd = make_forward_solution(self.raw.info, fname_trans,
                                                 fname_src, fname_bem)
                write_forward_solution(fname_fwd, self.fwd, overwrite)
Esempio n. 25
0
def get_fs_home():
    fs_home = mne.get_config('FREESURFER_HOME')
    save_fs_home = False
    while True:
        problem = fs_home_problem(fs_home)
        if problem:
            save_fs_home = True
            print(problem)
            if fs_home == os.environ.get('FREESURFER_HOME', 0):
                print("WARNING: This directory is set as FREESURFER_HOME "
                      "environment variable. As long as you don't remove this "
                      "environment variable, you will be asked for the proper "
                      "FreeSurfer location every time a FreeSurfer command is "
                      "run.")
            message = "Please select the directory where FreeSurfer is installed"
            print(message)
            fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
            if fs_home is False:
                raise RuntimeError("Could not find FreeSurfer")
        else:
            break
    if save_fs_home:
        mne.set_config('FREESURFER_HOME', fs_home)
    return fs_home
Esempio n. 26
0
def get_fs_home():
    fs_home = mne.get_config('FREESURFER_HOME')
    save_fs_home = False
    while True:
        problem = fs_home_problem(fs_home)
        if problem:
            save_fs_home = True
            print(problem)
            if fs_home == os.environ.get('FREESURFER_HOME', 0):
                print("WARNING: This directory is set as FREESURFER_HOME "
                      "environment variable. As long as you don't remove this "
                      "environment variable, you will be asked for the proper "
                      "FreeSurfer location every time a FreeSurfer command is "
                      "run.")
            message = "Please select the directory where FreeSurfer is installed"
            print(message)
            fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
            if fs_home is False:
                raise RuntimeError("Could not find FreeSurfer")
        else:
            break
    if save_fs_home:
        mne.set_config('FREESURFER_HOME', fs_home)
    return fs_home
Esempio n. 27
0
# License: BSD (3-clause)

          
import numpy as np
import mnefun
import os
#import glob
os.chdir('/home/sjjoo/git/BrainTools/projects/NLR_MEG')
from score import score
from nlr_organizeMEG_mnefun import nlr_organizeMEG_mnefun
import mne
import time

t0 = time.time()

mne.set_config('MNE_USE_CUDA', 'true')

# At Possum projects folder mounted in the local disk
raw_dir = '/mnt/diskArray/projects/MEG/nlr/raw'
#out_dir = '/mnt/diskArray/scratch/NLR_MEG'
#out_dir = '/mnt/scratch/NLR_MEG'

# At local hard drive
out_dir = '/mnt/scratch/adult'
#out_dir = '/mnt/scratch/adult'

if not os.path.isdir(out_dir):
    os.mkdir(out_dir)
    
os.chdir(out_dir)
Esempio n. 28
0
##############################################################################
# Define and read epochs
# ^^^^^^^^^^^^^^^^^^^^^^
#
# First extract events:

events = mne.find_events(raw, stim_channel='STI 014')
print(events[:5])

##############################################################################
# Note that, by default, we use stim_channel='STI 014'. If you have a different
# system (e.g., a newer system that uses channel 'STI101' by default), you can
# use the following to set the default stim channel to use for finding events:

mne.set_config('MNE_STIM_CHANNEL', 'STI101', set_env=True)

##############################################################################
# Events are stored as a 2D numpy array where the first column is the time
# instant and the last one is the event number. It is therefore easy to
# manipulate.
#
# Define epochs parameters:

event_id = dict(aud_l=1, aud_r=2)  # event trigger and conditions
tmin = -0.2  # start of each epoch (200ms before the trigger)
tmax = 0.5  # end of each epoch (500ms after the trigger)

##############################################################################
# Exclude some channels (original bads + 2 more):
Esempio n. 29
0
import sys
import mne
import matplotlib.pyplot as plt
from mne.utils import run_subprocess, logger
import os
from os import path as op
import copy
import shutil
import numpy as np
from numpy.random import randn
from scipy import stats as stats
import time
from functools import partial

from mne import set_config
set_config('MNE_MEMMAP_MIN_SIZE', '1M')
set_config('MNE_CACHE_DIR', '.tmp')

mne.set_config('MNE_USE_CUDA', 'true')

this_env = copy.copy(os.environ)
#fs_dir = '/mnt/diskArray/projects/freesurfer'
fs_dir = '/mnt/diskArray/projects/avg_fsurfer'

this_env['SUBJECTS_DIR'] = fs_dir
#this_env['FREESURFER_HOME'] = '/usr/local/freesurfer'

raw_dir = '/mnt/scratch/NLR_MEG4'

os.chdir(raw_dir)
Esempio n. 30
0
##############################################################################
# Define and read epochs
# ^^^^^^^^^^^^^^^^^^^^^^
#
# First extract events:

events = mne.find_events(raw, stim_channel='STI 014')
print(events[:5])

##############################################################################
# Note that, by default, we use stim_channel='STI 014'. If you have a different
# system (e.g., a newer system that uses channel 'STI101' by default), you can
# use the following to set the default stim channel to use for finding events:

mne.set_config('MNE_STIM_CHANNEL', 'STI101', set_env=True)

##############################################################################
# Events are stored as a 2D numpy array where the first column is the time
# instant and the last one is the event number. It is therefore easy to
# manipulate.
#
# Define epochs parameters:

event_id = dict(aud_l=1, aud_r=2)  # event trigger and conditions
tmin = -0.2  # start of each epoch (200ms before the trigger)
tmax = 0.5  # end of each epoch (500ms after the trigger)

##############################################################################
# Exclude some channels (original bads + 2 more):
Esempio n. 31
0
# configuration variable, pass its name to :func:`~mne.get_config` as the
# ``key`` parameter (``key`` is the first parameter so you can pass it unnamed
# if you want):

print(mne.get_config('MNE_USE_CUDA'))
print(type(mne.get_config('MNE_USE_CUDA')))

###############################################################################
# Note that the string values read from the JSON file are not parsed in any
# way, so :func:`~mne.get_config` returns a string even for true/false config
# values, rather than a Python :ref:`boolean <bltin-boolean-values>`.
# Similarly, :func:`~mne.set_config` will only set string values (or ``None``
# values, to unset a variable):

try:
    mne.set_config('MNE_USE_CUDA', True)
except TypeError as err:
    print(err)

###############################################################################
# If you're unsure whether a config variable has been set, there is a
# convenient way to check it and provide a fallback in case it doesn't exist:
# :func:`~mne.get_config` has a ``default`` parameter.

print(mne.get_config('missing_config_key', default='fallback value'))

###############################################################################
# There are also two convenience modes of :func:`~mne.get_config`. The first
# will return a :class:`dict` containing all config variables (and their
# values) that have been set on your system; this is done by passing
# ``key=None`` (which is the default, so it can be omitted):
Esempio n. 32
0
import sys
import mne
import matplotlib.pyplot as plt
from mne.utils import run_subprocess, logger
import os
from os import path as op
import copy
import shutil
import numpy as np
from numpy.random import randn
from scipy import stats as stats
import time
from functools import partial

from mne import set_config
set_config('MNE_MEMMAP_MIN_SIZE', '1M')
set_config('MNE_CACHE_DIR', '.tmp')

mne.set_config('MNE_USE_CUDA', 'true')

this_env = copy.copy(os.environ)
#fs_dir = '/mnt/diskArray/projects/freesurfer'
fs_dir = '/mnt/diskArray/projects/avg_fsurfer'

this_env['SUBJECTS_DIR'] = fs_dir
#this_env['FREESURFER_HOME'] = '/usr/local/freesurfer'

raw_dir = '/mnt/scratch/NLR_MEG3'

os.chdir(raw_dir)
Esempio n. 33
0
from braindecode.datautil import create_from_mne_epochs
from scipy import signal
from skorch.callbacks import LRScheduler
from skorch.helper import predefined_split
from sklearn.preprocessing import StandardScaler
from common_dl import set_random_seeds
from common_dsp import *
from gesture.models.d2l_resnet import d2lresnet
from myskorch import on_epoch_begin_callback, on_batch_end_callback
from ecog_finger.config import *
from ecog_finger.preprocess.chn_settings import get_channel_setting

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

try:
    mne.set_config('MNE_LOGGING_LEVEL', 'ERROR')
except TypeError as err:
    print(err)

seed = 20200220  # random seed to make results reproducible
set_random_seeds(seed=seed)

import inspect as i
import sys
#sys.stdout.write(i.getsource(deepnet))

sid = 2
fs = 1000
use_active_only = False
if use_active_only:
    active_chn = get_channel_setting(sid)
Esempio n. 34
0
"""
@author Farzaneh.Tlb
8/11/19 2:10 PM
Implementation of .... (Fill this line)
"""

import statistics

import matplotlib.pyplot as plt
import mne

import Analysis.Utils as utils
import Analysis.Utils as ut
import numpy as np

original_level = mne.set_config('MNE_LOGGING_LEVEL', 'ERROR')
mne.set_log_level('ERROR')

ctrl_total = [
    156, 129, 169, 211, 209, 188, 218, 180, 234, 215, 145, 206, 146, 221, 198,
    230, 233, 222, 163, 135
]

pfc_total = [
    224, 108, 109, 207, 235, 239, 237, 219, 148, 220, 212, 114, 111, 178
]
ctrl_encmain_le, ctrl_encmain_intact, \
pfc_encmain_le, pfc_encmain_intact, \
ctrl_pretrial_le, ctrl_pretrial_intact, \
pfc_pretrial_le, pfc_pretrial_intact, \
ctrl_proc_le, ctrl_proc_intact, \
#
# License: BSD (3-clause)

import numpy as np
import mnefun
import os
#import glob
os.chdir('/home/sjjoo/git/BrainTools/projects/NLR_MEG')
from score import score
from nlr_organizeMEG_mnefun import nlr_organizeMEG_mnefun
import mne
import time

t0 = time.time()

mne.set_config('MNE_USE_CUDA', 'true')

# At Possum projects folder mounted in the local disk
raw_dir = '/mnt/diskArray/projects/MEG/nlr/raw'
#out_dir = '/mnt/diskArray/scratch/NLR_MEG'
#out_dir = '/mnt/scratch/NLR_MEG'

# At local hard drive
out_dir = '/mnt/scratch/NLR_MEG4'
#out_dir = '/mnt/scratch/adult'

if not os.path.isdir(out_dir):
    os.mkdir(out_dir)

os.chdir(out_dir)
##############################################################################
# If you'd like to turn information status messages off:

mne.set_log_level('WARNING')

##############################################################################
# But it's generally a good idea to leave them on:

mne.set_log_level('INFO')

##############################################################################
# You can set the default level by setting the environment variable
# "MNE_LOGGING_LEVEL", or by having mne-python write preferences to a file:

mne.set_config('MNE_LOGGING_LEVEL', 'WARNING', set_env=True)

##############################################################################
# Note that the location of the mne-python preferences file (for easier manual
# editing) can be found using:

mne.get_config_path()

##############################################################################
# By default logging messages print to the console, but look at
# mne.set_log_file() to save output to a file.
#
# Access raw data
# ^^^^^^^^^^^^^^^

from mne.datasets import sample  # noqa
Esempio n. 37
0
                giden=36,
                gidt=37,
                gondt=38,
                MRgide=39),

                dict(nide=40,
                nidet=45,
                niden=46,
                nidt=47,
                nondt=48,
                MRnide=49)]
event_id = event_ids[block]

# raw.info['bads'] = ['EEG0', 'EEG0', 'EEG0', 'EEG0', 'EEG0', 'EEG0', 'EEG0', 'MEG', 'MEG0']
#raw.info['bads'] = ['EEG074', 'EEG019', 'EEG027', 'EEG051', 'EEG037', 'EEG029', 'EEG004', 'MEG1541', 'MEG0141']
mne.set_config('MNE_STIM_CHANNEL', 'STI101')

reject = dict(grad=2500e-13, # T / m(gradiometers)
              mag=4e-12, # T (magnetometers)
              eeg=150e-6, # uV (EEG channels)
              eog=150e-6) # uV (EOG channels)

events = mne.find_events(raw, stim_channel='STI101', min_duration=0.002)
picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True, stim=False, exclude=[])
epochs = mne.Epochs(raw, events, event_id=event_id, tmin=-0.1, tmax=0.9,
                    picks=picks, baseline=(-0.086,0.014), preload=True,
                    reject=reject, proj=False)
                    # reject=reject, add_eeg_ref=False, proj=False)# add average reference when i plot evoked [DEPRECATED?]
epochs.times -= 0.014 # it's to correct for the 14ms delay

# epochs_data = epochs.get_data()
Esempio n. 38
0
# configuration variable, pass its name to :func:`~mne.get_config` as the
# ``key`` parameter (``key`` is the first parameter so you can pass it unnamed
# if you want):

print(mne.get_config('MNE_USE_CUDA'))
print(type(mne.get_config('MNE_USE_CUDA')))

# %%
# Note that the string values read from the JSON file are not parsed in any
# way, so :func:`~mne.get_config` returns a string even for true/false config
# values, rather than a Python :ref:`boolean <bltin-boolean-values>`.
# Similarly, :func:`~mne.set_config` will only set string values (or ``None``
# values, to unset a variable):

try:
    mne.set_config('MNE_USE_CUDA', True)
except TypeError as err:
    print(err)

# %%
# If you're unsure whether a config variable has been set, there is a
# convenient way to check it and provide a fallback in case it doesn't exist:
# :func:`~mne.get_config` has a ``default`` parameter.

print(mne.get_config('missing_config_key', default='fallback value'))

# %%
# There are also two convenience modes of :func:`~mne.get_config`. The first
# will return a :class:`dict` containing all config variables (and their
# values) that have been set on your system; this is done by passing
# ``key=None`` (which is the default, so it can be omitted):
Esempio n. 39
0
print(mne.get_config())

###############################################################################
# We see fields like "MNE_DATASETS_SAMPLE_PATH". As the name suggests, this is
# the path the sample data is downloaded to. All the fields in the
# configuration file can be modified by calling :func:`mne.set_config`.

###############################################################################
#
# .. _tut_logging:
#
# Logging
# =======
# Configurations also include the default logging level for the functions. This
# field is called "MNE_LOGGING_LEVEL".
mne.set_config('MNE_LOGGING_LEVEL', 'INFO')
print(mne.get_config(key='MNE_LOGGING_LEVEL'))

###############################################################################
# The default value is now set to INFO. This level will now be used by default
# every time we call a function in MNE. We can set the global logging level for
# only this session by calling :func:`mne.set_log_level` function.
mne.set_log_level('WARNING')
print(mne.get_config(key='MNE_LOGGING_LEVEL'))

###############################################################################
# Notice how the value in the config file was not changed. Logging level of
# WARNING only applies for this session. Let's see what logging level of
# WARNING prints for :func:`mne.compute_raw_covariance`.
cov_raw = mne.compute_raw_covariance(raw)
Esempio n. 40
0
##############################################################################
# If you'd like to turn information status messages off:

mne.set_log_level('WARNING')

##############################################################################
# But it's generally a good idea to leave them on:

mne.set_log_level('INFO')

##############################################################################
# You can set the default level by setting the environment variable
# "MNE_LOGGING_LEVEL", or by having mne-python write preferences to a file:

mne.set_config('MNE_LOGGING_LEVEL','WARNING')

##############################################################################
# Note that the location of the mne-python preferences file (for easier manual
# editing) can be found using:

mne.get_config_path()

##############################################################################
# By default logging messages print to the console, but look at
# mne.set_log_file() to save output to a file.
#
# Access raw data
# ^^^^^^^^^^^^^^^

from mne.datasets import sample