From 7ebec056de7fbe0c671857551f20cd3aab37560f Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Sat, 12 Oct 2019 13:54:35 -0400 Subject: [PATCH 1/2] deprecate parallel_helper --- sklearn/ensemble/forest.py | 7 +++---- sklearn/multioutput.py | 3 +-- sklearn/utils/fixes.py | 9 +++++++++ sklearn/utils/tests/test_deprecated_utils.py | 7 +++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/sklearn/ensemble/forest.py b/sklearn/ensemble/forest.py index 38f886f4e9664..f0abf5eb3b387 100644 --- a/sklearn/ensemble/forest.py +++ b/sklearn/ensemble/forest.py @@ -59,7 +59,7 @@ class calls the ``fit`` method of each sub-estimator on random samples from ..utils import check_random_state, check_array, compute_sample_weight from ..exceptions import DataConversionWarning from .base import BaseEnsemble, _partition_estimators -from ..utils.fixes import parallel_helper, _joblib_parallel_args +from ..utils.fixes import _joblib_parallel_args from ..utils.multiclass import check_classification_targets from ..utils.validation import check_is_fitted @@ -218,7 +218,7 @@ def apply(self, X): X = self._validate_X_predict(X) results = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, **_joblib_parallel_args(prefer="threads"))( - delayed(parallel_helper)(tree, 'apply', X, check_input=False) + delayed(tree.apply)(X, check_input=False) for tree in self.estimators_) return np.array(results).T @@ -249,8 +249,7 @@ def decision_path(self, X): X = self._validate_X_predict(X) indicators = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, **_joblib_parallel_args(prefer='threads'))( - delayed(parallel_helper)(tree, 'decision_path', X, - check_input=False) + delayed(tree.decision_path)(X, check_input=False) for tree in self.estimators_) n_nodes = [0] diff --git a/sklearn/multioutput.py b/sklearn/multioutput.py index c1c3872a4c22a..fc76ca831d9aa 100644 --- a/sklearn/multioutput.py +++ b/sklearn/multioutput.py @@ -23,7 +23,6 @@ from .base import RegressorMixin, ClassifierMixin, is_classifier from .model_selection import cross_val_predict from .utils import check_array, check_X_y, check_random_state -from .utils.fixes import parallel_helper from .utils.metaestimators import if_delegate_has_method from .utils.validation import check_is_fitted, has_fit_parameter from .utils.multiclass import check_classification_targets @@ -193,7 +192,7 @@ def predict(self, X): X = check_array(X, accept_sparse=True) y = Parallel(n_jobs=self.n_jobs)( - delayed(parallel_helper)(e, 'predict', X) + delayed(e.predict)(X) for e in self.estimators_) return np.asarray(y).T diff --git a/sklearn/utils/fixes.py b/sklearn/utils/fixes.py index f8348a7534f3c..1bf5a3b0c44b9 100644 --- a/sklearn/utils/fixes.py +++ b/sklearn/utils/fixes.py @@ -18,6 +18,8 @@ import scipy.stats from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa +from . import deprecated + def _parse_version(version_string): version = [] @@ -155,7 +157,14 @@ def _argmax(arr_or_matrix, axis=None): return arr_or_matrix.argmax(axis=axis) +# TODO: remove in 0.24 +@deprecated("parallel_helper is deprecated in version " + "0.22 and will be removed in version 0.24.") def parallel_helper(obj, methodname, *args, **kwargs): + return _parallel_helper(obj, methodname, *args, **kwargs) + + +def _parallel_helper(obj, methodname, *args, **kwargs): """Workaround for Python 2 limitations of pickling instance methods Parameters diff --git a/sklearn/utils/tests/test_deprecated_utils.py b/sklearn/utils/tests/test_deprecated_utils.py index 472182a6b348a..4cf54c37df1c0 100644 --- a/sklearn/utils/tests/test_deprecated_utils.py +++ b/sklearn/utils/tests/test_deprecated_utils.py @@ -11,6 +11,7 @@ from sklearn.utils.optimize import newton_cg from sklearn.utils.random import random_choice_csc from sklearn.utils import safe_indexing +from sklearn.utils.fixes import parallel_helper # This file tests the utils that are deprecated @@ -85,3 +86,9 @@ def test_random_choice_csc(): def test_safe_indexing(): with pytest.warns(DeprecationWarning, match="removed in version 0.24"): safe_indexing([1, 2], 0) + + +# TODO: remove in 0.24 +def test_parallel_helper(): + with pytest.warns(DeprecationWarning, match="removed in version 0.24"): + parallel_helper(list(), 'append', 2) From 49729430378344150e6972951ae35393c69c7bf1 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Sat, 12 Oct 2019 13:57:08 -0400 Subject: [PATCH 2/2] comment --- sklearn/utils/fixes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sklearn/utils/fixes.py b/sklearn/utils/fixes.py index 1bf5a3b0c44b9..8768caace7571 100644 --- a/sklearn/utils/fixes.py +++ b/sklearn/utils/fixes.py @@ -164,6 +164,7 @@ def parallel_helper(obj, methodname, *args, **kwargs): return _parallel_helper(obj, methodname, *args, **kwargs) +# TODO: remove in 0.24. It isn't used anywhere def _parallel_helper(obj, methodname, *args, **kwargs): """Workaround for Python 2 limitations of pickling instance methods