Skip to content

Commit

Permalink
Merge pull request #121 from PolicyEngine/small-issue-fixes
Browse files Browse the repository at this point in the history
Small issue fixes
  • Loading branch information
nikhilwoodruff authored Oct 5, 2023
2 parents a989aba + 8a0f056 commit e3046f1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
7 changes: 7 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- bump: minor
changes:
added:
- Support for adds/subtracts targeting parameters.
- Support for adds/subtracts targeting year ranges where no parameters exist.
changed:
- Deprecated sum_of_variables.
6 changes: 6 additions & 0 deletions policyengine_core/commons/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from numpy import round as round_
from numpy import select, where

from warnings import warn

from policyengine_core.parameters.parameter_node import ParameterNode
from policyengine_core.periods.period_ import Period
from policyengine_core.populations.population import Population
Expand Down Expand Up @@ -395,6 +397,10 @@ def sum_of_variables(variables: Union[List[str], str]) -> Callable:
Callable: A function that sums the values of the variables.
"""

warn(
f"Sum-of-variables formulas are deprecated- please use `adds` or `subtracts` instead."
)

def sum_of_variables(entity, period, parameters):
if isinstance(variables, str):
# A string parameter name is passed
Expand Down
5 changes: 2 additions & 3 deletions policyengine_core/scripts/run_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@


def dataset_summary(datasets: List[Dataset]) -> str:
years = list(sorted(list(set(sum([ds.years for ds in datasets], [])))))
df = pd.DataFrame(
{
year: ["✓" if year in ds.years else "" for ds in datasets]
for year in years
"Label": [ds.label for ds in datasets],
"Stored": ["✓" if ds.exists else "" for ds in datasets],
},
index=[ds.name for ds in datasets],
)
Expand Down
41 changes: 33 additions & 8 deletions policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def _run_formula(
formula = variable.get_formula(period)
if formula is None:
values = None
if variable.adds is not None:
if variable.adds is not None and len(variable.adds) > 0:
if isinstance(variable.adds, str):
try:
adds_parameter = get_parameter(
Expand All @@ -715,10 +715,20 @@ def _run_formula(
adds_list = variable.adds
values = 0
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 added_variable in self.tax_benefit_system.variables:
values = values + self.calculate(
added_variable, period, map_to=variable.entity.key
)
else:
try:
parameter = get_parameter(
self.tax_benefit_system.parameters,
added_variable,
)
values = values + parameter(period.start)
except:
pass
if variable.subtracts is not None and len(variable.subtracts) > 0:
if isinstance(variable.subtracts, str):
try:
subtracts_parameter = get_parameter(
Expand All @@ -735,9 +745,24 @@ def _run_formula(
if values is None:
values = 0
for subtracted_variable in subtracts_list:
values = values - self.calculate(
subtracted_variable, period, map_to=variable.entity.key
)
if (
subtracted_variable
in self.tax_benefit_system.variables
):
values = values - self.calculate(
subtracted_variable,
period,
map_to=variable.entity.key,
)
else:
try:
parameter = get_parameter(
self.tax_benefit_system.parameters,
subtracted_variable,
)
values = values + parameter(period.start)
except:
pass
return values

if self.trace and not isinstance(
Expand Down

0 comments on commit e3046f1

Please sign in to comment.