diff --git a/changelog.d/avoid-eager-holder-allocation.fixed.md b/changelog.d/avoid-eager-holder-allocation.fixed.md new file mode 100644 index 000000000..b8e59c467 --- /dev/null +++ b/changelog.d/avoid-eager-holder-allocation.fixed.md @@ -0,0 +1 @@ +`Simulation.__init__` and `SimulationBuilder.build_from_dict` now build `input_variables` from recorded inputs without eagerly allocating empty `Holder` objects for every variable in the system, so `memory_config` set after setup applies to holders created afterwards (values can go to disk, and `variables_to_drop` variables are no longer stored). diff --git a/policyengine_core/simulations/simulation.py b/policyengine_core/simulations/simulation.py index 297e7a778..0768e4e6d 100644 --- a/policyengine_core/simulations/simulation.py +++ b/policyengine_core/simulations/simulation.py @@ -892,10 +892,25 @@ def __init__( self.calc = self.calculate self.df = self.calculate_dataframe + recorded_input_names = { + name for name, _branch_name, _period in self._user_input_keys + } + if populations is not None: + # Populations passed in may already hold values this simulation + # did not record. + recorded_input_names.update( + name + for population in self.populations.values() + for name, holder in population._holders.items() + if len(holder.get_known_periods()) > 0 + ) + # A reform may have moved a recorded variable to another entity, leaving + # its value in a holder this simulation no longer reads. self.input_variables = [ - variable.name - for variable in self.tax_benefit_system.variables.values() - if len(self.get_holder(variable.name).get_known_periods()) > 0 + name + for name, variable in self.tax_benefit_system.variables.items() + if name in recorded_input_names + and name in self.populations[variable.entity.key]._holders ] self.situation_input = situation diff --git a/policyengine_core/simulations/simulation_builder.py b/policyengine_core/simulations/simulation_builder.py index dbda2e86f..6b65cfe9b 100644 --- a/policyengine_core/simulations/simulation_builder.py +++ b/policyengine_core/simulations/simulation_builder.py @@ -186,10 +186,18 @@ def build_from_dict( tax_benefit_system, input_dict, simulation ) + # A subclass that replaces ``__init__`` has no record until an input is + # stored. + recorded_input_names = { + name + for name, _branch_name, _period in getattr( + simulation, "_user_input_keys", () + ) + } simulation.input_variables = [ - variable.name - for variable in simulation.tax_benefit_system.variables.values() - if len(simulation.get_holder(variable.name).get_known_periods()) > 0 + name + for name in simulation.tax_benefit_system.variables + if name in recorded_input_names ] return simulation diff --git a/tests/core/test_apply_reform_holders_property.py b/tests/core/test_apply_reform_holders_property.py index d787346dc..7f2a6878d 100644 --- a/tests/core/test_apply_reform_holders_property.py +++ b/tests/core/test_apply_reform_holders_property.py @@ -72,7 +72,7 @@ def test_every_existing_holder_uses_its_system_variable( simulation = build(mask) for name in REPLACED: population = simulation.get_variable_population(name) - del population._holders[name] + population._holders.pop(name, None) for name in order: simulation.get_holder(name) if warm: @@ -132,7 +132,7 @@ def test_reform_before_or_after_holder_creation_returns_same_values( mask, default, warm ): before_holder = build(mask) - del before_holder.person._holders["award"] + before_holder.person._holders.pop("award", None) after_holder = build(mask) if warm: after_holder.calculate("award", YEAR) diff --git a/tests/core/test_reform_baseline_system.py b/tests/core/test_reform_baseline_system.py index 519223374..c1c337280 100644 --- a/tests/core/test_reform_baseline_system.py +++ b/tests/core/test_reform_baseline_system.py @@ -193,7 +193,14 @@ def test_the_baseline_drops_a_holder_built_for_another_entity(): "households": {"h": {"parents": ["a", "b"]}}, } reform = Simulation(situation=situation, reform=move_salary_to_households) - baseline = reform.baseline + # Create the reform's household holder before cloning the baseline branch so + # ``_bind_to_tax_benefit_system`` must drop the cross-entity holder. + reform.calculate("salary", PERIOD) + del reform.branches["baseline"] + baseline = reform.get_branch("baseline") + baseline.tax_benefit_system = reform.default_tax_benefit_system_instance + baseline._bind_to_tax_benefit_system() + baseline.input_variables = list(baseline.input_variables) assert "salary" in reform.household._holders assert "salary" not in baseline.household._holders diff --git a/tests/core/test_simulations.py b/tests/core/test_simulations.py index 5118021b4..8cf99c1a5 100644 --- a/tests/core/test_simulations.py +++ b/tests/core/test_simulations.py @@ -242,3 +242,179 @@ def test__given_branch_inherits_dataset_inputs__then_safe_exports_include_them( assert "salary" in exported_data assert "pseudo_input_for_safe_export" not in exported_data assert "salary" in branch.true_input_variables + + +def test__given_situation_simulation__then_untouched_variables_have_no_holders( + isolated_tax_benefit_system, +): + situation = { + "persons": {"Alicia": {"salary": {"2022-01": 1000}}}, + "households": {"_": {"parents": ["Alicia"]}}, + } + + built = SimulationBuilder().build_from_dict(isolated_tax_benefit_system, situation) + simulation = CountryTemplateSimulation( + tax_benefit_system=isolated_tax_benefit_system, + situation=situation, + ) + cloned = simulation.clone() + + for sim in (built, simulation, cloned): + assert sim.input_variables == ["salary"] + assert set(sim.persons._holders) == {"salary"} + assert sim.household._holders == {} + assert "income_tax" not in sim.persons._holders + assert "rent" not in sim.household._holders + + +def test__given_dataset_simulation__then_untouched_variables_have_no_holders( + isolated_tax_benefit_system, +): + simulation = _safe_export_simulation(isolated_tax_benefit_system) + + assert "salary" in simulation.input_variables + assert "income_tax" not in simulation.persons._holders + assert "rent" not in simulation.household._holders + + +def test__given_reform_removing_input_variable__then_init_excludes_it_without_allocating_holders( + isolated_tax_benefit_system, +): + from policyengine_core.reforms import Reform + + class DropSalaryReform(Reform): + def apply(self): + del self.variables["salary"] + + situation = { + "persons": {"Alicia": {"salary": {"2022-01": 1000}, "age": {"2022-01": 30}}}, + "households": {"_": {"parents": ["Alicia"]}}, + } + + simulation = CountryTemplateSimulation( + tax_benefit_system=isolated_tax_benefit_system, + situation=situation, + reform=DropSalaryReform, + ) + + assert simulation.input_variables == ["age"] + assert "income_tax" not in simulation.persons._holders + assert "rent" not in simulation.household._holders + + +def test__given_dataset_simulation__then_input_variables_preserve_system_order( + isolated_tax_benefit_system, +): + system_variables = list(isolated_tax_benefit_system.variables) + dataframe = pd.DataFrame( + { + "household_weight__2022": [1.0], + "rent__2022-01": [1200.0], + "salary__2022-01": [500.0], + "age__2022-01": [30], + "person_household_role__2022": ["parent"], + "person_household_id__2022": [0], + "household_id__2022": [0], + "person_id__2022": [0], + } + ) + simulation = CountryTemplateSimulation( + tax_benefit_system=isolated_tax_benefit_system, + dataset=_safe_export_dataset(dataframe), + ) + + expected_names = { + "person_id", + "household_id", + "person_household_id", + "person_household_role", + "household_weight", + "salary", + "age", + "rent", + } + expected_order = [name for name in system_variables if name in expected_names] + assert simulation.input_variables == expected_order + assert "income_tax" not in simulation.persons._holders + assert "accommodation_size" not in simulation.household._holders + + +def test__given_prepopulated_populations__then_input_variables_include_their_inputs( + isolated_tax_benefit_system, +): + situation = { + "persons": {"Alicia": {"salary": {"2022-01": 1000}, "age": {"2022-01": 30}}}, + "households": {"_": {"parents": ["Alicia"]}}, + } + built = SimulationBuilder().build_from_dict(isolated_tax_benefit_system, situation) + + # Fresh Simulation given pre-filled populations has an empty _user_input_keys + # of its own, so __init__ must still discover existing holder inputs. + simulation = CountryTemplateSimulation( + tax_benefit_system=isolated_tax_benefit_system, + populations=built.populations, + ) + + expected_order = [ + name + for name in isolated_tax_benefit_system.variables + if name in {"salary", "age"} + ] + assert simulation.input_variables == expected_order + assert "income_tax" not in simulation.persons._holders + assert "rent" not in simulation.household._holders + + +def test__given_reform_moving_input_variable_entity__then_init_excludes_it_without_allocating_holders( + isolated_tax_benefit_system, +): + from policyengine_core.country_template.entities import Household + from policyengine_core.reforms import Reform + + class MoveSalaryToHousehold(Reform): + def apply(self): + class salary(Variable): + entity = Household + + self.update_variable(salary) + + situation = { + "persons": {"Alicia": {"salary": {"2022-01": 1000}, "age": {"2022-01": 30}}}, + "households": {"_": {"parents": ["Alicia"]}}, + } + + simulation = CountryTemplateSimulation( + tax_benefit_system=isolated_tax_benefit_system, + situation=situation, + reform=MoveSalaryToHousehold, + ) + + assert simulation.input_variables == ["age"] + assert "salary" not in simulation.household._holders + assert "income_tax" not in simulation.persons._holders + assert "rent" not in simulation.household._holders + + +def test__given_simulation_without_user_input_keys__then_build_from_dict_leaves_input_variables_empty( + isolated_tax_benefit_system, +): + from policyengine_core.simulations import Simulation + + class CustomInitSimulation(Simulation): + def __init__(self, tax_benefit_system): + self.tax_benefit_system = tax_benefit_system + self.build_from_populations(tax_benefit_system.instantiate_entities()) + + simulation = CustomInitSimulation(isolated_tax_benefit_system) + situation = { + "persons": {"Alicia": {"income_tax": {"2022-01": None}}}, + "households": {"_": {"parents": ["Alicia"]}}, + } + + built = SimulationBuilder().build_from_dict( + isolated_tax_benefit_system, situation, simulation=simulation + ) + + assert built is simulation + assert not hasattr(built, "_user_input_keys") + assert built.input_variables == [] diff --git a/tests/core/test_storage_input_index.py b/tests/core/test_storage_input_index.py index ffbd75bcd..fa95a6d9c 100644 --- a/tests/core/test_storage_input_index.py +++ b/tests/core/test_storage_input_index.py @@ -237,9 +237,11 @@ def test_a_pickle_records_that_the_inputs_were_recorded(): def test_only_storages_holding_an_input_have_a_set(tax_benefit_system): simulation = build_simulation(tax_benefit_system) simulation.calculate("disposable_income", JANUARY) + # Ensure at least one empty storage exists alongside populated storages. + simulation.get_holder("postal_code") storages = _storages(simulation) - assert len(storages) == len(tax_benefit_system.variables) + assert set(storages) < set(tax_benefit_system.variables) with_inputs = {name for name, storage in storages.items() if storage._inputs} with_derived = { name diff --git a/tests/core/test_storage_shared_index.py b/tests/core/test_storage_shared_index.py index c919eb959..0dcc80ac8 100644 --- a/tests/core/test_storage_shared_index.py +++ b/tests/core/test_storage_shared_index.py @@ -234,10 +234,10 @@ def test_storage_from_a_pickle_with_an_empty_set_still_works(): def test_simulation_storages_have_no_sets_of_their_own(tax_benefit_system): simulation = build_simulation(tax_benefit_system) simulation.calculate("disposable_income", JANUARY) + simulation.get_holder("postal_code") storages = _storages(simulation) - # One storage per variable, as in every simulation. - assert len(storages) == len(tax_benefit_system.variables) + assert storages and set(storages) < set(tax_benefit_system.variables) assert all(storage._shared is _NOTHING_SHARED for storage in storages.values()) copy = simulation.clone() @@ -251,6 +251,8 @@ def test_branch_storages_have_a_set_only_for_the_arrays_they_share( ): simulation = build_simulation(tax_benefit_system) simulation.calculate("disposable_income", JANUARY) + # Include an empty storage so the branch checks unpopulated storages too. + simulation.get_holder("postal_code") with_arrays = { name for name, storage in _storages(simulation).items() if storage._arrays } diff --git a/tests/fixtures/set_input_helper_order.py b/tests/fixtures/set_input_helper_order.py index e0a184647..7ce0bb160 100644 --- a/tests/fixtures/set_input_helper_order.py +++ b/tests/fixtures/set_input_helper_order.py @@ -205,10 +205,10 @@ def build_simulation(system=None, on_disk: bool = False): warnings.simplefilter("ignore") simulation.memory_config = MemoryConfig(max_memory_occupation=0) # A holder takes its storage from the configuration it is created - # under, and building the simulation creates them all: drop the - # (still empty) ones of the fixture's variables. + # under, so drop any already-created empty holders for the fixture's + # variables before continuing with on-disk storage. for name in NAMES: - del simulation.get_variable_population(name)._holders[name] + simulation.get_variable_population(name)._holders.pop(name, None) return simulation