from __future__ import annotations import json import sys from pathlib import Path from openpyxl import load_workbook HERE = Path(__file__).resolve().parent ROOT = HERE.parents[3] sys.path.insert(0, str(HERE.parents[1])) from charting import save_chart YEARS = list(range(2015, 2025)) REGIONS = ["Africa", "Asia", "Europe", "North America", "Oceania", "South America"] COUNTRIES = ["Mexico", "India", "Afghanistan", "Cuba", "China, People's Republic", "Venezuela"] STATES = ["Florida", "California", "Texas", "New York", "New Jersey", "Illinois"] PALETTE = ("#fbf7ef", "#142334", "#21615e", "#9c3930", "#2b527f", "#5d7831") def series(rows, key): row = next(r for r in rows if r[0] == key) return list(map(int, row[1:11])) def main(): index = ROOT / "research/launch_payloads_2026-12-01.json" source = next(row for row in json.loads(index.read_text(encoding="utf-8"))["items"] if row["id"] == "launch_dhs_lpr_2024_yearbook") wb = load_workbook(ROOT / source["local_path"], data_only=True, read_only=True) try: tables = {i: list(wb[f"Table {i}"].values) for i in (2, 3, 4, 6)} finally: wb.close() reported = series(tables[2], "Total") assert all(series(tables[i], "Total") == reported for i in (3, 4, 6)) unknown = {name: series(tables[table], "Unknown") for name, table in (("Last foreign residence", 2), ("Birthplace", 3), ("U.S. state or territory", 4))} last = {key: series(tables[2], key) for key in REGIONS} birth = {key: series(tables[3], key) for key in REGIONS} keys = [str(r[0]) for r in tables[2] if r[0]] countries = [key for key in COUNTRIES if key in keys] if len(countries) < 5: alternatives = [key for key in keys if key.startswith("China") and key in {r[0] for r in tables[3]}] countries.extend(alternatives[:1]) country_last = {key: series(tables[2], key) for key in countries} country_birth = {key: series(tables[3], key) for key in countries} state = {key: series(tables[4], key) for key in STATES} assert tables[6][33][0] == "ADJUSTMENTS OF STATUS" and tables[6][60][0] == "NEW ARRIVALS" adjustment = list(map(int, tables[6][34][1:11])) new_arrivals = list(map(int, tables[6][61][1:11])) known = [value - missing for value, missing in zip(reported, unknown["Last foreign residence"], strict=True)] annual_deltas = [reported[-1] - reported[-2], known[-1] - known[-2], unknown["Last foreign residence"][-1] - unknown["Last foreign residence"][-2]] assert annual_deltas[0] == annual_deltas[1] + annual_deltas[2] plan = [] def figure(tag, title, question, unit, labels, values, summary, kind="bar"): name = f"chart-{len(plan) + 1:02d}-{tag}" save_chart(HERE / "charts", name, title=title, unit=unit, labels=labels, series=values, source="DHS OHSS Yearbook, fiscal year 2024, Tables 2, 3, 4 and 6", palette=PALETTE, kind=kind) plan.append({"asset": f"charts/{name}.png", "data": f"charts/{name}.csv", "question": question, "source": source["source_url"], "population": "People obtaining U.S. lawful permanent resident status", "period": "Fiscal years 2015–2024, as shown", "unit": unit, "takeaway": summary}) figure("missing-fields", "Last foreign residence became harder to identify", "Which geography fields lost completeness?", "Share coded unknown (%)", YEARS, {k: [100 * a / b for a, b in zip(v, reported)] for k, v in unknown.items()}, "Birthplace and U.S. state fields remained far more complete than last foreign residence.", "line") figure("net-change", "The unknown group grew more than the national total", "How did records with and without known last residence contribute to the total change?", "Change in thousands of people, FY2023 to FY2024", ["All records", "Known last residence", "Unknown last residence"], {"Change": [n / 1000 for n in annual_deltas]}, "The published known count fell while the unknown count rose.") figure("region-fields", "The same national cohort gives two regional totals", "How do the birthplace and last-residence rows differ?", "Thousands of people, FY2024", REGIONS, {"Last residence": [last[k][-1] / 1000 for k in REGIONS], "Birthplace": [birth[k][-1] / 1000 for k in REGIONS]}, "A cross-field gap does not identify a personal migration route.") figure("asia-history", "Asia’s two field series diverged in 2024", "How do figures for Asia read across the full file?", "Thousands of people", YEARS, {"Last residence": [x / 1000 for x in last["Asia"]], "Birthplace": [x / 1000 for x in birth["Asia"]]}, "The reported birthplace series rose in 2024 while the reported last-residence series fell.", "line") figure("lower-upper", "Unassigned rows leave wide logical bounds", "How much could the last-residence regional comparison move?", "Thousands of people in each scenario, FY2024", ["Asia", "Europe", "North America"], {"Published region count": [last[k][-1] / 1000 for k in ("Asia", "Europe", "North America")], "If every unknown belonged to the region": [(last[k][-1] + unknown["Last foreign residence"][-1]) / 1000 for k in ("Asia", "Europe", "North America")]}, "The upper values are separate accounting scenarios and give no estimate of the actual allocation.") figure("country-fields", "Country-level movements also depend on the field", "How did the two country fields change at selected markets?", "Change in thousands, FY2023 to FY2024", countries, {"Last residence": [(country_last[k][-1] - country_last[k][-2]) / 1000 for k in countries], "Birthplace": [(country_birth[k][-1] - country_birth[k][-2]) / 1000 for k in countries]}, "Country-of-birth and last-residence figures answer distinct questions and can point in opposite directions.") figure("destination", "U.S. destination states are a separate geography", "What can the more complete U.S. residence series describe?", "Thousands of people", STATES, {"FY2023": [state[k][-2] / 1000 for k in STATES], "FY2024": [state[k][-1] / 1000 for k in STATES]}, "State of residence at reporting offers a different geographic view from prior foreign residence.") figure("admission-type", "Adjustments accounted for most of the national increase", "How did the two admission pathways change overall?", "Thousands of people", YEARS, {"Adjustments": [x / 1000 for x in adjustment], "New arrivals": [x / 1000 for x in new_arrivals]}, "The pathway margins are available, but they do not locate the missing foreign-residence rows.", "line") facts = {"archive": {k: source.get(k) for k in ("source_url", "local_path", "sha256")}, "years": YEARS, "total": reported, "known": known, "unknown": unknown, "last_region": last, "birth_region": birth, "last_country": country_last, "birth_country": country_birth, "state": state, "adjustments": adjustment, "new_arrivals": new_arrivals, "deltas": annual_deltas} for key, record in (("evidence.json", facts), ("CHART-PLAN.json", plan)): (HERE / key).write_text(json.dumps(record, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") if __name__ == "__main__": main()