Skip to content
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

Merged
merged 16 commits into from
Aug 25, 2023

Conversation

Alnusjaponica
Copy link
Collaborator

@Alnusjaponica Alnusjaponica commented Aug 17, 2023

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.

  • I agree this patch may be ported to Goptuna by other Goptuna contributors.

Reference Issues/PRs

What does this implement/fix? Explain your changes.

@c-bata c-bata self-assigned this Aug 17, 2023
@Alnusjaponica Alnusjaponica changed the title Deprecate artifact Store artifact meta in trial_system_attr Aug 17, 2023
@Alnusjaponica Alnusjaponica marked this pull request as ready for review August 18, 2023 02:06
Copy link
Member

@keisuke-umezawa keisuke-umezawa left a 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?

Copy link
Member

@c-bata c-bata left a 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.

optuna_dashboard/artifact/_backend.py Outdated Show resolved Hide resolved
Comment on lines 19 to 62
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
Copy link
Member

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?

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

Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@c-bata c-bata merged commit 273d4fe into optuna:main Aug 25, 2023
12 checks passed
@Alnusjaponica Alnusjaponica deleted the deprecate-artifact branch August 25, 2023 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants