@@ -1197,97 +1197,95 @@ def test_scorer_no_op_multiclass_select_proba():
1197
1197
scorer (lr , X_test , y_test )
1198
1198
1199
1199
1200
+ @pytest .mark .usefixtures ("enable_slep006" )
1200
1201
@pytest .mark .parametrize ("name" , get_scorer_names (), ids = get_scorer_names ())
1201
1202
def test_scorer_metadata_request (name ):
1202
1203
"""Testing metadata requests for scorers.
1203
1204
1204
1205
This test checks many small things in a large test, to reduce the
1205
1206
boilerplate required for each section.
1206
1207
"""
1207
- with config_context (enable_metadata_routing = True ):
1208
- # Make sure they expose the routing methods.
1209
- scorer = get_scorer (name )
1210
- assert hasattr (scorer , "set_score_request" )
1211
- assert hasattr (scorer , "get_metadata_routing" )
1212
-
1213
- # Check that by default no metadata is requested.
1214
- assert_request_is_empty (scorer .get_metadata_routing ())
1215
-
1216
- weighted_scorer = scorer .set_score_request (sample_weight = True )
1217
- # set_score_request should mutate the instance, rather than returning a
1218
- # new instance
1219
- assert weighted_scorer is scorer
1220
-
1221
- # make sure the scorer doesn't request anything on methods other than
1222
- # `score`, and that the requested value on `score` is correct.
1223
- assert_request_is_empty (weighted_scorer .get_metadata_routing (), exclude = "score" )
1224
- assert (
1225
- weighted_scorer .get_metadata_routing ().score .requests ["sample_weight" ]
1226
- is True
1227
- )
1208
+ # Make sure they expose the routing methods.
1209
+ scorer = get_scorer (name )
1210
+ assert hasattr (scorer , "set_score_request" )
1211
+ assert hasattr (scorer , "get_metadata_routing" )
1212
+
1213
+ # Check that by default no metadata is requested.
1214
+ assert_request_is_empty (scorer .get_metadata_routing ())
1215
+
1216
+ weighted_scorer = scorer .set_score_request (sample_weight = True )
1217
+ # set_score_request should mutate the instance, rather than returning a
1218
+ # new instance
1219
+ assert weighted_scorer is scorer
1220
+
1221
+ # make sure the scorer doesn't request anything on methods other than
1222
+ # `score`, and that the requested value on `score` is correct.
1223
+ assert_request_is_empty (weighted_scorer .get_metadata_routing (), exclude = "score" )
1224
+ assert (
1225
+ weighted_scorer .get_metadata_routing ().score .requests ["sample_weight" ] is True
1226
+ )
1228
1227
1229
- # make sure putting the scorer in a router doesn't request anything by
1230
- # default
1231
- router = MetadataRouter (owner = "test" ).add (
1232
- method_mapping = "score" , scorer = get_scorer (name )
1233
- )
1234
- # make sure `sample_weight` is refused if passed.
1235
- with pytest .raises (TypeError , match = "got unexpected argument" ):
1236
- router .validate_metadata (params = {"sample_weight" : 1 }, method = "score" )
1237
- # make sure `sample_weight` is not routed even if passed.
1238
- routed_params = router .route_params (params = {"sample_weight" : 1 }, caller = "score" )
1239
- assert not routed_params .scorer .score
1240
-
1241
- # make sure putting weighted_scorer in a router requests sample_weight
1242
- router = MetadataRouter (owner = "test" ).add (
1243
- scorer = weighted_scorer , method_mapping = "score"
1244
- )
1228
+ # make sure putting the scorer in a router doesn't request anything by
1229
+ # default
1230
+ router = MetadataRouter (owner = "test" ).add (
1231
+ method_mapping = "score" , scorer = get_scorer (name )
1232
+ )
1233
+ # make sure `sample_weight` is refused if passed.
1234
+ with pytest .raises (TypeError , match = "got unexpected argument" ):
1245
1235
router .validate_metadata (params = {"sample_weight" : 1 }, method = "score" )
1246
- routed_params = router .route_params (params = {"sample_weight" : 1 }, caller = "score" )
1247
- assert list (routed_params .scorer .score .keys ()) == ["sample_weight" ]
1236
+ # make sure `sample_weight` is not routed even if passed.
1237
+ routed_params = router .route_params (params = {"sample_weight" : 1 }, caller = "score" )
1238
+ assert not routed_params .scorer .score
1239
+
1240
+ # make sure putting weighted_scorer in a router requests sample_weight
1241
+ router = MetadataRouter (owner = "test" ).add (
1242
+ scorer = weighted_scorer , method_mapping = "score"
1243
+ )
1244
+ router .validate_metadata (params = {"sample_weight" : 1 }, method = "score" )
1245
+ routed_params = router .route_params (params = {"sample_weight" : 1 }, caller = "score" )
1246
+ assert list (routed_params .scorer .score .keys ()) == ["sample_weight" ]
1248
1247
1249
1248
1249
+ @pytest .mark .usefixtures ("enable_slep006" )
1250
1250
def test_metadata_kwarg_conflict ():
1251
1251
"""This test makes sure the right warning is raised if the user passes
1252
1252
some metadata both as a constructor to make_scorer, and during __call__.
1253
1253
"""
1254
- with config_context (enable_metadata_routing = True ):
1255
- X , y = make_classification (
1256
- n_classes = 3 , n_informative = 3 , n_samples = 20 , random_state = 0
1257
- )
1258
- lr = LogisticRegression ().fit (X , y )
1254
+ X , y = make_classification (
1255
+ n_classes = 3 , n_informative = 3 , n_samples = 20 , random_state = 0
1256
+ )
1257
+ lr = LogisticRegression ().fit (X , y )
1259
1258
1260
- scorer = make_scorer (
1261
- roc_auc_score ,
1262
- needs_proba = True ,
1263
- multi_class = "ovo" ,
1264
- labels = lr .classes_ ,
1265
- )
1266
- with pytest .warns (UserWarning , match = "already set as kwargs" ):
1267
- scorer .set_score_request (labels = True )
1259
+ scorer = make_scorer (
1260
+ roc_auc_score ,
1261
+ needs_proba = True ,
1262
+ multi_class = "ovo" ,
1263
+ labels = lr .classes_ ,
1264
+ )
1265
+ with pytest .warns (UserWarning , match = "already set as kwargs" ):
1266
+ scorer .set_score_request (labels = True )
1268
1267
1269
- with config_context (enable_metadata_routing = True ):
1270
- with pytest .warns (UserWarning , match = "There is an overlap" ):
1271
- scorer (lr , X , y , labels = lr .classes_ )
1268
+ with pytest .warns (UserWarning , match = "There is an overlap" ):
1269
+ scorer (lr , X , y , labels = lr .classes_ )
1272
1270
1273
1271
1272
+ @pytest .mark .usefixtures ("enable_slep006" )
1274
1273
def test_PassthroughScorer_metadata_request ():
1275
1274
"""Test that _PassthroughScorer properly routes metadata.
1276
1275
1277
1276
_PassthroughScorer should behave like a consumer, mirroring whatever is the
1278
1277
underlying score method.
1279
1278
"""
1280
- with config_context (enable_metadata_routing = True ):
1281
- scorer = _PassthroughScorer (
1282
- estimator = LinearSVC ()
1283
- .set_score_request (sample_weight = "alias" )
1284
- .set_fit_request (sample_weight = True )
1285
- )
1286
- # test that _PassthroughScorer leaves everything other than `score` empty
1287
- assert_request_is_empty (scorer .get_metadata_routing (), exclude = "score" )
1288
- # test that _PassthroughScorer doesn't behave like a router and leaves
1289
- # the request as is.
1290
- assert scorer .get_metadata_routing ().score .requests ["sample_weight" ] == "alias"
1279
+ scorer = _PassthroughScorer (
1280
+ estimator = LinearSVC ()
1281
+ .set_score_request (sample_weight = "alias" )
1282
+ .set_fit_request (sample_weight = True )
1283
+ )
1284
+ # test that _PassthroughScorer leaves everything other than `score` empty
1285
+ assert_request_is_empty (scorer .get_metadata_routing (), exclude = "score" )
1286
+ # test that _PassthroughScorer doesn't behave like a router and leaves
1287
+ # the request as is.
1288
+ assert scorer .get_metadata_routing ().score .requests ["sample_weight" ] == "alias"
1291
1289
1292
1290
1293
1291
def test_multimetric_scoring_metadata_routing ():
@@ -1344,5 +1342,7 @@ def score(y_true, y_pred, param=None):
1344
1342
clf = DecisionTreeClassifier ().fit (X , y )
1345
1343
scorer = make_scorer (score )
1346
1344
with config_context (enable_metadata_routing = False ):
1347
- with pytest .raises (ValueError , match = "kwargs is only supported if" ):
1345
+ with pytest .raises (
1346
+ ValueError , match = "is only supported if enable_metadata_routing=True"
1347
+ ):
1348
1348
scorer (clf , X , y , param = "blah" )
0 commit comments