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
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}
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}
def run(): """Run command.""" from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', help='Overwrite previously computed surface') parser.add_option('-s', '--subject', dest='subject', help='The name of the subject', type='str') parser.add_option('-f', '--force', dest='force', action='store_true', help='Force transformation of surface into bem.') parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Print the debug messages.') parser.add_option("-d", "--subjects-dir", dest="subjects_dir", help="Subjects directory", default=subjects_dir) parser.add_option("-n", "--no-decimate", dest="no_decimate", help="Disable medium and sparse decimations " "(dense only)", action='store_true') options, args = parser.parse_args() subject = vars(options).get('subject', os.getenv('SUBJECT')) subjects_dir = options.subjects_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, options.force, options.overwrite, options.no_decimate, options.verbose)
def test_anatomy(): """Test anatomy functions (slow!).""" import matplotlib.pyplot as plt # This is where the data are after downloading from HCP temp_dir = mne.utils._TempDir() subjects_dir = op.join(temp_dir, 'hcp-subjects') recordings_path = op.join(temp_dir, 'hcp-meg') make_mne_anatomy(recordings_path=recordings_path, subjects_dir=subjects_dir, **hcp_params) assert_true( op.isfile( op.join(subjects_dir, hcp_params['subject'], 'bem', 'inner_skull.surf'))) # Now we need fsaverage... mne_subjects_dir = mne.get_config('SUBJECTS_DIR') assert_true(mne_subjects_dir is not None) shutil.copytree(op.join(mne_subjects_dir, 'fsaverage'), op.join(subjects_dir, 'fsaverage')) compute_forward_stack(subjects_dir=subjects_dir, recordings_path=recordings_path, src_params=dict(add_dist=False, spacing='oct1'), verbose=True, **hcp_params) # let's do our viz tests, too plot_coregistration(subjects_dir=subjects_dir, recordings_path=recordings_path, **hcp_params) plt.close('all') mne.viz.plot_bem(subject=tconf.test_subject, subjects_dir=subjects_dir) plt.close('all')
def combine_medial_labels(labels, subject='fsaverage', surf='white', dist_limit=0.02): from scipy.spatial.distance import cdist subjects_dir = mne.get_config('SUBJECTS_DIR') rrs = dict((hemi, mne.read_surface(os.path.join(subjects_dir, subject, 'surf', '%s.%s' % (hemi, surf)))[0] / 1000.) for hemi in ('lh', 'rh')) use_labels = list() used = np.zeros(len(labels), bool) logger.info('Matching medial regions for %s labels on %s %s, d=%0.1f mm' % (len(labels), subject, surf, 1000 * dist_limit)) for li1, l1 in enumerate(labels): if used[li1]: continue used[li1] = True use_label = l1.copy() rr1 = rrs[l1.hemi][l1.vertices] for li2 in np.where(~used)[0]: l2 = labels[li2] same_name = (l2.name.replace(l2.hemi, '') == l1.name.replace(l1.hemi, '')) if l2.hemi != l1.hemi and same_name: rr2 = rrs[l2.hemi][l2.vertices] mean_min = np.mean(mne.surface._compute_nearest( rr1, rr2, return_dists=True)[1]) if mean_min <= dist_limit: use_label += l2 used[li2] = True logger.info(' Matched: ' + l1.name) use_labels.append(use_label) logger.info('Total %d labels' % (len(use_labels),)) return use_labels
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 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)
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)
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
def run(): from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-s', '--subject', dest='subject', help='Freesurfer subject id', type='str') parser.add_option('-l', '--layers', dest='layers', default=1, type=int, help='Number BEM layers.') parser.add_option('-i', '--ico', dest='ico', default=4, type=int, help='Triangle decimation number for single layer bem') parser.add_option('-d', '--subjects-dir', dest='subjects_dir', help='FS Subjects directory', default=subjects_dir) parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', help='Overwrite existing neuromag MRI and MNE BEM files.') options, args = parser.parse_args() subject = options.subject subjects_dir = options.subjects_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, options.layers, options.ico, options.overwrite)
def test_dataset_accept(self): """verify that accept licence is working""" # Only Shin2017 (bbci_eeg_fnirs) for now for ds in [Shin2017A(), Shin2017B()]: # if the data is already downloaded: if mne.get_config("MNE_DATASETS_BBCIFNIRS_PATH") is None: self.assertRaises(AttributeError, ds.get_data, [1])
def run(): from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-s', '--subject', dest='subject', help='Freesurfer subject id', type='str') parser.add_option('-l', '--layers', dest='layers', default=1, type=int, help='Number BEM layers.') parser.add_option('--spacing', dest='spacing', default=5, type=int, help='Triangle decimation number for single layer bem') parser.add_option('-d', '--subjects-dir', dest='subjects_dir', help='FS Subjects directory', default=subjects_dir) parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', help='Overwrite existing neuromag MRI and MNE BEM files.') options, args = parser.parse_args() subject = options.subject subjects_dir = options.subjects_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, options.layers, options.spacing, options.overwrite)
def run(): from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-s', '--subject', dest='subject', help='Freesurfer subject id', type='str') parser.add_option('-r', '--raw-dir', dest='raw_dir', help='Path to parent directory containing raw mri data', default='PARREC', metavar='FILE') parser.add_option('-d', '--subjects-dir', dest='subjects_dir', help='FS Subjects directory', default=subjects_dir) parser.add_option('-f', '--force', dest='force', action='store_true', help='Force FreeSurfer reconstruction.') parser.add_option('-o', '--openmp', dest='openmp', default=2, type=int, help='Number of CPUs to use for reconstruction routines.') parser.add_option('-v', '--volume', dest='volume', default='MPRAGE', type=str, help='Input raw volume file for nii conversion. Default is MPRAGE ' 'it can also be MEMP.') options, args = parser.parse_args() subject = vars(options).get('subject', os.getenv('SUBJECT')) subjects_dir = options.subjects_dir raw_dir = options.raw_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, raw_dir, options.force, options.openmp, options.volume)
def _check_load_fold(fold_files, atlas): _validate_type(fold_files, (list, 'path-like', None), 'fold_files') if fold_files is None: fold_files = mne.get_config('MNE_NIRS_FOLD_PATH') if fold_files is None: raise ValueError( 'MNE_NIRS_FOLD_PATH not set, either set it using ' 'mne.set_config or pass fold_files as str or list') if not isinstance(fold_files, list): # path-like fold_files = _check_fname(fold_files, overwrite='read', must_exist=True, name='fold_files', need_dir=True) fold_files = [op.join(fold_files, f'10-{x}.xls') for x in (5, 10)] fold_tbl = pd.DataFrame() for fi, fname in enumerate(fold_files): fname = _check_fname(fname, overwrite='read', must_exist=True, name=f'fold_files[{fi}]') fold_tbl = pd.concat( [fold_tbl, _read_fold_xls(fname, atlas=atlas)], ignore_index=True) return fold_tbl
def run(): from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-s', '--subject', dest='subject', help='Freesurfer subject id', type='str') parser.add_option('-r', '--raw-dir', dest='raw_dir', help='Path to parent directory containing raw mri data', default='PARREC', metavar='FILE') parser.add_option('-d', '--subjects-dir', dest='subjects_dir', help='FS Subjects directory', default=subjects_dir) parser.add_option('-f', '--force', dest='force', action='store_true', help='Force FreeSurfer reconstruction.') parser.add_option('-o', '--openmp', dest='openmp', default=2, type=str, help='Number of CPUs to use for reconstruction routines.') parser.add_option('-v', '--volume', dest='volume', default='MPRAGE', type=str, help='Input raw volume file for nii conversion. Default is MPRAGE ' 'it can also be MEMP_VBM.') options, args = parser.parse_args() subject = vars(options).get('subject', os.getenv('SUBJECT')) subjects_dir = options.subjects_dir raw_dir = options.raw_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, raw_dir, options.force, options.openmp, options.volume)
def run(): from mne.commands.utils import get_optparser parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', help='Overwrite previously computed surface') parser.add_option('-s', '--subject', dest='subject', help='The name of the subject', type='str') parser.add_option('-f', '--force', dest='force', action='store_true', help='Force transformation of surface into bem.') parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Print the debug messages.') parser.add_option("-d", "--subjects-dir", dest="subjects_dir", help="Subjects directory", default=subjects_dir) options, args = parser.parse_args() subject = vars(options).get('subject', os.getenv('SUBJECT')) subjects_dir = options.subjects_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) _run(subjects_dir, subject, options.force, options.overwrite, options.verbose)
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)
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
def reset_logger(logger): # lower mne's screen logging level, but defer to user-defined setting level = mne.get_config('MNE_LOGGING_LEVEL', WARNING) formatter = Formatter("%(levelname)-8s %(name)s:%(message)s") for h in logger.handlers: if isinstance(h, StreamHandler): h.setFormatter(formatter) if h.level < level: h.setLevel(level)
def reset_logger(logger): # lower mne's screen logging level, but defer to user-defined setting level = log_level(mne.get_config('MNE_LOGGING_LEVEL', WARNING)) formatter = Formatter("%(levelname)-8s %(name)s:%(message)s") for h in logger.handlers: if isinstance(h, StreamHandler): h.setFormatter(formatter) if h.level < level: h.setLevel(level)
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
def get_root(package): "Get the root path for a package" if package not in _env_vars: raise KeyError("Unknown binary package: %r" % package) root = mne.get_config(_env_vars[package]) if root: test_root(package, root) else: root = _ask_user_for_bin_dir(package) return root
def _fast_map_meg_channels(info, pick_from, pick_to, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.forward._lead_dots import _do_self_dots, _do_cross_dots from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') def _compute_dots(info, mode='fast'): """Compute all-to-all dots.""" templates = _read_coil_defs() coils = _create_meg_coils(info['chs'], 'normal', info['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils, 'meg') self_dots = _do_self_dots(int_rad, False, coils, my_origin, 'meg', lut_fun, n_fact, n_jobs=1) cross_dots = _do_cross_dots(int_rad, False, coils, coils, my_origin, 'meg', lut_fun, n_fact).T return self_dots, cross_dots _compute_fast_dots = mem.cache(_compute_dots, verbose=0) info['bads'] = [] # if bads is different, hash will be different info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. self_dots, cross_dots = _compute_fast_dots( info, mode=mode) cross_dots = cross_dots[pick_to, :][:, pick_from] self_dots = self_dots[pick_from, :][:, pick_from] ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']
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()))
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)
def run(): """Run command.""" from mne.commands.utils import get_optparser, _add_verbose_flag parser = get_optparser(__file__) subjects_dir = mne.get_config('SUBJECTS_DIR') parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', help='Overwrite previously computed surface') parser.add_option('-s', '--subject', dest='subject', help='The name of the subject', type='str') parser.add_option('-f', '--force', dest='force', action='store_true', help='Force creation of the surface even if it has ' 'some topological defects.') parser.add_option("-d", "--subjects-dir", dest="subjects_dir", help="Subjects directory", default=subjects_dir) parser.add_option("-n", "--no-decimate", dest="no_decimate", help="Disable medium and sparse decimations " "(dense only)", action='store_true') _add_verbose_flag(parser) options, args = parser.parse_args() subject = vars(options).get('subject', os.getenv('SUBJECT')) subjects_dir = options.subjects_dir if subject is None or subjects_dir is None: parser.print_help() sys.exit(1) make_scalp_surfaces(subject=subject, subjects_dir=subjects_dir, force=options.force, overwrite=options.overwrite, no_decimate=options.no_decimate, verbose=options.verbose)
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
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)
def get_bin(package, name): "Get path for a binary" if package not in _env_vars: raise KeyError("Unknown binary package: %r" % package) root = mne.get_config(_env_vars[package]) if not root: root = _ask_user_for_bin_dir(package) relpath = _bin_dirs[package] while True: if relpath: bin_dir = os.path.join(root, relpath) else: bin_dir = root bin_path = os.path.join(bin_dir, name) if os.path.exists(bin_path): return bin_path else: root = _ask_user_for_bin_dir(package)
def _fast_map_meg_channels(info, pick_from, pick_to, dots=None, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _patch_setup_dots( mode, info_from, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. if dots is None: dots = self_dots, cross_dots = _compute_dots(info, mode=mode) else: self_dots, cross_dots = dots self_dots, cross_dots = _pick_dots(dots, pick_from, pick_to) ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']
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
def _fast_map_meg_channels(info, pick_from, pick_to, dots=None, mode='fast'): from mne.io.pick import pick_info from mne.forward._field_interpolation import _setup_dots from mne.forward._field_interpolation import _compute_mapping_matrix from mne.forward._make_forward import _create_meg_coils, _read_coil_defs from mne.bem import _check_origin miss = 1e-4 # Smoothing criterion for MEG # XXX: hack to silence _compute_mapping_matrix verbose = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') mne.set_log_level('WARNING') info_from = pick_info(info, pick_from, copy=True) templates = _read_coil_defs() coils_from = _create_meg_coils(info_from['chs'], 'normal', info_from['dev_head_t'], templates) my_origin = _check_origin((0., 0., 0.04), info_from) int_rad, noise, lut_fun, n_fact = _setup_dots(mode, coils_from, 'meg') # This function needs a clean input. It hates the presence of other # channels than MEG channels. Make sure all is picked. if dots is None: dots = self_dots, cross_dots = _compute_dots(info, mode=mode) else: self_dots, cross_dots = dots self_dots, cross_dots = _pick_dots(dots, pick_from, pick_to) ch_names = [c['ch_name'] for c in info_from['chs']] fmd = dict(kind='meg', ch_names=ch_names, origin=my_origin, noise=noise, self_dots=self_dots, surface_dots=cross_dots, int_rad=int_rad, miss=miss) fmd['data'] = _compute_mapping_matrix(fmd, info_from) mne.set_log_level(verbose) return fmd['data']
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
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)
def mne_lcmv_template(raw, apply_to=None, fmin=3, fmax=45): # set paths and filenames subject = 'fsaverage' trans = 'fsaverage' # MNE has a built-in fsaverage transformation subjects_dir = mne.get_config('SUBJECTS_DIR') fs_dir = str(pathlib.Path(subjects_dir, 'fsaverage')) src = str(pathlib.Path(fs_dir, 'bem', 'fsaverage-ico-5-src.fif')) bem = str( pathlib.Path(fs_dir, 'bem', 'fsaverage-5120-5120-5120-bem-sol.fif')) # prepare raw data raw.load_data() raw = mne_filter(raw, [fmin, fmax]) raw = mne_set_1020_montage(raw) raw.interpolate_bads() raw.set_eeg_reference('average', projection=True) # prepare mne beamforming solutions fwd = mne.make_forward_solution(raw.info, trans=trans, src=src, bem=bem, eeg=True, mindist=5.0, n_jobs=tb.n_jobs()) # cov = mne.compute_raw_covariance(raw) # compute before band-pass of interest epochs = mne_epoch(raw, 3.) epochs.load_data() cov = mne.compute_covariance(epochs) filters = mne.beamformer.make_lcmv(raw.info, fwd, cov) # , 0.05, noise_cov=None, pick_ori='max-power', weight_norm='nai') if apply_to is None: raw_lcmv = mne.beamformer.apply_lcmv_raw(raw, filters) else: apply_to = mne_set_1020_montage(apply_to) apply_to.interpolate_bads() apply_to.set_eeg_reference('average', projection=True) raw_lcmv = mne.beamformer.apply_lcmv_raw(apply_to, filters) return raw_lcmv
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)
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
# -*- coding: utf-8 -*- """ ====================== Configuring MNE python ====================== This tutorial gives a short introduction to MNE configurations. """ import os.path as op import mne from mne.datasets.sample import data_path fname = op.join(data_path(), 'MEG', 'sample', 'sample_audvis_raw.fif') raw = mne.io.read_raw_fif(fname).crop(0, 10) original_level = mne.get_config('MNE_LOGGING_LEVEL', 'INFO') ############################################################################### # MNE-python stores configurations to a folder called `.mne` in the user's # home directory, or to AppData directory on Windows. The path to the config # file can be found out by calling :func:`mne.get_config_path`. print(mne.get_config_path()) ############################################################################### # These configurations include information like sample data paths and plotter # window sizes. Files inside this folder should never be modified manually. # Let's see what the configurations contain. print(mne.get_config()) ############################################################################### # We see fields like "MNE_DATASETS_SAMPLE_PATH". As the name suggests, this is
""" import os.path as op import warnings import numpy as np import matplotlib.pyplot as plt from mne import get_config, read_source_spaces, SourceEstimate, read_labels_from_annot, get_chpi_positions from mne.io import read_info, Raw from mne.simulation import simulate_raw from mne.chpi import _calculate_chpi_positions pulse_tmin, pulse_tmax = 0.0, 0.1 this_dir = op.dirname(__file__) subjects_dir = get_config("SUBJECTS_DIR") subj, subject = "subj_01", "AKCLEE_107_slim" # This is a position file that has been modified/truncated for speed fname_pos_orig = op.join(this_dir, "%s_funloc_hp_trunc.txt" % subj) # Set up paths data_dir = op.join(this_dir, "funloc", subj) bem_dir = op.join(subjects_dir, subject, "bem") fname_raw = op.join(data_dir, "raw_fif", "%s_funloc_raw.fif" % subj) trans = op.join(data_dir, "trans", "%s-trans.fif" % subj) bem = op.join(bem_dir, "%s-5120-5120-5120-bem-sol.fif" % subject) src = read_source_spaces(op.join(bem_dir, "%s-oct-6-src.fif" % subject)) sfreq = read_info(fname_raw, verbose=False)["sfreq"] # ############################################################################
""" import os.path as op import warnings import numpy as np import matplotlib.pyplot as plt from mne import (get_config, read_source_spaces, SourceEstimate, read_labels_from_annot, get_chpi_positions) from mne.io import read_info, Raw from mnefun import simulate_movement pulse_tmin, pulse_tmax = 0., 0.1 this_dir = op.dirname(__file__) subjects_dir = get_config('SUBJECTS_DIR') subj, subject = 'subj_01', 'AKCLEE_107_slim' # This is a position file that has been modified/truncated for speed fname_pos_orig = op.join(this_dir, '%s_funloc_hp_trunc.txt' % subj) # These were generated using Maxfilter after simulation fname_pos_move = op.join(this_dir, 'hp_move.txt') fname_pos_stat = op.join(this_dir, 'hp_stat.txt') # Set up paths data_dir = op.join(this_dir, 'funloc', subj) bem_dir = op.join(subjects_dir, subject, 'bem') fname_raw = op.join(data_dir, 'raw_fif', '%s_funloc_raw.fif' % subj) fname_erm = op.join(data_dir, 'raw_fif', '%s_erm_raw.fif' % subj) trans = op.join(data_dir, 'trans', '%s-trans.fif' % subj) bem = op.join(bem_dir, '%s-5120-5120-5120-bem-sol.fif' % subject)
import os import mne ############################################################################### # .. _config-get-set: # # Getting and setting configuration variables # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Configuration variables are read and written using the functions # :func:`mne.get_config` and :func:`mne.set_config`. To read a specific # 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) ###############################################################################
from mne.io import Raw from mne import set_cache_dir, set_memmap_min_size, get_config #fname = 'ec_rest_before_tsss_mc_rsl.fif' # set_cache_dir('/Users/cjb/tmp/shm') # set_memmap_min_size('100M') print('MNE_CACHE_DIR set to', get_config('MNE_CACHE_DIR', None)) print('Arrays larger than', get_config('MNE_MEMMAP_MIN_SIZE', None), 'are memmapped!') def filter_raw(): fname='ec_rest_before_tsss_mc_rsl.fif' raw = Raw(fname, preload=False) raw.preload_data() # data becomes numpy.float64 raw.filter(None, 40, n_jobs=4) del raw fname='ec_rest_after_tsss_mc_rsl.fif' raw2 = Raw(fname, preload=False) raw2.preload_data() # data becomes numpy.float64 raw2.filter(None, 40, n_jobs=4) del raw2 #filtered_raw = filter_raw(fname)