Skip to content

Commit

Permalink
MRG, MAINT: Better downloading for testing and misc (#8696)
Browse files Browse the repository at this point in the history
* MAINT: Better downloading for testing and misc

* STY: Missed [circle front]

* FIX: Misc [circle front]

* FIX: Version [circle full]

* FIX: Version [circle full]
  • Loading branch information
larsoner authored Jan 4, 2021
1 parent 415ae68 commit 1386e14
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
66 changes: 54 additions & 12 deletions mne/datasets/tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from os import path as op
import re
import shutil
import zipfile
import sys
Expand All @@ -11,7 +12,7 @@
from mne.datasets._fsaverage.base import _set_montage_coreg_path
from mne.datasets.utils import _manifest_check_download

from mne.utils import (run_tests_if_main, requires_good_network, modified_env,
from mne.utils import (requires_good_network, modified_env,
get_subjects_dir, ArgvSetter, _pl, use_log_level,
catch_logging, hashfunc)

Expand Down Expand Up @@ -52,18 +53,62 @@ def test_datasets_basic(tmpdir):
assert sd.endswith('MNE-fsaverage-data')


def _fake_fetch_file(url, destination, print_destination=False):
with open(destination, 'w') as fid:
fid.write(url)


@requires_good_network
def test_downloads(tmpdir):
"""Test dataset URL handling."""
def test_downloads(tmpdir, monkeypatch, capsys):
"""Test dataset URL and version handling."""
# Try actually downloading a dataset
path = datasets._fake.data_path(path=str(tmpdir), update_path=False)
kwargs = dict(path=str(tmpdir), verbose=True)
path = datasets._fake.data_path(update_path=False, **kwargs)
out, _ = capsys.readouterr()
assert 'Downloading' in out
assert op.isdir(path)
assert op.isfile(op.join(path, 'bar'))
assert not datasets.utils.has_dataset('fake') # not in the desired path
assert datasets._fake.get_version() is None
assert datasets.utils._get_version('fake') is None
monkeypatch.setenv('_MNE_FAKE_HOME_DIR', str(tmpdir))
with pytest.warns(RuntimeWarning, match='non-standard config'):
new_path = datasets._fake.data_path(update_path=True, **kwargs)
assert path == new_path
out, _ = capsys.readouterr()
assert 'Downloading' not in out
# No version: shown as existing but unknown version
assert datasets.utils.has_dataset('fake')
# XXX logic bug, should be "unknown"
assert datasets._fake.get_version() == '0.7'
# With a version but no required one: shown as existing and gives version
fname = tmpdir / 'foo' / 'version.txt'
with open(fname, 'w') as fid:
fid.write('0.1')
assert datasets.utils.has_dataset('fake')
assert datasets._fake.get_version() == '0.1'
datasets._fake.data_path(download=False, **kwargs)
out, _ = capsys.readouterr()
assert 'out of date' not in out
# With the required version: shown as existing with the required version
monkeypatch.setattr(datasets.utils, '_FAKE_VERSION', '0.1')
assert datasets.utils.has_dataset('fake')
assert datasets._fake.get_version() == '0.1'
datasets._fake.data_path(download=False, **kwargs)
out, _ = capsys.readouterr()
assert 'out of date' not in out
monkeypatch.setattr(datasets.utils, '_FAKE_VERSION', '0.2')
# With an older version:
# 1. Marked as not actually being present
assert not datasets.utils.has_dataset('fake')
# 2. Will try to update when `data_path` gets called, with logged message
want_msg = 'Correctly trying to download newer version'

def _error_download(url, full_name, print_destination, hash_, hash_type):
assert 'foo.tgz' in url
assert str(tmpdir) in full_name
raise RuntimeError(want_msg)

monkeypatch.setattr(datasets.utils, '_fetch_file', _error_download)
with pytest.raises(RuntimeError, match=want_msg):
datasets._fake.data_path(**kwargs)
out, _ = capsys.readouterr()
assert re.match(r'.* 0\.1 .*out of date.* 0\.2.*', out, re.MULTILINE), out


@pytest.mark.slowtest
Expand Down Expand Up @@ -151,6 +196,3 @@ def test_manifest_check_download(tmpdir, n_have, monkeypatch):
assert op.isdir(destination)
for fname in _zip_fnames:
assert op.isfile(op.join(destination, fname))


run_tests_if_main()
15 changes: 13 additions & 2 deletions mne/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from ..externals.doccer import docformat


_FAKE_VERSION = None # used for monkeypatching while testing versioning

_data_path_doc = """Get path to local copy of {name} dataset.
Parameters
Expand Down Expand Up @@ -245,7 +247,7 @@ def _data_path(path=None, force_update=False, update_path=True, download=True,
path = _get_path(path, key, name)
# To update the testing or misc dataset, push commits, then make a new
# release on GitHub. Then update the "releases" variable:
releases = dict(testing='0.112', misc='0.7')
releases = dict(testing='0.112', misc='0.8')
# And also update the "md5_hashes['testing']" variable below.
# To update any other dataset, update the data archive itself (upload
# an updated version) and update the md5 hash.
Expand Down Expand Up @@ -327,7 +329,7 @@ def _data_path(path=None, force_update=False, update_path=True, download=True,
bst_raw='fa2efaaec3f3d462b319bc24898f440c',
bst_resting='70fc7bf9c3b97c4f2eab6260ee4a0430'),
fake='3194e9f7b46039bb050a74f3e1ae9908',
misc='2b2f2fec9d1197ed459117db1c6341ee',
misc='0f88194266121dd9409be94184231f25',
sample='12b75d1cb7df9dfb4ad73ed82f61094f',
somato='32fd2f6c8c7eb0784a1de6435273c48b',
spm='9f43f67150e3b694b523a21eb929ea75',
Expand Down Expand Up @@ -377,6 +379,15 @@ def _data_path(path=None, force_update=False, update_path=True, download=True,
logger.debug('folder_path: %s' % (folder_path,))

need_download = any(not op.exists(f) for f in folder_path)
# additional condition: check for version.txt and parse it
want_version = releases.get(name, None)
want_version = _FAKE_VERSION if name == 'fake' else want_version
if not need_download and want_version is not None:
data_version = _dataset_version(folder_path[0], name)
need_download = data_version != want_version
if need_download:
logger.info(f'Dataset {name} version {data_version} out of date, '
f'latest version is {want_version}')
if need_download and not download:
return ''

Expand Down

0 comments on commit 1386e14

Please sign in to comment.