BHI layer v1: docs, schema, Phase A ingestion stubs

This commit is contained in:
BHI Staging Agent
2026-04-05 20:15:36 +00:00
commit 3dfd9ea3c6
21 changed files with 2399 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
#!/usr/bin/env python3
# READY TO DEPLOY — requires base Brain Postgres schema + run schemas/bhi_tables.sql
"""
CMS Hospital General Information (Care Compare) — used to cross-reference
which acute hospitals host behavioral health units and to capture CCN-level
facility metadata.
Source: https://data.cms.gov/provider-data/api/1/datastore/query/xubh-q36u/0
"""
import logging
import sys
from _common import RateLimitedSession, bulk_insert, job_run
LOG = logging.getLogger("bhi.cms_hospital_compare")
BASE = "https://data.cms.gov/provider-data/api/1/datastore/query/xubh-q36u/0"
PAGE = 500
def test_endpoint():
s = RateLimitedSession()
r = s.get(BASE, params={"limit": 2}).json()
rows = r.get("results", [])
print(f"OK: {len(rows)} rows, sample:", rows[0].get("facility_name") if rows else None)
return bool(rows)
def fetch_rows():
s = RateLimitedSession(min_interval=0.25)
offset, out = 0, []
while True:
b = s.get(BASE, params={"limit": PAGE, "offset": offset}).json().get("results", [])
if not b:
break
out.extend(b)
if len(b) < PAGE:
break
offset += PAGE
LOG.info("fetched %d hospitals", len(out))
return out
def write_rows(conn, raw):
cols = [
"ccn","npi","name","address","city","state","zip","county_fips",
"lat","lon","facility_type","ownership","bed_count","psych_bed_count",
"pediatric_psych_bed_count","adolescent_unit","young_adult_unit",
"services_offered","populations_served","payment_accepted",
"medicaid_accepted","accreditation","opened_date","closed_date",
"last_verified","source","source_raw_id",
]
rows = []
for r in raw:
rows.append((
r.get("facility_id"), None,
r.get("facility_name"), r.get("address"),
r.get("citytown"), r.get("state"), r.get("zip_code"), None,
None, None,
(r.get("hospital_type") or "hospital"),
r.get("hospital_ownership"),
None, None, None, None, None,
[], [], [], None, None, None, None, None,
"cms_hospital_compare", None,
))
bulk_insert(conn, "bhi_facilities", cols, rows)
return len(rows)
def main():
with job_run("bhi_cms_hospital_compare") as (conn, _):
n = write_rows(conn, fetch_rows())
LOG.info("inserted %d", n)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "test":
sys.exit(0 if test_endpoint() else 1)
main()