compat.py 8.2 KB
Newer Older
wxchan's avatar
wxchan committed
1
# coding: utf-8
2
"""Compatibility library."""
3

4
from typing import Any, List
5

wxchan's avatar
wxchan committed
6
7
"""pandas"""
try:
8
    from pandas import DataFrame as pd_DataFrame
9
10
    from pandas import Series as pd_Series
    from pandas import concat
11

12
13
14
15
    try:
        from pandas import CategoricalDtype as pd_CategoricalDtype
    except ImportError:
        from pandas.api.types import CategoricalDtype as pd_CategoricalDtype
16
    PANDAS_INSTALLED = True
wxchan's avatar
wxchan committed
17
except ImportError:
18
19
    PANDAS_INSTALLED = False

20
    class pd_Series:  # type: ignore
21
22
        """Dummy class for pandas.Series."""

23
        def __init__(self, *args: Any, **kwargs: Any):
24
            pass
wxchan's avatar
wxchan committed
25

26
    class pd_DataFrame:  # type: ignore
27
28
        """Dummy class for pandas.DataFrame."""

29
        def __init__(self, *args: Any, **kwargs: Any):
30
            pass
wxchan's avatar
wxchan committed
31

32
    class pd_CategoricalDtype:  # type: ignore
33
34
        """Dummy class for pandas.CategoricalDtype."""

35
        def __init__(self, *args: Any, **kwargs: Any):
36
37
            pass

38
    concat = None
39

40
41
"""matplotlib"""
try:
42
    import matplotlib  # noqa: F401
43

44
45
46
47
48
49
    MATPLOTLIB_INSTALLED = True
except ImportError:
    MATPLOTLIB_INSTALLED = False

"""graphviz"""
try:
50
    import graphviz  # noqa: F401
51

52
53
54
55
    GRAPHVIZ_INSTALLED = True
except ImportError:
    GRAPHVIZ_INSTALLED = False

56
57
"""datatable"""
try:
58
    import datatable
59

60
    if hasattr(datatable, "Frame"):
61
        dt_DataTable = datatable.Frame
62
    else:
63
        dt_DataTable = datatable.DataTable
64
65
66
67
    DATATABLE_INSTALLED = True
except ImportError:
    DATATABLE_INSTALLED = False

68
    class dt_DataTable:  # type: ignore
69
        """Dummy class for datatable.DataTable."""
70

71
        def __init__(self, *args: Any, **kwargs: Any):
72
            pass
73
74


wxchan's avatar
wxchan committed
75
76
"""sklearn"""
try:
77
    from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
wxchan's avatar
wxchan committed
78
    from sklearn.preprocessing import LabelEncoder
79
    from sklearn.utils.class_weight import compute_sample_weight
80
    from sklearn.utils.multiclass import check_classification_targets
81
    from sklearn.utils.validation import assert_all_finite, check_array, check_X_y
82

wxchan's avatar
wxchan committed
83
    try:
84
        from sklearn.exceptions import NotFittedError
85
        from sklearn.model_selection import BaseCrossValidator, GroupKFold, StratifiedKFold
wxchan's avatar
wxchan committed
86
    except ImportError:
87
        from sklearn.cross_validation import BaseCrossValidator, GroupKFold, StratifiedKFold
88
        from sklearn.utils.validation import NotFittedError
89
90
91
92
93
94
    try:
        from sklearn.utils.validation import _check_sample_weight
    except ImportError:
        from sklearn.utils.validation import check_consistent_length

        # dummy function to support older version of scikit-learn
95
        def _check_sample_weight(sample_weight: Any, X: Any, dtype: Any = None) -> Any:
96
97
98
            check_consistent_length(sample_weight, X)
            return sample_weight

wxchan's avatar
wxchan committed
99
    SKLEARN_INSTALLED = True
100
    _LGBMBaseCrossValidator = BaseCrossValidator
101
102
103
104
105
106
107
108
109
    _LGBMModelBase = BaseEstimator
    _LGBMRegressorBase = RegressorMixin
    _LGBMClassifierBase = ClassifierMixin
    _LGBMLabelEncoder = LabelEncoder
    LGBMNotFittedError = NotFittedError
    _LGBMStratifiedKFold = StratifiedKFold
    _LGBMGroupKFold = GroupKFold
    _LGBMCheckXY = check_X_y
    _LGBMCheckArray = check_array
110
    _LGBMCheckSampleWeight = _check_sample_weight
111
    _LGBMAssertAllFinite = assert_all_finite
112
    _LGBMCheckClassificationTargets = check_classification_targets
113
    _LGBMComputeSampleWeight = compute_sample_weight
wxchan's avatar
wxchan committed
114
115
except ImportError:
    SKLEARN_INSTALLED = False
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

    class _LGBMModelBase:  # type: ignore
        """Dummy class for sklearn.base.BaseEstimator."""

        pass

    class _LGBMClassifierBase:  # type: ignore
        """Dummy class for sklearn.base.ClassifierMixin."""

        pass

    class _LGBMRegressorBase:  # type: ignore
        """Dummy class for sklearn.base.RegressorMixin."""

        pass

132
    _LGBMBaseCrossValidator = None
133
134
135
136
137
138
    _LGBMLabelEncoder = None
    LGBMNotFittedError = ValueError
    _LGBMStratifiedKFold = None
    _LGBMGroupKFold = None
    _LGBMCheckXY = None
    _LGBMCheckArray = None
139
    _LGBMCheckSampleWeight = None
140
    _LGBMAssertAllFinite = None
141
    _LGBMCheckClassificationTargets = None
142
    _LGBMComputeSampleWeight = None
143
144
145

"""dask"""
try:
146
147
    from dask import delayed
    from dask.array import Array as dask_Array
148
149
    from dask.array import from_delayed as dask_array_from_delayed
    from dask.bag import from_delayed as dask_bag_from_delayed
150
151
    from dask.dataframe import DataFrame as dask_DataFrame
    from dask.dataframe import Series as dask_Series
152
    from dask.distributed import Client, Future, default_client, wait
153

154
    DASK_INSTALLED = True
155
156
157
158
159
160
161
162
163
164
165
# catching 'ValueError' here because of this:
# https://github.com/microsoft/LightGBM/issues/6365#issuecomment-2002330003
#
# That's potentially risky as dask does some significant import-time processing,
# like loading configuration from environment variables and files, and catching
# ValueError here might hide issues with that config-loading.
#
# But in exchange, it's less likely that 'import lightgbm' will fail for
# dask-related reasons, which is beneficial for any workloads that are using
# lightgbm but not its Dask functionality.
except (ImportError, ValueError):
166
    DASK_INSTALLED = False
167

168
169
    dask_array_from_delayed = None  # type: ignore[assignment]
    dask_bag_from_delayed = None  # type: ignore[assignment]
170
    delayed = None
171
172
    default_client = None  # type: ignore[assignment]
    wait = None  # type: ignore[assignment]
173

174
175
176
    class Client:  # type: ignore
        """Dummy class for dask.distributed.Client."""

177
        def __init__(self, *args: Any, **kwargs: Any):
178
            pass
179

180
181
182
    class Future:  # type: ignore
        """Dummy class for dask.distributed.Future."""

183
        def __init__(self, *args: Any, **kwargs: Any):
184
185
            pass

186
    class dask_Array:  # type: ignore
187
188
        """Dummy class for dask.array.Array."""

189
        def __init__(self, *args: Any, **kwargs: Any):
190
            pass
191

192
    class dask_DataFrame:  # type: ignore
193
194
        """Dummy class for dask.dataframe.DataFrame."""

195
        def __init__(self, *args: Any, **kwargs: Any):
196
            pass
197

198
    class dask_Series:  # type: ignore
199
        """Dummy class for dask.dataframe.Series."""
200

201
        def __init__(self, *args: Any, **kwargs: Any):
202
            pass
203

204

205
206
"""pyarrow"""
try:
207
    import pyarrow.compute as pa_compute
208
209
    from pyarrow import Array as pa_Array
    from pyarrow import ChunkedArray as pa_ChunkedArray
210
    from pyarrow import Table as pa_Table
211
    from pyarrow import chunked_array as pa_chunked_array
212
    from pyarrow.cffi import ffi as arrow_cffi
213
    from pyarrow.types import is_boolean as arrow_is_boolean
214
215
    from pyarrow.types import is_floating as arrow_is_floating
    from pyarrow.types import is_integer as arrow_is_integer
216

217
218
219
220
    PYARROW_INSTALLED = True
except ImportError:
    PYARROW_INSTALLED = False

221
222
223
    class pa_Array:  # type: ignore
        """Dummy class for pa.Array."""

224
        def __init__(self, *args: Any, **kwargs: Any):
225
226
227
228
229
            pass

    class pa_ChunkedArray:  # type: ignore
        """Dummy class for pa.ChunkedArray."""

230
        def __init__(self, *args: Any, **kwargs: Any):
231
232
            pass

233
234
235
    class pa_Table:  # type: ignore
        """Dummy class for pa.Table."""

236
        def __init__(self, *args: Any, **kwargs: Any):
237
238
239
240
241
242
243
244
245
246
            pass

    class arrow_cffi:  # type: ignore
        """Dummy class for pyarrow.cffi.ffi."""

        CData = None
        addressof = None
        cast = None
        new = None

247
        def __init__(self, *args: Any, **kwargs: Any):
248
249
            pass

250
251
252
253
254
255
    class pa_compute:  # type: ignore
        """Dummy class for pyarrow.compute."""

        all = None
        equal = None

256
    pa_chunked_array = None
257
    arrow_is_boolean = None
258
259
260
    arrow_is_integer = None
    arrow_is_floating = None

261
262
263
264
"""cpu_count()"""
try:
    from joblib import cpu_count

265
    def _LGBMCpuCount(only_physical_cores: bool = True) -> int:
266
267
268
269
270
        return cpu_count(only_physical_cores=only_physical_cores)
except ImportError:
    try:
        from psutil import cpu_count

271
272
        def _LGBMCpuCount(only_physical_cores: bool = True) -> int:
            return cpu_count(logical=not only_physical_cores) or 1
273
274
275
    except ImportError:
        from multiprocessing import cpu_count

276
        def _LGBMCpuCount(only_physical_cores: bool = True) -> int:
277
            return cpu_count()
278

279

280
__all__: List[str] = []