diff --git a/sklearn/cluster/tests/test_bicluster.py b/sklearn/cluster/tests/test_bicluster.py index 38800de6a59cb..7dd54416f0b04 100644 --- a/sklearn/cluster/tests/test_bicluster.py +++ b/sklearn/cluster/tests/test_bicluster.py @@ -201,16 +201,13 @@ def test_project_and_cluster(): [0, 1], [0, 0]]) for mat in (data, csr_matrix(data)): - labels = model._project_and_cluster(data, vectors, + labels = model._project_and_cluster(mat, vectors, n_clusters=2) assert_almost_equal(v_measure_score(labels, [0, 0, 1, 1]), 1.0) def test_perfect_checkerboard(): - # XXX test always skipped - raise SkipTest("This test is failing on the buildbot, but cannot" - " reproduce. Temporarily disabling it until it can be" - " reproduced and fixed.") + # XXX Previously failed on build bot (not reproducible) model = SpectralBiclustering(3, svd_method="arpack", random_state=0) S, rows, cols = make_checkerboard((30, 30), 3, noise=0, diff --git a/sklearn/compose/tests/test_column_transformer.py b/sklearn/compose/tests/test_column_transformer.py index 2f2985172ea15..f6a49f4cd6601 100644 --- a/sklearn/compose/tests/test_column_transformer.py +++ b/sklearn/compose/tests/test_column_transformer.py @@ -523,7 +523,8 @@ def predict(self, X): X_array = np.array([[0, 1, 2], [2, 4, 6]]).T ct = ColumnTransformer([('trans', NoTrans(), [0])]) - assert_raise_message(TypeError, "All estimators should implement fit", + assert_raise_message(TypeError, + "All estimators should implement fit and transform", ct.fit, X_array) diff --git a/sklearn/datasets/tests/test_openml.py b/sklearn/datasets/tests/test_openml.py index cb1a95c27a077..f9969c75d5c8e 100644 --- a/sklearn/datasets/tests/test_openml.py +++ b/sklearn/datasets/tests/test_openml.py @@ -102,7 +102,7 @@ def _fetch_dataset_from_openml(data_id, data_name, data_version, assert data_by_id.target.shape == (expected_observations, len(target_column)) assert data_by_id.target_names == target_column - assert data_by_id.data.dtype == np.float64 + assert data_by_id.data.dtype == expected_data_dtype assert data_by_id.target.dtype == expected_target_dtype assert len(data_by_id.feature_names) == expected_features for feature in data_by_id.feature_names: @@ -118,11 +118,7 @@ def _fetch_dataset_from_openml(data_id, data_name, data_version, if compare_default_target: # check whether the data by id and data by id target are equal data_by_id_default = fetch_openml(data_id=data_id, cache=False) - if data_by_id.data.dtype == np.float64: - np.testing.assert_allclose(data_by_id.data, - data_by_id_default.data) - else: - assert np.array_equal(data_by_id.data, data_by_id_default.data) + np.testing.assert_allclose(data_by_id.data, data_by_id_default.data) if data_by_id.target.dtype == np.float64: np.testing.assert_allclose(data_by_id.target, data_by_id_default.target) @@ -740,7 +736,7 @@ def test_fetch_openml_iris_multitarget(monkeypatch, gzip_response): _fetch_dataset_from_openml(data_id, data_name, data_version, target_column, expected_observations, expected_features, expected_missing, - object, np.float64, expect_sparse=False, + np.float64, np.float64, expect_sparse=False, compare_default_target=False) @@ -759,7 +755,7 @@ def test_fetch_openml_anneal(monkeypatch, gzip_response): _fetch_dataset_from_openml(data_id, data_name, data_version, target_column, expected_observations, expected_features, expected_missing, - object, object, expect_sparse=False, + np.float64, object, expect_sparse=False, compare_default_target=True) @@ -784,7 +780,7 @@ def test_fetch_openml_anneal_multitarget(monkeypatch, gzip_response): _fetch_dataset_from_openml(data_id, data_name, data_version, target_column, expected_observations, expected_features, expected_missing, - object, object, expect_sparse=False, + np.float64, object, expect_sparse=False, compare_default_target=False) @@ -802,7 +798,7 @@ def test_fetch_openml_cpu(monkeypatch, gzip_response): _fetch_dataset_from_openml(data_id, data_name, data_version, target_column, expected_observations, expected_features, expected_missing, - object, np.float64, expect_sparse=False, + np.float64, np.float64, expect_sparse=False, compare_default_target=True) diff --git a/sklearn/decomposition/tests/test_fastica.py b/sklearn/decomposition/tests/test_fastica.py index 40e71f896f638..9f37ac25c2f76 100644 --- a/sklearn/decomposition/tests/test_fastica.py +++ b/sklearn/decomposition/tests/test_fastica.py @@ -158,7 +158,6 @@ def test_fastica_convergence_fail(): s2 = np.ceil(np.sin(np.pi * t)) s = np.c_[s1, s2].T center_and_norm(s) - s1, s2 = s # Mixing matrix mixing = rng.randn(6, 2) @@ -170,7 +169,8 @@ def test_fastica_convergence_fail(): assert_warns(ConvergenceWarning, ica.fit, m.T) -def test_non_square_fastica(add_noise=False): +@pytest.mark.parametrize('add_noise', [True, False]) +def test_non_square_fastica(add_noise): # Test the FastICA algorithm on very simple data. rng = np.random.RandomState(0) diff --git a/sklearn/decomposition/tests/test_nmf.py b/sklearn/decomposition/tests/test_nmf.py index 4fd21ffbf5b1d..eed09cad19f15 100644 --- a/sklearn/decomposition/tests/test_nmf.py +++ b/sklearn/decomposition/tests/test_nmf.py @@ -1,6 +1,5 @@ import numpy as np import scipy.sparse as sp -import numbers from scipy import linalg from sklearn.decomposition import NMF, non_negative_factorization @@ -10,7 +9,6 @@ import pytest from sklearn.utils._testing import assert_raise_message -from sklearn.utils._testing import assert_warns_message from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_almost_equal @@ -246,11 +244,6 @@ def _beta_divergence_dense(X, W, H, beta): Used as a reference for testing nmf._beta_divergence. """ - if isinstance(X, numbers.Number): - W = np.array([[W]]) - H = np.array([[H]]) - X = np.array([[X]]) - WH = np.dot(W, H) if beta == 2: diff --git a/sklearn/ensemble/_hist_gradient_boosting/tests/test_compare_lightgbm.py b/sklearn/ensemble/_hist_gradient_boosting/tests/test_compare_lightgbm.py index 32bb5dee4b197..c66eb612721c7 100644 --- a/sklearn/ensemble/_hist_gradient_boosting/tests/test_compare_lightgbm.py +++ b/sklearn/ensemble/_hist_gradient_boosting/tests/test_compare_lightgbm.py @@ -13,9 +13,6 @@ get_equivalent_estimator) -pytest.importorskip("lightgbm") - - @pytest.mark.parametrize('seed', range(5)) @pytest.mark.parametrize('min_samples_leaf', (1, 20)) @pytest.mark.parametrize('n_samples, max_leaf_nodes', [ @@ -46,6 +43,7 @@ def test_same_predictions_regression(seed, min_samples_leaf, n_samples, # discrepancy between the initial values leads to biggish differences in # the predictions. These differences are much smaller with more # iterations. + pytest.importorskip("lightgbm") rng = np.random.RandomState(seed=seed) n_samples = n_samples @@ -98,6 +96,7 @@ def test_same_predictions_regression(seed, min_samples_leaf, n_samples, def test_same_predictions_classification(seed, min_samples_leaf, n_samples, max_leaf_nodes): # Same as test_same_predictions_regression but for classification + pytest.importorskip("lightgbm") rng = np.random.RandomState(seed=seed) n_samples = n_samples @@ -158,6 +157,7 @@ def test_same_predictions_classification(seed, min_samples_leaf, n_samples, def test_same_predictions_multiclass_classification( seed, min_samples_leaf, n_samples, max_leaf_nodes): # Same as test_same_predictions_regression but for classification + pytest.importorskip("lightgbm") rng = np.random.RandomState(seed=seed) n_samples = n_samples diff --git a/sklearn/feature_extraction/tests/test_text.py b/sklearn/feature_extraction/tests/test_text.py index 022ce776de092..0b680b2a9e400 100644 --- a/sklearn/feature_extraction/tests/test_text.py +++ b/sklearn/feature_extraction/tests/test_text.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from collections.abc import Mapping import re -import warnings import pytest from scipy import sparse @@ -31,10 +30,11 @@ from numpy.testing import assert_array_equal from sklearn.utils import IS_PYPY from sklearn.utils._testing import (assert_almost_equal, - assert_warns_message, assert_raise_message, - SkipTest, assert_no_warnings, - fails_if_pypy, assert_allclose_dense_sparse, - skip_if_32bit) + assert_warns_message, assert_raise_message, + assert_no_warnings, + fails_if_pypy, + assert_allclose_dense_sparse, + skip_if_32bit) from collections import defaultdict from functools import partial import pickle @@ -296,18 +296,17 @@ def test_countvectorizer_custom_vocabulary_pipeline(): def test_countvectorizer_custom_vocabulary_repeated_indices(): vocab = {"pizza": 0, "beer": 0} - try: - CountVectorizer(vocabulary=vocab) - except ValueError as e: - assert "vocabulary contains repeated indices" in str(e).lower() + msg = "Vocabulary contains repeated indices" + with pytest.raises(ValueError, match=msg): + vect = CountVectorizer(vocabulary=vocab) + vect.fit(["pasta_siziliana"]) def test_countvectorizer_custom_vocabulary_gap_index(): vocab = {"pizza": 1, "beer": 2} - try: - CountVectorizer(vocabulary=vocab) - except ValueError as e: - assert "doesn't contain index" in str(e).lower() + with pytest.raises(ValueError, match="doesn't contain index"): + vect = CountVectorizer(vocabulary=vocab) + vect.fit(['pasta_verdura']) def test_countvectorizer_stop_words(): @@ -326,20 +325,14 @@ def test_countvectorizer_stop_words(): def test_countvectorizer_empty_vocabulary(): - try: + with pytest.raises(ValueError, match="empty vocabulary"): vect = CountVectorizer(vocabulary=[]) vect.fit(["foo"]) - assert False, "we shouldn't get here" - except ValueError as e: - assert "empty vocabulary" in str(e).lower() - try: + with pytest.raises(ValueError, match="empty vocabulary"): v = CountVectorizer(max_df=1.0, stop_words="english") # fit on stopwords only v.fit(["to be or not to be", "and me too", "and so do you"]) - assert False, "we shouldn't get here" - except ValueError as e: - assert "empty vocabulary" in str(e).lower() def test_fit_countvectorizer_twice(): @@ -387,15 +380,9 @@ def test_tfidf_no_smoothing(): [1, 0, 0]] tr = TfidfTransformer(smooth_idf=False, norm='l2') - with warnings.catch_warnings(record=True) as w: - 1. / np.array([0.]) - numpy_provides_div0_warning = len(w) == 1 - in_warning_message = 'divide by zero' - tfidf = assert_warns_message(RuntimeWarning, in_warning_message, - tr.fit_transform, X).toarray() - if not numpy_provides_div0_warning: - raise SkipTest("Numpy does not provide div 0 warnings.") + assert_warns_message(RuntimeWarning, in_warning_message, + tr.fit_transform, X).toarray() def test_sublinear_tf(): @@ -1155,7 +1142,7 @@ def test_vectorizers_invalid_ngram_range(vec): message = ("Invalid value for ngram_range=%s " "lower boundary larger than the upper boundary." % str(invalid_range)) - if isinstance(vec, HashingVectorizer): + if isinstance(vec, HashingVectorizer) and IS_PYPY: pytest.xfail(reason='HashingVectorizer is not supported on PyPy') assert_raise_message( diff --git a/sklearn/feature_selection/tests/test_rfe.py b/sklearn/feature_selection/tests/test_rfe.py index 654675e677a11..ad0773edcb7b8 100644 --- a/sklearn/feature_selection/tests/test_rfe.py +++ b/sklearn/feature_selection/tests/test_rfe.py @@ -30,8 +30,8 @@ class MockClassifier: def __init__(self, foo_param=0): self.foo_param = foo_param - def fit(self, X, Y): - assert len(X) == len(Y) + def fit(self, X, y): + assert len(X) == len(y) self.coef_ = np.ones(X.shape[1], dtype=np.float64) return self @@ -42,12 +42,8 @@ def predict(self, T): decision_function = predict transform = predict - def score(self, X=None, Y=None): - if self.foo_param > 1: - score = 1. - else: - score = 0. - return score + def score(self, X=None, y=None): + return 0. def get_params(self, deep=True): return {'foo_param': self.foo_param} diff --git a/sklearn/feature_selection/tests/test_variance_threshold.py b/sklearn/feature_selection/tests/test_variance_threshold.py index 77d9c9445bc71..23e7703708984 100644 --- a/sklearn/feature_selection/tests/test_variance_threshold.py +++ b/sklearn/feature_selection/tests/test_variance_threshold.py @@ -11,6 +11,7 @@ [0, 2, 2, 3, 5], [1, 1, 2, 4, 0]] +data2 = [[-0.13725701]] * 10 def test_zero_variance(): # Test VarianceThreshold with default setting, zero variance. @@ -32,17 +33,16 @@ def test_variance_threshold(): assert (len(data), 1) == X.shape +@pytest.mark.skipif(np.var(data2) == 0, + reason=('This test is not valid for this platform, ' + 'as it relies on numerical instabilities.')) def test_zero_variance_floating_point_error(): # Test that VarianceThreshold(0.0).fit eliminates features that have # the same value in every sample, even when floating point errors # cause np.var not to be 0 for the feature. # See #13691 - data = [[-0.13725701]] * 10 - if np.var(data) == 0: - pytest.skip('This test is not valid for this platform, as it relies ' - 'on numerical instabilities.') - for X in [data, csr_matrix(data), csc_matrix(data), bsr_matrix(data)]: + for X in [data2, csr_matrix(data2), csc_matrix(data2), bsr_matrix(data2)]: msg = "No feature in X meets the variance threshold 0.00000" with pytest.raises(ValueError, match=msg): VarianceThreshold().fit(X) diff --git a/sklearn/gaussian_process/tests/test_kernels.py b/sklearn/gaussian_process/tests/test_kernels.py index e282786caf5ce..9e2248a66ee28 100644 --- a/sklearn/gaussian_process/tests/test_kernels.py +++ b/sklearn/gaussian_process/tests/test_kernels.py @@ -250,6 +250,7 @@ def test_kernel_clone_after_set_params(kernel): isotropic_kernels): length_scale = params['length_scale'] if np.iterable(length_scale): + # XXX unreached code as of v0.22 params['length_scale'] = length_scale[0] params['length_scale_bounds'] = bounds else: diff --git a/sklearn/impute/tests/test_impute.py b/sklearn/impute/tests/test_impute.py index 052a99908c569..5fe35fffd960f 100644 --- a/sklearn/impute/tests/test_impute.py +++ b/sklearn/impute/tests/test_impute.py @@ -177,6 +177,7 @@ def test_imputation_mean_median(): X[:, j] = np.hstack((v, z, p)) if 0 == test_missing_values: + # XXX unreached code as of v0.22 X_true[:, j] = np.hstack((v, np.repeat( true_statistics[j], @@ -706,7 +707,6 @@ def test_iterative_imputer_truncated_normal_posterior(): # note that starting from the wrong random seed will make this test fail # because random sampling doesn't occur at all when the imputation # is outside of the (min_value, max_value) range - pytest.importorskip("scipy", minversion="0.17.0") rng = np.random.RandomState(42) X = rng.normal(size=(5, 5)) @@ -763,7 +763,6 @@ def test_iterative_imputer_missing_at_transform(strategy): def test_iterative_imputer_transform_stochasticity(): - pytest.importorskip("scipy", minversion="0.17.0") rng1 = np.random.RandomState(0) rng2 = np.random.RandomState(1) n = 100 diff --git a/sklearn/impute/tests/test_knn.py b/sklearn/impute/tests/test_knn.py index e9c89c03f89b8..c7a2b5f60de45 100644 --- a/sklearn/impute/tests/test_knn.py +++ b/sklearn/impute/tests/test_knn.py @@ -5,18 +5,9 @@ from sklearn.metrics.pairwise import nan_euclidean_distances from sklearn.metrics.pairwise import pairwise_distances from sklearn.neighbors import KNeighborsRegressor -from sklearn.utils._mask import _get_mask from sklearn.utils._testing import assert_allclose -def _missing_mean(X, missing_value): - masked_X = np.ma.array(X, mask=_get_mask(X, missing_value)) - masked_X_mean = masked_X.mean(axis=0) - output = masked_X_mean.data - output[masked_X_mean.mask] = np.nan - return output - - @pytest.mark.parametrize("weights", ["uniform", "distance"]) @pytest.mark.parametrize("n_neighbors", range(1, 6)) def test_knn_imputer_shape(weights, n_neighbors): diff --git a/sklearn/linear_model/tests/test_logistic.py b/sklearn/linear_model/tests/test_logistic.py index 3c4ddda1d7b0e..3590793de5071 100644 --- a/sklearn/linear_model/tests/test_logistic.py +++ b/sklearn/linear_model/tests/test_logistic.py @@ -1723,7 +1723,7 @@ def fit(X, y, **kw): if sys.platform == 'darwin' and solver == 'lbfgs': pytest.xfail('Issue #11924: LogisticRegressionCV(solver="lbfgs", ' 'multi_class="multinomial") is nondterministic on ' - 'MacOS.') # pragma: no cover + 'MacOS.') assert_allclose(est_auto_multi.coef_, est_multi_multi.coef_) assert_allclose(est_auto_multi.predict_proba(X2), est_multi_multi.predict_proba(X2)) diff --git a/sklearn/linear_model/tests/test_sgd.py b/sklearn/linear_model/tests/test_sgd.py index 1d7c582c51a7d..d9dd082242a79 100644 --- a/sklearn/linear_model/tests/test_sgd.py +++ b/sklearn/linear_model/tests/test_sgd.py @@ -62,6 +62,7 @@ def partial_fit(self, X, y, *args, **kw): return linear_model.SGDRegressor.partial_fit(self, X, y, *args, **kw) def decision_function(self, X, *args, **kw): + # XXX untested as of v0.22 X = sp.csr_matrix(X) return linear_model.SGDRegressor.decision_function(self, X, *args, **kw) @@ -1563,11 +1564,6 @@ def test_multi_core_gridsearch_and_early_stopping(): assert search.best_score_ > 0.8 -@pytest.mark.skipif( - not hasattr(sp, "random"), - reason="this test uses scipy.random, that was introduced in version " - "0.17. This skip condition can be dropped as soon as we drop " - "support for scipy versions older than 0.17") @pytest.mark.parametrize("backend", ["loky", "multiprocessing", "threading"]) def test_SGDClassifier_fit_for_all_backends(backend): diff --git a/sklearn/manifold/tests/test_spectral_embedding.py b/sklearn/manifold/tests/test_spectral_embedding.py index 49bf413516e74..3d9d87af3a09a 100644 --- a/sklearn/manifold/tests/test_spectral_embedding.py +++ b/sklearn/manifold/tests/test_spectral_embedding.py @@ -32,18 +32,13 @@ cluster_std=1., random_state=42) -def _check_with_col_sign_flipping(A, B, tol=0.0): +def _assert_equal_with_sign_flipping(A, B, tol=0.0): """ Check array A and B are equal with possible sign flipping on each columns""" - sign = True - for column_idx in range(A.shape[1]): - sign = sign and ((((A[:, column_idx] - - B[:, column_idx]) ** 2).mean() <= tol ** 2) or - (((A[:, column_idx] + - B[:, column_idx]) ** 2).mean() <= tol ** 2)) - if not sign: - return False - return True + tol_squared = tol ** 2 + for A_col, B_col in zip(A.T, B.T): + assert (np.max((A_col - B_col) ** 2) <= tol_squared or + np.max((A_col + B_col) ** 2) <= tol_squared) def test_sparse_graph_connected_component(): @@ -139,7 +134,7 @@ def test_spectral_embedding_precomputed_affinity(X, seed=36): embed_rbf = se_rbf.fit_transform(X) assert_array_almost_equal( se_precomp.affinity_matrix_, se_rbf.affinity_matrix_) - assert _check_with_col_sign_flipping(embed_precomp, embed_rbf, 0.05) + _assert_equal_with_sign_flipping(embed_precomp, embed_rbf, 0.05) def test_precomputed_nearest_neighbors_filtering(): @@ -178,7 +173,7 @@ def test_spectral_embedding_callable_affinity(X, seed=36): assert_array_almost_equal( se_callable.affinity_matrix_, se_rbf.affinity_matrix_) assert_array_almost_equal(kern, se_rbf.affinity_matrix_) - assert _check_with_col_sign_flipping(embed_rbf, embed_callable, 0.05) + _assert_equal_with_sign_flipping(embed_rbf, embed_callable, 0.05) # TODO: Remove when pyamg does replaces sp.rand call with np.random.rand @@ -197,7 +192,7 @@ def test_spectral_embedding_amg_solver(seed=36): random_state=np.random.RandomState(seed)) embed_amg = se_amg.fit_transform(S) embed_arpack = se_arpack.fit_transform(S) - assert _check_with_col_sign_flipping(embed_amg, embed_arpack, 1e-5) + _assert_equal_with_sign_flipping(embed_amg, embed_arpack, 1e-5) # same with special case in which amg is not actually used # regression test for #10715 @@ -212,7 +207,7 @@ def test_spectral_embedding_amg_solver(seed=36): se_arpack.affinity = "precomputed" embed_amg = se_amg.fit_transform(affinity) embed_arpack = se_arpack.fit_transform(affinity) - assert _check_with_col_sign_flipping(embed_amg, embed_arpack, 1e-5) + _assert_equal_with_sign_flipping(embed_amg, embed_arpack, 1e-5) # TODO: Remove filterwarnings when pyamg does replaces sp.rand call with @@ -239,8 +234,7 @@ def test_spectral_embedding_amg_solver_failure(): n_components=10, eigen_solver='amg', random_state=i + 1) - assert _check_with_col_sign_flipping( - embedding, new_embedding, tol=0.05) + _assert_equal_with_sign_flipping(embedding, new_embedding, tol=0.05) @pytest.mark.filterwarnings("ignore:the behavior of nmi will " diff --git a/sklearn/metrics/tests/test_common.py b/sklearn/metrics/tests/test_common.py index 331bcf197dccb..7301d21a35f39 100644 --- a/sklearn/metrics/tests/test_common.py +++ b/sklearn/metrics/tests/test_common.py @@ -1168,8 +1168,7 @@ def check_sample_weight_invariance(name, metric, y1, y2): assert_allclose(unweighted_score, weighted_score) raise ValueError("Unweighted and weighted scores are unexpectedly " "almost equal (%s) and (%s) " - "for %s" % (unweighted_score, - weighted_score, name)) + "for %s" % (unweighted_score, weighted_score, name)) # check that sample_weight can be a list weighted_score_list = metric(y1, y2, diff --git a/sklearn/mixture/tests/test_gaussian_mixture.py b/sklearn/mixture/tests/test_gaussian_mixture.py index ac0a829d2bc24..1282a752271a8 100644 --- a/sklearn/mixture/tests/test_gaussian_mixture.py +++ b/sklearn/mixture/tests/test_gaussian_mixture.py @@ -914,12 +914,8 @@ def test_monotonic_likelihood(): # training log likelihood increases after each iteration. for _ in range(600): prev_log_likelihood = current_log_likelihood - try: - current_log_likelihood = gmm.fit(X).score(X) - except ConvergenceWarning: - pass - assert (current_log_likelihood >= - prev_log_likelihood) + current_log_likelihood = gmm.fit(X).score(X) + assert current_log_likelihood >= prev_log_likelihood if gmm.converged_: break diff --git a/sklearn/model_selection/tests/test_search.py b/sklearn/model_selection/tests/test_search.py index bacfc20eb1fc1..30ff1b90de277 100644 --- a/sklearn/model_selection/tests/test_search.py +++ b/sklearn/model_selection/tests/test_search.py @@ -1119,7 +1119,6 @@ def test_random_search_cv_results_multimetric(): n_splits = 3 n_search_iter = 30 - scoring = ('accuracy', 'recall') # Scipy 0.12's stats dists do not accept seed, hence we use param grid params = dict(C=np.logspace(-4, 1, 3), @@ -1145,9 +1144,8 @@ def test_random_search_cv_results_multimetric(): compare_cv_results_multimetric_with_single(*random_searches, iid=iid) - if refit: - compare_refit_methods_when_refit_with_acc( - random_searches[0], random_searches[1], refit) + compare_refit_methods_when_refit_with_acc( + random_searches[0], random_searches[1], refit) @pytest.mark.filterwarnings("ignore:The parameter 'iid' is deprecated") # 0.24 @@ -1184,11 +1182,12 @@ def compare_cv_results_multimetric_with_single( def compare_refit_methods_when_refit_with_acc(search_multi, search_acc, refit): """Compare refit multi-metric search methods with single metric methods""" + assert search_acc.refit == refit if refit: assert search_multi.refit == 'accuracy' else: assert not search_multi.refit - assert search_acc.refit == refit + return # search cannot predict/score without refit X, y = make_blobs(n_samples=100, n_features=4, random_state=42) for method in ('predict', 'predict_proba', 'predict_log_proba'): diff --git a/sklearn/model_selection/tests/test_split.py b/sklearn/model_selection/tests/test_split.py index 253593968ad24..875e113f8dc36 100644 --- a/sklearn/model_selection/tests/test_split.py +++ b/sklearn/model_selection/tests/test_split.py @@ -63,69 +63,6 @@ digits = load_digits() -class MockClassifier: - """Dummy classifier to test the cross-validation""" - - def __init__(self, a=0, allow_nd=False): - self.a = a - self.allow_nd = allow_nd - - def fit(self, X, Y=None, sample_weight=None, class_prior=None, - sparse_sample_weight=None, sparse_param=None, dummy_int=None, - dummy_str=None, dummy_obj=None, callback=None): - """The dummy arguments are to test that this fit function can - accept non-array arguments through cross-validation, such as: - - int - - str (this is actually array-like) - - object - - function - """ - self.dummy_int = dummy_int - self.dummy_str = dummy_str - self.dummy_obj = dummy_obj - if callback is not None: - callback(self) - - if self.allow_nd: - X = X.reshape(len(X), -1) - if X.ndim >= 3 and not self.allow_nd: - raise ValueError('X cannot be d') - if sample_weight is not None: - assert sample_weight.shape[0] == X.shape[0], ( - 'MockClassifier extra fit_param sample_weight.shape[0]' - ' is {0}, should be {1}'.format(sample_weight.shape[0], - X.shape[0])) - if class_prior is not None: - assert class_prior.shape[0] == len(np.unique(y)), ( - 'MockClassifier extra fit_param class_prior.shape[0]' - ' is {0}, should be {1}'.format(class_prior.shape[0], - len(np.unique(y)))) - if sparse_sample_weight is not None: - fmt = ('MockClassifier extra fit_param sparse_sample_weight' - '.shape[0] is {0}, should be {1}') - assert sparse_sample_weight.shape[0] == X.shape[0], \ - fmt.format(sparse_sample_weight.shape[0], X.shape[0]) - if sparse_param is not None: - fmt = ('MockClassifier extra fit_param sparse_param.shape ' - 'is ({0}, {1}), should be ({2}, {3})') - assert sparse_param.shape == P_sparse.shape, ( - fmt.format(sparse_param.shape[0], - sparse_param.shape[1], - P_sparse.shape[0], P_sparse.shape[1])) - return self - - def predict(self, T): - if self.allow_nd: - T = T.reshape(len(T), -1) - return T[:, 0] - - def score(self, X=None, Y=None): - return 1. / (1 + np.abs(self.a)) - - def get_params(self, deep=False): - return {'a': self.a, 'allow_nd': self.allow_nd} - - @ignore_warnings def test_cross_validator_with_default_params(): n_samples = 4 @@ -227,13 +164,10 @@ def check_valid_split(train, test, n_samples=None): assert train.union(test) == set(range(n_samples)) -def check_cv_coverage(cv, X, y, groups, expected_n_splits=None): +def check_cv_coverage(cv, X, y, groups, expected_n_splits): n_samples = _num_samples(X) # Check that a all the samples appear at least once in a test fold - if expected_n_splits is not None: - assert cv.get_n_splits(X, y, groups) == expected_n_splits - else: - expected_n_splits = cv.get_n_splits(X, y, groups) + assert cv.get_n_splits(X, y, groups) == expected_n_splits collected_test_samples = set() iterations = 0 @@ -1345,9 +1279,9 @@ def test_cv_iterable_wrapper(): list(kf_randomized_iter_wrapped.split(X, y))) try: + splits_are_equal = True np.testing.assert_equal(list(kf_iter_wrapped.split(X, y)), list(kf_randomized_iter_wrapped.split(X, y))) - splits_are_equal = True except AssertionError: splits_are_equal = False assert not splits_are_equal, ( diff --git a/sklearn/neighbors/tests/test_ball_tree.py b/sklearn/neighbors/tests/test_ball_tree.py index b707e21c1b3ff..8da703dbe207d 100644 --- a/sklearn/neighbors/tests/test_ball_tree.py +++ b/sklearn/neighbors/tests/test_ball_tree.py @@ -30,10 +30,6 @@ 'sokalsneath'] -def dist_func(x1, x2, p): - return np.sum((x1 - x2) ** p) ** (1. / p) - - def brute_force_neighbors(X, Y, k, metric, **kwargs): D = DistanceMetric.get_metric(metric, **kwargs).pairwise(Y, X) ind = np.argsort(D, axis=1)[:, :k] diff --git a/sklearn/neighbors/tests/test_dist_metrics.py b/sklearn/neighbors/tests/test_dist_metrics.py index b7939d2a0e680..7a2ec2ce4cd73 100644 --- a/sklearn/neighbors/tests/test_dist_metrics.py +++ b/sklearn/neighbors/tests/test_dist_metrics.py @@ -191,7 +191,7 @@ def wrong_distance(x, y): def test_input_data_size(): # Regression test for #6288 - # Previoulsly, a metric requiring a particular input dimension would fail + # Previously, a metric requiring a particular input dimension would fail def custom_metric(x, y): assert x.shape[0] == 3 return np.sum((x - y) ** 2) @@ -199,6 +199,6 @@ def custom_metric(x, y): rng = check_random_state(0) X = rng.rand(10, 3) - pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2) + pyfunc = DistanceMetric.get_metric("pyfunc", func=custom_metric) eucl = DistanceMetric.get_metric("euclidean") - assert_array_almost_equal(pyfunc.pairwise(X), eucl.pairwise(X)) + assert_array_almost_equal(pyfunc.pairwise(X), eucl.pairwise(X) ** 2) diff --git a/sklearn/neighbors/tests/test_kde.py b/sklearn/neighbors/tests/test_kde.py index 1fdbc0f352853..69aca8e8f75b8 100644 --- a/sklearn/neighbors/tests/test_kde.py +++ b/sklearn/neighbors/tests/test_kde.py @@ -12,6 +12,7 @@ import joblib +# XXX Duplicated in test_neighbors_tree, test_kde def compute_kernel_slow(Y, X, kernel, h): d = np.sqrt(((Y[:, None, :] - X) ** 2).sum(-1)) norm = kernel_norm(h, X.shape[1], kernel) / X.shape[0] diff --git a/sklearn/neighbors/tests/test_lof.py b/sklearn/neighbors/tests/test_lof.py index e8ddfb7090735..750fc57a8f457 100644 --- a/sklearn/neighbors/tests/test_lof.py +++ b/sklearn/neighbors/tests/test_lof.py @@ -7,6 +7,7 @@ import numpy as np from sklearn import neighbors +import pytest from numpy.testing import assert_array_equal from sklearn import metrics @@ -214,12 +215,12 @@ def test_novelty_true_common_tests(): check_estimator(neighbors.LocalOutlierFactor(novelty=True)) -def test_predicted_outlier_number(): +@pytest.mark.parametrize('expected_outliers', [30, 53]) +def test_predicted_outlier_number(expected_outliers): # the number of predicted outliers should be equal to the number of # expected outliers unless there are ties in the abnormality scores. X = iris.data n_samples = X.shape[0] - expected_outliers = 30 contamination = float(expected_outliers)/n_samples clf = neighbors.LocalOutlierFactor(contamination=contamination) diff --git a/sklearn/preprocessing/tests/test_data.py b/sklearn/preprocessing/tests/test_data.py index cdff446cb336c..cb5959c0e49b6 100644 --- a/sklearn/preprocessing/tests/test_data.py +++ b/sklearn/preprocessing/tests/test_data.py @@ -80,9 +80,7 @@ def toarray(a): def _check_dim_1axis(a): - if isinstance(a, list): - return np.array(a).shape[0] - return a.shape[0] + return np.asarray(a).shape[0] def assert_correct_incr(i, batch_start, batch_stop, n, chunk_size, diff --git a/sklearn/preprocessing/tests/test_encoders.py b/sklearn/preprocessing/tests/test_encoders.py index f6283c71313b6..2a872c2e06c49 100644 --- a/sklearn/preprocessing/tests/test_encoders.py +++ b/sklearn/preprocessing/tests/test_encoders.py @@ -14,12 +14,6 @@ from sklearn.preprocessing import OrdinalEncoder -def toarray(a): - if hasattr(a, "toarray"): - a = a.toarray() - return a - - def test_one_hot_encoder_sparse_dense(): # check that sparse and dense will give the same results diff --git a/sklearn/svm/_base.py b/sklearn/svm/_base.py index 5a183a2fdfe7c..1f81987f78c52 100644 --- a/sklearn/svm/_base.py +++ b/sklearn/svm/_base.py @@ -75,7 +75,7 @@ def __init__(self, kernel, degree, gamma, coef0, tol, C, nu, epsilon, shrinking, probability, cache_size, class_weight, verbose, max_iter, random_state): - if self._impl not in LIBSVM_IMPL: # pragma: no cover + if self._impl not in LIBSVM_IMPL: raise ValueError("impl should be one of %s, %s was given" % ( LIBSVM_IMPL, self._impl)) @@ -200,7 +200,7 @@ def fit(self, X, y, sample_weight=None): self._gamma = self.gamma fit = self._sparse_fit if self._sparse else self._dense_fit - if self.verbose: # pragma: no cover + if self.verbose: print('[LibSVM]', end='') seed = rnd.randint(np.iinfo('i').max) diff --git a/sklearn/svm/tests/test_svm.py b/sklearn/svm/tests/test_svm.py index ceabcb83056cb..191420d1d7147 100644 --- a/sklearn/svm/tests/test_svm.py +++ b/sklearn/svm/tests/test_svm.py @@ -690,11 +690,8 @@ def test_unicode_kernel(): def test_sparse_precomputed(): clf = svm.SVC(kernel='precomputed') sparse_gram = sparse.csr_matrix([[1, 0], [0, 1]]) - try: + with pytest.raises(TypeError, match="Sparse precomputed"): clf.fit(sparse_gram, [0, 1]) - assert not "reached" - except TypeError as e: - assert "Sparse precomputed" in str(e) def test_sparse_fit_support_vectors_empty(): diff --git a/sklearn/tests/test_common.py b/sklearn/tests/test_common.py index 08388289bd043..b9fa122e2c888 100644 --- a/sklearn/tests/test_common.py +++ b/sklearn/tests/test_common.py @@ -23,7 +23,7 @@ from sklearn.utils.estimator_checks import check_estimator import sklearn -from sklearn.base import RegressorMixin, BiclusterMixin +from sklearn.base import BiclusterMixin from sklearn.linear_model._base import LinearClassifierMixin from sklearn.linear_model import LogisticRegression @@ -131,7 +131,8 @@ def test_configure(): setup_path = os.path.abspath(os.path.join(sklearn.__path__[0], '..')) setup_filename = os.path.join(setup_path, 'setup.py') if not os.path.exists(setup_filename): - return + pytest.skip('setup.py not available') + # XXX unreached code as of v0.22 try: os.chdir(setup_path) old_argv = sys.argv @@ -184,10 +185,8 @@ def test_import_all_consistency(): continue package = __import__(modname, fromlist="dummy") for name in getattr(package, '__all__', ()): - if getattr(package, name, None) is None: - raise AttributeError( - "Module '{0}' has no attribute '{1}'".format( - modname, name)) + assert hasattr(package, name),\ + "Module '{0}' has no attribute '{1}'".format(modname, name) def test_root_import_all_completeness(): diff --git a/sklearn/tests/test_docstring_parameters.py b/sklearn/tests/test_docstring_parameters.py index b0f3d9e336e8d..dc5cac756fd67 100644 --- a/sklearn/tests/test_docstring_parameters.py +++ b/sklearn/tests/test_docstring_parameters.py @@ -11,7 +11,6 @@ import sklearn from sklearn.utils import IS_PYPY -from sklearn.utils._testing import SkipTest from sklearn.utils._testing import check_docstring_parameters from sklearn.utils._testing import _get_func_name from sklearn.utils._testing import ignore_warnings @@ -61,11 +60,10 @@ def test_docstring_parameters(): # Test module docstring formatting # Skip test if numpydoc is not found - try: - import numpydoc # noqa - except ImportError: - raise SkipTest("numpydoc is required to test the docstrings") + pytest.importorskip('numpydoc', + reason="numpydoc is required to test the docstrings") + # XXX unreached code as of v0.22 from numpydoc import docscrape incorrect = [] @@ -159,7 +157,7 @@ def test_tabs(): except IOError: # user probably should have run "make clean" continue assert '\t' not in source, ('"%s" has tabs, please remove them ', - 'or add it to theignore list' + 'or add it to the ignore list' % modname) diff --git a/sklearn/tests/test_kernel_approximation.py b/sklearn/tests/test_kernel_approximation.py index b0cc584dff209..8d37ce218f227 100644 --- a/sklearn/tests/test_kernel_approximation.py +++ b/sklearn/tests/test_kernel_approximation.py @@ -20,6 +20,10 @@ Y /= Y.sum(axis=1)[:, np.newaxis] +def _linear_kernel(X, Y): + return np.dot(X, Y.T) + + def test_additive_chi2_sampler(): # test that AdditiveChi2Sampler approximates kernel on random data @@ -176,9 +180,7 @@ def test_nystroem_approximation(): assert X_transformed.shape == (X.shape[0], 2) # test callable kernel - def linear_kernel(X, Y): - return np.dot(X, Y.T) - trans = Nystroem(n_components=2, kernel=linear_kernel, random_state=rnd) + trans = Nystroem(n_components=2, kernel=_linear_kernel, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert X_transformed.shape == (X.shape[0], 2) @@ -256,14 +258,11 @@ def logging_histogram_kernel(x, y, log): kernel_params={'log': kernel_log}).fit(X) assert len(kernel_log) == n_samples * (n_samples - 1) / 2 - def linear_kernel(X, Y): - return np.dot(X, Y.T) - # if degree, gamma or coef0 is passed, we raise a warning msg = "Don't pass gamma, coef0 or degree to Nystroem" params = ({'gamma': 1}, {'coef0': 1}, {'degree': 2}) for param in params: - ny = Nystroem(kernel=linear_kernel, **param) + ny = Nystroem(kernel=_linear_kernel, **param) with pytest.raises(ValueError, match=msg): ny.fit(X) diff --git a/sklearn/tree/tests/test_reingold_tilford.py b/sklearn/tree/tests/test_reingold_tilford.py index dfab29d0705c0..6494536004333 100644 --- a/sklearn/tree/tests/test_reingold_tilford.py +++ b/sklearn/tree/tests/test_reingold_tilford.py @@ -43,8 +43,8 @@ def walk_tree(draw_tree): # we could also do it quicker using defaultdicts.. depth = 0 while True: - x_at_this_depth = [coordinates[0] for node in coordinates - if coordinates[1] == depth] + x_at_this_depth = [node[0] for node in coordinates + if node[1] == depth] if not x_at_this_depth: # reached all leafs break diff --git a/sklearn/utils/_mocking.py b/sklearn/utils/_mocking.py index e7525460fc975..07c7b9a70ca05 100644 --- a/sklearn/utils/_mocking.py +++ b/sklearn/utils/_mocking.py @@ -163,4 +163,4 @@ def predict_proba(self, X): return self.est.predict_proba(X) def _more_tags(self): - return {'_skip_test': True} # pragma: no cover + return {'_skip_test': True} diff --git a/sklearn/utils/tests/test_cython_blas.py b/sklearn/utils/tests/test_cython_blas.py index d55b274a6f0db..eb33e9455a563 100644 --- a/sklearn/utils/tests/test_cython_blas.py +++ b/sklearn/utils/tests/test_cython_blas.py @@ -17,10 +17,15 @@ from sklearn.utils._cython_blas import RowMajor, ColMajor from sklearn.utils._cython_blas import Trans, NoTrans -cython = pytest.importorskip("cython") + +def _numpy_to_cython(dtype): + cython = pytest.importorskip("cython") + if dtype == np.float32: + return cython.float + elif dtype == np.float64: + return cython.double -NUMPY_TO_CYTHON = {np.float32: cython.float, np.float64: cython.double} RTOL = {np.float32: 1e-6, np.float64: 1e-12} ORDER = {RowMajor: 'C', ColMajor: 'F'} @@ -31,7 +36,7 @@ def _no_op(x): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_dot(dtype): - dot = _dot_memview[NUMPY_TO_CYTHON[dtype]] + dot = _dot_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -45,7 +50,7 @@ def test_dot(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_asum(dtype): - asum = _asum_memview[NUMPY_TO_CYTHON[dtype]] + asum = _asum_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -58,7 +63,7 @@ def test_asum(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_axpy(dtype): - axpy = _axpy_memview[NUMPY_TO_CYTHON[dtype]] + axpy = _axpy_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -73,7 +78,7 @@ def test_axpy(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_nrm2(dtype): - nrm2 = _nrm2_memview[NUMPY_TO_CYTHON[dtype]] + nrm2 = _nrm2_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -86,7 +91,7 @@ def test_nrm2(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_copy(dtype): - copy = _copy_memview[NUMPY_TO_CYTHON[dtype]] + copy = _copy_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -100,7 +105,7 @@ def test_copy(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_scal(dtype): - scal = _scal_memview[NUMPY_TO_CYTHON[dtype]] + scal = _scal_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -114,7 +119,7 @@ def test_scal(dtype): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_rotg(dtype): - rotg = _rotg_memview[NUMPY_TO_CYTHON[dtype]] + rotg = _rotg_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) a = dtype(rng.randn()) @@ -139,7 +144,7 @@ def expected_rotg(a, b): @pytest.mark.parametrize("dtype", [np.float32, np.float64]) def test_rot(dtype): - rot = _rot_memview[NUMPY_TO_CYTHON[dtype]] + rot = _rot_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -163,7 +168,7 @@ def test_rot(dtype): @pytest.mark.parametrize("order", [RowMajor, ColMajor], ids=["RowMajor", "ColMajor"]) def test_gemv(dtype, opA, transA, order): - gemv = _gemv_memview[NUMPY_TO_CYTHON[dtype]] + gemv = _gemv_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) A = np.asarray(opA(rng.random_sample((20, 10)).astype(dtype, copy=False)), @@ -182,7 +187,7 @@ def test_gemv(dtype, opA, transA, order): @pytest.mark.parametrize("order", [RowMajor, ColMajor], ids=["RowMajor", "ColMajor"]) def test_ger(dtype, order): - ger = _ger_memview[NUMPY_TO_CYTHON[dtype]] + ger = _ger_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) x = rng.random_sample(10).astype(dtype, copy=False) @@ -207,7 +212,7 @@ def test_ger(dtype, order): @pytest.mark.parametrize("order", [RowMajor, ColMajor], ids=["RowMajor", "ColMajor"]) def test_gemm(dtype, opA, transA, opB, transB, order): - gemm = _gemm_memview[NUMPY_TO_CYTHON[dtype]] + gemm = _gemm_memview[_numpy_to_cython(dtype)] rng = np.random.RandomState(0) A = np.asarray(opA(rng.random_sample((30, 10)).astype(dtype, copy=False)), diff --git a/sklearn/utils/tests/test_deprecated_utils.py b/sklearn/utils/tests/test_deprecated_utils.py index 08bd95aacc284..c23c72866666b 100644 --- a/sklearn/utils/tests/test_deprecated_utils.py +++ b/sklearn/utils/tests/test_deprecated_utils.py @@ -74,9 +74,6 @@ def func(x): def grad(x): return A.T.dot(A.dot(x)) - def hess(x, p): - return p.dot(A.T.dot(A.dot(x.all()))) - def grad_hess(x): return grad(x), lambda x: A.T.dot(A.dot(x)) diff --git a/sklearn/utils/tests/test_estimator_checks.py b/sklearn/utils/tests/test_estimator_checks.py index 2fdad773e624f..cc017b6667658 100644 --- a/sklearn/utils/tests/test_estimator_checks.py +++ b/sklearn/utils/tests/test_estimator_checks.py @@ -10,9 +10,9 @@ from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.utils import deprecated from sklearn.utils._testing import (assert_raises_regex, - ignore_warnings, - assert_warns, assert_raises, - SkipTest) + ignore_warnings, + assert_warns, assert_raises, + SkipTest) from sklearn.utils.estimator_checks import check_estimator, _NotAnArray from sklearn.utils.estimator_checks \ import check_class_weight_balanced_linear_classifier @@ -260,9 +260,8 @@ def fit(self, X, y): raise ValueError( "Estimator doesn't support 64-bit indices") elif X.getformat() in ["csc", "csr"]: - if X.indices.dtype == "int64" or X.indptr.dtype == "int64": - raise ValueError( - "Estimator doesn't support 64-bit indices") + assert "int64" not in (X.indices.dtype, X.indptr.dtype),\ + "Estimator doesn't support 64-bit indices" return self diff --git a/sklearn/utils/tests/test_extmath.py b/sklearn/utils/tests/test_extmath.py index fdca303e15d8b..2abcbfa3c74e7 100644 --- a/sklearn/utils/tests/test_extmath.py +++ b/sklearn/utils/tests/test_extmath.py @@ -545,15 +545,8 @@ def naive_mean_variance_update(x, last_mean, last_variance, A1 = np.full((n_samples // 2, n_features), x2, dtype=np.float64) A = np.vstack((A0, A1)) - # Older versions of numpy have different precision - # In some old version, np.var is not stable - if np.abs(np_var(A) - two_pass_var(A)).max() < 1e-6: - stable_var = np_var - else: - stable_var = two_pass_var - # Naive one pass var: >tol (=1063) - assert np.abs(stable_var(A) - one_pass_var(A)).max() > tol + assert np.abs(np_var(A) - one_pass_var(A)).max() > tol # Starting point for online algorithms: after A0 @@ -565,7 +558,7 @@ def naive_mean_variance_update(x, last_mean, last_variance, assert n == A.shape[0] # the mean is also slightly unstable assert np.abs(A.mean(axis=0) - mean).max() > 1e-6 - assert np.abs(stable_var(A) - var).max() > tol + assert np.abs(np_var(A) - var).max() > tol # Robust implementation: np.abs(stable_var(A) - var).max() + assert tol > np.abs(np_var(A) - var).max() def test_incremental_variance_ddof(): diff --git a/sklearn/utils/tests/test_pprint.py b/sklearn/utils/tests/test_pprint.py index 556d57e9f8dfa..146ccf781ae8a 100644 --- a/sklearn/utils/tests/test_pprint.py +++ b/sklearn/utils/tests/test_pprint.py @@ -371,6 +371,7 @@ def test_gridsearch_pipeline(): 'function chi2 at some_address>', repr_) assert repr_ == expected + def test_n_max_elements_to_show(): n_max_elements_to_show = 30 @@ -397,7 +398,7 @@ def test_n_max_elements_to_show(): 27: 27, 28: 28, 29: 29})""" expected = expected[1:] # remove first \n - assert pp.pformat(vectorizer) == expected + assert pp.pformat(vectorizer) == expected # Now with ellipsis vocabulary = {i: i for i in range(n_max_elements_to_show + 1)} @@ -417,7 +418,7 @@ def test_n_max_elements_to_show(): 27: 27, 28: 28, 29: 29, ...})""" expected = expected[1:] # remove first \n - assert pp.pformat(vectorizer) == expected + assert pp.pformat(vectorizer) == expected # Also test with lists param_grid = {'C': list(range(n_max_elements_to_show))} @@ -437,7 +438,7 @@ def test_n_max_elements_to_show(): scoring=None, verbose=0)""" expected = expected[1:] # remove first \n - assert pp.pformat(gs) == expected + assert pp.pformat(gs) == expected # Now with ellipsis param_grid = {'C': list(range(n_max_elements_to_show + 1))} @@ -457,7 +458,7 @@ def test_n_max_elements_to_show(): scoring=None, verbose=0)""" expected = expected[1:] # remove first \n - assert pp.pformat(gs) == expected + assert pp.pformat(gs) == expected def test_bruteforce_ellipsis(): @@ -533,6 +534,7 @@ def test_bruteforce_ellipsis(): expected = expected[1:] # remove first \n assert expected == lr.__repr__(N_CHAR_MAX=n_nonblank - 2) + def test_builtin_prettyprinter(): # non regression test than ensures we can still use the builtin # PrettyPrinter class for estimators (as done e.g. by joblib). diff --git a/sklearn/utils/tests/test_sparsefuncs.py b/sklearn/utils/tests/test_sparsefuncs.py index 680c9738e5cec..443a4a3229311 100644 --- a/sklearn/utils/tests/test_sparsefuncs.py +++ b/sklearn/utils/tests/test_sparsefuncs.py @@ -473,7 +473,7 @@ def test_count_nonzero(): count_nonzero(X_csr, axis=1, sample_weight=sample_weight).dtype) # Check dtypes with large sparse matrices too - # XXX: test fails on Appveyor (python3.5 32bit) + # XXX: test fails on 32bit (Windows/Linux) try: X_csr.indices = X_csr.indices.astype(np.int64) X_csr.indptr = X_csr.indptr.astype(np.int64) @@ -484,11 +484,8 @@ def test_count_nonzero(): count_nonzero(X_csr, axis=1, sample_weight=sample_weight).dtype) except TypeError as e: - if ("according to the rule 'safe'" in e.args[0] and - np.intp().nbytes < 8): - pass - else: - raise + assert ("according to the rule 'safe'" in e.args[0] + and np.intp().nbytes < 8), e def test_csc_row_median(): diff --git a/sklearn/utils/tests/test_testing.py b/sklearn/utils/tests/test_testing.py index b304b2c0ad18b..4eafaad97fbb2 100644 --- a/sklearn/utils/tests/test_testing.py +++ b/sklearn/utils/tests/test_testing.py @@ -35,7 +35,6 @@ _delete_folder, _convert_container) -from sklearn.utils._testing import SkipTest from sklearn.tree import DecisionTreeClassifier from sklearn.discriminant_analysis import LinearDiscriminantAnalysis @@ -486,11 +485,8 @@ def fit(self, X, y): def test_check_docstring_parameters(): - try: - import numpydoc # noqa - except ImportError: - raise SkipTest( - "numpydoc is required to test the docstrings") + pytest.importorskip('numpydoc', + reason="numpydoc is required to test the docstrings") incorrect = check_docstring_parameters(f_ok) assert incorrect == [] diff --git a/sklearn/utils/tests/test_validation.py b/sklearn/utils/tests/test_validation.py index e48d030900e25..6748dbcad9951 100644 --- a/sklearn/utils/tests/test_validation.py +++ b/sklearn/utils/tests/test_validation.py @@ -297,6 +297,7 @@ def test_check_array(): X_checked = check_array(X, dtype=dtype, accept_sparse=accept_sparse, copy=copy) if (dtype is object or sp.isspmatrix_dok(X)) and len(w): + # XXX unreached code as of v0.22 message = str(w[0].message) messages = ["object dtype is not supported by sparse matrices", "Can't check dok sparse matrix for nan or inf."]