file_handler.py 1.31 KB
Newer Older
1
2
3
4
5
6
7
8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Utilities for file."""

from pathlib import Path
from datetime import datetime

9
import yaml
10
from omegaconf import OmegaConf
11

12

13
14
def create_sb_output_dir(output_dir=None):
    """Create output directory.
15

16
17
18
19
    Create output directory on filesystem, generate a new name based on current time if not provided.

    Args:
        output_dir (str): Output directory. Defaults to None.
20
21

    Returns:
22
        str: Given or generated output directory.
23
    """
24
25
26
    if not output_dir:
        output_dir = str(Path('outputs', datetime.now().strftime('%Y-%m-%d_%H-%M-%S')))
    output_path = Path(output_dir).expanduser().resolve()
27
    output_path.mkdir(mode=0o755, parents=True, exist_ok=True)
28
    return output_dir
29
30


31
def get_sb_config(config_file):
32
33
34
35
36
37
38
39
    """Read SuperBench config yaml.

    Read config file, use default config if None is provided.

    Args:
        config_file (str): config file path.

    Returns:
40
        OmegaConf: Config object, None if file does not exist.
41
    """
42
43
    default_config_file = Path(__file__).parent / '../../config/default.yaml'
    p = Path(config_file) if config_file else default_config_file
44
45
    if not p.is_file():
        return None
46
47
    with p.open() as fp:
        return OmegaConf.create(yaml.load(fp, Loader=yaml.SafeLoader))