Skip to content

Commit

Permalink
Swap logger inheritance for composition
Browse files Browse the repository at this point in the history
This allows for the floris.simulation package to use attrs and slotted classes while the floris.tools package can remain typical Python classes.
The LoggerBase did have @define in order to maintain the slotted classes, but that is removed here.
  • Loading branch information
rafmudaf committed Dec 1, 2023
1 parent 3ea29ab commit dfb2870
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 30 deletions.
7 changes: 3 additions & 4 deletions floris/logging_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ def filter(self, record):
return True


@define
class LoggerBase:
class LoggingManager:
"""
Convenience super-class to any class requiring access to the logging
module. The virtual property here allows a simple and dynamic method
This class provide an easy access to the global logger.
The virtual property here allows a simple and dynamic method
for obtaining the correct logger for the calling class.
"""

Expand Down
13 changes: 11 additions & 2 deletions floris/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
fields,
)

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager
from floris.type_dec import FromDictMixin


Expand All @@ -44,14 +44,19 @@ class State(Enum):


@define
class BaseClass(LoggerBase, FromDictMixin):
class BaseClass(FromDictMixin):
"""
BaseClass object class. This class does the logging and MixIn class inheritance.
"""

# Initialize `state` and ensure it is treated as an attribute rather than a constant parameter.
# See https://www.attrs.org/en/stable/api-attr.html#attr.ib
state = field(init=False, default=State.UNINITIALIZED)
_logging_manager: LoggingManager = field(init=False, default=None)

def __attrs_post_init__(self):
"""Post-initialization hook that sets the logger manager."""
self._logging_manager = LoggingManager()

@classmethod
def get_model_defaults(cls) -> Dict[str, Any]:
Expand All @@ -75,6 +80,10 @@ def _get_model_dict(self) -> dict:
"""
return asdict(self)

@property
def logger(self):
"""Returns the logger manager object."""
return self._logging_manager.logger

@define
class BaseModel(BaseClass):
Expand Down
7 changes: 3 additions & 4 deletions floris/tools/floris_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

import numpy as np
import pandas as pd
from scipy.interpolate import LinearNDInterpolator, NearestNDInterpolator

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager
from floris.simulation import Floris, State
from floris.simulation.turbine import (
average_velocity,
Expand All @@ -35,7 +34,7 @@
from floris.type_dec import NDArrayFloat


class FlorisInterface(LoggerBase):
class FlorisInterface(LoggingManager):
"""
FlorisInterface provides a high-level user interface to many of the
underlying methods within the FLORIS framework. It is meant to act as a
Expand All @@ -61,7 +60,7 @@ def __init__(self, configuration: dict | str | Path):
except FileNotFoundError:
# If the file cannot be found, then attempt the configuration path relative to the
# file location from which FlorisInterface was attempted to be run. If successful,
# update self.configuration to an aboslute, working file path and name.
# update self.configuration to an absolute, working file path and name.
base_fn = Path(inspect.stack()[-1].filename).resolve().parent
config = (base_fn / self.configuration).resolve()
self.floris = Floris.from_file(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
YawOptimizationGeometric,
)

from ....logging_manager import LoggerBase
from ....logging_manager import LoggingManager


class LayoutOptimization(LoggerBase):
class LayoutOptimization(LoggingManager):
def __init__(self, fi, boundaries, min_dist=None, freq=None, enable_geometric_yaw=False):
self.fi = fi.copy()
self.boundaries = boundaries
Expand Down
4 changes: 2 additions & 2 deletions floris/tools/optimization/legacy/pyoptsparse/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

# See https://floris.readthedocs.io for documentation

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager


class Optimization(LoggerBase):
class Optimization(LoggingManager):
"""
Base optimization class.
Expand Down
4 changes: 2 additions & 2 deletions floris/tools/optimization/legacy/scipy/yaw_clustered.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import numpy as np
import pandas as pd

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

from .cluster_turbines import cluster_turbines
from .yaw import YawOptimization


class YawOptimizationClustered(YawOptimization, LoggerBase):
class YawOptimizationClustered(YawOptimization, LoggingManager):
"""
YawOptimization is a subclass of
:py:class:`~.tools.optimizationscipy.YawOptimization` that is used to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import numpy as np
import pandas as pd

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

from .cluster_turbines import cluster_turbines
from .yaw_wind_rose import YawOptimizationWindRose


class YawOptimizationWindRoseClustered(YawOptimizationWindRose, LoggerBase):
class YawOptimizationWindRoseClustered(YawOptimizationWindRose, LoggingManager):
"""
YawOptimizationWindRose is a subclass of
:py:class:`~.tools.optimizationscipy.YawOptimizationWindRose` that is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import pandas as pd
from scipy.optimize import minimize

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

from .yaw_wind_rose import YawOptimizationWindRose


class YawOptimizationWindRoseParallel(YawOptimizationWindRose, LoggerBase):
class YawOptimizationWindRoseParallel(YawOptimizationWindRose, LoggingManager):
"""
YawOptimizationWindRose is a subclass of
:py:class:`~.tools.optimizationscipy.YawOptimizationWindRose` that is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import pandas as pd
from scipy.optimize import minimize

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

from .yaw_wind_rose_clustered import YawOptimizationWindRoseClustered


class YawOptimizationWindRoseParallelClustered(YawOptimizationWindRoseClustered, LoggerBase):
class YawOptimizationWindRoseParallelClustered(YawOptimizationWindRoseClustered, LoggingManager):
"""
YawOptimizationWindRoseClustered is a subclass of
:py:class:`~.tools.optimizationscipy.YawOptimizationWindRoseClustered` that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import numpy as np
import pandas as pd

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

from .yaw_optimization_tools import derive_downstream_turbines, find_layout_symmetry


class YawOptimization(LoggerBase):
class YawOptimization(LoggingManager):
"""
YawOptimization is a subclass of :py:class:`floris.tools.optimization.scipy.
Optimization` that is used to optimize the yaw angles of all turbines in a Floris
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import numpy as np
import pandas as pd

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager

# from .yaw_optimizer_scipy import YawOptimizationScipy
from .yaw_optimization_base import YawOptimization


class YawOptimizationSR(YawOptimization, LoggerBase):
class YawOptimizationSR(YawOptimization, LoggingManager):
def __init__(
self,
fi,
Expand Down
4 changes: 2 additions & 2 deletions floris/tools/parallel_computing_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pandas as pd

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager
from floris.tools.optimization.yaw_optimization.yaw_optimizer_sr import YawOptimizationSR
from floris.tools.uncertainty_interface import FlorisInterface, UncertaintyInterface

Expand Down Expand Up @@ -66,7 +66,7 @@ def _optimize_yaw_angles_serial(
return df_opt


class ParallelComputingInterface(LoggerBase):
class ParallelComputingInterface(LoggingManager):
def __init__(
self,
fi,
Expand Down
4 changes: 2 additions & 2 deletions floris/tools/uncertainty_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import numpy as np
from scipy.stats import norm

from floris.logging_manager import LoggerBase
from floris.logging_manager import LoggingManager
from floris.tools import FlorisInterface
from floris.utilities import wrap_360


class UncertaintyInterface(LoggerBase):
class UncertaintyInterface(LoggingManager):
def __init__(
self,
configuration,
Expand Down

0 comments on commit dfb2870

Please sign in to comment.