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

Keep same extension when created tmp config file #885

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions src/orion/core/worker/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ def __call__(self, trial, **kwargs):
True if the trial was successfully executed. False if the trial is broken.

"""
log.debug("Consumer context: %s", trial.working_dir)
os.makedirs(trial.working_dir, exist_ok=True)

results_file = self._consume(trial, trial.working_dir)

log.debug("Parsing results from file and fill corresponding Trial object.")
Expand Down Expand Up @@ -197,9 +194,16 @@ def get_execution_environment(self, trial, results_file="results.log"):

return env

def _consume(self, trial, workdirname):
def _prepare_config(self, trial, workdirname):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I know your stance is to add type hints to new modules, but I think new methods might also be a good opportunity to add type hints.

log.debug("Consumer context: %s", trial.working_dir)
os.makedirs(trial.working_dir, exist_ok=True)

if self.template_builder.file_config_path:
_, suffix = os.path.splitext(self.template_builder.file_config_path)
else:
suffix = ".conf"
config_file = tempfile.NamedTemporaryFile(
mode="w", prefix="trial_", suffix=".conf", dir=workdirname, delete=False
mode="w", prefix="trial_", suffix=suffix, dir=workdirname, delete=False
)
config_file.close()
log.debug("New temp config file: %s", config_file.name)
Expand All @@ -209,6 +213,10 @@ def _consume(self, trial, workdirname):
results_file.close()
log.debug("New temp results file: %s", results_file.name)

return config_file, results_file

def _consume(self, trial, workdirname):
config_file, results_file = self._prepare_config(trial, workdirname)
log.debug("Building command line argument and configuration for trial.")
env = self.get_execution_environment(trial, results_file.name)
cmd_args = self.template_builder.format(
Expand Down
34 changes: 34 additions & 0 deletions tests/unittests/core/worker/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ def test_trial_working_dir_is_created(config):
shutil.rmtree(trial.working_dir)


@pytest.mark.usefixtures("storage")
@pytest.mark.parametrize(
"config_path_name",
["yaml_sample_path", "json_sample_path", "unknown_type_template_path"],
)
def test_trial_config_file_is_created_with_correct_ext(
config, config_path_name, request
):
"""Check that trial config file is created with correct extension."""
config_path = request.getfixturevalue(config_path_name)
config["metadata"]["user_args"].insert(1, f"--config={config_path}")
backward.populate_space(config)
exp = experiment_builder.build(**config)

trial = exp.space.sample()[0]

exp.register_trial(trial, status="reserved")

assert not os.path.exists(trial.working_dir)

con = Consumer(exp)
config_file, results_file = con._prepare_config(trial, trial.working_dir)

assert os.path.exists(trial.working_dir)
assert os.path.exists(config_file.name)

_, original_ext = os.path.splitext(config_path)
_, tmp_ext = os.path.splitext(config_file.name)

assert original_ext == tmp_ext

shutil.rmtree(trial.working_dir)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it necessary to do this? Shouldn't the working directory in the config always be based on the temp_path fixture?

Copy link
Member Author

Choose a reason for hiding this comment

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



def setup_code_change_mock(config, monkeypatch, ignore_code_changes):
"""Mock create experiment and trials, and infer_versioning_metadata"""
exp = experiment_builder.build(**config)
Expand Down