Skip to content
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

Final Tweaks #946

Merged
merged 12 commits into from
Aug 15, 2019
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_biplot_2d.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_biplot_3d.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_colorbar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_continuous.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_discrete.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_heatmap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_features/test_pca/test_scale_true_3d.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions tests/test_features/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Provides fixtures for the feature tests module.
"""

##########################################################################
## Imports
##########################################################################
import pytest
from sklearn.datasets import make_classification, make_regression, make_s_curve

from tests.fixtures import Dataset

##########################################################################
## Fixtures
##########################################################################

@pytest.fixture(scope="class")
def discrete(request):
"""
Creates a fixture of train and test splits for the sklearn digits dataset
For ease of use returns a Dataset named tuple composed of two Split tuples.
"""
X, y = make_classification(
n_samples=400,
n_features=12,
n_informative=8,
n_redundant=0,
n_classes=5,
n_clusters_per_class=1,
class_sep=1.8,
random_state=854,
scale=[14.2, 2.1, 0.32, 0.001, 32.3, 44.1, 102.3, 2.3, 2.4, 38.2, 0.05, 1.0],
)

# Set a class attribute for discrete data.
request.cls.discrete = Dataset(X, y)


@pytest.fixture(scope="class")
def continuous(request):
"""
Creates a random regressor fixture.
"""
X, y = make_regression(
n_samples=500, n_features=22, n_informative=8, random_state=2019
)

# Set a class attribute for continuous data
request.cls.continuous = Dataset(X, y)

@pytest.fixture(scope="class")
def s_curves(request):
"""
Creates a random regressor fixture.
"""
X, y = make_s_curve(1000, random_state=888)
# Set a class attribute for continuous data
request.cls.s_curves = Dataset(X, y)

77 changes: 27 additions & 50 deletions tests/test_features/test_manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.datasets.samples_generator import make_s_curve
from sklearn.datasets import make_classification, make_regression, make_blobs
from sklearn.datasets import make_s_curve
from sklearn.datasets import make_blobs

from unittest.mock import patch
from tests.base import VisualTestCase
Expand All @@ -40,7 +40,7 @@
## Manifold Visualizer Tests
##########################################################################


@pytest.mark.usefixtures("s_curves", "discrete", "continuous")
class TestManifold(VisualTestCase):
"""
Test Manifold visualizer
Expand Down Expand Up @@ -112,41 +112,37 @@ def test_manifold_instance_construction(self):

@pytest.mark.filterwarnings("ignore:Conversion of the second argument")
@pytest.mark.parametrize(
"algorithm",
["lle", "ltsa", "hessian", "modified", "isomap"],
"algorithm", ["lle", "ltsa", "hessian", "modified", "isomap"]
)
def test_manifold_algorithm_transform_fit(self, algorithm):
"""
Test manifold fit with algorithms having transform implemented
"""
X, y = make_s_curve(1000, random_state=94)
manifold = Manifold(manifold=algorithm, target="auto")
with pytest.warns(YellowbrickWarning):
manifold = Manifold(manifold=algorithm, target="auto")
Copy link
Member

Choose a reason for hiding this comment

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

removed n_neighbors warning


assert manifold.fit(X, y) is manifold, "fit did not return self"

@pytest.mark.filterwarnings("ignore:Conversion of the second argument")
@pytest.mark.parametrize(
"algorithm",
["mds", "spectral", "tsne"],
)
@pytest.mark.parametrize("algorithm", ["mds", "spectral", "tsne"])
def test_manifold_algorithm_no_transform_fit(self, algorithm):
"""
Test manifold fit with algorithms not having transform implemented
"""
X, y = make_s_curve(200, random_state=888)
X, y = self.s_curves
msg = "requires data to be simultaneously fit and transformed"
oz = Manifold(manifold=algorithm, n_neighbors=10, random_state=223)
with pytest.raises(ModelError, match=msg):
oz.fit(X)


@patch("yellowbrick.features.manifold.Manifold.draw", spec=True)
@pytest.mark.parametrize("projection", [2, 3])
def test_manifold_fit_transform(self, mock_draw, projection):
"""
Test manifold fit_transform method
"""
X, y = make_s_curve(1000, random_state=888)
X, y = self.s_curves
manifold = Manifold(target="auto", projection=projection)

assert not hasattr(manifold, "fit_time_")
Expand All @@ -157,29 +153,31 @@ def test_manifold_fit_transform(self, mock_draw, projection):
mock_draw.assert_called_once()
assert hasattr(manifold, "fit_time_")
assert manifold._target_color_type == TargetType.CONTINUOUS

@patch("yellowbrick.features.manifold.Manifold.fit_transform", spec=True)
@patch("yellowbrick.features.manifold.Manifold.draw", spec=True)
@pytest.mark.parametrize("projection", [2, 3])
def test_manifold_transform(self, mock_draw, mock_fit_transform, projection):
"""
Test manifold transform method
"""
X, y = make_s_curve(1000, random_state=888)
manifold = Manifold(manifold="lle", target="auto", projection=projection)
X, y = self.s_curves
manifold = Manifold(
manifold="lle", target="auto", n_neighbors=5, projection=projection
)
Copy link
Member

Choose a reason for hiding this comment

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

removed n_neighbors warning


manifold.fit(X, y)
Xp = manifold.transform(X, y)
assert Xp.shape == (X.shape[0], projection)

mock_draw.assert_called_once()

def test_manifold_no_transform(self):
"""
Test the exception when manifold doesn't implement transform.
"""
X, _ = make_s_curve(1000, random_state=888)
manifold = Manifold(manifold="lle", target="auto")
X, _ = self.s_curves
manifold = Manifold(manifold="lle", n_neighbors=5, target="auto")
Copy link
Member

Choose a reason for hiding this comment

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

removed n_neighbors warning


msg = "instance is not fitted yet, please call fit"
with pytest.raises(NotFitted, match=msg):
Expand All @@ -191,27 +189,19 @@ def test_manifold_assert_no_transform(self, mock_fit, manifolds):
"""
Assert that transform raises error when MDS, TSNE or Spectral Embedding algorithms are used.
"""
X, _ = make_s_curve(1000, random_state=888)
X, _ = self.s_curves
manifold = Manifold(manifold=manifolds, target="auto", n_neighbors=10)
mock_fit(X)
msg = "requires data to be simultaneously fit and transformed"
with pytest.raises(ModelError, match=msg):
manifold.transform(X)
manifold.transform(X)

@pytest.mark.filterwarnings("ignore:Conversion of the second argument")
def test_manifold_classification(self):
"""
Image similarity test for classification dataset (discrete y)
"""
X, y = make_classification(
n_samples=300,
n_features=7,
n_informative=4,
n_redundant=2,
n_classes=4,
n_clusters_per_class=2,
random_state=78,
)
X, y = self.discrete

oz = Manifold(
manifold="spectral", target="discrete", n_neighbors=5, random_state=108
Expand All @@ -228,15 +218,7 @@ def test_manifold_classification_3d(self):
"""
Image similarity test for classification dataset (discrete y)
"""
X, y = make_classification(
n_samples=300,
n_features=7,
n_informative=4,
n_redundant=2,
n_classes=4,
n_clusters_per_class=2,
random_state=78,
)
X, y = self.discrete

oz = Manifold(
manifold="spectral",
Expand All @@ -258,9 +240,7 @@ def test_manifold_regression(self):
"""
Image similarity test for regression dataset (continuous y)
"""
X, y = make_regression(
n_samples=300, n_features=7, n_informative=4, random_state=87
)
X, y = self.continuous

oz = Manifold(manifold="tsne", target="continuous", random_state=1)
assert not hasattr(oz, "range_")
Expand All @@ -275,9 +255,7 @@ def test_manifold_regression_3d(self):
"""
Image similarity test for regression dataset (continuous y)
"""
X, y = make_regression(
n_samples=300, n_features=7, n_informative=4, random_state=87
)
X, y = self.continuous

oz = Manifold(
manifold="tsne", target="continuous", random_state=1, projection=3
Expand Down Expand Up @@ -318,18 +296,17 @@ def test_manifold_pandas(self):
"""
Test manifold on a dataset made up of a pandas DataFrame and Series
"""
X, y = make_s_curve(200, random_state=888)
X, y = self.s_curves

oz = Manifold(
manifold="ltsa",
colormap="nipy_spectral",
n_neighbors=10,
target="continuous",
random_state=223,
)
oz.fit_transform(X, y)
oz.fit_transform(X, y)
oz.finalize()
oz.cbar.set_ticks([])
# TODO: find a way to decrease this tolerance
self.assert_images_similar(oz, tol=40)