Skip to content

Commit

Permalink
[ENH] meta-object mixins (#216)
Browse files Browse the repository at this point in the history
This PR adds meta-object mixins - previously, the package provided only classes inheriting from `BaseObject` or `BaseEstimator`, which can
create multiple inheritance conflicts if downstream needs to inherit
from an abstract desceendant of `BaseObject` or `BaseEstimator`.

This is also more in line with the actual use cases in `sktime` and
`skpro`.
  • Loading branch information
fkiraly authored Aug 26, 2023
1 parent cd7bec6 commit 027e0d7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
9 changes: 8 additions & 1 deletion skbase/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
from typing import List

from skbase.base._base import BaseEstimator, BaseObject
from skbase.base._meta import BaseMetaEstimator, BaseMetaObject
from skbase.base._meta import (
BaseMetaEstimator,
BaseMetaEstimatorMixin,
BaseMetaObject,
BaseMetaObjectMixin,
)

__author__: List[str] = ["mloning", "RNKuhns", "fkiraly"]
__all__: List[str] = [
"BaseObject",
"BaseEstimator",
"BaseMetaEstimator",
"BaseMetaObject",
"BaseMetaEstimatorMixin",
"BaseMetaObjectMixin",
]
86 changes: 80 additions & 6 deletions skbase/base/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,48 @@ class has values that follow the named object specification. For example,
this would allow `get_params` and `set_params` to retrieve and update the
parameters of the objects in each step.
Note: if inheriting from an abstract descendant of `BaseObject`, use
``BaseMetaObjectMixin`` and not ``BaseMetaObject``.
See Also
--------
BaseMetaObjectMixin :
Mixin for inheriting from abstract descendants of ``BaseObject``.
Same as ``BaseMetaObject``, but does not inherit from ``BaseObject``.
BaseMetaEstimator :
Expands on `BaseMetaObject` by adding functionality for getting fitted
parameters from a class's component estimators. `BaseEstimator` should
be used when you want to create a meta estimator.
Expands on ``BaseMetaObject`` by adding functionality for getting fitted
parameters from a class's component estimators.
"""


class BaseMetaObjectMixin(_MetaObjectMixin, _MetaTagLogicMixin):
"""Parameter and tag management for objects composed of named objects.
Allows objects to get and set nested parameters when a parameter of the the
class has values that follow the named object specification. For example,
in a pipeline class with the the "step" parameter accepting named objects,
this would allow `get_params` and `set_params` to retrieve and update the
parameters of the objects in each step.
Mixin for inheriting from abstract descendants of ``BaseObject``.
Intended use is inheriting as follows:
``class MyAbstractBaseClass(BaseObject)``, and then
``class MyConcreteClass(BaseMetaObjectMixin, MyAbstractBaseClass)``
The mixin will override:
``get_params``, ``set_params``, ``_get_params``, ``_set_params``,
``_get_fitted_params``, ``_sk_visual_block_``
See Also
--------
BaseMetaEstimatorMixin :
Expands on ``BaseMetaObjectMixin`` by adding functionality for getting fitted
parameters from a class's component estimators.
BaseMetaObject :
same as ``BaseMetaObjectMixin``, but also inherits from ``BaseObject``.
Use for a standalone meta-object class.
Do not use if inheriting from an abstract descendant of ``BaseObject``.
"""


Expand All @@ -874,10 +910,48 @@ class has values that follow the named object specification. For example,
this would allow `get_params` and `set_params` to retrieve and update the
parameters of the objects in each step.
Note: if inheriting from an abstract descendant of `BaseEstimator`, use
``BaseMetaEstimatorMixin`` and not ``BaseMetaEstimator``.
See Also
--------
BaseMetaEstimatorMixin :
Mixin for inheriting from abstract descendants of ``BaseObject``.
Same as ``BaseMetaObject``, but does not inherit from ``BaseObject``.
BaseMetaObject :
Provides similar functionality to `BaseMetaEstimator` for getting
parameters from a class's component objects, but does not have the
estimator interface.
Provides similar functionality to `BaseMetaEstimator`,
but does not have the estimator interface for fitting and fitted parameters.
"""


class BaseMetaEstimatorMixin(_MetaObjectMixin, _MetaTagLogicMixin):
"""Parameter and tag management for estimators composed of named objects.
Allows estimators to get and set nested parameters when a parameter of the the
class has values that follow the named object specification. For example,
in a pipeline class with the the "step" parameter accepting named objects,
this would allow `get_params` and `set_params` to retrieve and update the
parameters of the objects in each step.
Mixin for inheriting from abstract descendants of ``BaseEstimator``.
Intended use is inheriting as follows:
``class MyAbstractBaseClass(BaseEstimator)``, and then
``class MyConcreteClass(BaseMetaEstimatorMixin, MyAbstractBaseClass)``
Note: the order of inheritance is important.
The mixin will override:
``get_params``, ``set_params``, ``_get_params``, ``_set_params``,
``_get_fitted_params``, ``_sk_visual_block_``
See Also
--------
BaseMetaObjectMixin :
Provides similar functionality to `BaseMetaEstimatorMixin`,
but does not have the estimator interface for fitting and fitted parameters.
BaseMetaEstimator :
same as ``BaseMetaEstimatorMixin``, but also inherits from ``BaseEstimator``.
Use for a standalone meta-estimator class.
Do not use if inheriting from an abstract descendant of ``BaseEstimator``.
"""
11 changes: 10 additions & 1 deletion skbase/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@
"skbase.base": (
"BaseEstimator",
"BaseMetaEstimator",
"BaseMetaEstimatorMixin",
"BaseMetaObject",
"BaseMetaObjectMixin",
"BaseObject",
),
"skbase.base._base": ("BaseEstimator", "BaseObject"),
"skbase.base._meta": ("BaseMetaObject", "BaseMetaEstimator"),
"skbase.base._meta": (
"BaseMetaObject",
"BaseMetaObjectMixin",
"BaseMetaEstimator",
"BaseMetaEstimatorMixin",
),
"skbase.base._pretty_printing._pprint": ("KeyValTuple", "KeyValTupleParam"),
"skbase.lookup._lookup": (),
"skbase.testing": ("BaseFixtureGenerator", "QuickTester", "TestAllObjects"),
Expand All @@ -104,7 +111,9 @@
{
"skbase.base._meta": (
"BaseMetaObject",
"BaseMetaObjectMixin",
"BaseMetaEstimator",
"BaseMetaEstimatorMixin",
"_MetaObjectMixin",
"_MetaTagLogicMixin",
),
Expand Down

0 comments on commit 027e0d7

Please sign in to comment.