Skip to content

Commit e3f43f2

Browse files
lrafeeihmstepanek
andauthored
Add calibration model function traces (#709)
* Add sklearn to tox * Add function traces around model methods * Support Python 2.7 & 3.7 sklearn * Add test for multiple calls to model method * Fixup: add comments & organize * Add ensemble models * Add ensemble model tests * Edit tests * Add ensemble library models from sklearn * Start tests with empty commit * Clean up tests * Fix tests for various versions of sklearn * Fix ensemble tests with changes from tree PR * [Mega-Linter] Apply linters fixes * Remove breakpoints * Create tests/instrumentation for calibration models * Fix calibration tests * Remove commented out code * Remove yaml file in commit * Remove duplicate ensemble module defs Co-authored-by: Hannah Stepanek <[email protected]> Co-authored-by: lrafeei <[email protected]>
1 parent f33d21e commit e3f43f2

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

newrelic/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,12 @@ def _process_module_builtin_defaults():
29022902
"instrument_sklearn_ensemble_hist_models",
29032903
)
29042904

2905+
_process_module_definition(
2906+
"sklearn.calibration",
2907+
"newrelic.hooks.mlmodel_sklearn",
2908+
"instrument_sklearn_calibration_models",
2909+
)
2910+
29052911
_process_module_definition(
29062912
"sklearn.cluster._affinity_propagation",
29072913
"newrelic.hooks.mlmodel_sklearn",

newrelic/hooks/mlmodel_sklearn.py

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ def instrument_sklearn_ensemble_hist_models(module):
201201
_instrument_sklearn_models(module, model_classes)
202202

203203

204+
def instrument_sklearn_calibration_models(module):
205+
model_classes = ("CalibratedClassifierCV",)
206+
_instrument_sklearn_models(module, model_classes)
207+
208+
204209
def instrument_sklearn_cluster_models(module):
205210
model_classes = (
206211
"AffinityPropagation",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from testing_support.validators.validate_transaction_metrics import (
17+
validate_transaction_metrics,
18+
)
19+
20+
from newrelic.api.background_task import background_task
21+
from newrelic.packages import six
22+
23+
24+
def test_model_methods_wrapped_in_function_trace(calibration_model_name, run_calibration_model):
25+
expected_scoped_metrics = {
26+
"CalibratedClassifierCV": [
27+
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.fit", 1),
28+
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.predict", 1),
29+
("Function/MLModel/Sklearn/Named/CalibratedClassifierCV.predict_proba", 2),
30+
],
31+
}
32+
33+
expected_transaction_name = "test_calibration_models:_test"
34+
if six.PY3:
35+
expected_transaction_name = (
36+
"test_calibration_models:test_model_methods_wrapped_in_function_trace.<locals>._test"
37+
)
38+
39+
@validate_transaction_metrics(
40+
expected_transaction_name,
41+
scoped_metrics=expected_scoped_metrics[calibration_model_name],
42+
rollup_metrics=expected_scoped_metrics[calibration_model_name],
43+
background_task=True,
44+
)
45+
@background_task()
46+
def _test():
47+
run_calibration_model()
48+
49+
_test()
50+
51+
52+
@pytest.fixture(params=["CalibratedClassifierCV"])
53+
def calibration_model_name(request):
54+
return request.param
55+
56+
57+
@pytest.fixture
58+
def run_calibration_model(calibration_model_name):
59+
def _run():
60+
import sklearn.calibration
61+
from sklearn.datasets import load_iris
62+
from sklearn.model_selection import train_test_split
63+
64+
X, y = load_iris(return_X_y=True)
65+
x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
66+
67+
clf = getattr(sklearn.calibration, calibration_model_name)()
68+
69+
model = clf.fit(x_train, y_train)
70+
model.predict(x_test)
71+
72+
model.predict_proba(x_test)
73+
74+
return model
75+
76+
return _run

0 commit comments

Comments
 (0)