sdk/src/mlnotify/hooks.py
def apply_hooks(before: Callable, after: Callable):
    """Applies hooks.

    This function applies all hooks - imports the relevant packages and patches
    the specified functions. Since usually not all packages exist, it runs on
    a best-effort assumption.

    Args:
        before: The function to run before the original function
        after: The function to run after the original function
    """
    logger.debug("Applying hooks")

    # LightGBM
    try:
        import lightgbm

        patch(lightgbm, "train", before=before, after=after)
        patch(lightgbm.sklearn, "train", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch lightgbm", exc_info=True)

    # XGBoost
    try:
        import xgboost

        patch(xgboost, "train", before=before, after=after)
        patch(xgboost.sklearn, "train", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch xgboost", exc_info=True)

    # Tensorflow & Keras
    try:
        import tensorflow

        patch(tensorflow.keras.Model, "fit", before=before, after=after)
        patch(tensorflow.keras.Model, "train_on_batch", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch tensorflow.keras", exc_info=True)
        # If tensorflow.keras patching doesn't work, we can try
        # patching keras as a standalone
        try:
            import keras

            patch(keras.Model, "fit", before=before, after=after)
            patch(keras.Model, "train_on_batch", before=before, after=after)
        except Exception:
            logger.debug("Could not import and patch keras", exc_info=True)

    # SKLearn
    try:
        import sklearn.svm
        import sklearn.tree

        patch(sklearn.svm.SVC, "fit", before=before, after=after)
        patch(sklearn.svm.SVR, "fit", before=before, after=after)
        patch(sklearn.svm.OneClassSVM, "fit", before=before, after=after)
        patch(sklearn.svm.NuSVC, "fit", before=before, after=after)
        patch(sklearn.svm.NuSVR, "fit", before=before, after=after)
        patch(sklearn.svm.LinearSVR, "fit", before=before, after=after)
        patch(sklearn.svm.LinearSVC, "fit", before=before, after=after)
        patch(sklearn.tree.DecisionTreeClassifier, "fit", before=before, after=after)
        patch(sklearn.tree.DecisionTreeRegressor, "fit", before=before, after=after)
    except ImportError:
        logger.debug("Could not import and patch sklearn", exc_info=True)

    # Catboost
    try:
        import catboost

        patch(catboost.CatBoostRegressor, "fit", before=before, after=after)
        patch(catboost.CatBoostClassifier, "fit", before=before, after=after)
    except ImportError:
        logger.debug("Could not import and patch catboost", exc_info=True)
Applies hooks.

    This function applies all hooks - imports the relevant packages and patches
    the specified functions. Since usually not all packages exist, it runs on
    a best-effort assumption.

    Args:
        before: The function to run before the original function
        after: The function to run after the original function
    
Applies hooks.
This function applies all hooks - imports the relevant packages and patches
the specified functions. Since usually not all packages exist, it runs on
a best-effort assumption.
Applies hooks . This function applies all hooks - imports the relevant packages and patches the specified functions . Since usually not all packages exist it runs on a best - effort assumption .
def apply_hooks(before: Callable, after: Callable):
    logger.debug("Applying hooks")
    try:
        import lightgbm
        patch(lightgbm, "train", before=before, after=after)
        patch(lightgbm.sklearn, "train", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch lightgbm", exc_info=True)
    try:
        import xgboost
        patch(xgboost, "train", before=before, after=after)
        patch(xgboost.sklearn, "train", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch xgboost", exc_info=True)
    try:
        import tensorflow
        patch(tensorflow.keras.Model, "fit", before=before, after=after)
        patch(tensorflow.keras.Model, "train_on_batch", before=before, after=after)
    except Exception:
        logger.debug("Could not import and patch tensorflow.keras", exc_info=True)
        try:
            import keras
            patch(keras.Model, "fit", before=before, after=after)
            patch(keras.Model, "train_on_batch", before=before, after=after)
        except Exception:
            logger.debug("Could not import and patch keras", exc_info=True)
    try:
        import sklearn.svm
        import sklearn.tree
        patch(sklearn.svm.SVC, "fit", before=before, after=after)
        patch(sklearn.svm.SVR, "fit", before=before, after=after)
        patch(sklearn.svm.OneClassSVM, "fit", before=before, after=after)
        patch(sklearn.svm.NuSVC, "fit", before=before, after=after)
        patch(sklearn.svm.NuSVR, "fit", before=before, after=after)
        patch(sklearn.svm.LinearSVR, "fit", before=before, after=after)
        patch(sklearn.svm.LinearSVC, "fit", before=before, after=after)
        patch(sklearn.tree.DecisionTreeClassifier, "fit", before=before, after=after)
        patch(sklearn.tree.DecisionTreeRegressor, "fit", before=before, after=after)
    except ImportError:
        logger.debug("Could not import and patch sklearn", exc_info=True)
    try:
        import catboost
        patch(catboost.CatBoostRegressor, "fit", before=before, after=after)
        patch(catboost.CatBoostClassifier, "fit", before=before, after=after)
    except ImportError:
        logger.debug("Could not import and patch catboost", exc_info=True)
def apply_hooks ( before : Callable , after : Callable ) : logger . debug ( "Applying hooks" ) try : import lightgbm patch ( lightgbm , "train" , before = before , after = after ) patch ( lightgbm . sklearn , "train" , before = before , after = after ) except Exception : logger . debug ( "Could not import and patch lightgbm" , exc_info = True ) try : import xgboost patch ( xgboost , "train" , before = before , after = after ) patch ( xgboost . sklearn , "train" , before = before , after = after ) except Exception : logger . debug ( "Could not import and patch xgboost" , exc_info = True ) try : import tensorflow patch ( tensorflow . keras . Model , "fit" , before = before , after = after ) patch ( tensorflow . keras . Model , "train_on_batch" , before = before , after = after ) except Exception : logger . debug ( "Could not import and patch tensorflow.keras" , exc_info = True ) try : import keras patch ( keras . Model , "fit" , before = before , after = after ) patch ( keras . Model , "train_on_batch" , before = before , after = after ) except Exception : logger . debug ( "Could not import and patch keras" , exc_info = True ) try : import sklearn . svm import sklearn . tree patch ( sklearn . svm . SVC , "fit" , before = before , after = after ) patch ( sklearn . svm . SVR , "fit" , before = before , after = after ) patch ( sklearn . svm . OneClassSVM , "fit" , before = before , after = after ) patch ( sklearn . svm . NuSVC , "fit" , before = before , after = after ) patch ( sklearn . svm . NuSVR , "fit" , before = before , after = after ) patch ( sklearn . svm . LinearSVR , "fit" , before = before , after = after ) patch ( sklearn . svm . LinearSVC , "fit" , before = before , after = after ) patch ( sklearn . tree . DecisionTreeClassifier , "fit" , before = before , after = after ) patch ( sklearn . tree . DecisionTreeRegressor , "fit" , before = before , after = after ) except ImportError : logger . debug ( "Could not import and patch sklearn" , exc_info = True ) try : import catboost patch ( catboost . CatBoostRegressor , "fit" , before = before , after = after ) patch ( catboost . CatBoostClassifier , "fit" , before = before , after = after ) except ImportError : logger . debug ( "Could not import and patch catboost" , exc_info = True )
Applies hooks.
Applies hooks .
"""Applies hooks.

    This function applies all hooks - imports the relevant packages and patches
    the specified functions. Since usually not all packages exist, it runs on
    a best-effort assumption.

    Args:
        before: The function to run before the original function
        after: The function to run after the original function
    """ # LightGBM # XGBoost # Tensorflow & Keras # If tensorflow.keras patching doesn't work, we can try # patching keras as a standalone # SKLearn # Catboost
before
Callable
The function to run before the original function
The function to run before the original function
after
Callable
The function to run after the original function
The function to run after the original function