2011-09-20 21:22:51 +08:00
|
|
|
"""Compatibility fixes for older version of python, numpy and scipy
|
|
|
|
|
|
|
|
|
|
If you add content to this file, please give the version of the package
|
2021-09-18 01:04:54 +08:00
|
|
|
at which the fix is no longer needed.
|
2011-09-20 21:22:51 +08:00
|
|
|
"""
|
2010-08-26 04:00:03 +08:00
|
|
|
# Authors: Emmanuelle Gouillart <emmanuelle.gouillart@normalesup.org>
|
|
|
|
|
# Gael Varoquaux <gael.varoquaux@normalesup.org>
|
2010-12-17 03:34:59 +08:00
|
|
|
# Fabian Pedregosa <fpedregosa@acm.org>
|
2014-02-22 21:34:17 +08:00
|
|
|
# Lars Buitinck
|
2013-07-28 18:39:58 +08:00
|
|
|
#
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2010-08-26 04:00:03 +08:00
|
|
|
|
2020-09-21 22:59:42 +08:00
|
|
|
from functools import update_wrapper
|
|
|
|
|
import functools
|
2018-11-07 05:10:23 +08:00
|
|
|
|
2021-10-08 21:14:00 +08:00
|
|
|
import sklearn
|
2012-09-28 15:32:03 +08:00
|
|
|
import numpy as np
|
2014-11-14 19:51:43 +08:00
|
|
|
import scipy
|
2019-10-02 19:56:24 +08:00
|
|
|
import scipy.stats
|
2021-10-08 21:14:00 +08:00
|
|
|
import threadpoolctl
|
2020-09-21 22:59:42 +08:00
|
|
|
from .._config import config_context, get_config
|
2021-04-19 23:06:38 +08:00
|
|
|
from ..externals._packaging.version import parse as parse_version
|
2014-11-14 19:51:43 +08:00
|
|
|
|
|
|
|
|
|
2020-06-24 19:35:49 +08:00
|
|
|
np_version = parse_version(np.__version__)
|
|
|
|
|
sp_version = parse_version(scipy.__version__)
|
2017-06-10 21:01:02 +08:00
|
|
|
|
|
|
|
|
|
2020-06-24 19:35:49 +08:00
|
|
|
if sp_version >= parse_version("1.4"):
|
2019-06-26 23:23:17 +08:00
|
|
|
from scipy.sparse.linalg import lobpcg
|
|
|
|
|
else:
|
2019-09-16 19:39:53 +08:00
|
|
|
# Backport of lobpcg functionality from scipy 1.4.0, can be removed
|
2020-06-24 19:35:49 +08:00
|
|
|
# once support for sp_version < parse_version('1.4') is dropped
|
2020-03-30 21:41:05 +08:00
|
|
|
# mypy error: Name 'lobpcg' already defined (possibly by an import)
|
|
|
|
|
from ..externals._lobpcg import lobpcg # type: ignore # noqa
|
2015-01-12 19:03:03 +08:00
|
|
|
|
2021-11-10 04:00:27 +08:00
|
|
|
try:
|
|
|
|
|
from scipy.optimize._linesearch import line_search_wolfe2, line_search_wolfe1
|
|
|
|
|
except ImportError: # SciPy < 1.8
|
|
|
|
|
from scipy.optimize.linesearch import line_search_wolfe2, line_search_wolfe1 # type: ignore # noqa
|
|
|
|
|
|
2018-04-22 01:06:10 +08:00
|
|
|
|
2020-02-10 19:28:42 +08:00
|
|
|
def _object_dtype_isnan(X):
|
|
|
|
|
return X != X
|
2018-07-15 04:14:02 +08:00
|
|
|
|
|
|
|
|
|
2019-10-02 19:56:24 +08:00
|
|
|
class loguniform(scipy.stats.reciprocal):
|
|
|
|
|
"""A class supporting log-uniform random variables.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
low : float
|
|
|
|
|
The minimum value
|
|
|
|
|
high : float
|
|
|
|
|
The maximum value
|
|
|
|
|
|
|
|
|
|
Methods
|
|
|
|
|
-------
|
|
|
|
|
rvs(self, size=None, random_state=None)
|
|
|
|
|
Generate log-uniform random variables
|
|
|
|
|
|
|
|
|
|
The most useful method for Scikit-learn usage is highlighted here.
|
|
|
|
|
For a full list, see
|
|
|
|
|
`scipy.stats.reciprocal
|
|
|
|
|
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.reciprocal.html>`_.
|
|
|
|
|
This list includes all functions of ``scipy.stats`` continuous
|
|
|
|
|
distributions such as ``pdf``.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
This class generates values between ``low`` and ``high`` or
|
|
|
|
|
|
|
|
|
|
low <= loguniform(low, high).rvs() <= high
|
|
|
|
|
|
|
|
|
|
The logarithmic probability density function (PDF) is uniform. When
|
|
|
|
|
``x`` is a uniformly distributed random variable between 0 and 1, ``10**x``
|
2020-12-22 05:58:27 +08:00
|
|
|
are random variables that are equally likely to be returned.
|
2019-10-02 19:56:24 +08:00
|
|
|
|
|
|
|
|
This class is an alias to ``scipy.stats.reciprocal``, which uses the
|
|
|
|
|
reciprocal distribution:
|
|
|
|
|
https://en.wikipedia.org/wiki/Reciprocal_distribution
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
>>> from sklearn.utils.fixes import loguniform
|
|
|
|
|
>>> rv = loguniform(1e-3, 1e1)
|
|
|
|
|
>>> rvs = rv.rvs(random_state=42, size=1000)
|
|
|
|
|
>>> rvs.min() # doctest: +SKIP
|
|
|
|
|
0.0010435856341129003
|
|
|
|
|
>>> rvs.max() # doctest: +SKIP
|
|
|
|
|
9.97403052786026
|
|
|
|
|
"""
|
2020-05-13 14:52:25 +08:00
|
|
|
|
|
|
|
|
|
2020-09-21 22:59:42 +08:00
|
|
|
# remove when https://github.com/joblib/joblib/issues/1071 is fixed
|
|
|
|
|
def delayed(function):
|
|
|
|
|
"""Decorator used to capture the arguments of a function."""
|
2021-06-18 02:21:09 +08:00
|
|
|
|
2020-09-21 22:59:42 +08:00
|
|
|
@functools.wraps(function)
|
|
|
|
|
def delayed_function(*args, **kwargs):
|
|
|
|
|
return _FuncWrapper(function), args, kwargs
|
2021-06-18 02:21:09 +08:00
|
|
|
|
2020-09-21 22:59:42 +08:00
|
|
|
return delayed_function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _FuncWrapper:
|
|
|
|
|
""" "Load the global configuration before calling the function."""
|
2021-06-18 02:21:09 +08:00
|
|
|
|
2020-09-21 22:59:42 +08:00
|
|
|
def __init__(self, function):
|
|
|
|
|
self.function = function
|
|
|
|
|
self.config = get_config()
|
|
|
|
|
update_wrapper(self, self.function)
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
with config_context(**self.config):
|
|
|
|
|
return self.function(*args, **kwargs)
|
2021-01-24 17:53:27 +08:00
|
|
|
|
|
|
|
|
|
2021-11-30 18:46:32 +08:00
|
|
|
# Rename the `method` kwarg to `interpolation` for NumPy < 1.22, because
|
|
|
|
|
# `interpolation` kwarg was deprecated in favor of `method` in NumPy >= 1.22.
|
|
|
|
|
def _percentile(a, q, *, method="linear", **kwargs):
|
|
|
|
|
return np.percentile(a, q, interpolation=method, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if np_version < parse_version("1.22"):
|
|
|
|
|
percentile = _percentile
|
|
|
|
|
else: # >= 1.22
|
|
|
|
|
from numpy import percentile # type: ignore # noqa
|
|
|
|
|
|
|
|
|
|
|
2021-10-08 21:14:00 +08:00
|
|
|
# compatibility fix for threadpoolctl >= 3.0.0
|
|
|
|
|
# since version 3 it's possible to setup a global threadpool controller to avoid
|
|
|
|
|
# looping through all loaded shared libraries each time.
|
|
|
|
|
# the global controller is created during the first call to threadpoolctl.
|
|
|
|
|
def _get_threadpool_controller():
|
|
|
|
|
if not hasattr(threadpoolctl, "ThreadpoolController"):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if not hasattr(sklearn, "_sklearn_threadpool_controller"):
|
|
|
|
|
sklearn._sklearn_threadpool_controller = threadpoolctl.ThreadpoolController()
|
|
|
|
|
|
|
|
|
|
return sklearn._sklearn_threadpool_controller
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def threadpool_limits(limits=None, user_api=None):
|
|
|
|
|
controller = _get_threadpool_controller()
|
|
|
|
|
if controller is not None:
|
|
|
|
|
return controller.limit(limits=limits, user_api=user_api)
|
|
|
|
|
else:
|
|
|
|
|
return threadpoolctl.threadpool_limits(limits=limits, user_api=user_api)
|
|
|
|
|
|
|
|
|
|
|
2021-10-15 18:51:12 +08:00
|
|
|
threadpool_limits.__doc__ = threadpoolctl.threadpool_limits.__doc__
|
|
|
|
|
|
|
|
|
|
|
2021-10-08 21:14:00 +08:00
|
|
|
def threadpool_info():
|
|
|
|
|
controller = _get_threadpool_controller()
|
|
|
|
|
if controller is not None:
|
|
|
|
|
return controller.info()
|
|
|
|
|
else:
|
|
|
|
|
return threadpoolctl.threadpool_info()
|
2021-10-15 18:51:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
threadpool_info.__doc__ = threadpoolctl.threadpool_info.__doc__
|