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

fix: Return the experiment path instead of the experiment #76

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions niceml/experiments/experimenterrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ class ExperimentNotFoundError(Exception):
"""Error when the path doesn't contain an experiment"""


class MultipleExperimentsFoundError(Exception):
"""Error when the path contain multiple experiment with a given id"""


class AmbigousFilenameError(Exception):
"""Filename can be interpreted in multiple ways"""
9 changes: 6 additions & 3 deletions niceml/experiments/exppathfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from typing import List

from niceml.data.storages.fsspecstorage import FSSpecStorage
from niceml.experiments.experimenterrors import ExperimentNotFoundError
from niceml.experiments.experimenterrors import (
ExperimentNotFoundError,
MultipleExperimentsFoundError,
)
from niceml.experiments.experimentinfo import ExperimentInfo
from niceml.utilities.fsspec.locationutils import LocationConfig

Expand All @@ -21,7 +24,7 @@ def get_exp_filepath(fs_path_config: LocationConfig, exp_id: str):
f"Experiment with id: {exp_id} not found in path: {fs_path_config.uri}"
)
if len(exps_w_id) > 1:
raise ExperimentNotFoundError(
raise MultipleExperimentsFoundError(
f"Multiple experiments with id: {exp_id} found in path: {fs_path_config.uri}"
)
return exps_w_id[0]
return exps_w_id[0].exp_filepath
76 changes: 76 additions & 0 deletions tests/unit/niceml/experiments/test_exppathfinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import datetime
from typing import Union

import pytest

from niceml.experiments.experimenterrors import (
MultipleExperimentsFoundError,
ExperimentNotFoundError,
)
from niceml.experiments.experimentinfo import ExperimentInfo
from niceml.experiments.expfilenames import ExperimentFilenames
from niceml.experiments.exppathfinder import get_exp_filepath
from niceml.utilities.fsspec.locationutils import (
LocationConfig,
open_location,
join_fs_path,
join_location_w_path,
)
from niceml.utilities.ioutils import write_yaml


@pytest.fixture
def exp_location(
tmp_dir,
) -> LocationConfig:
location_config = LocationConfig(uri=tmp_dir)
exp_ids = ["abcd", "efgh", "efgh"]
test_prefix = "TEST"
with open_location(location_config) as (exps_fs, exps_root):
for exp_id in exp_ids:
date = datetime.datetime.utcnow()
date_string = date.strftime("%Y-%m-%dT%H.%M.%S.%fZ")
exp_filepath = join_fs_path(
exps_fs, exps_root, f"{test_prefix}-{date_string}-id_{exp_id}"
)
exps_fs.mkdir(exp_filepath)
exp_info = ExperimentInfo(
experiment_prefix=test_prefix,
experiment_name="test",
experiment_type="",
run_id=date_string,
short_id=exp_id,
environment={},
description="",
exp_dir="",
)
with open_location(join_location_w_path(location_config, exp_filepath)) as (
exp_fs,
exp_root,
):
write_yaml(
data=exp_info.as_save_dict(),
filepath=join_fs_path(
exp_fs, exp_root, ExperimentFilenames.EXP_INFO
),
)

return location_config


@pytest.mark.parametrize(
"exp_id,expected",
[
("abcd", "abcd"),
("efgh", MultipleExperimentsFoundError),
("ijkl", ExperimentNotFoundError),
],
)
def test_get_exp_filepath(exp_location, exp_id: str, expected: Union[str, Exception]):
if expected in [MultipleExperimentsFoundError, ExperimentNotFoundError]:
with pytest.raises(expected):
get_exp_filepath(fs_path_config=exp_location, exp_id=exp_id)
else:
exp_filepath = get_exp_filepath(fs_path_config=exp_location, exp_id=exp_id)

assert expected == exp_filepath[-4:]