Skip to content

request.fetch

Request copies of relational datasets.

fetch(name, version=None)

Get a dataset with a name/version. Return path to a zipfile.

Something else.

Parameters:

Name Type Description Default
name str

Dataset name, usually lowercase with underscores.

required
version Optional[str]

Dataset version. Downloads a default (v0.0.3) if not provided.

None

Returns:

Type Description
str

A string representing the path to the downloaded dataset. For example:

str

```python

str

'/home/user/relational_datasets/toy_cancer_v0.0.3.zip'

str

```

str

The path is converted to a string from a pathlib object, so it should

str

work cross-platform.

Raises:

Type Description
urllib.error.URLError

If the data is not in the cache and cannot be downloaded, a failed request will raise this exception.

Examples:

Fetch toy_cancer dataset, version v0.0.3:

from relational_datasets import fetch

fetch('toy_cancer', 'v0.0.3')
# '/home/user/relational_datasets/toy_cancer_v0.0.3.zip'
Source code in relational_datasets/request.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def fetch(name: str, version: Optional[str] = None) -> str:
    """Get a dataset with a name/version. Return path to a zipfile.

    Something else.

    Arguments:
        name: Dataset name, usually lowercase with underscores.
        version: Dataset version. Downloads a default (`v0.0.3`) if not provided.

    Returns:
        A string representing the path to the downloaded dataset. For example:

        ```python
        '/home/user/relational_datasets/toy_cancer_v0.0.3.zip'
        ```

        The path is converted to a string from a `pathlib` object, so it should
        work cross-platform.

    Raises:
        urllib.error.URLError: If the data is not in the cache and cannot be
            downloaded, a failed request will raise this exception.

    Examples:

    Fetch `toy_cancer` dataset, version `v0.0.3`:

    ```python
    from relational_datasets import fetch

    fetch('toy_cancer', 'v0.0.3')
    # '/home/user/relational_datasets/toy_cancer_v0.0.3.zip'
    ```
    """

    # TODO(hayesall): This logic might be moved into the same function.
    data_file = _make_file_path(name, version)
    if data_file.is_file():
        return str(data_file)

    # Else the data needs to be downloaded.

    download_url = _make_data_url(name, version)

    with urlopen(download_url) as url:
        data = BytesIO(url.read())

    with open(data_file, "wb") as _fh:
        _fh.write(data.getbuffer())

    return str(data_file)

Last update: June 20, 2022