Skip to content

Commit

Permalink
Neutralising (or updating/removing) variables don't affect caching (#214
Browse files Browse the repository at this point in the history
)

Fixes #213
  • Loading branch information
nikhilwoodruff authored May 31, 2024
1 parent 64937e2 commit 2eadbd1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: minor
changes:
fixed:
- Bug causing reforms affecting data to not affect caches.
6 changes: 4 additions & 2 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def build_from_dataset(self) -> None:

self.default_calculation_period = self.dataset.time_period

self.tax_benefit_system.data_modified = False

@property
def trace(self) -> bool:
return self._trace
Expand Down Expand Up @@ -1375,7 +1377,7 @@ def _get_macro_cache_value(
"""
Get the value of a variable from a cache file.
"""
if not self.macro_cache_read:
if not self.macro_cache_read or self.tax_benefit_system.data_modified:
return None
with h5py.File(cache_file_path, "r") as f:
return f["values"][()]
Expand All @@ -1388,7 +1390,7 @@ def _set_macro_cache_value(
"""
Set the value of a variable in a cache file.
"""
if not self.macro_cache_write:
if not self.macro_cache_write or self.tax_benefit_system.data_modified:
return None
with h5py.File(cache_file_path, "w") as f:
f.create_dataset("values", data=value)
Expand Down
5 changes: 5 additions & 0 deletions policyengine_core/taxbenefitsystems/tax_benefit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(self, entities: Sequence[Entity] = None, reform=None) -> None:

if self.variables_dir is not None:
self.add_variables_from_directory(self.variables_dir)
self.data_modified = False

if self.parameters_dir is not None:
self.load_parameters(self.parameters_dir)
Expand Down Expand Up @@ -228,6 +229,7 @@ def add_variable(self, variable: Variable) -> Variable:
policyengine_core.errors.VariableNameConflictError: if a variable with the same name have previously been added to the tax and benefit system.
"""
self.data_modified = True
return self.load_variable(variable, update=False)

def replace_variable(self, variable: str) -> Variable:
Expand All @@ -244,6 +246,7 @@ def replace_variable(self, variable: str) -> Variable:
if self.variables.get(name) is not None:
del self.variables[name]
self.load_variable(variable, update=False)
self.data_modified = True

def update_variable(self, variable: str) -> Variable:
"""
Expand All @@ -257,6 +260,7 @@ def update_variable(self, variable: str) -> Variable:
:param Variable variable: Variable to add. Must be a subclass of Variable.
"""
self.data_modified = True
return self.load_variable(variable, update=True)

def add_variables_from_file(self, file_path: str) -> None:
Expand Down Expand Up @@ -501,6 +505,7 @@ def neutralize_variable(self, variable_name: str) -> None:
self.variables[variable_name] = variables.get_neutralized_variable(
self.get_variable(variable_name)
)
self.data_modified = True

def annualize_variable(
self, variable_name: str, period: typing.Optional[Period] = None
Expand Down

0 comments on commit 2eadbd1

Please sign in to comment.