From cf27ddfb566ceb2fd3ed24b15e990581bf90a58e Mon Sep 17 00:00:00 2001 From: Normann Date: Mon, 7 Oct 2024 18:35:01 +0200 Subject: [PATCH 1/4] remove unused function replace_nan_with_none(data) is not needed anymore since we don't have any NaN values. Another 13% speed increase. --- src/akkudoktoreos/class_ems.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/akkudoktoreos/class_ems.py b/src/akkudoktoreos/class_ems.py index 6dd75f1..b2670d9 100644 --- a/src/akkudoktoreos/class_ems.py +++ b/src/akkudoktoreos/class_ems.py @@ -4,24 +4,6 @@ import numpy as np -def replace_nan_with_none( - data: Union[np.ndarray, dict, list, float], -) -> Union[List, dict, float, None]: - if data is None: - return None - if isinstance(data, np.ndarray): - # Use numpy vectorized approach - return np.where(np.isnan(data), None, data).tolist() - elif isinstance(data, dict): - return {key: replace_nan_with_none(value) for key, value in data.items()} - elif isinstance(data, list): - return [replace_nan_with_none(element) for element in data] - elif isinstance(data, (float, np.floating)) and np.isnan(data): - return None - else: - return data - - class EnergieManagementSystem: def __init__( self, @@ -156,4 +138,4 @@ def simuliere(self, start_stunde: int) -> dict: "Haushaltsgeraet_wh_pro_stunde": haushaltsgeraet_wh_pro_stunde, } - return replace_nan_with_none(out) + return out From 6b92aaddbe6f8d276350e64e9e62bf73f31996bb Mon Sep 17 00:00:00 2001 From: Normann Date: Tue, 8 Oct 2024 10:21:55 +0200 Subject: [PATCH 2/4] set current hour value None in class_ems.py --- src/akkudoktoreos/class_ems.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/akkudoktoreos/class_ems.py b/src/akkudoktoreos/class_ems.py index b2670d9..404766b 100644 --- a/src/akkudoktoreos/class_ems.py +++ b/src/akkudoktoreos/class_ems.py @@ -138,4 +138,8 @@ def simuliere(self, start_stunde: int) -> dict: "Haushaltsgeraet_wh_pro_stunde": haushaltsgeraet_wh_pro_stunde, } + # set the first value to None so no action will be done in the current hour + out["Last_Wh_pro_Stunde"][0] = None + out["Netzeinspeisung_Wh_pro_Stunde"][0] = None + out["Netzbezug_Wh_pro_Stunde"][0] = None return out From 3e54f0aac467db56019e669dfa8e5677969b8810 Mon Sep 17 00:00:00 2001 From: Normann Date: Tue, 8 Oct 2024 10:25:45 +0200 Subject: [PATCH 3/4] revert check in test for ems --- tests/test_class_ems.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_class_ems.py b/tests/test_class_ems.py index b7979e5..ed24f09 100644 --- a/tests/test_class_ems.py +++ b/tests/test_class_ems.py @@ -279,15 +279,15 @@ def test_simulation(create_ems_instance): # Verify that the value at index 0 is 'None' assert ( - result["Last_Wh_pro_Stunde"][0] == 0.0 + result["Last_Wh_pro_Stunde"][0] is None ), "The value at index 0 of 'Last_Wh_pro_Stunde' should be None." # Check that 'Netzeinspeisung_Wh_pro_Stunde' and 'Netzbezug_Wh_pro_Stunde' are consistent assert ( - result["Netzeinspeisung_Wh_pro_Stunde"][0] == 0.0 + result["Netzeinspeisung_Wh_pro_Stunde"][0] is None ), "The value at index 0 of 'Netzeinspeisung_Wh_pro_Stunde' should be None." assert ( - result["Netzbezug_Wh_pro_Stunde"][0] == 0.0 + result["Netzbezug_Wh_pro_Stunde"][0] is None ), "The value at index 0 of 'Netzbezug_Wh_pro_Stunde' should be None." assert ( result["Netzbezug_Wh_pro_Stunde"][1] == 21679.13 From 52b87bc1fb03181d1d099491ac7c08d9e61bdf77 Mon Sep 17 00:00:00 2001 From: Normann Date: Tue, 8 Oct 2024 10:38:08 +0200 Subject: [PATCH 4/4] convert np arrys to lists before changing --- src/akkudoktoreos/class_ems.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/akkudoktoreos/class_ems.py b/src/akkudoktoreos/class_ems.py index 404766b..f20443a 100644 --- a/src/akkudoktoreos/class_ems.py +++ b/src/akkudoktoreos/class_ems.py @@ -138,8 +138,22 @@ def simuliere(self, start_stunde: int) -> dict: "Haushaltsgeraet_wh_pro_stunde": haushaltsgeraet_wh_pro_stunde, } - # set the first value to None so no action will be done in the current hour - out["Last_Wh_pro_Stunde"][0] = None - out["Netzeinspeisung_Wh_pro_Stunde"][0] = None - out["Netzbezug_Wh_pro_Stunde"][0] = None + # List output keys where the first element needs to be changed to None + keys_to_modify = [ + "Last_Wh_pro_Stunde", + "Netzeinspeisung_Wh_pro_Stunde", + "Netzbezug_Wh_pro_Stunde", + ] + + # Loop through each key in the list + for key in keys_to_modify: + # Convert the NumPy array to a list + element_list = out[key].tolist() + + # Change the first value to None + element_list[0] = None + + # Assign the modified list back to the dictionary + out[key] = element_list + return out