Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSYE-568: Change hierarchy self consumption into percentage values #1800

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/gsy_e/gsy_e_core/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"status": "status",
"trade_profile": "trade_profile",
"imported_exported_energy": "imported_exported_energy",
"hierarchy_self_consumption": "hierarchy_self_consumption",
"hierarchy_self_consumption_percent": "hierarchy_self_consumption_percent",
}


Expand Down
38 changes: 30 additions & 8 deletions src/gsy_e/gsy_e_core/sim_results/endpoint_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
from collections import defaultdict
from typing import TYPE_CHECKING, Dict, Iterable, List
from math import isclose

from gsy_framework.constants_limits import (
DATE_TIME_FORMAT,
Expand Down Expand Up @@ -94,7 +95,7 @@
self.status = ""
self.area_result_dict = self._create_area_tree_dict(area)
self.flattened_area_core_stats_dict = {}
self.hierarchy_self_consumption: Dict[int, float] = {}
self.hierarchy_self_consumption_percent: Dict[int, float] = {}
self.simulation_progress = {
"eta_seconds": 0,
"elapsed_time_seconds": 0,
Expand Down Expand Up @@ -135,7 +136,7 @@
"status": self.status,
"progress_info": self.simulation_progress,
"simulation_state": self.simulation_state,
"hierarchy_self_consumption": self.hierarchy_self_consumption,
"hierarchy_self_consumption_percent": self.hierarchy_self_consumption_percent,
**self.results_handler.all_raw_results,
}

Expand Down Expand Up @@ -195,14 +196,35 @@
def create_hierarchy_stats(self, area: "AreaBase"):
"""Calculate hierarchy related statistics."""
hierarchy_self_consumption_list = {}
self._calc_hierarchy_stats(area, 0, hierarchy_self_consumption_list)
hierarchy_self_consumption = {}
self._calc_hierarchy_self_consumption(area, 0, hierarchy_self_consumption_list)
for level, consumption_list in hierarchy_self_consumption_list.items():
if len(consumption_list):
self.hierarchy_self_consumption[level] = sum(consumption_list) / len(
consumption_list
)
hierarchy_self_consumption[level] = sum(consumption_list) / len(consumption_list)
if (
len(hierarchy_self_consumption) > 0
and hierarchy_self_consumption[next(iter(hierarchy_self_consumption))] == 0
):
# early return in case there is no self consumpton at all (in order to not devide by 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# early return in case there is no self consumpton at all (in order to not devide by 0)
# early return in case there is no self consumption at all (in order to not devide by 0)

Nit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

Check warning on line 209 in src/gsy_e/gsy_e_core/sim_results/endpoint_buffer.py

View check run for this annotation

Codecov / codecov/patch

src/gsy_e/gsy_e_core/sim_results/endpoint_buffer.py#L209

Added line #L209 was not covered by tests

def _calc_hierarchy_stats(self, area: "AreaBase", level: int, results: Dict):
for level, self_consumption in hierarchy_self_consumption.items():
if level + 1 not in hierarchy_self_consumption:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that big of an issue, since it probably makes the solution simpler, however I think that hierarchy_self_consumption could be a list and not a dict, since the index / position in the list could be used as the level number. Regardless if you believe it is more explicit that way leave it as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not mind either way, so I changed it to be a list: 87ff961

# lowest level case:
self.hierarchy_self_consumption_percent[level] = (
self_consumption / hierarchy_self_consumption[0]
) * 100
else:
self.hierarchy_self_consumption_percent[level] = (
(self_consumption - hierarchy_self_consumption[level + 1])
/ hierarchy_self_consumption[0]
) * 100
if self.hierarchy_self_consumption_percent:
assert isclose(
sum(self.hierarchy_self_consumption_percent.values()), 100
), "Self consumption percentages do not sum up to 100%"
Comment on lines +220 to +222
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great assert, it will always confirm that this feature is working as expected, thanks!


def _calc_hierarchy_self_consumption(self, area: "AreaBase", level: int, results: Dict):
if not area.children:
return
kpis = self.results_handler.results_mapping["kpi"].ui_formatted_results
Expand All @@ -215,7 +237,7 @@
if level not in results:
results[level] = []
results[level].append(kpis[child.uuid]["self_consumption"])
self._calc_hierarchy_stats(child, level + 1, results)
self._calc_hierarchy_self_consumption(child, level + 1, results)

def _generate_result_report(self) -> Dict:
"""Create dict that contains all statistics that are sent to the gsy-web."""
Expand Down
10 changes: 5 additions & 5 deletions tests/test_endpoint_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_generate_json_report_returns_successfully(self, general_setup):
endpoint_buffer.results_handler = results_handler_mock

assert endpoint_buffer.generate_json_report() == {
"hierarchy_self_consumption": {},
"hierarchy_self_consumption_percent": {},
"job_id": "JOB_1",
"random_seed": 41,
"status": "",
Expand Down Expand Up @@ -353,11 +353,11 @@ def test_update_stats_spot_markets_updates_successfully(self, general_setup):
def _create_kpis(area):
kpis = defaultdict(dict)
for n_city, city in enumerate(area.children):
kpis[city.uuid]["self_consumption"] = n_city * 10
kpis[city.uuid]["self_consumption"] = (n_city + 2) * 10
for n_community, community in enumerate(city.children):
kpis[community.uuid]["self_consumption"] = (n_community + 1) * 10
for n_house, house in enumerate(community.children):
kpis[house.uuid]["self_consumption"] = (n_house + 2) * 10
kpis[house.uuid]["self_consumption"] = n_house * 10
return kpis

def test_create_hierarchy_stats_returns_correct_results(self, setup_multiple_levels):
Expand All @@ -368,7 +368,7 @@ def test_create_hierarchy_stats_returns_correct_results(self, setup_multiple_lev
endpoint_buffer.results_handler.results_mapping["kpi"].performance_indices_redis = kpis
endpoint_buffer.create_hierarchy_stats(setup_multiple_levels)

assert endpoint_buffer.hierarchy_self_consumption == {0: 5.0, 1: 15.0, 2: 25.0}
assert endpoint_buffer.hierarchy_self_consumption_percent == {0: 40, 1: 40, 2: 20}


class TestSimulationEndpointBufferForward:
Expand Down Expand Up @@ -512,7 +512,7 @@ def test_generate_json_report_returns_successfully(self, forward_setup):
endpoint_buffer.results_handler = results_handler_mock

assert endpoint_buffer.generate_json_report() == {
"hierarchy_self_consumption": {},
"hierarchy_self_consumption_percent": {},
"job_id": "JOB_1",
"random_seed": 41,
"status": "",
Expand Down
Loading