Skip to content

[MRG]: fix tests, centralize warnings and reset __warningregistry__ #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Nov 29, 2013
19 changes: 8 additions & 11 deletions sklearn/cluster/tests/test_hierarchical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
# Authors: Vincent Michel, 2010, Gael Varoquaux 2012
# License: BSD 3 clause
import warnings
from tempfile import mkdtemp

import numpy as np
Expand All @@ -19,6 +18,7 @@
from sklearn.cluster import Ward, WardAgglomeration, ward_tree
from sklearn.cluster.hierarchical import _hc_cut
from sklearn.feature_extraction.image import grid_to_graph
from sklearn.utils.testing import assert_warns


def test_structured_ward_tree():
Expand Down Expand Up @@ -46,14 +46,12 @@ def test_unstructured_ward_tree():
rnd = np.random.RandomState(0)
X = rnd.randn(50, 100)
for this_X in (X, X[0]):
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always", UserWarning)
warnings.simplefilter("ignore", DeprecationWarning)
# With specified a number of clusters just for the sake of
# raising a warning and testing the warning code
children, n_nodes, n_leaves, parent = ward_tree(this_X.T,
n_clusters=10)
assert_equal(len(warning_list), 1)
# With specified a number of clusters just for the sake of
# raising a warning and testing the warning code
children, n_nodes, n_leaves, parent = assert_warns(UserWarning,
ward_tree,
this_X.T,
n_clusters=10)
n_nodes = 2 * X.shape[1] - 1
assert_equal(len(children) + n_leaves, n_nodes)

Expand Down Expand Up @@ -196,8 +194,7 @@ def test_connectivity_fixing_non_lil():
m = np.array([[True, False], [False, True]])
c = grid_to_graph(n_x=2, n_y=2, mask=m)
w = Ward(connectivity=c)
with warnings.catch_warnings(record=True):
w.fit(x)
assert_warns(UserWarning, w.fit, x)


if __name__ == '__main__':
Expand Down
18 changes: 6 additions & 12 deletions sklearn/cluster/tests/test_k_means.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Testing for K-means"""
import sys
import warnings

import numpy as np
from scipy import sparse as sp
Expand All @@ -12,9 +11,10 @@
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_true

from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_less
from sklearn.utils.testing import assert_warns

from sklearn.utils.extmath import row_norms
from sklearn.utils.fixes import unique
from sklearn.metrics.cluster import v_measure_score
Expand Down Expand Up @@ -44,9 +44,8 @@ def test_kmeans_dtype():
X = rnd.normal(size=(40, 2))
X = (X * 10).astype(np.uint8)
km = KMeans(n_init=1).fit(X)
with warnings.catch_warnings(record=True) as w:
assert_array_equal(km.labels_, km.predict(X))
assert_equal(len(w), 1)
pred_x = assert_warns(RuntimeWarning, km.predict, X)
assert_array_equal(km.labels_, pred_x)


def test_labels_assignment_and_inertia():
Expand Down Expand Up @@ -287,10 +286,7 @@ def test_minibatch_init_with_large_k():
mb_k_means = MiniBatchKMeans(init='k-means++', init_size=10, n_clusters=20)
# Check that a warning is raised, as the number clusters is larger
# than the init_size
with warnings.catch_warnings(record=True) as warn_queue:
mb_k_means.fit(X)

assert_equal(len(warn_queue), 1)
assert_warns(RuntimeWarning, mb_k_means.fit, X)


def test_minibatch_k_means_random_init_dense_array():
Expand Down Expand Up @@ -610,9 +606,7 @@ def test_k_means_function():
assert_greater(inertia, 0.0)

# check warning when centers are passed
with warnings.catch_warnings(record=True) as w:
k_means(X, n_clusters=n_clusters, init=centers)
assert_equal(len(w), 1)
assert_warns(RuntimeWarning, k_means, X, n_clusters=n_clusters, init=centers)

# to many clusters desired
assert_raises(ValueError, k_means, X, n_clusters=X.shape[0] + 1)
12 changes: 5 additions & 7 deletions sklearn/covariance/tests/test_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# License: BSD 3 clause

import numpy as np
import warnings

from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_warns

from sklearn import datasets
from sklearn.covariance import empirical_covariance, EmpiricalCovariance, \
Expand Down Expand Up @@ -59,8 +59,8 @@ def test_covariance():
# test with one sample
X_1sample = np.arange(5)
cov = EmpiricalCovariance()
with warnings.catch_warnings(record=True):
cov.fit(X_1sample)

assert_warns(UserWarning, cov.fit, X_1sample)

# test integer type
X_integer = np.asarray([[0, 1], [1, 0]])
Expand Down Expand Up @@ -182,8 +182,7 @@ def test_ledoit_wolf():
# test with one sample
X_1sample = np.arange(5)
lw = LedoitWolf()
with warnings.catch_warnings(record=True):
lw.fit(X_1sample)
assert_warns(UserWarning, lw.fit, X_1sample)

# test shrinkage coeff on a simple data set (without saving precision)
lw = LedoitWolf(store_precision=False)
Expand Down Expand Up @@ -254,8 +253,7 @@ def test_oas():
# test with one sample
X_1sample = np.arange(5)
oa = OAS()
with warnings.catch_warnings(record=True):
oa.fit(X_1sample)
assert_warns(UserWarning, oa.fit, X_1sample)

# test shrinkage coeff on a simple data set (without saving precision)
oa = OAS(store_precision=False)
Expand Down
18 changes: 6 additions & 12 deletions sklearn/decomposition/tests/test_factor_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Licence: BSD3

import warnings
import numpy as np

from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_less
Expand Down Expand Up @@ -69,16 +68,11 @@ def test_factor_analysis():
fa1, fa2 = fas
for attr in ['loglike_', 'components_', 'noise_variance_']:
assert_almost_equal(f(fa1, attr), f(fa2, attr))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', ConvergenceWarning)
fa1.max_iter = 1
fa1.verbose = True
fa1.fit(X)
assert_true(w[-1].category == ConvergenceWarning)

warnings.simplefilter('always', DeprecationWarning)
FactorAnalysis(verbose=1)
assert_true(w[-1].category == DeprecationWarning)

fa1.max_iter = 1
fa1.verbose = True
assert_warns(ConvergenceWarning, fa1.fit, X)
assert_warns(DeprecationWarning, FactorAnalysis, verbose=1)

# Test get_covariance and get_precision with n_components == n_features
# with n_components < n_features and with n_components == 0
Expand Down
13 changes: 4 additions & 9 deletions sklearn/decomposition/tests/test_fastica.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Test the fastica algorithm.
"""
import warnings
import itertools

import numpy as np
Expand All @@ -14,6 +13,7 @@
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_less
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_warns

from sklearn.decomposition import FastICA, fastica, PCA
from sklearn.decomposition.fastica_ import _gs_decorrelation
Expand Down Expand Up @@ -137,16 +137,11 @@ def g_test(x):

def test_fastica_nowhiten():
m = [[0, 1], [1, 0]]
ica = FastICA(whiten=False, random_state=0)
ica.fit(m)
ica.mixing_

# test for issue #697
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
ica = FastICA(n_components=1, whiten=False, random_state=0)
ica.fit(m) # should raise warning
assert_true(len(w) == 1) # 1 warning should be raised
ica = FastICA(n_components=1, whiten=False, random_state=0)
assert_warns(UserWarning, ica.fit, m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line 'ica.mixing_' disappeared. It was, in my opinion, useful to check that there was indeed a mixing_ attribute create.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

assert_true(hasattr(ica, 'mixing_'))


def test_non_square_fastica(add_noise=False):
Expand Down
86 changes: 35 additions & 51 deletions sklearn/decomposition/tests/test_pca.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import warnings

import numpy as np
from scipy.sparse import csr_matrix

from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_less, assert_greater
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_warns

from sklearn import datasets
from sklearn.decomposition import PCA
Expand Down Expand Up @@ -45,7 +44,8 @@ def test_pca():
pca.fit(X)
cov = pca.get_covariance()
precision = pca.get_precision()
assert_array_almost_equal(np.dot(cov, precision), np.eye(X.shape[1]), 12)
assert_array_almost_equal(np.dot(cov, precision),
np.eye(X.shape[1]), 12)


def test_whitening():
Expand Down Expand Up @@ -190,11 +190,8 @@ def test_sparse_randomized_pca_check_projection():
Xt = 0.1 * rng.randn(1, p) + np.array([3, 4, 5])
Xt = csr_matrix(Xt)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', DeprecationWarning)
Yt = RandomizedPCA(n_components=2, random_state=0).fit(X).transform(Xt)
assert_equal(len(w), 1)
assert_equal(w[0].category, DeprecationWarning)
pca = RandomizedPCA(n_components=2, random_state=0)
Yt = assert_warns(DeprecationWarning, pca.fit, X).transform(Xt)

Yt /= np.sqrt((Yt ** 2).sum())

Expand All @@ -213,25 +210,16 @@ def test_sparse_randomized_pca_inverse():

# same check that we can find the original data from the transformed signal
# (since the data is almost of rank n_components)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', DeprecationWarning)
pca = RandomizedPCA(n_components=2, random_state=0).fit(X)
assert_equal(len(w), 1)
assert_equal(w[0].category, DeprecationWarning)

pca = RandomizedPCA(n_components=2, random_state=0)
assert_warns(DeprecationWarning, pca.fit, X)
Y = pca.transform(X)

Y_inverse = pca.inverse_transform(Y)
assert_almost_equal(X.todense(), Y_inverse, decimal=2)

# same as above with whitening (approximate reconstruction)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', DeprecationWarning)
pca = RandomizedPCA(n_components=2, whiten=True,
random_state=0).fit(X)
assert_equal(len(w), 1)
assert_equal(w[0].category, DeprecationWarning)

pca = assert_warns(DeprecationWarning, RandomizedPCA(n_components=2,
whiten=True, random_state=0).fit, X)
Y = pca.transform(X)
Y_inverse = pca.inverse_transform(Y)
relative_max_delta = (np.abs(X.todense() - Y_inverse)
Expand Down Expand Up @@ -374,25 +362,23 @@ def test_probabilistic_pca_1():
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])

with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
h = -0.5 * np.log(2 * np.pi * np.exp(1) * 0.1 ** 2) * p
np.testing.assert_almost_equal(ll1.mean() / h, 1, 0)
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
h = -0.5 * np.log(2 * np.pi * np.exp(1) * 0.1 ** 2) * p
np.testing.assert_almost_equal(ll1.mean() / h, 1, 0)


def test_probabilistic_pca_2():
"""Test that probabilistic PCA correctly separated different datasets"""
n, p = 100, 3
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ll2 = ppca.score(rng.randn(n, p) * .2 + np.array([3, 4, 5]))
assert_greater(ll1.mean(), ll2.mean())
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ll2 = ppca.score(rng.randn(n, p) * .2 + np.array([3, 4, 5]))
assert_greater(ll1.mean(), ll2.mean())


def test_probabilistic_pca_3():
Expand All @@ -402,32 +388,30 @@ def test_probabilistic_pca_3():
n, p = 100, 3
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ppca.fit(X, homoscedastic=False)
ll2 = ppca.score(X)
# XXX : Don't test as homoscedastic=False is buggy
# Comment to be removed with ProbabilisticPCA is removed
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X).score(X)
ppca.fit(X, homoscedastic=False).score(X)
# XXX : Don't test as homoscedastic=False is buggy
# Comment to be removed with ProbabilisticPCA is removed


def test_probabilistic_pca_4():
"""Check that ppca select the right model"""

n, p = 200, 3
rng = np.random.RandomState(0)
Xl = (rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5])
+ np.array([1, 0, 7]))
Xt = (rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5])
+ np.array([1, 0, 7]))
ll = np.zeros(p)
with warnings.catch_warnings(record=True) as w:
for k in range(p):
ppca = ProbabilisticPCA(n_components=k)
ppca.fit(Xl)
ll[k] = ppca.score(Xt).mean()
for k in range(p):
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA,
n_components=k)
ppca.fit(Xl)
ll[k] = ppca.score(Xt).mean()

assert_true(ll.argmax() == 1)
assert_true(ll.argmax() == 1)


def test_probabilistic_pca_vs_pca():
Expand All @@ -437,9 +421,9 @@ def test_probabilistic_pca_vs_pca():
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
pca = PCA(n_components=2).fit(X)
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2).fit(X)
assert_array_almost_equal(pca.score_samples(X), ppca.score(X))
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA,
n_components=2).fit(X)
assert_array_almost_equal(pca.score_samples(X), ppca.score(X))


if __name__ == '__main__':
Expand Down
Loading