Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure every instance of Simulation class has dataset, fix check_macro_cache #266

Merged
merged 1 commit into from
Sep 3, 2024
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
5 changes: 5 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- bump: patch
changes:
changed:
- Ensure that every instance of 'Simulation' class has 'dataset' attribute, even if equal to 'None'
- Add better safeguards to 'check_macro_cache' method of 'Simulation' class
18 changes: 14 additions & 4 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(
self.reform = reform
self.tax_benefit_system = tax_benefit_system
self.branch_name = "default"
self.dataset = dataset

if dataset is None:
if self.default_dataset is not None:
Expand Down Expand Up @@ -607,7 +608,7 @@ def _calculate(

smc = SimulationMacroCache(self.tax_benefit_system)

# Check if cache could be used, if available, check if path exists
# Check if cache can be used, if available, check if path exists
is_cache_available = self.check_macro_cache(variable_name, str(period))
if is_cache_available:
smc.set_cache_path(
Expand Down Expand Up @@ -1418,9 +1419,18 @@ def check_macro_cache(self, variable_name: str, period: str) -> bool:
"""
Check if the variable is able to have cached value
"""
if hasattr(self, "dataset"):
if self.dataset.data_format == Dataset.FLAT_FILE:
return False

# Dataset should always exist, but just in case
if not hasattr(self, "dataset"):
return False

# If no dataset, no need to cache
if self.dataset is None:
return False

# If using a flat file dataset, we're unable to cache
if self.dataset.data_format == Dataset.FLAT_FILE:
return False

if self.is_over_dataset:
return True
Expand Down
Loading