From 55040624f1ee7b963c2451827f65a67d871e1b74 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Thu, 14 Mar 2024 19:26:06 +0200 Subject: [PATCH] fix: Repair function to find last uprating date --- .../operations/uprate_parameters.py | 48 ++++++++++++------- .../parameters/operations/test_uprating.py | 1 + 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/policyengine_core/parameters/operations/uprate_parameters.py b/policyengine_core/parameters/operations/uprate_parameters.py index f4b04979..4802553a 100644 --- a/policyengine_core/parameters/operations/uprate_parameters.py +++ b/policyengine_core/parameters/operations/uprate_parameters.py @@ -225,11 +225,11 @@ def find_cadence_last(uprater: Parameter, cadence_options: dict) -> Instant: cadence_end_year = None if ( # e.g., If last_param is 10-1 and uprater end date is 4-1 - last_param.month > cadence_options["enactment"]["month"] or + last_param.month > cadence_options["end"]["month"] or ( # e.g., If last_param is 4-15 and uprater end date is 4-1 - last_param.month == cadence_options["enactment"]["month"] or - last_param.day > cadence_options["enactment"]["day"] + last_param.month == cadence_options["end"]["month"] and + last_param.day > cadence_options["end"]["day"] ) ): cadence_end_year = last_param.year - 1 @@ -250,27 +250,39 @@ def find_cadence_last(uprater: Parameter, cadence_options: dict) -> Instant: def construct_cadence_uprater(uprating_parameter: Parameter, cadence_options: dict, first_date: Instant, last_date: Instant) -> Parameter: - # Find uprating last date, corresponding to first date on or after - # last cadence "end" date defined within uprating_parameter + # Measure expected output size - in future, this will depend upon + # "interval" from cadence_options, but for now, it's number of individual + # years from start to end + ITERATIONS = last_date.year - first_date.year + 1 - # Measure expected output size + edited_uprater_data = {} + start_offset = cadence_options["enactment"]["year"] - cadence_options["start"]["year"] + end_offset = cadence_options["enactment"]["year"] - cadence_options["end"]["year"] # Within the range of this output size... - # + for i in range(ITERATIONS): - # Clone parameter's value list - # param_values: ParameterNode = parameter.values_list.clone() - uprater_values = [] - for i in range(len(uprating_parameter.values_list)): - uprater_values.append(uprating_parameter.values_list[i].clone()) + # Find uprater value at cadence start + start_value = uprating_parameter(instant(( + first_date.year + i - start_offset, + cadence_options["start"]["month"], + cadence_options["start"]["day"] + ))) - # There's no guarantee of a particular order for the value list's items; - # sort the list to ensure newest item is first, akin to defined order elsewhere in repo - uprater_values.sort( - key=lambda x: x.instant_str, reverse=True - ) + # Find uprater value at cadence end + end_value = uprating_parameter(instant(( + first_date.year + i - end_offset, + cadence_options["end"]["month"], + cadence_options["end"]["day"] + ))) + + # Find difference of these values + difference = end_value / start_value + + # Apply that at enactment date for this iteration + edited_uprater_data[str(first_date.offset(i, "year"))] = difference - pass + return Parameter(uprating_parameter.name, edited_uprater_data) def construct_cadence_options(cadence_settings: dict, parameter: Parameter) -> dict: diff --git a/tests/core/parameters/operations/test_uprating.py b/tests/core/parameters/operations/test_uprating.py index 52623739..a7a3668c 100644 --- a/tests/core/parameters/operations/test_uprating.py +++ b/tests/core/parameters/operations/test_uprating.py @@ -157,6 +157,7 @@ def test_parameter_uprating_with_cadence(): # Interpolate halfway assert uprated.to_be_uprated("2018-04-01") == 6 + assert uprated.to_be_uprated("2019-04-01") == 3 def test_parameter_uprating_with_cadence_malformed_syntax(): """