diff --git a/src/planscape/impacts/services.py b/src/planscape/impacts/services.py index 94188454b..0652c3144 100644 --- a/src/planscape/impacts/services.py +++ b/src/planscape/impacts/services.py @@ -245,10 +245,14 @@ def generate_impact_results_data_to_plot( .annotate( value_dividend=Sum(F("value") * F("stand_count")), baseline_dividend=Sum(F("baseline") * F("stand_count")), - delta_dividend=Sum(F("delta") * F("stand_count")), + sum_baselines=Sum("baseline"), divisor=Sum("stand_count"), ) .annotate( + delta=Case( + When(sum_baselines=0, then=None), + default=((F("sum_baselines") - Sum("value")) / F("sum_baselines")), + ), value=Case( When(divisor=0, then=None), default=(F("value_dividend") / F("divisor")), @@ -257,10 +261,6 @@ def generate_impact_results_data_to_plot( When(divisor=0, then=None), default=(F("baseline_dividend") / F("divisor")), ), - delta=Case( - When(divisor=0, then=None), - default=(F("delta_dividend") / F("divisor")), - ), ) ) @@ -275,7 +275,7 @@ def generate_impact_results_data_to_plot( for value in values: value.pop("value_dividend") value.pop("baseline_dividend") - value.pop("delta_dividend") + value.pop("sum_baselines") value.pop("divisor") return values diff --git a/src/planscape/impacts/tests/test_services.py b/src/planscape/impacts/tests/test_services.py index 3431dfd4d..d7adb67ce 100644 --- a/src/planscape/impacts/tests/test_services.py +++ b/src/planscape/impacts/tests/test_services.py @@ -597,7 +597,7 @@ def test_generate_data_to_plot__filter_by_actions(self): self.assertIsNotNone(item.get("delta")) self.assertIsNone(item.get("value_dividend")) self.assertIsNone(item.get("baseline_dividend")) - self.assertIsNone(item.get("delta_dividend")) + self.assertIsNone(item.get("sum_baselines")) self.assertIsNone(item.get("divisor")) def test_empty_results(self): @@ -626,6 +626,9 @@ def test_project_area_with_no_treatment(self): aggregation=ImpactVariableAggregation.MEAN, action=None, stand_count=0, + value=0, + baseline=0, + delta=0, ) input_variables = [