Datasets:

Modalities:
Text
Formats:
csv
Languages:
Arabic
ArXiv:
License:
File size: 2,405 Bytes
4aa23b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Rebuild the verified AraSTEM configs from the public source file on GitHub.

Usage:
    python apply_filter.py [output_dir]

Writes one parquet file per config, <output_dir>/<level>_<subject>.parquet.
"""
import hashlib
import json
import os
import sys
import urllib.request

import pandas as pd
from huggingface_hub import hf_hub_download

SOURCE = "AI-DSCHubAUB/AraSTEM-Dataset"
SOURCE_REVISION = "d8f8fdd4e420df45737df1105a60a34b9b4aecd2"
SOURCE_URL = f"https://raw.githubusercontent.com/{SOURCE}/{SOURCE_REVISION}/AraSTEMv5.json"
THIS_REPO = "IFM/AraSTEM_verified"
COLUMNS = ["question", "option_0", "option_1", "option_2", "option_3", "option_4",
           "correct_option", "level", "subject", "resource"]
EXPECTED_KEPT = {
    "college_biology": 567, "college_chemistry": 292, "college_dentistry": 603,
    "college_information_technology": 331, "college_medicine": 690, "college_pharmacy": 408,
    "college_physics": 133, "primary_math": 2774, "primary_science": 3802,
    "secondary_biology": 313, "secondary_chemistry": 228, "secondary_math": 81,
    "secondary_physics": 173, "secondary_science": 99,
}


def fingerprint(row):
    return hashlib.sha256("\x1f".join(str(row[c]) for c in COLUMNS).encode()).hexdigest()[:16]


ids = pd.read_csv(hf_hub_download(THIS_REPO, "ids.csv", repo_type="dataset"))
with urllib.request.urlopen(SOURCE_URL) as f:
    source = pd.DataFrame(json.load(f), columns=COLUMNS)
assert len(source) == len(ids), f"expected {len(ids)} source rows, got {len(source)}"
mismatch = [i for i, row in source.iterrows() if fingerprint(row) != ids.at[i, "fingerprint"]]
assert not mismatch, f"source rows differ from the audited version: {mismatch[:10]}"

config = (source["level"] + "_" + source["subject"]).str.replace(" ", "_")
assert config.equals(ids["config"]), "config labels differ from ids.csv"
kept = ids["status"].eq("kept").to_numpy()

out = sys.argv[1] if len(sys.argv) > 1 else "arastem_verified"
os.makedirs(out, exist_ok=True)
for name, expected in EXPECTED_KEPT.items():
    verified = source[kept & config.eq(name).to_numpy()].reset_index(drop=True)
    assert len(verified) == expected, f"{name}: expected {expected} rows, got {len(verified)}"
    verified.to_parquet(os.path.join(out, f"{name}.parquet"))
total = sum(EXPECTED_KEPT.values())
assert total == int(kept.sum()) == 10494
print(f"Wrote {total} rows in {len(EXPECTED_KEPT)} configs to {out}/")