forked from optuna/optuna-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request optuna#615 from moririn2528/settting-api
add preference feedback component api
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from typing import TYPE_CHECKING | ||
|
||
from optuna.storages import BaseStorage | ||
|
||
from .preferential._study import PreferentialStudy | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Literal | ||
|
||
OUTPUT_COMPONENT_TYPE = Literal["note", "artifact"] | ||
|
||
_SYSTEM_ATTR_FEEDBACK_COMPONENT = "preference:component" | ||
|
||
|
||
def _register_preference_feedback_component( | ||
study_id: int, | ||
storage: BaseStorage, | ||
component_type: OUTPUT_COMPONENT_TYPE, | ||
artifact_key: str | None = None, | ||
) -> None: | ||
value: dict[str, Any] = {"type": component_type} | ||
if artifact_key is not None: | ||
value["artifact_key"] = artifact_key | ||
storage.set_study_system_attr( | ||
study_id=study_id, | ||
key=_SYSTEM_ATTR_FEEDBACK_COMPONENT, | ||
value=value, | ||
) | ||
|
||
|
||
def register_preference_feedback_component( | ||
study: PreferentialStudy, | ||
component_type: OUTPUT_COMPONENT_TYPE, | ||
artifact_key: str | None = None, | ||
) -> None: | ||
"""Register a preference feedback component to the study. | ||
With this feature, you can change the component, displayed on the | ||
human feedback pages. By default, the Markdown note (``component_type="note"``) | ||
is displayed. If you specify ``component_type="artifact"``, the viewer for the | ||
specified artifact file will be displayed. | ||
Args: | ||
study: | ||
The study to register the preference feedback component. | ||
component_type: | ||
The component type, displayed on the human feedback pages | ||
(default: ``"note"``). | ||
user_attr_artifact_key: | ||
This option is required when the ``component_type`` is ``"artifact"``. | ||
The user attribute, which is specified this field, must contain the | ||
``artifact``id you want to display on the human feedback page. | ||
""" | ||
if component_type == "artifact": | ||
assert ( | ||
artifact_key is not None | ||
), "artifact_key must be specified when component_type is Artifact" | ||
|
||
_register_preference_feedback_component( | ||
study_id=study._study._study_id, | ||
storage=study._study._storage, | ||
component_type=component_type, | ||
artifact_key=artifact_key, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from unittest import TestCase | ||
|
||
import optuna | ||
from optuna_dashboard._preference_setting import _SYSTEM_ATTR_FEEDBACK_COMPONENT | ||
from optuna_dashboard._preference_setting import register_preference_feedback_component | ||
from optuna_dashboard.preferential._study import PreferentialStudy | ||
|
||
|
||
class FeedbackSettingTestCase(TestCase): | ||
def test_widget_to_dict_from_dict(self) -> None: | ||
study = PreferentialStudy(optuna.create_study()) | ||
register_preference_feedback_component(study, "artifact", "image_key") | ||
system_attrs = study._study.system_attrs | ||
feedback_type = system_attrs.get(_SYSTEM_ATTR_FEEDBACK_COMPONENT, {}) | ||
assert "type" in feedback_type | ||
assert feedback_type["type"] == "artifact" | ||
assert "artifact_key" in feedback_type | ||
assert feedback_type["artifact_key"] == "image_key" |