Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/us-post-calibration-pass-reuse.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The US fiscal-refresh release tool no longer reruns policyengine-us over every household for the post-calibration December SSI candidate amounts and Medicaid eligibility. Each pre-calibration pass keeps its per-person values and a digest of each household's rows: IDs, membership, row order, and every column the engine can read for `uncapped_ssi` or `is_medicaid_eligible`. That is the engine's static dependency graph, plus the variables named in strings in a listed formula's source, constants or closure (branch simulations, `get_holder` reads, `all_of_variables` lists and generated state flags) and the state flags that programs are `defined_for`. After calibration, households whose digest is unchanged take their earlier values, matched by person ID. The tool runs policyengine-us on the households whose inputs changed, one batch at a time, and on one batch of unchanged households spread over the frame as a check; if that check disagrees, it runs every household again. The counts are printed and sent with the build's telemetry. The Medicaid reuse runs every household when a positive `medicaid_claiming_tax_unit_id` points outside its own household, and an unbatched build reruns in one selection. Neither variable reads weights or a population-wide sum, so calibrated weights and L0's dropped households change no remaining value. No column written between the two SSI passes is an SSI input. Between the Medicaid passes, SNAP take-up and the other-health-insurance premium attribution change some households' inputs, and those households are rerun. Reruns of selected households switch the population-aggregate refusal on, and `household_batches.one_pass_garbage_collection` gives a reuse's rerun batches one full collection, as a full pass has. The new country-neutral helper is `microcosm.build.household_pass_reuse`; `microcosm.build.us_runtime.household_pass_inputs` lists a policyengine-us pass's input columns.
535 changes: 535 additions & 0 deletions packages/microcosm-build/src/microcosm/build/household_pass_reuse.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
:func:`collect_pass_garbage` are the garbage-collection pattern of a batch
loop: no automatic collection inside a batch, a youngest-generation
collection after it, and one full collection when the pass ends.
:func:`one_pass_garbage_collection` runs several calls of a pass function
as one pass, with one full collection at the end.

A population-aggregate variable is a policyengine-us formula that sums or ranks
over every person or household in its engine
Expand Down Expand Up @@ -66,6 +68,7 @@
"group_to_household_positions",
"household_batch_plan",
"household_position_batches",
"one_pass_garbage_collection",
"record_engine_branches",
"refuse_batch_population_aggregates",
"select_households_by_position",
Expand Down Expand Up @@ -375,12 +378,42 @@ def collect_pass_garbage() -> None:
CPython's own full collections are throttled against the build's multi-GB
long-lived heap, so without an explicit sweep the skeletons accumulate
for the run's lifetime. One full collection per pass (a target family,
an input stage) is cheap next to the pass's engine work.
an input stage) is cheap next to the pass's engine work. Inside
:func:`one_pass_garbage_collection` it leaves the collection to the end.
"""

if _PASS_GARBAGE_DEFERRED:
return
gc.collect()


#: While set, :func:`collect_pass_garbage` leaves the full collection to
#: :func:`one_pass_garbage_collection`.
_PASS_GARBAGE_DEFERRED = False


@contextmanager
def one_pass_garbage_collection() -> Iterator[None]:
"""Run the pass calls inside as one pass: one full collection at the end.

A reuse of an earlier pass (``microcosm.build.household_pass_reuse``)
calls the pass function once per batch of households it reruns, and each
call ends with :func:`collect_pass_garbage`; a full pass collects once.
Nested uses collect once, when the outermost ends, also after an
exception.
"""

global _PASS_GARBAGE_DEFERRED
already_deferred = _PASS_GARBAGE_DEFERRED
_PASS_GARBAGE_DEFERRED = True
try:
yield
finally:
_PASS_GARBAGE_DEFERRED = already_deferred
if not already_deferred:
gc.collect()


@contextmanager
def automatic_gc_suspended() -> Iterator[None]:
"""Avoid surprise full-graph cyclic GC inside memory-heavy PE batches."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
"""The columns a household-batched policyengine-us pass can read.

``microcosm.build.household_pass_reuse`` reuses a pass's values for
households whose input columns are unchanged. For policyengine-us those
columns are every variable the pass's variable can read, the variable itself
included, because a stored column replaces a formula. The list starts from
the static dependency graph
(``PolicyEngineUSVariableMetadataIndex.variable_dependency_closure``: every
variable its formulas read through ``entity(...)``, ``add`` and similar
calls, on any branch and in any period). That graph does not follow some
reads, so the list adds, until nothing new appears:

* every string in a listed variable's class source, or among its formula
functions' constants and closure values, that names an engine variable,
with each such variable's own graph. That covers
``simulation.get_branch(...).calculate("...")``,
``simulation.get_holder("...")``, ``all_of_variables([...])`` and formulas
generated at import without a class source (the state flags such as ``CA``
read ``state_code_str`` this way);
* each listed variable's ``defined_for`` variable (state programs name their
state flag as a ``StateCode`` member, not a string).

A string that names a variable without reading it only adds a column to the
digest, which can rerun a household but never reuses a wrong value.

Reads inside module-level helper functions are not followed: in
policyengine-us 2.2.1 the only variables those helpers read outside the list
are behavioral-response measurements (``household_net_income``,
``marginal_tax_rate`` and similar), which a simulation without a reform does
not compute. ``test_us_household_pass_inputs.py`` pins that set, so an engine
whose helpers read anything else fails until it is reviewed.
"""

from __future__ import annotations

import ast
import enum
import inspect
import textwrap
import types
from functools import cache

from microcosm.build.household_pass_reuse import HouseholdPassInputs, PassInputColumn
from microcosm.frame.adapters.policyengine_us import PolicyEngineUSVariableMetadataIndex

__all__ = ["us_household_pass_inputs"]


def _stored_default(definition) -> object:
"""The default as a dataset stores it: Enum members by name."""

default = definition.default_value
if isinstance(default, enum.Enum):
return default.name
return default


def _absent_reads_default(definition) -> bool:
"""Whether a stored default gives the engine the same input as no column.

Not for a variable the engine computes (a formula, or an ``adds`` or
``subtracts`` sum): a stored value replaces it. Not for a nonzero numeric
default either: the engine uprates a stored value it reads at a later
period and divides a yearly value into months, while an absent column
reads the default unchanged.
"""

computed = (
bool(definition.formulas)
or bool(getattr(definition, "adds", None))
or bool(getattr(definition, "subtracts", None))
)
if computed:
return False
if definition.value_type in (int, float):
return definition.default_value == 0
return True


def us_household_pass_inputs(
variable: str,
*,
metadata_index: PolicyEngineUSVariableMetadataIndex,
system,
) -> HouseholdPassInputs:
"""Every engine variable ``variable``'s pass can read, with its table.

``system`` is the installed policyengine-us tax-benefit system; it gives
each variable's entity, default and whether it has a formula. Graph nodes
the system does not define (a few parameter paths the static index reads
as names) are left out, because the engine reads no column by those names.
"""

closure = metadata_index.variable_dependency_closure(variable)
names = _readable_variables(variable, metadata_index=metadata_index, system=system)
columns = tuple(
PassInputColumn(
name=name,
entity=system.variables[name].entity.key,
default=_stored_default(system.variables[name]),
absent_reads_default=_absent_reads_default(system.variables[name]),
)
for name in names
)
return HouseholdPassInputs(
variable=variable,
columns=columns,
description=(
f"policyengine-us {closure.engine_version}: {len(columns)} variables "
f"{variable} can read (dependency graph sha256 {closure.sha256}, "
"with string and defined_for reads)"
),
)


def _graph_variables(
name: str, *, metadata_index: PolicyEngineUSVariableMetadataIndex, system
) -> set[str]:
try:
closure = metadata_index.variable_dependency_closure(name)
except ValueError:
# A variable the static index does not define (one generated at
# import) has no graph to add; its own name is still listed.
return {name}
return {
node
for node in (*closure.input_leaves, *closure.formula_nodes)
if node in system.variables
}


@cache
def _class_string_constants(variable_class: type) -> frozenset[str]:
try:
source = inspect.getsource(variable_class)
except (OSError, TypeError):
# A class generated at import has no class source; its formula
# functions' constants are read in _formula_strings.
return frozenset()
return frozenset(
node.value
for node in ast.walk(ast.parse(textwrap.dedent(source)))
if isinstance(node, ast.Constant) and isinstance(node.value, str)
)


def _code_strings(code: types.CodeType) -> set[str]:
strings = set()
for constant in code.co_consts:
if isinstance(constant, str):
strings.add(constant)
elif isinstance(constant, types.CodeType):
strings |= _code_strings(constant)
return strings


def _formula_strings(definition) -> set[str]:
"""Strings among the formula functions' constants and closure values."""

strings: set[str] = set()
for formula in definition.formulas.values():
code = getattr(formula, "__code__", None)
if code is not None:
strings |= _code_strings(code)
for cell in getattr(formula, "__closure__", None) or ():
try:
value = cell.cell_contents
except ValueError:
continue
if isinstance(value, str):
strings.add(value)
elif isinstance(value, (list, tuple, set, frozenset)):
strings |= {item for item in value if isinstance(item, str)}
return strings


def _extra_reads(definition, system) -> set[str]:
"""Variables ``definition`` reads that its static graph may not show."""

reads = {
value
for value in (
_class_string_constants(type(definition)) | _formula_strings(definition)
)
if value in system.variables
}
defined_for = getattr(definition, "defined_for", None)
if defined_for is not None:
reads.add(getattr(defined_for, "name", defined_for))
reads.discard(definition.name)
return {read for read in reads if read in system.variables}


def _readable_variables(
variable: str, *, metadata_index: PolicyEngineUSVariableMetadataIndex, system
) -> list[str]:
names = _graph_variables(variable, metadata_index=metadata_index, system=system)
names.add(variable)
pending = sorted(names)
while pending:
definition = system.variables[pending.pop()]
for read in sorted(_extra_reads(definition, system) - names):
added = {read} | _graph_variables(
read, metadata_index=metadata_index, system=system
)
pending.extend(sorted(added - names))
names |= added
return sorted(names)
Loading
Loading