Pics or it didn't happen (#402)

* inverter added

* png creation

* save svg into cache folder

* mypy

* comment
This commit is contained in:
Normann 2025-01-26 18:27:09 +01:00 committed by GitHub
parent 6516455071
commit 90688a36f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 12 deletions

View File

@ -164,6 +164,9 @@ def prepare_optimization_real_parameters() -> OptimizationParameters:
"max_charge_power_w": 11040,
"initial_soc_percentage": 5,
},
"inverter": {
"max_power_wh": 10000,
},
"temperature_forecast": temperature_forecast,
"start_solution": start_solution,
}
@ -318,6 +321,9 @@ def prepare_optimization_parameters() -> OptimizationParameters:
"max_charge_power_w": 11040,
"initial_soc_percentage": 5,
},
"inverter": {
"max_power_wh": 10000,
},
"temperature_forecast": temperature_forecast,
"start_solution": start_solution,
}

View File

@ -24,7 +24,12 @@ matplotlib.use(
class VisualizationReport(ConfigMixin):
def __init__(self, filename: str = "visualization_results.pdf", version: str = "0.0.1") -> None:
def __init__(
self,
filename: str = "visualization_results.pdf",
version: str = "0.0.1",
create_img: bool = True,
) -> None:
# Initialize the report with a given filename and empty groups
self.filename = filename
self.groups: list[list[Callable[[], None]]] = [] # Store groups of charts
@ -36,10 +41,21 @@ class VisualizationReport(ConfigMixin):
self.current_time = to_datetime(
as_string="YYYY-MM-DD HH:mm:ss", in_timezone=self.config.general.timezone
)
self.create_img = create_img
def add_chart_to_group(self, chart_func: Callable[[], None]) -> None:
"""Add a chart function to the current group."""
def add_chart_to_group(self, chart_func: Callable[[], None], title: str | None) -> None:
"""Add a chart function to the current group and save it as a PNG and SVG."""
self.current_group.append(chart_func)
if self.create_img and title:
server_output_dir = self.config.data_cache_path
server_output_dir.mkdir(parents=True, exist_ok=True)
fig, ax = plt.subplots()
chart_func()
plt.tight_layout() # Adjust the layout to ensure titles are not cut off
sanitized_title = "".join(c if c.isalnum() else "_" for c in title)
chart_filename_base = os.path.join(server_output_dir, f"chart_{sanitized_title}")
fig.savefig(f"{chart_filename_base}.svg")
plt.close(fig)
def finalize_group(self) -> None:
"""Finalize the current group and prepare for a new group."""
@ -195,7 +211,7 @@ class VisualizationReport(ConfigMixin):
# Ensure ax1 and ax2 are aligned
# assert ax1.get_xlim() == ax2.get_xlim(), "ax1 and ax2 are not aligned"
self.add_chart_to_group(chart) # Add chart function to current group
self.add_chart_to_group(chart, title) # Add chart function to current group
def create_line_chart(
self,
@ -256,7 +272,7 @@ class VisualizationReport(ConfigMixin):
plt.grid(True) # Show grid
plt.xlim(x[0] - 0.5, x[-1] + 0.5) # Adjust x-limits
self.add_chart_to_group(chart) # Add chart function to current group
self.add_chart_to_group(chart, title) # Add chart function to current group
def create_scatter_plot(
self,
@ -278,7 +294,7 @@ class VisualizationReport(ConfigMixin):
plt.colorbar(scatter, label="Constraint") # Add colorbar if color data is provided
plt.grid(True) # Show grid
self.add_chart_to_group(chart) # Add chart function to current group
self.add_chart_to_group(chart, title) # Add chart function to current group
def create_bar_chart(
self,
@ -328,7 +344,7 @@ class VisualizationReport(ConfigMixin):
plt.grid(True, zorder=0) # Show grid in the background
plt.xlim(-0.5, len(labels) - 0.5) # Set x-axis limits
self.add_chart_to_group(chart) # Add chart function to current group
self.add_chart_to_group(chart, title) # Add chart function to current group
def create_violin_plot(
self, data_list: list[np.ndarray], labels: list[str], title: str, xlabel: str, ylabel: str
@ -343,7 +359,7 @@ class VisualizationReport(ConfigMixin):
plt.ylabel(ylabel) # Set y-axis label
plt.grid(True) # Show grid
self.add_chart_to_group(chart) # Add chart function to current group
self.add_chart_to_group(chart, title) # Add chart function to current group
def add_text_page(self, text: str, title: Optional[str] = None, fontsize: int = 12) -> None:
"""Add a page with text content to the PDF."""
@ -362,7 +378,7 @@ class VisualizationReport(ConfigMixin):
self.pdf_pages.savefig(fig) # Save the figure as a page in the PDF
plt.close(fig) # Close the figure to free up memory
self.add_chart_to_group(chart) # Treat the text page as a "chart" in the group
self.add_chart_to_group(chart, title) # Treat the text page as a "chart" in the group
def add_json_page(
self, json_obj: dict, title: Optional[str] = None, fontsize: int = 12
@ -400,7 +416,7 @@ class VisualizationReport(ConfigMixin):
self.pdf_pages.savefig(fig) # Save the figure as a page in the PDF
plt.close(fig) # Close the figure to free up memory
self.add_chart_to_group(chart) # Treat the JSON page as a "chart" in the group
self.add_chart_to_group(chart, title) # Treat the JSON page as a "chart" in the group
def generate_pdf(self) -> None:
"""Generate the PDF report with all the added chart groups."""
@ -503,7 +519,7 @@ def prepare_visualize(
report.create_line_chart_date(
next_full_hour_date, # start_date
[parameters.ems.strompreis_euro_pro_wh],
# title="Electricity Price", # not enough space
title="Electricity Price",
# xlabel="Date", # not enough space
ylabel="Electricity Price (€/Wh)",
x2label=None, # not enough space
@ -538,7 +554,7 @@ def prepare_visualize(
report.create_scatter_plot(
extra_data["verluste"],
extra_data["bilanz"],
title="",
title="Scatter Plot",
xlabel="losses",
ylabel="balance",
c=extra_data["nebenbedingung"],