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

Bug/fe 478 In crease accuracy when rounding results for the UI #510

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 gsy_framework/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def get_area_uuid_name_mapping(area_dict, results):

def round_floats_for_ui(number):
"""Round the given number using the scale required by the UI."""
return round(number, 3)
return round(number, 5)


def round_prices_to_cents(number):
Expand Down
52 changes: 27 additions & 25 deletions tests/test_bills.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


class TestBills(unittest.TestCase):
# pylint: disable=protected-access

def setUp(self):
self.bills = MarketEnergyBills()
Expand All @@ -17,11 +18,11 @@ def tearDown(self):
@property
def _empty_bills(self):
return {
'bought': 0.0, 'sold': 0.0, 'spent': 0.0, 'earned': 0.0, 'total_energy': 0.0,
'total_cost': 0.0, 'market_fee': 0.0, 'type': 'Area'
"bought": 0.0, "sold": 0.0, "spent": 0.0, "earned": 0.0, "total_energy": 0.0,
"total_cost": 0.0, "market_fee": 0.0, "type": "Area"
}

def create_empty_results_with_children(self, uuid_or_name_list):
def _create_empty_results_with_children(self, uuid_or_name_list):
result_dict = {k: self._empty_bills for k in ACCUMULATED_KEYS_LIST}
result_dict.update({u: self._empty_bills for u in uuid_or_name_list})
return result_dict
Expand All @@ -41,12 +42,12 @@ def test_swap_children_uuids_to_names(self):
{"name": "house2", "uuid": house2_uuid, "parent_uuid": grid_uuid, "children": [
{"name": "pv2", "uuid": pv2_uuid, "parent_uuid": house2_uuid, "children": []},
]}
]
}
]
}
uuid_results = {
grid_uuid: self.create_empty_results_with_children([house1_uuid, house2_uuid]),
house1_uuid: self.create_empty_results_with_children([pv_uuid, load_uuid]),
house2_uuid: self.create_empty_results_with_children([pv2_uuid]),
grid_uuid: self._create_empty_results_with_children([house1_uuid, house2_uuid]),
house1_uuid: self._create_empty_results_with_children([pv_uuid, load_uuid]),
house2_uuid: self._create_empty_results_with_children([pv2_uuid]),
pv_uuid: self._empty_bills,
load_uuid: self._empty_bills,
pv2_uuid: self._empty_bills,
Expand All @@ -66,9 +67,9 @@ def test_restore_area_results_state(self):
load_uuid = str(uuid4())
pv2_uuid = str(uuid4())
name_results = {
grid_uuid: self.create_empty_results_with_children(["house1", "house2"]),
house1_uuid: self.create_empty_results_with_children(["pv", "load"]),
house2_uuid: self.create_empty_results_with_children(["pv2"]),
grid_uuid: self._create_empty_results_with_children(["house1", "house2"]),
house1_uuid: self._create_empty_results_with_children(["pv", "load"]),
house2_uuid: self._create_empty_results_with_children(["pv2"]),
pv_uuid: self._empty_bills,
load_uuid: self._empty_bills,
pv2_uuid: self._empty_bills,
Expand All @@ -81,22 +82,23 @@ def test_restore_area_results_state(self):
{"name": "house2", "uuid": house2_uuid, "parent_uuid": grid_uuid, "children": [
{"name": "pv2", "uuid": pv2_uuid, "parent_uuid": house2_uuid, "children": []},
]}
]
}
]
}

self.bills.restore_area_results_state(area_dict, name_results[grid_uuid])
assert_dicts_identical(self.bills.bills_redis_results[grid_uuid], name_results[grid_uuid])
assert_dicts_identical(self.bills.bills_results["grid"], name_results[grid_uuid])
assert_dicts_identical(self.bills.current_raw_bills[grid_uuid],
self.create_empty_results_with_children([house1_uuid, house2_uuid]))
self._create_empty_results_with_children(
[house1_uuid, house2_uuid]))

self.bills.restore_area_results_state(area_dict["children"][0],
name_results[house1_uuid])
assert_dicts_identical(self.bills.bills_redis_results[house1_uuid],
name_results[house1_uuid])
assert_dicts_identical(self.bills.bills_results["house1"], name_results[house1_uuid])
assert_dicts_identical(self.bills.current_raw_bills[house1_uuid],
self.create_empty_results_with_children([pv_uuid, load_uuid]))
self._create_empty_results_with_children([pv_uuid, load_uuid]))

self.bills.restore_area_results_state(area_dict["children"][1]["children"][0],
name_results[pv2_uuid])
Expand All @@ -119,12 +121,12 @@ def test_bills_local_format(self):
{"name": "house2", "uuid": house2_uuid, "parent_uuid": grid_uuid, "children": [
{"name": "pv2", "uuid": pv2_uuid, "parent_uuid": house2_uuid, "children": []},
]}
]
}
]
}
uuid_results = {
grid_uuid: self.create_empty_results_with_children(["house1", "house2"]),
house1_uuid: self.create_empty_results_with_children(["pv", "load"]),
house2_uuid: self.create_empty_results_with_children(["pv2"]),
grid_uuid: self._create_empty_results_with_children(["house1", "house2"]),
house1_uuid: self._create_empty_results_with_children(["pv", "load"]),
house2_uuid: self._create_empty_results_with_children(["pv2"]),
pv_uuid: self._empty_bills,
load_uuid: self._empty_bills,
pv2_uuid: self._empty_bills,
Expand All @@ -139,9 +141,9 @@ def test_bills_local_format(self):

def test_round_results_for_ui(self):
uuid_results = {
"1234": self.create_empty_results_with_children(["house1", "house2"]),
"2345": self.create_empty_results_with_children(["pv", "load"]),
"3456": self.create_empty_results_with_children(["pv2"]),
"1234": self._create_empty_results_with_children(["house1", "house2"]),
"2345": self._create_empty_results_with_children(["pv", "load"]),
"3456": self._create_empty_results_with_children(["pv2"]),
"4567": self._empty_bills,
"5678": self._empty_bills,
"6789": self._empty_bills,
Expand All @@ -152,7 +154,7 @@ def test_round_results_for_ui(self):
uuid_results["2345"]["pv"]["sold"] = 0.987
uuid_results["6789"]["earned"] = 12
results = self.bills._round_results_for_ui(uuid_results)
assert results["1234"]["house1"]["bought"] == 0.123
assert results["1234"]["house2"]["sold"] == 0.988
assert results["1234"]["house1"]["bought"] == 0.12346
assert results["1234"]["house2"]["sold"] == 0.98765
assert results["2345"]["pv"]["sold"] == 0.987
assert results["6789"]["earned"] == 12
8 changes: 4 additions & 4 deletions tests/test_sim_results/test_cumulative_bills.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ def test_update_cumulative_bills_works(self):
# Equals to penalty_energy * PV_PENALTY_RATE / 100.0
assert self._bills.cumulative_bills[seller_uuid]["penalties"] == 0.12 * 0.5
# Equals to the trade price since the PV is the seller
assert self._bills.cumulative_bills[seller_uuid]["earned"] == 0.002
assert self._bills.cumulative_bills[seller_uuid]["earned"] == 0.0019
# Equals to penalties - earned
assert isclose(self._bills.cumulative_bills[seller_uuid]["total"], 0.06-0.002)
assert isclose(self._bills.cumulative_bills[seller_uuid]["total"], 0.06 - 0.0019)

assert self._bills.cumulative_bills[buyer_uuid]["name"] == buyer_name
# Equal to the energy_requirement_kWh (consumption not bought)
assert self._bills.cumulative_bills[buyer_uuid]["penalty_energy"] == 0.05
# Equals to penalty_energy * LOAD_PENALTY_RATE / 100.0
assert self._bills.cumulative_bills[buyer_uuid]["penalties"] == 0.05 * 0.5
# Equals to the trade price since the Load is the buyer
assert self._bills.cumulative_bills[buyer_uuid]["spent_total"] == 0.002
assert self._bills.cumulative_bills[buyer_uuid]["spent_total"] == 0.0021
# Equals to penalties + spent
assert isclose(self._bills.cumulative_bills[buyer_uuid]["total"], 0.027)
assert isclose(self._bills.cumulative_bills[buyer_uuid]["total"], 0.0271)
Loading