Instrumentor

The instrumentor class is used for package, class, and instance instrumentation. Multiple instrumentors can be made to apply different instrumentation decorators. Different instruments can be applied separately to different machine learning models using instance instrumentation.

By default, instrumentation is never applied to classes or instances derived from sklearn.tree.BaseDecisionTree. This can be overridden by the exclude arg on instantiation.

class sklearn_instrumentation.instrumentor.SklearnInstrumentor(instrument: Callable, instrument_kwargs: Optional[dict] = None, methods: Optional[List[str]] = None, exclude: Optional[List[Type]] = None)[source]

Instrumentor for sklearn estimators.

A container for instrumentation configuration.

The instrument (a decorator) should be similar to the following:

def instrument_decorator(func: Callable, **dkwargs):

    @wraps(func)
    def wrapper(*args, **kwargs):
        print("Before executing")
        retval = func(*args, **kwargs)
        print("After executing")
        return retval

    return wrapper

By default, classes derived from sklearn.tree._classes.BaseDecisionTree are excluded from instrumentation.

By default, methods on which instrumentation is applied includes _fit, _predict, _predict_proba, _transform, fit, predict, predict_proba, and transform.

Methods which are properties are not instrumented on instances, but are instrumented on classes.

Parameters
  • instrument (Callable) – A decorator to apply to sklearn estimator methods. The wrapping function signature should be (func, **dkwargs), where func is the target method and dkwargs is the instrument_kwargs argument of the instrumentor.

  • instrument_kwargs (dict) – Keyword args to be passed to the decorator.

  • methods (list(str)) – A list of method names on which to apply decorators.

  • exclude (list(Type)) – A list of types for which instrumentation should be skipped.

instrument_class(estimator: Type[sklearn.base.BaseEstimator])[source]

Instrument a BaseEstimator class.

Apply this instrumentor’s decorator to the methods of a BaseEstimator class.

Parameters

estimator (Type[BaseEstimator]) – A class on which to apply instrumentation.

instrument_classes(estimators: Iterable[Type[sklearn.base.BaseEstimator]])[source]

Instrument multiple BaseEstimator classes.

Apply this instrumentor’s decorator to the methods of several BaseEstimator classes.

Parameters

estimators (Iterable[Type[BaseEstimator]]) – Iterable of classes on which to apply instrumentation.

instrument_instance(estimator: sklearn.base.BaseEstimator, recursive: bool = True, instrument_kwargs: Optional[dict] = None)[source]

Instrument a BaseEstimator instance.

Decorate the methods of the estimator instance.

Parameters
  • estimator (BaseEstimator) – An instance of a BaseEstimator-derived class.

  • recursive (bool) – Whether to iterate recursively through metaestimators to apply the same instrumentation.

  • instrument_kwargs (dict) – Keyword args to be passed to the decorator, overriding the ones initialized by the instrumentor

instrument_instance_classes(estimator: sklearn.base.BaseEstimator)[source]

Instrument the classes (not the instances) found in the estimator.

Lighter version of instrument_package. Instead of crawling the package modules and dynamically importing objects, crawls the estimator hierarchy and only instruments classes which are already imported. This is generally faster and uses less memory.

Inspects the (meta)estimator hierarchy, only instrumenting the classes found in the estimator, its methods, and its attributes.

Parameters

estimator (BaseEstimator) – An estimator instance with which to instrument related classes

instrument_instances_classes(estimators: Iterable[sklearn.base.BaseEstimator])[source]

Instrument the classes (not the instances) found in the estimators.

Lighter version of instrument_package. Instead of crawling the package modules and dynamically importing objects, crawls the estimators’ hierarchies and only instruments classes which are already imported. This is generally faster and uses less memory.

Inspects the (meta)estimator hierarchy, only instrumenting the classes found in the estimator, its methods, and its attributes.

Parameters

estimators (Iterable[BaseEstimator]) – Several estimator instances of which to instrument related classes.

instrument_package(package_name: str = 'sklearn')[source]

Instrument a package.

Apply this instrumentor’s decorator to the components of the package.

Parameters

package_name (str) – A list of package names.

instrument_packages(package_names: List[str])[source]

Instrument multiple packages.

Apply this instrumentor’s decorator to the components of the packages.

Parameters

package_names (list(str)) – A list of package names.

uninstrument_class(estimator: Type[sklearn.base.BaseEstimator], full: bool = False)[source]

Uninstrument a BaseEstimator class.

Remove this instrumentor’s decorator to the methods of a BaseEstimator class.

Parameters
  • estimator (Type[BaseEstimator]) – A class from which to remove instrumentation.

  • full (bool) – Whether to fully uninstrument the estimator class.

uninstrument_classes(estimators: Iterable[Type[sklearn.base.BaseEstimator]], full: bool = False)[source]

Uninstrument BaseEstimator classes.

Remove this instrumentor’s decorator from the methods of a BaseEstimator classes.

Parameters
  • estimators (Iterable[Type[BaseEstimator]]) – Classes from which to remove instrumentation.

  • full (bool) – Whether to fully uninstrument the estimator classes.

uninstrument_instance(estimator: sklearn.base.BaseEstimator, recursive: bool = True, full: bool = False)[source]

Uninstrument a BaseEstimator instance.

Remove this instrumentor’s decorators on the methods of the estimator instance.

Parameters
  • estimator (BaseEstimator) – An instance of a BaseEstimator-derived class.

  • recursive (bool) – Whether to iterate recursively through metaestimators to apply the same uninstrumentation.

  • full (bool) – Whether to fully uninstrument the estimator.

uninstrument_instance_classes(estimator: sklearn.base.BaseEstimator, full: bool = False)[source]

Uninstrument the classes (not the instances) found in the estimator.

Inspects the (meta)estimator hierarchy, only uninstrumenting the classes found in the estimator, its methods, and its attributes.

Parameters
  • estimator (BaseEstimator) – An estimator instance with which to uninstrument related classes

  • full (bool) – Whether to fully uninstrument the estimator classes.

uninstrument_package(package_name: str = 'sklearn', full: bool = False)[source]

Uninstrument a package.

Remove this instrumentor’s decorators from the components of the package.

Parameters
  • package_name (str) – A package name.

  • full (bool) – Whether to fully uninstrument the packages.

uninstrument_packages(package_names: List[str], full: bool = False)[source]

Uninstrument multiple packages.

Remove this instrumentor’s decorators from the components of the packages.

Parameters
  • package_names (list(str)) – A list of package names.

  • full (bool) – Whether to fully uninstrument the packages.