"""ML-ENSEMBLE
:author: Sebastian Flennerhag
:copyright: 2017
:licence: MIT
Controls that an estimator was built as expected.
"""
import warnings
from .exceptions import (NotFittedError, LayerSpecificationWarning,
LayerSpecificationError, ParallelProcessingError,
ParallelProcessingWarning)
[docs]def check_is_fitted(estimator, attr):
"""Check that ensemble has been fitted.
Parameters
----------
estimator : estimator instance
ensemble instance to check.
attr : str
attribute to assert existence of.
"""
msg = ("This %s instance is not fitted yet. Call 'fit' with "
"appropriate arguments before using this method.")
if not hasattr(estimator, attr):
raise NotFittedError(msg % type(estimator).__name__)
[docs]def check_ensemble_build(inst, attr='layers'):
"""Check that layers have been instantiated."""
if not hasattr(inst, attr):
# No layer container. This should not happen!
msg = ("No layer class attached to instance (%s). (Cannot find a "
"'LayerContainer' class instance as attribute [%s].)")
raise AttributeError(msg % (inst.__class__.__name__, attr))
if getattr(inst, attr) is None:
# No layers instantiated.
if not getattr(inst, 'raise_on_exception', True):
msg = "No Layers in instance (%s). Nothing to fit / predict."
warnings.warn(msg % inst.__class__.__name__,
LayerSpecificationWarning)
# For PASSED flag on soft fail
return False
else:
msg = ("No Layers in instance (%s). Add layers before calling "
"'fit' and 'predict'.")
raise LayerSpecificationError(msg % inst.__class__.__name__)
# For PASSED flag
return True
[docs]def assert_valid_estimator(instance):
"""Assert that an instance has a ``get_params`` and ``fit`` method."""
has_get_params = hasattr(instance, 'get_params')
has_fit = hasattr(instance, 'fit')
if not has_get_params:
raise TypeError("'%s' does not appear to be a valid"
" estimator as it does not implement a "
"'get_params' method. Type: "
"%s" % (instance, type(instance)))
if not has_fit:
raise TypeError("'%s' does not appear to be a valid"
" estimator as it does not implement a "
"'fit' method. Type: "
"%s" % (instance, type(instance)))
try:
instance.get_params()
except TypeError:
raise TypeError("'%s' does not appear to be an instance of an "
"estimator class, but the class itself." % instance)
[docs]def check_initialized(inst):
"""Check if a ParallelProcessing instance is initialized properly."""
if not inst.__initialized__:
msg = "ParallelProcessing is not initialized. Call " \
"'initialize' before calling 'fit'."
raise ParallelProcessingError(msg)
if getattr(inst, '__fitted__', None):
if inst.layers.raise_on_exception:
raise ParallelProcessingError("This instance is already "
"fitted and its parallel "
"processing jobs has not been "
"terminated. To refit "
"instance, call 'terminate' "
"before calling 'fit'.")
else:
warnings.warn("This instance is already "
"fitted and its parallel "
"processing job has not been "
"terminated. Will refit using previous job's cache.",
ParallelProcessingWarning)