2024-11-10 23:22:30 +01:00
|
|
|
from pathlib import Path
|
2026-11-23 20:23:25 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2024-11-10 23:22:30 +01:00
|
|
|
|
2026-11-23 20:23:25 +00:00
|
|
|
import numpy as np
|
|
|
|
|
import pytest
|
2024-11-10 23:22:30 +01:00
|
|
|
from matplotlib.testing.compare import compare_images
|
|
|
|
|
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.core.coreabc import get_ems
|
|
|
|
|
from akkudoktoreos.optimization.genetic0 import genetic0prepare
|
|
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0optimizer import Genetic0Optimizer
|
2026-08-01 12:45:19 +02:00
|
|
|
from akkudoktoreos.optimization.genetic0.genetic0visualize import (
|
|
|
|
|
genetic0_generate_example_report,
|
2026-11-23 20:23:25 +00:00
|
|
|
genetic0_prepare_visualize,
|
2026-08-01 12:45:19 +02:00
|
|
|
)
|
2026-11-23 20:23:25 +00:00
|
|
|
from akkudoktoreos.utils.datetimeutil import to_datetime
|
2024-12-24 13:10:31 +01:00
|
|
|
|
|
|
|
|
filename = "example_report.pdf"
|
|
|
|
|
|
2024-11-10 23:22:30 +01:00
|
|
|
|
2026-08-01 12:45:19 +02:00
|
|
|
DIR_TESTDATA = Path(__file__).parent / "testdata" / "genetic0"
|
2024-12-24 13:10:31 +01:00
|
|
|
reference_file = DIR_TESTDATA / "test_example_report.pdf"
|
2026-08-01 12:45:19 +02:00
|
|
|
reference_png_file = DIR_TESTDATA / "test_example_report_pdf.png"
|
2026-07-15 16:38:53 +02:00
|
|
|
output_file = DIR_TESTDATA / "test_example_report_new.pdf"
|
2026-08-01 12:45:19 +02:00
|
|
|
output_png_file = DIR_TESTDATA / "test_example_report_new_pdf.png"
|
|
|
|
|
|
2024-11-10 23:22:30 +01:00
|
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_generate_pdf_example(config_eos):
|
2024-12-29 18:42:49 +01:00
|
|
|
"""Test generation of example visualization report."""
|
2024-12-30 13:41:39 +01:00
|
|
|
# Generate PDF
|
2026-08-01 12:45:19 +02:00
|
|
|
genetic0_generate_example_report(filename=str(output_file))
|
2024-11-11 21:38:13 +01:00
|
|
|
|
2024-12-24 13:10:31 +01:00
|
|
|
# Check if the file exists
|
2024-12-30 13:41:39 +01:00
|
|
|
assert output_file.exists()
|
2024-12-24 13:10:31 +01:00
|
|
|
|
|
|
|
|
# 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}"
|
2026-08-01 12:45:19 +02:00
|
|
|
|
|
|
|
|
# Everything passed, remove compare file
|
|
|
|
|
reference_png_file.unlink()
|
|
|
|
|
output_file.unlink()
|
|
|
|
|
output_png_file.unlink()
|
2026-11-23 20:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"prediction_hours, start",
|
|
|
|
|
[
|
|
|
|
|
(6, "2026-08-01T00:00:00+00:00"), # fewer than 12 hours shown
|
|
|
|
|
(30, "2026-08-01T14:00:00+00:00"), # not the 48 hours the labels once assumed
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
async def test_report_of_a_run_that_is_not_48_hours_long(config_eos, prediction_hours, start):
|
|
|
|
|
"""The report shows a run of any length the run accepts."""
|
|
|
|
|
config_eos.merge_settings_from_dict(
|
|
|
|
|
{
|
|
|
|
|
"general": {"timezone_override": "UTC"},
|
|
|
|
|
"prediction": {"hours": prediction_hours},
|
|
|
|
|
"optimization": {"genetic0": {"individuals": 10, "generations": 10, "seed": 1}},
|
|
|
|
|
"elecfee": {"provider": None},
|
|
|
|
|
"devices": {
|
|
|
|
|
"max_batteries": 0,
|
|
|
|
|
"max_electric_vehicles": 0,
|
|
|
|
|
"max_inverters": 0,
|
|
|
|
|
"max_home_appliances": 0,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
get_ems().set_start_datetime(to_datetime(start, in_timezone="UTC"))
|
|
|
|
|
|
|
|
|
|
async def read_array(*, start_datetime, end_datetime, interval, **kwargs):
|
|
|
|
|
slots = int((end_datetime - start_datetime).total_seconds() / interval.total_seconds())
|
|
|
|
|
return np.full(slots, 0.0003)
|
|
|
|
|
|
|
|
|
|
prediction = Mock(
|
|
|
|
|
update_data=AsyncMock(),
|
|
|
|
|
key_to_array=AsyncMock(side_effect=read_array),
|
|
|
|
|
key_to_raw_series=AsyncMock(side_effect=KeyError("no tariff")),
|
|
|
|
|
record_keys=[key for key, _ in genetic0prepare.PRESENTATION_PREDICTIONS],
|
|
|
|
|
)
|
|
|
|
|
optimizer = Genetic0Optimizer()
|
|
|
|
|
with patch.object(genetic0prepare, "get_prediction", return_value=prediction):
|
|
|
|
|
parameters = await optimizer.prepare(
|
|
|
|
|
get_ems().start_datetime, get_ems().observation_datetime, None
|
|
|
|
|
)
|
|
|
|
|
result = optimizer.run(parameters)
|
|
|
|
|
|
|
|
|
|
assert genetic0_prepare_visualize(parameters, result).startswith(b"%PDF-")
|