|
1 | 1 | from policyengine_core.country_template.situation_examples import single |
| 2 | +from policyengine_core.country_template import Simulation as CountryTemplateSimulation |
| 3 | +from policyengine_core.country_template.entities import Person |
| 4 | +from policyengine_core.data import Dataset |
| 5 | +from policyengine_core.model_api import Variable |
| 6 | +from policyengine_core.periods import MONTH |
2 | 7 | from policyengine_core.simulations import SimulationBuilder |
3 | 8 | import policyengine_core.simulations.simulation as simulation_module |
4 | 9 | from policyengine_core.simulations.simulation_macro_cache import ( |
5 | 10 | SimulationMacroCache, |
6 | 11 | ) |
7 | 12 | import importlib.metadata |
8 | 13 | import numpy as np |
| 14 | +import pandas as pd |
9 | 15 | from pathlib import Path |
10 | 16 |
|
11 | 17 |
|
@@ -112,3 +118,127 @@ def __init__(self, tax_benefit_system): |
112 | 118 | simulation = SimulationBuilder().build_default_simulation(tax_benefit_system) |
113 | 119 |
|
114 | 120 | simulation.calculate("income_tax", "2017-01") |
| 121 | + |
| 122 | + |
| 123 | +class formula_component_for_safe_export(Variable): |
| 124 | + value_type = float |
| 125 | + entity = Person |
| 126 | + definition_period = MONTH |
| 127 | + label = "Formula component for safe export tests." |
| 128 | + |
| 129 | + def formula(person, period): |
| 130 | + return person("salary", period) * 0 |
| 131 | + |
| 132 | + |
| 133 | +class pseudo_input_for_safe_export(Variable): |
| 134 | + value_type = float |
| 135 | + entity = Person |
| 136 | + definition_period = MONTH |
| 137 | + label = "Pseudo-input for safe export tests." |
| 138 | + adds = ["formula_component_for_safe_export"] |
| 139 | + |
| 140 | + |
| 141 | +def _safe_export_dataset(dataframe): |
| 142 | + return Dataset.from_dataframe(dataframe, "2022-01") |
| 143 | + |
| 144 | + |
| 145 | +def _safe_export_simulation(isolated_tax_benefit_system): |
| 146 | + isolated_tax_benefit_system.add_variable(formula_component_for_safe_export) |
| 147 | + isolated_tax_benefit_system.add_variable(pseudo_input_for_safe_export) |
| 148 | + |
| 149 | + dataframe = pd.DataFrame( |
| 150 | + { |
| 151 | + "person_id__2022": [0], |
| 152 | + "household_id__2022": [0], |
| 153 | + "person_household_id__2022": [0], |
| 154 | + "person_household_role__2022": ["parent"], |
| 155 | + "household_weight__2022": [1.0], |
| 156 | + "salary__2022-01": [0.0], |
| 157 | + "pseudo_input_for_safe_export__2022-01": [999.0], |
| 158 | + } |
| 159 | + ) |
| 160 | + return CountryTemplateSimulation( |
| 161 | + tax_benefit_system=isolated_tax_benefit_system, |
| 162 | + dataset=_safe_export_dataset(dataframe), |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +def test__given_pseudo_input_in_dataset__then_input_dataframe_excludes_it( |
| 167 | + isolated_tax_benefit_system, |
| 168 | +): |
| 169 | + # Given |
| 170 | + simulation = _safe_export_simulation(isolated_tax_benefit_system) |
| 171 | + |
| 172 | + assert simulation.calculate("pseudo_input_for_safe_export", "2022-01")[0] == 999.0 |
| 173 | + |
| 174 | + # When |
| 175 | + dataframe = simulation.to_input_dataframe() |
| 176 | + reloaded = CountryTemplateSimulation( |
| 177 | + tax_benefit_system=isolated_tax_benefit_system, |
| 178 | + dataset=_safe_export_dataset(dataframe), |
| 179 | + ) |
| 180 | + |
| 181 | + # Then |
| 182 | + assert "salary__2022-01" in dataframe.columns |
| 183 | + assert "pseudo_input_for_safe_export__2022-01" not in dataframe.columns |
| 184 | + assert "salary" in simulation.true_input_variables |
| 185 | + assert "pseudo_input_for_safe_export" not in simulation.true_input_variables |
| 186 | + assert ( |
| 187 | + "pseudo_input_for_safe_export__2022-01" |
| 188 | + in simulation.to_input_dataframe(include_computed_variables=True).columns |
| 189 | + ) |
| 190 | + assert reloaded.calculate("pseudo_input_for_safe_export", "2022-01")[0] == 0.0 |
| 191 | + |
| 192 | + |
| 193 | +def test__given_pseudo_input_in_dataset__then_input_dict_h5_round_trip_excludes_it( |
| 194 | + isolated_tax_benefit_system, tmp_path |
| 195 | +): |
| 196 | + # Given |
| 197 | + simulation = _safe_export_simulation(isolated_tax_benefit_system) |
| 198 | + exported_data = simulation.to_input_dict() |
| 199 | + h5_path = tmp_path / "safe_export.h5" |
| 200 | + |
| 201 | + class SafeExportDataset(Dataset): |
| 202 | + name = "safe_export" |
| 203 | + label = "Safe export" |
| 204 | + file_path = h5_path |
| 205 | + data_format = Dataset.TIME_PERIOD_ARRAYS |
| 206 | + |
| 207 | + # When |
| 208 | + SafeExportDataset().save_dataset(exported_data) |
| 209 | + reloaded = CountryTemplateSimulation( |
| 210 | + tax_benefit_system=isolated_tax_benefit_system, |
| 211 | + dataset=Dataset.from_file(h5_path), |
| 212 | + ) |
| 213 | + |
| 214 | + # Then |
| 215 | + assert "salary" in exported_data |
| 216 | + assert "pseudo_input_for_safe_export" not in exported_data |
| 217 | + assert "pseudo_input_for_safe_export" in simulation.to_input_dict( |
| 218 | + include_computed_variables=True |
| 219 | + ) |
| 220 | + assert reloaded.calculate("pseudo_input_for_safe_export", "2022-01")[0] == 0.0 |
| 221 | + |
| 222 | + |
| 223 | +def test__given_branch_inherits_dataset_inputs__then_safe_exports_include_them( |
| 224 | + isolated_tax_benefit_system, |
| 225 | +): |
| 226 | + # Given |
| 227 | + simulation = _safe_export_simulation(isolated_tax_benefit_system) |
| 228 | + branch = simulation.get_branch("reform") |
| 229 | + |
| 230 | + assert branch.calculate("salary", "2022-01")[0] == 0.0 |
| 231 | + |
| 232 | + # When |
| 233 | + dataframe = branch.to_input_dataframe() |
| 234 | + exported_data = branch.to_input_dict() |
| 235 | + |
| 236 | + # Then |
| 237 | + assert "person_id__ETERNITY" in dataframe.columns |
| 238 | + assert "household_id__ETERNITY" in dataframe.columns |
| 239 | + assert "household_weight__2022" in dataframe.columns |
| 240 | + assert "salary__2022-01" in dataframe.columns |
| 241 | + assert "pseudo_input_for_safe_export__2022-01" not in dataframe.columns |
| 242 | + assert "salary" in exported_data |
| 243 | + assert "pseudo_input_for_safe_export" not in exported_data |
| 244 | + assert "salary" in branch.true_input_variables |
0 commit comments