This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
Multi30k mocked testing #1554
Merged
Merged
Multi30k mocked testing #1554
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import os | ||
| import random | ||
| import string | ||
| import tarfile | ||
| from collections import defaultdict | ||
| from unittest.mock import patch | ||
|
|
||
| from ..common.parameterized_utils import nested_params | ||
| from torchtext.datasets import Multi30k | ||
|
|
||
| from ..common.case_utils import TempDirMixin, zip_equal | ||
| from ..common.torchtext_test_case import TorchtextTestCase | ||
|
|
||
|
|
||
| def _get_mock_dataset(root_dir): | ||
| """ | ||
| root_dir: directory to the mocked dataset | ||
| """ | ||
| base_dir = os.path.join(root_dir, "Multi30k") | ||
| temp_dataset_dir = os.path.join(base_dir, "temp_dataset_dir") | ||
| os.makedirs(temp_dataset_dir, exist_ok=True) | ||
|
|
||
| seed = 1 | ||
| mocked_data = defaultdict(list) | ||
| for file_name in ("train.de", "train.en", "val.de", "val.en", "test.de", "test.en"): | ||
| txt_file = os.path.join(temp_dataset_dir, file_name) | ||
| with open(txt_file, "w") as f: | ||
| for i in range(5): | ||
| rand_string = " ".join( | ||
| random.choice(string.ascii_letters) for i in range(seed) | ||
| ) | ||
| content = f"{rand_string}\n" | ||
| f.write(content) | ||
| mocked_data[file_name].append(content) | ||
| seed += 1 | ||
|
|
||
| archive = {} | ||
| archive["train"] = os.path.join(base_dir, "training.tar.gz") | ||
| archive["val"] = os.path.join(base_dir, "validation.tar.gz") | ||
| archive["test"] = os.path.join(base_dir, "mmt16_task1_test.tar.gz") | ||
|
|
||
| for split in ("train", "val", "test"): | ||
| with tarfile.open(archive[split], "w:gz") as tar: | ||
| tar.add(os.path.join(temp_dataset_dir, f"{split}.de")) | ||
| tar.add(os.path.join(temp_dataset_dir, f"{split}.en")) | ||
|
|
||
| return mocked_data | ||
|
|
||
|
|
||
| class TestMulti30k(TempDirMixin, TorchtextTestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.root_dir = cls.get_base_temp_dir() | ||
| cls.samples = _get_mock_dataset(cls.root_dir) | ||
| cls.patcher = patch( | ||
| "torchdata.datapipes.iter.util.cacheholder._hash_check", return_value=True | ||
| ) | ||
| cls.patcher.start() | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| cls.patcher.stop() | ||
| super().tearDownClass() | ||
|
|
||
| @nested_params(["train", "valid", "test"], [("de", "en"), ("en", "de")]) | ||
| def test_multi30k(self, split, language_pair): | ||
| dataset = Multi30k(root=self.root_dir, split=split, language_pair=language_pair) | ||
| if split == "valid": | ||
| split = "val" | ||
| samples = list(dataset) | ||
| expected_samples = [(d1, d2) for d1, d2 in zip(self.samples[f'{split}.{language_pair[0]}'], self.samples[f'{split}.{language_pair[1]}'])] | ||
| for sample, expected_sample in zip_equal(samples, expected_samples): | ||
| self.assertEqual(sample, expected_sample) | ||
|
|
||
| @nested_params(["train", "valid", "test"], [("de", "en"), ("en", "de")]) | ||
| def test_multi30k_split_argument(self, split, language_pair): | ||
| dataset1 = Multi30k(root=self.root_dir, split=split, language_pair=language_pair) | ||
| (dataset2,) = Multi30k(root=self.root_dir, split=(split,), language_pair=language_pair) | ||
|
|
||
| for d1, d2 in zip_equal(dataset1, dataset2): | ||
| self.assertEqual(d1, d2) | ||
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.
One thought: since all of our datasets are utf-8 files, does it make sense to write unicode strings to make sure we don't have lingering bugs from default encodings when opening files? Maybe this is overkill, but it's been a big source of bugs when I did mostly windows development.
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.
Ya, agreed. I think it is a good suggestion!
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.
I will keep it as a follow-up item as generating random UTF-8 string is not trivial.