-
-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store artifact meta in trial_system_attr
#564
Conversation
trial_system_attr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some comments. Could you check them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your pull request. I left some comments.
def init_storage_with_artifact_meta() -> MagicMock: | ||
storage = MagicMock() | ||
|
||
get_study_system_attrs = ( | ||
lambda study_id: { | ||
"dashboard:artifacts:0:id0": '{"artifact_id": "id0", "filename": "foo.txt"}', | ||
"dashboard:artifacts:0:id1": '{"artifact_id": "id1", "filename": "bar.txt"}', | ||
"baz": "baz", | ||
} | ||
if study_id == 0 | ||
else {} | ||
) | ||
storage.get_study_system_attrs.side_effect = get_study_system_attrs | ||
|
||
trial0_system_attrs = { | ||
"artifacts:id2": '{"artifact_id": "id2", "filename": "baz.txt"}', | ||
} | ||
trial1_system_attrs = { | ||
"artifacts:id3": '{"artifact_id": "id3", "filename": "qux.txt"}', | ||
} | ||
get_trial_system_attrs = ( | ||
lambda trial_id: trial0_system_attrs | ||
if trial_id == 0 | ||
else trial1_system_attrs | ||
if trial_id == 1 | ||
else {} | ||
) | ||
storage.get_trial_system_attrs.side_effect = get_trial_system_attrs | ||
|
||
get_all_trials = ( | ||
lambda study_id: [ | ||
MagicMock( | ||
_trial_id=0, study=MagicMock(_study_id=study_id), system_attrs=trial0_system_attrs | ||
), | ||
MagicMock( | ||
_trial_id=1, study=MagicMock(_study_id=study_id), system_attrs=trial1_system_attrs | ||
), | ||
] | ||
if study_id == 0 | ||
else [] | ||
) | ||
storage.get_all_trials.side_effect = get_all_trials | ||
|
||
return storage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overusing MagicMock
can decrease the readability and make tests more fragile, increasing maintenance costs. How about creating an actual InMemoryStorage
object instead of using a Mock like the test case below?
optuna-dashboard/python_tests/artifact/test_optuna_compatibility.py
Lines 19 to 53 in f125930
def test_list_optuna_trial_artifacts() -> None: | |
from optuna.artifacts import FileSystemArtifactStore | |
from optuna.artifacts import upload_artifact | |
storage = optuna.storages.InMemoryStorage() | |
study = optuna.create_study(storage=storage) | |
dummy_content = b"dummy content" | |
with tempfile.TemporaryDirectory() as tmpdir: | |
artifact_store = FileSystemArtifactStore(tmpdir) | |
trial = study.ask() | |
with tempfile.NamedTemporaryFile() as f: | |
f.write(dummy_content) | |
f.flush() | |
upload_artifact(trial, f.name, artifact_store=artifact_store) | |
study.tell(trial, 0.0) | |
study_system_attrs = storage.get_study_system_attrs(study._study_id) | |
frozen_trial = storage.get_trial(trial._trial_id) | |
artifact_meta_list = list_trial_artifacts(study_system_attrs, frozen_trial) | |
assert len(artifact_meta_list) == 1 | |
artifact_id = artifact_meta_list[0]["artifact_id"] | |
with artifact_store.open_reader(artifact_id) as reader: | |
assert reader.read() == dummy_content | |
artifact_meta = get_artifact_meta( | |
storage=storage, | |
study_id=study._study_id, | |
trial_id=trial._trial_id, | |
artifact_id=artifact_id, | |
) | |
assert artifact_meta is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Contributor License Agreement
This repository (
optuna-dashboard
) and Goptuna share common code.This pull request may therefore be ported to Goptuna.
Make sure that you understand the consequences concerning licenses and check the box below if you accept the term before creating this pull request.
Reference Issues/PRs
What does this implement/fix? Explain your changes.