Skip to content

[MRG]: openml, Adds retrying if reading from cache fails - #12526

Merged
jnothman merged 12 commits into
scikit-learn:masterfrom
thomasjpfan:openml_cache_redownload
Nov 14, 2018
Merged

jnothman merged 12 commits into
scikit-learn:masterfrom
thomasjpfan:openml_cache_redownload

Conversation

@thomasjpfan

Copy link
Copy Markdown
Member

Reference Issues/PRs

Resolves #12517

What does this implement/fix? Explain your changes.

Adds a decorator called _retry_if_error_with_cache_removed to implement the retry logic, when the read from cache fails.

This PR includes a minor refactoring to use contextlib.closing to make sure streams are closed, when an exception gets raised.

Comment thread sklearn/datasets/openml.py Outdated
return os.path.join(data_home, 'openml.org', openml_path + ".gz")


def _retry_if_error_with_cache_removed(openml_path, data_home):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about calling this _retry_with_clean_cache

@janvanrijn

Copy link
Copy Markdown
Contributor

Sorry for checking in so late on this PR. I am currently trying to find out whether the following two cases hold:

  1. Dataset that had a problem on OpenML is being downloaded (legal openml error). What do we store in cache (preferably: nothing)? Can this workaround handle this?
  2. openML has a problem while we try to download a dataset (e.g., database connectivity error, php error. worst case: empty response. Should not happen often). What do we store in cache (preferably: nothing)? Can this workaround handle this?

Not sure if this is part of this PR, but this gives me an error: (dataset with if 100000 does not exist yet)

def test_fetch_openml_server_error():
    fetch_openml(data_id=100000)

last part trace:

openml_path = 'api/v1/json/data/100000', data_home = '/home/janvanrijn/scikit_learn_data/openml'

    def _open_openml_url(openml_path, data_home):
        """
        Returns a resource from OpenML.org. Caches it to data_home if required.
    
        Parameters
        ----------
        openml_path : str
            OpenML URL that will be accessed. This will be prefixes with
            _OPENML_PREFIX
    
        data_home : str
            Directory to which the files will be cached. If None, no caching will
            be applied.
    
        Returns
        -------
        result : stream
            A stream to the OpenML resource
        """
        def is_gzip(_fsrc):
            return _fsrc.info().get('Content-Encoding', '') == 'gzip'
    
        req = Request(_OPENML_PREFIX + openml_path)
        req.add_header('Accept-encoding', 'gzip')
    
        if data_home is None:
            fsrc = urlopen(req)
            if is_gzip(fsrc):
                if PY2:
                    fsrc = BytesIO(fsrc.read())
                return gzip.GzipFile(fileobj=fsrc, mode='rb')
            return fsrc
    
        local_path = _get_local_path(openml_path, data_home)
        if not os.path.exists(local_path):
            try:
                os.makedirs(os.path.dirname(local_path))
            except OSError:
                # potentially, the directory has been created already
                pass
    
            try:
                with closing(urlopen(req)) as fsrc:
                    if is_gzip(fsrc):
                        with open(local_path, 'wb') as fdst:
                            shutil.copyfileobj(fsrc, fdst)
                    else:
                        with gzip.GzipFile(local_path, 'wb') as fdst:
                            shutil.copyfileobj(fsrc, fdst)
            except Exception:
>               os.unlink(local_path)
E               FileNotFoundError: [Errno 2] No such file or directory: '/home/janvanrijn/scikit_learn_data/openml/openml.org/api/v1/json/data/100000.gz'

I will contribute test-cases for the first 2 examples as well.

@janvanrijn

Copy link
Copy Markdown
Contributor

Fun fact. I tried to download the following two files.

def test_fetch_openml_server_error():
    # does not exists
    fetch_openml(data_id=1000000)
def test_fetch_openml_server_error():
    # due to inconsistency, can not download data file
    fetch_openml(data_id=40864)

In both cases, there are no (unwanted) residuals in the cache folder, except for the aforementioned error. Indeed, for dataset 40864 the features and data description are available, but there is no harm there.

Additionally, I faked an error on the test server. Upon fetching the dataset for the first time, it failed and cached the failed reply. After fixing the response, it succeeded with warning message. After trying it again, it succeeded.

Apart from the error message (does that belong to this PR?) this LGTM.

Comment thread sklearn/datasets/openml.py Outdated
with gzip.GzipFile(local_path, 'wb') as fdst:
shutil.copyfileobj(fsrc, fdst)
except Exception:
os.unlink(local_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can throw an error if the file in local_path does not exists

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we should probably only unlink if it exists and otherwise do nothing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR was updated to check if the file exists before unlinking it.

@amueller amueller added this to the 0.20.1 milestone Nov 12, 2018
@amueller

Copy link
Copy Markdown
Member

can you please add a test for that case as well?

@thomasjpfan

Copy link
Copy Markdown
Member Author

@amueller This PR was updated with a test: test_open_openml_url_unlinks_local_path.

@amueller

Copy link
Copy Markdown
Member

cool, merge?

@jnothman
jnothman merged commit 14c816e into scikit-learn:master Nov 14, 2018
@jnothman

Copy link
Copy Markdown
Member

Thanks @thomasjpfan

jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Nov 14, 2018
jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Nov 14, 2018
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetch_openml migration between 0.20.0 and 0.20.1

4 participants