prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Use OrderedDict to keep the order of intermediate values.
  This PR addresses #886. 
  
  I think it has two issues.
  
  1. `dict` does not keep the order of keys
  
  The current implementation of `FrozenTrial.intermediate_values` employs `dict`. The order of keys is not ensured if the users use Python 3.6 or older. This PR uses `OrderedDict` for `FrozenTrial.intermediate_values`.
  
  2. RDBs do not ensure the order of results if a query has no `order by` clause.
  
  This PR arranges the intermediate values when they are retrieved from databases.
  Please note that the results are not necessarily ordered by primary keys when queries do not contain `order by`. (c.f., ['When no 'Order by' is specified, what order does a query choose for your record set?'](https://stackoverflow.com/questions/20050341/when-no-order-by-is-specified-what-order-does-a-query-choose-for-your-record))
  
  --- 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/visualization/intermediate_values.py`
  Content:
  ```
  1 from optuna.logging import get_logger
  2 from optuna.structs import TrialState
  3 from optuna import type_checking
  4 from optuna.visualization.utils import _check_plotly_availability
  5 from optuna.visualization.utils import is_available
  6 
  7 if type_checking.TYPE_CHECKING:
  8     from optuna.study import Study  # NOQA
  9 
  10 if is_available():
  11     from optuna.visualization.plotly_imports import go
  12 
  13 logger = get_logger(__name__)
  14 
  15 
  16 def plot_intermediate_values(study):
  17     # type: (Study) -> go.Figure
  18     """Plot intermediate values of all trials in a study.
  19 
  20     Example:
  21 
  22         The following code snippet shows how to plot intermediate values.
  23 
  24         .. code::
  25 
  26             import optuna
  27 
  28             def objective(trial):
  29                 # Intermediate values are supposed to be reported inside the objective function.
  30                 ...
  31 
  32             study = optuna.create_study()
  33             study.optimize(objective, n_trials=100)
  34 
  35             optuna.visualization.plot_intermediate_values(study)
  36 
  37     Args:
  38         study:
  39             A :class:`~optuna.study.Study` object whose trials are plotted for their intermediate
  40             values.
  41 
  42     Returns:
  43         A :class:`plotly.graph_objs.Figure` object.
  44     """
  45 
  46     _check_plotly_availability()
  47     return _get_intermediate_plot(study)
  48 
  49 
  50 def _get_intermediate_plot(study):
  51     # type: (Study) -> go.Figure
  52 
  53     layout = go.Layout(
  54         title='Intermediate Values Plot',
  55         xaxis={'title': 'Step'},
  56         yaxis={'title': 'Intermediate Value'},
  57         showlegend=False
  58     )
  59 
  60     target_state = [TrialState.PRUNED, TrialState.COMPLETE, TrialState.RUNNING]
  61     trials = [trial for trial in study.trials if trial.state in target_state]
  62 
  63     if len(trials) == 0:
  64         logger.warning('Study instance does not contain trials.')
  65         return go.Figure(data=[], layout=layout)
  66 
  67     traces = []
  68     for trial in trials:
  69         if trial.intermediate_values:
  70             trace = go.Scatter(
  71                 x=tuple(trial.intermediate_values.keys()),
  72                 y=tuple(trial.intermediate_values.values()),
  73                 mode='lines+markers',
  74                 marker={
  75                     'maxdisplayed': 10
  76                 },
  77                 name='Trial{}'.format(trial.number)
  78             )
  79             traces.append(trace)
  80 
  81     if not traces:
  82         logger.warning(
  83             'You need to set up the pruning feature to utilize `plot_intermediate_values()`')
  84         return go.Figure(data=[], layout=layout)
  85 
  86     figure = go.Figure(data=traces, layout=layout)
  87 
  88     return figure
  89
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/optuna/visualization/intermediate_values.py b/optuna/visualization/intermediate_values.py
  --- a/optuna/visualization/intermediate_values.py
  +++ b/optuna/visualization/intermediate_values.py
  @@ -67,9 +67,10 @@
       traces = []
       for trial in trials:
           if trial.intermediate_values:
  +            sorted_intermediate_values = sorted(trial.intermediate_values.items())
               trace = go.Scatter(
  -                x=tuple(trial.intermediate_values.keys()),
  -                y=tuple(trial.intermediate_values.values()),
  +                x=tuple((x for x, _ in sorted_intermediate_values)),
  +                y=tuple((y for _, y in sorted_intermediate_values)),
                   mode='lines+markers',
                   marker={
                       'maxdisplayed': 10
  
verification_info: {"golden_diff": "diff --git a/optuna/visualization/intermediate_values.py b/optuna/visualization/intermediate_values.py\n--- a/optuna/visualization/intermediate_values.py\n+++ b/optuna/visualization/intermediate_values.py\n@@ -67,9 +67,10 @@\n     traces = []\n     for trial in trials:\n         if trial.intermediate_values:\n+            sorted_intermediate_values = sorted(trial.intermediate_values.items())\n             trace = go.Scatter(\n-                x=tuple(trial.intermediate_values.keys()),\n-                y=tuple(trial.intermediate_values.values()),\n+                x=tuple((x for x, _ in sorted_intermediate_values)),\n+                y=tuple((y for _, y in sorted_intermediate_values)),\n                 mode='lines+markers',\n                 marker={\n                     'maxdisplayed': 10\n", "issue": "Use OrderedDict to keep the order of intermediate values.\nThis PR addresses #886. \r\n\r\nI think it has two issues.\r\n\r\n1. `dict` does not keep the order of keys\r\n\r\nThe current implementation of `FrozenTrial.intermediate_values` employs `dict`. The order of keys is not ensured if the users use Python 3.6 or older. This PR uses `OrderedDict` for `FrozenTrial.intermediate_values`.\r\n\r\n2. RDBs do not ensure the order of results if a query has no `order by` clause.\r\n\r\nThis PR arranges the intermediate values when they are retrieved from databases.\r\nPlease note that the results are not necessarily ordered by primary keys when queries do not contain `order by`. (c.f., ['When no 'Order by' is specified, what order does a query choose for your record set?'](https://stackoverflow.com/questions/20050341/when-no-order-by-is-specified-what-order-does-a-query-choose-for-your-record))\n", "before_files": [{"content": "from optuna.logging import get_logger\nfrom optuna.structs import TrialState\nfrom optuna import type_checking\nfrom optuna.visualization.utils import _check_plotly_availability\nfrom optuna.visualization.utils import is_available\n\nif type_checking.TYPE_CHECKING:\n    from optuna.study import Study  # NOQA\n\nif is_available():\n    from optuna.visualization.plotly_imports import go\n\nlogger = get_logger(__name__)\n\n\ndef plot_intermediate_values(study):\n    # type: (Study) -> go.Figure\n    \"\"\"Plot intermediate values of all trials in a study.\n\n    Example:\n\n        The following code snippet shows how to plot intermediate values.\n\n        .. code::\n\n            import optuna\n\n            def objective(trial):\n                # Intermediate values are supposed to be reported inside the objective function.\n                ...\n\n            study = optuna.create_study()\n            study.optimize(objective, n_trials=100)\n\n            optuna.visualization.plot_intermediate_values(study)\n\n    Args:\n        study:\n            A :class:`~optuna.study.Study` object whose trials are plotted for their intermediate\n            values.\n\n    Returns:\n        A :class:`plotly.graph_objs.Figure` object.\n    \"\"\"\n\n    _check_plotly_availability()\n    return _get_intermediate_plot(study)\n\n\ndef _get_intermediate_plot(study):\n    # type: (Study) -> go.Figure\n\n    layout = go.Layout(\n        title='Intermediate Values Plot',\n        xaxis={'title': 'Step'},\n        yaxis={'title': 'Intermediate Value'},\n        showlegend=False\n    )\n\n    target_state = [TrialState.PRUNED, TrialState.COMPLETE, TrialState.RUNNING]\n    trials = [trial for trial in study.trials if trial.state in target_state]\n\n    if len(trials) == 0:\n        logger.warning('Study instance does not contain trials.')\n        return go.Figure(data=[], layout=layout)\n\n    traces = []\n    for trial in trials:\n        if trial.intermediate_values:\n            trace = go.Scatter(\n                x=tuple(trial.intermediate_values.keys()),\n                y=tuple(trial.intermediate_values.values()),\n                mode='lines+markers',\n                marker={\n                    'maxdisplayed': 10\n                },\n                name='Trial{}'.format(trial.number)\n            )\n            traces.append(trace)\n\n    if not traces:\n        logger.warning(\n            'You need to set up the pruning feature to utilize `plot_intermediate_values()`')\n        return go.Figure(data=[], layout=layout)\n\n    figure = go.Figure(data=traces, layout=layout)\n\n    return figure\n", "path": "optuna/visualization/intermediate_values.py"}], "after_files": [{"content": "from optuna.logging import get_logger\nfrom optuna.structs import TrialState\nfrom optuna import type_checking\nfrom optuna.visualization.utils import _check_plotly_availability\nfrom optuna.visualization.utils import is_available\n\nif type_checking.TYPE_CHECKING:\n    from optuna.study import Study  # NOQA\n\nif is_available():\n    from optuna.visualization.plotly_imports import go\n\nlogger = get_logger(__name__)\n\n\ndef plot_intermediate_values(study):\n    # type: (Study) -> go.Figure\n    \"\"\"Plot intermediate values of all trials in a study.\n\n    Example:\n\n        The following code snippet shows how to plot intermediate values.\n\n        .. code::\n\n            import optuna\n\n            def objective(trial):\n                # Intermediate values are supposed to be reported inside the objective function.\n                ...\n\n            study = optuna.create_study()\n            study.optimize(objective, n_trials=100)\n\n            optuna.visualization.plot_intermediate_values(study)\n\n    Args:\n        study:\n            A :class:`~optuna.study.Study` object whose trials are plotted for their intermediate\n            values.\n\n    Returns:\n        A :class:`plotly.graph_objs.Figure` object.\n    \"\"\"\n\n    _check_plotly_availability()\n    return _get_intermediate_plot(study)\n\n\ndef _get_intermediate_plot(study):\n    # type: (Study) -> go.Figure\n\n    layout = go.Layout(\n        title='Intermediate Values Plot',\n        xaxis={'title': 'Step'},\n        yaxis={'title': 'Intermediate Value'},\n        showlegend=False\n    )\n\n    target_state = [TrialState.PRUNED, TrialState.COMPLETE, TrialState.RUNNING]\n    trials = [trial for trial in study.trials if trial.state in target_state]\n\n    if len(trials) == 0:\n        logger.warning('Study instance does not contain trials.')\n        return go.Figure(data=[], layout=layout)\n\n    traces = []\n    for trial in trials:\n        if trial.intermediate_values:\n            sorted_intermediate_values = sorted(trial.intermediate_values.items())\n            trace = go.Scatter(\n                x=tuple((x for x, _ in sorted_intermediate_values)),\n                y=tuple((y for _, y in sorted_intermediate_values)),\n                mode='lines+markers',\n                marker={\n                    'maxdisplayed': 10\n                },\n                name='Trial{}'.format(trial.number)\n            )\n            traces.append(trace)\n\n    if not traces:\n        logger.warning(\n            'You need to set up the pruning feature to utilize `plot_intermediate_values()`')\n        return go.Figure(data=[], layout=layout)\n\n    figure = go.Figure(data=traces, layout=layout)\n\n    return figure\n", "path": "optuna/visualization/intermediate_values.py"}]}