Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PathLike input in save_to_disk / load_from_disk #6828

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/datasets/arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import pyarrow as pa
import pyarrow.compute as pc
from fsspec.core import url_to_fs
from fsspec.utils import stringify_path
from huggingface_hub import (
CommitInfo,
CommitOperationAdd,
Expand Down Expand Up @@ -1449,7 +1450,7 @@ def save_to_disk(
If you want to store paths or urls, please use the Value("string") type.

Args:
dataset_path (`str`):
dataset_path (`PathLike`):
Copy link
Member

Choose a reason for hiding this comment

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

Great, so now this is aligned with the type hint: dataset_path: PathLike

Copy link
Member Author

Choose a reason for hiding this comment

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

yes we should have one imo before merging

Path (e.g. `dataset/train`) or remote URI (e.g. `s3://my-bucket/dataset/train`)
of the dataset directory where the dataset will be saved to.
fs (`fsspec.spec.AbstractFileSystem`, *optional*):
Expand Down Expand Up @@ -1512,7 +1513,7 @@ def save_to_disk(
num_shards = num_shards if num_shards is not None else num_proc

fs: fsspec.AbstractFileSystem
fs, _ = url_to_fs(dataset_path, **(storage_options or {}))
fs, _ = url_to_fs(stringify_path(dataset_path), **(storage_options or {}))

if not is_remote_filesystem(fs):
parent_cache_files_paths = {
Expand Down Expand Up @@ -1649,7 +1650,7 @@ def _build_local_temp_path(uri_or_path: str) -> Path:

@staticmethod
def load_from_disk(
dataset_path: str,
dataset_path: PathLike,
fs="deprecated",
keep_in_memory: Optional[bool] = None,
storage_options: Optional[dict] = None,
Expand All @@ -1659,7 +1660,7 @@ def load_from_disk(
filesystem using any implementation of `fsspec.spec.AbstractFileSystem`.

Args:
dataset_path (`str`):
dataset_path (`PathLike`):
Path (e.g. `"dataset/train"`) or remote URI (e.g. `"s3//my-bucket/dataset/train"`)
of the dataset directory where the dataset will be loaded from.
fs (`fsspec.spec.AbstractFileSystem`, *optional*):
Expand Down Expand Up @@ -1702,7 +1703,7 @@ def load_from_disk(
storage_options = fs.storage_options

fs: fsspec.AbstractFileSystem
fs, dataset_path = url_to_fs(dataset_path, **(storage_options or {}))
fs, dataset_path = url_to_fs(stringify_path(dataset_path), **(storage_options or {}))

dest_dataset_path = dataset_path
dataset_dict_json_path = posixpath.join(dest_dataset_path, config.DATASETDICT_JSON_FILENAME)
Expand Down
5 changes: 3 additions & 2 deletions src/datasets/dataset_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fsspec
import numpy as np
from fsspec.core import url_to_fs
from fsspec.utils import stringify_path
from huggingface_hub import (
CommitInfo,
CommitOperationAdd,
Expand Down Expand Up @@ -1231,7 +1232,7 @@ def save_to_disk(
If you want to store paths or urls, please use the Value("string") type.

Args:
dataset_dict_path (`str`):
dataset_dict_path (`PathLike`):
Path (e.g. `dataset/train`) or remote URI
(e.g. `s3://my-bucket/dataset/train`) of the dataset dict directory where the dataset dict will be
saved to.
Expand Down Expand Up @@ -1281,7 +1282,7 @@ def save_to_disk(
storage_options = fs.storage_options

fs: fsspec.AbstractFileSystem
fs, _ = url_to_fs(dataset_dict_path, **(storage_options or {}))
fs, _ = url_to_fs(stringify_path(dataset_dict_path), **(storage_options or {}))

if num_shards is None:
num_shards = {k: None for k in self}
Expand Down
11 changes: 8 additions & 3 deletions src/datasets/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import requests
import yaml
from fsspec.core import url_to_fs
from fsspec.utils import stringify_path
from huggingface_hub import DatasetCard, DatasetCardData, HfApi, HfFileSystem

from . import config
Expand Down Expand Up @@ -89,6 +90,7 @@
from .utils.logging import get_logger
from .utils.metadata import MetadataConfigs
from .utils.py_utils import get_imports, lock_importable_file
from .utils.typing import PathLike
from .utils.version import Version


Expand Down Expand Up @@ -2632,14 +2634,17 @@ def load_dataset(


def load_from_disk(
dataset_path: str, fs="deprecated", keep_in_memory: Optional[bool] = None, storage_options: Optional[dict] = None
dataset_path: PathLike,
fs="deprecated",
keep_in_memory: Optional[bool] = None,
storage_options: Optional[dict] = None,
) -> Union[Dataset, DatasetDict]:
"""
Loads a dataset that was previously saved using [`~Dataset.save_to_disk`] from a dataset directory, or
from a filesystem using any implementation of `fsspec.spec.AbstractFileSystem`.

Args:
dataset_path (`str`):
dataset_path (`PathLike`):
Path (e.g. `"dataset/train"`) or remote URI (e.g.
`"s3://my-bucket/dataset/train"`) of the [`Dataset`] or [`DatasetDict`] directory where the dataset will be
loaded from.
Expand Down Expand Up @@ -2684,7 +2689,7 @@ def load_from_disk(
storage_options = fs.storage_options

fs: fsspec.AbstractFileSystem
fs, *_ = url_to_fs(dataset_path, **(storage_options or {}))
fs, *_ = url_to_fs(stringify_path(dataset_path), **(storage_options or {}))
if not fs.exists(dataset_path):
raise FileNotFoundError(f"Directory {dataset_path} not found")
if fs.isfile(posixpath.join(dataset_path, config.DATASET_INFO_FILENAME)) and fs.isfile(
Expand Down