Skip to content

Commit

Permalink
Adding baseline and delta to project area chart
Browse files Browse the repository at this point in the history
  • Loading branch information
roquebetioljr committed Dec 18, 2024
1 parent f96681a commit 05dad15
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/planscape/impacts/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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")),
),
)
)

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/planscape/impacts/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 05dad15

Please sign in to comment.