-
Notifications
You must be signed in to change notification settings - Fork 3k
[data_files] Only match separated split names #4633
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d963a0e
only match separated split names
lhoestq 159649a
docs
lhoestq 7893856
add space separator
lhoestq 31bf1bb
fix win
lhoestq 3681589
Merge branch 'main' into only-match-separated-split-names
lhoestq 93f4526
add testing
lhoestq e6adffb
add evaluation
lhoestq 9393386
suggestions in doc
lhoestq 8efbb78
use list comprehension + support numbers
lhoestq ee15ede
update tests
lhoestq 61a18f8
Merge branch 'main' into only-match-separated-split-names
lhoestq d372b28
remove unnecessary patching and context manager
lhoestq 3e97fd5
style
lhoestq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import os | ||
| from itertools import chain | ||
| from pathlib import Path, PurePath | ||
| from typing import List | ||
| from unittest.mock import patch | ||
|
|
||
| import fsspec | ||
| import pytest | ||
| from fsspec.spec import AbstractFileSystem | ||
| from huggingface_hub.hf_api import DatasetInfo | ||
|
|
||
| from datasets.data_files import ( | ||
|
|
@@ -491,6 +492,47 @@ def test_DataFilesDict_from_hf_local_or_remote_hashing(text_file): | |
| assert Hasher.hash(data_files1) != Hasher.hash(data_files2) | ||
|
|
||
|
|
||
| def mock_fs(file_paths: List[str]): | ||
| """ | ||
| Set up a mock filesystem for fsspec containing the provided files | ||
|
|
||
| Example: | ||
|
|
||
| ```py | ||
| >>> fs = mock_fs(["data/train.txt", "data.test.txt"]) | ||
| >>> assert fsspec.get_filesystem_class("mock").__name__ == "DummyTestFS" | ||
| >>> assert type(fs).__name__ == "DummyTestFS" | ||
| >>> print(fs.glob("**")) | ||
| ["data", "data/train.txt", "data.test.txt"] | ||
| ``` | ||
| """ | ||
|
|
||
| dir_paths = {file_path.rsplit("/")[0] for file_path in file_paths if "/" in file_path} | ||
| fs_contents = [{"name": dir_path, "type": "directory"} for dir_path in dir_paths] + [ | ||
| {"name": file_path, "type": "file", "size": 10} for file_path in file_paths | ||
| ] | ||
|
|
||
| class DummyTestFS(AbstractFileSystem): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome that you use a mock file system. I personally think this goes in the right direction. |
||
| protocol = "mock" | ||
| _fs_contents = fs_contents | ||
|
|
||
| def ls(self, path, detail=True, refresh=True, **kwargs): | ||
| if kwargs.pop("strip_proto", True): | ||
| path = self._strip_protocol(path) | ||
|
|
||
| files = not refresh and self._ls_from_cache(path) | ||
| if not files: | ||
| files = [file for file in self._fs_contents if path == self._parent(file["name"])] | ||
| files.sort(key=lambda file: file["name"]) | ||
| self.dircache[path.rstrip("/")] = files | ||
|
|
||
| if detail: | ||
| return files | ||
| return [file["name"] for file in files] | ||
|
|
||
| return DummyTestFS() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "data_file_per_split", | ||
| [ | ||
|
|
@@ -541,24 +583,30 @@ def test_DataFilesDict_from_hf_local_or_remote_hashing(text_file): | |
| {"validation": "dev/dataset.txt"}, | ||
| # With other extensions | ||
| {"train": "train.parquet", "test": "test.parquet", "validation": "valid.parquet"}, | ||
| # With "dev" or "eval" without separators | ||
| {"train": "developers_list.txt"}, | ||
| {"train": "data/seqeval_results.txt"}, | ||
|
Comment on lines
+587
to
+588
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also adding a test for "test": "contest.txt"? |
||
| {"train": "contest.txt"}, | ||
| # With supported separators | ||
| {"test": "my.test.file.txt"}, | ||
| {"test": "my-test-file.txt"}, | ||
| {"test": "my_test_file.txt"}, | ||
| {"test": "my test file.txt"}, | ||
| {"test": "test00001.txt"}, | ||
| ], | ||
| ) | ||
| def test_get_data_files_patterns(data_file_per_split): | ||
| data_file_per_split = {k: v if isinstance(v, list) else [v] for k, v in data_file_per_split.items()} | ||
| file_paths = [file_path for split_file_paths in data_file_per_split.values() for file_path in split_file_paths] | ||
| fs = mock_fs(file_paths) | ||
|
|
||
| def resolver(pattern): | ||
| return [PurePath(path) for path in chain(*data_file_per_split.values()) if PurePath(path).match(pattern)] | ||
| return [PurePath(file_path) for file_path in fs.glob(pattern) if fs.isfile(file_path)] | ||
|
|
||
| patterns_per_split = _get_data_files_patterns(resolver) | ||
| assert sorted(patterns_per_split.keys()) == sorted(data_file_per_split.keys()) | ||
| for split, patterns in patterns_per_split.items(): | ||
| matched = [ | ||
| path | ||
| for path in chain(*data_file_per_split.values()) | ||
| for pattern in patterns | ||
| if PurePath(path).match(pattern) | ||
| ] | ||
| assert len(matched) == len(data_file_per_split[split]) | ||
| matched = [file_path.as_posix() for pattern in patterns for file_path in resolver(pattern)] | ||
| assert matched == data_file_per_split[split] | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Indeed, much clearer this way! Thanks.