def test_doubleml_exception_learner(): err_msg_prefix = 'Invalid learner provided for ml_g: ' warn_msg_prefix = 'Learner provided for ml_g is probably invalid: ' msg = err_msg_prefix + 'provide an instance of a learner instead of a class.' with pytest.raises(TypeError, match=msg): _ = DoubleMLPLR(dml_data, Lasso, ml_m) msg = err_msg_prefix + r'BaseEstimator\(\) has no method .fit\(\).' with pytest.raises(TypeError, match=msg): _ = DoubleMLPLR(dml_data, BaseEstimator(), ml_m) # msg = err_msg_prefix + r'_DummyNoSetParams\(\) has no method .set_params\(\).' with pytest.raises(TypeError): _ = DoubleMLPLR(dml_data, _DummyNoSetParams(), ml_m) # msg = err_msg_prefix + r'_DummyNoSetParams\(\) has no method .get_params\(\).' with pytest.raises(TypeError): _ = DoubleMLPLR(dml_data, _DummyNoGetParams(), ml_m) # msg = 'Learner provided for ml_m is probably invalid: ' + r'_DummyNoClassifier\(\) is \(probably\) no classifier.' with pytest.warns(UserWarning): _ = DoubleMLIRM(dml_data_irm, Lasso(), _DummyNoClassifier()) # ToDo: Currently for ml_g (and others) we only check whether the learner can be identified as regressor. However, # we do not check whether it can instead be identified as classifier, which could be used to throw an error. msg = warn_msg_prefix + r'LogisticRegression\(\) is \(probably\) no regressor.' with pytest.warns(UserWarning, match=msg): _ = DoubleMLPLR(dml_data, LogisticRegression(), Lasso()) # we allow classifiers for ml_m in PLR, but only for binary treatment variables msg = ( r'The ml_m learner LogisticRegression\(\) was identified as classifier ' 'but at least one treatment variable is not binary with values 0 and 1.' ) with pytest.raises(ValueError, match=msg): _ = DoubleMLPLR(dml_data, Lasso(), LogisticRegression()) # construct a classifier which is not identifiable as classifier via is_classifier by sklearn # it then predicts labels and therefore an exception will be thrown log_reg = LogisticRegression() log_reg._estimator_type = None msg = ( r'Learner provided for ml_m is probably invalid: LogisticRegression\(\) is \(probably\) neither a regressor ' 'nor a classifier. Method predict is used for prediction.') with pytest.warns(UserWarning, match=msg): dml_plr_hidden_classifier = DoubleMLPLR(dml_data_irm, Lasso(), log_reg) msg = ( r'For the binary treatment variable d, predictions obtained with the ml_m learner LogisticRegression\(\) ' 'are also observed to be binary with values 0 and 1. Make sure that for classifiers probabilities and not ' 'labels are predicted.') with pytest.raises(ValueError, match=msg): dml_plr_hidden_classifier.fit()