We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Dashboard only supports direction="minimize"

![image](https://user-images.githubusercontent.com/13174805/66481128-821b3980-eaa0-11e9-835e-34f47db3c2a6.png)

I used `optuna create-study --direction maximize --storage sqlite:///example.db` to create my study but the curve on the dashboard tracks only the minimum, not the maximum.

Maybe this is an error of `create-study` that doesn't take my argument into account…

--- 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/dashboard.py`
Content:
```
1 try:
2     import bokeh.command.bootstrap
3     import bokeh.document  # NOQA
4     import bokeh.layouts
5     import bokeh.models
6     import bokeh.models.widgets
7     import bokeh.plotting
8     import bokeh.themes
9     import tornado.gen
10     _available = True
11 except ImportError as e:
12     _available = False
13     _import_error = e
14 
15 import collections
16 import numpy as np
17 import threading
18 import time
19 
20 import optuna.logging
21 import optuna.structs
22 import optuna.study
23 from optuna import type_checking
24 
25 if type_checking.TYPE_CHECKING:
26     from typing import Any  # NOQA
27     from typing import Dict  # NOQA
28     from typing import List  # NOQA
29     from typing import Optional  # NOQA
30 
31 _mode = None  # type: Optional[str]
32 _study = None  # type: Optional[optuna.study.Study]
33 
34 _HEADER_FORMAT = '''
35 <style>
36 body {{
37     margin: 20px;
38 }}
39 h1, p {{
40     margin: 10px 0px;
41 }}
42 </style>
43 
44 <h1>Optuna Dashboard (Beta)</h1>
45 <p>
46 <b>Study name:</b> {study_name}<br>
47 </p>
48 '''
49 
50 _DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
51 
52 if _available:
53 
54     class _CompleteTrialsWidget(object):
55         def __init__(self, trials):
56             # type: (List[optuna.structs.FrozenTrial]) -> None
57 
58             complete_trials = [
59                 trial for trial in trials if trial.state == optuna.structs.TrialState.COMPLETE
60             ]
61             self.trial_ids = set([trial.trial_id for trial in complete_trials])
62 
63             values = [trial.value for trial in complete_trials]
64             best_values = np.minimum.accumulate(values, axis=0)
65             self.cds = bokeh.models.ColumnDataSource({
66                 '#': list(range(len(complete_trials))),
67                 'value': values,
68                 'best_value': best_values,
69             })
70 
71             self.best_value = best_values[-1] if complete_trials else np.inf
72 
73         def create_figure(self):
74             # type: () -> bokeh.plotting.Figure
75 
76             figure = bokeh.plotting.figure(height=150)
77             figure.circle(x='#', y='value', source=self.cds, alpha=0.3, color='navy')
78             figure.line(x='#', y='best_value', source=self.cds, color='firebrick')
79             figure.xaxis[0].axis_label = 'Number of Trials'
80             figure.yaxis[0].axis_label = 'Objective Value'
81             return figure
82 
83         def update(self, new_trials):
84             # type: (List[optuna.structs.FrozenTrial]) -> None
85 
86             stream_dict = collections.defaultdict(list)  # type: Dict[str, List[Any]]
87 
88             for trial in new_trials:
89                 if trial.state != optuna.structs.TrialState.COMPLETE:
90                     continue
91                 if trial.trial_id in self.trial_ids:
92                     continue
93                 stream_dict['#'].append(len(self.trial_ids))
94                 stream_dict['value'].append(trial.value)
95                 self.best_value = min(self.best_value, trial.value)
96                 stream_dict['best_value'].append(self.best_value)
97                 self.trial_ids.add(trial.trial_id)
98 
99             if stream_dict:
100                 self.cds.stream(stream_dict)
101 
102     class _AllTrialsWidget(object):
103         def __init__(self, trials):
104             # type: (List[optuna.structs.FrozenTrial]) -> None
105 
106             self.cds = bokeh.models.ColumnDataSource(self.trials_to_dict(trials))
107 
108         def create_table(self):
109             # type: () -> bokeh.models.widgets.DataTable
110 
111             return bokeh.models.widgets.DataTable(
112                 source=self.cds,
113                 columns=[
114                     bokeh.models.widgets.TableColumn(field=field, title=field) for field in
115                     ['number', 'state', 'value', 'params', 'datetime_start', 'datetime_complete']
116                 ])
117 
118         def update(
119                 self,
120                 old_trials,  # type: List[optuna.structs.FrozenTrial]
121                 new_trials,  # type: List[optuna.structs.FrozenTrial]
122         ):
123             # type: (...) -> None
124 
125             modified_indices = []
126             modified_trials = []
127             for i, old_trial in enumerate(old_trials):
128                 new_trial = new_trials[i]
129                 if old_trial != new_trial:
130                     modified_indices.append(i)
131                     modified_trials.append(new_trial)
132 
133             patch_dict = self.trials_to_dict(modified_trials)
134             patch_dict = {k: list(zip(modified_indices, v)) for k, v in patch_dict.items()}
135             self.cds.patch(patch_dict)
136 
137             self.cds.stream(self.trials_to_dict(new_trials[len(old_trials):]))
138 
139         @staticmethod
140         def trials_to_dict(trials):
141             # type: (List[optuna.structs.FrozenTrial]) -> Dict[str, List[Any]]
142 
143             return {
144                 'number': [trial.number for trial in trials],
145                 'state': [trial.state.name for trial in trials],
146                 'value': [trial.value for trial in trials],
147                 'params': [str(trial.params) for trial in trials],
148                 'datetime_start': [
149                     trial.datetime_start.strftime(_DATETIME_FORMAT)
150                     if trial.datetime_start is not None else None for trial in trials
151                 ],
152                 'datetime_complete': [
153                     trial.datetime_complete.strftime(_DATETIME_FORMAT)
154                     if trial.datetime_complete is not None else None for trial in trials
155                 ],
156             }
157 
158     class _DashboardApp(object):
159         def __init__(self, study, launch_update_thread):
160             # type: (optuna.study.Study, bool) -> None
161 
162             self.study = study
163             self.launch_update_thread = launch_update_thread
164             self.lock = threading.Lock()
165 
166         def __call__(self, doc):
167             # type: (bokeh.document.Document) -> None
168 
169             self.doc = doc
170             self.current_trials = \
171                 self.study.trials  # type: Optional[List[optuna.structs.FrozenTrial]]
172             self.new_trials = None  # type: Optional[List[optuna.structs.FrozenTrial]]
173             self.complete_trials_widget = _CompleteTrialsWidget(self.current_trials)
174             self.all_trials_widget = _AllTrialsWidget(self.current_trials)
175 
176             self.doc.title = 'Optuna Dashboard (Beta)'
177             header = _HEADER_FORMAT.format(study_name=self.study.study_name)
178             self.doc.add_root(
179                 bokeh.layouts.layout([[bokeh.models.widgets.Div(text=header)],
180                                       [self.complete_trials_widget.create_figure()],
181                                       [self.all_trials_widget.create_table()]],
182                                      sizing_mode='scale_width'))
183 
184             if self.launch_update_thread:
185                 thread = threading.Thread(target=self.thread_loop)
186                 thread.daemon = True
187                 thread.start()
188 
189         def thread_loop(self):
190             # type: () -> None
191 
192             while True:
193                 time.sleep(1)
194                 new_trials = self.study.trials
195                 with self.lock:
196                     need_to_add_callback = (self.new_trials is None)
197                     self.new_trials = new_trials
198                     if need_to_add_callback:
199                         self.doc.add_next_tick_callback(self.update_callback)
200 
201         @tornado.gen.coroutine
202         def update_callback(self):
203             # type: () -> None
204 
205             with self.lock:
206                 current_trials = self.current_trials
207                 new_trials = self.new_trials
208                 self.current_trials = self.new_trials
209                 self.new_trials = None
210 
211             assert current_trials is not None
212             assert new_trials is not None
213             self.complete_trials_widget.update(new_trials)
214             self.all_trials_widget.update(current_trials, new_trials)
215 
216 
217 def _check_bokeh_availability():
218     # type: () -> None
219 
220     if not _available:
221         raise ImportError(
222             'Bokeh is not available. Please install Bokeh to use the dashboard. '
223             'Bokeh can be installed by executing `$ pip install bokeh`. '
224             'For further information, please refer to the installation guide of Bokeh. '
225             '(The actual import error is as follows: ' + str(_import_error) + ')')
226 
227 
228 def _show_experimental_warning():
229     # type: () -> None
230 
231     logger = optuna.logging.get_logger(__name__)
232     logger.warning('Optuna dashboard is still highly experimental. Please use with caution!')
233 
234 
235 def _get_this_source_path():
236     # type: () -> str
237 
238     path = __file__
239 
240     # Sometimes __file__ points to a *.pyc file, but Bokeh doesn't accept it.
241     if path.endswith('.pyc'):
242         path = path[:-1]
243     return path
244 
245 
246 def serve(study, bokeh_allow_websocket_origins=None):
247     # type: (optuna.study.Study, Optional[List[str]]) -> None
248 
249     global _mode, _study
250 
251     _check_bokeh_availability()
252     _show_experimental_warning()
253 
254     # We want to pass the mode (launching a server? or, just writing an HTML?) and a target study
255     # to our Bokeh app. Unfortunately, as we are using `bokeh.command.bootstrap.main` to launch
256     # our Bokeh app, we cannot directly pass Python objects to it. Therefore, we have no choice but
257     # to use global variables to pass them.
258     _mode = 'serve'
259     _study = study
260 
261     # TODO(akiba): Stop using Bokeh's CLI entry point, and start the HTTP server by ourselves.
262 
263     # This is not a very clean way to launch Bokeh server.
264     # Another seemingly better way is to
265     # instantiate and launch `bokeh.server.server.Server` by ourselves. However, in this way,
266     # for some reason, we found that the CDS update is not reflected to browsers, at least on Bokeh
267     # version 0.12.15. In addition, we will need to do many configuration to servers, which can be
268     # done automatically with the following one line. So, for now, we decided to use this way.
269     command = ['bokeh', 'serve', '--show', _get_this_source_path()]
270     if bokeh_allow_websocket_origins is not None:
271         for bokeh_allow_websocket_origin in bokeh_allow_websocket_origins:
272             command.extend(['--allow-websocket-origin', bokeh_allow_websocket_origin])
273     bokeh.command.bootstrap.main(command)
274 
275 
276 def write(study, out_path):
277     # type: (optuna.study.Study, str) -> None
278 
279     global _mode, _study
280 
281     _check_bokeh_availability()
282     _show_experimental_warning()
283 
284     _mode = 'html'
285     _study = study
286     bokeh.command.bootstrap.main(['bokeh', 'html', _get_this_source_path(), '-o', out_path])
287 
288 
289 def _run():
290     # type: () -> None
291 
292     # Please note that `_study` and `optuna.dashboard._study` are different here. Here, this module
293     # is loaded inside Bokeh, and thus it is not `optuna.dashboard`, but `bk_script_????`.
294     study = optuna.dashboard._study
295     mode = optuna.dashboard._mode
296 
297     assert study is not None
298     app = _DashboardApp(study, launch_update_thread=(mode == 'serve'))
299     doc = bokeh.plotting.curdoc()
300     app(doc)
301 
302 
303 if __name__.startswith('bk_script_'):
304     # Here, this module is loaded inside Bokeh. Therefore, we should launch the Bokeh app.
305     _run()
306
```
--- 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/dashboard.py b/optuna/dashboard.py
--- a/optuna/dashboard.py
+++ b/optuna/dashboard.py
@@ -52,16 +52,21 @@
 if _available:
 
     class _CompleteTrialsWidget(object):
-        def __init__(self, trials):
-            # type: (List[optuna.structs.FrozenTrial]) -> None
+        def __init__(self, trials, direction):
+            # type: (List[optuna.structs.FrozenTrial], optuna.structs.StudyDirection) -> None
 
             complete_trials = [
                 trial for trial in trials if trial.state == optuna.structs.TrialState.COMPLETE
             ]
             self.trial_ids = set([trial.trial_id for trial in complete_trials])
 
+            self.direction = direction
             values = [trial.value for trial in complete_trials]
-            best_values = np.minimum.accumulate(values, axis=0)
+            if direction == optuna.structs.StudyDirection.MINIMIZE:
+                best_values = np.minimum.accumulate(values, axis=0)
+            else:
+                best_values = np.maximum.accumulate(values, axis=0)
+
             self.cds = bokeh.models.ColumnDataSource({
                 '#': list(range(len(complete_trials))),
                 'value': values,
@@ -92,7 +97,10 @@
                     continue
                 stream_dict['#'].append(len(self.trial_ids))
                 stream_dict['value'].append(trial.value)
-                self.best_value = min(self.best_value, trial.value)
+                if self.direction == optuna.structs.StudyDirection.MINIMIZE:
+                    self.best_value = min(self.best_value, trial.value)
+                else:
+                    self.best_value = max(self.best_value, trial.value)
                 stream_dict['best_value'].append(self.best_value)
                 self.trial_ids.add(trial.trial_id)
 
@@ -170,7 +178,8 @@
             self.current_trials = \
                 self.study.trials  # type: Optional[List[optuna.structs.FrozenTrial]]
             self.new_trials = None  # type: Optional[List[optuna.structs.FrozenTrial]]
-            self.complete_trials_widget = _CompleteTrialsWidget(self.current_trials)
+            self.complete_trials_widget = _CompleteTrialsWidget(
+                self.current_trials, self.study.direction)
             self.all_trials_widget = _AllTrialsWidget(self.current_trials)
 
             self.doc.title = 'Optuna Dashboard (Beta)'

{"golden_diff": "diff --git a/optuna/dashboard.py b/optuna/dashboard.py\n--- a/optuna/dashboard.py\n+++ b/optuna/dashboard.py\n@@ -52,16 +52,21 @@\n if _available:\n \n     class _CompleteTrialsWidget(object):\n-        def __init__(self, trials):\n-            # type: (List[optuna.structs.FrozenTrial]) -> None\n+        def __init__(self, trials, direction):\n+            # type: (List[optuna.structs.FrozenTrial], optuna.structs.StudyDirection) -> None\n \n             complete_trials = [\n                 trial for trial in trials if trial.state == optuna.structs.TrialState.COMPLETE\n             ]\n             self.trial_ids = set([trial.trial_id for trial in complete_trials])\n \n+            self.direction = direction\n             values = [trial.value for trial in complete_trials]\n-            best_values = np.minimum.accumulate(values, axis=0)\n+            if direction == optuna.structs.StudyDirection.MINIMIZE:\n+                best_values = np.minimum.accumulate(values, axis=0)\n+            else:\n+                best_values = np.maximum.accumulate(values, axis=0)\n+\n             self.cds = bokeh.models.ColumnDataSource({\n                 '#': list(range(len(complete_trials))),\n                 'value': values,\n@@ -92,7 +97,10 @@\n                     continue\n                 stream_dict['#'].append(len(self.trial_ids))\n                 stream_dict['value'].append(trial.value)\n-                self.best_value = min(self.best_value, trial.value)\n+                if self.direction == optuna.structs.StudyDirection.MINIMIZE:\n+                    self.best_value = min(self.best_value, trial.value)\n+                else:\n+                    self.best_value = max(self.best_value, trial.value)\n                 stream_dict['best_value'].append(self.best_value)\n                 self.trial_ids.add(trial.trial_id)\n \n@@ -170,7 +178,8 @@\n             self.current_trials = \\\n                 self.study.trials  # type: Optional[List[optuna.structs.FrozenTrial]]\n             self.new_trials = None  # type: Optional[List[optuna.structs.FrozenTrial]]\n-            self.complete_trials_widget = _CompleteTrialsWidget(self.current_trials)\n+            self.complete_trials_widget = _CompleteTrialsWidget(\n+                self.current_trials, self.study.direction)\n             self.all_trials_widget = _AllTrialsWidget(self.current_trials)\n \n             self.doc.title = 'Optuna Dashboard (Beta)'\n", "issue": "Dashboard only supports direction=\"minimize\"\n\r\n![image](https://user-images.githubusercontent.com/13174805/66481128-821b3980-eaa0-11e9-835e-34f47db3c2a6.png)\r\n\r\nI used `optuna create-study --direction maximize --storage sqlite:///example.db` to create my study but the curve on the dashboard tracks only the minimum, not the maximum.\r\n\r\nMaybe this is an error of `create-study` that doesn't take my argument into account\u2026\n", "before_files": [{"content": "try:\n    import bokeh.command.bootstrap\n    import bokeh.document  # NOQA\n    import bokeh.layouts\n    import bokeh.models\n    import bokeh.models.widgets\n    import bokeh.plotting\n    import bokeh.themes\n    import tornado.gen\n    _available = True\nexcept ImportError as e:\n    _available = False\n    _import_error = e\n\nimport collections\nimport numpy as np\nimport threading\nimport time\n\nimport optuna.logging\nimport optuna.structs\nimport optuna.study\nfrom optuna import type_checking\n\nif type_checking.TYPE_CHECKING:\n    from typing import Any  # NOQA\n    from typing import Dict  # NOQA\n    from typing import List  # NOQA\n    from typing import Optional  # NOQA\n\n_mode = None  # type: Optional[str]\n_study = None  # type: Optional[optuna.study.Study]\n\n_HEADER_FORMAT = '''\n<style>\nbody {{\n    margin: 20px;\n}}\nh1, p {{\n    margin: 10px 0px;\n}}\n</style>\n\n<h1>Optuna Dashboard (Beta)</h1>\n<p>\n<b>Study name:</b> {study_name}<br>\n</p>\n'''\n\n_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'\n\nif _available:\n\n    class _CompleteTrialsWidget(object):\n        def __init__(self, trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> None\n\n            complete_trials = [\n                trial for trial in trials if trial.state == optuna.structs.TrialState.COMPLETE\n            ]\n            self.trial_ids = set([trial.trial_id for trial in complete_trials])\n\n            values = [trial.value for trial in complete_trials]\n            best_values = np.minimum.accumulate(values, axis=0)\n            self.cds = bokeh.models.ColumnDataSource({\n                '#': list(range(len(complete_trials))),\n                'value': values,\n                'best_value': best_values,\n            })\n\n            self.best_value = best_values[-1] if complete_trials else np.inf\n\n        def create_figure(self):\n            # type: () -> bokeh.plotting.Figure\n\n            figure = bokeh.plotting.figure(height=150)\n            figure.circle(x='#', y='value', source=self.cds, alpha=0.3, color='navy')\n            figure.line(x='#', y='best_value', source=self.cds, color='firebrick')\n            figure.xaxis[0].axis_label = 'Number of Trials'\n            figure.yaxis[0].axis_label = 'Objective Value'\n            return figure\n\n        def update(self, new_trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> None\n\n            stream_dict = collections.defaultdict(list)  # type: Dict[str, List[Any]]\n\n            for trial in new_trials:\n                if trial.state != optuna.structs.TrialState.COMPLETE:\n                    continue\n                if trial.trial_id in self.trial_ids:\n                    continue\n                stream_dict['#'].append(len(self.trial_ids))\n                stream_dict['value'].append(trial.value)\n                self.best_value = min(self.best_value, trial.value)\n                stream_dict['best_value'].append(self.best_value)\n                self.trial_ids.add(trial.trial_id)\n\n            if stream_dict:\n                self.cds.stream(stream_dict)\n\n    class _AllTrialsWidget(object):\n        def __init__(self, trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> None\n\n            self.cds = bokeh.models.ColumnDataSource(self.trials_to_dict(trials))\n\n        def create_table(self):\n            # type: () -> bokeh.models.widgets.DataTable\n\n            return bokeh.models.widgets.DataTable(\n                source=self.cds,\n                columns=[\n                    bokeh.models.widgets.TableColumn(field=field, title=field) for field in\n                    ['number', 'state', 'value', 'params', 'datetime_start', 'datetime_complete']\n                ])\n\n        def update(\n                self,\n                old_trials,  # type: List[optuna.structs.FrozenTrial]\n                new_trials,  # type: List[optuna.structs.FrozenTrial]\n        ):\n            # type: (...) -> None\n\n            modified_indices = []\n            modified_trials = []\n            for i, old_trial in enumerate(old_trials):\n                new_trial = new_trials[i]\n                if old_trial != new_trial:\n                    modified_indices.append(i)\n                    modified_trials.append(new_trial)\n\n            patch_dict = self.trials_to_dict(modified_trials)\n            patch_dict = {k: list(zip(modified_indices, v)) for k, v in patch_dict.items()}\n            self.cds.patch(patch_dict)\n\n            self.cds.stream(self.trials_to_dict(new_trials[len(old_trials):]))\n\n        @staticmethod\n        def trials_to_dict(trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> Dict[str, List[Any]]\n\n            return {\n                'number': [trial.number for trial in trials],\n                'state': [trial.state.name for trial in trials],\n                'value': [trial.value for trial in trials],\n                'params': [str(trial.params) for trial in trials],\n                'datetime_start': [\n                    trial.datetime_start.strftime(_DATETIME_FORMAT)\n                    if trial.datetime_start is not None else None for trial in trials\n                ],\n                'datetime_complete': [\n                    trial.datetime_complete.strftime(_DATETIME_FORMAT)\n                    if trial.datetime_complete is not None else None for trial in trials\n                ],\n            }\n\n    class _DashboardApp(object):\n        def __init__(self, study, launch_update_thread):\n            # type: (optuna.study.Study, bool) -> None\n\n            self.study = study\n            self.launch_update_thread = launch_update_thread\n            self.lock = threading.Lock()\n\n        def __call__(self, doc):\n            # type: (bokeh.document.Document) -> None\n\n            self.doc = doc\n            self.current_trials = \\\n                self.study.trials  # type: Optional[List[optuna.structs.FrozenTrial]]\n            self.new_trials = None  # type: Optional[List[optuna.structs.FrozenTrial]]\n            self.complete_trials_widget = _CompleteTrialsWidget(self.current_trials)\n            self.all_trials_widget = _AllTrialsWidget(self.current_trials)\n\n            self.doc.title = 'Optuna Dashboard (Beta)'\n            header = _HEADER_FORMAT.format(study_name=self.study.study_name)\n            self.doc.add_root(\n                bokeh.layouts.layout([[bokeh.models.widgets.Div(text=header)],\n                                      [self.complete_trials_widget.create_figure()],\n                                      [self.all_trials_widget.create_table()]],\n                                     sizing_mode='scale_width'))\n\n            if self.launch_update_thread:\n                thread = threading.Thread(target=self.thread_loop)\n                thread.daemon = True\n                thread.start()\n\n        def thread_loop(self):\n            # type: () -> None\n\n            while True:\n                time.sleep(1)\n                new_trials = self.study.trials\n                with self.lock:\n                    need_to_add_callback = (self.new_trials is None)\n                    self.new_trials = new_trials\n                    if need_to_add_callback:\n                        self.doc.add_next_tick_callback(self.update_callback)\n\n        @tornado.gen.coroutine\n        def update_callback(self):\n            # type: () -> None\n\n            with self.lock:\n                current_trials = self.current_trials\n                new_trials = self.new_trials\n                self.current_trials = self.new_trials\n                self.new_trials = None\n\n            assert current_trials is not None\n            assert new_trials is not None\n            self.complete_trials_widget.update(new_trials)\n            self.all_trials_widget.update(current_trials, new_trials)\n\n\ndef _check_bokeh_availability():\n    # type: () -> None\n\n    if not _available:\n        raise ImportError(\n            'Bokeh is not available. Please install Bokeh to use the dashboard. '\n            'Bokeh can be installed by executing `$ pip install bokeh`. '\n            'For further information, please refer to the installation guide of Bokeh. '\n            '(The actual import error is as follows: ' + str(_import_error) + ')')\n\n\ndef _show_experimental_warning():\n    # type: () -> None\n\n    logger = optuna.logging.get_logger(__name__)\n    logger.warning('Optuna dashboard is still highly experimental. Please use with caution!')\n\n\ndef _get_this_source_path():\n    # type: () -> str\n\n    path = __file__\n\n    # Sometimes __file__ points to a *.pyc file, but Bokeh doesn't accept it.\n    if path.endswith('.pyc'):\n        path = path[:-1]\n    return path\n\n\ndef serve(study, bokeh_allow_websocket_origins=None):\n    # type: (optuna.study.Study, Optional[List[str]]) -> None\n\n    global _mode, _study\n\n    _check_bokeh_availability()\n    _show_experimental_warning()\n\n    # We want to pass the mode (launching a server? or, just writing an HTML?) and a target study\n    # to our Bokeh app. Unfortunately, as we are using `bokeh.command.bootstrap.main` to launch\n    # our Bokeh app, we cannot directly pass Python objects to it. Therefore, we have no choice but\n    # to use global variables to pass them.\n    _mode = 'serve'\n    _study = study\n\n    # TODO(akiba): Stop using Bokeh's CLI entry point, and start the HTTP server by ourselves.\n\n    # This is not a very clean way to launch Bokeh server.\n    # Another seemingly better way is to\n    # instantiate and launch `bokeh.server.server.Server` by ourselves. However, in this way,\n    # for some reason, we found that the CDS update is not reflected to browsers, at least on Bokeh\n    # version 0.12.15. In addition, we will need to do many configuration to servers, which can be\n    # done automatically with the following one line. So, for now, we decided to use this way.\n    command = ['bokeh', 'serve', '--show', _get_this_source_path()]\n    if bokeh_allow_websocket_origins is not None:\n        for bokeh_allow_websocket_origin in bokeh_allow_websocket_origins:\n            command.extend(['--allow-websocket-origin', bokeh_allow_websocket_origin])\n    bokeh.command.bootstrap.main(command)\n\n\ndef write(study, out_path):\n    # type: (optuna.study.Study, str) -> None\n\n    global _mode, _study\n\n    _check_bokeh_availability()\n    _show_experimental_warning()\n\n    _mode = 'html'\n    _study = study\n    bokeh.command.bootstrap.main(['bokeh', 'html', _get_this_source_path(), '-o', out_path])\n\n\ndef _run():\n    # type: () -> None\n\n    # Please note that `_study` and `optuna.dashboard._study` are different here. Here, this module\n    # is loaded inside Bokeh, and thus it is not `optuna.dashboard`, but `bk_script_????`.\n    study = optuna.dashboard._study\n    mode = optuna.dashboard._mode\n\n    assert study is not None\n    app = _DashboardApp(study, launch_update_thread=(mode == 'serve'))\n    doc = bokeh.plotting.curdoc()\n    app(doc)\n\n\nif __name__.startswith('bk_script_'):\n    # Here, this module is loaded inside Bokeh. Therefore, we should launch the Bokeh app.\n    _run()\n", "path": "optuna/dashboard.py"}], "after_files": [{"content": "try:\n    import bokeh.command.bootstrap\n    import bokeh.document  # NOQA\n    import bokeh.layouts\n    import bokeh.models\n    import bokeh.models.widgets\n    import bokeh.plotting\n    import bokeh.themes\n    import tornado.gen\n    _available = True\nexcept ImportError as e:\n    _available = False\n    _import_error = e\n\nimport collections\nimport numpy as np\nimport threading\nimport time\n\nimport optuna.logging\nimport optuna.structs\nimport optuna.study\nfrom optuna import type_checking\n\nif type_checking.TYPE_CHECKING:\n    from typing import Any  # NOQA\n    from typing import Dict  # NOQA\n    from typing import List  # NOQA\n    from typing import Optional  # NOQA\n\n_mode = None  # type: Optional[str]\n_study = None  # type: Optional[optuna.study.Study]\n\n_HEADER_FORMAT = '''\n<style>\nbody {{\n    margin: 20px;\n}}\nh1, p {{\n    margin: 10px 0px;\n}}\n</style>\n\n<h1>Optuna Dashboard (Beta)</h1>\n<p>\n<b>Study name:</b> {study_name}<br>\n</p>\n'''\n\n_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'\n\nif _available:\n\n    class _CompleteTrialsWidget(object):\n        def __init__(self, trials, direction):\n            # type: (List[optuna.structs.FrozenTrial], optuna.structs.StudyDirection) -> None\n\n            complete_trials = [\n                trial for trial in trials if trial.state == optuna.structs.TrialState.COMPLETE\n            ]\n            self.trial_ids = set([trial.trial_id for trial in complete_trials])\n\n            self.direction = direction\n            values = [trial.value for trial in complete_trials]\n            if direction == optuna.structs.StudyDirection.MINIMIZE:\n                best_values = np.minimum.accumulate(values, axis=0)\n            else:\n                best_values = np.maximum.accumulate(values, axis=0)\n\n            self.cds = bokeh.models.ColumnDataSource({\n                '#': list(range(len(complete_trials))),\n                'value': values,\n                'best_value': best_values,\n            })\n\n            self.best_value = best_values[-1] if complete_trials else np.inf\n\n        def create_figure(self):\n            # type: () -> bokeh.plotting.Figure\n\n            figure = bokeh.plotting.figure(height=150)\n            figure.circle(x='#', y='value', source=self.cds, alpha=0.3, color='navy')\n            figure.line(x='#', y='best_value', source=self.cds, color='firebrick')\n            figure.xaxis[0].axis_label = 'Number of Trials'\n            figure.yaxis[0].axis_label = 'Objective Value'\n            return figure\n\n        def update(self, new_trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> None\n\n            stream_dict = collections.defaultdict(list)  # type: Dict[str, List[Any]]\n\n            for trial in new_trials:\n                if trial.state != optuna.structs.TrialState.COMPLETE:\n                    continue\n                if trial.trial_id in self.trial_ids:\n                    continue\n                stream_dict['#'].append(len(self.trial_ids))\n                stream_dict['value'].append(trial.value)\n                if self.direction == optuna.structs.StudyDirection.MINIMIZE:\n                    self.best_value = min(self.best_value, trial.value)\n                else:\n                    self.best_value = max(self.best_value, trial.value)\n                stream_dict['best_value'].append(self.best_value)\n                self.trial_ids.add(trial.trial_id)\n\n            if stream_dict:\n                self.cds.stream(stream_dict)\n\n    class _AllTrialsWidget(object):\n        def __init__(self, trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> None\n\n            self.cds = bokeh.models.ColumnDataSource(self.trials_to_dict(trials))\n\n        def create_table(self):\n            # type: () -> bokeh.models.widgets.DataTable\n\n            return bokeh.models.widgets.DataTable(\n                source=self.cds,\n                columns=[\n                    bokeh.models.widgets.TableColumn(field=field, title=field) for field in\n                    ['number', 'state', 'value', 'params', 'datetime_start', 'datetime_complete']\n                ])\n\n        def update(\n                self,\n                old_trials,  # type: List[optuna.structs.FrozenTrial]\n                new_trials,  # type: List[optuna.structs.FrozenTrial]\n        ):\n            # type: (...) -> None\n\n            modified_indices = []\n            modified_trials = []\n            for i, old_trial in enumerate(old_trials):\n                new_trial = new_trials[i]\n                if old_trial != new_trial:\n                    modified_indices.append(i)\n                    modified_trials.append(new_trial)\n\n            patch_dict = self.trials_to_dict(modified_trials)\n            patch_dict = {k: list(zip(modified_indices, v)) for k, v in patch_dict.items()}\n            self.cds.patch(patch_dict)\n\n            self.cds.stream(self.trials_to_dict(new_trials[len(old_trials):]))\n\n        @staticmethod\n        def trials_to_dict(trials):\n            # type: (List[optuna.structs.FrozenTrial]) -> Dict[str, List[Any]]\n\n            return {\n                'number': [trial.number for trial in trials],\n                'state': [trial.state.name for trial in trials],\n                'value': [trial.value for trial in trials],\n                'params': [str(trial.params) for trial in trials],\n                'datetime_start': [\n                    trial.datetime_start.strftime(_DATETIME_FORMAT)\n                    if trial.datetime_start is not None else None for trial in trials\n                ],\n                'datetime_complete': [\n                    trial.datetime_complete.strftime(_DATETIME_FORMAT)\n                    if trial.datetime_complete is not None else None for trial in trials\n                ],\n            }\n\n    class _DashboardApp(object):\n        def __init__(self, study, launch_update_thread):\n            # type: (optuna.study.Study, bool) -> None\n\n            self.study = study\n            self.launch_update_thread = launch_update_thread\n            self.lock = threading.Lock()\n\n        def __call__(self, doc):\n            # type: (bokeh.document.Document) -> None\n\n            self.doc = doc\n            self.current_trials = \\\n                self.study.trials  # type: Optional[List[optuna.structs.FrozenTrial]]\n            self.new_trials = None  # type: Optional[List[optuna.structs.FrozenTrial]]\n            self.complete_trials_widget = _CompleteTrialsWidget(\n                self.current_trials, self.study.direction)\n            self.all_trials_widget = _AllTrialsWidget(self.current_trials)\n\n            self.doc.title = 'Optuna Dashboard (Beta)'\n            header = _HEADER_FORMAT.format(study_name=self.study.study_name)\n            self.doc.add_root(\n                bokeh.layouts.layout([[bokeh.models.widgets.Div(text=header)],\n                                      [self.complete_trials_widget.create_figure()],\n                                      [self.all_trials_widget.create_table()]],\n                                     sizing_mode='scale_width'))\n\n            if self.launch_update_thread:\n                thread = threading.Thread(target=self.thread_loop)\n                thread.daemon = True\n                thread.start()\n\n        def thread_loop(self):\n            # type: () -> None\n\n            while True:\n                time.sleep(1)\n                new_trials = self.study.trials\n                with self.lock:\n                    need_to_add_callback = (self.new_trials is None)\n                    self.new_trials = new_trials\n                    if need_to_add_callback:\n                        self.doc.add_next_tick_callback(self.update_callback)\n\n        @tornado.gen.coroutine\n        def update_callback(self):\n            # type: () -> None\n\n            with self.lock:\n                current_trials = self.current_trials\n                new_trials = self.new_trials\n                self.current_trials = self.new_trials\n                self.new_trials = None\n\n            assert current_trials is not None\n            assert new_trials is not None\n            self.complete_trials_widget.update(new_trials)\n            self.all_trials_widget.update(current_trials, new_trials)\n\n\ndef _check_bokeh_availability():\n    # type: () -> None\n\n    if not _available:\n        raise ImportError(\n            'Bokeh is not available. Please install Bokeh to use the dashboard. '\n            'Bokeh can be installed by executing `$ pip install bokeh`. '\n            'For further information, please refer to the installation guide of Bokeh. '\n            '(The actual import error is as follows: ' + str(_import_error) + ')')\n\n\ndef _show_experimental_warning():\n    # type: () -> None\n\n    logger = optuna.logging.get_logger(__name__)\n    logger.warning('Optuna dashboard is still highly experimental. Please use with caution!')\n\n\ndef _get_this_source_path():\n    # type: () -> str\n\n    path = __file__\n\n    # Sometimes __file__ points to a *.pyc file, but Bokeh doesn't accept it.\n    if path.endswith('.pyc'):\n        path = path[:-1]\n    return path\n\n\ndef serve(study, bokeh_allow_websocket_origins=None):\n    # type: (optuna.study.Study, Optional[List[str]]) -> None\n\n    global _mode, _study\n\n    _check_bokeh_availability()\n    _show_experimental_warning()\n\n    # We want to pass the mode (launching a server? or, just writing an HTML?) and a target study\n    # to our Bokeh app. Unfortunately, as we are using `bokeh.command.bootstrap.main` to launch\n    # our Bokeh app, we cannot directly pass Python objects to it. Therefore, we have no choice but\n    # to use global variables to pass them.\n    _mode = 'serve'\n    _study = study\n\n    # TODO(akiba): Stop using Bokeh's CLI entry point, and start the HTTP server by ourselves.\n\n    # This is not a very clean way to launch Bokeh server.\n    # Another seemingly better way is to\n    # instantiate and launch `bokeh.server.server.Server` by ourselves. However, in this way,\n    # for some reason, we found that the CDS update is not reflected to browsers, at least on Bokeh\n    # version 0.12.15. In addition, we will need to do many configuration to servers, which can be\n    # done automatically with the following one line. So, for now, we decided to use this way.\n    command = ['bokeh', 'serve', '--show', _get_this_source_path()]\n    if bokeh_allow_websocket_origins is not None:\n        for bokeh_allow_websocket_origin in bokeh_allow_websocket_origins:\n            command.extend(['--allow-websocket-origin', bokeh_allow_websocket_origin])\n    bokeh.command.bootstrap.main(command)\n\n\ndef write(study, out_path):\n    # type: (optuna.study.Study, str) -> None\n\n    global _mode, _study\n\n    _check_bokeh_availability()\n    _show_experimental_warning()\n\n    _mode = 'html'\n    _study = study\n    bokeh.command.bootstrap.main(['bokeh', 'html', _get_this_source_path(), '-o', out_path])\n\n\ndef _run():\n    # type: () -> None\n\n    # Please note that `_study` and `optuna.dashboard._study` are different here. Here, this module\n    # is loaded inside Bokeh, and thus it is not `optuna.dashboard`, but `bk_script_????`.\n    study = optuna.dashboard._study\n    mode = optuna.dashboard._mode\n\n    assert study is not None\n    app = _DashboardApp(study, launch_update_thread=(mode == 'serve'))\n    doc = bokeh.plotting.curdoc()\n    app(doc)\n\n\nif __name__.startswith('bk_script_'):\n    # Here, this module is loaded inside Bokeh. Therefore, we should launch the Bokeh app.\n    _run()\n", "path": "optuna/dashboard.py"}]}