From af251a19c623dc371c959c75e823ef11aeeccdf2 Mon Sep 17 00:00:00 2001 From: Dmitrii Agibov Date: Wed, 15 Feb 2023 09:08:14 +0000 Subject: MLIA-813 Change default output directory - If no output directory provided then create one in the current working directory - Update documentation and tests Change-Id: Id1f2acef14e1bd2edffbfa6139a961a5c5018211 --- src/mlia/cli/options.py | 4 ++-- src/mlia/core/context.py | 35 ++++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src/mlia') diff --git a/src/mlia/cli/options.py b/src/mlia/cli/options.py index b16f77f..d40aa88 100644 --- a/src/mlia/cli/options.py +++ b/src/mlia/cli/options.py @@ -233,8 +233,8 @@ def add_output_directory(parser: argparse.ArgumentParser) -> None: type=Path, help="Path to the directory where MLIA " "stores artifacts, e.g. logs, target profiles and model files. " - "If not specified then MLIA will use temporary " - "directory instead.", + "If not specified then MLIA will use " + "directory 'mlia-output' in the current working directory instead.", ) diff --git a/src/mlia/core/context.py b/src/mlia/core/context.py index c0267f1..e8c8a2c 100644 --- a/src/mlia/core/context.py +++ b/src/mlia/core/context.py @@ -10,9 +10,9 @@ parameters). from __future__ import annotations import logging -import tempfile from abc import ABC from abc import abstractmethod +from datetime import datetime from pathlib import Path from typing import Any from typing import Mapping @@ -97,6 +97,18 @@ class Context(ABC): self.event_publisher.register_event_handlers(self.event_handlers) +def create_output_dir_with_timestamp() -> Path: + """Generate output directory for the MLIA.""" + base_dir = Path.cwd() / "mlia-output" + base_dir.mkdir(exist_ok=True) + + timestamp = datetime.now().isoformat().replace(":", "") + output_dir = base_dir / f"mlia-output-{timestamp}" + output_dir.mkdir() + + return output_dir + + class ExecutionContext(Context): """Execution context.""" @@ -120,7 +132,7 @@ class ExecutionContext(Context): :param config_parameters: dictionary like object with input parameters :param output_dir: path to the directory that will be used as a place to store temporary files, logs, models. If not provided then - temporary directory will be used instead + autogenerated directory will be used instead :param event_handlers: optional list of event handlers :param event_publisher: optional event publisher instance. If not provided then default implementation of event publisher will be used @@ -131,15 +143,12 @@ class ExecutionContext(Context): temporary models will be stored :param action_resolver: instance of the action resolver that could make advice actionable + :param output_format: format for the application output """ self._advice_category = advice_category or {AdviceCategory.COMPATIBILITY} self._config_parameters = config_parameters - if output_dir: - self._output_dir_path = Path(output_dir) - self._output_dir_path.mkdir(exist_ok=True) - else: - self._output_dir_path = generate_temp_output_dir() + self._init_output_directory(output_dir) self._event_handlers = event_handlers self._event_publisher = event_publisher or DefaultEventPublisher() @@ -239,8 +248,12 @@ class ExecutionContext(Context): f"output_format={self.output_format}" ) + def _init_output_directory(self, output_dir: str | Path | None) -> None: + """Init output directory for the execution.""" + if output_dir: + output_dir_path = Path(output_dir) + output_dir_path.mkdir(exist_ok=True) + else: + output_dir_path = create_output_dir_with_timestamp() -def generate_temp_output_dir() -> Path: - """Generate a temporary output dir and returns the path.""" - output_dir = tempfile.mkdtemp(suffix=None, prefix="mlia-", dir=None) - return Path(output_dir) + self._output_dir_path = output_dir_path -- cgit v1.2.1