Datasets:
Download src/validate.py from skyleee/diversa-biomedical-v1: direct link, hf CLI and curl.
- Browser
- Download file 5.06 kB
-
https://huggingface.co/datasets/skyleee/diversa-biomedical-v1/resolve/main/src/validate.py
- Command line
-
hf download hf://datasets/skyleee/diversa-biomedical-v1/src/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/skyleee/diversa-biomedical-v1/resolve/main/src/validate.py
5.06 kB
| """Validate the produced Parquet tables against the canonical schemas.""" | |
| from __future__ import annotations | |
| import pyarrow as pa | |
| import pyarrow.compute as pc | |
| import pyarrow.parquet as pq | |
| from common import ensure_dirs, get_logger, load_config, p, utc_now, write_json | |
| from schema import ( | |
| AUTHORS_DIM, CITATION_EDGES, COUNTRY_COLLABORATION, COUNTRY_YEAR_COUNTS, | |
| INSTITUTIONS_DIM, INSTITUTION_ACTIVITY, SOURCES_DIM, TOPIC_GROWTH, | |
| TOPIC_YEAR_COUNTS, TOPICS_DIM, WORK_AUTHORS, WORK_INSTITUTIONS, WORK_TOPICS, | |
| WORKS_FULL, | |
| ) | |
| # rel -> (schema, required non-null columns, primary-key column or None) | |
| EXPECTED = { | |
| "data/works.parquet": (WORKS_FULL, ["work_id", "title", "publication_year"], "work_id"), | |
| "data/work_authors.parquet": (WORK_AUTHORS, ["work_id"], None), | |
| "data/work_institutions.parquet": (WORK_INSTITUTIONS, ["work_id", "institution_id"], None), | |
| "data/work_topics.parquet": (WORK_TOPICS, ["work_id", "topic_id"], None), | |
| "data/authors.parquet": (AUTHORS_DIM, ["author_id"], "author_id"), | |
| "data/institutions.parquet": (INSTITUTIONS_DIM, ["institution_id"], "institution_id"), | |
| "data/topics.parquet": (TOPICS_DIM, ["topic_id"], "topic_id"), | |
| "data/sources.parquet": (SOURCES_DIM, ["source_id"], "source_id"), | |
| "data/citation_edges.parquet": (CITATION_EDGES, ["citing_work_id", "cited_work_id"], None), | |
| "analytics/topic_year_counts.parquet": (TOPIC_YEAR_COUNTS, ["topic_id", "publication_year"], None), | |
| "analytics/topic_growth.parquet": (TOPIC_GROWTH, ["topic_id"], "topic_id"), | |
| "analytics/country_year_counts.parquet": (COUNTRY_YEAR_COUNTS, ["country_code", "publication_year"], None), | |
| "analytics/country_collaboration.parquet": (COUNTRY_COLLABORATION, ["country_a", "country_b"], None), | |
| "analytics/institution_activity.parquet": (INSTITUTION_ACTIVITY, ["institution_id", "publication_year"], None), | |
| } | |
| def run(cfg: dict | None = None) -> dict: | |
| cfg = cfg or load_config() | |
| ensure_dirs(cfg) | |
| logger = get_logger("diversa.validate", p(cfg, "logs") / "pipeline.log") | |
| root = p(cfg, "data").parent | |
| results = {"checked_at": utc_now(), "tables": {}, "errors": [], "warnings": []} | |
| for rel, (schema, required, pk) in EXPECTED.items(): | |
| path = root / rel | |
| entry = {"path": rel} | |
| if not path.exists(): | |
| entry["status"] = "missing" | |
| results["errors"].append(f"{rel} missing") | |
| results["tables"][rel] = entry | |
| continue | |
| pf = pq.ParquetFile(path) | |
| arrow = pf.schema_arrow | |
| names = arrow.names | |
| missing_cols = [c for c in schema.names if c not in names] | |
| extra_cols = [c for c in names if c not in schema.names] | |
| type_mismatch = [] | |
| for f in schema: | |
| if f.name in names: | |
| actual = arrow.field(f.name).type | |
| if actual != f.type and str(actual).replace("element", "item") != str(f.type): | |
| type_mismatch.append(f"{f.name}: {actual} != {f.type}") | |
| entry.update({"rows": pf.metadata.num_rows, "columns": len(names), | |
| "missing_columns": missing_cols, "extra_columns": extra_cols, | |
| "type_mismatch": type_mismatch}) | |
| if missing_cols: | |
| results["errors"].append(f"{rel} missing columns {missing_cols}") | |
| if type_mismatch: | |
| results["errors"].append(f"{rel} type mismatch {type_mismatch}") | |
| if extra_cols: | |
| results["warnings"].append(f"{rel} extra columns {extra_cols}") | |
| cols = [c for c in required if c in names] | |
| if cols: | |
| tbl = pq.read_table(path, columns=cols) | |
| for c in cols: | |
| nn = tbl.column(c).null_count | |
| if nn: | |
| results["errors"].append(f"{rel}:{c} has {nn} nulls") | |
| list_cols = [f.name for f in schema if pa.types.is_list(f.type) and f.name in names] | |
| for c in list_cols: | |
| flat = pc.list_flatten(pq.read_table(path, columns=[c]).column(c)) | |
| if flat.null_count: | |
| results["errors"].append( | |
| f"{rel}:{c} has {flat.null_count} null elements inside lists") | |
| if pk and pk in names: | |
| vals = pq.read_table(path, columns=[pk]).column(pk).to_pylist() | |
| dup = len(vals) - len(set(vals)) | |
| entry["duplicate_pk"] = dup | |
| if dup: | |
| results["errors"].append(f"{rel} has {dup} duplicate {pk}") | |
| entry["status"] = "ok" if not missing_cols and not type_mismatch else "schema_error" | |
| results["tables"][rel] = entry | |
| logger.info("validated %-45s rows=%-9s status=%s", | |
| rel, f"{entry.get('rows', 0):,}", entry["status"]) | |
| results["status"] = "pass" if not results["errors"] else "fail" | |
| write_json(p(cfg, "metadata") / "validation_report.json", results) | |
| logger.info("validation %s (%d errors, %d warnings)", | |
| results["status"], len(results["errors"]), len(results["warnings"])) | |
| return results | |
| if __name__ == "__main__": | |
| run() | |