joyboseroy's picture
Upload 7 files
9bd31b0 verified
Raw History Blame Contribute Delete
4.99 kB
"""Build the Indus Sequences + Null Baselines dataset from the indus_decipher repo.
Real corpora are read from the repo's CSVs. Null corpora are generated with the repo's own
null models, fitted ONLY on the train half of each real corpus, so test inscriptions never
leak into the nulls.
Usage: python build.py --repo /path/to/indus_decipher --out data
"""
import argparse
import hashlib
import os
import random
import sys
import pyarrow as pa
import pyarrow.parquet as pq
SOURCES = {
"cisi": ("data/cisi_real_corpus.csv", "MIT", "mayig/indus-valley-script-corpus (digitisation of CISI, Parpola et al.)"),
"indus_website": ("data/indus_website_real_corpus.csv", "GPL-3.0 (source repo)", "yajnadevam/indus-website SQL dump"),
}
SEEDS = [0, 1, 2]
def split_of(signs):
"""Deterministic 50/50 split grouped by sequence content, so duplicate inscriptions stay together."""
h = hashlib.sha256(" ".join(signs).encode()).hexdigest()
return "train" if int(h[:8], 16) % 2 == 0 else "test"
def real_rows(corpus_name, corpus):
rows = []
for ins in corpus.inscriptions:
canon = ins.normalized_signs()
rows.append(
{
"inscription_id": ins.inscription_id,
"corpus": corpus_name,
"signs": canon,
"length": len(canon),
"site": ins.site,
"object_type": ins.object_type,
"line_count": ins.line_count,
"damaged": bool(ins.damaged),
"reading_direction_stored": ins.reading_direction,
"motif": ins.motif,
"mean_uncertainty": float(ins.mean_uncertainty),
"split": split_of(canon),
}
)
return rows
def null_rows(corpus_name, null_name, seed, corpus):
rows = []
for i, ins in enumerate(corpus.inscriptions):
canon = ins.normalized_signs()
rows.append(
{
"null_id": f"{corpus_name}-{null_name}-s{seed}-{i}",
"corpus": corpus_name,
"null_model": null_name,
"seed": seed,
"signs": canon,
"length": len(canon),
}
)
return rows
def main(repo, out):
sys.path.insert(0, repo)
from data.loader import Corpus, load_corpus_csv
from data.adversarial_null_model import generate_matched_null_corpus
from data.permutation_nulls import (
bigram_markov_null,
global_shuffle,
position_preserving_shuffle,
trigram_markov_null,
within_inscription_shuffle,
)
from data.stratified_null_model import motif_stratified_null, site_stratified_null
os.makedirs(out, exist_ok=True)
real_all, null_all, notes = [], [], []
for name, (path, _lic, _src) in SOURCES.items():
corpus = load_corpus_csv(os.path.join(repo, path))
rows = real_rows(name, corpus)
real_all += rows
train_ids = {r["inscription_id"] for r in rows if r["split"] == "train" and not r["damaged"]}
train = Corpus([i for i in corpus.inscriptions if i.inscription_id in train_ids])
n = len(train)
print(name, "real", len(rows), "train(non-damaged)", n)
fns = {
"within_inscription_shuffle": lambda c, s: within_inscription_shuffle(c, seed=s),
"global_shuffle": lambda c, s: global_shuffle(c, seed=s),
"position_preserving_shuffle": lambda c, s: position_preserving_shuffle(c, seed=s),
"bigram_markov": lambda c, s: bigram_markov_null(c, n, seed=s),
"trigram_markov": lambda c, s: trigram_markov_null(c, n, seed=s),
"unigram_matched": lambda c, s: generate_matched_null_corpus(c, n, seed=s),
"motif_stratified": lambda c, s: motif_stratified_null(c, n, seed=s),
"site_stratified": lambda c, s: site_stratified_null(c, n, seed=s),
}
for null_name, fn in fns.items():
ok = True
for s in SEEDS:
try:
nc = fn(train, s)
except Exception as e: # some strata are too small in small corpora
notes.append(f"{name}/{null_name}: skipped ({type(e).__name__}: {e})")
ok = False
break
null_all += null_rows(name, null_name, s, nc)
if ok:
print(" null", null_name, "ok")
pq.write_table(pa.Table.from_pylist(real_all), os.path.join(out, "real.parquet"), compression="zstd")
pq.write_table(pa.Table.from_pylist(null_all), os.path.join(out, "nulls.parquet"), compression="zstd")
print("real rows", len(real_all), "null rows", len(null_all))
for n_ in notes:
print("NOTE", n_)
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--repo", required=True)
ap.add_argument("--out", default="data")
a = ap.parse_args()
random.seed(0)
main(a.repo, a.out)