Visualize reworked v2 (#267)

* initial commit

* Delete duplicate openapi.json

* revert temp cahnges

* test fixed

* mypy fixes

* mypy fixed

* Financial Overview included

* test data save path

* almost done

* mypy fix

* Update visualize.py

* ruff fix

* config, label

* improved start_hour vis

* fix

* fix2
This commit is contained in:
Normann
2024-12-24 13:10:31 +01:00
committed by GitHub
parent 5f898e8aab
commit bec24588e1
8 changed files with 552 additions and 574 deletions

View File

@@ -11,6 +11,9 @@ from akkudoktoreos.optimization.genetic import (
OptimizeResponse,
optimization_problem,
)
from akkudoktoreos.utils.visualize import (
prepare_visualize, # Import the new prepare_visualize
)
DIR_TESTDATA = Path(__file__).parent / "testdata"
@@ -64,16 +67,12 @@ def test_optimize(fn_in: str, fn_out: str, ngen: int, is_full_run: bool):
visualize_filename = str((DIR_TESTDATA / f"new_{fn_out}").with_suffix(".pdf"))
def visualize_to_file(*args, **kwargs):
from akkudoktoreos.visualize import visualisiere_ergebnisse
# Write test output pdf to file, so we can look at it manually
kwargs["filename"] = visualize_filename
return visualisiere_ergebnisse(*args, **kwargs)
with patch(
"akkudoktoreos.optimization.genetic.visualisiere_ergebnisse", side_effect=visualize_to_file
) as visualisiere_ergebnisse_patch:
"akkudoktoreos.utils.visualize.prepare_visualize",
side_effect=lambda parameters, results, *args, **kwargs: prepare_visualize(
parameters, results, filename=visualize_filename, **kwargs
),
) as prepare_visualize_patch:
# Call the optimization function
ergebnis = opt_class.optimierung_ems(
parameters=input_data, start_hour=start_hour, ngen=ngen
@@ -92,4 +91,4 @@ def test_optimize(fn_in: str, fn_out: str, ngen: int, is_full_run: bool):
compare_dict(ergebnis.model_dump(), expected_result.model_dump())
# The function creates a visualization result PDF as a side-effect.
visualisiere_ergebnisse_patch.assert_called_once()
prepare_visualize_patch.assert_called_once()

View File

@@ -1,36 +1,36 @@
import json
import os
import subprocess
from pathlib import Path
import pytest
from matplotlib.testing.compare import compare_images
from akkudoktoreos.config.config import get_config
from akkudoktoreos.visualize import visualisiere_ergebnisse
filename = "example_report.pdf"
config = get_config()
output_dir = config.data_output_path
output_dir.mkdir(parents=True, exist_ok=True)
output_file = os.path.join(output_dir, filename)
DIR_TESTDATA = Path(__file__).parent / "testdata"
DIR_IMAGEDATA = DIR_TESTDATA / "images"
reference_file = DIR_TESTDATA / "test_example_report.pdf"
@pytest.mark.parametrize(
"fn_in, fn_out, fn_out_base",
[("visualize_input_1.json", "visualize_output_1.pdf", "visualize_base_output_1.pdf")],
)
def test_visualisiere_ergebnisse(fn_in, fn_out, fn_out_base):
with open(DIR_TESTDATA / fn_in, "r") as f:
input_data = json.load(f)
visualisiere_ergebnisse(**input_data)
def test_generate_pdf_main():
# Delete the old generated file if it exists
if os.path.isfile(output_file):
os.remove(output_file)
config = get_config()
output_dir = config.data_output_path
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir.joinpath(fn_out)
# Execute the __main__ block of visualize.py by running it as a script
script_path = Path(__file__).parent.parent / "src" / "akkudoktoreos" / "utils" / "visualize.py"
subprocess.run(["python", str(script_path)], check=True)
assert output_file.is_file()
assert (
compare_images(
str(output_file),
str(DIR_IMAGEDATA / fn_out_base),
0,
)
is None
)
# Check if the file exists
assert os.path.isfile(output_file)
# Compare the generated file with the reference file
comparison = compare_images(str(reference_file), str(output_file), tol=0)
# Assert that there are no differences
assert comparison is None, f"Images differ: {comparison}"

Binary file not shown.

BIN
tests/testdata/test_example_report.pdf vendored Normal file

Binary file not shown.

View File

@@ -1,175 +0,0 @@
{
"gesamtlast": [
676.71, 876.19, 527.13, 468.88, 531.38, 517.95, 483.15, 472.28, 1011.68,
995.0, 1053.07, 1063.91, 1320.56, 1132.03, 1163.67, 1176.82, 1216.22,
1103.78, 1129.12, 1178.71, 1050.98, 988.56, 912.38, 704.61, 516.37, 868.05,
694.34, 608.79, 556.31, 488.89, 506.91, 804.89, 1141.98, 1056.97, 992.46,
1155.99, 827.01, 1257.98, 1232.67, 871.26, 860.88, 1158.03, 1222.72,
1221.04, 949.99, 987.01, 733.99, 592.97
],
"pv_forecast": [
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5000.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
],
"strompreise": [
0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001,
0.00001, 0.00001, 0.001, 0.00005, 0.00005, 0.00005, 0.00005, 0.001, 0.001,
0.001, 0.001, 0.001, 0.00001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001
],
"ergebnisse": {
"Last_Wh_pro_Stunde": [
12493.71, 10502.19, 12775.13, 15356.88, 11468.38, 4037.95, 6047.15,
3112.2799999999997, 3211.68, 995.0, 1053.07, 1063.91, 1320.56, 1132.03,
1163.67, 1176.82, 1216.22, 1103.78, 1129.12, 1178.71, 1050.98, 988.56,
2035.7436363636361, 704.61, 516.37, 868.05, 694.34, 608.79, 556.31,
488.89, 506.91, 804.89, 1141.98, 1056.97, 992.46, 1155.99, 827.01,
1257.98, 1232.67, 871.26, 860.88, 1158.03, 1222.72, 1221.04, 949.99,
987.01, 733.99, 592.97
],
"Netzeinspeisung_Wh_pro_Stunde": [
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3679.44, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
],
"Netzbezug_Wh_pro_Stunde": [
12493.71, 10502.19, 12775.13, 15356.88, 11468.38, 4037.95, 6047.15,
3112.2799999999997, 3211.68, 995.0, 1053.07, 1063.91, 0.0, 1132.03,
1163.67, 1176.82, 1216.22, 1103.78, 1129.12, 1178.71, 1050.98, 0.0,
2035.7436363636361, 704.61, 516.37, 868.05, 694.34, 608.79, 556.31,
488.89, 506.91, 804.89, 1141.98, 1056.97, 992.46, 1155.99, 827.01,
1257.98, 1232.67, 871.26, 860.88, 1158.03, 0.0, 1221.04, 0.0, 0.0, 0.0,
592.97
],
"Kosten_Euro_pro_Stunde": [
0.1249371, 0.10502190000000002, 0.1277513, 0.1535688, 0.1146838,
0.0403795, 0.060471500000000004, 0.0311228, 0.0321168, 0.00995, 1.05307,
0.05319550000000001, 0.0, 0.0566015, 0.058183500000000006, 1.17682,
1.21622, 1.10378, 1.12912, 1.1787100000000001, 0.010509800000000001, 0.0,
2.035743636363636, 0.7046100000000001, 0.51637, 0.86805,
0.6943400000000001, 0.6087899999999999, 0.55631, 0.48889,
0.5069100000000001, 0.80489, 1.14198, 1.05697, 0.99246, 1.15599, 0.82701,
1.25798, 1.2326700000000002, 0.87126, 0.86088, 1.15803, 0.0, 1.22104, 0.0,
0.0, 0.0, 0.59297
],
"akku_soc_pro_stunde": [
25.0, 31.666666666666664, 38.333333333333336, 55.00000000000001,
61.66666666666667, 75.0, 81.66666666666667, 91.66666666666666, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 95.7448347107438, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 94.73691460055097, 94.73691460055097,
90.64777031680441, 86.39927685950414, 83.23988464187329, 83.23988464187329
],
"Einnahmen_Euro_pro_Stunde": [
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2575608,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0
],
"Gesamtbilanz_Euro": 27.732796636363638,
"EAuto_SoC_pro_Stunde": [
30.294999999999998, 43.405, 60.885, 78.365, 93.66, 93.66, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0
],
"Gesamteinnahmen_Euro": 0.2575608,
"Gesamtkosten_Euro": 27.990357436363638,
"Verluste_Pro_Stunde": [
843.0, 654.0, 792.0, 1152.0, 723.0, 480.0, 440.2105263157896, 360.0,
300.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
134.80363636363631, 153.18595041322305, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
166.73454545454547, 0.0, 129.54409090909098, 134.59227272727276,
100.08954545454549, 0.0
],
"Gesamt_Verluste": 6563.160567638104,
"Home_appliance_wh_per_hour": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
},
"ac": [
0.6, 0.4, 0.4, 1.0, 0.4, 0.8, 0.4, 0.6, 1.0, 0.2, 0.2, 0.2, 0.6, 0.0, 0.2,
0.6, 0.0, 0.0, 0.8, 0.8, 0.4, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.2, 0.8,
0.2, 0.4, 0.6, 1.0, 0.0, 1.0, 0.8, 0.4, 0.4, 1.0, 0.2, 0.6, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0
],
"dc": [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
],
"discharge": [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0
],
"temperature": [
18.3, 17.8, 16.9, 16.2, 15.6, 15.1, 14.6, 14.2, 14.3, 14.8, 15.7, 16.7,
17.4, 18.0, 18.6, 19.2, 19.1, 18.7, 18.5, 17.7, 16.2, 14.6, 13.6, 13.0,
12.6, 12.2, 11.7, 11.6, 11.3, 11.0, 10.7, 10.2, 11.4, 14.4, 16.4, 18.3,
19.5, 20.7, 21.9, 22.7, 23.1, 23.1, 22.8, 21.8, 20.2, 19.1, 18.0, 17.4
],
"start_hour": 0,
"einspeiseverguetung_euro_pro_wh": [
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007,
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007,
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007,
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007,
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007,
0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007, 0.00007
],
"filename": "visualize_output_1.pdf",
"extra_data": null
}