Alexander Wirth commited on
Delete generate_samples_v23.py
Browse files- generate_samples_v23.py +0 -90
generate_samples_v23.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
generate_samples_v23.py
|
| 4 |
-
Generates ethno_sample_400.json and ethno_sample_400.parquet
|
| 5 |
-
from the v2.3 master dataset. Guarantees 100% schema integrity.
|
| 6 |
-
"""
|
| 7 |
-
import json
|
| 8 |
-
import pathlib
|
| 9 |
-
|
| 10 |
-
MASTER = pathlib.Path("/opt/ethno-enrichment/delivery/ethno_dataset_2026_v2.3.json")
|
| 11 |
-
OUT_DIR = pathlib.Path(__file__).parent
|
| 12 |
-
SAMPLE_SIZE = 400
|
| 13 |
-
|
| 14 |
-
# Canonical field order (matches MANIFEST_v2.json)
|
| 15 |
-
FIELD_ORDER = [
|
| 16 |
-
"chemical",
|
| 17 |
-
"plant_species",
|
| 18 |
-
"application",
|
| 19 |
-
"dosage",
|
| 20 |
-
"pubmed_mentions_2026",
|
| 21 |
-
"clinical_trials_count_2026",
|
| 22 |
-
"chembl_bioactivity_count",
|
| 23 |
-
"patent_count_since_2020",
|
| 24 |
-
"pubchem_cid",
|
| 25 |
-
"canonical_smiles",
|
| 26 |
-
]
|
| 27 |
-
|
| 28 |
-
print(f"Loading master: {MASTER}")
|
| 29 |
-
with open(MASTER) as f:
|
| 30 |
-
master = json.load(f)
|
| 31 |
-
|
| 32 |
-
print(f"Master records: {len(master):,}")
|
| 33 |
-
assert len(master) == 76_907, f"Expected 76,907, got {len(master)}"
|
| 34 |
-
|
| 35 |
-
# Extract first 400, normalize field order and types
|
| 36 |
-
sample = []
|
| 37 |
-
for rec in master[:SAMPLE_SIZE]:
|
| 38 |
-
row = {}
|
| 39 |
-
for field in FIELD_ORDER:
|
| 40 |
-
val = rec.get(field)
|
| 41 |
-
# pubchem_cid: convert float → int (e.g. 119250.0 → 119250)
|
| 42 |
-
if field == "pubchem_cid" and isinstance(val, float):
|
| 43 |
-
val = int(val)
|
| 44 |
-
row[field] = val
|
| 45 |
-
sample.append(row)
|
| 46 |
-
|
| 47 |
-
assert len(sample) == SAMPLE_SIZE, f"Expected {SAMPLE_SIZE}, got {len(sample)}"
|
| 48 |
-
assert list(sample[0].keys()) == FIELD_ORDER, "Field order mismatch"
|
| 49 |
-
|
| 50 |
-
# Write JSON
|
| 51 |
-
json_path = OUT_DIR / "ethno_sample_400.json"
|
| 52 |
-
with open(json_path, "w") as f:
|
| 53 |
-
json.dump(sample, f, indent=2, ensure_ascii=False)
|
| 54 |
-
print(f"Wrote {json_path} ({json_path.stat().st_size:,} bytes)")
|
| 55 |
-
|
| 56 |
-
# Write Parquet
|
| 57 |
-
try:
|
| 58 |
-
import pandas as pd
|
| 59 |
-
df = pd.DataFrame(sample)
|
| 60 |
-
parquet_path = OUT_DIR / "ethno_sample_400.parquet"
|
| 61 |
-
df.to_parquet(parquet_path, index=False, engine="pyarrow")
|
| 62 |
-
print(f"Wrote {parquet_path} ({parquet_path.stat().st_size:,} bytes)")
|
| 63 |
-
except ImportError:
|
| 64 |
-
print("WARNING: pandas/pyarrow not available, skipping parquet")
|
| 65 |
-
|
| 66 |
-
# Validation
|
| 67 |
-
print("\n--- Validation ---")
|
| 68 |
-
with open(json_path) as f:
|
| 69 |
-
check = json.load(f)
|
| 70 |
-
print(f"Records: {len(check)}")
|
| 71 |
-
print(f"Fields: {list(check[0].keys())}")
|
| 72 |
-
print(f"Field count: {len(check[0])}")
|
| 73 |
-
|
| 74 |
-
# Null counts in sample
|
| 75 |
-
from collections import Counter
|
| 76 |
-
nulls = Counter()
|
| 77 |
-
for r in check:
|
| 78 |
-
for k, v in r.items():
|
| 79 |
-
if v is None:
|
| 80 |
-
nulls[k] += 1
|
| 81 |
-
print(f"Null counts: {dict(nulls)}")
|
| 82 |
-
|
| 83 |
-
# Type check pubchem_cid
|
| 84 |
-
cid_types = set()
|
| 85 |
-
for r in check:
|
| 86 |
-
if r["pubchem_cid"] is not None:
|
| 87 |
-
cid_types.add(type(r["pubchem_cid"]).__name__)
|
| 88 |
-
print(f"pubchem_cid types: {cid_types}")
|
| 89 |
-
|
| 90 |
-
print("\n✓ Sample generation complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|