Repository navigation
Add sample fixture drift checker #302
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
5 commits
Select commit
Hold shift + click to select a range
5802e37
Add sample fixture drift checker
neubig ca908db
Apply pre-commit fixes
neubig 199e48a
Harden sample fixture checker errors
neubig 5b1ba5d
Match sample fixture JSONL serialization
neubig ab3785c
Warn when fixture update may stale SFT samples
neubig 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,122 @@ | ||
| """Check or refresh generated dataset sample fixtures. | ||
|
|
||
| The check regenerates ``sample_std.json`` from each dataset's checked-in | ||
| ``sample_atif.json`` and compares it with the committed fixture. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import difflib | ||
| import json | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
| DATASETS_DIR = REPO_ROOT / "datasets" | ||
|
|
||
|
|
||
| def dataset_dirs(names: list[str]) -> list[Path]: | ||
| if names: | ||
| dirs = [DATASETS_DIR / name for name in names] | ||
| for dataset_dir in dirs: | ||
| if not dataset_dir.is_dir(): | ||
| raise SystemExit(f"error: unknown dataset '{dataset_dir.name}'") | ||
| return dirs | ||
| return sorted(path for path in DATASETS_DIR.iterdir() if path.is_dir()) | ||
|
|
||
|
|
||
| def render_jsonl(items: list[object]) -> str: | ||
| return "".join(json.dumps(item, ensure_ascii=True) + "\n" for item in items) | ||
|
|
||
|
|
||
| def regenerate_sample_std(dataset_dir: Path) -> str: | ||
| sample_atif = dataset_dir / "sample_atif.json" | ||
| converter = dataset_dir / "atif_to_std.py" | ||
| if not sample_atif.exists() or not converter.exists(): | ||
| raise FileNotFoundError(f"{dataset_dir.name} is missing sample_atif.json or atif_to_std.py") | ||
|
|
||
| env = os.environ.copy() | ||
| existing_pythonpath = os.environ.get("PYTHONPATH", "") | ||
| env["PYTHONPATH"] = os.pathsep.join( | ||
| entry for entry in [str(REPO_ROOT), existing_pythonpath] if entry | ||
| ) | ||
| atif_rows = json.loads(sample_atif.read_text()) | ||
| result = subprocess.run( | ||
| [sys.executable, str(converter)], | ||
| input=render_jsonl(atif_rows), | ||
| text=True, | ||
| capture_output=True, | ||
| check=True, | ||
| env=env, | ||
| ) | ||
| std_rows = [json.loads(line) for line in result.stdout.splitlines() if line.strip()] | ||
| return json.dumps(std_rows, indent=2, ensure_ascii=True) + "\n" | ||
|
|
||
|
|
||
| def check_dataset(dataset_dir: Path, update: bool) -> tuple[bool, str]: | ||
| sample_std = dataset_dir / "sample_std.json" | ||
| if not sample_std.exists(): | ||
| return True, f"skip {dataset_dir.name}: missing sample_std.json" | ||
|
|
||
| regenerated = regenerate_sample_std(dataset_dir) | ||
| current = sample_std.read_text() | ||
| if regenerated == current: | ||
| return True, f"ok {dataset_dir.name}" | ||
|
|
||
| if update: | ||
| sample_std.write_text(regenerated) | ||
| sft_dir = dataset_dir / "sample_sft" | ||
| sft_warning = ( | ||
| " (re-run agents/*/std_to_sft.py to refresh sample_sft/)" | ||
| if sft_dir.is_dir() | ||
| else "" | ||
| ) | ||
| return True, f"updated {dataset_dir.name}{sft_warning}" | ||
|
|
||
| diff = "".join( | ||
| difflib.unified_diff( | ||
| current.splitlines(keepends=True), | ||
| regenerated.splitlines(keepends=True), | ||
| fromfile=f"{dataset_dir.name}/sample_std.json", | ||
| tofile=f"{dataset_dir.name}/sample_std.json regenerated", | ||
| n=3, | ||
| ) | ||
| ) | ||
| return False, f"drift {dataset_dir.name}\n{diff}" | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--dataset", | ||
| action="append", | ||
| default=[], | ||
| help="Dataset directory name to check. May be repeated. Defaults to all datasets.", | ||
| ) | ||
| parser.add_argument( | ||
| "--update", | ||
| action="store_true", | ||
| help="Rewrite stale sample_std.json files instead of failing.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| ok = True | ||
| for dataset_dir in dataset_dirs(args.dataset): | ||
| try: | ||
| dataset_ok, message = check_dataset(dataset_dir, args.update) | ||
| except subprocess.CalledProcessError as exc: | ||
| dataset_ok = False | ||
| message = f"error {dataset_dir.name}: converter failed\n{exc.stderr}" | ||
| except Exception as exc: | ||
| dataset_ok = False | ||
| message = f"error {dataset_dir.name}: {exc}" | ||
| ok = ok and dataset_ok | ||
| print(message) | ||
| return 0 if ok else 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.