Skip to content

Commit

Permalink
Enable parameter adds/subtracts
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilwoodruff committed Jan 2, 2023
1 parent 0aeae3f commit 1236b42
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 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:
added:
- Adds/subtracts option for parameter names.
30 changes: 28 additions & 2 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,41 @@ def _run_formula(
if formula is None:
values = None
if variable.adds is not None:
if isinstance(variable.adds, str):
try:
adds_parameter = get_parameter(
self.tax_benefit_system.parameters,
variable.adds,
)
except:
raise ValueError(
f"In the variable '{variable.name}', the 'adds' attribute is a string '{variable.adds}' that does not match any parameter."
)
adds_list = adds_parameter(period.start)
else:
adds_list = variable.adds
values = 0
for added_variable in variable.adds:
for added_variable in adds_list:
values = values + self.calculate(
added_variable, period, map_to=variable.entity.key
)
if variable.subtracts is not None:
if isinstance(variable.subtracts, str):
try:
subtracts_parameter = get_parameter(
self.tax_benefit_system.parameters,
variable.subtracts,
)
except:
raise ValueError(
f"In the variable '{variable.name}', the 'subtracts' attribute is a string '{variable.subtracts}' that does not match any parameter."
)
subtracts_list = subtracts_parameter(period.start)
else:
subtracts_list = variable.subtracts
if values is None:
values = 0
for subtracted_variable in variable.subtracts:
for subtracted_variable in subtracts_list:
values = values - self.calculate(
subtracted_variable, period, map_to=variable.entity.key
)
Expand Down
6 changes: 3 additions & 3 deletions policyengine_core/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class Variable:
"""Index of the variable in the module it is defined in."""

adds: List[str] = None
"""List of variables that are added to the variable."""
"""List of variables that are added to the variable. Alternatively, can be a parameter name."""

subtracts: List[str] = None
"""List of variables that are subtracted from the variable."""
"""List of variables that are subtracted from the variable. Alternatively, can be a parameter name."""

uprating: str = None
"""Name of a parameter used to uprate the variable."""
Expand Down Expand Up @@ -236,7 +236,7 @@ def __init__(self, baseline_variable=None):
default=None,
)

self.adds = self.set(attr, "adds", allowed_type=list)
self.adds = self.set(attr, "adds")
self.subtracts = self.set(attr, "subtracts", allowed_type=list)
self.uprating = self.set(attr, "uprating", allowed_type=str)
self.hidden_input = self.set(
Expand Down

0 comments on commit 1236b42

Please sign in to comment.