We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
The doc of `optuna.integration.lightgbm.train` function cannot be generated if LightGBM isn't installed.
Due to the bug the title describes, [the current API reference doc](https://optuna.readthedocs.io/en/latest/) doesn't contain the entry of `optuna.integration.lightgbm.train` function.

# Expected behavior

The build of the function doc should be succeeded even if LightGBM isn't installed in the build environment.

## Error messages, stack traces, or logs

```console
$ cd optuna/docs
$ make html
...
WARNING: autodoc: failed to import function 'train' from module 'optuna.integration.lightgbm'; the following exception was raised:
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/util/inspect.py", line 225, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: module 'optuna.integration.lightgbm' has no attribute 'train'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/importer.py", line 193, in import_object
    obj = attrgetter(obj, attrname)
  File "/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/__init__.py", line 290, in get_attr
    return autodoc_attrgetter(self.env.app, obj, name, *defargs)
  File "/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/__init__.py", line 1563, in autodoc_attrgetter
    return safe_getattr(obj, name, *defargs)
  File "/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/util/inspect.py", line 241, in safe_getattr
    raise AttributeError(name)
AttributeError: train
...
```

## Steps to reproduce

1. Ensure that LightGBM isn't installed in the built environment.
2. `$ git clone git://github.com/optuna/optuna.git`
3. `$ cd optuna/docs`
4. `$ make html`




--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `optuna/integration/lightgbm.py`
Content:
```
1 import sys
2 
3 import optuna
4 
5 try:
6     import lightgbm as lgb  # NOQA
7     _available = True
8 except ImportError as e:
9     _import_error = e
10     # LightGBMPruningCallback is disabled because LightGBM is not available.
11     _available = False
12 
13 
14 # Attach lightgbm API.
15 if _available:
16     # API from optuna integration.
17     from optuna.integration import lightgbm_tuner as tuner
18 
19     # Workaround for mypy.
20     from lightgbm import Dataset  # NOQA
21     from optuna.integration.lightgbm_tuner import LightGBMTuner  # NOQA
22 
23     _names_from_tuners = ['train', 'LGBMModel', 'LGBMClassifier', 'LGBMRegressor']
24 
25     # API from lightgbm.
26     for api_name in lgb.__dict__['__all__']:
27         if api_name in _names_from_tuners:
28             continue
29         setattr(sys.modules[__name__], api_name, lgb.__dict__[api_name])
30 
31     for api_name in _names_from_tuners:
32         setattr(sys.modules[__name__], api_name, tuner.__dict__[api_name])
33 else:
34     LightGBMTuner = object  # type: ignore
35 
36 
37 class LightGBMPruningCallback(object):
38     """Callback for LightGBM to prune unpromising trials.
39 
40     Example:
41 
42         Add a pruning callback which observes validation scores to training of a LightGBM model.
43 
44         .. code::
45 
46                 param = {'objective': 'binary', 'metric': 'binary_error'}
47                 pruning_callback = LightGBMPruningCallback(trial, 'binary_error')
48                 gbm = lgb.train(param, dtrain, valid_sets=[dtest], callbacks=[pruning_callback])
49 
50     Args:
51         trial:
52             A :class:`~optuna.trial.Trial` corresponding to the current evaluation of
53             the objective function.
54         metric:
55             An evaluation metric for pruning, e.g., ``binary_error`` and ``multi_error``.
56             Please refer to
57             `LightGBM reference
58             <https://lightgbm.readthedocs.io/en/latest/Parameters.html#metric>`_
59             for further details.
60         valid_name:
61             The name of the target validation.
62             Validation names are specified by ``valid_names`` option of
63             `train method
64             <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.train>`_.
65             If omitted, ``valid_0`` is used which is the default name of the first validation.
66             Note that this argument will be ignored if you are calling
67             `cv method <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.cv>`_
68             instead of train method.
69     """
70 
71     def __init__(self, trial, metric, valid_name='valid_0'):
72         # type: (optuna.trial.Trial, str, str) -> None
73 
74         _check_lightgbm_availability()
75 
76         self._trial = trial
77         self._valid_name = valid_name
78         self._metric = metric
79 
80     def __call__(self, env):
81         # type: (lgb.callback.CallbackEnv) -> None
82 
83         # If this callback has been passed to `lightgbm.cv` function,
84         # the value of `is_cv` becomes `True`. See also:
85         # https://github.com/Microsoft/LightGBM/blob/v2.2.2/python-package/lightgbm/engine.py#L329
86         # Note that `5` is not the number of folds but the length of sequence.
87         is_cv = len(env.evaluation_result_list) > 0 and len(env.evaluation_result_list[0]) == 5
88         if is_cv:
89             target_valid_name = 'cv_agg'
90         else:
91             target_valid_name = self._valid_name
92 
93         for evaluation_result in env.evaluation_result_list:
94             valid_name, metric, current_score, is_higher_better = evaluation_result[:4]
95             if valid_name != target_valid_name or metric != self._metric:
96                 continue
97 
98             if is_higher_better:
99                 if self._trial.storage.get_study_direction(self._trial.study._study_id) != \
100                         optuna.structs.StudyDirection.MAXIMIZE:
101                     raise ValueError(
102                         "The intermediate values are inconsistent with the objective values in "
103                         "terms of study directions. Please specify a metric to be minimized for "
104                         "LightGBMPruningCallback.")
105             else:
106                 if self._trial.storage.get_study_direction(self._trial.study._study_id) != \
107                         optuna.structs.StudyDirection.MINIMIZE:
108                     raise ValueError(
109                         "The intermediate values are inconsistent with the objective values in "
110                         "terms of study directions. Please specify a metric to be maximized for "
111                         "LightGBMPruningCallback.")
112 
113             self._trial.report(current_score, step=env.iteration)
114             if self._trial.should_prune():
115                 message = "Trial was pruned at iteration {}.".format(env.iteration)
116                 raise optuna.exceptions.TrialPruned(message)
117 
118             return None
119 
120         raise ValueError(
121             'The entry associated with the validation name "{}" and the metric name "{}" '
122             'is not found in the evaluation result list {}.'.format(
123                 target_valid_name, self._metric, str(env.evaluation_result_list)))
124 
125 
126 def _check_lightgbm_availability():
127     # type: () -> None
128 
129     if not _available:
130         raise ImportError(
131             'LightGBM is not available. Please install LightGBM to use this feature. '
132             'LightGBM can be installed by executing `$ pip install lightgbm`. '
133             'For further information, please refer to the installation guide of LightGBM. '
134             '(The actual import error is as follows: ' + str(_import_error) + ')')
135
```

Path: `setup.py`
Content:
```
1 import os
2 import sys
3 
4 import pkg_resources
5 from setuptools import find_packages
6 from setuptools import setup
7 
8 from typing import Dict  # NOQA
9 from typing import List  # NOQA
10 from typing import Optional  # NOQA
11 
12 
13 def get_version():
14     # type: () -> str
15 
16     version_filepath = os.path.join(os.path.dirname(__file__), 'optuna', 'version.py')
17     with open(version_filepath) as f:
18         for line in f:
19             if line.startswith('__version__'):
20                 return line.strip().split()[-1][1:-1]
21     assert False
22 
23 
24 def get_long_description():
25     # type: () -> str
26 
27     readme_filepath = os.path.join(os.path.dirname(__file__), 'README.md')
28     with open(readme_filepath) as f:
29         return f.read()
30 
31 
32 def get_install_requires():
33     # type: () -> List[str]
34 
35     return [
36         'alembic',
37         'cliff',
38         'colorlog',
39         'numpy',
40         'scipy!=1.4.0',
41         'sqlalchemy>=1.1.0',
42         'tqdm',
43         'joblib',
44     ]
45 
46 
47 def get_tests_require():
48     # type: () -> List[str]
49 
50     return get_extras_require()['testing']
51 
52 
53 def get_extras_require():
54     # type: () -> Dict[str, List[str]]
55 
56     requirements = {
57         'checking': [
58             'autopep8',
59             'hacking',
60             'mypy',
61         ],
62         'codecov': [
63             'codecov',
64             'pytest-cov',
65         ],
66         'doctest': [
67             'pandas',
68             'scikit-learn>=0.19.0',
69         ],
70         'document': [
71             'lightgbm',
72             'sphinx',
73             'sphinx_rtd_theme',
74         ],
75         'example': [
76             'catboost',
77             'chainer',
78             'lightgbm',
79             'mlflow',
80             'mxnet',
81             'pytorch-ignite',
82             'scikit-image',
83             'scikit-learn',
84             'torch',
85             'torchvision>=0.5.0',
86             'xgboost',
87         ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])
88         + ([
89             'dask[dataframe]',
90             'dask-ml',
91             'keras',
92             'pytorch-lightning',
93             'tensorflow>=2.0.0',
94         ] if sys.version_info[:2] < (3, 8) else []),
95         'testing': [
96             'bokeh',
97             'chainer>=5.0.0',
98             'cma',
99             'lightgbm',
100             'mock',
101             'mpi4py',
102             'mxnet',
103             'pandas',
104             'plotly>=4.0.0',
105             'pytest',
106             'pytorch-ignite',
107             'scikit-learn>=0.19.0',
108             'scikit-optimize',
109             'torch',
110             'torchvision>=0.5.0',
111             'xgboost',
112         ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])
113         + ([
114             'keras',
115             'pytorch-lightning',
116             'tensorflow',
117             'tensorflow-datasets',
118         ] if sys.version_info[:2] < (3, 8) else []),
119     }
120 
121     return requirements
122 
123 
124 def find_any_distribution(pkgs):
125     # type: (List[str]) -> Optional[pkg_resources.Distribution]
126 
127     for pkg in pkgs:
128         try:
129             return pkg_resources.get_distribution(pkg)
130         except pkg_resources.DistributionNotFound:
131             pass
132     return None
133 
134 
135 pfnopt_pkg = find_any_distribution(['pfnopt'])
136 if pfnopt_pkg is not None:
137     msg = 'We detected that PFNOpt is installed in your environment.\n' \
138         'PFNOpt has been renamed Optuna. Please uninstall the old\n' \
139         'PFNOpt in advance (e.g. by executing `$ pip uninstall pfnopt`).'
140     print(msg)
141     exit(1)
142 
143 setup(
144     name='optuna',
145     version=get_version(),
146     description='A hyperparameter optimization framework',
147     long_description=get_long_description(),
148     long_description_content_type='text/markdown',
149     author='Takuya Akiba',
150     author_email='akiba@preferred.jp',
151     url='https://optuna.org/',
152     packages=find_packages(),
153     package_data={
154         'optuna': [
155             'storages/rdb/alembic.ini',
156             'storages/rdb/alembic/*.*',
157             'storages/rdb/alembic/versions/*.*'
158         ]
159     },
160     install_requires=get_install_requires(),
161     tests_require=get_tests_require(),
162     extras_require=get_extras_require(),
163     entry_points={'console_scripts': ['optuna = optuna.cli:main']})
164
```

Path: `optuna/integration/lightgbm_tuner/__init__.py`
Content:
```
1 from typing import Any
2 
3 from optuna._experimental import experimental
4 from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA
5 from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner
6 
7 
8 @experimental("0.18.0")
9 def train(*args: Any, **kwargs: Any) -> Any:
10     """Wrapper of LightGBM Training API to tune hyperparameters.
11 
12     It tunes important hyperparameters (e.g., `min_child_samples` and `feature_fraction`) in a
13     stepwise manner. Arguments and keyword arguments for `lightgbm.train()
14     <https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html>`_ can be passed.
15     """
16 
17     auto_booster = LightGBMTuner(*args, **kwargs)
18     booster = auto_booster.run()
19     return booster
20
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/optuna/integration/lightgbm.py b/optuna/integration/lightgbm.py
--- a/optuna/integration/lightgbm.py
+++ b/optuna/integration/lightgbm.py
@@ -2,6 +2,8 @@
 
 import optuna
 
+from optuna.integration import lightgbm_tuner as tuner
+
 try:
     import lightgbm as lgb  # NOQA
     _available = True
@@ -13,10 +15,7 @@
 
 # Attach lightgbm API.
 if _available:
-    # API from optuna integration.
-    from optuna.integration import lightgbm_tuner as tuner
-
-    # Workaround for mypy.
+    # To pass tests/integration_tests/lightgbm_tuner_tests/test_optimize.py.
     from lightgbm import Dataset  # NOQA
     from optuna.integration.lightgbm_tuner import LightGBMTuner  # NOQA
 
@@ -28,10 +27,12 @@
             continue
         setattr(sys.modules[__name__], api_name, lgb.__dict__[api_name])
 
+    # API from lightgbm_tuner.
     for api_name in _names_from_tuners:
         setattr(sys.modules[__name__], api_name, tuner.__dict__[api_name])
 else:
-    LightGBMTuner = object  # type: ignore
+    # To create docstring of train.
+    setattr(sys.modules[__name__], 'train', tuner.__dict__['train'])
 
 
 class LightGBMPruningCallback(object):
diff --git a/optuna/integration/lightgbm_tuner/__init__.py b/optuna/integration/lightgbm_tuner/__init__.py
--- a/optuna/integration/lightgbm_tuner/__init__.py
+++ b/optuna/integration/lightgbm_tuner/__init__.py
@@ -1,8 +1,20 @@
 from typing import Any
 
 from optuna._experimental import experimental
-from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA
-from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner
+from optuna import type_checking
+
+try:
+    from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA
+    from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner
+    _available = True
+except ImportError as e:
+    _import_error = e
+    # LightGBMTuner is disabled because LightGBM is not available.
+    _available = False
+
+
+if type_checking.TYPE_CHECKING:
+    from typing import Any  # NOQA
 
 
 @experimental("0.18.0")
@@ -13,7 +25,19 @@
     stepwise manner. Arguments and keyword arguments for `lightgbm.train()
     <https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html>`_ can be passed.
     """
+    _check_lightgbm_availability()
 
     auto_booster = LightGBMTuner(*args, **kwargs)
     booster = auto_booster.run()
     return booster
+
+
+def _check_lightgbm_availability():
+    # type: () -> None
+
+    if not _available:
+        raise ImportError(
+            'LightGBM is not available. Please install LightGBM to use this feature. '
+            'LightGBM can be installed by executing `$ pip install lightgbm`. '
+            'For further information, please refer to the installation guide of LightGBM. '
+            '(The actual import error is as follows: ' + str(_import_error) + ')')
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -68,7 +68,6 @@
             'scikit-learn>=0.19.0',
         ],
         'document': [
-            'lightgbm',
             'sphinx',
             'sphinx_rtd_theme',
         ],

{"golden_diff": "diff --git a/optuna/integration/lightgbm.py b/optuna/integration/lightgbm.py\n--- a/optuna/integration/lightgbm.py\n+++ b/optuna/integration/lightgbm.py\n@@ -2,6 +2,8 @@\n \n import optuna\n \n+from optuna.integration import lightgbm_tuner as tuner\n+\n try:\n     import lightgbm as lgb  # NOQA\n     _available = True\n@@ -13,10 +15,7 @@\n \n # Attach lightgbm API.\n if _available:\n-    # API from optuna integration.\n-    from optuna.integration import lightgbm_tuner as tuner\n-\n-    # Workaround for mypy.\n+    # To pass tests/integration_tests/lightgbm_tuner_tests/test_optimize.py.\n     from lightgbm import Dataset  # NOQA\n     from optuna.integration.lightgbm_tuner import LightGBMTuner  # NOQA\n \n@@ -28,10 +27,12 @@\n             continue\n         setattr(sys.modules[__name__], api_name, lgb.__dict__[api_name])\n \n+    # API from lightgbm_tuner.\n     for api_name in _names_from_tuners:\n         setattr(sys.modules[__name__], api_name, tuner.__dict__[api_name])\n else:\n-    LightGBMTuner = object  # type: ignore\n+    # To create docstring of train.\n+    setattr(sys.modules[__name__], 'train', tuner.__dict__['train'])\n \n \n class LightGBMPruningCallback(object):\ndiff --git a/optuna/integration/lightgbm_tuner/__init__.py b/optuna/integration/lightgbm_tuner/__init__.py\n--- a/optuna/integration/lightgbm_tuner/__init__.py\n+++ b/optuna/integration/lightgbm_tuner/__init__.py\n@@ -1,8 +1,20 @@\n from typing import Any\n \n from optuna._experimental import experimental\n-from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA\n-from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner\n+from optuna import type_checking\n+\n+try:\n+    from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA\n+    from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner\n+    _available = True\n+except ImportError as e:\n+    _import_error = e\n+    # LightGBMTuner is disabled because LightGBM is not available.\n+    _available = False\n+\n+\n+if type_checking.TYPE_CHECKING:\n+    from typing import Any  # NOQA\n \n \n @experimental(\"0.18.0\")\n@@ -13,7 +25,19 @@\n     stepwise manner. Arguments and keyword arguments for `lightgbm.train()\n     <https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html>`_ can be passed.\n     \"\"\"\n+    _check_lightgbm_availability()\n \n     auto_booster = LightGBMTuner(*args, **kwargs)\n     booster = auto_booster.run()\n     return booster\n+\n+\n+def _check_lightgbm_availability():\n+    # type: () -> None\n+\n+    if not _available:\n+        raise ImportError(\n+            'LightGBM is not available. Please install LightGBM to use this feature. '\n+            'LightGBM can be installed by executing `$ pip install lightgbm`. '\n+            'For further information, please refer to the installation guide of LightGBM. '\n+            '(The actual import error is as follows: ' + str(_import_error) + ')')\ndiff --git a/setup.py b/setup.py\n--- a/setup.py\n+++ b/setup.py\n@@ -68,7 +68,6 @@\n             'scikit-learn>=0.19.0',\n         ],\n         'document': [\n-            'lightgbm',\n             'sphinx',\n             'sphinx_rtd_theme',\n         ],\n", "issue": "The doc of `optuna.integration.lightgbm.train` function cannot be generated if LightGBM isn't installed.\nDue to the bug the title describes, [the current API reference doc](https://optuna.readthedocs.io/en/latest/) doesn't contain the entry of `optuna.integration.lightgbm.train` function.\r\n\r\n# Expected behavior\r\n\r\nThe build of the function doc should be succeeded even if LightGBM isn't installed in the build environment.\r\n\r\n## Error messages, stack traces, or logs\r\n\r\n```console\r\n$ cd optuna/docs\r\n$ make html\r\n...\r\nWARNING: autodoc: failed to import function 'train' from module 'optuna.integration.lightgbm'; the following exception was raised:\r\nTraceback (most recent call last):\r\n  File \"/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/util/inspect.py\", line 225, in safe_getattr\r\n    return getattr(obj, name, *defargs)\r\nAttributeError: module 'optuna.integration.lightgbm' has no attribute 'train'\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File \"/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/importer.py\", line 193, in import_object\r\n    obj = attrgetter(obj, attrname)\r\n  File \"/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/__init__.py\", line 290, in get_attr\r\n    return autodoc_attrgetter(self.env.app, obj, name, *defargs)\r\n  File \"/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/ext/autodoc/__init__.py\", line 1563, in autodoc_attrgetter\r\n    return safe_getattr(obj, name, *defargs)\r\n  File \"/home/docs/checkouts/readthedocs.org/user_builds/optuna/envs/latest/lib/python3.7/site-packages/sphinx/util/inspect.py\", line 241, in safe_getattr\r\n    raise AttributeError(name)\r\nAttributeError: train\r\n...\r\n```\r\n\r\n## Steps to reproduce\r\n\r\n1. Ensure that LightGBM isn't installed in the built environment.\r\n2. `$ git clone git://github.com/optuna/optuna.git`\r\n3. `$ cd optuna/docs`\r\n4. `$ make html`\r\n\r\n\r\n\n", "before_files": [{"content": "import sys\n\nimport optuna\n\ntry:\n    import lightgbm as lgb  # NOQA\n    _available = True\nexcept ImportError as e:\n    _import_error = e\n    # LightGBMPruningCallback is disabled because LightGBM is not available.\n    _available = False\n\n\n# Attach lightgbm API.\nif _available:\n    # API from optuna integration.\n    from optuna.integration import lightgbm_tuner as tuner\n\n    # Workaround for mypy.\n    from lightgbm import Dataset  # NOQA\n    from optuna.integration.lightgbm_tuner import LightGBMTuner  # NOQA\n\n    _names_from_tuners = ['train', 'LGBMModel', 'LGBMClassifier', 'LGBMRegressor']\n\n    # API from lightgbm.\n    for api_name in lgb.__dict__['__all__']:\n        if api_name in _names_from_tuners:\n            continue\n        setattr(sys.modules[__name__], api_name, lgb.__dict__[api_name])\n\n    for api_name in _names_from_tuners:\n        setattr(sys.modules[__name__], api_name, tuner.__dict__[api_name])\nelse:\n    LightGBMTuner = object  # type: ignore\n\n\nclass LightGBMPruningCallback(object):\n    \"\"\"Callback for LightGBM to prune unpromising trials.\n\n    Example:\n\n        Add a pruning callback which observes validation scores to training of a LightGBM model.\n\n        .. code::\n\n                param = {'objective': 'binary', 'metric': 'binary_error'}\n                pruning_callback = LightGBMPruningCallback(trial, 'binary_error')\n                gbm = lgb.train(param, dtrain, valid_sets=[dtest], callbacks=[pruning_callback])\n\n    Args:\n        trial:\n            A :class:`~optuna.trial.Trial` corresponding to the current evaluation of\n            the objective function.\n        metric:\n            An evaluation metric for pruning, e.g., ``binary_error`` and ``multi_error``.\n            Please refer to\n            `LightGBM reference\n            <https://lightgbm.readthedocs.io/en/latest/Parameters.html#metric>`_\n            for further details.\n        valid_name:\n            The name of the target validation.\n            Validation names are specified by ``valid_names`` option of\n            `train method\n            <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.train>`_.\n            If omitted, ``valid_0`` is used which is the default name of the first validation.\n            Note that this argument will be ignored if you are calling\n            `cv method <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.cv>`_\n            instead of train method.\n    \"\"\"\n\n    def __init__(self, trial, metric, valid_name='valid_0'):\n        # type: (optuna.trial.Trial, str, str) -> None\n\n        _check_lightgbm_availability()\n\n        self._trial = trial\n        self._valid_name = valid_name\n        self._metric = metric\n\n    def __call__(self, env):\n        # type: (lgb.callback.CallbackEnv) -> None\n\n        # If this callback has been passed to `lightgbm.cv` function,\n        # the value of `is_cv` becomes `True`. See also:\n        # https://github.com/Microsoft/LightGBM/blob/v2.2.2/python-package/lightgbm/engine.py#L329\n        # Note that `5` is not the number of folds but the length of sequence.\n        is_cv = len(env.evaluation_result_list) > 0 and len(env.evaluation_result_list[0]) == 5\n        if is_cv:\n            target_valid_name = 'cv_agg'\n        else:\n            target_valid_name = self._valid_name\n\n        for evaluation_result in env.evaluation_result_list:\n            valid_name, metric, current_score, is_higher_better = evaluation_result[:4]\n            if valid_name != target_valid_name or metric != self._metric:\n                continue\n\n            if is_higher_better:\n                if self._trial.storage.get_study_direction(self._trial.study._study_id) != \\\n                        optuna.structs.StudyDirection.MAXIMIZE:\n                    raise ValueError(\n                        \"The intermediate values are inconsistent with the objective values in \"\n                        \"terms of study directions. Please specify a metric to be minimized for \"\n                        \"LightGBMPruningCallback.\")\n            else:\n                if self._trial.storage.get_study_direction(self._trial.study._study_id) != \\\n                        optuna.structs.StudyDirection.MINIMIZE:\n                    raise ValueError(\n                        \"The intermediate values are inconsistent with the objective values in \"\n                        \"terms of study directions. Please specify a metric to be maximized for \"\n                        \"LightGBMPruningCallback.\")\n\n            self._trial.report(current_score, step=env.iteration)\n            if self._trial.should_prune():\n                message = \"Trial was pruned at iteration {}.\".format(env.iteration)\n                raise optuna.exceptions.TrialPruned(message)\n\n            return None\n\n        raise ValueError(\n            'The entry associated with the validation name \"{}\" and the metric name \"{}\" '\n            'is not found in the evaluation result list {}.'.format(\n                target_valid_name, self._metric, str(env.evaluation_result_list)))\n\n\ndef _check_lightgbm_availability():\n    # type: () -> None\n\n    if not _available:\n        raise ImportError(\n            'LightGBM is not available. Please install LightGBM to use this feature. '\n            'LightGBM can be installed by executing `$ pip install lightgbm`. '\n            'For further information, please refer to the installation guide of LightGBM. '\n            '(The actual import error is as follows: ' + str(_import_error) + ')')\n", "path": "optuna/integration/lightgbm.py"}, {"content": "import os\nimport sys\n\nimport pkg_resources\nfrom setuptools import find_packages\nfrom setuptools import setup\n\nfrom typing import Dict  # NOQA\nfrom typing import List  # NOQA\nfrom typing import Optional  # NOQA\n\n\ndef get_version():\n    # type: () -> str\n\n    version_filepath = os.path.join(os.path.dirname(__file__), 'optuna', 'version.py')\n    with open(version_filepath) as f:\n        for line in f:\n            if line.startswith('__version__'):\n                return line.strip().split()[-1][1:-1]\n    assert False\n\n\ndef get_long_description():\n    # type: () -> str\n\n    readme_filepath = os.path.join(os.path.dirname(__file__), 'README.md')\n    with open(readme_filepath) as f:\n        return f.read()\n\n\ndef get_install_requires():\n    # type: () -> List[str]\n\n    return [\n        'alembic',\n        'cliff',\n        'colorlog',\n        'numpy',\n        'scipy!=1.4.0',\n        'sqlalchemy>=1.1.0',\n        'tqdm',\n        'joblib',\n    ]\n\n\ndef get_tests_require():\n    # type: () -> List[str]\n\n    return get_extras_require()['testing']\n\n\ndef get_extras_require():\n    # type: () -> Dict[str, List[str]]\n\n    requirements = {\n        'checking': [\n            'autopep8',\n            'hacking',\n            'mypy',\n        ],\n        'codecov': [\n            'codecov',\n            'pytest-cov',\n        ],\n        'doctest': [\n            'pandas',\n            'scikit-learn>=0.19.0',\n        ],\n        'document': [\n            'lightgbm',\n            'sphinx',\n            'sphinx_rtd_theme',\n        ],\n        'example': [\n            'catboost',\n            'chainer',\n            'lightgbm',\n            'mlflow',\n            'mxnet',\n            'pytorch-ignite',\n            'scikit-image',\n            'scikit-learn',\n            'torch',\n            'torchvision>=0.5.0',\n            'xgboost',\n        ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])\n        + ([\n            'dask[dataframe]',\n            'dask-ml',\n            'keras',\n            'pytorch-lightning',\n            'tensorflow>=2.0.0',\n        ] if sys.version_info[:2] < (3, 8) else []),\n        'testing': [\n            'bokeh',\n            'chainer>=5.0.0',\n            'cma',\n            'lightgbm',\n            'mock',\n            'mpi4py',\n            'mxnet',\n            'pandas',\n            'plotly>=4.0.0',\n            'pytest',\n            'pytorch-ignite',\n            'scikit-learn>=0.19.0',\n            'scikit-optimize',\n            'torch',\n            'torchvision>=0.5.0',\n            'xgboost',\n        ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])\n        + ([\n            'keras',\n            'pytorch-lightning',\n            'tensorflow',\n            'tensorflow-datasets',\n        ] if sys.version_info[:2] < (3, 8) else []),\n    }\n\n    return requirements\n\n\ndef find_any_distribution(pkgs):\n    # type: (List[str]) -> Optional[pkg_resources.Distribution]\n\n    for pkg in pkgs:\n        try:\n            return pkg_resources.get_distribution(pkg)\n        except pkg_resources.DistributionNotFound:\n            pass\n    return None\n\n\npfnopt_pkg = find_any_distribution(['pfnopt'])\nif pfnopt_pkg is not None:\n    msg = 'We detected that PFNOpt is installed in your environment.\\n' \\\n        'PFNOpt has been renamed Optuna. Please uninstall the old\\n' \\\n        'PFNOpt in advance (e.g. by executing `$ pip uninstall pfnopt`).'\n    print(msg)\n    exit(1)\n\nsetup(\n    name='optuna',\n    version=get_version(),\n    description='A hyperparameter optimization framework',\n    long_description=get_long_description(),\n    long_description_content_type='text/markdown',\n    author='Takuya Akiba',\n    author_email='akiba@preferred.jp',\n    url='https://optuna.org/',\n    packages=find_packages(),\n    package_data={\n        'optuna': [\n            'storages/rdb/alembic.ini',\n            'storages/rdb/alembic/*.*',\n            'storages/rdb/alembic/versions/*.*'\n        ]\n    },\n    install_requires=get_install_requires(),\n    tests_require=get_tests_require(),\n    extras_require=get_extras_require(),\n    entry_points={'console_scripts': ['optuna = optuna.cli:main']})\n", "path": "setup.py"}, {"content": "from typing import Any\n\nfrom optuna._experimental import experimental\nfrom optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA\nfrom optuna.integration.lightgbm_tuner.optimize import LightGBMTuner\n\n\n@experimental(\"0.18.0\")\ndef train(*args: Any, **kwargs: Any) -> Any:\n    \"\"\"Wrapper of LightGBM Training API to tune hyperparameters.\n\n    It tunes important hyperparameters (e.g., `min_child_samples` and `feature_fraction`) in a\n    stepwise manner. Arguments and keyword arguments for `lightgbm.train()\n    <https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html>`_ can be passed.\n    \"\"\"\n\n    auto_booster = LightGBMTuner(*args, **kwargs)\n    booster = auto_booster.run()\n    return booster\n", "path": "optuna/integration/lightgbm_tuner/__init__.py"}], "after_files": [{"content": "import sys\n\nimport optuna\n\nfrom optuna.integration import lightgbm_tuner as tuner\n\ntry:\n    import lightgbm as lgb  # NOQA\n    _available = True\nexcept ImportError as e:\n    _import_error = e\n    # LightGBMPruningCallback is disabled because LightGBM is not available.\n    _available = False\n\n\n# Attach lightgbm API.\nif _available:\n    # To pass tests/integration_tests/lightgbm_tuner_tests/test_optimize.py.\n    from lightgbm import Dataset  # NOQA\n    from optuna.integration.lightgbm_tuner import LightGBMTuner  # NOQA\n\n    _names_from_tuners = ['train', 'LGBMModel', 'LGBMClassifier', 'LGBMRegressor']\n\n    # API from lightgbm.\n    for api_name in lgb.__dict__['__all__']:\n        if api_name in _names_from_tuners:\n            continue\n        setattr(sys.modules[__name__], api_name, lgb.__dict__[api_name])\n\n    # API from lightgbm_tuner.\n    for api_name in _names_from_tuners:\n        setattr(sys.modules[__name__], api_name, tuner.__dict__[api_name])\nelse:\n    # To create docstring of train.\n    setattr(sys.modules[__name__], 'train', tuner.__dict__['train'])\n\n\nclass LightGBMPruningCallback(object):\n    \"\"\"Callback for LightGBM to prune unpromising trials.\n\n    Example:\n\n        Add a pruning callback which observes validation scores to training of a LightGBM model.\n\n        .. code::\n\n                param = {'objective': 'binary', 'metric': 'binary_error'}\n                pruning_callback = LightGBMPruningCallback(trial, 'binary_error')\n                gbm = lgb.train(param, dtrain, valid_sets=[dtest], callbacks=[pruning_callback])\n\n    Args:\n        trial:\n            A :class:`~optuna.trial.Trial` corresponding to the current evaluation of\n            the objective function.\n        metric:\n            An evaluation metric for pruning, e.g., ``binary_error`` and ``multi_error``.\n            Please refer to\n            `LightGBM reference\n            <https://lightgbm.readthedocs.io/en/latest/Parameters.html#metric>`_\n            for further details.\n        valid_name:\n            The name of the target validation.\n            Validation names are specified by ``valid_names`` option of\n            `train method\n            <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.train>`_.\n            If omitted, ``valid_0`` is used which is the default name of the first validation.\n            Note that this argument will be ignored if you are calling\n            `cv method <https://lightgbm.readthedocs.io/en/latest/Python-API.html#lightgbm.cv>`_\n            instead of train method.\n    \"\"\"\n\n    def __init__(self, trial, metric, valid_name='valid_0'):\n        # type: (optuna.trial.Trial, str, str) -> None\n\n        _check_lightgbm_availability()\n\n        self._trial = trial\n        self._valid_name = valid_name\n        self._metric = metric\n\n    def __call__(self, env):\n        # type: (lgb.callback.CallbackEnv) -> None\n\n        # If this callback has been passed to `lightgbm.cv` function,\n        # the value of `is_cv` becomes `True`. See also:\n        # https://github.com/Microsoft/LightGBM/blob/v2.2.2/python-package/lightgbm/engine.py#L329\n        # Note that `5` is not the number of folds but the length of sequence.\n        is_cv = len(env.evaluation_result_list) > 0 and len(env.evaluation_result_list[0]) == 5\n        if is_cv:\n            target_valid_name = 'cv_agg'\n        else:\n            target_valid_name = self._valid_name\n\n        for evaluation_result in env.evaluation_result_list:\n            valid_name, metric, current_score, is_higher_better = evaluation_result[:4]\n            if valid_name != target_valid_name or metric != self._metric:\n                continue\n\n            if is_higher_better:\n                if self._trial.storage.get_study_direction(self._trial.study._study_id) != \\\n                        optuna.structs.StudyDirection.MAXIMIZE:\n                    raise ValueError(\n                        \"The intermediate values are inconsistent with the objective values in \"\n                        \"terms of study directions. Please specify a metric to be minimized for \"\n                        \"LightGBMPruningCallback.\")\n            else:\n                if self._trial.storage.get_study_direction(self._trial.study._study_id) != \\\n                        optuna.structs.StudyDirection.MINIMIZE:\n                    raise ValueError(\n                        \"The intermediate values are inconsistent with the objective values in \"\n                        \"terms of study directions. Please specify a metric to be maximized for \"\n                        \"LightGBMPruningCallback.\")\n\n            self._trial.report(current_score, step=env.iteration)\n            if self._trial.should_prune():\n                message = \"Trial was pruned at iteration {}.\".format(env.iteration)\n                raise optuna.exceptions.TrialPruned(message)\n\n            return None\n\n        raise ValueError(\n            'The entry associated with the validation name \"{}\" and the metric name \"{}\" '\n            'is not found in the evaluation result list {}.'.format(\n                target_valid_name, self._metric, str(env.evaluation_result_list)))\n\n\ndef _check_lightgbm_availability():\n    # type: () -> None\n\n    if not _available:\n        raise ImportError(\n            'LightGBM is not available. Please install LightGBM to use this feature. '\n            'LightGBM can be installed by executing `$ pip install lightgbm`. '\n            'For further information, please refer to the installation guide of LightGBM. '\n            '(The actual import error is as follows: ' + str(_import_error) + ')')\n", "path": "optuna/integration/lightgbm.py"}, {"content": "import os\nimport sys\n\nimport pkg_resources\nfrom setuptools import find_packages\nfrom setuptools import setup\n\nfrom typing import Dict  # NOQA\nfrom typing import List  # NOQA\nfrom typing import Optional  # NOQA\n\n\ndef get_version():\n    # type: () -> str\n\n    version_filepath = os.path.join(os.path.dirname(__file__), 'optuna', 'version.py')\n    with open(version_filepath) as f:\n        for line in f:\n            if line.startswith('__version__'):\n                return line.strip().split()[-1][1:-1]\n    assert False\n\n\ndef get_long_description():\n    # type: () -> str\n\n    readme_filepath = os.path.join(os.path.dirname(__file__), 'README.md')\n    with open(readme_filepath) as f:\n        return f.read()\n\n\ndef get_install_requires():\n    # type: () -> List[str]\n\n    return [\n        'alembic',\n        'cliff',\n        'colorlog',\n        'numpy',\n        'scipy!=1.4.0',\n        'sqlalchemy>=1.1.0',\n        'tqdm',\n        'joblib',\n    ]\n\n\ndef get_tests_require():\n    # type: () -> List[str]\n\n    return get_extras_require()['testing']\n\n\ndef get_extras_require():\n    # type: () -> Dict[str, List[str]]\n\n    requirements = {\n        'checking': [\n            'autopep8',\n            'hacking',\n            'mypy',\n        ],\n        'codecov': [\n            'codecov',\n            'pytest-cov',\n        ],\n        'doctest': [\n            'pandas',\n            'scikit-learn>=0.19.0',\n        ],\n        'document': [\n            'sphinx',\n            'sphinx_rtd_theme',\n        ],\n        'example': [\n            'catboost',\n            'chainer',\n            'lightgbm',\n            'mlflow',\n            'mxnet',\n            'pytorch-ignite',\n            'scikit-image',\n            'scikit-learn',\n            'torch',\n            'torchvision>=0.5.0',\n            'xgboost',\n        ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])\n        + ([\n            'dask[dataframe]',\n            'dask-ml',\n            'keras',\n            'pytorch-lightning',\n            'tensorflow>=2.0.0',\n        ] if sys.version_info[:2] < (3, 8) else []),\n        'testing': [\n            'bokeh',\n            'chainer>=5.0.0',\n            'cma',\n            'lightgbm',\n            'mock',\n            'mpi4py',\n            'mxnet',\n            'pandas',\n            'plotly>=4.0.0',\n            'pytest',\n            'pytorch-ignite',\n            'scikit-learn>=0.19.0',\n            'scikit-optimize',\n            'torch',\n            'torchvision>=0.5.0',\n            'xgboost',\n        ] + (['fastai<2'] if (3, 5) < sys.version_info[:2] < (3, 8) else [])\n        + ([\n            'keras',\n            'pytorch-lightning',\n            'tensorflow',\n            'tensorflow-datasets',\n        ] if sys.version_info[:2] < (3, 8) else []),\n    }\n\n    return requirements\n\n\ndef find_any_distribution(pkgs):\n    # type: (List[str]) -> Optional[pkg_resources.Distribution]\n\n    for pkg in pkgs:\n        try:\n            return pkg_resources.get_distribution(pkg)\n        except pkg_resources.DistributionNotFound:\n            pass\n    return None\n\n\npfnopt_pkg = find_any_distribution(['pfnopt'])\nif pfnopt_pkg is not None:\n    msg = 'We detected that PFNOpt is installed in your environment.\\n' \\\n        'PFNOpt has been renamed Optuna. Please uninstall the old\\n' \\\n        'PFNOpt in advance (e.g. by executing `$ pip uninstall pfnopt`).'\n    print(msg)\n    exit(1)\n\nsetup(\n    name='optuna',\n    version=get_version(),\n    description='A hyperparameter optimization framework',\n    long_description=get_long_description(),\n    long_description_content_type='text/markdown',\n    author='Takuya Akiba',\n    author_email='akiba@preferred.jp',\n    url='https://optuna.org/',\n    packages=find_packages(),\n    package_data={\n        'optuna': [\n            'storages/rdb/alembic.ini',\n            'storages/rdb/alembic/*.*',\n            'storages/rdb/alembic/versions/*.*'\n        ]\n    },\n    install_requires=get_install_requires(),\n    tests_require=get_tests_require(),\n    extras_require=get_extras_require(),\n    entry_points={'console_scripts': ['optuna = optuna.cli:main']})\n", "path": "setup.py"}, {"content": "from typing import Any\n\nfrom optuna._experimental import experimental\nfrom optuna import type_checking\n\ntry:\n    from optuna.integration.lightgbm_tuner.sklearn import LGBMClassifier, LGBMModel, LGBMRegressor  # NOQA\n    from optuna.integration.lightgbm_tuner.optimize import LightGBMTuner\n    _available = True\nexcept ImportError as e:\n    _import_error = e\n    # LightGBMTuner is disabled because LightGBM is not available.\n    _available = False\n\n\nif type_checking.TYPE_CHECKING:\n    from typing import Any  # NOQA\n\n\n@experimental(\"0.18.0\")\ndef train(*args: Any, **kwargs: Any) -> Any:\n    \"\"\"Wrapper of LightGBM Training API to tune hyperparameters.\n\n    It tunes important hyperparameters (e.g., `min_child_samples` and `feature_fraction`) in a\n    stepwise manner. Arguments and keyword arguments for `lightgbm.train()\n    <https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html>`_ can be passed.\n    \"\"\"\n    _check_lightgbm_availability()\n\n    auto_booster = LightGBMTuner(*args, **kwargs)\n    booster = auto_booster.run()\n    return booster\n\n\ndef _check_lightgbm_availability():\n    # type: () -> None\n\n    if not _available:\n        raise ImportError(\n            'LightGBM is not available. Please install LightGBM to use this feature. '\n            'LightGBM can be installed by executing `$ pip install lightgbm`. '\n            'For further information, please refer to the installation guide of LightGBM. '\n            '(The actual import error is as follows: ' + str(_import_error) + ')')\n", "path": "optuna/integration/lightgbm_tuner/__init__.py"}]}