|
| 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