Skip to content

Commit

Permalink
Use as_dict to get model parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rafmudaf committed Dec 6, 2023
1 parent 63884b9 commit 1cf526c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 41 deletions.
23 changes: 0 additions & 23 deletions floris/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)

from attrs import (
asdict,
Attribute,
define,
field,
Expand Down Expand Up @@ -54,28 +53,6 @@ class BaseClass(FromDictMixin):
state = field(init=False, default=State.UNINITIALIZED)
_logging_manager: LoggingManager = field(init=False, default=LoggingManager())

@classmethod
def get_model_defaults(cls) -> Dict[str, Any]:
"""Produces a dictionary of the keyword arguments and their defaults.
Returns
-------
Dict[str, Any]
Dictionary of keyword argument: default.
"""
return {el.name: el.default for el in fields(cls)}

def _get_model_dict(self) -> dict:
"""Convenience method that wraps the `attrs.asdict` method. Returns the object's
parameters as a dictionary.
Returns
-------
dict
The provided or default, if no input provided, model settings as a dictionary.
"""
return asdict(self)

@property
def logger(self):
"""Returns the logger manager object."""
Expand Down
1 change: 1 addition & 0 deletions floris/tools/interface_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def show_params(
# props = get_props(obj, fi)
props = fi.floris.wake._asdict()
# props = props["wake_velocity_parameters"][fi.floris.wake.velocity_model.model_string]
# NOTE: _get_model_dict is remove and model.as_dict() should be used instead
props = fi.floris.wake.velocity_model._get_model_dict()

if verbose:
Expand Down
23 changes: 5 additions & 18 deletions tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,16 @@ def function() -> None:
return None


def test_get_model_defaults():
"""
This tests that the default parameter values are set correctly and unchanged if not
explicitly set.
Note that the attributes in BaseClass and BaseModel are treated as default parameters
by attrs.
"""
defaults = ClassTest.get_model_defaults()
assert len(defaults) == 5
assert defaults["x"] == 1
assert defaults["a_string"] == "abc"


def test_get_model_values():
"""
BaseClass and therefore BaseModel previously had a method `get_model_values` that
returned the values of the model parameters. This was removed because it was redundant
but this test was refactored to test the as_dict method from FromDictMixin.
This tests that the parameters are changed when set by the user.
Note that the attributes in BaseClass and BaseModel are treated as parameters by attrs.
They aren't changed in the instantiation method, but they are still included in the
parameter set.
"""
cls = ClassTest(x=4, a_string="xyz")
values = cls._get_model_dict()
assert len(values) == 5
values = cls.as_dict()
assert len(values) == 2
assert values["x"] == 4
assert values["a_string"] == "xyz"

Expand Down

0 comments on commit 1cf526c

Please sign in to comment.