Upload alea/genbase.py with huggingface_hub
Browse files- alea/genbase.py +103 -0
alea/genbase.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared emit/validate helpers for gen_* dataset modules.
|
| 2 |
+
|
| 3 |
+
Each gen_<name>.py module defines generator functions returning
|
| 4 |
+
(state: str, qs: list[question-dict]) where a question-dict is:
|
| 5 |
+
|
| 6 |
+
{"qid": str, # unique within the request
|
| 7 |
+
"qtype": "choice"|"noul"|"score",
|
| 8 |
+
"instructions": str,
|
| 9 |
+
"options": [str, ...], # choice/score option texts; [] for noul
|
| 10 |
+
"target": [float, ...] or float} # choice/score: distribution over
|
| 11 |
+
# options (must sum to ~1); noul: P(yes)
|
| 12 |
+
|
| 13 |
+
and exposes FAMILIES = {name: fn} and WEIGHTS = {name: w}.
|
| 14 |
+
|
| 15 |
+
Rules for all generators:
|
| 16 |
+
- stdlib only (random, math, json, fractions, datetime)
|
| 17 |
+
- use the module-level `rng = random.Random()` seeded by generate(seed)
|
| 18 |
+
- closed-form exact q whenever the event is stochastic; soft q allowed for
|
| 19 |
+
judgment tasks; never leave q ambiguous without intent
|
| 20 |
+
- keep total request under ~400 tokens
|
| 21 |
+
- vary surface phrasing — several templates per family
|
| 22 |
+
"""
|
| 23 |
+
import json
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def emit(family, idx, state, qs):
|
| 27 |
+
questions, targets = {}, {}
|
| 28 |
+
for q in qs:
|
| 29 |
+
questions[q["qid"]] = {"type": q["qtype"],
|
| 30 |
+
"instructions": q["instructions"]}
|
| 31 |
+
if q["qtype"] in ("choice", "score"):
|
| 32 |
+
questions[q["qid"]]["criteria"] = {
|
| 33 |
+
str(j): o for j, o in enumerate(q["options"])}
|
| 34 |
+
if q["qtype"] == "regress" and q.get("unit"):
|
| 35 |
+
questions[q["qid"]]["unit"] = q["unit"]
|
| 36 |
+
targets[q["qid"]] = q["target"]
|
| 37 |
+
return {"id": f"{family}-{idx}", "family": family, "state": state,
|
| 38 |
+
"questions": questions, "targets": targets}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def q_choice(qid, instructions, options, q):
|
| 42 |
+
return {"qid": qid, "qtype": "choice", "instructions": instructions,
|
| 43 |
+
"options": options, "target": q}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def q_noul(qid, instructions, q):
|
| 47 |
+
return {"qid": qid, "qtype": "noul", "instructions": instructions,
|
| 48 |
+
"options": [], "target": float(q)}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def q_score(qid, instructions, levels, q):
|
| 52 |
+
return {"qid": qid, "qtype": "score", "instructions": instructions,
|
| 53 |
+
"options": levels, "target": q}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def q_regress(qid, instructions, quantiles19, unit=None):
|
| 57 |
+
"""quantiles19: 19 monotone values at taus .05,.10,...,.95."""
|
| 58 |
+
return {"qid": qid, "qtype": "regress", "instructions": instructions,
|
| 59 |
+
"options": [], "target": list(quantiles19), "unit": unit}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def validate_row(row):
|
| 63 |
+
for qid, spec in row["questions"].items():
|
| 64 |
+
t = row["targets"][qid]
|
| 65 |
+
if spec["type"] in ("choice", "score"):
|
| 66 |
+
n = len(spec["criteria"])
|
| 67 |
+
assert len(t) == n, f"{row['id']}:{qid} target len != options"
|
| 68 |
+
s = sum(t)
|
| 69 |
+
assert abs(s - 1.0) < 1e-3, f"{row['id']}:{qid} q sums to {s}"
|
| 70 |
+
assert all(0 <= x <= 1 for x in t)
|
| 71 |
+
elif spec["type"] == "regress":
|
| 72 |
+
assert len(t) == 19, f"{row['id']}:{qid} regress target needs 19 quantiles"
|
| 73 |
+
assert all(t[i] <= t[i + 1] + 1e-9 for i in range(18)), \
|
| 74 |
+
f"{row['id']}:{qid} quantiles not monotone"
|
| 75 |
+
else:
|
| 76 |
+
assert 0.0 <= t <= 1.0, f"{row['id']}:{qid} noul target {t}"
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def write_jsonl(rows, path):
|
| 80 |
+
with open(path, "w", encoding="utf-8") as fh:
|
| 81 |
+
for r in rows:
|
| 82 |
+
fh.write(json.dumps(r, ensure_ascii=False) + "\n")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def generate(families, weights, n, seed, rng):
|
| 86 |
+
"""Sample n rows. `rng` must be the module's own Random so seeding works."""
|
| 87 |
+
rng.seed(seed)
|
| 88 |
+
fams, ws = zip(*weights.items())
|
| 89 |
+
rows, i = [], 0
|
| 90 |
+
while len(rows) < n:
|
| 91 |
+
f = rng.choices(fams, ws)[0]
|
| 92 |
+
try:
|
| 93 |
+
state, qs = families[f]()
|
| 94 |
+
except Exception:
|
| 95 |
+
continue
|
| 96 |
+
row = emit(f, i, state, qs)
|
| 97 |
+
try:
|
| 98 |
+
validate_row(row)
|
| 99 |
+
except AssertionError:
|
| 100 |
+
continue
|
| 101 |
+
rows.append(row)
|
| 102 |
+
i += 1
|
| 103 |
+
return rows
|