models train: train a model from the command line

This commit is contained in:
2026-11-04 23:36:46 -06:00
parent 37fb292a4f
commit 0fdab3651c
5 changed files with 130 additions and 26 deletions
+30
View File
@@ -4,11 +4,13 @@ Long format: one row per member and event, with the member's index date on every
frame and computes its own outcome and predictors from it; it never reads the database.
"""
import os
from pathlib import Path
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from sqlalchemy.engine import make_url
SCHEMA = pa.schema([
("pac_id", pa.int64()), ("fecha_indice", pa.date32()), ("fecha", pa.date32()),
@@ -22,6 +24,34 @@ class CohortFileError(ValueError):
"""The file isn't a cohort build: its columns or types aren't `SCHEMA`'s."""
def cohorts_root(database_url: str) -> Path:
"""`COHORTS_DIR`, or `cohorts/` next to the domain's SQLite file. Other engines have no such folder."""
if configured := os.environ.get("COHORTS_DIR"):
return Path(configured)
url = make_url(database_url)
if url.get_backend_name() != "sqlite" or not url.database:
raise ValueError("this database has no data folder: set COHORTS_DIR")
return Path(url.database.removeprefix("file:")).parent / "cohorts"
def cohort_path(root: Path, url: str, armado_id: int) -> Path:
domain, cohort = url.split("/", 1)
return root / domain / cohort / f"{armado_id}.parquet"
def find_cohort_file(root: Path, url: str, armado_id: int | None = None) -> Path:
"""The file of that build, or of the cohort's latest build that has a file."""
if armado_id is None:
builds = [int(file.stem) for file in cohort_path(root, url, 0).parent.glob("*.parquet") if file.stem.isdigit()]
if not builds:
raise FileNotFoundError(f"no exported builds of {url} under {root}")
armado_id = max(builds)
path = cohort_path(root, url, armado_id)
if not path.exists():
raise FileNotFoundError(f"build {armado_id} of {url} isn't exported: no {path}")
return path
def cohort_lineage(path: str | Path) -> dict[str, str]:
"""The lineage in the file's metadata, without reading any rows."""
metadata = pq.read_metadata(path).metadata