kozo2's picture
Upload bootstrap.py with huggingface_hub
4b40eb9 verified
Raw History Blame Contribute Delete
2.52 kB
"""Rebuild the DuckDB database the pipeline scripts expect, from released files.
DATA_DIR=. uv run python bootstrap.py
Creates graph.duckdb in DATA_DIR with three tables:
edge_ML_expected_ge5 the 2,709,209 filtered edges (from the Parquet file)
nodes_ML the 118,422 node property rows (from the TSV)
graph_nodes the 18,494 graph nodes with their contiguous indices
The released TSV is already UTF-8: the mixed-encoding repair described in the
dataset card was applied before upload, so no fallback decoding is needed here.
See provenance/import_nodes.py for the code that performed it.
"""
import os
import pathlib
import duckdb
DATA_DIR = pathlib.Path(os.environ.get(
"DATA_DIR", str(pathlib.Path(__file__).resolve().parent)))
DB = DATA_DIR / "graph.duckdb"
PARQUET = DATA_DIR / "data" / "edge_ML_expected_ge5.parquet"
NODES = DATA_DIR / "data" / "nodes_ML_utf8.tsv"
NODE_COLUMNS = ["id", "factor", "Study", "Analytics", "factor1", "factor2",
"factor1_org", "factor2_org", "species", "color_group",
"color_group_upper1", "color_group_upper2"]
for path in (PARQUET, NODES):
if not path.exists():
raise SystemExit(f"missing {path} — download the data/ directory first")
con = duckdb.connect(str(DB))
cols = "{" + ", ".join(f"'{c}': 'VARCHAR'" for c in NODE_COLUMNS) + "}"
con.execute(f"""
CREATE OR REPLACE TABLE edge_ML_expected_ge5 AS
SELECT * FROM '{PARQUET}'
""")
con.execute(f"""
CREATE OR REPLACE TABLE nodes_ML AS
SELECT * FROM read_csv('{NODES}', delim = '\t', header = false,
quote = '', escape = '', columns = {cols})
""")
# Lexicographic order over the union of both endpoint columns. This is the same
# ordering the released graph's node_id list uses, so indices line up.
con.execute("""
CREATE OR REPLACE TABLE graph_nodes AS
SELECT id, (row_number() OVER (ORDER BY id) - 1)::BIGINT AS idx
FROM (
SELECT DISTINCT id FROM (
SELECT start_id AS id FROM edge_ML_expected_ge5
UNION ALL
SELECT end_id FROM edge_ML_expected_ge5
)
)
""")
for table, expected in (("edge_ML_expected_ge5", 2_709_209),
("nodes_ML", 118_422),
("graph_nodes", 18_494)):
n = con.execute(f"SELECT count(*) FROM {table}").fetchone()[0]
flag = "ok" if n == expected else f"MISMATCH, expected {expected:,}"
print(f" {table:<22} {n:>9,} {flag}")
print(f"\nwrote {DB}")