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/derive-reform-systems-from-baseline.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Build a reform's tax-benefit system from the already-built default system instead of reloading every parameter and variable file.
30 changes: 27 additions & 3 deletions policyengine_us/spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ def clone_spm_system(system, *, copy_receipts=True, parameters=None):
cloned.spm_forecast_provider = system.spm_forecast_provider.snapshot(
copy_receipts=copy_receipts
)
# The record a default system keeps to derive reform systems holds its
# own variable objects, which the clone has replaced: keeping it would
# keep those objects alive for as long as the clone.
cloned.__dict__.pop("_unreformed_build", None)
# A clone owns its tree outright, but it does not keep it to itself: a
# branch copies the system, and another simulation can be built on it, at
# which point a reform must clone before it writes. ``plain_policy_copy``
Expand Down Expand Up @@ -951,9 +955,7 @@ def _prepare_spm_system(self, args, kwargs, config, start_instant):
if supplied is None:
if reform is not None:
chosen = with_parameter_barrier(
self.default_tax_benefit_system(
reform=reform, spm=config, start_instant=start_instant
)
self._reform_system(reform, config, start_instant)
)
else:
# No reform and no supplied system: nothing distinguishes this
Expand Down Expand Up @@ -990,6 +992,28 @@ def _prepare_spm_system(self, args, kwargs, config, start_instant):
key: value for key, value in arguments.arguments.items() if key != "self"
}

def _reform_system(self, reform, config, start_instant):
"""The system ``default_tax_benefit_system(reform=...)`` would build.

Derive it from the shared default system when that system can give
the same result (``CountryTaxBenefitSystem.can_derive_reform_systems``),
which skips loading and processing every parameter and variable file.
Otherwise build it.
"""
default = self.default_tax_benefit_system_instance
can_derive = getattr(default, "can_derive_reform_systems", None)
if (
type(default) is self.default_tax_benefit_system
and can_derive is not None
and can_derive()
):
return default.derive_reform_system(
reform, start_instant=start_instant, spm=config
)
return self.default_tax_benefit_system(
reform=reform, spm=config, start_instant=start_instant
)

def clone(self, debug=False, trace=False, clone_tax_benefit_system=True):
"""Retain cached-result receipts while isolating future calculations."""
cloned = super().clone(debug=debug, trace=trace, clone_tax_benefit_system=False)
Expand Down
240 changes: 240 additions & 0 deletions policyengine_us/system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import hashlib
from copy import deepcopy
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
from policyengine_core.taxbenefitsystems import TaxBenefitSystem
Expand Down Expand Up @@ -50,6 +52,13 @@
from .tools.per_capita_uprating import (
add_per_capita_parameters_for_parameter_uprating,
add_per_capita_uprating,
derived_per_capita_series,
detached_copy,
is_national_total_path,
per_capita_nodes_unchanged,
remove_per_capita_series,
restore_child_order,
restore_per_capita_series,
)
from policyengine_us.data.dataset_schema import (
_DEFAULT_TIME_PERIOD,
Expand All @@ -61,9 +70,12 @@
from typing import Annotated
from spm_calculator.policyengine_adapter import build_policyengine_variables
from policyengine_us.spm import (
SHARED_TREE_MARK,
SPMSimulationMixin,
bind_private_entities,
clone_spm_system,
create_spm_provider,
plain_policy_copy,
)

install_social_security_input_compatibility()
Expand Down Expand Up @@ -91,6 +103,34 @@
)


@dataclass(frozen=True, eq=False)
class UnreformedBuild:
"""What a system built without a reform records to derive reform systems.

It holds no reference to the system, so a copy or clone that carries it
does not keep the original's parameter tree alive.
"""

# The registry before the structural reforms and per-state variables.
variables_before_reforms: dict
# Copies, taken at the same point, of the variables in that registry that
# uprate by a national total. ``add_per_capita_uprating`` later points the
# registry's own objects at the per-capita series in place.
total_uprated_variables: dict
# The per-capita series ``add_per_capita_uprating`` added to the tree
# after that point, and copies, as they stood at that point, of those it
# replaced when it refreshed them.
per_capita_series_added: frozenset
per_capita_series_replaced: dict
# The order of the children of each node holding a replaced series, at
# that point: a refresh moves the series to the end of its parent.
per_capita_parent_child_order: dict
# The children of those nodes when construction finished.
per_capita_parent_children: dict
# The registry when construction finished.
variables: dict


class CountryTaxBenefitSystem(TaxBenefitSystem):
"""
The tax-benefit system for the United States.
Expand Down Expand Up @@ -169,6 +209,61 @@ def __init__(
"current_law_cap", draw_cap
)

# Nothing above depends on the reform. A system built without one
# records its policy as it stands here, before the structural reforms,
# the per-state variables and the per-capita series, so that a reform
# system can be derived from it by running only the steps below. An
# empty reform counts as a reform here, as it does for a simulation,
# which derives its system whenever its reform is not None.
if reform is not None:
self._apply_reforms(reform, start_instant)
self._unreformed_build = None
return
variables_before_reforms = dict(self.variables)
total_uprated_variables = {
name: variable.clone()
for name, variable in variables_before_reforms.items()
if is_national_total_path(variable.uprating)
}
series_before = derived_per_capita_series(self.parameters)
series_copies = {
path: detached_copy(series) for path, series in series_before.items()
}
child_order_before = {
parent_path: tuple(self.parameters.get_child(parent_path).children)
for parent_path in {path.rpartition(".")[0] for path in series_before}
}
self._apply_reforms(reform, start_instant)
series_after = derived_per_capita_series(self.parameters)
series_replaced = {
path: series_copies[path]
for path, series in series_before.items()
if series_after.get(path) is not series
}
self._unreformed_build = UnreformedBuild(
variables_before_reforms=variables_before_reforms,
total_uprated_variables=total_uprated_variables,
per_capita_series_added=frozenset(
series_after.keys() - series_before.keys()
),
per_capita_series_replaced=series_replaced,
per_capita_parent_child_order={
parent_path: child_order_before[parent_path]
for parent_path in {path.rpartition(".")[0] for path in series_replaced}
},
per_capita_parent_children={
parent_path: frozenset(self.parameters.get_child(parent_path).children)
for parent_path in {path.rpartition(".")[0] for path in series_replaced}
},
variables=dict(self.variables),
)

def _apply_reforms(self, reform, start_instant):
"""Apply ``reform`` and the structural reforms to processed policy.

These are the steps of construction that depend on the reform, so
``derive_reform_system`` runs them on a copy of an unreformed system.
"""
if reform:
# Applied after the parameter processing pipeline so that values
# a reform inserts at future dates cannot act as defined values
Expand Down Expand Up @@ -206,6 +301,151 @@ def __init__(
add_per_capita_uprating(self)
self._per_capita_uprating_built = True

def can_derive_reform_systems(self) -> bool:
"""Whether ``derive_reform_system`` gives what a full rebuild would.

Only a system built without a reform, and changed in neither its
variable registry nor its parameters since, holds the policy that
construction reaches before it applies a reform. A clone drops the
record of the build (``clone_spm_system``); a shallow copy that still
reads the same registry objects and tree keeps it, and derives the
same system. A subclass with its own constructor may build more than
``_apply_reforms`` gives a derived system, so it is rebuilt.

The check sees every change to the variable registry, and every
parameter change made through ``Parameter.update``, which marks the
parameter and its ancestors modified; reform dictionaries and most
``modify_parameters`` modifiers change parameters that way. It does
not see changes that mark nothing: nodes added or replaced with
``add_child``, ``_replace_child`` or ``merge`` (by a modifier, or by
``load_extension`` for an extension's parameters), assignments to
``values_list[i].value``, and edits to metadata or to a variable's
attributes. A reform system derived afterwards includes
such changes, as every simulation that shares this system's policy
already does; a full rebuild would not. Detecting them would need a
comparison of the whole tree on every derivation (measured at 0.7 to
1.6 s, against about 2.75 s for the derivation). The exception is the
few nodes derivation itself undoes the per-capita step on: if a series
the build added or refreshed, or the set of children of a node holding
a refreshed one, has changed, the system is rebuilt.
"""
build = self.__dict__.get("_unreformed_build")
if build is None:
return False
if type(self).__init__ is not CountryTaxBenefitSystem.__init__:
return False
if self.variables.keys() != build.variables.keys() or any(
self.variables[name] is not variable
for name, variable in build.variables.items()
):
return False
# Derivation removes the per-capita series the build added and
# restores those it refreshed, so those nodes must be as it left them.
if not per_capita_nodes_unchanged(
self.parameters,
build.per_capita_series_added | build.per_capita_series_replaced.keys(),
build.per_capita_parent_children,
):
return False
# Construction leaves every parameter unmodified, and
# ``Parameter.update`` marks what it changes. A node added after
# construction has no flag at all: an unmarked change, like those
# the docstring lists.
return not any(
getattr(parameter, "modified", False)
for parameter in self.parameters.get_descendants()
)

def derive_reform_system(
self,
reform,
start_instant: Annotated[
str, "ISO date format YYYY-MM-DD"
] = DEFAULT_START_DATE,
spm: dict | None = None,
) -> "CountryTaxBenefitSystem":
"""The system ``CountryTaxBenefitSystem(reform, start_instant, spm)``
builds, without loading or processing any parameter or variable file.

Construction loads about 12,000 parameter files and 12,300 variable
files and then homogenizes, interpolates, uprates and backdates the
tree, none of which depends on the reform. So copy this system's
processed parameters and its variable registry from before the
structural reforms, and run only the steps that follow
(``_apply_reforms``): apply the reform, detect the structural reforms
from the reformed parameters at ``start_instant``, add the per-state
variables and rebuild the per-capita uprating series.

Like core's ``Reform(baseline)``, this clones the parameter tree and
copies the variable dictionary, but the result is a
``CountryTaxBenefitSystem``. A core ``Reform`` takes its class
attributes and methods from ``TaxBenefitSystem`` instead, which drops
``auto_carry_over_input_variables`` and this class's ``clone`` and
``modify_parameters``.

Variable objects are shared with this system, as ``share_spm_policy``
shares them with every ordinary simulation: each core operation a
reform performs on a variable binds a new object to the name. The
exceptions are the variables that uprate by a national total, which
the per-capita step changes in place: those are fresh copies of the
recorded ones.

Edits written directly into this system's objects, bypassing the
methods that mark parameters modified, carry over into the derived
system (see ``can_derive_reform_systems``).
"""
if not self.can_derive_reform_systems():
raise ValueError(
"Only a system built without a reform, and unchanged since, "
"can derive reform systems; build CountryTaxBenefitSystem("
"reform=...) instead."
)
derived = plain_policy_copy(self)
derived._unreformed_build = None
# A simulation may have bound this system as its own; the derived
# system is not bound to any simulation yet.
derived.__dict__.pop("simulation", None)
# Core's clone copies the root node's attributes, including the mark
# that another system reads this tree and any tracer a traced request
# wrote there. A tree loaded from files has no mark (which reads as
# unshared) and no tracer.
build = self._unreformed_build
parameters = self.parameters.clone()
parameters.__dict__.pop(SHARED_TREE_MARK, None)
parameters.trace = False
parameters.tracer = None
parameters.branch_name = None
# In the default build, the per-capita step that ends construction is
# the only step after the recorded point that changes the tree: it
# adds series and refreshes some that parameter uprating built. Undo
# both, so a reform sees the series a full rebuild has when it
# applies the reform.
for path in build.per_capita_series_added:
remove_per_capita_series(parameters, path)
for path, earlier in build.per_capita_series_replaced.items():
restore_per_capita_series(parameters, path, detached_copy(earlier))
for parent_path, order in build.per_capita_parent_child_order.items():
restore_child_order(parameters, parent_path, order)
derived.parameters = parameters
derived._parameters_at_instant_cache = {}
derived.variables = dict(build.variables_before_reforms)
# Fresh copies each time: the per-capita step points them at the
# per-capita series in place, and a reform's ``update_variable``
# inherits the national total from them as it does in a full rebuild.
derived.variables.update(
(name, variable.clone())
for name, variable in build.total_uprated_variables.items()
)
bind_private_entities(derived)
derived.spm_forecast_provider = create_spm_provider(spm)
derived.modelled_policies = deepcopy(self.modelled_policies)
derived.variable_module_metadata = dict(self.variable_module_metadata)
# Construction applies the reform before it builds the per-capita
# series, so ``modify_parameters`` must not rebuild them midway.
derived._per_capita_uprating_built = False
derived._apply_reforms(reform, start_instant)
return derived

def modify_parameters(self, modifier_function):
result = super().modify_parameters(modifier_function)
# Reforms applied after init (core's Simulation applies the reform
Expand Down
Loading
Loading