From 63809df438da01199129879aaada31f4674c2374 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:08:56 +0100 Subject: [PATCH 1/5] Fix CLI datasets list error `no attribute 'years'` #91 --- policyengine_core/scripts/run_data.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/policyengine_core/scripts/run_data.py b/policyengine_core/scripts/run_data.py index b608d928f..53c465370 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], ) From 7713239f06fa9daf2affcd777e8f8766f67a51a8 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:27:58 +0100 Subject: [PATCH 2/5] Fix #116 --- policyengine_core/simulations/simulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/policyengine_core/simulations/simulation.py b/policyengine_core/simulations/simulation.py index d0cd74ab4..e8a6796e8 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( @@ -718,7 +718,7 @@ def _run_formula( values = values + self.calculate( added_variable, period, map_to=variable.entity.key ) - if variable.subtracts is not None: + if variable.subtracts is not None and len(variable.subtracts) > 0: if isinstance(variable.subtracts, str): try: subtracts_parameter = get_parameter( From 64bde2a3ecbd94d7448ad1bbed2771b9d7ce944a Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:31:08 +0100 Subject: [PATCH 3/5] Fix #69 --- policyengine_core/commons/formulas.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 9e3908094..4e36efaa0 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,8 @@ 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 From 3936dc0c21aaa4a1d0c34f952578e369d3763b0b Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:34:31 +0100 Subject: [PATCH 4/5] Fix #102 --- policyengine_core/commons/formulas.py | 4 ++- policyengine_core/simulations/simulation.py | 37 +++++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 4e36efaa0..df9ceb361 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -397,7 +397,9 @@ 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.") + 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): diff --git a/policyengine_core/simulations/simulation.py b/policyengine_core/simulations/simulation.py index e8a6796e8..240a2b825 100644 --- a/policyengine_core/simulations/simulation.py +++ b/policyengine_core/simulations/simulation.py @@ -715,9 +715,19 @@ 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 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: @@ -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( From 8a0f056b25beaa6995279e8b01fbe7b0f51dffcb Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:36:58 +0100 Subject: [PATCH 5/5] Versioning --- changelog_entry.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29bb..7db28cb8b 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