Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calibration model function traces #709

Merged
merged 26 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ea466a
Add sklearn to tox
hmstepanek Nov 10, 2022
07fef71
Add function traces around model methods
hmstepanek Nov 10, 2022
8a39862
Support Python 2.7 & 3.7 sklearn
hmstepanek Nov 16, 2022
c72d9df
Add test for multiple calls to model method
hmstepanek Nov 16, 2022
fc1f179
Fixup: add comments & organize
hmstepanek Nov 17, 2022
59a9511
Add ensemble models
lrafeei Nov 17, 2022
b26fa84
Add ensemble model tests
lrafeei Nov 18, 2022
fba42c8
Edit tests
lrafeei Nov 18, 2022
80ad2c0
Add ensemble library models from sklearn
lrafeei Nov 23, 2022
531b12a
Start tests with empty commit
lrafeei Nov 23, 2022
94cb0c2
Clean up tests
lrafeei Nov 23, 2022
5b9d1b3
Merge branch 'feature-scikitlearn' into add-ensemble-model-function-t…
lrafeei Nov 28, 2022
36115e5
Fix tests for various versions of sklearn
lrafeei Dec 2, 2022
7328d7a
Merge branch 'develop-scikitlearn' into add-ensemble-model-function-t…
lrafeei Dec 6, 2022
ad14a00
Fix ensemble tests with changes from tree PR
lrafeei Dec 7, 2022
3d55348
[Mega-Linter] Apply linters fixes
lrafeei Dec 7, 2022
c9b599c
Merge branch 'develop-scikitlearn' into add-ensemble-model-function-t…
lrafeei Dec 7, 2022
1202ff7
Remove breakpoints
lrafeei Dec 7, 2022
f38410d
Create tests/instrumentation for calibration models
lrafeei Dec 7, 2022
f24c8d4
Fix calibration tests
lrafeei Dec 8, 2022
1c352af
Merge branch 'develop-scikitlearn' into add-calibration-model-functio…
lrafeei Dec 8, 2022
c422d89
Merge branch 'develop-scikitlearn' into add-calibration-model-functio…
lrafeei Dec 14, 2022
bda993c
Remove commented out code
lrafeei Dec 14, 2022
03951d5
Remove yaml file in commit
lrafeei Dec 14, 2022
e1dac00
Remove duplicate ensemble module defs
hmstepanek Dec 15, 2022
b2de7d1
Merge branch 'develop-scikitlearn' into add-calibration-model-functio…
lrafeei Dec 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,12 @@ def _process_module_builtin_defaults():
"instrument_sklearn_ensemble_hist_models",
)

_process_module_definition(
"sklearn.calibration",
"newrelic.hooks.mlmodel_sklearn",
"instrument_sklearn_calibration_models",
)

_process_module_definition(
"sklearn.cluster._affinity_propagation",
"newrelic.hooks.mlmodel_sklearn",
Expand Down
5 changes: 5 additions & 0 deletions newrelic/hooks/mlmodel_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def instrument_sklearn_ensemble_hist_models(module):
_instrument_sklearn_models(module, model_classes)


def instrument_sklearn_calibration_models(module):
model_classes = ("CalibratedClassifierCV",)
_instrument_sklearn_models(module, model_classes)


def instrument_sklearn_cluster_models(module):
model_classes = (
"AffinityPropagation",
Expand Down
76 changes: 76 additions & 0 deletions tests/mlmodel_sklearn/test_calibration_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task
from newrelic.packages import six


def test_model_methods_wrapped_in_function_trace(calibration_model_name, run_calibration_model):
expected_scoped_metrics = {
"CalibratedClassifierCV": [
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.fit", 1),
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.predict", 1),
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.predict_proba", 2),
],
}

expected_transaction_name = "test_calibration_models:_test"
if six.PY3:
expected_transaction_name = (
"test_calibration_models:test_model_methods_wrapped_in_function_trace.<locals>._test"
)

@validate_transaction_metrics(
expected_transaction_name,
scoped_metrics=expected_scoped_metrics[calibration_model_name],
rollup_metrics=expected_scoped_metrics[calibration_model_name],
background_task=True,
)
@background_task()
def _test():
run_calibration_model()

_test()


@pytest.fixture(params=["CalibratedClassifierCV"])
def calibration_model_name(request):
return request.param


@pytest.fixture
def run_calibration_model(calibration_model_name):
def _run():
import sklearn.calibration
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)

clf = getattr(sklearn.calibration, calibration_model_name)()

model = clf.fit(x_train, y_train)
model.predict(x_test)

model.predict_proba(x_test)

return model

return _run