# Copyright 2021 IRT Saint Exupéry, https://www.irt-saintexupery.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Contributors:
# INITIAL AUTHORS - initial API and implementation and/or initial
# documentation
# :author: Francois Gallard
# OTHER AUTHORS - MACROSCOPIC CHANGES
"""A factory to create or execute a post-processor from its class name."""
from __future__ import annotations
from typing import TYPE_CHECKING
from gemseo.algos.opt_problem import OptimizationProblem
from gemseo.core.base_factory import BaseFactory
from gemseo.post.opt_post_processor import OptPostProcessor
from gemseo.post.opt_post_processor import OptPostProcessorOptionType
if TYPE_CHECKING:
from pathlib import Path
[docs]
class PostFactory(BaseFactory):
"""Post-processing factory to run optimization post-processors.
List the available post-processors on the current configuration and execute them on
demand.
Work both from memory, from a ran optimization problem, and from disk, from a
serialized optimization problem.
"""
_CLASS = OptPostProcessor
_MODULE_NAMES = ("gemseo.post",)
def __init__(self) -> None: # noqa:D107
super().__init__()
self.executed_post = []
@property
def posts(self) -> list[str]:
"""The available post processors."""
return self.class_names
[docs]
def create(
self,
class_name: str,
opt_problem: OptimizationProblem,
**options: OptPostProcessorOptionType,
) -> OptPostProcessor:
"""Create a post-processor from its class name.
Args:
class_name: The name of the post-processor.
opt_problem: The optimization problem to be post-processed.
**options: The options of the post-processor.
"""
return super().create(class_name, opt_problem=opt_problem, **options)
[docs]
def execute(
self,
opt_problem: str | OptimizationProblem,
post_name: str,
save: bool = True,
show: bool = False,
file_path: str | Path | None = None,
directory_path: str | Path | None = None,
file_name: str | None = None,
file_extension: str | None = None,
**options: OptPostProcessorOptionType,
) -> OptPostProcessor:
"""Post-process an optimization problem.
Args:
opt_problem: The optimization problem to be post-processed.
post_name: The name of the post-processor.
save: If ``True``, save the figure.
show: If ``True``, display the figure.
file_path: The path of the file to save the figures.
If the extension is missing, use ``file_extension``.
If ``None``,
create a file path
from ``directory_path``, ``file_name`` and ``file_extension``.
directory_path: The path of the directory to save the figures.
If ``None``, use the current working directory.
file_name: The name of the file to save the figures.
If ``None``, use a default one generated by the post-processing.
file_extension: A file extension, e.g. 'png', 'pdf', 'svg', ...
If ``None``, use a default file extension.
**options: The options of the post-processor.
Returns:
The post-processor.
"""
if isinstance(opt_problem, str):
opt_problem = OptimizationProblem.from_hdf(opt_problem)
post = self.create(post_name, opt_problem)
post.execute(
save=save,
show=show,
file_path=file_path,
file_name=file_name,
file_extension=file_extension,
directory_path=directory_path,
**options,
)
self.executed_post.append(post)
return post
[docs]
def list_generated_plots(self) -> set[str]:
"""The generated plot files."""
plots = []
for post in self.executed_post:
plots.extend(post.output_files)
return set(plots)