import os
import os.path as op
import sys
import time
from pathlib import Path
from shutil import rmtree
from .. import __version__ as mne_version
from ..fixes import _compare_version
from ..utils import _safe_input, logger, warn
from .config import (
MISC_VERSIONED,
RELEASES,
TESTING_VERSIONED,
_bst_license_text,
)
from .utils import (
_dataset_version,
_do_path_update,
_downloader_params,
_get_path,
_log_time_size,
_pl,
)
_FAKE_VERSION = None
def fetch_dataset(
dataset_params,
processor=None,
path=None,
force_update=False,
update_path=True,
download=True,
check_version=False,
return_version=False,
accept=False,
auth=None,
token=None,
) -> Path | tuple[Path, str]:
"""Fetch an MNE-compatible dataset using pooch.
Parameters
----------
dataset_params : list of dict | dict
The dataset name(s) and corresponding parameters to download the
dataset(s). The dataset parameters that contains the following keys:
``archive_name``, ``url``, ``folder_name``, ``hash``,
``config_key`` (optional). See Notes.
processor : None | "unzip" | "untar" | instance of pooch.Unzip | instance of pooch.Untar
What to do after downloading the file. ``"unzip"`` and ``"untar"`` will
decompress the downloaded file in place; for custom extraction (e.g.,
only extracting certain files from the archive) pass an instance of
``pooch.Unzip`` or ``pooch.Untar``. If ``None`` (the
default), the files are left as-is.
path : None | str
Directory in which to put the dataset. If ``None``, the dataset
location is determined by first checking whether
``dataset_params['config_key']`` is defined, and if so, whether that
config key exists in the MNE-Python config file. If so, the configured
path is used; if not, the location is set to the value of the
``MNE_DATA`` config key (if it exists), or ``~/mne_data`` otherwise.
force_update : bool
Force update of the dataset even if a local copy exists.
Default is False.
update_path : bool | None
If True (default), set the mne-python config to the given
path. If None, the user is prompted.
download : bool
If False and the dataset has not been downloaded yet, it will not be
downloaded and the path will be returned as ``''`` (empty string). This
is mostly used for testing purposes and can be safely ignored by most
users.
check_version : bool
Whether to check the version of the dataset or not. Each version
of the dataset is stored in the root with a ``version.txt`` file.
return_version : bool
Whether or not to return the version of the dataset or not.
Defaults to False.
accept : bool
Some MNE-supplied datasets require acceptance of an additional license.
Default is ``False``.
auth : tuple | None
Optional authentication tuple containing the username and
password/token, passed to ``pooch.HTTPDownloader`` (e.g.,
``auth=('foo', 012345)``).
token : str | None
Optional authentication token passed to ``pooch.HTTPDownloader``.
Returns
-------
data_path : instance of Path
The path to the fetched dataset.
version : str
Only returned if ``return_version`` is True.
See Also
--------
mne.get_config
mne.set_config
mne.datasets.has_dataset
Notes
-----
The ``dataset_params`` argument must contain the following keys:
- ``archive_name``: The name of the (possibly compressed) file to download
- ``url``: URL from which the file can be downloaded
- ``folder_name``: the subfolder within the ``MNE_DATA`` folder in which to
save and uncompress (if needed) the file(s)
- ``hash``: the cryptographic hash type of the file followed by a colon and
then the hash value (examples: "sha256:19uheid...", "md5:upodh2io...")
- ``config_key`` (optional): key passed to :func:`mne.set_config` to store
the on-disk location of the downloaded dataset (e.g.,
``"MNE_DATASETS_EEGBCI_PATH"``). This will only work for the provided
datasets listed :ref:`here <datasets>`; do not use for user-defined
datasets.
An example would look like::
{'dataset_name': 'sample',
'archive_name': 'MNE-sample-data-processed.tar.gz',
'hash': 'md5:e8f30c4516abdc12a0c08e6bae57409c',
'url': 'https://github.com/mne-tools/mne-data/releases/download/'
'sample-6/MNE-sample-data-processed.tar.gz',
'folder_name': 'MNE-sample-data',
'config_key': 'MNE_DATASETS_SAMPLE_PATH'}
For datasets where a single (possibly compressed) file must be downloaded,
pass a single :class:`dict` as ``dataset_params``. For datasets where
multiple files must be downloaded and (optionally) uncompressed separately,
pass a list of dicts.
"""
import pooch
t0 = time.time()
if auth is not None:
if len(auth) != 2:
raise RuntimeError(
"auth should be a 2-tuple consisting of a username and password/token."
)
if processor == "untar":
processor = pooch.Untar(extract_dir=path)
elif processor == "unzip":
processor = pooch.Unzip(extract_dir=path)
if isinstance(dataset_params, dict):
dataset_params = [dataset_params]
names = [params["dataset_name"] for params in dataset_params]
name = names[0]
dataset_dict = dataset_params[0]
config_key = dataset_dict.get("config_key", None)
folder_name = dataset_dict["folder_name"]
path = _get_path(path=path, key=config_key, name=name)
final_path = op.join(path, folder_name)
if name.startswith("bst_"):
final_path = op.join(final_path, name)
final_path = Path(final_path)
want_version = RELEASES.get(name, None)
want_version = _FAKE_VERSION if name == "fake" else want_version
data_version = _dataset_version(final_path, name)
outdated = want_version is not None and _compare_version(
want_version, ">", data_version
)
if outdated:
logger.info(
f"Dataset {name} version {data_version} out of date, "
f"latest version is {want_version}"
)
empty = Path("")
if (not force_update) and outdated and not download:
logger.info(
"Dataset out of date but force_update=False and download=False, "
"returning empty data_path"
)
return (empty, data_version) if return_version else empty
if (not force_update) and (not outdated) and (not name.startswith("hf_sef_")):
if op.isdir(final_path):
if config_key is not None:
_do_path_update(path, update_path, config_key, name)
return (final_path, data_version) if return_version else final_path
elif not download:
return (empty, data_version) if return_version else empty
elif name.startswith("bst_"):
if accept or "--accept-brainstorm-license" in sys.argv:
answer = "y"
else:
answer = _safe_input(f"{_bst_license_text}Agree (y/[n])? ", use="y")
if answer.lower() != "y":
raise RuntimeError("You must agree to the license to use this dataset")
download_params = _downloader_params(auth=auth, token=token)
if name == "fake":
download_params["progressbar"] = False
downloader = pooch.HTTPDownloader(**download_params)
urls = dict()
registry = dict()
for idx, this_name in enumerate(names):
this_dataset = dataset_params[idx]
archive_name = this_dataset["archive_name"]
dataset_url = this_dataset["url"]
dataset_hash = this_dataset["hash"]
urls[archive_name] = dataset_url
registry[archive_name] = dataset_hash
use_path = final_path if processor is None else Path(path)
fetcher = pooch.create(
path=str(use_path),
base_url="",
version=None,
urls=urls,
registry=registry,
retry_if_failed=2,
)
pooch.get_logger().setLevel(logger.getEffectiveLevel())
sz = 0
logger.info(
"Fetching %s file%s for the %s dataset ...", len(names), _pl(names), name
)
for idx in range(len(names)):
archive_name = dataset_params[idx]["archive_name"]
try:
fetcher.fetch(
fname=archive_name, downloader=downloader, processor=processor
)
except ValueError as err:
err = str(err)
if "hash of downloaded file" in str(err):
raise ValueError(
f"{err} Consider using force_update=True to force "
"the dataset to be downloaded again."
) from None
else:
raise
fname = use_path / archive_name
sz += fname.stat().st_size
if processor is not None:
fname.unlink()
if name == "misc":
rmtree(final_path, ignore_errors=True)
os.replace(op.join(path, MISC_VERSIONED), final_path)
elif name == "testing":
rmtree(final_path, ignore_errors=True)
os.replace(op.join(path, TESTING_VERSIONED), final_path)
if config_key is not None:
old_name = "brainstorm" if name.startswith("bst_") else name
_do_path_update(path, update_path, config_key, old_name)
data_version = _dataset_version(path, name)
if check_version and (
_compare_version(data_version, "<", mne_version.strip(".git"))
):
warn(
f"The {name} dataset (version {data_version}) is older than "
f"mne-python (version {mne_version}). If the examples fail, "
f"you may need to update the {name} dataset by using "
f"mne.datasets.{name}.data_path(force_update=True)"
)
_log_time_size(t0, sz)
return (final_path, data_version) if return_version else final_path