From e629d08cd401d9c0970134db3b36b6f7b4d5904f Mon Sep 17 00:00:00 2001
From: Gael Varoquaux <gael.varoquaux@normalesup.org>
Date: Sun, 6 May 2012 16:07:22 +0200
Subject: [PATCH] FIX: handle deprecation with estimator API

An estimator's init cannot include any logic
---
 examples/manifold/plot_swissroll.py |  3 ++-
 sklearn/manifold/isomap.py          | 13 ++++++++++---
 sklearn/manifold/locally_linear.py  | 21 +++++++++++++++------
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/examples/manifold/plot_swissroll.py b/examples/manifold/plot_swissroll.py
index 92d4bf4022..71dfd1c868 100644
--- a/examples/manifold/plot_swissroll.py
+++ b/examples/manifold/plot_swissroll.py
@@ -25,7 +25,8 @@ from sklearn import manifold, datasets
 X, color = datasets.samples_generator.make_swiss_roll(n_samples=1500)
 
 print "Computing LLE embedding"
-X_r, err = manifold.locally_linear_embedding(X, n_neighbors=12, out_dim=2)
+X_r, err = manifold.locally_linear_embedding(X, n_neighbors=12,
+                                             n_components=2)
 print "Done. Reconstruction error: %g" % err
 
 #----------------------------------------------------------------------
diff --git a/sklearn/manifold/isomap.py b/sklearn/manifold/isomap.py
index dff954215f..9cb9e225e9 100644
--- a/sklearn/manifold/isomap.py
+++ b/sklearn/manifold/isomap.py
@@ -80,10 +80,11 @@ class Isomap(BaseEstimator):
             tol=0, max_iter=None, path_method='auto',
             neighbors_algorithm='auto', out_dim=None):
 
-        if not out_dim is None:
+        if out_dim:
             warnings.warn("Parameter ``out_dim`` was renamed to "
-                "``n_components`` and is now deprecated.", DeprecationWarning)
-            n_components = n_components
+                "``n_components`` and is now deprecated.", DeprecationWarning,
+                stacklevel=2)
+        self.out_dim = out_dim
         self.n_neighbors = n_neighbors
         self.n_components = n_components
         self.eigen_solver = eigen_solver
@@ -95,6 +96,12 @@ class Isomap(BaseEstimator):
                                       algorithm=neighbors_algorithm)
 
     def _fit_transform(self, X):
+        if self.out_dim:
+            warnings.warn("Parameter ``out_dim`` was renamed to "
+                "``n_components`` and is now deprecated.", DeprecationWarning,
+                stacklevel=3)
+            self.n_components = self.out_dim
+            self.out_dim = None
         self.nbrs_.fit(X)
         self.training_data_ = self.nbrs_._fit_X
         self.kernel_pca_ = KernelPCA(n_components=self.n_components,
diff --git a/sklearn/manifold/locally_linear.py b/sklearn/manifold/locally_linear.py
index b9529670f4..ebda42af17 100644
--- a/sklearn/manifold/locally_linear.py
+++ b/sklearn/manifold/locally_linear.py
@@ -272,10 +272,11 @@ def locally_linear_embedding(
     if method not in ('standard', 'hessian', 'modified', 'ltsa'):
         raise ValueError("unrecognized method '%s'" % method)
 
-    if not out_dim is None:
+    if out_dim:
         warnings.warn("Parameter ``out_dim`` was renamed to ``n_components`` "
-                "and is now deprecated.", DeprecationWarning)
-        n_components = n_components
+                "and is now deprecated.", DeprecationWarning,
+                stacklevel=2)
+        n_components = out_dim
 
     nbrs = NearestNeighbors(n_neighbors=n_neighbors + 1)
     nbrs.fit(X)
@@ -594,10 +595,11 @@ class LocallyLinearEmbedding(BaseEstimator):
             hessian_tol=1E-4, modified_tol=1E-12, neighbors_algorithm='auto',
             random_state=None, out_dim=None):
 
-        if not out_dim is None:
+        if out_dim:
             warnings.warn("Parameter ``out_dim`` was renamed to "
-                "``n_components`` and is now deprecated.", DeprecationWarning)
-            n_components = n_components
+                "``n_components`` and is now deprecated.", DeprecationWarning,
+                stacklevel=2)
+        self.out_dim = out_dim
 
         self.n_neighbors = n_neighbors
         self.n_components = n_components
@@ -613,6 +615,13 @@ class LocallyLinearEmbedding(BaseEstimator):
                                       algorithm=neighbors_algorithm)
 
     def _fit_transform(self, X):
+        if self.out_dim:
+            warnings.warn("Parameter ``out_dim`` was renamed to "
+                "``n_components`` and is now deprecated.", DeprecationWarning,
+                stacklevel=3)
+            self.n_components = self.out_dim
+            self.out_dim = None
+
         self.random_state = check_random_state(self.random_state)
         self.nbrs_.fit(X)
         self.embedding_, self.reconstruction_error_ = \
-- 
GitLab