mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-08-25 15:01:14 +00:00
backport visualize v3 (#279)
* backport visualize v3 * test backport * compare file * old test files removed
This commit is contained in:
@@ -11,6 +11,7 @@ from akkudoktoreos.optimization.genetic import (
|
||||
OptimizeResponse,
|
||||
optimization_problem,
|
||||
)
|
||||
from akkudoktoreos.utils.visualize import prepare_visualize
|
||||
|
||||
DIR_TESTDATA = Path(__file__).parent / "testdata"
|
||||
|
||||
@@ -67,16 +68,12 @@ def test_optimize(
|
||||
|
||||
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
|
||||
@@ -95,4 +92,4 @@ def test_optimize(
|
||||
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()
|
||||
|
@@ -1,32 +1,42 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from matplotlib.testing.compare import compare_images
|
||||
|
||||
from akkudoktoreos.config import AppConfig
|
||||
from akkudoktoreos.visualize import visualisiere_ergebnisse
|
||||
from akkudoktoreos.config import get_working_dir, load_config
|
||||
|
||||
filename = "example_report.pdf"
|
||||
|
||||
working_dir = get_working_dir()
|
||||
config = load_config(working_dir)
|
||||
output_dir = config.working_dir / config.directories.output
|
||||
|
||||
# If self.filename is already a valid path, use it; otherwise, combine it with output_dir
|
||||
if os.path.isabs(filename):
|
||||
output_file = filename
|
||||
else:
|
||||
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, tmp_config: AppConfig):
|
||||
with open(DIR_TESTDATA / fn_in, "r") as f:
|
||||
input_data = json.load(f)
|
||||
visualisiere_ergebnisse(config=tmp_config, **input_data)
|
||||
output_file: Path = tmp_config.working_dir / tmp_config.directories.output / fn_out
|
||||
def test_generate_pdf_main():
|
||||
# Delete the old generated file if it exists
|
||||
if os.path.isfile(output_file):
|
||||
os.remove(output_file)
|
||||
|
||||
assert output_file.is_file()
|
||||
assert (
|
||||
compare_images(
|
||||
str(output_file),
|
||||
str(DIR_IMAGEDATA / fn_out_base),
|
||||
0,
|
||||
)
|
||||
is None
|
||||
)
|
||||
# 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)
|
||||
|
||||
# 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}"
|
||||
|
BIN
tests/testdata/images/visualize_base_output_1.pdf
vendored
BIN
tests/testdata/images/visualize_base_output_1.pdf
vendored
Binary file not shown.
BIN
tests/testdata/test_example_report.pdf
vendored
Normal file
BIN
tests/testdata/test_example_report.pdf
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user