-
-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'optuna:master' into fix-lightGBM-argument
- Loading branch information
Showing
39 changed files
with
850 additions
and
259 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
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
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,4 @@ | ||
from optuna.artifacts._filesystem import FileSystemArtifactStore | ||
|
||
|
||
__all__ = ["FileSystemArtifactStore"] |
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,63 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import shutil | ||
from typing import TYPE_CHECKING | ||
|
||
from optuna.artifacts.exceptions import ArtifactNotFound | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import BinaryIO | ||
|
||
|
||
class FileSystemArtifactStore: | ||
"""An artifact backend for file systems. | ||
Example: | ||
.. code-block:: python | ||
import optuna | ||
from optuna_dashboard.artifact import upload_artifact | ||
from optuna_dashboard.artifact.file_system import FileSystemBackend | ||
artifact_backend = FileSystemBackend("./artifacts") | ||
def objective(trial: optuna.Trial) -> float: | ||
... = trial.suggest_float("x", -10, 10) | ||
file_path = generate_example_png(...) | ||
upload_artifact(artifact_backend, trial, file_path) | ||
return ... | ||
""" | ||
|
||
def __init__(self, base_path: str) -> None: | ||
self._base_path = base_path | ||
|
||
def open_reader(self, artifact_id: str) -> BinaryIO: | ||
filepath = os.path.join(self._base_path, artifact_id) | ||
try: | ||
f = open(filepath, "rb") | ||
except FileNotFoundError as e: | ||
raise ArtifactNotFound("not found") from e | ||
return f | ||
|
||
def write(self, artifact_id: str, content_body: BinaryIO) -> None: | ||
filepath = os.path.join(self._base_path, artifact_id) | ||
with open(filepath, "wb") as f: | ||
shutil.copyfileobj(content_body, f) | ||
|
||
def remove(self, artifact_id: str) -> None: | ||
filepath = os.path.join(self._base_path, artifact_id) | ||
try: | ||
os.remove(filepath) | ||
except FileNotFoundError as e: | ||
raise ArtifactNotFound("not found") from e | ||
|
||
|
||
if TYPE_CHECKING: | ||
# A mypy-runtime assertion to ensure that LocalArtifactBackend | ||
# implements all abstract methods in ArtifactBackendProtocol. | ||
from ._protocol import ArtifactStore | ||
|
||
_: ArtifactStore = FileSystemArtifactStore("") |
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,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
||
try: | ||
from typing import Protocol | ||
except ImportError: | ||
from typing_extensions import Protocol # type: ignore | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import BinaryIO | ||
|
||
|
||
class ArtifactStore(Protocol): | ||
"""A protocol defining the interface for an artifact backend. | ||
An artifact backend is responsible for managing the storage and retrieval | ||
of artifact data. The backend should provide methods for opening, writing | ||
and removing artifacts. | ||
""" | ||
|
||
def open_reader(self, artifact_id: str) -> BinaryIO: | ||
"""Open the artifact identified by the artifact_id. | ||
This method should return a binary file-like object in read mode, similar to | ||
``open(..., mode="rb")``. If the artifact does not exist, an | ||
:exc:`~optuna_dashboard.artifact.exceptions.ArtifactNotFound` exception | ||
should be raised. | ||
Args: | ||
artifact_id: The identifier of the artifact to open. | ||
Returns: | ||
BinaryIO: A binary file-like object that can be read from. | ||
""" | ||
... | ||
|
||
def write(self, artifact_id: str, content_body: BinaryIO) -> None: | ||
"""Save the content to the backend. | ||
Args: | ||
artifact_id: The identifier of the artifact to write to. | ||
content_body: The content to write to the artifact. | ||
""" | ||
... | ||
|
||
def remove(self, artifact_id: str) -> None: | ||
"""Remove the artifact identified by the artifact_id. | ||
This method should delete the artifact from the backend. If the artifact does not | ||
exist, an :exc:`~optuna_dashboard.artifact.exceptions.ArtifactNotFound` exception | ||
may be raised. | ||
Args: | ||
artifact_id: The identifier of the artifact to remove. | ||
""" | ||
... |
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,12 @@ | ||
from optuna.exceptions import OptunaError | ||
|
||
|
||
class ArtifactNotFound(OptunaError): | ||
"""Exception raised when an artifact is not found. | ||
It is typically raised while calling | ||
:meth:`~optuna_dashboard.artifact.protocol.ArtifactBackend.open` or | ||
:meth:`~optuna_dashboard.artifact.protocol.ArtifactBackend.remove` methods. | ||
""" | ||
|
||
... |
Oops, something went wrong.