1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
# Author: Eric Larson <larson.eric.d@gmail.com>
# License: BSD Style.
from ...externals.six import string_types
import os
from os import path as op
import zipfile
from sys import stdout
from ...utils import _fetch_file, get_config, set_config, _url_to_local_path
from .urls import (url_match, valid_data_types, valid_data_formats,
valid_conditions)
def data_path(url, path=None, force_update=False, update_path=None):
"""Get path to local copy of MEGSIM dataset URL
This is a low-level function useful for getting a local copy of a
remote MEGSIM dataet.
Parameters
----------
url : str
The dataset to use.
path : None | str
Location of where to look for the MEGSIM data storing location.
If None, the environment variable or config parameter
MNE_DATASETS_MEGSIM_PATH is used. If it doesn't exist, the
"mne-python/examples" directory is used. If the MEGSIM dataset
is not found under the given path (e.g., as
"mne-python/examples/MEGSIM"), 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_MEGSIM_PATH in mne-python
config to the given path. If None, the user is prompted.
Returns
-------
path : list of str
Local paths to the given data files. If URL was a .fif file, this
will be a list of length 1. If it was a .zip file, it may potentially
be many files.
Notes
-----
For example, one could do:
>>> from mne.datasets import megsim
>>> url = 'http://cobre.mrn.org/megsim/simdata/neuromag/visual/M87174545_vis_sim1A_4mm_30na_neuro_rn.fif'
>>> megsim.data_path(url, os.getenv('HOME') + '/datasets') # doctest:+SKIP
And this would download the given MEGSIM data file to the 'datasets'
folder, and prompt the user to save the 'datasets' path to the mne-python
config, if it isn't there already.
The MEGSIM dataset is documented in the following publication:
Aine CJ, Sanfratello L, Ranken D, Best E, MacArthur JA, Wallace T,
Gilliam K, Donahue CH, Montano R, Bryant JE, Scott A, Stephen JM
(2012) MEG-SIM: A Web Portal for Testing MEG Analysis Methods using
Realistic Simulated and Empirical Data. Neuroinform 10:141-158
"""
if path is None:
# use an intelligent guess if it's not defined
def_path = op.realpath(op.join(op.dirname(__file__), '..', '..',
'..', 'examples'))
key = 'MNE_DATASETS_MEGSIM_PATH'
if get_config(key) is None:
key = 'MNE_DATA'
path = get_config(key, def_path)
# use the same for all datasets
if not op.exists(path) or not os.access(path, os.W_OK):
try:
os.mkdir(path)
except OSError:
try:
logger.info("Checking for megsim data in '~/mne_data'...")
path = op.join(op.expanduser("~"), "mne_data")
if not op.exists(path):
logger.info("Trying to create "
"'~/mne_data' in home directory")
os.mkdir(path)
except OSError:
raise OSError("User does not have write permissions "
"at '%s', try giving the path as an argument "
"to data_path() where user has write "
"permissions, for ex:data_path"
"('/home/xyz/me2/')" % (path))
if not isinstance(path, string_types):
raise ValueError('path must be a string or None')
destination = _url_to_local_path(url, op.join(path, 'MEGSIM'))
destinations = [destination]
split = op.splitext(destination)
is_zip = True if split[1].lower() == '.zip' else False
# Fetch the file
do_unzip = False
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)
do_unzip = True
if is_zip:
z = zipfile.ZipFile(destination)
decomp_dir, name = op.split(destination)
files = z.namelist()
# decompress if necessary (if download was re-done)
if do_unzip:
stdout.write('Decompressing %g files from\n'
'"%s" ...' % (len(files), name))
z.extractall(decomp_dir)
stdout.write(' [done]\n')
z.close()
destinations = [op.join(decomp_dir, f) for f in files]
# Offer to update the path
path = op.abspath(path)
if update_path is None:
if get_config(key, '') != path:
update_path = True
msg = ('Do you want to set the path:\n %s\nas the default '
'MEGSIM dataset path in the mne-python config ([y]/n)? '
% path)
answer = raw_input(msg)
if answer.lower() == 'n':
update_path = False
else:
update_path = False
if update_path is True:
set_config(key, path)
return destinations
def load_data(condition='visual', data_format='raw', data_type='experimental',
path=None, force_update=False, update_path=None):
"""Get path to local copy of MEGSIM dataset type
Parameters
----------
condition : str
The condition to use. Either 'visual', 'auditory', or 'somatosensory'.
data_format : str
The data format. Either 'raw', 'evoked', or 'single-trial'.
data_type : str
The type of data. Either 'experimental' or 'simulation'.
path : None | str
Location of where to look for the MEGSIM data storing location.
If None, the environment variable or config parameter
MNE_DATASETS_MEGSIM_PATH is used. If it doesn't exist, the
"mne-python/examples" directory is used. If the MEGSIM dataset
is not found under the given path (e.g., as
"mne-python/examples/MEGSIM"), 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_MEGSIM_PATH in mne-python
config to the given path. If None, the user is prompted.
Returns
-------
paths : list
List of local data paths of the given type.
Notes
-----
For example, one could do:
>>> from mne.datasets import megsim
>>> megsim.load_data('visual', 'raw', 'experimental', os.getenv('HOME') + '/datasets') # doctest:+SKIP
And this would download the raw visual experimental MEGSIM dataset to the
'datasets' folder, and prompt the user to save the 'datasets' path to the
mne-python config, if it isn't there already.
The MEGSIM dataset is documented in the following publication:
Aine CJ, Sanfratello L, Ranken D, Best E, MacArthur JA, Wallace T,
Gilliam K, Donahue CH, Montano R, Bryant JE, Scott A, Stephen JM
(2012) MEG-SIM: A Web Portal for Testing MEG Analysis Methods using
Realistic Simulated and Empirical Data. Neuroinform 10:141-158
"""
if not condition.lower() in valid_conditions:
raise ValueError('Unknown condition "%s"' % condition)
if not data_format in valid_data_formats:
raise ValueError('Unknown data_format "%s"' % data_format)
if not data_type in valid_data_types:
raise ValueError('Unknown data_type "%s"' % data_type)
urls = url_match(condition, data_format, data_type)
data_paths = list()
for url in urls:
data_paths.extend(data_path(url, path, force_update, update_path))
return data_paths
|