mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-10 12:23:22 +08:00
Add Python API to save the note
This commit is contained in:
@@ -88,6 +88,14 @@ This function uses wsgiref module which is not intended for the production use.
|
||||
This function exposes WSGI interface for people who want to run on the
|
||||
production-class WSGI servers like Gunicorn or uWSGI.
|
||||
|
||||
**`save_study_note(study: Study, body: string) -> None`**
|
||||
|
||||
Save the note (Markdown format) to the Study.
|
||||
|
||||
**`save_trial_note(trial: Trial, body: string) -> None`**
|
||||
|
||||
Save the note (Markdown format) to the Trial.
|
||||
|
||||
</details>
|
||||
|
||||
## Using an official Docker image
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from ._app import run_server # noqa
|
||||
from ._app import wsgi # noqa
|
||||
from ._note import save_study_note # noqa
|
||||
from ._note import save_trial_note # noqa
|
||||
|
||||
|
||||
__version__ = "0.9.0b1"
|
||||
|
||||
@@ -4,6 +4,7 @@ import math
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import optuna
|
||||
from optuna.storages import BaseStorage
|
||||
|
||||
|
||||
@@ -22,6 +23,67 @@ if TYPE_CHECKING:
|
||||
SYSTEM_ATTR_MAX_LENGTH = 2045
|
||||
|
||||
|
||||
def save_study_note(study: optuna.Study, body: str) -> None:
|
||||
"""Save the note (Markdown format) to the Study.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import optuna
|
||||
from optuna_dashboard import save_study_note
|
||||
|
||||
study = optuna.create_study()
|
||||
|
||||
note = textwrap.dedent('''\
|
||||
## Hello
|
||||
|
||||
You can *freely* take a **note** that is associated with the study.
|
||||
''')
|
||||
save_study_note(study, note)
|
||||
|
||||
"""
|
||||
storage = study._storage
|
||||
study_id = study._study_id
|
||||
system_attrs = storage.get_study_system_attrs(study_id)
|
||||
next_ver = system_attrs.get(note_ver_key(None), 0) + 1
|
||||
save_note(storage, study_id, None, next_ver, body)
|
||||
|
||||
|
||||
def save_trial_note(trial: optuna.Trial, body: str) -> None:
|
||||
"""Save the note (Markdown format) to the Trial.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import optuna
|
||||
import textwrap
|
||||
from optuna_dashboard import save_trial_note
|
||||
|
||||
def objective_single(trial: optuna.Trial) -> float:
|
||||
x1 = trial.suggest_float("x1", 0, 10)
|
||||
x2 = trial.suggest_float("x2", 0, 10)
|
||||
|
||||
note = textwrap.dedent(f'''\
|
||||
## Trial {trial._trial_id}
|
||||
|
||||
$$
|
||||
y = (x1 - 2)^{{2}} + (x2 - 5)^{{2}} = ({x1} - 2)^{{2}} + ({x2} - 5)^{{2}}
|
||||
$$
|
||||
''')
|
||||
save_trial_note(trial, note)
|
||||
return (x1 - 2) ** 2 + (x2 - 5) ** 2
|
||||
"""
|
||||
storage = trial.storage
|
||||
trial_id = trial._trial_id
|
||||
study_id = trial.study._study_id
|
||||
|
||||
system_attrs = storage.get_study_system_attrs(study_id)
|
||||
next_ver = system_attrs.get(note_ver_key(trial_id), 0) + 1
|
||||
save_note(storage, study_id, trial_id, next_ver, body)
|
||||
|
||||
|
||||
def note_ver_key(trial_id: Optional[int]) -> str:
|
||||
prefix = "dashboard:note_ver"
|
||||
if trial_id is None:
|
||||
|
||||
Reference in New Issue
Block a user