genomeqa / scripts /create_eukaryote_cls.py
lewtun's picture
lewtun HF Staff
Normalize eukaryote_cls schema and metadata
ae30ece verified
Raw History Blame Contribute Delete
3.79 kB
"""Normalize the GenomeQA eukaryote_cls configuration to the mcq schema."""
import argparse
import ast
import json
from pathlib import Path
from datasets import load_dataset
from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download
DATASET_ID = "HuggingFaceBio/genomeqa"
PARQUET_PATH = "mcq/eukaryote_cls_mcq.parquet"
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--source-revision", required=True)
parser.add_argument("--output-dataset", default=DATASET_ID)
parser.add_argument("--output-dir", type=Path, default=Path.cwd())
parser.add_argument("--publish", action="store_true")
args = parser.parse_args()
reference = load_dataset(DATASET_ID, "mcq", split="train", revision=args.source_revision)
source = load_dataset(DATASET_ID, "eukaryote_cls", split="train", revision=args.source_revision)
normalized = source.map(lambda row: {"metadata": json.dumps(ast.literal_eval(row["metadata"]))})
normalized = normalized.cast(reference.features)
parquet_path = args.output_dir / PARQUET_PATH
parquet_path.parent.mkdir(parents=True, exist_ok=True)
normalized.to_parquet(parquet_path)
api = HfApi()
head = api.repo_info(args.output_dataset, repo_type="dataset").sha
card = Path(hf_hub_download(args.output_dataset, "README.md", repo_type="dataset", revision=head)).read_text()
if "- config_name: eukaryote_cls\n features:" not in card:
info = f"""- config_name: eukaryote_cls
features:
- name: id
dtype: int64
- name: subtask
dtype: string
- name: question
dtype: string
- name: options
list: string
- name: answer_index
dtype: int64
- name: answer_text
dtype: string
- name: metadata
dtype: string
splits:
- name: train
num_bytes: {normalized.data.nbytes}
num_examples: {len(normalized)}
download_size: {parquet_path.stat().st_size}
dataset_size: {normalized.data.nbytes}
"""
card = card.replace("- config_name: binary_600\n", info + "- config_name: binary_600\n", 1)
if "The `eukaryote_cls` configuration uses the `mcq` feature schema" not in card:
note = "The `eukaryote_cls` configuration uses the `mcq` feature schema, including JSON-serialized `metadata`.\n\n"
card = card.replace("The `binary_600` and `mcq_600` configurations", note + "The `binary_600` and `mcq_600` configurations", 1)
if "scripts/create_eukaryote_cls.py" not in card:
card += f"""
## Reproducing
To rebuild and publish the normalized `eukaryote_cls` configuration from the pinned source revision, run:
```bash
python scripts/create_eukaryote_cls.py --source-revision {args.source_revision} --output-dataset {args.output_dataset} --publish
```
The script preserves the source rows, converts `metadata` from Python dictionary strings to JSON, and casts the
columns to the `mcq` feature schema. It publishes the Parquet file, this card, and the builder script in one commit.
"""
card_path = args.output_dir / "README.md"
card_path.write_text(card)
print(f"Prepared {len(normalized)} rows at {parquet_path}")
if args.publish:
commit = api.create_commit(
repo_id=args.output_dataset,
repo_type="dataset",
parent_commit=head,
commit_message="Normalize eukaryote_cls schema and metadata",
operations=[
CommitOperationAdd(path_in_repo=PARQUET_PATH, path_or_fileobj=parquet_path),
CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=card_path),
CommitOperationAdd(path_in_repo="scripts/create_eukaryote_cls.py", path_or_fileobj=__file__),
],
)
print(f"Published {commit.commit_url}")
if __name__ == "__main__":
main()