diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..7db28cb8 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -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. \ No newline at end of file diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 9e390809..df9ceb36 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -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 @@ -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 diff --git a/policyengine_core/scripts/run_data.py b/policyengine_core/scripts/run_data.py index b608d928..53c46537 100644 --- a/policyengine_core/scripts/run_data.py +++ b/policyengine_core/scripts/run_data.py @@ -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], ) diff --git a/policyengine_core/simulations/simulation.py b/policyengine_core/simulations/simulation.py index d0cd74ab..240a2b82 100644 --- a/policyengine_core/simulations/simulation.py +++ b/policyengine_core/simulations/simulation.py @@ -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( @@ -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( @@ -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(